-
Notifications
You must be signed in to change notification settings - Fork 712
[4.12.x]Support for HSM-backed Keystores for Signing and Encryption #4518
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
RivinduM
wants to merge
2
commits into
wso2:4.12.x
Choose a base branch
from
RivinduM:4.12.x
base: 4.12.x
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change | ||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -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; | ||||||||||||||||||
|
|
@@ -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 | ||||||||||||||||||
| * | ||||||||||||||||||
|
|
@@ -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; | ||||||||||||||||||
|
|
@@ -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) { | ||||||||||||||||||
| 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
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Log Improvement Suggestion No: 2
Suggested change
|
||||||||||||||||||
| return ks; | ||||||||||||||||||
| } | ||||||||||||||||||
| throw new CarbonException(String.format(PERMISSION_DENIED_ERROR, "HSM key store", "HSM key store")); | ||||||||||||||||||
| } | ||||||||||||||||||
|
|
||||||||||||||||||
| /** | ||||||||||||||||||
| * Load the requested tenant keystore. | ||||||||||||||||||
| * | ||||||||||||||||||
|
|
@@ -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")); | ||||||||||||||||||
| } | ||||||||||||||||||
|
|
@@ -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")); | ||||||||||||||||||
|
|
||||||||||||||||||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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