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;
}
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;
}
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;
}
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;
}
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;
}
Aggregations