mirror of
https://github.com/floci-io/floci.git
synced 2026-11-24 06:20:24 -05:00
fix(kms): support HMAC_* key specs in CreateKey (#544)
CreateKey with `--key-spec HMAC_{224,256,384,512} --key-usage
GENERATE_VERIFY_MAC` now succeeds instead of throwing
InvalidCustomerMasterKeySpecException. DescribeKey returns the matching
MacAlgorithms array (HMAC_SHA_*), and GetPublicKey correctly rejects
HMAC keys with UnsupportedOperationException.
Validation also rejects the two incompatible pairings:
- HMAC_* without GENERATE_VERIFY_MAC usage
- GENERATE_VERIFY_MAC usage with any non-HMAC spec
HMAC material is generated with SecureRandom and base64-stored on the
existing privateKeyEncoded field (same pattern as RSA/ECC private key
bytes). Generation sits outside the try block that wraps asymmetric
keypair creation, so ValidationExceptions aren't rewrapped as 500s.
Closes #497.
Out of scope (follow-up PRs):
- GenerateMac / VerifyMac operations
- HMAC key rotation support
- Catch-rewrap bug in generateKeyMaterial for asymmetric keys
This commit is contained in:
4 files changed
+125
-3
No files matched your search
@@ -350,6 +350,10 @@ public class KmsJsonHandler {
|
||||
node.put("KeyManager", "CUSTOMER");
|
||||
node.put("CustomerMasterKeySpec", k.getCustomerMasterKeySpec());
|
||||
node.put("KeySpec", k.getCustomerMasterKeySpec());
|
||||
String macAlgo = KmsService.macAlgorithmFor(k.getCustomerMasterKeySpec());
|
||||
if (macAlgo != null) {
|
||||
node.putArray("MacAlgorithms").add(macAlgo);
|
||||
}
|
||||
if (k.getDeletionDate() > 0) {
|
||||
node.put("DeletionDate", k.getDeletionDate());
|
||||
}
|
||||
|
||||
@@ -81,12 +81,16 @@ public class KmsService {
|
||||
String keyId = UUID.randomUUID().toString();
|
||||
String arn = regionResolver.buildArn("kms", region, "key/" + keyId);
|
||||
|
||||
String effectiveUsage = keyUsage != null ? keyUsage : "ENCRYPT_DECRYPT";
|
||||
String effectiveSpec = customerMasterKeySpec != null ? customerMasterKeySpec : "SYMMETRIC_DEFAULT";
|
||||
validateKeyUsageForSpec(effectiveUsage, effectiveSpec);
|
||||
|
||||
KmsKey key = new KmsKey();
|
||||
key.setKeyId(keyId);
|
||||
key.setArn(arn);
|
||||
key.setDescription(description);
|
||||
key.setKeyUsage(keyUsage != null ? keyUsage : "ENCRYPT_DECRYPT");
|
||||
key.setCustomerMasterKeySpec(customerMasterKeySpec != null ? customerMasterKeySpec : "SYMMETRIC_DEFAULT");
|
||||
key.setKeyUsage(effectiveUsage);
|
||||
key.setCustomerMasterKeySpec(effectiveSpec);
|
||||
key.setPolicy(policy != null ? policy : DEFAULT_KEY_POLICY);
|
||||
if (tags != null) {
|
||||
key.getTags().putAll(tags);
|
||||
@@ -104,6 +108,14 @@ public class KmsService {
|
||||
if ("SYMMETRIC_DEFAULT".equals(spec)) {
|
||||
return; // Use existing mock behavior for symmetric keys
|
||||
}
|
||||
if (isHmac(spec)) {
|
||||
// HMAC keys are symmetric byte strings; generate outside the try block
|
||||
// so ValidationException (400) isn't rewrapped as InternalFailure (500).
|
||||
byte[] material = new byte[hmacKeyByteLength(spec)];
|
||||
new SecureRandom().nextBytes(material);
|
||||
key.setPrivateKeyEncoded(Base64.getEncoder().encodeToString(material));
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
KeyPairGenerator generator;
|
||||
@@ -141,12 +153,55 @@ public class KmsService {
|
||||
|
||||
public KmsKey getPublicKey(String keyId, String region) {
|
||||
KmsKey key = resolveKey(keyId, region);
|
||||
if ("SYMMETRIC_DEFAULT".equals(key.getCustomerMasterKeySpec())) {
|
||||
String spec = key.getCustomerMasterKeySpec();
|
||||
if ("SYMMETRIC_DEFAULT".equals(spec) || isHmac(spec)) {
|
||||
throw new AwsException("UnsupportedOperationException", "GetPublicKey is not supported for symmetric keys.", 400);
|
||||
}
|
||||
return key;
|
||||
}
|
||||
|
||||
private static boolean isHmac(String spec) {
|
||||
return spec != null && spec.startsWith("HMAC_");
|
||||
}
|
||||
|
||||
private static void validateKeyUsageForSpec(String keyUsage, String spec) {
|
||||
if (isHmac(spec) && !"GENERATE_VERIFY_MAC".equals(keyUsage)) {
|
||||
throw new AwsException("ValidationException",
|
||||
"KeyUsage " + keyUsage + " is not compatible with KeySpec " + spec
|
||||
+ ". HMAC key specs require KeyUsage GENERATE_VERIFY_MAC.",
|
||||
400);
|
||||
}
|
||||
if ("GENERATE_VERIFY_MAC".equals(keyUsage) && !isHmac(spec)) {
|
||||
throw new AwsException("ValidationException",
|
||||
"KeyUsage GENERATE_VERIFY_MAC requires an HMAC KeySpec (HMAC_224, HMAC_256, HMAC_384, or HMAC_512).",
|
||||
400);
|
||||
}
|
||||
}
|
||||
|
||||
private static int hmacKeyByteLength(String spec) {
|
||||
return switch (spec) {
|
||||
case "HMAC_224" -> 28;
|
||||
case "HMAC_256" -> 32;
|
||||
case "HMAC_384" -> 48;
|
||||
case "HMAC_512" -> 64;
|
||||
default -> throw new AwsException("InvalidCustomerMasterKeySpecException",
|
||||
"Unsupported HMAC key spec: " + spec, 400);
|
||||
};
|
||||
}
|
||||
|
||||
static String macAlgorithmFor(String spec) {
|
||||
if (!isHmac(spec)) {
|
||||
return null;
|
||||
}
|
||||
return switch (spec) {
|
||||
case "HMAC_224" -> "HMAC_SHA_224";
|
||||
case "HMAC_256" -> "HMAC_SHA_256";
|
||||
case "HMAC_384" -> "HMAC_SHA_384";
|
||||
case "HMAC_512" -> "HMAC_SHA_512";
|
||||
default -> null;
|
||||
};
|
||||
}
|
||||
|
||||
public KmsKey describeKey(String keyId, String region) {
|
||||
return resolveKey(keyId, region);
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user