Search in sources :

Example 1 with Resource

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

the class KeyToolsTest method shouldVerifySignedCertificate.

@Ignore
@Test
public void shouldVerifySignedCertificate() throws Exception {
    Resource ca = Resource.from("classpath:ca.ks");
    KeyStoreAdapter caKeyStore = KeyTools.keyStoreFrom(ca, "ca");
    Resource signedResource = Resource.from("classpath:signed-by-ca.ks");
    KeyStoreAdapter signedKeyStore = KeyTools.keyStoreFrom(signedResource, "1234");
    signedKeyStore.verifyWithTrustStore("test", caKeyStore.toKeyStore());
}
Also used : Resource(com.liumapp.digitalsign.engine.keystore.entity.Resource) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 2 with Resource

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

the class KeyToolsTest method shouldLoadKeyStoreFromClassPath.

@Ignore
@Test
public void shouldLoadKeyStoreFromClassPath() throws Exception {
    Resource resource = Resource.from("classpath:keystore.ks");
    KeyStoreAdapter keyStoreAdapter = KeyTools.keyStoreFrom(resource, "1234");
    assertNotNull(keyStoreAdapter.toKeyStore());
    Certificate certificate = keyStoreAdapter.toKeyStore().getCertificate("test");
    assertNotNull(certificate);
    assertTrue(certificate instanceof X509Certificate);
    X509Certificate x509Certificate = (X509Certificate) certificate;
    assertEquals("CN=Andrea Como, ST=Toscana, L=Prato, C=IT", x509Certificate.getSubjectDN().getName());
}
Also used : Resource(com.liumapp.digitalsign.engine.keystore.entity.Resource) X509Certificate(java.security.cert.X509Certificate) X509Certificate(java.security.cert.X509Certificate) Certificate(java.security.cert.Certificate) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 3 with Resource

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

the class KeyToolsTest method shouldGenerateKeyStoreWithTwoKeyPairs.

@Ignore
@Test
public void shouldGenerateKeyStoreWithTwoKeyPairs() throws Exception {
    try {
        FileOutputStream out = new FileOutputStream("test2.ks");
        /**
         * 生成keystore
         * 并附带第一份证书
         */
        KeyTools.newKeyStore("1234").newKeyPair().keyLength(2048).generateWithCertificate().withValidity(1, ChronoUnit.YEARS).withDistinguishName().commonName("certA").state("city").locality("province").country("country").build().createInKeyStore("certA", "123").writeTo(out);
        out.close();
        /**
         * 读取keystore
         * 并写入第二份证书
         */
        Resource resource = Resource.from("test2.ks");
        KeyStoreAdapter keyStoreAdapter = KeyTools.keyStoreFrom(resource, "1234");
        keyStoreAdapter.newKeyPair().keyLength(2048).generateWithCertificate().withValidity(1, ChronoUnit.YEARS).withDistinguishName().commonName("certB").state("cityb").locality("provinceb").country("countryb").build().createInKeyStore("certB", "123");
        FileOutputStream out2 = new FileOutputStream("test2.ks");
        keyStoreAdapter.writeTo(out2);
        out2.close();
    } finally {
        File keyStoreFile = new File("test2.ks");
        assertTrue(keyStoreFile.exists());
        assertTrue(keyStoreFile.delete());
    }
}
Also used : FileOutputStream(java.io.FileOutputStream) Resource(com.liumapp.digitalsign.engine.keystore.entity.Resource) File(java.io.File) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 4 with Resource

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

the class KeyToolsTest method shouldPrintCert.

/**
 * this will export certA from test2.ks to your disk.
 * plz make sure you have test2.ks
 * @throws Exception
 */
@Ignore
@Test
public void shouldPrintCert() throws Exception {
    Resource resource = Resource.from("test2.ks");
    KeyStoreAdapter keyStoreAdapter = KeyTools.keyStoreFrom(resource, "1234");
    Certificate certificate = keyStoreAdapter.toKeyStore().getCertificate("certA");
    FileOutputStream out = new FileOutputStream("certA.cer");
    out.write(certificate.getEncoded());
    out.close();
}
Also used : FileOutputStream(java.io.FileOutputStream) Resource(com.liumapp.digitalsign.engine.keystore.entity.Resource) X509Certificate(java.security.cert.X509Certificate) Certificate(java.security.cert.Certificate) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 5 with Resource

use of com.liumapp.digitalsign.engine.keystore.entity.Resource 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)

Aggregations

Resource (com.liumapp.digitalsign.engine.keystore.entity.Resource)9 Ignore (org.junit.Ignore)7 Test (org.junit.Test)7 FileOutputStream (java.io.FileOutputStream)4 Certificate (java.security.cert.Certificate)3 X509Certificate (java.security.cert.X509Certificate)3 CSR (com.liumapp.digitalsign.engine.keystore.entity.CSR)2 KeyStoreAdapter (com.liumapp.digitalsign.engine.keystore.service.KeyStoreAdapter)2 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)2 P7B (com.liumapp.digitalsign.engine.keystore.entity.P7B)1 File (java.io.File)1