Search in sources :

Example 11 with SMIMECryptographerImpl

use of org.nhindirect.stagent.cryptography.SMIMECryptographerImpl in project nhin-d by DirectProject.

the class CryptographerTest method testEncryptAndDecryptMultipartEntity.

private void testEncryptAndDecryptMultipartEntity(EncryptionAlgorithm encAlgo) throws Exception {
    X509Certificate cert = TestUtils.getExternalCert("user1");
    SMIMECryptographerImpl cryptographer = new SMIMECryptographerImpl();
    cryptographer.setEncryptionAlgorithm(encAlgo);
    MimeEntity entityText = new MimeEntity();
    entityText.setText("Hello world.");
    entityText.setHeader(MimeStandard.ContentTypeHeader, "text/plain");
    entityText.setHeader(MimeStandard.ContentTransferEncodingHeader, "7bit");
    MimeEntity entityXML = new MimeEntity();
    entityXML.setText("<Test></Test>");
    entityXML.setHeader(MimeStandard.ContentTypeHeader, "text/xml");
    MimeMultipart mpEntity = new MimeMultipart();
    mpEntity.addBodyPart(entityText);
    mpEntity.addBodyPart(entityXML);
    MimeEntity encEntity = cryptographer.encrypt(mpEntity, cert);
    assertNotNull(encEntity);
    X509CertificateEx certex = TestUtils.getInternalCert("user1");
    MimeEntity decryEntity = cryptographer.decrypt(encEntity, certex);
    assertNotNull(decryEntity);
    ByteArrayOutputStream oStream = new ByteArrayOutputStream();
    mpEntity.writeTo(oStream);
    InternetHeaders hdrs = new InternetHeaders();
    hdrs.addHeader(MimeStandard.ContentTypeHeader, mpEntity.getContentType());
    MimeEntity orgEntity = new MimeEntity(hdrs, oStream.toByteArray());
    byte[] decryEntityBytes = EntitySerializer.Default.serializeToBytes(decryEntity);
    byte[] entityBytes = EntitySerializer.Default.serializeToBytes(orgEntity);
    System.out.println("Original:\r\n" + new String(entityBytes));
    System.out.println("\r\n\r\n\r\nNew:\r\n" + new String(decryEntityBytes));
    assertTrue(Arrays.equals(decryEntityBytes, entityBytes));
}
Also used : InternetHeaders(javax.mail.internet.InternetHeaders) SMIMECryptographerImpl(org.nhindirect.stagent.cryptography.SMIMECryptographerImpl) MimeMultipart(javax.mail.internet.MimeMultipart) X509CertificateEx(org.nhindirect.stagent.cert.X509CertificateEx) MimeEntity(org.nhindirect.stagent.mail.MimeEntity) ByteArrayOutputStream(java.io.ByteArrayOutputStream) X509Certificate(java.security.cert.X509Certificate)

Example 12 with SMIMECryptographerImpl

use of org.nhindirect.stagent.cryptography.SMIMECryptographerImpl in project nhin-d by DirectProject.

the class CryptographerTest method testEncryptAndDecryptMimeEntity_hsmDecryption.

