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);
}
}
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);
}
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;
}
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);
}
}
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);
}
Aggregations