Search in sources :

Example 16 with Message

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

the class DefaultNHINDAgent method decryptSignedContent.

/*
     * Decrypts the signed message
     */
@SuppressWarnings("unchecked")
protected void decryptSignedContent(IncomingMessage message) {
    MimeEntity decryptedEntity = this.decryptMessage(message);
    CMSSignedData signatures;
    MimeEntity payload;
    try {
        if (SMIMEStandard.isContentEnvelopedSignature(new ContentType(decryptedEntity.getContentType()))) {
            signatures = cryptographer.deserializeEnvelopedSignature(decryptedEntity);
            payload = new MimeEntity(new ByteArrayInputStream(signatures.getContentInfo().getEncoded()));
        } else if (SMIMEStandard.isContentMultipartSignature(new ContentType(decryptedEntity.getContentType()))) {
            //
            // Extract the signature envelope. That contains both the signature and the actual message content
            //
            ByteArrayDataSource dataSource = new ByteArrayDataSource(decryptedEntity.getRawInputStream(), decryptedEntity.getContentType());
            MimeMultipart verifyMM = new MimeMultipart(dataSource);
            SignedEntity signedEntity = SignedEntity.load(verifyMM);
            signatures = cryptographer.deserializeSignatureEnvelope(signedEntity);
            payload = signedEntity.getContent();
        } else {
            throw new AgentException(AgentError.UnsignedMessage);
        }
        message.setSignature(signatures);
        //
        // Alter body to contain actual content. Also clean up mime headers on the message that were there to support
        // signatures etc
        //         	
        InternetHeaders headers = new InternetHeaders();
        // remove all mime headers
        Enumeration<Header> eHeaders = message.getMessage().getAllHeaders();
        while (eHeaders.hasMoreElements()) {
            Header hdr = (Header) eHeaders.nextElement();
            if (!MimeStandard.startsWith(hdr.getName(), MimeStandard.HeaderPrefix))
                headers.setHeader(hdr.getName(), hdr.getValue());
        }
        // add back in headers from original message
        eHeaders = payload.getAllHeaders();
        while (eHeaders.hasMoreElements()) {
            Header hdr = (Header) eHeaders.nextElement();
            headers.setHeader(hdr.getName(), hdr.getValue());
        }
        Message msg = new Message(headers, payload.getContentAsBytes());
        message.setMessage(msg);
    } catch (MessagingException e) {
        throw new MimeException(MimeError.InvalidBody, e);
    } catch (IOException e) {
        throw new MimeException(MimeError.InvalidBody, e);
    }
}
Also used : ContentType(javax.mail.internet.ContentType) InternetHeaders(javax.mail.internet.InternetHeaders) WrappedMessage(org.nhindirect.stagent.mail.WrappedMessage) Message(org.nhindirect.stagent.mail.Message) MimeMessage(javax.mail.internet.MimeMessage) MessagingException(javax.mail.MessagingException) IOException(java.io.IOException) CMSSignedData(org.bouncycastle.cms.CMSSignedData) Header(javax.mail.Header) ByteArrayInputStream(java.io.ByteArrayInputStream) MimeMultipart(javax.mail.internet.MimeMultipart) MimeEntity(org.nhindirect.stagent.mail.MimeEntity) MimeException(org.nhindirect.stagent.mail.MimeException) ByteArrayDataSource(javax.mail.util.ByteArrayDataSource) SignedEntity(org.nhindirect.stagent.cryptography.SignedEntity)

Example 17 with Message

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

the class NHINDAgentTest method testRejectMessageOnRoutingTamper_policyFalse_assertDecrtyped.

