Search in sources :

Example 66 with PKCS10CertificationRequest

use of org.bouncycastle.pkcs.PKCS10CertificationRequest in project athenz by yahoo.

the class CryptoTest method testExtractX509CSRFields.

@Test
public void testExtractX509CSRFields() throws IOException {
    Path path = Paths.get("src/test/resources/valid.csr");
    String csr = new String(Files.readAllBytes(path));
    PKCS10CertificationRequest certReq1 = Crypto.getPKCS10CertRequest(csr);
    assertNotNull(certReq1);
    PKCS10CertificationRequest certReq = Mockito.spy(certReq1);
    assertNotNull(certReq);
    assertEquals(Crypto.extractX509CSRCommonName(certReq), "athenz.syncer");
    assertEquals(Crypto.extractX509CSRSubjectOField(certReq), "My Test Company");
    assertNull(Crypto.extractX509CSRSubjectOUField(certReq));
    assertNull(Crypto.extractX509CSREmail(certReq));
}
Also used : Path(java.nio.file.Path) PKCS10CertificationRequest(org.bouncycastle.pkcs.PKCS10CertificationRequest) DERIA5String(org.bouncycastle.asn1.DERIA5String) Test(org.testng.annotations.Test)

Example 67 with PKCS10CertificationRequest

use of org.bouncycastle.pkcs.PKCS10CertificationRequest in project athenz by yahoo.

the class CryptoTest method testExtractX509CSRSubjectField.

@Test
public void testExtractX509CSRSubjectField() throws IOException {
    Path path = Paths.get("src/test/resources/valid.csr");
    String csr = new String(Files.readAllBytes(path));
    PKCS10CertificationRequest certReq1 = Crypto.getPKCS10CertRequest(csr);
    assertNotNull(certReq1);
    PKCS10CertificationRequest certReq = Mockito.spy(certReq1);
    assertNotNull(certReq);
    assertEquals(Crypto.extractX509CSRCommonName(certReq), "athenz.syncer");
    assertEquals(Crypto.extractX509CSRSubjectOField(certReq), "My Test Company");
    assertNull(Crypto.extractX509CSRSubjectOUField(certReq));
    assertNull(Crypto.extractX509CSREmail(certReq));
}
Also used : Path(java.nio.file.Path) PKCS10CertificationRequest(org.bouncycastle.pkcs.PKCS10CertificationRequest) DERIA5String(org.bouncycastle.asn1.DERIA5String) Test(org.testng.annotations.Test)

Example 68 with PKCS10CertificationRequest

use of org.bouncycastle.pkcs.PKCS10CertificationRequest in project athenz by yahoo.

the class CryptoTest method testExtractX509CSRDnsNames.

@Test
public void testExtractX509CSRDnsNames() throws IOException {
    Path path = Paths.get("src/test/resources/valid_emails.csr");
    String csr = new String(Files.readAllBytes(path));
    PKCS10CertificationRequest certReq = Crypto.getPKCS10CertRequest(csr);
    assertNotNull(certReq);
    List<String> dnsNames = new LinkedList<>();
    dnsNames.add("production.athenz.ostk.athenz.cloud");
    dnsNames.add("1001.instanceid.athenz.ostk.athenz.cloud");
    assertEquals(Crypto.extractX509CSRDnsNames(certReq), dnsNames);
}
Also used : Path(java.nio.file.Path) PKCS10CertificationRequest(org.bouncycastle.pkcs.PKCS10CertificationRequest) DERIA5String(org.bouncycastle.asn1.DERIA5String) Test(org.testng.annotations.Test)

Example 69 with PKCS10CertificationRequest

use of org.bouncycastle.pkcs.PKCS10CertificationRequest in project athenz by yahoo.

the class CryptoTest method testGenerateX509Certificate.

@Test
public void testGenerateX509Certificate() throws IOException {
    Path path = Paths.get("src/test/resources/valid.csr");
    String certStr = new String(Files.readAllBytes(path));
    PKCS10CertificationRequest certReq = Crypto.getPKCS10CertRequest(certStr);
    X509Certificate caCertificate = Crypto.loadX509Certificate(ecPublicX509Cert);
    PrivateKey caPrivateKey = Crypto.loadPrivateKey(privateEncryptedKey, encryptedKeyPassword);
    X509Certificate cert = Crypto.generateX509Certificate(certReq, caPrivateKey, caCertificate, 600, false);
    assertNotNull(cert);
    assertEquals(cert.getIssuerX500Principal().getName(), "CN=athenz.syncer,O=My Test Company,L=Sunnyvale,ST=CA,C=US");
    Date notAfter = cert.getNotAfter();
    long diff = notAfter.getTime() - System.currentTimeMillis();
    // convert minutes to milliseconds
    assertTrue(diff <= 600 * 60 * 1000);
}
Also used : Path(java.nio.file.Path) PKCS10CertificationRequest(org.bouncycastle.pkcs.PKCS10CertificationRequest) PrivateKey(java.security.PrivateKey) DERIA5String(org.bouncycastle.asn1.DERIA5String) X509Certificate(java.security.cert.X509Certificate) Test(org.testng.annotations.Test)

Example 70 with PKCS10CertificationRequest

use of org.bouncycastle.pkcs.PKCS10CertificationRequest in project athenz by yahoo.

the class CryptoTest method testExtractX509CSRFieldsWithRfc822.

@Test
public void testExtractX509CSRFieldsWithRfc822() throws IOException {
    Path path = Paths.get("src/test/resources/valid_email.csr");
    String csr = new String(Files.readAllBytes(path));
    PKCS10CertificationRequest certReq = Crypto.getPKCS10CertRequest(csr);
    assertNotNull(certReq);
    assertEquals(Crypto.extractX509CSRCommonName(certReq), "sports:role.readers");
    assertEquals(Crypto.extractX509CSREmail(certReq), "sports.scores@aws.yahoo.cloud");
}
Also used : Path(java.nio.file.Path) PKCS10CertificationRequest(org.bouncycastle.pkcs.PKCS10CertificationRequest) DERIA5String(org.bouncycastle.asn1.DERIA5String) Test(org.testng.annotations.Test)

Aggregations

PKCS10CertificationRequest (org.bouncycastle.pkcs.PKCS10CertificationRequest)78 Test (org.testng.annotations.Test)39 Path (java.nio.file.Path)34 DERIA5String (org.bouncycastle.asn1.DERIA5String)19 X509Certificate (java.security.cert.X509Certificate)17 IOException (java.io.IOException)14 X500Name (org.bouncycastle.asn1.x500.X500Name)13 PrivateKey (java.security.PrivateKey)12 JcaContentSignerBuilder (org.bouncycastle.operator.jcajce.JcaContentSignerBuilder)10 JcaPKCS10CertificationRequestBuilder (org.bouncycastle.pkcs.jcajce.JcaPKCS10CertificationRequestBuilder)10 KeyPair (java.security.KeyPair)9 KeyPairGenerator (java.security.KeyPairGenerator)8 X500Principal (javax.security.auth.x500.X500Principal)8 OperatorCreationException (org.bouncycastle.operator.OperatorCreationException)8 File (java.io.File)7 BouncyCastleProvider (org.bouncycastle.jce.provider.BouncyCastleProvider)7 ContentSigner (org.bouncycastle.operator.ContentSigner)7 JcaPKCS10CertificationRequest (org.bouncycastle.pkcs.jcajce.JcaPKCS10CertificationRequest)7 PemObject (org.bouncycastle.util.io.pem.PemObject)6 CryptoException (org.kse.crypto.CryptoException)6