Search in sources :

Example 6 with SplitProviderDirectSignedDataGeneratorFactory

use of org.nhindirect.stagent.cryptography.activekeyops.SplitProviderDirectSignedDataGeneratorFactory 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);
}
Also used : Message(org.nhindirect.stagent.mail.Message) ByteArrayInputStream(java.io.ByteArrayInputStream) MimeMultipart(javax.mail.internet.MimeMultipart) MimeEntity(org.nhindirect.stagent.mail.MimeEntity) X509Certificate(java.security.cert.X509Certificate) SplitProviderDirectSignedDataGeneratorFactory(org.nhindirect.stagent.cryptography.activekeyops.SplitProviderDirectSignedDataGeneratorFactory)

Aggregations

SplitProviderDirectSignedDataGeneratorFactory (org.nhindirect.stagent.cryptography.activekeyops.SplitProviderDirectSignedDataGeneratorFactory)6 ByteArrayInputStream (java.io.ByteArrayInputStream)2 X509Certificate (java.security.cert.X509Certificate)2 MimeMultipart (javax.mail.internet.MimeMultipart)2 SplitDirectRecipientInformationFactory (org.nhindirect.stagent.cryptography.activekeyops.SplitDirectRecipientInformationFactory)2 SplitProviderDirectSignedDataGenerator (org.nhindirect.stagent.cryptography.activekeyops.SplitProviderDirectSignedDataGenerator)2 Message (org.nhindirect.stagent.mail.Message)2 MimeEntity (org.nhindirect.stagent.mail.MimeEntity)2 KeyStore (java.security.KeyStore)1 X509CertificateEx (org.nhindirect.stagent.cert.X509CertificateEx)1