use of org.nhindirect.stagent.mail.MimeEntity in project nhin-d by DirectProject.
the class CryptographerTest method testEncryptWithSingleCert_wrongDecryptCert_assertFailDecrypt.
public void testEncryptWithSingleCert_wrongDecryptCert_assertFailDecrypt() throws Exception {
X509Certificate cert = TestUtils.getExternalCert("user1");
SMIMECryptographerImpl cryptographer = new SMIMECryptographerImpl();
MimeEntity entity = new MimeEntity();
entity.setText("Hello world.");
entity.setHeader(MimeStandard.ContentTypeHeader, "text/plain");
entity.setHeader(MimeStandard.ContentTransferEncodingHeader, "7bit");
MimeEntity encEntity = cryptographer.encrypt(entity, cert);
assertNotNull(encEntity);
X509CertificateEx certex = TestUtils.getInternalCert("altnameonly");
boolean exceptionOccured = false;
try {
cryptographer.decrypt(encEntity, certex);
} catch (NHINDException e) {
if (e.getError().equals(MimeError.Unexpected))
;
exceptionOccured = true;
}
assertTrue(exceptionOccured);
}
use of org.nhindirect.stagent.mail.MimeEntity in project nhin-d by DirectProject.
the class SMIMECryptographerImpl_createSignatureEntityTest method deserializeSignatureEnvelope.
protected CMSSignedData deserializeSignatureEnvelope(MimeMultipart mm) throws Exception {
final MimeEntity contentEntity = contentToMimeEntity(mm.getBodyPart(0));
byte[] messageBytes = EntitySerializer.Default.serializeToBytes(contentEntity);
MimeBodyPart signedContent = null;
signedContent = new MimeBodyPart(new ByteArrayInputStream(messageBytes));
return new CMSSignedData(new CMSProcessableBodyPart(signedContent), mm.getBodyPart(1).getInputStream());
}
use of org.nhindirect.stagent.mail.MimeEntity 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);
}
use of org.nhindirect.stagent.mail.MimeEntity in project nhin-d by DirectProject.
the class SMIMECryptographerImpl_createSignatureEntityTest method testCreateSignatureEntity_defaultSigGenerator_assertEntityCreated.
/*
* This is the control test
*/
public void testCreateSignatureEntity_defaultSigGenerator_assertEntityCreated() throws Exception {
final SMIMECryptographerImpl impl = new SMIMECryptographerImpl();
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);
validateSignature(deserializeSignatureEnvelope(mm), sigCertBPrivate);
}
use of org.nhindirect.stagent.mail.MimeEntity in project nhin-d by DirectProject.
the class DefaultDirectRecipientInformationFactory_createInstanceTest method createSMIMEEnv.
protected SMIMEEnveloped createSMIMEEnv() throws Exception {
// get the cert
final X509Certificate cert = TestUtils.getExternalCert("user1");
// create an encrypted message
final MimeEntity entity = new MimeEntity();
entity.setText("Hello world.");
entity.setHeader(MimeStandard.ContentTypeHeader, "text/plain");
entity.setHeader(MimeStandard.ContentTransferEncodingHeader, "7bit");
final SMIMECryptographerImpl encryptor = new SMIMECryptographerImpl();
return new SMIMEEnveloped(encryptor.encrypt(entity, Arrays.asList(cert)));
}
Aggregations