]> WPIA git - gigi.git/blob - tests/org/cacert/gigi/TestSeparateSessionScope.java
upd: split certificate issuance as organisation into seperate
[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.Job;
20 import org.cacert.gigi.dbObjects.User;
21 import org.cacert.gigi.testUtils.ManagedTest;
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=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);
39
40         assertTrue(isLoggedin(cookie));
41         assertFalse(isLoggedin(scookie));
42
43         checkCertLogin(c, pk, scookie, 200);
44         checkCertLogin(c, pk, cookie, 302);
45     }
46
47     @Test
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);
59         j1.waitFor(60000);
60         final X509Certificate ce = c.cert();
61         String scookie = login(pk, ce);
62
63         checkCertLogin(c, pk, scookie, 200);
64         checkCertLogin(c2, pk, scookie, 403);
65         checkCertLogin(c, pk, scookie, 302);
66
67     }
68
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());
75     }
76 }