private void testEncryptAndDecryptMimeEntity_hsmDecryption(EncryptionAlgorithm encAlg) throws Exception {
    OptionsManager.destroyInstance();
    CryptoExtensions.registerJCEProviders();
    try {
        final PKCS11Credential cred = new BootstrappedPKCS11Credential("1Kingpuff");
        final MutableKeyStoreProtectionManager mgr = new StaticPKCS11TokenKeyStoreProtectionManager(cred, "", "");
        final CacheableKeyStoreManagerCertificateStore store = new CacheableKeyStoreManagerCertificateStore(mgr);
        store.add(TestUtils.getInternalCert("user1"));
        X509Certificate cert = TestUtils.getExternalCert("user1");
        SMIMECryptographerImpl cryptographer = new SMIMECryptographerImpl();
        cryptographer.setRecipientInformationFactory(new SplitDirectRecipientInformationFactory(pkcs11ProviderName, ""));
        cryptographer.setEncryptionAlgorithm(encAlg);
        MimeEntity entity = new MimeEntity();
        entity.setText("Hello world.");
        entity.setHeader(MimeStandard.ContentTypeHeader, "text/plain");
        entity.setHeader(MimeStandard.ContentTransferEncodingHeader, "7bit");
        MimeEntity encEntity = cryptographer.encrypt(entity, cert);
        assertNotNull(encEntity);
        // open up the pkcs11 store and find the private key
        KeyStore ks = KeyStore.getInstance("PKCS11");
        ks.load(null, "1Kingpuff".toCharArray());
        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;
                }
            }
        }
        MimeEntity decryEntity = cryptographer.decrypt(encEntity, decryptCert);
        assertNotNull(decryEntity);
        byte[] decryEntityBytes = EntitySerializer.Default.serializeToBytes(decryEntity);
        byte[] entityBytes = EntitySerializer.Default.serializeToBytes(entity);
        assertTrue(Arrays.equals(decryEntityBytes, entityBytes));
    } finally {
        System.setProperty("org.nhindirect.stagent.cryptography.JCESensitiveProviderName", "");
        System.setProperty("org.nhindirect.stagent.cryptography.JCESensitiveProviderClassNames", "");
        OptionsManager.destroyInstance();
    }
}
Also used : PKCS11Credential(org.nhindirect.common.crypto.PKCS11Credential) BootstrappedPKCS11Credential(org.nhindirect.common.crypto.impl.BootstrappedPKCS11Credential) PrivateKey(java.security.PrivateKey) SMIMECryptographerImpl(org.nhindirect.stagent.cryptography.SMIMECryptographerImpl) KeyStore(java.security.KeyStore) X509Certificate(java.security.cert.X509Certificate) SplitDirectRecipientInformationFactory(org.nhindirect.stagent.cryptography.activekeyops.SplitDirectRecipientInformationFactory) CacheableKeyStoreManagerCertificateStore(org.nhindirect.stagent.cert.impl.CacheableKeyStoreManagerCertificateStore) X509CertificateEx(org.nhindirect.stagent.cert.X509CertificateEx) MutableKeyStoreProtectionManager(org.nhindirect.common.crypto.MutableKeyStoreProtectionManager) StaticPKCS11TokenKeyStoreProtectionManager(org.nhindirect.common.crypto.impl.StaticPKCS11TokenKeyStoreProtectionManager) MimeEntity(org.nhindirect.stagent.mail.MimeEntity) BootstrappedPKCS11Credential(org.nhindirect.common.crypto.impl.BootstrappedPKCS11Credential) Key(java.security.Key) PrivateKey(java.security.PrivateKey) X509Certificate(java.security.cert.X509Certificate) Certificate(java.security.cert.Certificate)

Example 13 with SMIMECryptographerImpl

use of org.nhindirect.stagent.cryptography.SMIMECryptographerImpl in project nhin-d by DirectProject.

the class CryptographerTest method testSignMimeEntity_SHA256Digest_forceStrongDigest_assertValidation.

public void testSignMimeEntity_SHA256Digest_forceStrongDigest_assertValidation() throws Exception {
    X509CertificateEx certex = TestUtils.getInternalCert("user1");
    SMIMECryptographerImpl cryptographer = new SMIMECryptographerImpl();
    cryptographer.setDigestAlgorithm(DigestAlgorithm.SHA256);
    MimeEntity entity = new MimeEntity();
    entity.setText("Hello world.");
    entity.setHeader(MimeStandard.ContentTypeHeader, "text/plain");
    entity.setHeader(MimeStandard.ContentTransferEncodingHeader, "7bit");
    SignedEntity signedEnt = cryptographer.sign(entity, certex);
    assertNotNull(signedEnt);
    byte[] signedEntityBytes = EntitySerializer.Default.serializeToBytes(signedEnt.getContent());
    byte[] entityBytes = EntitySerializer.Default.serializeToBytes(entity);
    assertTrue(Arrays.equals(signedEntityBytes, entityBytes));
    assertNotNull(signedEnt.getSignature());
    X509Certificate cert = TestUtils.getExternalCert("user1");
    cryptographer.checkSignature(signedEnt, cert, new ArrayList<X509Certificate>());
}
Also used : X509CertificateEx(org.nhindirect.stagent.cert.X509CertificateEx) SMIMECryptographerImpl(org.nhindirect.stagent.cryptography.SMIMECryptographerImpl) MimeEntity(org.nhindirect.stagent.mail.MimeEntity) SignedEntity(org.nhindirect.stagent.cryptography.SignedEntity) X509Certificate(java.security.cert.X509Certificate)

Example 14 with SMIMECryptographerImpl

use of org.nhindirect.stagent.cryptography.SMIMECryptographerImpl in project nhin-d by DirectProject.

