Search in sources :

Example 21 with X509CertificateEx

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

the class CacheableKeyStoreManagerCertificateStore_removeTest method testRemove_removingExistingCert_assertRemoved.

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

Example 22 with X509CertificateEx

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

the class CacheableKeyStoreManagerCertificateStore_removeTest method testRemove_removingNonExistantCert_assertNotRemoved.

public void testRemove_removingNonExistantCert_assertNotRemoved() throws Exception {
    if (store != null) {
        // add a certificate
        final X509CertificateEx user1 = (X509CertificateEx) TestUtils.getInternalCert("user1");
        store.add(user1);
        final Collection<X509Certificate> retrievedCerts = store.getAllCertificates();
        final X509Certificate retrievedCert = retrievedCerts.iterator().next();
        assertEquals(user1, retrievedCert);
        // try removing another non existant cert
        final X509Certificate cacert = TestUtils.getInternalCACert("cacert");
        store.remove(cacert);
        assertFalse(store.getAllCertificates().isEmpty());
    }
}
Also used : X509CertificateEx(org.nhindirect.stagent.cert.X509CertificateEx) X509Certificate(java.security.cert.X509Certificate)

Example 23 with X509CertificateEx

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

the class SplitDirectRecipientInformation_getDecryptedContentTest method testGetDecryptedContent_sameDefaultEncAndKeyEncProvider_assertDecrypted.

public void testGetDecryptedContent_sameDefaultEncAndKeyEncProvider_assertDecrypted() throws Exception {
    final SMIMEEnveloped env = createSMIMEEnv();
    final RecipientInformation recipient = (RecipientInformation) env.getRecipientInfos().getRecipients().iterator().next();
    final SplitDirectRecipientInformationFactory factory = new SplitDirectRecipientInformationFactory();
    final SplitDirectRecipientInformation recInfo = (SplitDirectRecipientInformation) factory.createInstance(recipient, env);
    // this will be non-null if it works correctly
    assertNotNull(recInfo.getDecryptedContent(((X509CertificateEx) encCert).getPrivateKey()));
}
Also used : RecipientInformation(org.bouncycastle.cms.RecipientInformation) X509CertificateEx(org.nhindirect.stagent.cert.X509CertificateEx) SMIMEEnveloped(org.bouncycastle.mail.smime.SMIMEEnveloped)

Example 24 with X509CertificateEx

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

the class SplitDirectRecipientInformation_getDecryptedContentTest method testGetDecryptedContent_safeNetHSMKeyEncProvider_differntEncCert_assertNotDecrypted.

public void testGetDecryptedContent_safeNetHSMKeyEncProvider_differntEncCert_assertNotDecrypted() throws Exception {
    /**
         * This test is only run if a specific SafeNet eToken Pro HSM is connected to the testing 
         * system.  This can be modified for another specific machine and/or token.
         */
    pkcs11ProvName = TestUtils.setupSafeNetToken();
    if (!StringUtils.isEmpty(pkcs11ProvName)) {
        // get a certificate from the key store
        final KeyStore ks = KeyStore.getInstance("PKCS11");
        ks.load(null, "1Kingpuff".toCharArray());
        // get the decryption cert
        X509CertificateEx decryptCert = null;
        final Enumeration<String> aliases = ks.aliases();
        while (aliases.hasMoreElements()) {
            String alias = aliases.nextElement();
            Certificate pkcs11Cert = ks.getCertificate(alias);
            if (pkcs11Cert != null && pkcs11Cert instanceof X509Certificate) {
                // check if there is private key
                Key key = ks.getKey(alias, null);
                if (key != null && key instanceof PrivateKey && CryptoExtensions.certSubjectContainsName((X509Certificate) pkcs11Cert, "user1@cerner.com")) {
                    decryptCert = X509CertificateEx.fromX509Certificate((X509Certificate) pkcs11Cert, (PrivateKey) key);
                    break;
                }
            }
        }
        encCert = TestUtils.getInternalCert("gm2552");
        final SMIMEEnveloped env = createSMIMEEnv(encCert);
        final RecipientInformation recipient = (RecipientInformation) env.getRecipientInfos().getRecipients().iterator().next();
        final SplitDirectRecipientInformationFactory factory = new SplitDirectRecipientInformationFactory(pkcs11ProvName, "BC");
        final SplitDirectRecipientInformation recInfo = (SplitDirectRecipientInformation) factory.createInstance(recipient, env);
        boolean exceptionOccured = false;
        try {
            recInfo.getDecryptedContent(decryptCert.getPrivateKey());
        } catch (Exception e) {
            exceptionOccured = true;
        }
        assertTrue(exceptionOccured);
    }
}
Also used : PrivateKey(java.security.PrivateKey) KeyStore(java.security.KeyStore) SMIMEEnveloped(org.bouncycastle.mail.smime.SMIMEEnveloped) X509Certificate(java.security.cert.X509Certificate) RecipientInformation(org.bouncycastle.cms.RecipientInformation) X509CertificateEx(org.nhindirect.stagent.cert.X509CertificateEx) Key(java.security.Key) PrivateKey(java.security.PrivateKey) X509Certificate(java.security.cert.X509Certificate) Certificate(java.security.cert.Certificate)

Example 25 with X509CertificateEx

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

the class SplitDirectRecipientInformation_getDecryptedContentTest method testGetDecryptedContent_differentEncAndKeyEncProvider_assertDecrypted.

public void testGetDecryptedContent_differentEncAndKeyEncProvider_assertDecrypted() throws Exception {
    final SMIMEEnveloped env = createSMIMEEnv();
    final RecipientInformation recipient = (RecipientInformation) env.getRecipientInfos().getRecipients().iterator().next();
    final SplitDirectRecipientInformationFactory factory = new SplitDirectRecipientInformationFactory("SunJCE", "BC");
    final SplitDirectRecipientInformation recInfo = (SplitDirectRecipientInformation) factory.createInstance(recipient, env);
    // this will be non-null if it works correctly
    assertNotNull(recInfo.getDecryptedContent(((X509CertificateEx) encCert).getPrivateKey()));
}
Also used : RecipientInformation(org.bouncycastle.cms.RecipientInformation) X509CertificateEx(org.nhindirect.stagent.cert.X509CertificateEx) SMIMEEnveloped(org.bouncycastle.mail.smime.SMIMEEnveloped)

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