Search in sources :

Example 11 with CryptoException

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

the class AbstractPKCS11TokenKeyStoreProtectionManager method getAllEntries.

/**
	 * {@inheritDoc}
	 */
@Override
public Map<String, Entry> getAllEntries() throws CryptoException {
    final Map<String, Entry> entries = new HashMap<String, Entry>();
    try {
        final Enumeration<String> aliases = ks.aliases();
        while (aliases.hasMoreElements()) {
            final String alias = aliases.nextElement();
            if (ks.isKeyEntry(alias)) {
                try {
                    final Entry entry = ks.getEntry(alias, null);
                    entries.put(alias, entry);
                } catch (Exception e) {
                // no-op, this might be a key that we don't care about
                }
            }
        }
    } catch (Exception e) {
        throw new CryptoException("Error extracting entries from PKCS11 token", e);
    }
    return entries;
}
Also used : Entry(java.security.KeyStore.Entry) HashMap(java.util.HashMap) CryptoException(org.nhindirect.common.crypto.exceptions.CryptoException) CryptoException(org.nhindirect.common.crypto.exceptions.CryptoException)

Example 12 with CryptoException

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

the class AbstractPKCS11TokenKeyStoreProtectionManager method unwrapWithSecretKey.

/**
	 * {@inheritDoc}}
	 */
@Override
public Key unwrapWithSecretKey(SecretKey kek, byte[] wrappedData, String keyAlg, int keyType) throws CryptoException {
    final IvParameterSpec iv = new IvParameterSpec(IV_BYTES);
    try {
        final Cipher unwrapCipher = Cipher.getInstance(WRAP_ALGO, ks.getProvider().getName());
        unwrapCipher.init(Cipher.UNWRAP_MODE, kek, iv);
        return unwrapCipher.unwrap(wrappedData, keyAlg, keyType);
    } catch (Exception e) {
        throw new CryptoException("Failed to unwrap key: " + e.getMessage(), e);
    }
}
Also used : IvParameterSpec(javax.crypto.spec.IvParameterSpec) Cipher(javax.crypto.Cipher) CryptoException(org.nhindirect.common.crypto.exceptions.CryptoException) CryptoException(org.nhindirect.common.crypto.exceptions.CryptoException)

Example 13 with CryptoException

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

the class BootstrappedKeyStoreProtectionManager method unwrapWithSecretKey.

/**
	 * {@inheritDoc}}
	 */
@Override
public Key unwrapWithSecretKey(SecretKey kek, byte[] wrappedData, String keyAlg, int keyType) throws CryptoException {
    final IvParameterSpec iv = new IvParameterSpec(AbstractPKCS11TokenKeyStoreProtectionManager.IV_BYTES);
    try {
        final Cipher unwrapCipher = Cipher.getInstance(AbstractPKCS11TokenKeyStoreProtectionManager.WRAP_ALGO);
        unwrapCipher.init(Cipher.UNWRAP_MODE, kek, iv);
        return unwrapCipher.unwrap(wrappedData, keyAlg, keyType);
    } catch (Exception e) {
        throw new CryptoException("Failed to unwrap key: " + e.getMessage(), e);
    }
}
Also used : IvParameterSpec(javax.crypto.spec.IvParameterSpec) Cipher(javax.crypto.Cipher) CryptoException(org.nhindirect.common.crypto.exceptions.CryptoException) CryptoException(org.nhindirect.common.crypto.exceptions.CryptoException)

Example 14 with CryptoException

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

the class BootstrappedKeyStoreProtectionManager method wrapWithSecretKey.

/**
	 * {@inheritDoc}}
	 */
@Override
public byte[] wrapWithSecretKey(SecretKey kek, Key keyToWrap) throws CryptoException {
    final IvParameterSpec iv = new IvParameterSpec(AbstractPKCS11TokenKeyStoreProtectionManager.IV_BYTES);
    try {
        final Cipher wrapCipher = Cipher.getInstance(AbstractPKCS11TokenKeyStoreProtectionManager.WRAP_ALGO);
        wrapCipher.init(Cipher.WRAP_MODE, kek, iv);
        return wrapCipher.wrap(keyToWrap);
    } catch (Exception e) {
        throw new CryptoException("Failed to wrap key: " + e.getMessage(), e);
    }
}
Also used : IvParameterSpec(javax.crypto.spec.IvParameterSpec) Cipher(javax.crypto.Cipher) CryptoException(org.nhindirect.common.crypto.exceptions.CryptoException) CryptoException(org.nhindirect.common.crypto.exceptions.CryptoException)

Example 15 with CryptoException

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

the class DynamicPKCS11TokenKeyStoreProtectionManager method initTokenStore.

/**
	 * {@inheritDocs}
	 */
public void initTokenStore() throws CryptoException {
    try {
        loadProvider();
        ks = keyStoreBuilder.getKeyStore();
        ks.load(keyStoreSource, null);
    } catch (Exception e) {
        throw new CryptoException("Error initializing PKCS11 token", e);
    }
}
Also used : CryptoException(org.nhindirect.common.crypto.exceptions.CryptoException) CryptoException(org.nhindirect.common.crypto.exceptions.CryptoException)

Aggregations

CryptoException (org.nhindirect.common.crypto.exceptions.CryptoException)20 SecretKey (javax.crypto.SecretKey)6 Key (java.security.Key)5 SecretKeySpec (javax.crypto.spec.SecretKeySpec)5 ByteArrayInputStream (java.io.ByteArrayInputStream)4 File (java.io.File)4 IOException (java.io.IOException)4 InputStream (java.io.InputStream)4 Cipher (javax.crypto.Cipher)4 IvParameterSpec (javax.crypto.spec.IvParameterSpec)4 Properties (java.util.Properties)3 KeyStore (java.security.KeyStore)2 PrivateKey (java.security.PrivateKey)2 MutableKeyStoreProtectionManager (org.nhindirect.common.crypto.MutableKeyStoreProtectionManager)2 Point (java.awt.Point)1 BufferedReader (java.io.BufferedReader)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 Console (java.io.Console)1 InputStreamReader (java.io.InputStreamReader)1 KeyFactory (java.security.KeyFactory)1