* @param newAlias
* @throws Exception on any error
* @return the alias of the key imported
*/
public String importPKCS12Key(File keyFile, String password, String alias, String newAlias) throws Exception {
KeyStore kspkcs12 = KeyStore.getInstance("PKCS12");
kspkcs12.load(new FileInputStream(keyFile), password == null ? null : password.toCharArray());
boolean hasTemp = false;
if(isKeyStoreEmpty()) {
if(isKeyStoreExists()) {
deleteKeyStore();
}
createKeyStore();
String dname = "cn=tmp, ou=tmp, o=tmp, l=tmp, st=tmp, c=GB";
createKey("temporary-key", dname);
hasTemp = true;
reloadKeystore();
}
try {
String firstAlias = (String) kspkcs12.aliases().nextElement();
if(Util.isNullOrTrimmedBlank(alias)) {
log.info("Alias not specified, importing first alias " + firstAlias);
alias = firstAlias;
}
if(Util.isNullOrTrimmedBlank(newAlias)) {
log.info("New alias not specified, using imported alias " + alias);
newAlias = alias;
}
Certificate c[] = kspkcs12.getCertificateChain(alias);
// Make sure we don't have a null chain
if (c == null)
c = new Certificate[] {};
Key key = kspkcs12.getKey(alias, password == null ? null : password.toCharArray());
if(key == null) {
throw new Exception("No alias of '" + alias + "' in imported PKCS12 key file.");
}
this.keyStore.setKeyEntry(newAlias, key, getKeyStorePassword().toCharArray(), c);
} finally {