Search in sources :

Example 11 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 writer with a given boundary.
     * @param entity The entities to serialize.
     * @param boundary The boundary string that will separate each entity.
     * @param writer The writer that the entities will be serialized to.
     */
public void serialize(Collection<MimeBodyPart> parts, String boundary, Writer writer) {
    if (parts == null || parts.size() == 0) {
        throw new IllegalArgumentException();
    }
    try {
        ByteArrayOutputStream oStream = new ByteArrayOutputStream();
        serialize(parts, boundary, oStream);
        oStream.flush();
        String str = oStream.toString("ASCII");
        writer.write(str, 0, str.length());
        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 12 with MimeException

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

the class SMIMECryptographerImpl method createSignatureEntity.

protected MimeMultipart createSignatureEntity(byte[] entity, Collection<X509Certificate> signingCertificates) {
    MimeMultipart retVal = null;
    try {
        final MimeBodyPart signedContent = new MimeBodyPart(new ByteArrayInputStream(entity));
        final ASN1EncodableVector signedAttrs = new ASN1EncodableVector();
        final SMIMECapabilityVector caps = new SMIMECapabilityVector();
        caps.addCapability(SMIMECapability.dES_EDE3_CBC);
        caps.addCapability(SMIMECapability.rC2_CBC, 128);
        caps.addCapability(SMIMECapability.dES_CBC);
        caps.addCapability(new DERObjectIdentifier("1.2.840.113549.1.7.1"));
        caps.addCapability(x509CertificateObjectsIdent);
        signedAttrs.add(new SMIMECapabilitiesAttribute(caps));
        final List<X509Certificate> certList = new ArrayList<X509Certificate>();
        final DirectSignedDataGenerator generator = sigFactory.createInstance();
        for (X509Certificate signer : signingCertificates) {
            if (signer instanceof X509CertificateEx) {
                generator.addSigner(((X509CertificateEx) signer).getPrivateKey(), signer, this.m_digestAlgorithm.getOID(), createAttributeTable(signedAttrs), null);
                certList.add(signer);
            }
        }
        final CertStore certsAndcrls = CertStore.getInstance("Collection", new CollectionCertStoreParameters(certList), CryptoExtensions.getJCEProviderNameForTypeAndAlgorithm("CertStore", "Collection"));
        generator.addCertificatesAndCRLs(certsAndcrls);
        final CMSProcessableBodyPart content = new CMSProcessableBodyPart(signedContent);
        final CMSSignedData signedData = generator.generate(content);
        final String header = "signed; protocol=\"application/pkcs7-signature\"; micalg=" + CryptoAlgorithmsHelper.toDigestAlgorithmMicalg(this.m_digestAlgorithm);
        //String encodedSig = Base64.encodeBase64String(signedData.getEncoded());
        final String encodedSig = StringUtils.newStringUtf8(Base64.encodeBase64(signedData.getEncoded(), true));
        retVal = new MimeMultipart(header.toString());
        final MimeBodyPart sig = new MimeBodyPart(new InternetHeaders(), encodedSig.getBytes("ASCII"));
        sig.addHeader("Content-Type", "application/pkcs7-signature; name=smime.p7s; smime-type=signed-data");
        sig.addHeader("Content-Disposition", "attachment; filename=\"smime.p7s\"");
        sig.addHeader("Content-Description", "S/MIME Cryptographic Signature");
        sig.addHeader("Content-Transfer-Encoding", "base64");
        retVal.addBodyPart(signedContent);
        retVal.addBodyPart(sig);
    } catch (MessagingException e) {
        throw new MimeException(MimeError.InvalidMimeEntity, e);
    } catch (IOException e) {
        throw new SignatureException(SignatureError.InvalidMultipartSigned, e);
    } catch (Exception e) {
        throw new NHINDException(MimeError.Unexpected, e);
    }
    return retVal;
}
Also used : InternetHeaders(javax.mail.internet.InternetHeaders) MessagingException(javax.mail.MessagingException) ArrayList(java.util.ArrayList) ASN1OctetString(org.bouncycastle.asn1.ASN1OctetString) IOException(java.io.IOException) DERObjectIdentifier(org.bouncycastle.asn1.DERObjectIdentifier) CMSSignedData(org.bouncycastle.cms.CMSSignedData) NHINDException(org.nhindirect.stagent.NHINDException) 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) CMSProcessableBodyPart(org.bouncycastle.mail.smime.CMSProcessableBodyPart) CollectionCertStoreParameters(java.security.cert.CollectionCertStoreParameters) MimeMultipart(javax.mail.internet.MimeMultipart) ByteArrayInputStream(java.io.ByteArrayInputStream) SMIMECapabilityVector(org.bouncycastle.asn1.smime.SMIMECapabilityVector) X509CertificateEx(org.nhindirect.stagent.cert.X509CertificateEx) ASN1EncodableVector(org.bouncycastle.asn1.ASN1EncodableVector) MimeException(org.nhindirect.stagent.mail.MimeException) MimeBodyPart(javax.mail.internet.MimeBodyPart) DirectSignedDataGenerator(org.nhindirect.stagent.cryptography.activekeyops.DirectSignedDataGenerator) SMIMECapabilitiesAttribute(org.bouncycastle.asn1.smime.SMIMECapabilitiesAttribute) CertStore(java.security.cert.CertStore)

Example 13 with MimeException

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

the class SMIMECryptographerImpl method encrypt.

/** 
     * Encrypts an entity using the provided certificates.
     * @param entity The entity that will be encrypted.
     * @param encryptingCertificate The public certificates that will be used to encrypt the message.
     * @return A MimeEntity containing the encrypted part.
     */
public MimeEntity encrypt(MimeEntity entity, Collection<X509Certificate> encryptingCertificates) {
    if (entity == null) {
        throw new IllegalArgumentException();
    }
    MimeBodyPart partToEncrypt = entity;
    MimeBodyPart encryptedPart = this.encrypt(partToEncrypt, encryptingCertificates);
    MimeEntity encryptedEntity = null;
    try {
        byte[] encBytes = EntitySerializer.Default.serializeToBytes(encryptedPart);
        ByteArrayInputStream inStream = new ByteArrayInputStream(EntitySerializer.Default.serializeToBytes(encryptedPart));
        encryptedEntity = new MimeEntity(inStream);
        if (LOGGER.isDebugEnabled()) {
            writePostEncypt(encBytes);
        }
        encryptedEntity.setHeader(MimeStandard.ContentTypeHeader, SMIMEStandard.EncryptedContentTypeHeaderValue);
    } catch (Exception e) {
        throw new MimeException(MimeError.Unexpected, e);
    }
    return encryptedEntity;
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) MimeEntity(org.nhindirect.stagent.mail.MimeEntity) MimeException(org.nhindirect.stagent.mail.MimeException) MimeBodyPart(javax.mail.internet.MimeBodyPart) 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)

