Search in sources :

Example 1 with MimeException

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

the class SMIMECryptographerImpl method sign.

/**
     * Signs an entity with the provided certificates.
     * @param message The entity that will be signed.
     * @param signingCertificates The certificates used to sign the message.
     * @return A signed entity that consists of a multipart/signed entity containing the original entity and a message signature. 
     */
public SignedEntity sign(MimeEntity entity, Collection<X509Certificate> signingCertificates) {
    if (entity == null) {
        throw new IllegalArgumentException();
    }
    // Serialize message out as ASCII encoded...
    byte[] messageBytes = EntitySerializer.Default.serializeToBytes(entity);
    MimeMultipart mm = this.createSignatureEntity(messageBytes, signingCertificates);
    SignedEntity retVal = null;
    try {
        retVal = new SignedEntity(new ContentType(mm.getContentType()), mm);
    } catch (ParseException e) {
        throw new MimeException(MimeError.InvalidHeader, e);
    }
    return retVal;
}
Also used : ContentType(javax.mail.internet.ContentType) MimeMultipart(javax.mail.internet.MimeMultipart) MimeException(org.nhindirect.stagent.mail.MimeException) ParseException(javax.mail.internet.ParseException)

Example 2 with MimeException

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

the class SMIMECryptographerImpl method deserializeSignatureEnvelope.

/**
     * Extracts the ASN1 encoded signature data from the signed entity.
     * @param entity The entity containing the original signed part and the message signature.
     * @return A CMSSignedData object that contains the ASN1 encoded signature data of the message.
     */
public CMSSignedData deserializeSignatureEnvelope(SignedEntity entity) {
    if (entity == null) {
        throw new NHINDException();
    }
    CMSSignedData signed = null;
    try {
        //signed = new SMIMESigned(entity.getMimeMultipart());
        byte[] messageBytes = EntitySerializer.Default.serializeToBytes(entity.getContent());
        MimeBodyPart signedContent = null;
        signedContent = new MimeBodyPart(new ByteArrayInputStream(messageBytes));
        signed = new CMSSignedData(new CMSProcessableBodyPart(signedContent), entity.getMimeMultipart().getBodyPart(1).getInputStream());
    } catch (Exception e) {
        e.printStackTrace();
        throw new MimeException(MimeError.Unexpected, e);
    }
    return signed;
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) MimeException(org.nhindirect.stagent.mail.MimeException) MimeBodyPart(javax.mail.internet.MimeBodyPart) NHINDException(org.nhindirect.stagent.NHINDException) CMSSignedData(org.bouncycastle.cms.CMSSignedData) 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) CMSProcessableBodyPart(org.bouncycastle.mail.smime.CMSProcessableBodyPart)

Example 3 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 String.
     * @param entity The entity to serialize.
     * @return A raw String representation of the entity.
     */
public String serialize(MimePart message) {
    String retVal = "";
    try {
        ByteArrayOutputStream oStream = new ByteArrayOutputStream();
        serialize(message, 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 4 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 reader.
     * @param stream The reader containing the serialized entity.
     * @return A MimeMessage deserialized from the reader.
     */
public MimeMessage deserialize(Reader reader) {
    MimeMessage retVal = null;
    if (reader == null) {
        throw new IllegalArgumentException();
    }
    try {
        ByteArrayInputStream inStream = new ByteArrayInputStream(IOUtils.toByteArray(reader, "ASCII"));
        retVal = deserialize(inStream);
    } catch (IOException 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) IOException(java.io.IOException)

Example 5 with MimeException

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

the class SMIMECryptographerImpl method createEncryptedEnvelope.

private MimeBodyPart createEncryptedEnvelope(MimeBodyPart bodyPart, Collection<X509Certificate> encryptingCertificates) {
    if (bodyPart == null || encryptingCertificates == null || encryptingCertificates.size() == 0) {
        throw new IllegalArgumentException();
    }
    if (LOGGER.isDebugEnabled()) {
        writePreEncypt(EntitySerializer.Default.serializeToBytes(bodyPart));
    }
    SMIMEEnvelopedGenerator gen = new SMIMEEnvelopedGenerator();
    for (X509Certificate cert : encryptingCertificates) gen.addKeyTransRecipient(cert);
    MimeBodyPart retVal = null;
    try {
        final String encryAlgOID = this.m_encryptionAlgorithm.getOID();
        retVal = gen.generate(bodyPart, encryAlgOID, CryptoExtensions.getJCEProviderNameForTypeAndAlgorithm("Cipher", encryAlgOID));
    } catch (Exception e) {
        throw new MimeException(MimeError.Unexpected, e);
    }
    return retVal;
}
Also used : SMIMEEnvelopedGenerator(org.bouncycastle.mail.smime.SMIMEEnvelopedGenerator) MimeException(org.nhindirect.stagent.mail.MimeException) ASN1OctetString(org.bouncycastle.asn1.ASN1OctetString) MimeBodyPart(javax.mail.internet.MimeBodyPart) X509Certificate(java.security.cert.X509Certificate) 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)

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