1 package org.cacert.gigi;
3 import static org.junit.Assert.*;
5 import java.io.IOException;
6 import java.net.HttpURLConnection;
8 import java.security.GeneralSecurityException;
9 import java.security.KeyManagementException;
10 import java.security.KeyPair;
11 import java.security.NoSuchAlgorithmException;
12 import java.security.PrivateKey;
13 import java.security.cert.X509Certificate;
14 import java.sql.SQLException;
16 import org.cacert.gigi.dbObjects.Certificate;
17 import org.cacert.gigi.dbObjects.Certificate.CSRType;
18 import org.cacert.gigi.dbObjects.CertificateProfile;
19 import org.cacert.gigi.dbObjects.User;
20 import org.cacert.gigi.testUtils.ManagedTest;
21 import org.cacert.gigi.util.Job;
22 import org.junit.Test;
24 public class TestSeparateSessionScope extends ManagedTest {
27 public void testSeparateScope() throws IOException, GeneralSecurityException, SQLException, InterruptedException, GigiApiException {
28 String mail = "thisgo" + createUniqueName() + "@example.com";
29 int user = createAssuranceUser("test", "tugo", mail, TEST_PASSWORD);
30 String cookie = login(mail, TEST_PASSWORD);
31 KeyPair kp = generateKeypair();
32 String csr = generatePEMCSR(kp, "CN=felix@dogcraft.de");
33 Certificate c = new Certificate(User.getById(user), Certificate.buildDN("CN", "testmail@example.com"), "sha256", csr, CSRType.CSR, CertificateProfile.getById(1));
34 final PrivateKey pk = kp.getPrivate();
35 c.issue(null, "2y").waitFor(60000);
36 final X509Certificate ce = c.cert();
37 String scookie = login(pk, ce);
39 assertTrue(isLoggedin(cookie));
40 assertFalse(isLoggedin(scookie));
42 checkCertLogin(c, pk, scookie, 200);
43 checkCertLogin(c, pk, cookie, 302);
47 public void testSerialSteal() throws IOException, GeneralSecurityException, SQLException, InterruptedException, GigiApiException {
48 String mail = "thisgo" + createUniqueName() + "@example.com";
49 int user = createAssuranceUser("test", "tugo", mail, TEST_PASSWORD);
50 KeyPair kp = generateKeypair();
51 String csr = generatePEMCSR(kp, "CN=felix@dogcraft.de");
52 Certificate c = new Certificate(User.getById(user), Certificate.buildDN("CN", "testmail@example.com"), "sha256", csr, CSRType.CSR, CertificateProfile.getById(1));
53 Certificate c2 = new Certificate(User.getById(user), Certificate.buildDN("CN", "testmail@example.com"), "sha256", csr, CSRType.CSR, CertificateProfile.getById(1));
54 final PrivateKey pk = kp.getPrivate();
55 Job j1 = c.issue(null, "2y");
56 c2.issue(null, "2y").waitFor(60000);
58 final X509Certificate ce = c.cert();
59 String scookie = login(pk, ce);
61 checkCertLogin(c, pk, scookie, 200);
62 checkCertLogin(c2, pk, scookie, 403);
63 checkCertLogin(c, pk, scookie, 302);
67 private void checkCertLogin(Certificate c2, final PrivateKey pk, String scookie, int expected) throws IOException, NoSuchAlgorithmException, KeyManagementException, GeneralSecurityException {
68 URL u = new URL("https://" + getServerName().replaceAll("^www", "secure") + SECURE_REFERENCE);
69 HttpURLConnection huc = (HttpURLConnection) u.openConnection();
70 authenticateClientCert(pk, c2.cert(), huc);
71 huc.setRequestProperty("Cookie", scookie);
72 assertEquals(expected, huc.getResponseCode());