privateKeyPassowrd = serverConfig
.getFirstProperty(RegistryResources.SecurityManagement.SERVER_PRIVATE_KEY_PASSWORD);
} else {
String path = SecurityConstants.KEY_STORES + "/" + keyStoreName;
if (!registry.resourceExists(path)) {
throw new SecurityConfigException("Key Store not found");
}
Resource resource = registry.get(path);
KeyStoreManager manager = KeyStoreManager.getInstance((UserRegistry)registry);
keyStore = manager.getKeyStore(keyStoreName);
keyStoreType = resource.getProperty(SecurityConstants.PROP_TYPE);
String encpass = resource.getProperty(SecurityConstants.PROP_PRIVATE_KEY_PASS);
CryptoUtil util = CryptoUtil.getDefaultCryptoUtil();
privateKeyPassowrd = new String(util.base64DecodeAndDecrypt(encpass));
}
// Fill the information about the certificates
Enumeration<String> aliases = keyStore.aliases();
List<org.wso2.carbon.security.keystore.service.CertData> certDataList = new ArrayList<CertData>();
Format formatter = new SimpleDateFormat("dd/MM/yyyy");
while (aliases.hasMoreElements()) {
String alias = aliases.nextElement();
if (keyStore.isCertificateEntry(alias)) {
X509Certificate cert = (X509Certificate) keyStore.getCertificate(alias);
certDataList.add(fillCertData(cert, alias, formatter));
}
}
// Create a cert array
CertData[] certs = certDataList.toArray(new CertData[certDataList.size()]);
// Create a KeyStoreData bean, set the name and fill in the cert information
KeyStoreData keyStoreData = new KeyStoreData();
keyStoreData.setKeyStoreName(keyStoreName);
keyStoreData.setCerts(certs);
keyStoreData.setKeyStoreType(keyStoreType);
aliases = keyStore.aliases();
while (aliases.hasMoreElements()) {
String alias = aliases.nextElement();
// There be only one entry in WSAS related keystores
if (keyStore.isKeyEntry(alias)) {
X509Certificate cert = (X509Certificate) keyStore.getCertificate(alias);
keyStoreData.setKey(fillCertData(cert, alias, formatter));
PrivateKey key = (PrivateKey) keyStore.getKey(alias, privateKeyPassowrd
.toCharArray());
String pemKey;
pemKey = "-----BEGIN PRIVATE KEY-----\n";
pemKey += Base64.encode(key.getEncoded());
pemKey += "\n-----END PRIVATE KEY-----";
keyStoreData.setKeyValue(pemKey);
break;
}
}
return keyStoreData;
} catch (Exception e) {
String msg = "Error has encounted while loading the keystore to the given keystore name "
+ keyStoreName;
log.error(msg, e);
throw new SecurityConfigException(msg);
}
}