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