1 package org.cacert.gigi;
3 import java.io.IOException;
4 import java.net.HttpURLConnection;
5 import java.net.Socket;
7 import java.security.GeneralSecurityException;
8 import java.security.Principal;
9 import java.security.PrivateKey;
10 import java.security.cert.X509Certificate;
11 import java.sql.SQLException;
13 import javax.net.ssl.HttpsURLConnection;
14 import javax.net.ssl.KeyManager;
15 import javax.net.ssl.SSLContext;
16 import javax.net.ssl.X509KeyManager;
18 import org.cacert.gigi.Certificate.CertificateStatus;
19 import org.cacert.gigi.testUtils.ManagedTest;
20 import org.cacert.gigi.testUtils.PemKey;
21 import org.junit.Test;
23 import static org.junit.Assert.*;
25 public class TestCertificate extends ManagedTest {
27 public void testClientCertLoginStates() throws IOException, GeneralSecurityException, SQLException,
28 InterruptedException {
29 String[] key1 = generateCSR("/CN=testmail@example.com");
30 Certificate c = new Certificate(1, "/CN=testmail@example.com", "sha256", key1[1]);
31 final PrivateKey pk = PemKey.parsePEMPrivateKey(key1[0]);
34 final X509Certificate ce = c.cert();
35 KeyManager km = new X509KeyManager() {
38 public String chooseClientAlias(String[] arg0, Principal[] arg1, Socket arg2) {
43 public String chooseServerAlias(String arg0, Principal[] arg1, Socket arg2) {
48 public X509Certificate[] getCertificateChain(String arg0) {
49 return new X509Certificate[] { ce };
53 public String[] getClientAliases(String arg0, Principal[] arg1) {
54 return new String[] { "client" };
58 public PrivateKey getPrivateKey(String arg0) {
59 if (arg0.equals("client")) {
66 public String[] getServerAliases(String arg0, Principal[] arg1) {
67 return new String[] { "client" };
70 SSLContext sc = SSLContext.getInstance("TLS");
71 sc.init(new KeyManager[] { km }, null, null);
73 HttpURLConnection connection = (HttpURLConnection) new URL("https://"
74 + getServerName().replaceFirst("^www.", "secure.") + "/login").openConnection();
75 if (connection instanceof HttpsURLConnection) {
76 ((HttpsURLConnection) connection).setSSLSocketFactory(sc.getSocketFactory());
78 assertEquals(302, connection.getResponseCode());
79 assertEquals("https://" + getServerName().replaceFirst("^www.", "secure.") + "/",
80 connection.getHeaderField("Location"));
84 public void testCertLifeCycle() throws IOException, GeneralSecurityException, SQLException, InterruptedException {
85 String[] key1 = generateCSR("/CN=testmail@example.com");
86 Certificate c = new Certificate(1, "/CN=testmail@example.com", "sha256", key1[1]);
87 testFails(CertificateStatus.DRAFT, c);
90 testFails(CertificateStatus.SIGNING, c);
93 testFails(CertificateStatus.ISSUED, c);
96 testFails(CertificateStatus.BEING_REVOKED, c);
99 testFails(CertificateStatus.REVOKED, c);
103 private void testFails(CertificateStatus status, Certificate c) throws IOException, GeneralSecurityException,
105 if (status != CertificateStatus.ISSUED) {
108 fail("is in invalid state");
109 } catch (IllegalStateException ise) {
113 if (status != CertificateStatus.DRAFT) {
116 fail("is in invalid state");
117 } catch (IllegalStateException ise) {
121 if (status != CertificateStatus.ISSUED) {
124 fail("is in invalid state");
125 } catch (IllegalStateException ise) {