mirror of
https://github.com/floci-io/floci.git
synced 2026-11-24 06:20:24 -05:00
* fix(kms): validate KeyUsage against KeySpec on CreateKey (#2787) * test(kms): boto3 coverage for CreateKey KeyUsage rejection (#2787)
This commit is contained in:
1 parent
e26e9a68fd
commit
69e8bf1ac3
4 files changed
+86
-21
No files matched your search
@@ -258,16 +258,9 @@ public class KmsService implements ResourceProvider {
|
||||
}
|
||||
|
||||
private static void validateKeyUsageForSpec(KmsKeyUsage keyUsage, KmsKeySpec spec) {
|
||||
if (isHmac(spec) && KmsKeyUsage.GENERATE_VERIFY_MAC != keyUsage) {
|
||||
if (!spec.allowedKeyUsages().contains(keyUsage)) {
|
||||
throw new AwsException("ValidationException",
|
||||
"KeyUsage " + keyUsage + " is not compatible with KeySpec " + spec
|
||||
+ ". HMAC key specs require KeyUsage GENERATE_VERIFY_MAC.",
|
||||
400);
|
||||
}
|
||||
if (KmsKeyUsage.GENERATE_VERIFY_MAC == 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);
|
||||
"KeyUsage " + keyUsage + " is not compatible with KeySpec " + spec + ".", 400);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -6,6 +6,7 @@ import io.quarkus.runtime.annotations.RegisterForReflection;
|
||||
import java.util.ArrayList;
|
||||
import java.util.EnumSet;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
|
||||
@RegisterForReflection
|
||||
public enum KmsKeySpec {
|
||||
@@ -79,6 +80,28 @@ public enum KmsKeySpec {
|
||||
public String curveName() {
|
||||
return curveName;
|
||||
}
|
||||
|
||||
/**
|
||||
* The KeyUsage values AWS KMS accepts for this key spec at CreateKey.
|
||||
*
|
||||
* <p>Source: CreateKey API reference, KeyUsage parameter. NIST-standard ECC
|
||||
* pairs allow SIGN_VERIFY or KEY_AGREEMENT; ECC_SECG_P256K1 and Edwards25519
|
||||
* are signing only; RSA allows ENCRYPT_DECRYPT or SIGN_VERIFY; symmetric is
|
||||
* ENCRYPT_DECRYPT only; HMAC is GENERATE_VERIFY_MAC only.
|
||||
*/
|
||||
public Set<KmsKeyUsage> allowedKeyUsages() {
|
||||
return switch (keyType) {
|
||||
case SYMMETRIC -> EnumSet.of(KmsKeyUsage.ENCRYPT_DECRYPT);
|
||||
case HMAC -> EnumSet.of(KmsKeyUsage.GENERATE_VERIFY_MAC);
|
||||
case RSA -> EnumSet.of(KmsKeyUsage.ENCRYPT_DECRYPT, KmsKeyUsage.SIGN_VERIFY);
|
||||
case ED25519, ML_DSA -> EnumSet.of(KmsKeyUsage.SIGN_VERIFY);
|
||||
case SM2 -> EnumSet.of(KmsKeyUsage.ENCRYPT_DECRYPT, KmsKeyUsage.SIGN_VERIFY, KmsKeyUsage.KEY_AGREEMENT);
|
||||
case ECC -> this == ECC_SECG_P256K1
|
||||
? EnumSet.of(KmsKeyUsage.SIGN_VERIFY)
|
||||
: EnumSet.of(KmsKeyUsage.SIGN_VERIFY, KmsKeyUsage.KEY_AGREEMENT);
|
||||
};
|
||||
}
|
||||
|
||||
public enum KeyType {
|
||||
RSA, ECC, ED25519, SYMMETRIC, HMAC, ML_DSA, SM2
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user