Skip to content

Commit

Permalink
Add private exponent and token attributes to RSA KeyAttributesMap in …
Browse files Browse the repository at this point in the history
…ImportKey sample (#58)
  • Loading branch information
nikran-amzn authored Aug 16, 2022
1 parent 207c12c commit 1b9ac71
Showing 1 changed file with 5 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,7 @@ private static Key importRsaKey(Key key, String keyLabel)
// Add key data for the key to be imported
KeyAttributesMap keySpec = new KeyAttributesMapBuilder()
.put(KeyAttribute.MODULUS, rsaKey.getModulus().toByteArray())
.put(KeyAttribute.PRIVATE_EXPONENT, rsaKey.getPrivateExponent().toByteArray())
.put(KeyAttribute.PUBLIC_EXPONENT, rsaKey.getPublicExponent().toByteArray())
.put(KeyAttribute.PRIME_P, rsaKey.getPrimeP().toByteArray())
.put(KeyAttribute.PRIME_Q, rsaKey.getPrimeQ().toByteArray())
Expand All @@ -260,6 +261,10 @@ private static Key importRsaKey(Key key, String keyLabel)
// Add additional key related attributes
keySpec.put(KeyAttribute.LABEL, keyLabel);

// The imported key will be ephemeral; it will be deleted from the HSM when the
// application exits. To persist the key you must set this attribute to true.
keySpec.put(KeyAttribute.TOKEN, false);

try {
KeyFactory keyFactory = KeyFactory.getInstance("RSA", CloudHsmProvider.PROVIDER_NAME);
PrivateKey importedKey = keyFactory.generatePrivate(keySpec);
Expand Down

0 comments on commit 1b9ac71

Please sign in to comment.