public void testRejectMessageOnRoutingTamper_policyFalse_assertDecrtyped() throws Exception {
    /*
		 * EncryptedMessage2
		 */
    DefaultNHINDAgent agent = TestUtils.getStockAgent(Arrays.asList(new String[] { "securehealthemail.com" }));
    String testMessage = TestUtils.readResource("EncryptedMessage2.txt");
    Message originalMsg = new Message(new MimeMessage(null, new ByteArrayInputStream(testMessage.getBytes("ASCII"))));
    // add an extra recipient that should not receive this message
    final NHINDAddressCollection tamperedRecips = new NHINDAddressCollection();
    tamperedRecips.add(new NHINDAddress("ryan@securehealthemail.com"));
    tamperedRecips.add(new NHINDAddress("john@securehealthemail.com"));
    final IncomingMessage inMessage = new IncomingMessage(originalMsg, tamperedRecips, new NHINDAddress(originalMsg.getFrom()[0].toString()));
    IncomingMessage strippedAndVerifiesMessage = agent.processIncoming(inMessage);
    assertNotNull(strippedAndVerifiesMessage);
    assertTrue(strippedAndVerifiesMessage.getMessage().toString().length() > 0);
}
Also used : Message(org.nhindirect.stagent.mail.Message) MimeMessage(javax.mail.internet.MimeMessage) MimeMessage(javax.mail.internet.MimeMessage) ByteArrayInputStream(java.io.ByteArrayInputStream) DefaultNHINDAgent(org.nhindirect.stagent.DefaultNHINDAgent)

Example 18 with Message

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

the class SMIMECryptographerImpl_checkSignature_Test method setUp.

@Override
public void setUp() throws Exception {
    CryptoExtensions.registerJCEProviders();
    // load sigCert A
    sigCertA = TestUtils.loadCertificate("certCheckA.der");
    // load sigCert A private certificate
    sigCertAPrivate = TestUtils.loadCertificate("certCheckA.p12");
    // load sigCert B
    sigCertB = TestUtils.loadCertificate("certCheckB.der");
    // load sigCert B
    sigCertBPrivate = TestUtils.loadCertificate("certCheckB.p12");
    // load sigCert anchor
    sigCertAnchor = TestUtils.loadCertificate("Check Signature CA.der");
    // load other anchor
    otherCert = TestUtils.loadCertificate("gm2552.der");
    // load the message that will be encrypted
    String testMessage = TestUtils.readResource("MultipartMimeMessage.txt");
    cryptographer = new SMIMECryptographerImpl();
    signedEntity = cryptographer.sign(new Message(new ByteArrayInputStream(testMessage.getBytes())), sigCertAPrivate);
}
Also used : Message(org.nhindirect.stagent.mail.Message) ByteArrayInputStream(java.io.ByteArrayInputStream)

Example 19 with Message

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

the class SMIMECryptographerImpl_checkSignature_Test method testMutlipleSigs_sameSignAndValidationCert_assertValidSignature.

public void testMutlipleSigs_sameSignAndValidationCert_assertValidSignature() throws Exception {
    // load the message that will be encrypted
    String testMessage = TestUtils.readResource("MultipartMimeMessage.txt");
    cryptographer = new SMIMECryptographerImpl();
    signedEntity = cryptographer.sign(new Message(new ByteArrayInputStream(testMessage.getBytes())), Arrays.asList(sigCertAPrivate, sigCertBPrivate));
    cryptographer.checkSignature(signedEntity, sigCertA, Arrays.asList(sigCertAnchor));
    cryptographer.checkSignature(signedEntity, sigCertB, Arrays.asList(sigCertAnchor));
}
Also used : Message(org.nhindirect.stagent.mail.Message) ByteArrayInputStream(java.io.ByteArrayInputStream)

Example 20 with Message

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

Aggregations

Message (org.nhindirect.stagent.mail.Message)25 MimeMessage (javax.mail.internet.MimeMessage)17 ByteArrayInputStream (java.io.ByteArrayInputStream)14 MessagingException (javax.mail.MessagingException)7 NHINDAddressCollection (org.nhindirect.stagent.NHINDAddressCollection)7 ArrayList (java.util.ArrayList)5 MimeMultipart (javax.mail.internet.MimeMultipart)5 NHINDAddress (org.nhindirect.stagent.NHINDAddress)5 MimeEntity (org.nhindirect.stagent.mail.MimeEntity)5 IOException (java.io.IOException)4 X509Certificate (java.security.cert.X509Certificate)4 MailAddress (org.apache.mailet.MailAddress)4 NotificationMessage (org.nhindirect.stagent.mail.notifications.NotificationMessage)4 Header (javax.mail.Header)3 InternetHeaders (javax.mail.internet.InternetHeaders)3 Mail (org.apache.mailet.Mail)3 CMSSignedData (org.bouncycastle.cms.CMSSignedData)3 InvocationOnMock (org.mockito.invocation.InvocationOnMock)3 MessageProcessResult (org.nhindirect.gateway.smtp.MessageProcessResult)3 SmtpAgent (org.nhindirect.gateway.smtp.SmtpAgent)3