Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,12 @@ public static final class SecurityManagement {

public static final String DEFAULT_SECURITY_CERTIFICATE_ALIAS = "wso2carbon";

//HSM key store
public static final String SERVER_HSM_KEYSTORE_ENABLED = "Security.HSMKeyStore.Enabled";
public static final String SERVER_HSM_KEYSTORE_PROVIDER_CONFIG_FILE = "Security.HSMKeyStore.ProviderConfiguration";
public static final String SERVER_HSM_KEYSTORE_PASSWORD = "Security.HSMKeyStore.Password";
public static final String SERVER_HSM_KEYSTORE_KEY_ALIAS = "Security.HSMKeyStore.KeyAlias";

//Registry store
public static final String SERVER_REGISTRY_KEYSTORE_FILE = "Security.RegistryKeyStore.Location";
public static final String SERVER_REGISTRY_KEYSTORE_PASSWORD = "Security.RegistryKeyStore.Password";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,9 @@
import java.security.NoSuchAlgorithmException;
import java.security.NoSuchProviderException;
import java.security.PrivateKey;
import java.security.Provider;
import java.security.PublicKey;
import java.security.Security;
import java.security.UnrecoverableKeyException;
import java.security.cert.Certificate;
import java.security.cert.CertificateException;
Expand Down Expand Up @@ -91,6 +93,9 @@ public class KeyStoreManager {
private static final String PERMISSION_DENIED_ERROR = "Permission denied for accessing %s. The %s is " +
"available only for the super tenant.";

private static final String SUN_PKCS11 = "SunPKCS11";
private static final String PKCS11 = "PKCS11";

/**
* Private Constructor of the KeyStoreManager
*
Expand Down Expand Up @@ -547,18 +552,23 @@ public KeyStore getPrimaryKeyStore() throws Exception {
if (tenantId == MultitenantConstants.SUPER_TENANT_ID) {
if (primaryKeyStore == null) {

ServerConfigurationService config = this.getServerConfigService();
String file =
new File(config
.getFirstProperty(RegistryResources.SecurityManagement.SERVER_PRIMARY_KEYSTORE_FILE))
.getAbsolutePath();
KeyStore store = KeystoreUtils.getKeystoreInstance(config
.getFirstProperty(RegistryResources.SecurityManagement.SERVER_PRIMARY_KEYSTORE_TYPE));
String password = config
.getFirstProperty(RegistryResources.SecurityManagement.SERVER_PRIMARY_KEYSTORE_PASSWORD);
try (FileInputStream in = new FileInputStream(file)) {
store.load(in, password.toCharArray());
primaryKeyStore = store;
if (isHSMEnabled()) {
log.info("HSM keystore is enabled. Loading HSM keystore.");
primaryKeyStore = getHSMKeyStore();
} else {
ServerConfigurationService config = this.getServerConfigService();
String file =
new File(config
.getFirstProperty(RegistryResources.SecurityManagement.SERVER_PRIMARY_KEYSTORE_FILE))
.getAbsolutePath();
KeyStore store = KeystoreUtils.getKeystoreInstance(config
.getFirstProperty(RegistryResources.SecurityManagement.SERVER_PRIMARY_KEYSTORE_TYPE));
String password = config
.getFirstProperty(RegistryResources.SecurityManagement.SERVER_PRIMARY_KEYSTORE_PASSWORD);
try (FileInputStream in = new FileInputStream(file)) {
store.load(in, password.toCharArray());
primaryKeyStore = store;
}
}
}
return primaryKeyStore;
Expand All @@ -567,6 +577,46 @@ public KeyStore getPrimaryKeyStore() throws Exception {
}
}

/**
* Check whether HSM based keystore is enabled or not.
* @return true if HSM based keystore is enabled, false otherwise.
*/
private boolean isHSMEnabled() {

return Boolean.parseBoolean(this.getServerConfigService()
.getFirstProperty(RegistryResources.SecurityManagement.SERVER_HSM_KEYSTORE_ENABLED));
}

/**
* Load the HSM key store
*
* @return HSM key store object
* @throws KeyStoreException if KeyStoreSpi implementation for the specified type is not available from the
* specified Provider object.
* @throws CertificateException if any of the certificates in the keystore could not be loaded.
* @throws IOException if there is an I/O or format problem with the keystore data, if a password is required but
* not given, or if the given password was incorrect.
* @throws NoSuchAlgorithmException if the algorithm used to check the integrity of the keystore cannot be found.
*
*/
public KeyStore getHSMKeyStore() throws
KeyStoreException, CertificateException, IOException, NoSuchAlgorithmException, CarbonException {

if (tenantId == MultitenantConstants.SUPER_TENANT_ID) {

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Log Improvement Suggestion No: 1

Suggested change
if (tenantId == MultitenantConstants.SUPER_TENANT_ID) {
if (tenantId == MultitenantConstants.SUPER_TENANT_ID) {
log.info("Loading HSM key store for super tenant.");

log.debug("Loading HSM key store.");
Provider pkcs11 = Security.getProvider(SUN_PKCS11);
pkcs11 = pkcs11.configure(this.getServerConfigService()
.getFirstProperty(RegistryResources.SecurityManagement.SERVER_HSM_KEYSTORE_PROVIDER_CONFIG_FILE));
Security.addProvider(pkcs11);
KeyStore ks = KeyStore.getInstance(PKCS11, pkcs11);
ks.load(null, this.getServerConfigService()
.getFirstProperty(RegistryResources.SecurityManagement.SERVER_HSM_KEYSTORE_PASSWORD).toCharArray());
log.info("HSM keystore loaded successfully.");
Comment on lines +611 to +614

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Log Improvement Suggestion No: 2

Suggested change
KeyStore ks = KeyStore.getInstance(PKCS11, pkcs11);
ks.load(null, this.getServerConfigService()
.getFirstProperty(RegistryResources.SecurityManagement.SERVER_HSM_KEYSTORE_PASSWORD).toCharArray());
log.info("HSM keystore loaded successfully.");
KeyStore ks = KeyStore.getInstance(PKCS11, pkcs11);
ks.load(null, this.getServerConfigService()
.getFirstProperty(RegistryResources.SecurityManagement.SERVER_HSM_KEYSTORE_PASSWORD).toCharArray());
log.info("HSM keystore loaded successfully with provider: " + pkcs11.getName());

return ks;
}
throw new CarbonException(String.format(PERMISSION_DENIED_ERROR, "HSM key store", "HSM key store"));
}

/**
* Load the requested tenant keystore.
*
Expand Down Expand Up @@ -764,11 +814,19 @@ public PrivateKey getDefaultPrivateKey() throws Exception {
}
if (tenantId == MultitenantConstants.SUPER_TENANT_ID) {
ServerConfigurationService config = this.getServerConfigService();
String password = config
.getFirstProperty(RegistryResources.SecurityManagement.SERVER_PRIMARY_KEYSTORE_PASSWORD);
String alias = config
.getFirstProperty(RegistryResources.SecurityManagement.SERVER_PRIMARY_KEYSTORE_KEY_ALIAS);
return (PrivateKey) primaryKeyStore.getKey(alias, password.toCharArray());
if (isHSMEnabled()) {
String alias = config
.getFirstProperty(RegistryResources.SecurityManagement.SERVER_HSM_KEYSTORE_KEY_ALIAS);
String password = config
.getFirstProperty(RegistryResources.SecurityManagement.SERVER_HSM_KEYSTORE_PASSWORD);
return (PrivateKey) primaryKeyStore.getKey(alias, password.toCharArray());
} else {
String password = config
.getFirstProperty(RegistryResources.SecurityManagement.SERVER_PRIMARY_KEYSTORE_PASSWORD);
String alias = config
.getFirstProperty(RegistryResources.SecurityManagement.SERVER_PRIMARY_KEYSTORE_KEY_ALIAS);
return (PrivateKey) primaryKeyStore.getKey(alias, password.toCharArray());
}
}
throw new CarbonException(String.format(PERMISSION_DENIED_ERROR, "primary key store", "primary key store"));
}
Expand Down Expand Up @@ -853,8 +911,12 @@ public PrivateKey getDefaultPrivateKey(String alias) throws CarbonException,
public PublicKey getDefaultPublicKey() throws Exception {
if (tenantId == MultitenantConstants.SUPER_TENANT_ID) {
ServerConfigurationService config = this.getServerConfigService();
String alias = config
.getFirstProperty(RegistryResources.SecurityManagement.SERVER_PRIMARY_KEYSTORE_KEY_ALIAS);
String alias = isHSMEnabled() ? config
.getFirstProperty(RegistryResources.SecurityManagement.SERVER_HSM_KEYSTORE_KEY_ALIAS) :
config.getFirstProperty(RegistryResources.SecurityManagement.SERVER_PRIMARY_KEYSTORE_KEY_ALIAS);
if (log.isDebugEnabled()) {
log.debug("Loading primary key store public certificate with alias: " + alias);
}
return getPrimaryKeyStore().getCertificate(alias).getPublicKey();
}
throw new CarbonException(String.format(PERMISSION_DENIED_ERROR, "primary key store", "primary key store"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -535,6 +535,16 @@
<!-- Keystore type (JKS/PKCS12 etc.)-->
<Type>{{keystore.tenant.type}}</Type>
</TenantKeyStore>
<!-- KeyStore configurations for HSM.-->
<HSMKeyStore>
<Enabled>{{keystore.hsm.enabled}}</Enabled>
<!-- Provider Configuration file location-->
<ProviderConfiguration>${carbon.home}/repository/resources/security/{{keystore.hsm.provider_configuration}}</ProviderConfiguration>
<!-- Keystore password-->
<Password>{{keystore.hsm.pin}}</Password>
<!-- Private Key alias-->
<KeyAlias>{{keystore.hsm.alias}}</KeyAlias>
</HSMKeyStore>

<!--
The KeyStore which is used for encrypting/decrypting internal data.
Expand Down