Search in sources :

Example 1 with X509CertificateEx

use of org.nhindirect.stagent.cert.X509CertificateEx in project nhin-d by DirectProject.

the class CertCommands method writeCertsToFiles.

protected void writeCertsToFiles(org.nhind.config.Certificate[] certs) throws IOException {
    int idx = 1;
    for (org.nhind.config.Certificate cert : certs) {
        CertUtils.CertContainer cont = CertUtils.toCertContainer(cert.getData());
        X509Certificate transCert = cont.getCert();
        String certFileName = "";
        String extension = (transCert instanceof X509CertificateEx) ? ".p12" : ".der";
        String certFileHold = CryptoExtensions.getSubjectAddress(transCert) + extension;
        if (certs.length > 1) {
            int index = certFileHold.lastIndexOf(".");
            if (index < 0)
                certFileHold += "(" + idx + ")";
            else {
                certFileName = certFileHold.substring(0, index - 1) + "(" + idx + ")" + certFileHold.substring(index);
            }
        } else
            certFileName = certFileHold;
        File certFile = new File(certFileName);
        if (certFile.exists())
            certFile.delete();
        System.out.println("Writing cert file: " + certFile.getAbsolutePath());
        try {
            FileUtils.writeByteArrayToFile(certFile, transCert.getEncoded());
        } catch (Exception e) {
            System.out.println("Failed to write cert file: " + certFile.getAbsolutePath() + " :" + e.getMessage());
        }
        ++idx;
    }
}
Also used : CertUtils(org.nhindirect.config.model.utils.CertUtils) X509CertificateEx(org.nhindirect.stagent.cert.X509CertificateEx) File(java.io.File) X509Certificate(java.security.cert.X509Certificate) IOException(java.io.IOException)

Example 2 with X509CertificateEx

use of org.nhindirect.stagent.cert.X509CertificateEx in project nhin-d by DirectProject.

the class CertCommands method importPrivateCertWithWrappedKey.

@Command(name = "AddPrivateCertWithWrappedKey", usage = IMPORT_PRIVATE_CERT_W_WRAPPEDKEY_USAGE)
public void importPrivateCertWithWrappedKey(String[] args) {
    final String certFileLoc = StringArrayUtil.getRequiredValue(args, 0);
    final String keyFileLoc = StringArrayUtil.getRequiredValue(args, 1);
    try {
        final byte[] certFileBytes = FileUtils.readFileToByteArray(new File(certFileLoc));
        final byte[] keyFileBytes = FileUtils.readFileToByteArray(new File(keyFileLoc));
        final X509Certificate cert = CertUtils.toX509Certificate(certFileBytes);
        byte[] certBytes = org.nhindirect.config.model.utils.CertUtils.certAndWrappedKeyToRawByteFormat(keyFileBytes, cert);
        org.nhind.config.Certificate addCert = new org.nhind.config.Certificate();
        addCert.setData(certBytes);
        addCert.setOwner(CryptoExtensions.getSubjectAddress(cert));
        addCert.setPrivateKey(cert instanceof X509CertificateEx);
        addCert.setStatus(EntityStatus.ENABLED);
        proxy.addCertificates(new org.nhind.config.Certificate[] { addCert });
        System.out.println("Successfully imported certificate.");
    } catch (IOException e) {
        System.out.println("Error reading file: " + e.getMessage());
        return;
    } catch (Exception e) {
        System.out.println("Error importing certificate " + e.getMessage());
    }
}
Also used : X509CertificateEx(org.nhindirect.stagent.cert.X509CertificateEx) IOException(java.io.IOException) File(java.io.File) X509Certificate(java.security.cert.X509Certificate) IOException(java.io.IOException) X509Certificate(java.security.cert.X509Certificate) Command(org.nhindirect.dns.tools.utils.Command)

Example 3 with X509CertificateEx

use of org.nhindirect.stagent.cert.X509CertificateEx in project nhin-d by DirectProject.

the class CertCommands method importPrivateCert.

