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