Search in sources :

Example 6 with MimeException

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

the class SMIMECryptographerImpl method decrypt.

/**
     * Decrypts an entity with the provided certificates' private key.
     * @param encryptedEntity The entity that will be decrypted.
     * @param decryptingCertificate The certificates whose private keys will be used to decrypt the message.
     * @return A MimeEntity containing the decrypted part.
     */
public MimeEntity decrypt(MimeEntity encryptedEntity, Collection<X509CertificateEx> decryptingCertificates) {
    if (decryptingCertificates == null || decryptingCertificates.size() == 0) {
        throw new IllegalArgumentException();
    }
    MimeEntity retEntity = null;
    try {
        if (LOGGER.isDebugEnabled()) {
            final byte[] encryptedContent = encryptedEntity.getContentAsBytes();
            writePreDecrypt(encryptedContent);
        }
        final SMIMEEnveloped m = new SMIMEEnveloped(encryptedEntity);
        if (!this.isAllowedEncryptionAlgorithm(m.getEncryptionAlgOID()))
            throw new NHINDException(MimeError.DisallowedEncryptionAlgorithm, "The encryption algorithm " + m.getEncryptionAlgOID() + " is not allowed");
        for (X509CertificateEx decryptCert : decryptingCertificates) {
            final RecipientId recId = generateRecipientSelector(decryptCert);
            final RecipientInformationStore recipients = m.getRecipientInfos();
            final DirectRecipientInformation recipient = decFactory.createInstance(recipients.get(recId), m);
            if (recipient == null)
                continue;
            final byte[] decryptedPayload = recipient.getDecryptedContent(decryptCert.getPrivateKey());
            if (LOGGER.isDebugEnabled()) {
                writePostDecrypt(decryptedPayload);
            }
            final ByteArrayInputStream inStream = new ByteArrayInputStream(decryptedPayload);
            retEntity = new MimeEntity(inStream);
            break;
        }
    } catch (MessagingException e) {
        throw new MimeException(MimeError.InvalidMimeEntity, e);
    } catch (Exception e) {
        throw new MimeException(MimeError.Unexpected, e);
    }
    if (retEntity == null) {
        throw new NHINDException(MimeError.Unexpected, "None of the the provided decryption certs were found in message's RecipientsInfo set.");
    }
    return retEntity;
}
Also used : RecipientId(org.bouncycastle.cms.RecipientId) MessagingException(javax.mail.MessagingException) DirectRecipientInformation(org.nhindirect.stagent.cryptography.activekeyops.DirectRecipientInformation) SMIMEEnveloped(org.bouncycastle.mail.smime.SMIMEEnveloped) NHINDException(org.nhindirect.stagent.NHINDException) MessagingException(javax.mail.MessagingException) MimeException(org.nhindirect.stagent.mail.MimeException) NHINDException(org.nhindirect.stagent.NHINDException) ParseException(javax.mail.internet.ParseException) IOException(java.io.IOException) SignatureValidationException(org.nhindirect.stagent.SignatureValidationException) X509CertificateEx(org.nhindirect.stagent.cert.X509CertificateEx) ByteArrayInputStream(java.io.ByteArrayInputStream) MimeEntity(org.nhindirect.stagent.mail.MimeEntity) RecipientInformationStore(org.bouncycastle.cms.RecipientInformationStore) MimeException(org.nhindirect.stagent.mail.MimeException)

Example 7 with MimeException

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

the class SignedEntity method getEntityBodyAsBytes.

/**
     * Gets the content (body) of the signed entity as a byte array.  This includes both the content part and the 
     * signature part.
     * @return The content (body) of the message as a byte array.
     */
public byte[] getEntityBodyAsBytes() {
    byte[] retVal = null;
    ByteArrayOutputStream oStream = new ByteArrayOutputStream();
    try {
        originalMMPart.writeTo(oStream);
        retVal = oStream.toByteArray();
        IOUtils.closeQuietly(oStream);
    } catch (Exception e) {
        throw new MimeException(MimeError.InvalidMimeEntity, e);
    }
    return retVal;
}
Also used : MimeException(org.nhindirect.stagent.mail.MimeException) ByteArrayOutputStream(java.io.ByteArrayOutputStream) MimeException(org.nhindirect.stagent.mail.MimeException) ParseException(javax.mail.internet.ParseException) MessagingException(javax.mail.MessagingException) IOException(java.io.IOException)

Example 8 with MimeException

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

the class EntitySerializer method serializeToBytes.

/**
     * Serializes a collection of MimeBodyPart to a byte array with a given boundary.
     * @param entity The entities to serialize.
     * @param boundary The boundary string that will separate each entity.
     * @return A raw byte array representation of the serialized entities.
     */
public byte[] serializeToBytes(Collection<MimeBodyPart> parts, String boundary) {
    byte[] retVal = null;
    try {
        ByteArrayOutputStream oStream = new ByteArrayOutputStream();
        serialize(parts, boundary, oStream);
        oStream.flush();
        retVal = oStream.toByteArray();
    } 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 9 with MimeException

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

the class EntitySerializer method serializeToBytes.

/**
     * Serializes a MimePart to a byte array.
     * @param entity The entity to serialize.
     * @return A raw byte representation of the entity.
     */
public byte[] serializeToBytes(MimePart message) {
    byte[] retVal;
    try {
        ByteArrayOutputStream oStream = new ByteArrayOutputStream();
        serialize(message, oStream);
        oStream.flush();
        retVal = oStream.toByteArray();
        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 10 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 an output stream with a given boundary.
     * @param entity The entities to serialize.
     * @param boundary The boundary string that will separate each entity.
     * @param stream The output stream that the entities will be serialized to.
     */
public void serialize(Collection<MimeBodyPart> parts, String boundary, OutputStream stream) {
    if (parts == null || parts.size() == 0) {
        throw new IllegalArgumentException();
    }
    try {
        MimeMultipart mm = new MimeMultipart();
        for (MimeBodyPart part : parts) {
            mm.addBodyPart(part);
        }
        ByteArrayOutputStream oStream = new ByteArrayOutputStream();
        mm.writeTo(oStream);
        oStream.flush();
    } catch (Exception e) {
        throw new MimeException(MimeError.Unexpected, e);
    }
}
Also used : MimeMultipart(javax.mail.internet.MimeMultipart) MimeException(org.nhindirect.stagent.mail.MimeException) ByteArrayOutputStream(java.io.ByteArrayOutputStream) MimeBodyPart(javax.mail.internet.MimeBodyPart) MessagingException(javax.mail.MessagingException) IOException(java.io.IOException) MimeException(org.nhindirect.stagent.mail.MimeException)

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