@Command(name = "AddPrivateCert", usage = IMPORT_PRIVATE_CERT_USAGE)
public void importPrivateCert(String[] args) {
    final String fileLoc = StringArrayUtil.getRequiredValue(args, 0);
    final String passPhrase = StringArrayUtil.getOptionalValue(args, 1, "");
    try {
        final byte[] certBytes = FileUtils.readFileToByteArray(new File(fileLoc));
        final byte[] insertBytes = (passPhrase == null || passPhrase.isEmpty()) ? certBytes : CertUtils.pkcs12ToStrippedPkcs12(certBytes, passPhrase);
        final X509Certificate cert = CertUtils.toX509Certificate(insertBytes);
        org.nhind.config.Certificate addCert = new org.nhind.config.Certificate();
        addCert.setData(certBytes);
        addCert.setOwner(CryptoExtensions.getSubjectAddress(cert));
        addCert.setPrivateKey(cert instanceof X509CertificateEx);
        addCert.setStatus(EntityStatus.ENABLED);
        proxy.addCertificates(new org.nhind.config.Certificate[] { addCert });
        System.out.println("Successfully imported private certificate.");
    } catch (IOException e) {
        System.out.println("Error reading file " + fileLoc + " : " + e.getMessage());
        return;
    } catch (Exception e) {
        System.out.println("Error importing certificate " + fileLoc + " : " + e.getMessage());
    }
}
Also used : X509CertificateEx(org.nhindirect.stagent.cert.X509CertificateEx) IOException(java.io.IOException) File(java.io.File) X509Certificate(java.security.cert.X509Certificate) IOException(java.io.IOException) X509Certificate(java.security.cert.X509Certificate) Command(org.nhindirect.dns.tools.utils.Command)

Example 4 with X509CertificateEx

use of org.nhindirect.stagent.cert.X509CertificateEx in project nhin-d by DirectProject.

the class CacheableKeyStoreManagerCertificateStore_addTest method testAdd_nonMutableStore_assertException.

public void testAdd_nonMutableStore_assertException() throws Exception {
    if (store != null) {
        final BootstrappedKeyStoreProtectionManager mgr = new BootstrappedKeyStoreProtectionManager();
        store.setKeyStoreManager(mgr);
        boolean exceptionOccured = false;
        try {
            final X509CertificateEx user1 = (X509CertificateEx) TestUtils.getInternalCert("user1");
            store.add(user1);
        } catch (IllegalStateException ex) {
            exceptionOccured = true;
        }
        assertTrue(exceptionOccured);
    }
}
Also used : X509CertificateEx(org.nhindirect.stagent.cert.X509CertificateEx) BootstrappedKeyStoreProtectionManager(org.nhindirect.common.crypto.impl.BootstrappedKeyStoreProtectionManager)

Example 5 with X509CertificateEx

use of org.nhindirect.stagent.cert.X509CertificateEx in project nhin-d by DirectProject.

the class CacheableKeyStoreManagerCertificateStore_addTest method testAdd_addNewCert_assertAdded.

public void testAdd_addNewCert_assertAdded() throws Exception {
    if (store != null) {
        // add a certificate
        final X509CertificateEx user1 = (X509CertificateEx) TestUtils.getInternalCert("user1");
        store.add(user1);
        final Collection<X509Certificate> retrievedCerts = store.getAllCertificates();
        assertEquals(1, retrievedCerts.size());
        final X509Certificate retrievedCert = retrievedCerts.iterator().next();
        assertTrue(retrievedCert instanceof X509CertificateEx);
        assertEquals(user1, retrievedCert);
    }
}
Also used : X509CertificateEx(org.nhindirect.stagent.cert.X509CertificateEx) X509Certificate(java.security.cert.X509Certificate)

Aggregations

X509CertificateEx (org.nhindirect.stagent.cert.X509CertificateEx)51 X509Certificate (java.security.cert.X509Certificate)39 MimeEntity (org.nhindirect.stagent.mail.MimeEntity)18 SMIMECryptographerImpl (org.nhindirect.stagent.cryptography.SMIMECryptographerImpl)13 IOException (java.io.IOException)11 KeyStore (java.security.KeyStore)11 NHINDException (org.nhindirect.stagent.NHINDException)9 ByteArrayInputStream (java.io.ByteArrayInputStream)6 ByteArrayOutputStream (java.io.ByteArrayOutputStream)6 Key (java.security.Key)6 PrivateKey (java.security.PrivateKey)6 File (java.io.File)5 Certificate (java.security.cert.Certificate)5 MimeMultipart (javax.mail.internet.MimeMultipart)5 SMIMEEnveloped (org.bouncycastle.mail.smime.SMIMEEnveloped)5 LdapCertificateStoreProvider (org.nhindirect.stagent.cert.impl.provider.LdapCertificateStoreProvider)5 SignedEntity (org.nhindirect.stagent.cryptography.SignedEntity)5 ArrayList (java.util.ArrayList)4 MessagingException (javax.mail.MessagingException)4 RecipientInformation (org.bouncycastle.cms.RecipientInformation)4