Search in sources :

Example 6 with MimeEntity

use of org.nhindirect.stagent.mail.MimeEntity in project nhin-d by DirectProject.

the class SMIMECryptographerImpl_createSignatureEntityTest method testCreateSignatureEntity_hsmSignatureGenerator_assertEntityCreatedAndMatchesControl.

public void testCreateSignatureEntity_hsmSignatureGenerator_assertEntityCreatedAndMatchesControl() throws Exception {
    final String installedAlias = "JunitTestKey";
    /**
         * 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());
        // delete the entry in case it exists
        try {
            ks.deleteEntry(installedAlias);
        } catch (Exception e) {
        /*no-op */
        }
        // add the signing cert and private key into the token
        final X509Certificate sigCertBPrivate = (X509CertificateEx) TestUtils.loadCertificate("certCheckB.p12");
        try {
            ks.setKeyEntry(installedAlias, ((X509CertificateEx) sigCertBPrivate).getPrivateKey(), null, new Certificate[] { sigCertBPrivate });
            final KeyStore.PrivateKeyEntry entry = (KeyStore.PrivateKeyEntry) ks.getEntry(installedAlias, null);
            final X509Certificate signerCert = X509CertificateEx.fromX509Certificate((X509Certificate) entry.getCertificate(), entry.getPrivateKey());
            SplitProviderDirectSignedDataGeneratorFactory factory = new SplitProviderDirectSignedDataGeneratorFactory(pkcs11ProvName, "BC");
            final SMIMECryptographerImpl impl = new SMIMECryptographerImpl();
            impl.setSignedDataGeneratorFactory(factory);
            final String testMessage = TestUtils.readResource("MultipartMimeMessage.txt");
            final MimeEntity ent = new Message(new ByteArrayInputStream(testMessage.getBytes())).extractEntityForSignature(true);
            byte[] bytesToSign = EntitySerializer.Default.serializeToBytes(ent);
            final MimeMultipart mm = impl.createSignatureEntity(bytesToSign, Arrays.asList(signerCert));
            assertNotNull(mm);
            assertEquals(2, mm.getCount());
            validatedSignatureHeaders(mm);
            // now create the control
            final SMIMECryptographerImpl controllImpl = new SMIMECryptographerImpl();
            final MimeMultipart controllmm = controllImpl.createSignatureEntity(bytesToSign, Arrays.asList(sigCertBPrivate));
            assertNotNull(controllmm);
            assertEquals(2, controllmm.getCount());
            // make sure the signatures can be verified
            // the actual byte data may not be the same due to 
            // randomness in the signature
            validateSignature(deserializeSignatureEnvelope(mm), sigCertBPrivate);
            validateSignature(deserializeSignatureEnvelope(controllmm), sigCertBPrivate);
        } finally {
            ks.deleteEntry(installedAlias);
        }
    }
}
Also used : Message(org.nhindirect.stagent.mail.Message) KeyStore(java.security.KeyStore) X509Certificate(java.security.cert.X509Certificate) X509CertificateEx(org.nhindirect.stagent.cert.X509CertificateEx) ByteArrayInputStream(java.io.ByteArrayInputStream) MimeMultipart(javax.mail.internet.MimeMultipart) MimeEntity(org.nhindirect.stagent.mail.MimeEntity) SplitProviderDirectSignedDataGeneratorFactory(org.nhindirect.stagent.cryptography.activekeyops.SplitProviderDirectSignedDataGeneratorFactory)

Example 7 with MimeEntity

use of org.nhindirect.stagent.mail.MimeEntity in project nhin-d by DirectProject.

the class CryptographerTest method testEncryptWithSingleCert_decryptWithMutlipeCerts_onlyOneCertCorrect_assertDecrypted.

public void testEncryptWithSingleCert_decryptWithMutlipeCerts_onlyOneCertCorrect_assertDecrypted() 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 certex1 = TestUtils.getInternalCert("altnameonly");
    X509CertificateEx certex2 = TestUtils.getInternalCert("user1");
    MimeEntity decryEntity = cryptographer.decrypt(encEntity, Arrays.asList(certex1, certex2));
    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)

Example 8 with MimeEntity

use of org.nhindirect.stagent.mail.MimeEntity in project nhin-d by DirectProject.

the class CryptographerTest method testSignMimeEntity_MD5Digest_doNotforceStrongDigest_assertValidation.

public void testSignMimeEntity_MD5Digest_doNotforceStrongDigest_assertValidation() throws Exception {
    X509CertificateEx certex = TestUtils.getInternalCert("user1");
    SMIMECryptographerImpl cryptographer = new SMIMECryptographerImpl();
    cryptographer.setDigestAlgorithm(DigestAlgorithm.MD5);
    cryptographer.setStrongDigestEnforced(false);
    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 9 with MimeEntity

use of org.nhindirect.stagent.mail.MimeEntity 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 10 with MimeEntity

use of org.nhindirect.stagent.mail.MimeEntity 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)

Aggregations

MimeEntity (org.nhindirect.stagent.mail.MimeEntity)34 X509Certificate (java.security.cert.X509Certificate)20 X509CertificateEx (org.nhindirect.stagent.cert.X509CertificateEx)18 SMIMECryptographerImpl (org.nhindirect.stagent.cryptography.SMIMECryptographerImpl)17 ByteArrayInputStream (java.io.ByteArrayInputStream)10 MessagingException (javax.mail.MessagingException)9 IOException (java.io.IOException)8 MimeMultipart (javax.mail.internet.MimeMultipart)8 SignedEntity (org.nhindirect.stagent.cryptography.SignedEntity)7 ByteArrayOutputStream (java.io.ByteArrayOutputStream)6 Message (org.nhindirect.stagent.mail.Message)6 MimeException (org.nhindirect.stagent.mail.MimeException)6 ArrayList (java.util.ArrayList)5 InternetHeaders (javax.mail.internet.InternetHeaders)5 SMIMEEnveloped (org.bouncycastle.mail.smime.SMIMEEnveloped)5 NHINDException (org.nhindirect.stagent.NHINDException)5 SignatureValidationException (org.nhindirect.stagent.SignatureValidationException)5 KeyStore (java.security.KeyStore)3 MimeBodyPart (javax.mail.internet.MimeBodyPart)3 MimeMessage (javax.mail.internet.MimeMessage)3