Search in sources :

Example 6 with SMIMECryptographerImpl

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

the class CryptographerTest method testSignMimeEntity.

private void testSignMimeEntity(DigestAlgorithm digAlg) throws Exception {
    X509CertificateEx certex = TestUtils.getInternalCert("user1");
    SMIMECryptographerImpl cryptographer = new SMIMECryptographerImpl();
    cryptographer.setDigestAlgorithm(digAlg);
    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 7 with SMIMECryptographerImpl

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

the class SplitDirectRecipientInformation_getDecryptedContentTest method createSMIMEEnv.

protected SMIMEEnveloped createSMIMEEnv(X509Certificate cert) throws Exception {
    // get the cert
    if (cert == null)
        encCert = TestUtils.getInternalCert("user1");
    else
        encCert = cert;
    // create an encrypted message
    final MimeEntity entity = new MimeEntity();
    entity.setText("Hello world.");
    entity.setHeader(MimeStandard.ContentTypeHeader, "text/plain");
    entity.setHeader(MimeStandard.ContentTransferEncodingHeader, "7bit");
    final SMIMECryptographerImpl encryptor = new SMIMECryptographerImpl();
    return new SMIMEEnveloped(encryptor.encrypt(entity, Arrays.asList(encCert)));
}
Also used : SMIMECryptographerImpl(org.nhindirect.stagent.cryptography.SMIMECryptographerImpl) MimeEntity(org.nhindirect.stagent.mail.MimeEntity) SMIMEEnveloped(org.bouncycastle.mail.smime.SMIMEEnveloped)

Example 8 with SMIMECryptographerImpl

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

the class TrustModel_findTrustedSignatureTest method setUp.

@Override
public void setUp() throws Exception {
    CryptoExtensions.registerJCEProviders();
    // load sigCert A
    sigUser1 = TestUtils.getInternalCert("user1");
    // load sigCert A private certificate
    sigUser1CA = TestUtils.getInternalCACert("cacert");
    // load other anchor
    otherCert = TestUtils.loadCertificate("gm2552.der");
    // load the message that will be encrypted
    String testMessage = TestUtils.readResource("MultipartMimeMessage.txt");
    cryptographer = new SMIMECryptographerImpl();
    inMessage = new IncomingMessage(new Message(new ByteArrayInputStream(testMessage.getBytes())));
    signedEntity = cryptographer.sign(inMessage.getMessage(), sigUser1);
    CMSSignedData signatures = cryptographer.deserializeSignatureEnvelope(signedEntity);
    inMessage.setSignature(signatures);
}
Also used : Message(org.nhindirect.stagent.mail.Message) IncomingMessage(org.nhindirect.stagent.IncomingMessage) SMIMECryptographerImpl(org.nhindirect.stagent.cryptography.SMIMECryptographerImpl) IncomingMessage(org.nhindirect.stagent.IncomingMessage) ByteArrayInputStream(java.io.ByteArrayInputStream) CMSSignedData(org.bouncycastle.cms.CMSSignedData)

Example 9 with SMIMECryptographerImpl

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

the class CryptographerTest method testEncryptAndDecryptMimeEntity_sensitiveDataInPKCS11.

private void testEncryptAndDecryptMimeEntity_sensitiveDataInPKCS11(EncryptionAlgorithm encAlg) throws Exception {
    OptionsManager.destroyInstance();
    System.setProperty("org.nhindirect.stagent.cryptography.JCESensitiveProviderName", "SunPKCS11-SafeNeteTokenPro");
    System.setProperty("org.nhindirect.stagent.cryptography.JCESensitiveProviderClassNames", "sun.security.pkcs11.SunPKCS11;./src/test/resources/pkcs11Config/pkcs11.cfg");
    CryptoExtensions.registerJCEProviders();
    try {
        X509Certificate cert = TestUtils.getExternalCert("user1");
        SMIMECryptographerImpl cryptographer = new SMIMECryptographerImpl();
        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);
        X509CertificateEx certex = TestUtils.getInternalCert("user1");
        // 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", "Hello");
        System.setProperty("org.nhindirect.stagent.cryptography.JCESensitiveProviderClassNames", "sun.security.pkcs11.SunPKCS11");
        OptionsManager.destroyInstance();
    }
}
Also used : PrivateKey(java.security.PrivateKey) SMIMECryptographerImpl(org.nhindirect.stagent.cryptography.SMIMECryptographerImpl) X509CertificateEx(org.nhindirect.stagent.cert.X509CertificateEx) MimeEntity(org.nhindirect.stagent.mail.MimeEntity) KeyStore(java.security.KeyStore) X509Certificate(java.security.cert.X509Certificate) Key(java.security.Key) PrivateKey(java.security.PrivateKey) SecretKey(javax.crypto.SecretKey) X509Certificate(java.security.cert.X509Certificate) Certificate(java.security.cert.Certificate)

Example 10 with SMIMECryptographerImpl

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

the class CryptographerTest method testEncryptAndDecryptMimeEntity.

private void testEncryptAndDecryptMimeEntity(EncryptionAlgorithm encAlg) throws Exception {
    X509Certificate cert = TestUtils.getExternalCert("user1");
    SMIMECryptographerImpl cryptographer = new SMIMECryptographerImpl();
    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);
    X509CertificateEx certex = TestUtils.getInternalCert("user1");
    MimeEntity decryEntity = cryptographer.decrypt(encEntity, certex);
    assertNotNull(decryEntity);
    byte[] decryEntityBytes = EntitySerializer.Default.serializeToBytes(decryEntity);
    byte[] entityBytes = EntitySerializer.Default.serializeToBytes(entity);
    assertTrue(Arrays.equals(decryEntityBytes, entityBytes));
}
Also used : SMIMECryptographerImpl(org.nhindirect.stagent.cryptography.SMIMECryptographerImpl) X509CertificateEx(org.nhindirect.stagent.cert.X509CertificateEx) MimeEntity(org.nhindirect.stagent.mail.MimeEntity) 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