use of org.nhindirect.stagent.mail.Message 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.Message in project nhin-d by DirectProject.
the class NotificationMessage_createNotificationForTest method testCreateNotificationFor_dispatchedMDN_assertSubjectHasCorrectPrefix.
public void testCreateNotificationFor_dispatchedMDN_assertSubjectHasCorrectPrefix() throws Exception {
final String testMessage = TestUtils.readResource("MultipartMimeMessage.txt");
final MimeMessage msg = new MimeMessage(null, new ByteArrayInputStream(testMessage.getBytes("ASCII")));
final Notification noti = new Notification(NotificationType.Dispatched);
final NotificationMessage notiMsg = NotificationMessage.createNotificationFor(new Message(msg), noti);
assertTrue(notiMsg.getHeader(MailStandard.Headers.Subject, ",").startsWith("Dispatched"));
}
use of org.nhindirect.stagent.mail.Message in project nhin-d by DirectProject.
the class NotificationMessage_createNotificationForTest method testCreateNotificationFor_processedMDN_assertSubjectHasCorrectPrefix.
public void testCreateNotificationFor_processedMDN_assertSubjectHasCorrectPrefix() throws Exception {
final String testMessage = TestUtils.readResource("MultipartMimeMessage.txt");
final MimeMessage msg = new MimeMessage(null, new ByteArrayInputStream(testMessage.getBytes("ASCII")));
final Notification noti = new Notification(NotificationType.Processed);
final NotificationMessage notiMsg = NotificationMessage.createNotificationFor(new Message(msg), noti);
assertTrue(notiMsg.getHeader(MailStandard.Headers.Subject, ",").startsWith("Processed"));
}
use of org.nhindirect.stagent.mail.Message in project nhin-d by DirectProject.
the class TrustModel_findTrustedSignatureTest method setUp.
@Override
public void setUp() throws Exception {
CryptoExtensions.registerJCEProviders();
// load sigCert A
sigUser1 = TestUtils.getInternalCert("user1");
// load sigCert A private certificate
sigUser1CA = TestUtils.getInternalCACert("cacert");
// load other anchor
otherCert = TestUtils.loadCertificate("gm2552.der");
// load the message that will be encrypted
String testMessage = TestUtils.readResource("MultipartMimeMessage.txt");
cryptographer = new SMIMECryptographerImpl();
inMessage = new IncomingMessage(new Message(new ByteArrayInputStream(testMessage.getBytes())));
signedEntity = cryptographer.sign(inMessage.getMessage(), sigUser1);
CMSSignedData signatures = cryptographer.deserializeSignatureEnvelope(signedEntity);
inMessage.setSignature(signatures);
}
use of org.nhindirect.stagent.mail.Message in project nhin-d by DirectProject.
the class SigTest method testCreateVerifySig.
public void testCreateVerifySig() throws Exception {
X509CertificateEx internalCert = TestUtils.getInternalCert("user1");
X509Certificate caCert = TestUtils.getExternalCert("cacert");
String testMessage = TestUtils.readResource("MultipartMimeMessage.txt");
MimeMessage entity = EntitySerializer.Default.deserialize(testMessage);
Message message = new Message(entity);
MimeEntity entityToSig = message.extractEntityForSignature(true);
// Serialize message out as ASCII encoded...
byte[] messageBytes = EntitySerializer.Default.serializeToBytes(entityToSig);
MimeBodyPart partToSign = null;
try {
partToSign = new MimeBodyPart(new ByteArrayInputStream(messageBytes));
} catch (Exception e) {
}
SMIMESignedGenerator gen = new SMIMESignedGenerator();
ASN1EncodableVector signedAttrs = new ASN1EncodableVector();
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(PKCSObjectIdentifiers.x509Certificate);
signedAttrs.add(new SMIMECapabilitiesAttribute(caps));
List certList = new ArrayList();
gen.addSigner(internalCert.getPrivateKey(), internalCert, SMIMESignedGenerator.DIGEST_SHA1, new AttributeTable(signedAttrs), null);
//SMIMESignedGenerator.DIGEST_SHA1, null, null);
certList.add(internalCert);
MimeMultipart retVal = null;
CertStore certsAndcrls = CertStore.getInstance("Collection", new CollectionCertStoreParameters(certList), CryptoExtensions.getJCEProviderName());
gen.addCertificatesAndCRLs(certsAndcrls);
_certStores.add(certsAndcrls);
_signers.add(new Signer(internalCert.getPrivateKey(), internalCert, SMIMESignedGenerator.DIGEST_SHA1, new AttributeTable(signedAttrs), null));
retVal = generate(partToSign, CryptoExtensions.getJCEProviderName());
for (int i = 0; i < 10; ++i) {
ByteArrayOutputStream oStream = new ByteArrayOutputStream();
retVal.writeTo(oStream);
oStream.flush();
byte[] serialzedBytes = oStream.toByteArray();
//System.out.println(new String(serialzedBytes, "ASCII") + "\r\n\r\n\r\n\r\n\r\n");
ByteArrayDataSource dataSource = new ByteArrayDataSource(serialzedBytes, retVal.getContentType());
MimeMultipart verifyMM = new MimeMultipart(dataSource);
CMSSignedData signed = null;
//CMSSignedData signeddata = new CMSSignedData(new CMSProcessableBodyPartInbound(verifyMM.getBodyPart(0)), verifyMM.getBodyPart(1).getInputStream());
CMSSignedData signeddata = new CMSSignedData(new CMSProcessableBodyPartInbound(partToSign), verifyMM.getBodyPart(1).getInputStream());
int verified = 0;
CertStore certs = signeddata.getCertificatesAndCRLs("Collection", CryptoExtensions.getJCEProviderName());
SignerInformationStore signers = signeddata.getSignerInfos();
Collection c = signers.getSigners();
Iterator it = c.iterator();
while (it.hasNext()) {
SignerInformation signer = (SignerInformation) it.next();
Collection certCollection = certs.getCertificates(signer.getSID());
Attribute dig = signer.getSignedAttributes().get(CMSAttributes.messageDigest);
DERObject hashObj = dig.getAttrValues().getObjectAt(0).getDERObject();
byte[] signedHash = ((ASN1OctetString) hashObj).getOctets();
System.out.print("value of signedHash: \r\n\tvalue: ");
for (byte bt : signedHash) {
System.out.print(bt + " ");
}
System.out.println();
Iterator certIt = certCollection.iterator();
try {
assertTrue(signer.verify(internalCert, CryptoExtensions.getJCEProviderName()));
} catch (Exception e) {
e.printStackTrace();
}
byte[] bytes = signer.getContentDigest();
/*
X509Certificate cert = (X509Certificate)certIt.next();
if (signer.verify(cert.getPublicKey()))
{
verified++;
}
*/
verified++;
}
}
}
Aggregations