refactor(kms): use enums for type-safety. add algorithms to keymetadata (#1430)

* refactor(kms) use enums for type-safety. add algorithms to keymetadata

Signed-off-by: Mark Hayen <mark.hayen@edsn.nl>

* refactor(kms): use enums for type-safety. add algorithms to keymetadata

Signed-off-by: Mark Hayen <mark.hayen@edsn.nl>

* refactor(kms): update missing algorithms handling and add tests to cover all combinations of keySpec,KeyUsage

Signed-off-by: Mark Hayen <mark.hayen@edsn.nl>

---------

Signed-off-by: Mark Hayen <mark.hayen@edsn.nl>
Co-authored-by: Mark Hayen <mark.hayen@edsn.nl>
This commit is contained in:
MarkandMark Hayen authored and GitHub committed 2026-06-21 01:02:01 -05:00
1 parent 93782c0142
commit c2cd31216b
10 files changed
+759 -230

No files matched your search

@@ -4,15 +4,19 @@ import io.github.hectorvent.floci.testing.RestAssuredJsonUtils;
import io.quarkus.test.junit.QuarkusTest;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.CsvSource;
import java.nio.charset.StandardCharsets;
import java.util.Base64;
import java.util.List;
import static io.restassured.RestAssured.given;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.notNullValue;
import static org.hamcrest.Matchers.startsWith;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
@QuarkusTest
class KmsIntegrationTest {
@@ -26,7 +30,7 @@ class KmsIntegrationTest {
@Test
void updateKeyDescriptionRoundTripThroughJsonHandler() {
String keyId = given()
var key = given()
.header("X-Amz-Target", "TrentService.CreateKey")
.contentType(KMS_CONTENT_TYPE)
.body("""
@@ -39,7 +43,12 @@ class KmsIntegrationTest {
.then()
.statusCode(200)
.body("KeyMetadata.KeyId", notNullValue())
.extract().jsonPath().getString("KeyMetadata.KeyId");
.extract().jsonPath();
String keyId = key.getString("KeyMetadata.KeyId");
assertNotNull(keyId);
List<String> encryptionAlgorithms = key.getList("KeyMetadata.EncryptionAlgorithms");
assertEquals(List.of("SYMMETRIC_DEFAULT"), encryptionAlgorithms);
given()
.header("X-Amz-Target", "TrentService.UpdateKeyDescription")
@@ -67,7 +76,8 @@ class KmsIntegrationTest {
.post("/")
.then()
.statusCode(200)
.body("KeyMetadata.Description", equalTo("new description"));
.body("KeyMetadata.Description", equalTo("new description"))
.body("KeyMetadata.EncryptionAlgorithms", equalTo(List.of("SYMMETRIC_DEFAULT")));
}
@Test
@@ -955,4 +965,145 @@ class KmsIntegrationTest {
.statusCode(400)
.body("__type", equalTo("ValidationException"));
}
@ParameterizedTest
@CsvSource({
"SYMMETRIC_DEFAULT, ENCRYPT_DECRYPT, EncryptionAlgorithms, SYMMETRIC_DEFAULT",
"RSA_2048, ENCRYPT_DECRYPT, EncryptionAlgorithms, 'RSAES_OAEP_SHA_1,RSAES_OAEP_SHA_256'",
"RSA_2048, SIGN_VERIFY, SigningAlgorithms, 'RSASSA_PKCS1_V1_5_SHA_256,RSASSA_PKCS1_V1_5_SHA_384,RSASSA_PKCS1_V1_5_SHA_512,RSASSA_PSS_SHA_256,RSASSA_PSS_SHA_384,RSASSA_PSS_SHA_512'",
"RSA_3072, ENCRYPT_DECRYPT, EncryptionAlgorithms, 'RSAES_OAEP_SHA_1,RSAES_OAEP_SHA_256'",
"RSA_3072, SIGN_VERIFY, SigningAlgorithms, 'RSASSA_PKCS1_V1_5_SHA_256,RSASSA_PKCS1_V1_5_SHA_384,RSASSA_PKCS1_V1_5_SHA_512,RSASSA_PSS_SHA_256,RSASSA_PSS_SHA_384,RSASSA_PSS_SHA_512'",
"RSA_4096, ENCRYPT_DECRYPT, EncryptionAlgorithms, 'RSAES_OAEP_SHA_1,RSAES_OAEP_SHA_256'",
"RSA_4096, SIGN_VERIFY, SigningAlgorithms, 'RSASSA_PKCS1_V1_5_SHA_256,RSASSA_PKCS1_V1_5_SHA_384,RSASSA_PKCS1_V1_5_SHA_512,RSASSA_PSS_SHA_256,RSASSA_PSS_SHA_384,RSASSA_PSS_SHA_512'",
"ECC_NIST_P256, SIGN_VERIFY, SigningAlgorithms, ECDSA_SHA_256",
"ECC_NIST_P384, SIGN_VERIFY, SigningAlgorithms, ECDSA_SHA_384",
"ECC_NIST_P521, SIGN_VERIFY, SigningAlgorithms, ECDSA_SHA_512",
"ECC_SECG_P256K1, SIGN_VERIFY, SigningAlgorithms, ECDSA_SHA_256",
"HMAC_224, GENERATE_VERIFY_MAC, MacAlgorithms, HMAC_SHA_224",
"HMAC_256, GENERATE_VERIFY_MAC, MacAlgorithms, HMAC_SHA_256",
"HMAC_384, GENERATE_VERIFY_MAC, MacAlgorithms, HMAC_SHA_384",
"HMAC_512, GENERATE_VERIFY_MAC, MacAlgorithms, HMAC_SHA_512"
})
void createKeyWithAllImplementedCombinations(String keySpec, String keyUsage, String algorithmField, String expectedAlgorithms) {
List<String> expectedList = List.of(expectedAlgorithms.split(","));
given()
.header("X-Amz-Target", "TrentService.CreateKey")
.contentType(KMS_CONTENT_TYPE)
.body("""
{
"Description": "test key",
"KeyUsage": "%s",
"CustomerMasterKeySpec": "%s"
}
""".formatted(keyUsage, keySpec))
.when()
.post("/")
.then()
.statusCode(200)
.body("KeyMetadata.%s".formatted(algorithmField), equalTo(expectedList));
}
@ParameterizedTest
@CsvSource({
"SYMMETRIC_DEFAULT, ENCRYPT_DECRYPT, 200",
"SYMMETRIC_DEFAULT, SIGN_VERIFY, 200",
"SYMMETRIC_DEFAULT, GENERATE_VERIFY_MAC, 400",
"SYMMETRIC_DEFAULT, KEY_AGREEMENT, 200",
"RSA_2048, ENCRYPT_DECRYPT, 200",
"RSA_2048, SIGN_VERIFY, 200",
"RSA_2048, GENERATE_VERIFY_MAC, 400",
"RSA_2048, KEY_AGREEMENT, 200",
"RSA_3072, ENCRYPT_DECRYPT, 200",
"RSA_3072, SIGN_VERIFY, 200",
"RSA_3072, GENERATE_VERIFY_MAC, 400",
"RSA_3072, KEY_AGREEMENT, 200",
"RSA_4096, ENCRYPT_DECRYPT, 200",
"RSA_4096, SIGN_VERIFY, 200",
"RSA_4096, GENERATE_VERIFY_MAC, 400",
"RSA_4096, KEY_AGREEMENT, 200",
"ECC_NIST_P256, ENCRYPT_DECRYPT, 200",
"ECC_NIST_P256, SIGN_VERIFY, 200",
"ECC_NIST_P256, GENERATE_VERIFY_MAC, 400",
"ECC_NIST_P256, KEY_AGREEMENT, 200",
"ECC_NIST_P384, ENCRYPT_DECRYPT, 200",
"ECC_NIST_P384, SIGN_VERIFY, 200",
"ECC_NIST_P384, GENERATE_VERIFY_MAC, 400",
"ECC_NIST_P384, KEY_AGREEMENT, 200",
"ECC_NIST_P521, ENCRYPT_DECRYPT, 200",
"ECC_NIST_P521, SIGN_VERIFY, 200",
"ECC_NIST_P521, GENERATE_VERIFY_MAC, 400",
"ECC_NIST_P521, KEY_AGREEMENT, 200",
"ECC_NIST_EDWARDS25519, ENCRYPT_DECRYPT, 200",
"ECC_NIST_EDWARDS25519, SIGN_VERIFY, 200",
"ECC_NIST_EDWARDS25519, GENERATE_VERIFY_MAC, 400",
"ECC_NIST_EDWARDS25519, KEY_AGREEMENT, 200",
"ECC_SECG_P256K1, ENCRYPT_DECRYPT, 200",
"ECC_SECG_P256K1, SIGN_VERIFY, 200",
"ECC_SECG_P256K1, GENERATE_VERIFY_MAC, 400",
"ECC_SECG_P256K1, KEY_AGREEMENT, 200",
"HMAC_224, ENCRYPT_DECRYPT, 400",
"HMAC_224, SIGN_VERIFY, 400",
"HMAC_224, GENERATE_VERIFY_MAC, 200",
"HMAC_224, KEY_AGREEMENT, 400",
"HMAC_256, ENCRYPT_DECRYPT, 400",
"HMAC_256, SIGN_VERIFY, 400",
"HMAC_256, GENERATE_VERIFY_MAC, 200",
"HMAC_256, KEY_AGREEMENT, 400",
"HMAC_384, ENCRYPT_DECRYPT, 400",
"HMAC_384, SIGN_VERIFY, 400",
"HMAC_384, GENERATE_VERIFY_MAC, 200",
"HMAC_384, KEY_AGREEMENT, 400",
"HMAC_512, ENCRYPT_DECRYPT, 400",
"HMAC_512, SIGN_VERIFY, 400",
"HMAC_512, GENERATE_VERIFY_MAC, 200",
"HMAC_512, KEY_AGREEMENT, 400",
"SM2, ENCRYPT_DECRYPT, 400", // Not implemented
"SM2, SIGN_VERIFY, 400",
"SM2, GENERATE_VERIFY_MAC, 400",
"SM2, KEY_AGREEMENT, 400",
"ML_DSA_44, ENCRYPT_DECRYPT, 400", // Not implemented
"ML_DSA_44, SIGN_VERIFY, 400",
"ML_DSA_44, GENERATE_VERIFY_MAC, 400",
"ML_DSA_44, KEY_AGREEMENT, 400",
"ML_DSA_65, ENCRYPT_DECRYPT, 400",
"ML_DSA_65, SIGN_VERIFY, 400",
"ML_DSA_65, GENERATE_VERIFY_MAC, 400",
"ML_DSA_65, KEY_AGREEMENT, 400",
"ML_DSA_87, ENCRYPT_DECRYPT, 400",
"ML_DSA_87, SIGN_VERIFY, 400",
"ML_DSA_87, GENERATE_VERIFY_MAC, 400",
"ML_DSA_87, KEY_AGREEMENT, 400"
})
void createKeyWithCombinations(String keySpec, String keyUsage, int expectedStatusCode) {
given()
.header("X-Amz-Target", "TrentService.CreateKey")
.contentType(KMS_CONTENT_TYPE)
.body("""
{
"Description": "test key",
"KeyUsage": "%s",
"KeySpec": "%s"
}
""".formatted(keyUsage, keySpec))
.when()
.post("/")
.then()
.statusCode(expectedStatusCode);
}
}
@@ -7,6 +7,9 @@ import io.github.hectorvent.floci.core.storage.InMemoryStorage;
import io.github.hectorvent.floci.services.kms.model.KmsAlias;
import io.github.hectorvent.floci.services.kms.model.KmsGrant;
import io.github.hectorvent.floci.services.kms.model.KmsKey;
import io.github.hectorvent.floci.services.kms.model.KmsKeySpec;
import io.github.hectorvent.floci.services.kms.model.KmsKeyUsage;
import io.github.hectorvent.floci.services.kms.model.KmsMessageType;
import org.bouncycastle.jce.provider.BouncyCastleProvider;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.BeforeEach;
@@ -789,11 +792,11 @@ class KmsServiceTest {
byte[] digest = MessageDigest.getInstance("SHA-512").digest(message);
byte[] sig = kmsService.sign(key.getKeyId(), digest,
"RSASSA_PKCS1_V1_5_SHA_512", "DIGEST", REGION);
"RSASSA_PKCS1_V1_5_SHA_512", KmsMessageType.DIGEST, REGION);
// floci's own Verify round-trips.
assertTrue(kmsService.verify(key.getKeyId(), digest, sig,
"RSASSA_PKCS1_V1_5_SHA_512", "DIGEST", REGION));
"RSASSA_PKCS1_V1_5_SHA_512", KmsMessageType.DIGEST, REGION));
// External verifier (standard JCA, standing in for openssl/python) reconstructs
// DigestInfo from the message hash and must validate the DIGEST signature (#1345).
@@ -819,9 +822,9 @@ class KmsServiceTest {
// PKCS#1 v1.5 is deterministic, so signing the digest (DIGEST) and signing the
// message (RAW) with the same algorithm must produce identical signatures.
byte[] digestSig = kmsService.sign(key.getKeyId(), digest,
"RSASSA_PKCS1_V1_5_SHA_256", "DIGEST", REGION);
"RSASSA_PKCS1_V1_5_SHA_256", KmsMessageType.DIGEST, REGION);
byte[] rawSig = kmsService.sign(key.getKeyId(), message,
"RSASSA_PKCS1_V1_5_SHA_256", "RAW", REGION);
"RSASSA_PKCS1_V1_5_SHA_256", KmsMessageType.RAW, REGION);
assertArrayEquals(rawSig, digestSig);
}
@@ -1030,8 +1033,8 @@ class KmsServiceTest {
@Test
void enableKeyRotationOnAsymmetricKeyThrows() {
KmsKey key = kmsService.createKey(null, REGION);
key.setCustomerMasterKeySpec("RSA_2048");
key.setKeyUsage("SIGN_VERIFY");
key.setKeySpec(KmsKeySpec.RSA_2048);
key.setKeyUsage(KmsKeyUsage.SIGN_VERIFY);
assertThrows(AwsException.class, () ->
kmsService.enableKeyRotation(key.getKeyId(), REGION));
}
@@ -1039,16 +1042,16 @@ class KmsServiceTest {
@Test
void getKeyRotationStatusOnAsymmetricKeyReturnsFalse() {
KmsKey key = kmsService.createKey(null, REGION);
key.setCustomerMasterKeySpec("ECC_NIST_P256");
key.setKeyUsage("SIGN_VERIFY");
key.setKeySpec(KmsKeySpec.ECC_NIST_P256);
key.setKeyUsage(KmsKeyUsage.SIGN_VERIFY);
assertFalse(kmsService.getKeyRotationStatus(key.getKeyId(), REGION));
}
@Test
void getKeyRotationStatusOnHmacKeyReturnsFalse() {
KmsKey key = kmsService.createKey(null, REGION);
key.setCustomerMasterKeySpec("HMAC_256");
key.setKeyUsage("GENERATE_VERIFY_MAC");
key.setKeySpec(KmsKeySpec.HMAC_256);
key.setKeyUsage(KmsKeyUsage.GENERATE_VERIFY_MAC);
assertFalse(kmsService.getKeyRotationStatus(key.getKeyId(), REGION));
}
@@ -1105,8 +1108,8 @@ class KmsServiceTest {
void createHmacKey_allSpecs(String spec) {
KmsKey key = kmsService.createKey("hmac key", "GENERATE_VERIFY_MAC", spec, null, Map.of(), REGION);
assertEquals(spec, key.getCustomerMasterKeySpec());
assertEquals("GENERATE_VERIFY_MAC", key.getKeyUsage());
assertEquals(KmsKeySpec.valueOf(spec), key.getKeySpec());
assertEquals("GENERATE_VERIFY_MAC", key.getKeyUsage().name());
assertNotNull(key.getPrivateKeyEncoded());
int expectedBytes = switch (spec) {
@@ -1119,7 +1122,7 @@ class KmsServiceTest {
assertEquals(expectedBytes, Base64.getDecoder().decode(key.getPrivateKeyEncoded()).length);
KmsKey found = kmsService.describeKey(key.getKeyId(), REGION);
assertEquals(spec, found.getCustomerMasterKeySpec());
assertEquals(KmsKeySpec.valueOf(spec), found.getKeySpec());
}
@ParameterizedTest
@@ -1128,7 +1131,7 @@ class KmsServiceTest {
KmsKey key = kmsService.createKey("hmac key", "GENERATE_VERIFY_MAC", spec, null, Map.of(), REGION);
byte[] message = "floci-mac-probe".getBytes(StandardCharsets.UTF_8);
byte[] mac = kmsService.generateMac(key.getKeyId(), message, KmsService.macAlgorithmFor(spec), REGION);
byte[] mac = kmsService.generateMac(key.getKeyId(), message, KmsKeySpec.valueOf(spec).getAlgorithm().getFirst().getAlgName(), REGION);
assertEquals(expectedMacByteLength(spec), mac.length);
}
@@ -1245,4 +1248,56 @@ class KmsServiceTest {
default -> -1;
};
}
@ParameterizedTest
@ValueSource(strings = {"RSA_2048", "RSA_3072", "RSA_4096"})
void signAndVerifyWithAllRsaSpecs(String keySpec) {
KmsKey key = kmsService.createKey("rsa key", "SIGN_VERIFY", keySpec, null, Map.of(), REGION);
byte[] message = "sign me with rsa".getBytes(StandardCharsets.UTF_8);
String algo = "RSASSA_PKCS1_V1_5_SHA_256";
byte[] sig = kmsService.sign(key.getKeyId(), message, algo, REGION);
assertNotNull(sig);
assertTrue(kmsService.verify(key.getKeyId(), message, sig, algo, REGION));
}
@ParameterizedTest
@ValueSource(strings = {
"ECC_NIST_P256", "ECC_NIST_P384", "ECC_NIST_P521", "ECC_SECG_P256K1",
"RSA_2048", "RSA_3072", "RSA_4096"
})
void signAndVerifyWithDigestAllSpecs(String keySpec) throws Exception {
KmsKey key = kmsService.createKey("digest key", "SIGN_VERIFY", keySpec, null, Map.of(), REGION);
byte[] message = "floci kms digest test".getBytes(StandardCharsets.UTF_8);
KmsKeySpec.Algorithm algorithm = KmsKeySpec.valueOf(keySpec).getAlgorithm().getFirst();
String digestAlgo = algorithm.getJavaName().substring(0, 6);
byte[] digest = MessageDigest.getInstance(digestAlgo).digest(message);
byte[] sig = kmsService.sign(key.getKeyId(), digest, algorithm.getAlgName(), KmsMessageType.DIGEST, REGION);
assertNotNull(sig);
assertTrue(kmsService.verify(key.getKeyId(), digest, sig, algorithm.getAlgName(), KmsMessageType.DIGEST, REGION));
}
@Test
void signFailsForSymmetricKey() {
KmsKey key = kmsService.createKey("symmetric", "ENCRYPT_DECRYPT", "SYMMETRIC_DEFAULT", null, Map.of(), REGION);
byte[] message = "hello".getBytes(StandardCharsets.UTF_8);
String keyId = key.getKeyId();
AwsException ex = assertThrows(AwsException.class, () ->
kmsService.sign(keyId, message, "RSASSA_PKCS1_V1_5_SHA_256", REGION));
assertEquals("UnsupportedOperationException", ex.getErrorCode());
}
@Test
void generateMacFailsForAsymmetricKey() {
KmsKey key = kmsService.createKey("asymmetric", "SIGN_VERIFY", "RSA_2048", null, Map.of(), REGION);
byte[] message = "hello".getBytes(StandardCharsets.UTF_8);
String keyId = key.getKeyId();
AwsException ex = assertThrows(AwsException.class, () ->
kmsService.generateMac(keyId, message, "HMAC_SHA_256", REGION));
assertEquals("InvalidKeyUsageException", ex.getErrorCode());
}
}