Search in sources :

Example 6 with CryptoException

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

the class StaticPKCS11TokenKeyStoreProtectionManager method initTokenStore.

/**
	 * {@inheritDoc}
	 */
public void initTokenStore() throws CryptoException {
    loadProvider();
    try {
        LOGGER.debug("Initializing token store type " + keyStoreType);
        ks = KeyStore.getInstance(keyStoreType);
        ks.load(keyStoreSource, credential.getPIN());
    } 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)

Example 7 with CryptoException

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

the class PKCS11SecretKeyManager method main.

public static void main(String[] argv) {
    String[] passArgs = null;
    // provider... if not, assume the JVM has already been configured for one
    if (argv.length > 0) {
        // Check parameters
        for (int i = 0; i < argv.length; i++) {
            String arg = argv[i];
            // Options
            if (!arg.startsWith("-")) {
                System.err.println("Error: Unexpected argument [" + arg + "]\n");
                printUsage();
                System.exit(-1);
            } else if (arg.equalsIgnoreCase("-pkcscfg")) {
                if (i == argv.length - 1 || argv[i + 1].startsWith("-")) {
                    System.err.println("Error: Missing pkcs config file");
                    System.exit(-1);
                }
                pkcs11ProviderCfg = argv[++i];
            } else if (arg.equals("-keyStoreCfg")) {
                if (i == argv.length - 1 || argv[i + 1].startsWith("-")) {
                    System.err.println("Error: Missing keystore config file");
                    System.exit(-1);
                }
                keyStoreConfigFile = argv[++i];
            } else if (arg.equals("-help")) {
                printUsage();
                System.exit(-1);
            } else {
                System.err.println("Error: Unknown argument " + arg + "\n");
                printUsage();
                System.exit(-1);
            }
        }
    }
    if (keyStoreConfigFile != null) {
        try {
            // get additional properties
            final InputStream inStream = FileUtils.openInputStream(new File(keyStoreConfigFile));
            final Properties props = new Properties();
            props.load(inStream);
            keyStoreType = props.getProperty("keyStoreType");
            providerName = props.getProperty("keyStoreProviderName");
            keyStoreSource = props.getProperty("keyStoreSource");
        } catch (IOException e) {
            System.err.println("Error reading keystore config file to properties: " + e.getMessage());
            System.exit(-1);
        }
    }
    MutableKeyStoreProtectionManager mgr = null;
    // need to login
    try {
        mgr = tokenLogin();
    } catch (CryptoException e) {
        System.out.println("Failed to login to hardware token: " + e.getMessage());
        System.exit(-1);
    }
    final PKCS11SecretKeyManager mgmt = new PKCS11SecretKeyManager(mgr);
    boolean runCommand = false;
    if (mgmt != null) {
        runCommand = mgmt.run(passArgs);
    }
    if (exitOnEndCommands)
        System.exit(runCommand ? 0 : -1);
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) MutableKeyStoreProtectionManager(org.nhindirect.common.crypto.MutableKeyStoreProtectionManager) IOException(java.io.IOException) Properties(java.util.Properties) CryptoException(org.nhindirect.common.crypto.exceptions.CryptoException) File(java.io.File)

Example 8 with CryptoException

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

the class WrappedOnDemandX509CertificateEx method getPrivateKey.

/**
     * {@inheritDoc}}
     */
public synchronized PrivateKey getPrivateKey() {
    if (wrappedKey != null)
        return wrappedKey;
    final WrappableKeyProtectionManager wrapManager = (WrappableKeyProtectionManager) mgr;
    // get the key algorithm from the public key... this will be needed
    // as a parameter to the unwrap method
    final String keyAlg = this.internalCert.getPublicKey().getAlgorithm();
    try {
        wrappedKey = (PrivateKey) wrapManager.unwrapWithSecretKey((SecretKey) mgr.getPrivateKeyProtectionKey(), wrappedData, keyAlg, Cipher.PRIVATE_KEY);
    } catch (CryptoException e) {
        throw new NHINDException(AgentError.Unexpected, "Failed to access wrapped private key.", e);
    }
    return wrappedKey;
}
Also used : WrappableKeyProtectionManager(org.nhindirect.common.crypto.WrappableKeyProtectionManager) CryptoException(org.nhindirect.common.crypto.exceptions.CryptoException) NHINDException(org.nhindirect.stagent.NHINDException)

Example 9 with CryptoException

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

the class PKCS11SecretKeyManager method tokenLogin.