the class CryptographerTest method testEncryptAndSignMimeEntity.

public void testEncryptAndSignMimeEntity() throws Exception {
    X509Certificate cert = TestUtils.getInternalCACert("user1");
    SMIMECryptographerImpl cryptographer = new SMIMECryptographerImpl();
    MimeEntity entity = new MimeEntity();
    entity.setText("Hello world.");
    entity.setHeader(MimeStandard.ContentTypeHeader, "text/plain");
    entity.setHeader(MimeStandard.ContentTransferEncodingHeader, "7bit");
    MimeEntity encEntity = cryptographer.encrypt(entity, cert);
    assertNotNull(encEntity);
    X509CertificateEx certex = TestUtils.getInternalCert("user1");
    SignedEntity signedEnt = cryptographer.sign(entity, certex);
    assertNotNull(signedEnt);
    cryptographer.checkSignature(signedEnt, cert, new ArrayList<X509Certificate>());
}
Also used : SMIMECryptographerImpl(org.nhindirect.stagent.cryptography.SMIMECryptographerImpl) X509CertificateEx(org.nhindirect.stagent.cert.X509CertificateEx) MimeEntity(org.nhindirect.stagent.mail.MimeEntity) SignedEntity(org.nhindirect.stagent.cryptography.SignedEntity) X509Certificate(java.security.cert.X509Certificate)

Example 15 with SMIMECryptographerImpl

use of org.nhindirect.stagent.cryptography.SMIMECryptographerImpl in project nhin-d by DirectProject.

the class CryptographerTest method testEncryptWithSingleCert_wrongDecryptCert_assertFailDecrypt.

public void testEncryptWithSingleCert_wrongDecryptCert_assertFailDecrypt() throws Exception {
    X509Certificate cert = TestUtils.getExternalCert("user1");
    SMIMECryptographerImpl cryptographer = new SMIMECryptographerImpl();
    MimeEntity entity = new MimeEntity();
    entity.setText("Hello world.");
    entity.setHeader(MimeStandard.ContentTypeHeader, "text/plain");
    entity.setHeader(MimeStandard.ContentTransferEncodingHeader, "7bit");
    MimeEntity encEntity = cryptographer.encrypt(entity, cert);
    assertNotNull(encEntity);
    X509CertificateEx certex = TestUtils.getInternalCert("altnameonly");
    boolean exceptionOccured = false;
    try {
        cryptographer.decrypt(encEntity, certex);
    } catch (NHINDException e) {
        if (e.getError().equals(MimeError.Unexpected))
            ;
        exceptionOccured = true;
    }
    assertTrue(exceptionOccured);
}
Also used : SMIMECryptographerImpl(org.nhindirect.stagent.cryptography.SMIMECryptographerImpl) X509CertificateEx(org.nhindirect.stagent.cert.X509CertificateEx) MimeEntity(org.nhindirect.stagent.mail.MimeEntity) NHINDException(org.nhindirect.stagent.NHINDException) X509Certificate(java.security.cert.X509Certificate)

Aggregations

SMIMECryptographerImpl (org.nhindirect.stagent.cryptography.SMIMECryptographerImpl)18 MimeEntity (org.nhindirect.stagent.mail.MimeEntity)17 X509Certificate (java.security.cert.X509Certificate)15 X509CertificateEx (org.nhindirect.stagent.cert.X509CertificateEx)13 SignedEntity (org.nhindirect.stagent.cryptography.SignedEntity)5 SMIMEEnveloped (org.bouncycastle.mail.smime.SMIMEEnveloped)4 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 Key (java.security.Key)2 KeyStore (java.security.KeyStore)2 PrivateKey (java.security.PrivateKey)2 Certificate (java.security.cert.Certificate)2 InternetHeaders (javax.mail.internet.InternetHeaders)2 MimeMultipart (javax.mail.internet.MimeMultipart)2 NHINDException (org.nhindirect.stagent.NHINDException)2 SignatureValidationException (org.nhindirect.stagent.SignatureValidationException)2 ByteArrayInputStream (java.io.ByteArrayInputStream)1 SecretKey (javax.crypto.SecretKey)1 ContentType (javax.mail.internet.ContentType)1 CMSSignedData (org.bouncycastle.cms.CMSSignedData)1 MutableKeyStoreProtectionManager (org.nhindirect.common.crypto.MutableKeyStoreProtectionManager)1