Search in sources :

Example 1 with SplitProviderDirectSignedDataGeneratorFactory

use of org.nhindirect.stagent.cryptography.activekeyops.SplitProviderDirectSignedDataGeneratorFactory in project nhin-d by DirectProject.

the class SMIMECryptographerImpl_constructTest method testContructSMIMECryptographerImpl_setDecFactory.

public void testContructSMIMECryptographerImpl_setDecFactory() {
    SMIMECryptographerImpl impl = new SMIMECryptographerImpl(EncryptionAlgorithm.RSA_3DES, DigestAlgorithm.SHA384, new SplitProviderDirectSignedDataGeneratorFactory(), new SplitDirectRecipientInformationFactory());
    assertEquals(DigestAlgorithm.SHA384, impl.getDigestAlgorithm());
    assertEquals(EncryptionAlgorithm.RSA_3DES, impl.getEncryptionAlgorithm());
    assertTrue(impl.getSignedDataGeneratorFactory() instanceof SplitProviderDirectSignedDataGeneratorFactory);
    assertTrue(impl.getRecipientInformationFactory() instanceof SplitDirectRecipientInformationFactory);
}
Also used : SplitProviderDirectSignedDataGeneratorFactory(org.nhindirect.stagent.cryptography.activekeyops.SplitProviderDirectSignedDataGeneratorFactory) SplitDirectRecipientInformationFactory(org.nhindirect.stagent.cryptography.activekeyops.SplitDirectRecipientInformationFactory)

Example 2 with SplitProviderDirectSignedDataGeneratorFactory

use of org.nhindirect.stagent.cryptography.activekeyops.SplitProviderDirectSignedDataGeneratorFactory in project nhin-d by DirectProject.

the class SMIMECryptographerImpl_createSignatureEntityTest method testCreateSignatureEntity_hsmSignatureGenerator_assertEntityCreatedAndMatchesControl.