public static MutableKeyStoreProtectionManager tokenLogin() throws CryptoException {
    try {
        //System.console();
        final Console cons = null;
        char[] passwd = null;
        if (cons != null) {
            passwd = cons.readPassword("[%s]", "Enter hardware token password: ");
            java.util.Arrays.fill(passwd, ' ');
        } else {
            System.out.print("Enter hardware token password: ");
            final BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
            passwd = reader.readLine().toCharArray();
        }
        final BootstrappedPKCS11Credential cred = new BootstrappedPKCS11Credential(new String(passwd));
        final StaticPKCS11TokenKeyStoreProtectionManager loginMgr = new StaticPKCS11TokenKeyStoreProtectionManager();
        loginMgr.setCredential(cred);
        loginMgr.setKeyStoreProviderName(providerName);
        if (!StringUtils.isEmpty(keyStoreType))
            loginMgr.setKeyStoreType(keyStoreType);
        if (!StringUtils.isEmpty(keyStoreSource)) {
            InputStream str = new ByteArrayInputStream(keyStoreSource.getBytes());
            loginMgr.setKeyStoreSource(str);
        }
        if (!StringUtils.isEmpty(pkcs11ProviderCfg))
            loginMgr.setPcks11ConfigFile(pkcs11ProviderCfg);
        loginMgr.initTokenStore();
        return loginMgr;
    } catch (Exception e) {
        throw new RuntimeException("Error getting password.", e);
    }
}
Also used : InputStreamReader(java.io.InputStreamReader) ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) StaticPKCS11TokenKeyStoreProtectionManager(org.nhindirect.common.crypto.impl.StaticPKCS11TokenKeyStoreProtectionManager) Console(java.io.Console) BufferedReader(java.io.BufferedReader) BootstrappedPKCS11Credential(org.nhindirect.common.crypto.impl.BootstrappedPKCS11Credential) IOException(java.io.IOException) CryptoException(org.nhindirect.common.crypto.exceptions.CryptoException)

Example 10 with CryptoException

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

the class PKCS11SecretKeyManagerUI method main.

public static void main(String[] argv) {
    // provider... if not, assume the JVM has already been configured for one
    if (argv.length > 0) {
        // Check parameters
        for (int i = 0; i < argv.length; i++) {
            String arg = argv[i];
            // Options
            if (!arg.startsWith("-")) {
                System.err.println("Error: Unexpected argument [" + arg + "]\n");
                printUsage();
                System.exit(-1);
            } else if (arg.equalsIgnoreCase("-pkcscfg")) {
                if (i == argv.length - 1 || argv[i + 1].startsWith("-")) {
                    System.err.println("Error: Missing pkcs config file");
                    System.exit(-1);
                }
                pkcs11ProviderCfg = argv[++i];
            } else if (arg.equals("-keyStoreCfg")) {
                if (i == argv.length - 1 || argv[i + 1].startsWith("-")) {
                    System.err.println("Error: Missing keystore config file");
                    System.exit(-1);
                }
                keyStoreConfigFile = argv[++i];
            } else if (arg.equals("-help")) {
                printUsage();
                System.exit(-1);
            } else {
                System.err.println("Error: Unknown argument " + arg + "\n");
                printUsage();
                System.exit(-1);
            }
        }
    }
    if (keyStoreConfigFile != null) {
        try {
            // get additional properties
            final InputStream inStream = FileUtils.openInputStream(new File(keyStoreConfigFile));
            final Properties props = new Properties();
            props.load(inStream);
            keyStoreType = props.getProperty("keyStoreType");
            providerName = props.getProperty("keyStoreProviderName");
            keyStoreSource = props.getProperty("keyStoreSource");
        } catch (IOException e) {
            System.err.println("Error reading keystore config file to properties: " + e.getMessage());
            System.exit(-1);
        }
    }
    // need to login
    try {
        mgr = tokenLogin();
    } catch (CryptoException e) {
        JOptionPane.showMessageDialog(null, "Failed to login to hardware token: " + e.getMessage(), "Token Login Failure", JOptionPane.ERROR_MESSAGE);
        System.exit(-1);
    }
    final PKCS11SecretKeyManagerUI hi = new PKCS11SecretKeyManagerUI();
    hi.setVisible(true);
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) IOException(java.io.IOException) Properties(java.util.Properties) CryptoException(org.nhindirect.common.crypto.exceptions.CryptoException) File(java.io.File) Point(java.awt.Point)

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