Search in sources :

Example 1 with CSR

use of com.liumapp.digitalsign.engine.keystore.entity.CSR in project spring-cloud-digital-sign by SpringForAll.

the class KeyToolsTest method shouldGenerateCertificateSignRequest.

@Ignore
@Test
public void shouldGenerateCertificateSignRequest() throws Exception {
    Resource resource = Resource.from("classpath:keystore.ks");
    KeyStoreAdapter keyStoreAdapter = KeyTools.keyStoreFrom(resource, "1234");
    CSR csr = keyStoreAdapter.generateCSR("test", "456");
    assertNotNull(csr);
    assertNotNull(csr.toPkcs10());
    assertEquals("CN=Andrea Como, ST=Toscana, L=Prato, C=IT", csr.toPkcs10().getSubjectName().toString());
}
Also used : CSR(com.liumapp.digitalsign.engine.keystore.entity.CSR) Resource(com.liumapp.digitalsign.engine.keystore.entity.Resource) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 2 with CSR

use of com.liumapp.digitalsign.engine.keystore.entity.CSR in project spring-cloud-digital-sign by SpringForAll.

the class KeyToolsTest method shouldSignCertificateSignRequest.

@Ignore
@Test
public void shouldSignCertificateSignRequest() throws Exception {
    Resource resource = Resource.from("classpath:keystore.ks");
    KeyStoreAdapter requesterKeyStore = KeyTools.keyStoreFrom(resource, "1234");
    X509Certificate[] certificates = requesterKeyStore.getCertificates("test");
    assertEquals(1, certificates.length);
    CSR csr = requesterKeyStore.generateCSR("test", "456");
    Resource ca = Resource.from("classpath:ca.ks");
    KeyStoreAdapter caKeyStore = KeyTools.keyStoreFrom(ca, "ca");
    P7B signResponse = caKeyStore.signCSR(csr, "ca", "ca").withValidity(1, ChronoUnit.YEARS).sign();
    requesterKeyStore.importCAReply(signResponse, "test", "456");
    certificates = requesterKeyStore.getCertificates("test");
    assertEquals(2, certificates.length);
}
Also used : CSR(com.liumapp.digitalsign.engine.keystore.entity.CSR) Resource(com.liumapp.digitalsign.engine.keystore.entity.Resource) P7B(com.liumapp.digitalsign.engine.keystore.entity.P7B) X509Certificate(java.security.cert.X509Certificate) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 3 with CSR

use of com.liumapp.digitalsign.engine.keystore.entity.CSR in project spring-cloud-digital-sign by SpringForAll.

the class KeyStoreAdapter method generateCSR.

public CSR generateCSR(String alias, String password) throws KeyStoreException {
    try {
        KeyPair keyPair = getKayPairFor(alias, password).orElseThrow(() -> new KeyStoreException("Cannot find key for alias  " + alias));
        ;
        // CSR container format
        PKCS10 pkcs10 = new PKCS10(keyPair.getPublic());
        Signature signature = Signature.getInstance("SHA256withRSA");
        signature.initSign(keyPair.getPrivate());
        pkcs10.encodeAndSign(getX500Name(this.keyStore.getCertificate(alias)), signature);
        return new CSR(pkcs10);
    } catch (NoSuchAlgorithmException | InvalidKeyException | CertificateException | IOException | SignatureException e) {
        LOGGER.log(Level.SEVERE, e.getMessage(), e);
        throw new KeyStoreException(e);
    }
}
Also used : CSR(com.liumapp.digitalsign.engine.keystore.entity.CSR) CertificateException(java.security.cert.CertificateException) IOException(java.io.IOException) PKCS10(sun.security.pkcs10.PKCS10)

Aggregations

CSR (com.liumapp.digitalsign.engine.keystore.entity.CSR)3 Resource (com.liumapp.digitalsign.engine.keystore.entity.Resource)2 Ignore (org.junit.Ignore)2 Test (org.junit.Test)2 P7B (com.liumapp.digitalsign.engine.keystore.entity.P7B)1 IOException (java.io.IOException)1 CertificateException (java.security.cert.CertificateException)1 X509Certificate (java.security.cert.X509Certificate)1 PKCS10 (sun.security.pkcs10.PKCS10)1