Search in sources :

Example 21 with MimeEntity

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

Example 22 with MimeEntity

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

the class SMIMECryptographerImpl_createSignatureEntityTest method deserializeSignatureEnvelope.

protected CMSSignedData deserializeSignatureEnvelope(MimeMultipart mm) throws Exception {
    final MimeEntity contentEntity = contentToMimeEntity(mm.getBodyPart(0));
    byte[] messageBytes = EntitySerializer.Default.serializeToBytes(contentEntity);
    MimeBodyPart signedContent = null;
    signedContent = new MimeBodyPart(new ByteArrayInputStream(messageBytes));
    return new CMSSignedData(new CMSProcessableBodyPart(signedContent), mm.getBodyPart(1).getInputStream());
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) MimeEntity(org.nhindirect.stagent.mail.MimeEntity) MimeBodyPart(javax.mail.internet.MimeBodyPart) CMSSignedData(org.bouncycastle.cms.CMSSignedData) CMSProcessableBodyPart(org.bouncycastle.mail.smime.CMSProcessableBodyPart)

Example 23 with MimeEntity

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

the class SMIMECryptographerImpl_createSignatureEntityTest method testCreateSignatureEntity_difSigAndDigestGenerators_assertEntityCreatedAndMatchesControl.

public void testCreateSignatureEntity_difSigAndDigestGenerators_assertEntityCreatedAndMatchesControl() throws Exception {
    SplitProviderDirectSignedDataGeneratorFactory factory = new SplitProviderDirectSignedDataGeneratorFactory("SunRsaSign", "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 X509Certificate sigCertBPrivate = TestUtils.loadCertificate("certCheckB.p12");
    final MimeMultipart mm = impl.createSignatureEntity(bytesToSign, Arrays.asList(sigCertBPrivate));
    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 match
    final MimeEntity signedContent = contentToMimeEntity(mm.getBodyPart(1));
    final MimeEntity controlSignedContent = contentToMimeEntity(controllmm.getBodyPart(1));
    assertTrue(Arrays.equals(signedContent.getContentAsBytes(), controlSignedContent.getContentAsBytes()));
    // verify the signatures
    validateSignature(deserializeSignatureEnvelope(mm), sigCertBPrivate);
    validateSignature(deserializeSignatureEnvelope(controllmm), sigCertBPrivate);
}
Also used : Message(org.nhindirect.stagent.mail.Message) ByteArrayInputStream(java.io.ByteArrayInputStream) MimeMultipart(javax.mail.internet.MimeMultipart) MimeEntity(org.nhindirect.stagent.mail.MimeEntity) X509Certificate(java.security.cert.X509Certificate) SplitProviderDirectSignedDataGeneratorFactory(org.nhindirect.stagent.cryptography.activekeyops.SplitProviderDirectSignedDataGeneratorFactory)

Example 24 with MimeEntity

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

the class SMIMECryptographerImpl_createSignatureEntityTest method testCreateSignatureEntity_defaultSigGenerator_assertEntityCreated.

/*
	 * This is the control test
	 */
public void testCreateSignatureEntity_defaultSigGenerator_assertEntityCreated() throws Exception {
    final SMIMECryptographerImpl impl = new SMIMECryptographerImpl();
    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 X509Certificate sigCertBPrivate = TestUtils.loadCertificate("certCheckB.p12");
    final MimeMultipart mm = impl.createSignatureEntity(bytesToSign, Arrays.asList(sigCertBPrivate));
    assertNotNull(mm);
    assertEquals(2, mm.getCount());
    validatedSignatureHeaders(mm);
    validateSignature(deserializeSignatureEnvelope(mm), sigCertBPrivate);
}
Also used : Message(org.nhindirect.stagent.mail.Message) ByteArrayInputStream(java.io.ByteArrayInputStream) MimeMultipart(javax.mail.internet.MimeMultipart) MimeEntity(org.nhindirect.stagent.mail.MimeEntity) X509Certificate(java.security.cert.X509Certificate)

Example 25 with MimeEntity

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

the class DefaultDirectRecipientInformationFactory_createInstanceTest method createSMIMEEnv.

protected SMIMEEnveloped createSMIMEEnv() throws Exception {
    // get the cert
    final X509Certificate cert = TestUtils.getExternalCert("user1");
    // 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(cert)));
}
Also used : SMIMECryptographerImpl(org.nhindirect.stagent.cryptography.SMIMECryptographerImpl) MimeEntity(org.nhindirect.stagent.mail.MimeEntity) SMIMEEnveloped(org.bouncycastle.mail.smime.SMIMEEnveloped) X509Certificate(java.security.cert.X509Certificate)

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