1 package org.cacert.gigi.ping;
3 import static org.junit.Assert.*;
4 import static org.junit.Assume.*;
6 import java.io.IOException;
7 import java.net.Socket;
9 import java.net.URLEncoder;
10 import java.security.GeneralSecurityException;
11 import java.security.KeyManagementException;
12 import java.security.KeyPair;
13 import java.security.NoSuchAlgorithmException;
14 import java.security.Principal;
15 import java.security.PrivateKey;
16 import java.security.SecureRandom;
17 import java.security.cert.CertificateException;
18 import java.security.cert.X509Certificate;
19 import java.sql.SQLException;
20 import java.util.regex.Matcher;
21 import java.util.regex.Pattern;
23 import javax.net.ssl.KeyManager;
24 import javax.net.ssl.SSLContext;
25 import javax.net.ssl.SSLServerSocket;
26 import javax.net.ssl.SSLServerSocketFactory;
27 import javax.net.ssl.TrustManager;
28 import javax.net.ssl.X509KeyManager;
29 import javax.net.ssl.X509TrustManager;
31 import org.cacert.gigi.GigiApiException;
32 import org.cacert.gigi.dbObjects.Certificate;
33 import org.cacert.gigi.dbObjects.Certificate.CSRType;
34 import org.cacert.gigi.dbObjects.CertificateProfile;
35 import org.cacert.gigi.dbObjects.User;
36 import org.cacert.gigi.pages.account.domain.DomainOverview;
37 import org.cacert.gigi.testUtils.IOUtils;
38 import org.cacert.gigi.testUtils.PingTest;
39 import org.cacert.gigi.testUtils.TestEmailReciever.TestMail;
40 import org.junit.Assert;
41 import org.junit.Test;
43 public class TestSSL extends PingTest {
47 private Certificate c;
49 @Test(timeout = 70000)
50 public void sslAndMailSuccess() throws IOException, InterruptedException, SQLException, GeneralSecurityException, GigiApiException {
51 testEmailAndSSL(0, 0, true);
54 @Test(timeout = 70000)
55 public void sslWongTypeAndMailSuccess() throws IOException, InterruptedException, SQLException, GeneralSecurityException, GigiApiException {
56 testEmailAndSSL(1, 0, true);
59 @Test(timeout = 70000)
60 public void sslOneMissingAndMailSuccess() throws IOException, InterruptedException, SQLException, GeneralSecurityException, GigiApiException {
61 testEmailAndSSL(2, 0, true);
64 @Test(timeout = 70000)
65 public void sslBothMissingAndMailSuccess() throws IOException, InterruptedException, SQLException, GeneralSecurityException, GigiApiException {
66 testEmailAndSSL(3, 0, true);
69 @Test(timeout = 70000)
70 public void sslWrongTypeAndMailFail() throws IOException, InterruptedException, SQLException, GeneralSecurityException, GigiApiException {
71 testEmailAndSSL(1, 1, false);
77 * <li>0= all valid</li>
78 * <li>1= wrong type</li>
79 * <li>2= one server missing</li>
80 * <li>3= both servers missing</li>
86 * @throws InterruptedException
87 * @throws SQLException
88 * @throws GeneralSecurityException
89 * @throws GigiApiException
92 private void testEmailAndSSL(int sslVariant, int emailVariant, boolean successMail) throws IOException, InterruptedException, SQLException, GeneralSecurityException, GigiApiException {
93 String test = getTestProps().getProperty("domain.local");
95 URL u = new URL("https://" + getServerName() + DomainOverview.PATH);
97 initailizeDomainForm(u);
99 createCertificate(test, CertificateProfile.getByName(sslVariant == 1 ? "client" : "server"));
100 SSLServerSocket sss = createSSLServer(kp.getPrivate(), c.cert());
101 int port = sss.getLocalPort();
102 SSLServerSocket sss2 = createSSLServer(kp.getPrivate(), c.cert());
103 int port2 = sss2.getLocalPort();
104 if (sslVariant == 3 || sslVariant == 2) {
106 if (sslVariant == 3) {
110 String content = "newdomain=" + URLEncoder.encode(test, "UTF-8") + //
111 "&emailType=y&email=2&SSLType=y" + //
112 "&ssl-type-0=direct&ssl-port-0=" + port + //
113 "&ssl-type-1=direct&ssl-port-1=" + port2 + //
114 "&ssl-type-2=direct&ssl-port-2=" + //
115 "&ssl-type-3=direct&ssl-port-3=" + //
116 "&adddomain&csrf=" + csrf;
117 URL u2 = sendDomainForm(u, content);
118 boolean firstSucceeds = sslVariant != 0 && sslVariant != 2;
119 assertTrue(firstSucceeds ^ acceptSSLServer(sss));
120 boolean secondsSucceeds = sslVariant != 0;
121 assertTrue(secondsSucceeds ^ acceptSSLServer(sss2));
123 TestMail mail = getMailReciever().recieve();
124 if (emailVariant == 0) {
125 Assert.assertNotNull(mail);
130 String newcontent = IOUtils.readURL(cookie(u2.openConnection(), cookie));
131 Pattern pat = Pattern.compile("<td>ssl</td>\\s*<td>success</td>");
132 Matcher matcher = pat.matcher(newcontent);
133 assertTrue(newcontent, firstSucceeds ^ matcher.find());
134 assertTrue(newcontent, secondsSucceeds ^ matcher.find());
135 assertFalse(newcontent, matcher.find());
136 pat = Pattern.compile("<td>email</td>\\s*<td>success</td>");
137 assertTrue(newcontent, !successMail ^ pat.matcher(newcontent).find());
140 private void createCertificate(String test, CertificateProfile profile) throws GeneralSecurityException, IOException, SQLException, InterruptedException, GigiApiException {
141 kp = generateKeypair();
142 String csr = generatePEMCSR(kp, "CN=" + test);
143 c = new Certificate(User.getById(id), Certificate.buildDN("CN", test), "sha256", csr, CSRType.CSR, profile);
144 c.issue(null, "2y").waitFor(60000);
147 private boolean acceptSSLServer(SSLServerSocket sss) throws IOException {
148 try (Socket s = sss.accept()) {
149 s.getOutputStream().write('b');
150 s.getOutputStream().close();
152 } catch (IOException e) {
157 private SSLServerSocket createSSLServer(final PrivateKey priv, final X509Certificate cert) throws Error, IOException {
160 sc = SSLContext.getInstance("SSL");
161 sc.init(new KeyManager[] {
162 new X509KeyManager() {
165 public String[] getServerAliases(String keyType, Principal[] issuers) {
166 return new String[] {
172 public PrivateKey getPrivateKey(String alias) {
177 public String[] getClientAliases(String keyType, Principal[] issuers) {
182 public X509Certificate[] getCertificateChain(String alias) {
183 return new X509Certificate[] {
189 public String chooseClientAlias(String[] keyType, Principal[] issuers, Socket socket) {
194 public String chooseServerAlias(String keyType, Principal[] issuers, Socket socket) {
199 }, new TrustManager[] {
200 new X509TrustManager() {
203 public X509Certificate[] getAcceptedIssuers() {
208 public void checkServerTrusted(X509Certificate[] chain, String authType) throws CertificateException {}
211 public void checkClientTrusted(X509Certificate[] chain, String authType) throws CertificateException {}
213 }, new SecureRandom());
214 } catch (NoSuchAlgorithmException e) {
217 } catch (KeyManagementException e) {
222 SSLServerSocketFactory sssf = sc.getServerSocketFactory();
223 return (SSLServerSocket) sssf.createServerSocket(0);