Skip to content
Open
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 @@ -268,7 +268,9 @@ public List<String> getAllAlias() {
public Key getKey(String alias) {
Key key = null;
try {
long startTime = System.currentTimeMillis();
key = keyStore.getKey(alias, keystorePwdCharArr);
LOGGER.debug("sessionId", "KeyStoreImpl","getKey", "HSM interaction time(ms): " + (System.currentTimeMillis() - startTime));
} catch (UnrecoverableKeyException | KeyStoreException | NoSuchAlgorithmException e) {
throw new KeystoreProcessingException(KeymanagerErrorCode.KEYSTORE_PROCESSING_ERROR.getErrorCode(),
KeymanagerErrorCode.KEYSTORE_PROCESSING_ERROR.getErrorMessage() + e.getMessage(), e);
Expand Down Expand Up @@ -296,15 +298,19 @@ public PrivateKeyEntry getAsymmetricKey(String alias) {
Exception exp = null;
do {
try {
if (keyStore.entryInstanceOf(alias, PrivateKeyEntry.class)) {
long startTime = System.currentTimeMillis();
boolean isPrivateKeyEntry = keyStore.entryInstanceOf(alias, PrivateKeyEntry.class);
if (isPrivateKeyEntry) {
LOGGER.debug("sessionId", "KeyStoreImpl", "getAsymmetricKey", "alias is instanceof keystore");
ProtectionParameter password = getPasswordProtection();
privateKeyEntry = (PrivateKeyEntry) keyStore.getEntry(alias, password);
LOGGER.debug("sessionId", "KeyStoreImpl","getAsymmetricKey", "HSM interaction time(ms): " + (System.currentTimeMillis() - startTime));
if (privateKeyEntry != null) {
LOGGER.debug("sessionId", "KeyStoreImpl", "getAsymmetricKey", "privateKeyEntry is not null");
break;
}
} else {
LOGGER.debug("sessionId", "KeyStoreImpl","getAsymmetricKey", "HSM interaction time(ms): " + (System.currentTimeMillis() - startTime));
throw new NoSuchSecurityProviderException(KeymanagerErrorCode.NO_SUCH_ALIAS.getErrorCode(),
KeymanagerErrorCode.NO_SUCH_ALIAS.getErrorMessage() + alias);
}
Expand Down Expand Up @@ -413,15 +419,19 @@ public SecretKey getSymmetricKey(String alias) {
Exception exp = null;
do {
try {
if (keyStore.entryInstanceOf(alias, SecretKeyEntry.class)) {
long startTime = System.currentTimeMillis();
boolean isSecretKeyEntry = keyStore.entryInstanceOf(alias, SecretKeyEntry.class);
if (isSecretKeyEntry) {
ProtectionParameter password = getPasswordProtection();
SecretKeyEntry retrivedSecret = (SecretKeyEntry) keyStore.getEntry(alias, password);
LOGGER.debug("sessionId", "KeyStoreImpl","getSymmetricKey", "HSM interaction time(ms): " + (System.currentTimeMillis() - startTime));
secretKey = retrivedSecret.getSecretKey();
if (secretKey != null) {
LOGGER.debug("sessionId", "KeyStoreImpl", "getSymmetricKey", "secretKey is not null");
break;
}
} else {
LOGGER.debug("sessionId", "KeyStoreImpl","getSymmetricKey", "HSM interaction time(ms): " + (System.currentTimeMillis() - startTime));
throw new NoSuchSecurityProviderException(KeymanagerErrorCode.NO_SUCH_ALIAS.getErrorCode(),
KeymanagerErrorCode.NO_SUCH_ALIAS.getErrorMessage() + alias);
}
Expand Down Expand Up @@ -468,8 +478,10 @@ private void storeCertificate(String alias, Certificate[] chain, PrivateKey priv
PrivateKeyEntry privateKeyEntry = new PrivateKeyEntry(privateKey, chain);
ProtectionParameter password = getPasswordProtection();
try {
long startTime = System.currentTimeMillis();
keyStore.setEntry(alias, privateKeyEntry, password);
keyStore.store(null, keystorePwdCharArr);
LOGGER.debug("sessionId", "KeyStoreImpl","storeCertificate", "HSM interaction time(ms): " + (System.currentTimeMillis() - startTime));
} catch (KeyStoreException | NoSuchAlgorithmException | CertificateException | IOException e) {
throw new KeystoreProcessingException(KeymanagerErrorCode.KEYSTORE_PROCESSING_ERROR.getErrorCode(),
KeymanagerErrorCode.KEYSTORE_PROCESSING_ERROR.getErrorMessage() + e.getMessage());
Expand Down Expand Up @@ -530,8 +542,10 @@ public void generateAndStoreSymmetricKey(String alias) {
SecretKeyEntry secret = new SecretKeyEntry(secretKey);
ProtectionParameter password = getPasswordProtection();
try {
long startTime = System.currentTimeMillis();
keyStore.setEntry(alias, secret, password);
keyStore.store(null, keystorePwdCharArr);
LOGGER.debug("sessionId", "KeyStoreImpl","generateAndStoreSymmetricKey", "HSM interaction time(ms): " + (System.currentTimeMillis() - startTime));
} catch (KeyStoreException | NoSuchAlgorithmException | CertificateException | IOException e) {
throw new KeystoreProcessingException(KeymanagerErrorCode.KEYSTORE_PROCESSING_ERROR.getErrorCode(),
KeymanagerErrorCode.KEYSTORE_PROCESSING_ERROR.getErrorMessage() + e.getMessage(), e);
Expand All @@ -542,7 +556,10 @@ private KeyPair generateRSAKeyPair() {
try {
KeyPairGenerator generator = KeyPairGenerator.getInstance(asymmetricKeyAlgorithm, provider);
generator.initialize(asymmetricKeyLength, secureRandom);
return generator.generateKeyPair();
long startTime = System.currentTimeMillis();
KeyPair keyPair = generator.generateKeyPair();
LOGGER.debug("sessionId", "KeyStoreImpl","generateRSAKeyPair", "HSM interaction time(ms): " + (System.currentTimeMillis() - startTime));
return keyPair;
} catch (java.security.NoSuchAlgorithmException e) {
throw new io.mosip.kernel.core.exception.NoSuchAlgorithmException(
KeyGeneratorExceptionConstant.MOSIP_NO_SUCH_ALGORITHM_EXCEPTION.getErrorCode(),
Expand All @@ -558,7 +575,10 @@ private KeyPair generateECKeyPair(String ecCurve) {
}
KeyPairGenerator generator = KeyPairGenerator.getInstance(asymmetricECKeyAlgorithm, provider);
generator.initialize(new ECGenParameterSpec(ecCurve), secureRandom);
return generator.generateKeyPair();
long startTime = System.currentTimeMillis();
KeyPair keyPair = generator.generateKeyPair();
LOGGER.debug("sessionId", "KeyStoreImpl","generateECKeyPair", "HSM interaction time(ms): " + (System.currentTimeMillis() - startTime));
return keyPair;
} catch (java.security.NoSuchAlgorithmException | InvalidAlgorithmParameterException e) {
throw new io.mosip.kernel.core.exception.NoSuchAlgorithmException(
KeyGeneratorExceptionConstant.MOSIP_NO_SUCH_ALGORITHM_EXCEPTION.getErrorCode(),
Expand All @@ -570,22 +590,27 @@ private SecretKey generateSymmetricKey() {
try {
KeyGenerator generator = KeyGenerator.getInstance(symmetricKeyAlgorithm, provider);
generator.init(symmetricKeyLength, secureRandom);
return generator.generateKey();
long startTime = System.currentTimeMillis();
SecretKey secretKey = generator.generateKey();
LOGGER.debug("sessionId", "KeyStoreImpl","generateSymmetricKey", "HSM interaction time(ms): " + (System.currentTimeMillis() - startTime));
return secretKey;
} catch (java.security.NoSuchAlgorithmException e) {
throw new io.mosip.kernel.core.exception.NoSuchAlgorithmException(
KeyGeneratorExceptionConstant.MOSIP_NO_SUCH_ALGORITHM_EXCEPTION.getErrorCode(),
KeyGeneratorExceptionConstant.MOSIP_NO_SUCH_ALGORITHM_EXCEPTION.getErrorMessage(), e);
}

}

@Override
public void storeCertificate(String alias, PrivateKey privateKey, Certificate certificate) {
try {
PrivateKeyEntry privateKeyEntry = new PrivateKeyEntry(privateKey, new Certificate[] {certificate});
ProtectionParameter password = getPasswordProtection();
long startTime = System.currentTimeMillis();
keyStore.setEntry(alias, privateKeyEntry, password);
keyStore.store(null, keystorePwdCharArr);
LOGGER.debug("sessionId", "KeyStoreImpl","storeCertificate", "HSM interaction time(ms): " + (System.currentTimeMillis() - startTime));
} catch (KeyStoreException | NoSuchAlgorithmException | CertificateException | IOException e) {
throw new KeystoreProcessingException(KeymanagerErrorCode.KEYSTORE_PROCESSING_ERROR.getErrorCode(),
KeymanagerErrorCode.KEYSTORE_PROCESSING_ERROR.getErrorMessage() + e.getMessage(), e);
Expand Down
Loading