use of org.nhindirect.stagent.mail.Message in project nhin-d by DirectProject.
the class DefaultNHINDAgent method decryptSignedContent.
/*
* Decrypts the signed message
*/
@SuppressWarnings("unchecked")
protected void decryptSignedContent(IncomingMessage message) {
MimeEntity decryptedEntity = this.decryptMessage(message);
CMSSignedData signatures;
MimeEntity payload;
try {
if (SMIMEStandard.isContentEnvelopedSignature(new ContentType(decryptedEntity.getContentType()))) {
signatures = cryptographer.deserializeEnvelopedSignature(decryptedEntity);
payload = new MimeEntity(new ByteArrayInputStream(signatures.getContentInfo().getEncoded()));
} else if (SMIMEStandard.isContentMultipartSignature(new ContentType(decryptedEntity.getContentType()))) {
//
// Extract the signature envelope. That contains both the signature and the actual message content
//
ByteArrayDataSource dataSource = new ByteArrayDataSource(decryptedEntity.getRawInputStream(), decryptedEntity.getContentType());
MimeMultipart verifyMM = new MimeMultipart(dataSource);
SignedEntity signedEntity = SignedEntity.load(verifyMM);
signatures = cryptographer.deserializeSignatureEnvelope(signedEntity);
payload = signedEntity.getContent();
} else {
throw new AgentException(AgentError.UnsignedMessage);
}
message.setSignature(signatures);
//
// Alter body to contain actual content. Also clean up mime headers on the message that were there to support
// signatures etc
//
InternetHeaders headers = new InternetHeaders();
// remove all mime headers
Enumeration<Header> eHeaders = message.getMessage().getAllHeaders();
while (eHeaders.hasMoreElements()) {
Header hdr = (Header) eHeaders.nextElement();
if (!MimeStandard.startsWith(hdr.getName(), MimeStandard.HeaderPrefix))
headers.setHeader(hdr.getName(), hdr.getValue());
}
// add back in headers from original message
eHeaders = payload.getAllHeaders();
while (eHeaders.hasMoreElements()) {
Header hdr = (Header) eHeaders.nextElement();
headers.setHeader(hdr.getName(), hdr.getValue());
}
Message msg = new Message(headers, payload.getContentAsBytes());
message.setMessage(msg);
} catch (MessagingException e) {
throw new MimeException(MimeError.InvalidBody, e);
} catch (IOException e) {
throw new MimeException(MimeError.InvalidBody, e);
}
}
use of org.nhindirect.stagent.mail.Message in project nhin-d by DirectProject.
the class NHINDAgentTest method testRejectMessageOnRoutingTamper_policyFalse_assertDecrtyped.
public void testRejectMessageOnRoutingTamper_policyFalse_assertDecrtyped() throws Exception {
/*
* EncryptedMessage2
*/
DefaultNHINDAgent agent = TestUtils.getStockAgent(Arrays.asList(new String[] { "securehealthemail.com" }));
String testMessage = TestUtils.readResource("EncryptedMessage2.txt");
Message originalMsg = new Message(new MimeMessage(null, new ByteArrayInputStream(testMessage.getBytes("ASCII"))));
// add an extra recipient that should not receive this message
final NHINDAddressCollection tamperedRecips = new NHINDAddressCollection();
tamperedRecips.add(new NHINDAddress("ryan@securehealthemail.com"));
tamperedRecips.add(new NHINDAddress("john@securehealthemail.com"));
final IncomingMessage inMessage = new IncomingMessage(originalMsg, tamperedRecips, new NHINDAddress(originalMsg.getFrom()[0].toString()));
IncomingMessage strippedAndVerifiesMessage = agent.processIncoming(inMessage);
assertNotNull(strippedAndVerifiesMessage);
assertTrue(strippedAndVerifiesMessage.getMessage().toString().length() > 0);
}
use of org.nhindirect.stagent.mail.Message in project nhin-d by DirectProject.
the class SMIMECryptographerImpl_checkSignature_Test method setUp.
@Override
public void setUp() throws Exception {
CryptoExtensions.registerJCEProviders();
// load sigCert A
sigCertA = TestUtils.loadCertificate("certCheckA.der");
// load sigCert A private certificate
sigCertAPrivate = TestUtils.loadCertificate("certCheckA.p12");
// load sigCert B
sigCertB = TestUtils.loadCertificate("certCheckB.der");
// load sigCert B
sigCertBPrivate = TestUtils.loadCertificate("certCheckB.p12");
// load sigCert anchor
sigCertAnchor = TestUtils.loadCertificate("Check Signature CA.der");
// load other anchor
otherCert = TestUtils.loadCertificate("gm2552.der");
// load the message that will be encrypted
String testMessage = TestUtils.readResource("MultipartMimeMessage.txt");
cryptographer = new SMIMECryptographerImpl();
signedEntity = cryptographer.sign(new Message(new ByteArrayInputStream(testMessage.getBytes())), sigCertAPrivate);
}
use of org.nhindirect.stagent.mail.Message in project nhin-d by DirectProject.
the class SMIMECryptographerImpl_checkSignature_Test method testMutlipleSigs_sameSignAndValidationCert_assertValidSignature.
public void testMutlipleSigs_sameSignAndValidationCert_assertValidSignature() throws Exception {
// load the message that will be encrypted
String testMessage = TestUtils.readResource("MultipartMimeMessage.txt");
cryptographer = new SMIMECryptographerImpl();
signedEntity = cryptographer.sign(new Message(new ByteArrayInputStream(testMessage.getBytes())), Arrays.asList(sigCertAPrivate, sigCertBPrivate));
cryptographer.checkSignature(signedEntity, sigCertA, Arrays.asList(sigCertAnchor));
cryptographer.checkSignature(signedEntity, sigCertB, Arrays.asList(sigCertAnchor));
}
use of org.nhindirect.stagent.mail.Message in project nhin-d by DirectProject.
the class SMIMECryptographerImpl_createSignatureEntityTest method testCreateSignatureEntity_difSigAndDigestGenerators_assertEntityCreatedAndMatchesControl.
public void testCreateSignatureEntity_difSigAndDigestGenerators_assertEntityCreatedAndMatchesControl() throws Exception {
SplitProviderDirectSignedDataGeneratorFactory factory = new SplitProviderDirectSignedDataGeneratorFactory("SunRsaSign", "BC");
final SMIMECryptographerImpl impl = new SMIMECryptographerImpl();
impl.setSignedDataGeneratorFactory(factory);
final String testMessage = TestUtils.readResource("MultipartMimeMessage.txt");
final MimeEntity ent = new Message(new ByteArrayInputStream(testMessage.getBytes())).extractEntityForSignature(true);
byte[] bytesToSign = EntitySerializer.Default.serializeToBytes(ent);
final X509Certificate sigCertBPrivate = TestUtils.loadCertificate("certCheckB.p12");
final MimeMultipart mm = impl.createSignatureEntity(bytesToSign, Arrays.asList(sigCertBPrivate));
assertNotNull(mm);
assertEquals(2, mm.getCount());
validatedSignatureHeaders(mm);
// now create the control
final SMIMECryptographerImpl controllImpl = new SMIMECryptographerImpl();
final MimeMultipart controllmm = controllImpl.createSignatureEntity(bytesToSign, Arrays.asList(sigCertBPrivate));
assertNotNull(controllmm);
assertEquals(2, controllmm.getCount());
// make sure the signatures match
final MimeEntity signedContent = contentToMimeEntity(mm.getBodyPart(1));
final MimeEntity controlSignedContent = contentToMimeEntity(controllmm.getBodyPart(1));
assertTrue(Arrays.equals(signedContent.getContentAsBytes(), controlSignedContent.getContentAsBytes()));
// verify the signatures
validateSignature(deserializeSignatureEnvelope(mm), sigCertBPrivate);
validateSignature(deserializeSignatureEnvelope(controllmm), sigCertBPrivate);
}
Aggregations