Example 14 with MimeException

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

the class SMIMECryptographerImpl method encrypt.

/**
     * Encrypts a mulit part MIME entity using the provided certificates.
     * @param entity The entity that will be encrypted.
     * @param encryptingCertificates The public certificates that will be used to encrypt the message.
     * @return A MimeEntity containing the encrypted part.
     */
public MimeEntity encrypt(MimeMultipart mmEntity, Collection<X509Certificate> encryptingCertificates) {
    MimeEntity entToEncrypt = null;
    ByteArrayOutputStream oStream = new ByteArrayOutputStream();
    try {
        mmEntity.writeTo(oStream);
        oStream.flush();
        InternetHeaders headers = new InternetHeaders();
        headers.addHeader(MimeStandard.ContentTypeHeader, mmEntity.getContentType());
        entToEncrypt = new MimeEntity(headers, oStream.toByteArray());
        IOUtils.closeQuietly(oStream);
    } catch (Exception e) {
        throw new MimeException(MimeError.InvalidMimeEntity, e);
    }
    return this.encrypt(entToEncrypt, encryptingCertificates);
}
Also used : InternetHeaders(javax.mail.internet.InternetHeaders) MimeEntity(org.nhindirect.stagent.mail.MimeEntity) MimeException(org.nhindirect.stagent.mail.MimeException) ByteArrayOutputStream(java.io.ByteArrayOutputStream) 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)

Example 15 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 String representation.
     * @param stream A raw String representation of the entity.
     * @return A MimeMessage deserialized from the string.
     */
public MimeMessage deserialize(String messageText) {
    MimeMessage retVal = null;
    if (messageText == null || messageText.length() == 0) {
        throw new IllegalArgumentException();
    }
    try {
        ByteArrayInputStream inStream = new ByteArrayInputStream(messageText.getBytes("ASCII"));
        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)

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