Search in sources :

Example 1 with PKCS11Credential

use of org.nhindirect.common.crypto.PKCS11Credential in project nhin-d by DirectProject.

the class ConfigServiceRESTCertificateStore_getCertificateWithHSMKeyTest method getCertService.

protected ConfigServiceRESTCertificateStore getCertService() throws Exception {
    if (StringUtils.isEmpty(TestUtils.setupSafeNetToken()))
        return null;
    final ConfigServiceRESTCertificateStore certService = new ConfigServiceRESTCertificateStore(proxy);
    final PKCS11Credential cred = new BootstrappedPKCS11Credential("1Kingpuff");
    final StaticPKCS11TokenKeyStoreProtectionManager mgr = new StaticPKCS11TokenKeyStoreProtectionManager(cred, "KeyStoreProtKey", "PrivKeyProtKey");
    certService.setKeyStoreProectionManager(mgr);
    return certService;
}
Also used : PKCS11Credential(org.nhindirect.common.crypto.PKCS11Credential) BootstrappedPKCS11Credential(org.nhindirect.common.crypto.impl.BootstrappedPKCS11Credential) StaticPKCS11TokenKeyStoreProtectionManager(org.nhindirect.common.crypto.impl.StaticPKCS11TokenKeyStoreProtectionManager) BootstrappedPKCS11Credential(org.nhindirect.common.crypto.impl.BootstrappedPKCS11Credential)

Example 2 with PKCS11Credential

use of org.nhindirect.common.crypto.PKCS11Credential in project nhin-d by DirectProject.

the class StaticPKCS11TokenKeyStoreProtectionManagerTest method testSetKeysAsByteArrayAndGetFromToken.

@Test
public void testSetKeysAsByteArrayAndGetFromToken() throws Exception {
    final String pkcs11ProvName = TestUtils.setupSafeNetToken();
    if (!StringUtils.isEmpty(pkcs11ProvName)) {
        PKCS11Credential cred = new BootstrappedPKCS11Credential("1Kingpuff");
        final StaticPKCS11TokenKeyStoreProtectionManager mgr = new StaticPKCS11TokenKeyStoreProtectionManager(cred, "KeyStoreProtKey", "PrivKeyProtKey");
        // create the keys on the token
        KeyGenerator keyGen = KeyGenerator.getInstance("AES");
        // cryptograph. secure random 
        SecureRandom random = new SecureRandom();
        keyGen.init(random);
        final SecretKey keyStoreSecretKey = keyGen.generateKey();
        mgr.clearKeyStoreProtectionKey();
        mgr.setKeyStoreProtectionKeyAsBytes(keyStoreSecretKey.getEncoded());
        keyGen = KeyGenerator.getInstance("AES");
        // cryptograph. secure random 
        random = new SecureRandom();
        keyGen.init(random);
        final SecretKey privKeySecretKey = keyGen.generateKey();
        mgr.clearPrivateKeyProtectionKey();
        mgr.setPrivateKeyProtectionKeyAsBytes(privKeySecretKey.getEncoded());
        assertTrue(Arrays.equals(keyStoreSecretKey.getEncoded(), mgr.getKeyStoreProtectionKey().getEncoded()));
        assertTrue(Arrays.equals(privKeySecretKey.getEncoded(), mgr.getPrivateKeyProtectionKey().getEncoded()));
    }
}
Also used : PKCS11Credential(org.nhindirect.common.crypto.PKCS11Credential) SecretKey(javax.crypto.SecretKey) SecureRandom(java.security.SecureRandom) KeyGenerator(javax.crypto.KeyGenerator) Test(org.junit.Test)

Example 3 with PKCS11Credential

use of org.nhindirect.common.crypto.PKCS11Credential in project nhin-d by DirectProject.

the class StaticPKCS11TokenKeyStoreProtectionManagerTest method testSetKeysAsKeyAndGetFromToken.

@Test
public void testSetKeysAsKeyAndGetFromToken() throws Exception {
    final String pkcs11ProvName = TestUtils.setupSafeNetToken();
    if (!StringUtils.isEmpty(pkcs11ProvName)) {
        final PKCS11Credential cred = new BootstrappedPKCS11Credential("1Kingpuff");
        final StaticPKCS11TokenKeyStoreProtectionManager mgr = new StaticPKCS11TokenKeyStoreProtectionManager(cred, "KeyStoreProtKey", "PrivKeyProtKey");
        // create the keys on the token
        KeyGenerator keyGen = KeyGenerator.getInstance("AES");
        // cryptograph. secure random 
        SecureRandom random = new SecureRandom();
        keyGen.init(random);
        final SecretKey keyStoreSecretKey = keyGen.generateKey();
        mgr.clearKeyStoreProtectionKey();
        mgr.setKeyStoreProtectionKey(keyStoreSecretKey);
        keyGen = KeyGenerator.getInstance("AES");
        // cryptograph. secure random 
        random = new SecureRandom();
        keyGen.init(random);
        final SecretKey privKeySecretKey = keyGen.generateKey();
        mgr.clearPrivateKeyProtectionKey();
        mgr.setPrivateKeyProtectionKey(privKeySecretKey);
        assertTrue(Arrays.equals(keyStoreSecretKey.getEncoded(), mgr.getKeyStoreProtectionKey().getEncoded()));
        assertTrue(Arrays.equals(privKeySecretKey.getEncoded(), mgr.getPrivateKeyProtectionKey().getEncoded()));
    }
}
Also used : PKCS11Credential(org.nhindirect.common.crypto.PKCS11Credential) SecretKey(javax.crypto.SecretKey) SecureRandom(java.security.SecureRandom) KeyGenerator(javax.crypto.KeyGenerator) Test(org.junit.Test)

