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.Job;
20 import org.cacert.gigi.dbObjects.User;
21 import org.cacert.gigi.testUtils.ManagedTest;
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=hans");
33 User u = User.getById(user);
34 Certificate c = new Certificate(u, u, Certificate.buildDN("CN", "hans"), "sha256", csr, CSRType.CSR, CertificateProfile.getById(1));
35 final PrivateKey pk = kp.getPrivate();
36 c.issue(null, "2y", u).waitFor(60000);
37 final X509Certificate ce = c.cert();
38 String scookie = login(pk, ce);
40 assertTrue(isLoggedin(cookie));
41 assertFalse(isLoggedin(scookie));
43 checkCertLogin(c, pk, scookie, 200);
44 checkCertLogin(c, pk, cookie, 302);
48 public void testSerialSteal() throws IOException, GeneralSecurityException, SQLException, InterruptedException, GigiApiException {
49 String mail = "thisgo" + createUniqueName() + "@example.com";
50 int user = createAssuranceUser("test", "tugo", mail, TEST_PASSWORD);
51 KeyPair kp = generateKeypair();
52 String csr = generatePEMCSR(kp, "CN=hans");
53 User u = User.getById(user);
54 Certificate c = new Certificate(u, u, Certificate.buildDN("CN", "hans"), "sha256", csr, CSRType.CSR, CertificateProfile.getById(1));
55 Certificate c2 = new Certificate(u, u, Certificate.buildDN("CN", "hans"), "sha256", csr, CSRType.CSR, CertificateProfile.getById(1));
56 final PrivateKey pk = kp.getPrivate();
57 Job j1 = c.issue(null, "2y", u);
58 c2.issue(null, "2y", u).waitFor(60000);
60 final X509Certificate ce = c.cert();
61 String scookie = login(pk, ce);
63 checkCertLogin(c, pk, scookie, 200);
64 checkCertLogin(c2, pk, scookie, 403);
65 checkCertLogin(c, pk, scookie, 302);
69 private void checkCertLogin(Certificate c2, final PrivateKey pk, String scookie, int expected) throws IOException, NoSuchAlgorithmException, KeyManagementException, GeneralSecurityException {
70 URL u = new URL("https://" + getServerName().replaceAll("^www", "secure") + SECURE_REFERENCE);
71 HttpURLConnection huc = (HttpURLConnection) u.openConnection();
72 authenticateClientCert(pk, c2.cert(), huc);
73 huc.setRequestProperty("Cookie", scookie);
74 assertEquals(expected, huc.getResponseCode());