1 package org.cacert.gigi.ping;
3 import static org.junit.Assert.*;
4 import static org.junit.Assume.*;
6 import java.io.ByteArrayInputStream;
7 import java.io.IOException;
8 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.CertificateFactory;
19 import java.security.cert.X509Certificate;
20 import java.sql.SQLException;
21 import java.util.Arrays;
22 import java.util.Date;
23 import java.util.HashMap;
24 import java.util.LinkedList;
25 import java.util.List;
26 import java.util.regex.Matcher;
27 import java.util.regex.Pattern;
29 import javax.net.ssl.KeyManager;
30 import javax.net.ssl.SSLContext;
31 import javax.net.ssl.SSLServerSocket;
32 import javax.net.ssl.SSLServerSocketFactory;
33 import javax.net.ssl.TrustManager;
34 import javax.net.ssl.X509KeyManager;
35 import javax.net.ssl.X509TrustManager;
36 import javax.security.auth.x500.X500Principal;
38 import org.cacert.gigi.GigiApiException;
39 import org.cacert.gigi.dbObjects.Certificate;
40 import org.cacert.gigi.dbObjects.Certificate.CSRType;
41 import org.cacert.gigi.dbObjects.CertificateProfile;
42 import org.cacert.gigi.dbObjects.Digest;
43 import org.cacert.gigi.dbObjects.User;
44 import org.cacert.gigi.testUtils.IOUtils;
45 import org.cacert.gigi.testUtils.PingTest;
46 import org.cacert.gigi.testUtils.TestEmailReceiver.TestMail;
47 import org.cacert.gigi.util.SimpleSigner;
48 import org.junit.Test;
49 import org.junit.runner.RunWith;
50 import org.junit.runners.Parameterized;
51 import org.junit.runners.Parameterized.Parameter;
52 import org.junit.runners.Parameterized.Parameters;
54 @RunWith(Parameterized.class)
55 public class TestSSL extends PingTest {
57 @Parameters(name = "self-signed = {0}")
58 public static Iterable<Object[]> genParams() throws IOException {
59 return Arrays.asList(new Object[] {
68 public Boolean self = false;
70 public abstract static class AsyncTask<T> {
78 public T join() throws InterruptedException {
87 runner = new Thread() {
92 res = AsyncTask.this.run();
93 } catch (Exception e) {
101 public abstract T run() throws Exception;
107 private X509Certificate c;
109 @Test(timeout = 70000)
110 public void sslAndMailSuccess() throws IOException, InterruptedException, SQLException, GeneralSecurityException, GigiApiException {
111 testEmailAndSSL(0, 0, true);
114 @Test(timeout = 70000)
115 public void sslWongTypeAndMailSuccess() throws IOException, InterruptedException, SQLException, GeneralSecurityException, GigiApiException {
116 testEmailAndSSL(1, 0, true);
119 @Test(timeout = 70000)
120 public void sslOneMissingAndMailSuccess() throws IOException, InterruptedException, SQLException, GeneralSecurityException, GigiApiException {
121 testEmailAndSSL(2, 0, true);
124 @Test(timeout = 70000)
125 public void sslBothMissingAndMailSuccess() throws IOException, InterruptedException, SQLException, GeneralSecurityException, GigiApiException {
126 testEmailAndSSL(3, 0, true);
129 @Test(timeout = 70000)
130 public void sslWrongTypeAndMailFail() throws IOException, InterruptedException, SQLException, GeneralSecurityException, GigiApiException {
131 testEmailAndSSL(1, 1, false);
137 * <li>0= all valid</li>
138 * <li>1= wrong type</li>
139 * <li>2= one server missing</li>
140 * <li>3= both servers missing</li>
142 * @param emailVariant
145 * @throws IOException
146 * @throws InterruptedException
147 * @throws SQLException
148 * @throws GeneralSecurityException
149 * @throws GigiApiException
152 private void testEmailAndSSL(int sslVariant, int emailVariant, boolean successMail) throws IOException, InterruptedException, SQLException, GeneralSecurityException, GigiApiException {
153 String test = getTestProps().getProperty("domain.local");
155 Matcher m = initailizeDomainForm();
156 String value = m.group(2);
159 createCertificateSelf(test, sslVariant == 1 ? "clientAuth" : "serverAuth", value);
161 createCertificate(test, CertificateProfile.getByName(sslVariant == 1 ? "client" : "server"));
164 final SSLServerSocket sss = createSSLServer(kp.getPrivate(), c);
165 int port = sss.getLocalPort();
166 final SSLServerSocket sss2 = createSSLServer(kp.getPrivate(), c);
167 int port2 = sss2.getLocalPort();
168 if (sslVariant == 3 || sslVariant == 2) {
170 if (sslVariant == 3) {
174 String content = "adddomain&newdomain=" + URLEncoder.encode(test, "UTF-8") + //
175 "&emailType=y&email=2&SSLType=y" + //
176 "&ssl-type-0=direct&ssl-port-0=" + port + //
177 "&ssl-type-1=direct&ssl-port-1=" + port2 + //
178 "&ssl-type-2=direct&ssl-port-2=" + //
179 "&ssl-type-3=direct&ssl-port-3=" + //
180 "&adddomain&csrf=" + csrf;
181 String p2 = sendDomainForm(content);
182 boolean firstSucceeds = sslVariant != 0 && sslVariant != 2;
183 AsyncTask<Boolean> ass = new AsyncTask<Boolean>() {
186 public Boolean run() throws Exception {
187 return acceptSSLServer(sss);
191 System.out.println(port + " and " + port2 + " ready");
192 System.err.println(port + " and " + port2 + " ready");
193 boolean accept2 = acceptSSLServer(sss2);
194 boolean accept1 = ass.join();
195 // assertTrue(firstSucceeds ^ accept1);
196 boolean secondsSucceeds = sslVariant != 0;
197 // assertTrue(secondsSucceeds ^ accept2);
199 TestMail mail = getMailReciever().receive();
200 if (emailVariant == 0) {
205 String newcontent = IOUtils.readURL(get(p2));
206 Pattern pat = Pattern.compile("<td>ssl</td>\\s*<td>success</td>");
207 Matcher matcher = pat.matcher(newcontent);
208 assertTrue(newcontent, firstSucceeds ^ matcher.find());
209 assertTrue(newcontent, secondsSucceeds ^ matcher.find());
210 assertFalse(newcontent, matcher.find());
211 pat = Pattern.compile("<td>email</td>\\s*<td>success</td>");
212 assertTrue(newcontent, !successMail ^ pat.matcher(newcontent).find());
215 private void createCertificate(String test, CertificateProfile profile) throws GeneralSecurityException, IOException, SQLException, InterruptedException, GigiApiException {
216 kp = generateKeypair();
217 String csr = generatePEMCSR(kp, "CN=" + test);
218 User u = User.getById(id);
219 Certificate c = new Certificate(u, u, Certificate.buildDN("CN", test), Digest.SHA256, csr, CSRType.CSR, profile);
220 c.issue(null, "2y", u).waitFor(60000);
224 private void createCertificateSelf(String test, String eku, String tok) throws GeneralSecurityException, IOException, SQLException, InterruptedException, GigiApiException {
225 kp = generateKeypair();
226 HashMap<String, String> name = new HashMap<>();
230 Date from = new Date();
231 Date to = new Date(from.getTime() + 1000 * 60 * 60 * 2);
232 List<Certificate.SubjectAlternateName> l = new LinkedList<>();
234 byte[] cert = SimpleSigner.generateCert(kp.getPublic(), kp.getPrivate(), name, new X500Principal(SimpleSigner.genX500Name(name).getEncoded()), l, from, to, Digest.SHA256, eku);
235 c = (X509Certificate) CertificateFactory.getInstance("X509").generateCertificate(new ByteArrayInputStream(cert));
238 private boolean acceptSSLServer(SSLServerSocket sss) throws IOException {
239 try (Socket s = sss.accept()) {
240 s.getOutputStream().write('b');
241 s.getOutputStream().close();
243 } catch (IOException e) {
248 private SSLServerSocket createSSLServer(final PrivateKey priv, final X509Certificate cert) throws Error, IOException {
251 sc = SSLContext.getInstance("SSL");
252 sc.init(new KeyManager[] {
253 new X509KeyManager() {
256 public String[] getServerAliases(String keyType, Principal[] issuers) {
257 return new String[] {
263 public PrivateKey getPrivateKey(String alias) {
268 public String[] getClientAliases(String keyType, Principal[] issuers) {
273 public X509Certificate[] getCertificateChain(String alias) {
274 return new X509Certificate[] {
280 public String chooseClientAlias(String[] keyType, Principal[] issuers, Socket socket) {
285 public String chooseServerAlias(String keyType, Principal[] issuers, Socket socket) {
290 }, new TrustManager[] {
291 new X509TrustManager() {
294 public X509Certificate[] getAcceptedIssuers() {
299 public void checkServerTrusted(X509Certificate[] chain, String authType) throws CertificateException {}
302 public void checkClientTrusted(X509Certificate[] chain, String authType) throws CertificateException {}
304 }, new SecureRandom());
305 } catch (NoSuchAlgorithmException e) {
308 } catch (KeyManagementException e) {
313 SSLServerSocketFactory sssf = sc.getServerSocketFactory();
314 return (SSLServerSocket) sssf.createServerSocket(0);
317 public static void main(String[] args) throws Exception {
319 TestSSL t1 = new TestSSL();
320 t1.sslAndMailSuccess();