]> WPIA git - gigi.git/blob - tests/org/cacert/gigi/TestSeparateSessionScope.java
Merge remote-tracking branch 'origin/libs/scrypt/local'
[gigi.git] / tests / org / cacert / gigi / TestSeparateSessionScope.java
1 package org.cacert.gigi;
2
3 import static org.junit.Assert.*;
4
5 import java.io.IOException;
6 import java.net.HttpURLConnection;
7 import java.net.URL;
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;
15
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;
23
24 public class TestSeparateSessionScope extends ManagedTest {
25
26     @Test
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);
38
39         assertTrue(isLoggedin(cookie));
40         assertFalse(isLoggedin(scookie));
41
42         checkCertLogin(c, pk, scookie, 200);
43         checkCertLogin(c, pk, cookie, 302);
44     }
45
46     @Test
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);
57         j1.waitFor(60000);
58         final X509Certificate ce = c.cert();
59         String scookie = login(pk, ce);
60
61         checkCertLogin(c, pk, scookie, 200);
62         checkCertLogin(c2, pk, scookie, 403);
63         checkCertLogin(c, pk, scookie, 302);
64
65     }
66
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());
73     }
74 }