public void testCreateSignatureEntity_hsmSignatureGenerator_assertEntityCreatedAndMatchesControl() throws Exception {
    final String installedAlias = "JunitTestKey";
    /**
         * This test is only run if a specific SafeNet eToken Pro HSM is connected to the testing 
         * system.  This can be modified for another specific machine and/or token.
         */
    pkcs11ProvName = TestUtils.setupSafeNetToken();
    if (!StringUtils.isEmpty(pkcs11ProvName)) {
        // get a certificate from the key store
        final KeyStore ks = KeyStore.getInstance("PKCS11");
        ks.load(null, "1Kingpuff".toCharArray());
        // delete the entry in case it exists
        try {
            ks.deleteEntry(installedAlias);
        } catch (Exception e) {
        /*no-op */
        }
        // add the signing cert and private key into the token
        final X509Certificate sigCertBPrivate = (X509CertificateEx) TestUtils.loadCertificate("certCheckB.p12");
        try {
            ks.setKeyEntry(installedAlias, ((X509CertificateEx) sigCertBPrivate).getPrivateKey(), null, new Certificate[] { sigCertBPrivate });
            final KeyStore.PrivateKeyEntry entry = (KeyStore.PrivateKeyEntry) ks.getEntry(installedAlias, null);
            final X509Certificate signerCert = X509CertificateEx.fromX509Certificate((X509Certificate) entry.getCertificate(), entry.getPrivateKey());
            SplitProviderDirectSignedDataGeneratorFactory factory = new SplitProviderDirectSignedDataGeneratorFactory(pkcs11ProvName, "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 MimeMultipart mm = impl.createSignatureEntity(bytesToSign, Arrays.asList(signerCert));
            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 can be verified
            // the actual byte data may not be the same due to 
            // randomness in the signature
            validateSignature(deserializeSignatureEnvelope(mm), sigCertBPrivate);
            validateSignature(deserializeSignatureEnvelope(controllmm), sigCertBPrivate);
        } finally {
            ks.deleteEntry(installedAlias);
        }
    }
}
Also used : Message(org.nhindirect.stagent.mail.Message) KeyStore(java.security.KeyStore) X509Certificate(java.security.cert.X509Certificate) X509CertificateEx(org.nhindirect.stagent.cert.X509CertificateEx) ByteArrayInputStream(java.io.ByteArrayInputStream) MimeMultipart(javax.mail.internet.MimeMultipart) MimeEntity(org.nhindirect.stagent.mail.MimeEntity) SplitProviderDirectSignedDataGeneratorFactory(org.nhindirect.stagent.cryptography.activekeyops.SplitProviderDirectSignedDataGeneratorFactory)

Example 3 with SplitProviderDirectSignedDataGeneratorFactory

use of org.nhindirect.stagent.cryptography.activekeyops.SplitProviderDirectSignedDataGeneratorFactory in project nhin-d by DirectProject.

the class SplitProviderDirectSignedDataGeneratorFactory_createGenTest method testCreateGenerator_emptyProvider_assertDefaultProvider.

public void testCreateGenerator_emptyProvider_assertDefaultProvider() throws Exception {
    final SplitProviderDirectSignedDataGeneratorFactory factory = new SplitProviderDirectSignedDataGeneratorFactory();
    final SplitProviderDirectSignedDataGenerator gen = (SplitProviderDirectSignedDataGenerator) factory.createInstance();
    assertEquals(CryptoExtensions.getJCESensitiveProviderName(), gen.sigProvider);
    assertEquals(CryptoExtensions.getJCEProviderName(), gen.digestProvider);
}
Also used : SplitProviderDirectSignedDataGenerator(org.nhindirect.stagent.cryptography.activekeyops.SplitProviderDirectSignedDataGenerator) SplitProviderDirectSignedDataGeneratorFactory(org.nhindirect.stagent.cryptography.activekeyops.SplitProviderDirectSignedDataGeneratorFactory)

Example 4 with SplitProviderDirectSignedDataGeneratorFactory

use of org.nhindirect.stagent.cryptography.activekeyops.SplitProviderDirectSignedDataGeneratorFactory in project nhin-d by DirectProject.

the class SplitProviderDirectSignedDataGeneratorFactory_createGenTest method testCreateGenerator_customeProvider_assertProviders.

public void testCreateGenerator_customeProvider_assertProviders() throws Exception {
    final SplitProviderDirectSignedDataGeneratorFactory factory = new SplitProviderDirectSignedDataGeneratorFactory("TestProvider1", "TestProvider2");
    final SplitProviderDirectSignedDataGenerator gen = (SplitProviderDirectSignedDataGenerator) factory.createInstance();
    assertEquals("TestProvider1", gen.sigProvider);
    assertEquals("TestProvider2", gen.digestProvider);
}
Also used : SplitProviderDirectSignedDataGenerator(org.nhindirect.stagent.cryptography.activekeyops.SplitProviderDirectSignedDataGenerator) SplitProviderDirectSignedDataGeneratorFactory(org.nhindirect.stagent.cryptography.activekeyops.SplitProviderDirectSignedDataGeneratorFactory)

Example 5 with SplitProviderDirectSignedDataGeneratorFactory

use of org.nhindirect.stagent.cryptography.activekeyops.SplitProviderDirectSignedDataGeneratorFactory in project nhin-d by DirectProject.

the class SMIMECryptographerImpl_constructTest method testContructSMIMECryptographerImpl_setSigningFactory.

public void testContructSMIMECryptographerImpl_setSigningFactory() {
    SMIMECryptographerImpl impl = new SMIMECryptographerImpl(EncryptionAlgorithm.RSA_3DES, DigestAlgorithm.SHA384, new SplitProviderDirectSignedDataGeneratorFactory(), null);
    assertEquals(DigestAlgorithm.SHA384, impl.getDigestAlgorithm());
    assertEquals(EncryptionAlgorithm.RSA_3DES, impl.getEncryptionAlgorithm());
    assertTrue(impl.getSignedDataGeneratorFactory() instanceof SplitProviderDirectSignedDataGeneratorFactory);
    assertTrue(impl.getRecipientInformationFactory() instanceof SplitDirectRecipientInformationFactory);
}
Also used : SplitProviderDirectSignedDataGeneratorFactory(org.nhindirect.stagent.cryptography.activekeyops.SplitProviderDirectSignedDataGeneratorFactory) SplitDirectRecipientInformationFactory(org.nhindirect.stagent.cryptography.activekeyops.SplitDirectRecipientInformationFactory)

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