Search in sources :

Example 16 with MimeException

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

the class EntitySerializer method deserialize.

/**
     * Deserializes a MimeMessage from a raw byte array representation.
     * @param stream A raw byte array representation of the entity.
     * @return A MimeMessage deserialized from the byte array.
     */
public MimeMessage deserialize(byte[] messageBytes) {
    MimeMessage retVal = null;
    if (messageBytes == null || messageBytes.length == 0) {
        throw new IllegalArgumentException();
    }
    try {
        ByteArrayInputStream inStream = new ByteArrayInputStream(messageBytes);
        retVal = deserialize(inStream);
    } catch (Exception e) {
        throw new MimeException(MimeError.Unexpected, e);
    }
    return retVal;
}
Also used : MimeMessage(javax.mail.internet.MimeMessage) ByteArrayInputStream(java.io.ByteArrayInputStream) MimeException(org.nhindirect.stagent.mail.MimeException) MessagingException(javax.mail.MessagingException) IOException(java.io.IOException) MimeException(org.nhindirect.stagent.mail.MimeException)

Example 17 with MimeException

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

the class EntitySerializer method serialize.

/**
     * Serializes a collection of MimeBodyPart to a string with a given boundary.
     * @param entity The entities to serialize.
     * @param boundary The boundary string that will separate each entity.
     * @return A raw String representation of the serialized entities.
     */
public String serialize(Collection<MimeBodyPart> parts, String boundary) {
    String retVal = "";
    try {
        ByteArrayOutputStream oStream = new ByteArrayOutputStream();
        serialize(parts, boundary, oStream);
        oStream.flush();
        retVal = oStream.toString("ASCII");
        IOUtils.closeQuietly(oStream);
    } catch (Exception e) {
        throw new MimeException(MimeError.Unexpected, e);
    }
    return retVal;
}
Also used : MimeException(org.nhindirect.stagent.mail.MimeException) ByteArrayOutputStream(java.io.ByteArrayOutputStream) MessagingException(javax.mail.MessagingException) IOException(java.io.IOException) MimeException(org.nhindirect.stagent.mail.MimeException)

Example 18 with MimeException

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

the class EntitySerializer method serialize.

/**
     * Serializes a MimePart to a writer.
     * @param entity The entity to serialize.
     * @param stream The writer that the serialized object will be written to.
     */
public void serialize(MimePart message, Writer writer) {
    try {
        ByteArrayOutputStream oStream = new ByteArrayOutputStream();
        serialize(message, oStream);
        oStream.flush();
        IOUtils.write(oStream.toByteArray(), writer, "ASCII");
        IOUtils.closeQuietly(oStream);
    } catch (Exception e) {
        throw new MimeException(MimeError.Unexpected, e);
    }
}
Also used : MimeException(org.nhindirect.stagent.mail.MimeException) ByteArrayOutputStream(java.io.ByteArrayOutputStream) MessagingException(javax.mail.MessagingException) IOException(java.io.IOException) MimeException(org.nhindirect.stagent.mail.MimeException)

Example 19 with MimeException

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

the class DefaultNHINDAgent method signAndEncryptMessage.

//
// First sign, THEN encrypt the message
//
@SuppressWarnings("unchecked")
protected void signAndEncryptMessage(OutgoingMessage message) {
    SignedEntity signedEntity = cryptographer.sign(message.getMessage(), message.getSender().getCertificates());
    try {
        if (encryptionEnabled) {
            MimeEntity encryptedEntity = cryptographer.encrypt(signedEntity.getMimeMultipart(), message.getRecipients().getCertificates());
            //
            // Alter message content to contain encrypted data
            //
            InternetHeaders headers = new InternetHeaders();
            Enumeration<Header> eHeaders = message.getMessage().getAllHeaders();
            while (eHeaders.hasMoreElements()) {
                Header hdr = eHeaders.nextElement();
                headers.setHeader(hdr.getName(), hdr.getValue());
            }
            eHeaders = encryptedEntity.getAllHeaders();
            while (eHeaders.hasMoreElements()) {
                Header hdr = (Header) eHeaders.nextElement();
                headers.setHeader(hdr.getName(), hdr.getValue());
            }
            Message msg = new Message(headers, encryptedEntity.getContentAsBytes());
            message.setMessage(msg);
        } else {
            InternetHeaders headers = new InternetHeaders();
            Enumeration<Header> eHeaders = message.getMessage().getAllHeaders();
            while (eHeaders.hasMoreElements()) {
                Header hdr = eHeaders.nextElement();
                headers.setHeader(hdr.getName(), hdr.getValue());
            }
            headers.setHeader(MimeStandard.ContentTypeHeader, signedEntity.getMimeMultipart().getContentType());
            Message msg = new Message(headers, signedEntity.getEntityBodyAsBytes());
            message.setMessage(msg);
        }
    } catch (Exception e) {
        throw new MimeException(MimeError.InvalidMimeEntity, e);
    }
}
Also used : InternetHeaders(javax.mail.internet.InternetHeaders) Header(javax.mail.Header) WrappedMessage(org.nhindirect.stagent.mail.WrappedMessage) Message(org.nhindirect.stagent.mail.Message) MimeMessage(javax.mail.internet.MimeMessage) MimeEntity(org.nhindirect.stagent.mail.MimeEntity) MimeException(org.nhindirect.stagent.mail.MimeException) SignedEntity(org.nhindirect.stagent.cryptography.SignedEntity) MessagingException(javax.mail.MessagingException) TrustException(org.nhindirect.stagent.trust.TrustException) MimeException(org.nhindirect.stagent.mail.MimeException) PolicyRequiredException(org.nhindirect.policy.PolicyRequiredException) PolicyParseException(org.nhindirect.policy.PolicyParseException) IOException(java.io.IOException) PolicyProcessException(org.nhindirect.policy.PolicyProcessException)

Example 20 with MimeException

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

Aggregations

MimeException (org.nhindirect.stagent.mail.MimeException)20 IOException (java.io.IOException)19 MessagingException (javax.mail.MessagingException)18 ByteArrayOutputStream (java.io.ByteArrayOutputStream)9 ByteArrayInputStream (java.io.ByteArrayInputStream)8 ParseException (javax.mail.internet.ParseException)8 NHINDException (org.nhindirect.stagent.NHINDException)6 SignatureValidationException (org.nhindirect.stagent.SignatureValidationException)6 MimeBodyPart (javax.mail.internet.MimeBodyPart)5 MimeMessage (javax.mail.internet.MimeMessage)5 MimeEntity (org.nhindirect.stagent.mail.MimeEntity)5 InternetHeaders (javax.mail.internet.InternetHeaders)4 MimeMultipart (javax.mail.internet.MimeMultipart)4 CMSSignedData (org.bouncycastle.cms.CMSSignedData)3 X509Certificate (java.security.cert.X509Certificate)2 Header (javax.mail.Header)2 ContentType (javax.mail.internet.ContentType)2 ASN1OctetString (org.bouncycastle.asn1.ASN1OctetString)2 CMSProcessableBodyPart (org.bouncycastle.mail.smime.CMSProcessableBodyPart)2 X509CertificateEx (org.nhindirect.stagent.cert.X509CertificateEx)2