- Newest
- Most votes
- Most comments
To read key attributes and key material using version 5 of the CloudHSM JCE provider, you'll need to consider a few important points:
-
Reading key attributes: Unfortunately, there isn't a direct method to read key attributes like the label using the JCE provider. The KeyAttributesMap is primarily used for setting attributes during key generation or import, not for retrieving them afterwards. To get key attributes, you would typically need to use the CloudHSM CLI or other management tools.
-
Retrieving key material: By default, the AWS CloudHSM JCE Provider doesn't allow extraction of key bytes in clear for private or secret keys. This is why key.getEncoded() is returning null. This is a security measure to protect sensitive key material.
However, if your use case requires it, you can enable extraction of key bytes in clear for private or secret keys under specific conditions:
a. The EXTRACTABLE attribute for the key must be set to true (which is the default). b. The WRAP_WITH_TRUSTED attribute for the key must be set to false.
Keep in mind that allowing key extraction in clear text should only be done when absolutely necessary, as it can pose security risks.
If you need to work with the key material, it's generally recommended to use secure key wrapping and unwrapping mechanisms instead of extracting the raw key bytes.
For public keys, you should be able to use getEncoded() without any special configuration.
To modify your code to potentially extract key material (assuming the necessary attributes are set correctly):
for (Key key : keys) { if (key instanceof PublicKey) { byte[] encodedKey = key.getEncoded(); if (encodedKey != null) { System.out.println("Public key material: " + Base64.getEncoder().encodeToString(encodedKey)); } } else if (key instanceof PrivateKey || key instanceof SecretKey) { // Be cautious with this part, as it may not work by default byte[] encodedKey = key.getEncoded(); if (encodedKey != null) { System.out.println("Private/Secret key material: " + Base64.getEncoder().encodeToString(encodedKey)); } else { System.out.println("Unable to extract key material (null)"); } } }
Remember, extracting key material, especially for private or secret keys, should be done with caution and only when absolutely necessary for your specific use case.
Sources
Key extraction using JCE for AWS CloudHSM - AWS CloudHSM
Supported Java key attributes for AWS CloudHSM Client SDK 5 - AWS CloudHSM
Relevant content
asked 3 years ago
- AWS OFFICIALUpdated 2 years ago