Example 4 with PKCS11Credential

use of org.nhindirect.common.crypto.PKCS11Credential in project nhin-d by DirectProject.

the class StaticPKCS11TokenKeyStoreProtectionManagerTest method testGetAllKeys_secureRandomGenKeys.

@Test
public void testGetAllKeys_secureRandomGenKeys() throws Exception {
    final String pkcs11ProvName = TestUtils.setupSafeNetToken();
    if (!StringUtils.isEmpty(pkcs11ProvName)) {
        PKCS11Credential cred = new BootstrappedPKCS11Credential("1Kingpuff");
        final StaticPKCS11TokenKeyStoreProtectionManager mgr = new StaticPKCS11TokenKeyStoreProtectionManager(cred, "KeyStoreProtKey", "PrivKeyProtKey");
        // create the keys on the token
        KeyGenerator keyGen = KeyGenerator.getInstance("AES");
        // cryptograph. secure random 
        SecureRandom random = new SecureRandom();
        keyGen.init(random);
        final SecretKey keyStoreSecretKey = keyGen.generateKey();
        mgr.clearKeyStoreProtectionKey();
        mgr.setKeyStoreProtectionKey(keyStoreSecretKey);
        keyGen = KeyGenerator.getInstance("AES");
        // cryptograph. secure random 
        random = new SecureRandom();
        keyGen.init(random);
        final SecretKey privKeySecretKey = keyGen.generateKey();
        mgr.clearPrivateKeyProtectionKey();
        mgr.setPrivateKeyProtectionKey(privKeySecretKey);
        final Map<String, Key> keys = mgr.getAllKeys();
        assertEquals(2, keys.size());
        Iterator<Entry<String, Key>> entryIter = keys.entrySet().iterator();
        Key key = entryIter.next().getValue();
        assertTrue(Arrays.equals(privKeySecretKey.getEncoded(), key.getEncoded()));
        key = entryIter.next().getValue();
        assertTrue(Arrays.equals(keyStoreSecretKey.getEncoded(), key.getEncoded()));
    }
}
Also used : PKCS11Credential(org.nhindirect.common.crypto.PKCS11Credential) SecretKey(javax.crypto.SecretKey) Entry(java.util.Map.Entry) SecureRandom(java.security.SecureRandom) KeyGenerator(javax.crypto.KeyGenerator) Key(java.security.Key) SecretKey(javax.crypto.SecretKey) Test(org.junit.Test)

Example 5 with PKCS11Credential

use of org.nhindirect.common.crypto.PKCS11Credential in project nhin-d by DirectProject.

the class BaseKeyStoreManagerCertStoreTest method setUp.

@Override
public void setUp() throws Exception {
    CertCacheFactory.getInstance().flushAll();
    if (!StringUtils.isEmpty(TestUtils.setupSafeNetToken())) {
        // clean out the token of all private keys
        final PKCS11Credential cred = new BootstrappedPKCS11Credential("1Kingpuff");
        final MutableKeyStoreProtectionManager mgr = new StaticPKCS11TokenKeyStoreProtectionManager(cred, "", "");
        store = new CacheableKeyStoreManagerCertificateStore(mgr);
        store.remove(store.getAllCertificates());
        assertTrue(store.getAllCertificates().isEmpty());
    }
}
Also used : PKCS11Credential(org.nhindirect.common.crypto.PKCS11Credential) BootstrappedPKCS11Credential(org.nhindirect.common.crypto.impl.BootstrappedPKCS11Credential) MutableKeyStoreProtectionManager(org.nhindirect.common.crypto.MutableKeyStoreProtectionManager) StaticPKCS11TokenKeyStoreProtectionManager(org.nhindirect.common.crypto.impl.StaticPKCS11TokenKeyStoreProtectionManager) BootstrappedPKCS11Credential(org.nhindirect.common.crypto.impl.BootstrappedPKCS11Credential)

Aggregations

PKCS11Credential (org.nhindirect.common.crypto.PKCS11Credential)12 Test (org.junit.Test)7 KeyGenerator (javax.crypto.KeyGenerator)6 SecureRandom (java.security.SecureRandom)5 SecretKey (javax.crypto.SecretKey)5 BootstrappedPKCS11Credential (org.nhindirect.common.crypto.impl.BootstrappedPKCS11Credential)5 StaticPKCS11TokenKeyStoreProtectionManager (org.nhindirect.common.crypto.impl.StaticPKCS11TokenKeyStoreProtectionManager)5 Key (java.security.Key)4 KeyStore (java.security.KeyStore)3 PrivateKey (java.security.PrivateKey)3 MutableKeyStoreProtectionManager (org.nhindirect.common.crypto.MutableKeyStoreProtectionManager)3 Certificate (java.security.cert.Certificate)2 X509Certificate (java.security.cert.X509Certificate)2 Entry (java.util.Map.Entry)2 X509CertificateEx (org.nhindirect.stagent.cert.X509CertificateEx)2 CacheableKeyStoreManagerCertificateStore (org.nhindirect.stagent.cert.impl.CacheableKeyStoreManagerCertificateStore)2 File (java.io.File)1 RecipientInformation (org.bouncycastle.cms.RecipientInformation)1 SMIMEEnveloped (org.bouncycastle.mail.smime.SMIMEEnveloped)1 CryptoException (org.nhindirect.common.crypto.exceptions.CryptoException)1