Light, fluffy, and always free - The AWS Local Emulator alternative
feat(kms): add GenerateDataKeyPair and GenerateDataKeyPairWithoutPlaintext (#6359)
Renald Dubus5116b3c578f0
diff --git a/docs/services/index.md b/docs/services/index.mdindex 2f00bae0aa..6dd89b4346 100644--- a/docs/services/index.md+++ b/docs/services/index.md@@ -30,7 +30,7 @@ Operation counts are exact. For dispatch-table services (Query and JSON 1.1) eac| [Cognito](cognito.md) | `POST /` + `X-Amz-Target: AWSCognitoIdentityProviderService.*` | JSON 1.1 | 90 || [Cognito Identity](cognitoidentity.md) | `POST /` + `X-Amz-Target: AWSCognitoIdentityService.*` | JSON 1.1 | 12 || [Global Accelerator](globalaccelerator.md) | `POST /` + `X-Amz-Target: GlobalAccelerator_V20180706.*` | JSON 1.1 | 22 |-| [KMS](kms.md) | `POST /` + `X-Amz-Target: TrentService.*` | JSON 1.1 | 42 |+| [KMS](kms.md) | `POST /` + `X-Amz-Target: TrentService.*` | JSON 1.1 | 44 || [CloudHSM v2](cloudhsmv2.md) | `POST /` + `X-Amz-Target: BaldrApiService.*` | JSON 1.1 | 18 || [Kinesis](kinesis.md) | `POST /` + `X-Amz-Target: Kinesis_20131202.*` | JSON 1.1 | 29 || [Managed Service for Apache Flink](kinesisanalytics.md) | `POST /` + `X-Amz-Target: KinesisAnalytics_20180523.*` | JSON 1.1 | 15 |diff --git a/docs/services/kms.md b/docs/services/kms.mdindex d937284355..b7dd9e13b5 100644--- a/docs/services/kms.md+++ b/docs/services/kms.md@@ -24,6 +24,8 @@| `ReEncrypt` | Re-encrypt under a different key || `GenerateDataKey` | Generate a data key (plaintext + encrypted) || `GenerateDataKeyWithoutPlaintext` | Generate only the encrypted data key |+| `GenerateDataKeyPair` | Generate an asymmetric data key pair (plaintext + encrypted private key, public key) |+| `GenerateDataKeyPairWithoutPlaintext` | Generate an asymmetric data key pair (encrypted private key, public key) || `Sign` | Sign a message with an asymmetric key || `Verify` | Verify a signature || `GenerateMac` | Generate a MAC with an HMAC key |diff --git a/src/main/java/io/github/hectorvent/floci/services/kms/KmsJsonHandler.java b/src/main/java/io/github/hectorvent/floci/services/kms/KmsJsonHandler.javaindex 8c2bd5a0a9..3f37038582 100644--- a/src/main/java/io/github/hectorvent/floci/services/kms/KmsJsonHandler.java+++ b/src/main/java/io/github/hectorvent/floci/services/kms/KmsJsonHandler.java@@ -39,7 +39,7 @@ public class KmsJsonHandler {private static final Set<String> KEY_ID_OPERATIONS = Set.of("GetPublicKey", "DescribeKey", "ReplicateKey", "CreateGrant", "ListGrants", "RevokeGrant", "Encrypt", "GenerateDataKey",- "GenerateDataKeyWithoutPlaintext", "Sign", "Verify", "GenerateMac", "VerifyMac", "ScheduleKeyDeletion",+ "GenerateDataKeyWithoutPlaintext", "GenerateDataKeyPair", "GenerateDataKeyPairWithoutPlaintext", "Sign", "Verify", "GenerateMac", "VerifyMac", "ScheduleKeyDeletion","CancelKeyDeletion", "TagResource", "UntagResource", "ListResourceTags", "GetKeyPolicy", "PutKeyPolicy","ListKeyPolicies", "UpdateKeyDescription", "GetKeyRotationStatus", "EnableKeyRotation","DisableKeyRotation", "EnableKey", "DisableKey", "RotateKeyOnDemand", "GetParametersForImport",@@ -81,6 +81,8 @@ public class KmsJsonHandler {case "ReEncrypt" -> handleReEncrypt(request, region);case "GenerateDataKey" -> handleGenerateDataKey(request, region);case "GenerateDataKeyWithoutPlaintext" -> handleGenerateDataKeyWithoutPlaintext(request, region);+ case "GenerateDataKeyPair" -> handleGenerateDataKeyPair(request, region);+ case "GenerateDataKeyPairWithoutPlaintext" -> handleGenerateDataKeyPairWithoutPlaintext(request, region);case "Sign" -> handleSign(request, region);case "Verify" -> handleVerify(request, region);case "GenerateMac" -> handleGenerateMac(request, region);@@ -194,10 +196,7 @@ public class KmsJsonHandler {}if (!node.isTextual()) {// MarkerType is a string; any other JSON value is refused rather than coerced.- String token = node.isNumber() ? "NUMBER_VALUE"- : node.isBoolean() ? (node.booleanValue() ? "TRUE_VALUE" : "FALSE_VALUE")- : node.isArray() ? "START_ARRAY" : "START_OBJECT";- throw new AwsException("SerializationException", token + " can not be converted to a String", 400);+ throw new AwsException("SerializationException", jsonToken(node) + " can not be converted to a String", 400);}String marker = node.textValue();if (marker.isEmpty()) {@@ -402,6 +401,37 @@ public class KmsJsonHandler {return Response.ok(response).build();}+ private Response handleGenerateDataKeyPair(JsonNode request, String region) {+ String keyId = request.path("KeyId").asText();+ String spec = readDataKeyPairSpec(request);+ Map<String, String> context = readStrictEncryptionContext(request.path("EncryptionContext"));++ KmsService.DataKeyPair result = service.generateDataKeyPair(keyId, spec, context, region);++ ObjectNode response = dataKeyPairResponse(result);+ response.put("PrivateKeyPlaintext", Base64.getEncoder().encodeToString(result.privateKeyPlaintext()));+ return Response.ok(response).build();+ }++ private Response handleGenerateDataKeyPairWithoutPlaintext(JsonNode request, String region) {+ String keyId = request.path("KeyId").asText();+ String spec = readDataKeyPairSpec(request);+ Map<String, String> context = readStrictEncryptionContext(request.path("EncryptionContext"));++ KmsService.DataKeyPair result = service.generateDataKeyPairWithoutPlaintext(keyId, spec, context, region);++ return Response.ok(dataKeyPairResponse(result)).build();+ }++ private ObjectNode dataKeyPairResponse(KmsService.DataKeyPair result) {+ ObjectNode response = objectMapper.createObjectNode();+ response.put("PrivateKeyCiphertextBlob", Base64.getEncoder().encodeToString(result.privateKeyCiphertextBlob()));+ response.put("PublicKey", Base64.getEncoder().encodeToString(result.publicKey()));+ response.put("KeyId", result.keyArn());+ response.put("KeyPairSpec", result.keyPairSpec());+ return response;+ }+private Response handleReEncrypt(JsonNode request, String region) {byte[] ciphertext = requireSizedBlob(request, "CiphertextBlob", false, KmsService.MAX_CIPHERTEXT_BYTES);String destKeyId = request.path("DestinationKeyId").asText();@@ -434,6 +464,37 @@ public class KmsJsonHandler {return result;}+ /**+ * EncryptionContextType is a map of string to string, so any other JSON value is refused rather+ * than read as no context: a private key encrypted without the caller's context would decrypt+ * without it too.+ */+ private static Map<String, String> readStrictEncryptionContext(JsonNode node) {+ if (node.isMissingNode() || node.isNull()) {+ return Map.of();+ }+ if (!node.isObject()) {+ throw new AwsException("SerializationException", jsonToken(node) + " can not be converted to a Map", 400);+ }+ Map<String, String> result = new HashMap<>();+ node.fields().forEachRemaining(e -> {+ if (!e.getValue().isTextual()) {+ throw new AwsException("SerializationException",+ jsonToken(e.getValue()) + " can not be converted to a String", 400);+ }+ result.put(e.getKey(), e.getValue().textValue());+ });+ return result;+ }++ private static String jsonToken(JsonNode node) {+ return node.isTextual() ? "STRING_VALUE"+ : node.isNumber() ? "NUMBER_VALUE"+ : node.isBoolean() ? (node.booleanValue() ? "TRUE_VALUE" : "FALSE_VALUE")+ : node.isNull() ? "VALUE_NULL"+ : node.isArray() ? "START_ARRAY" : "START_OBJECT";+ }+private Response handleSign(JsonNode request, String region) {String keyId = request.path("KeyId").asText();byte[] message = requireSizedBlob(request, "Message", true, KmsService.MAX_PLAINTEXT_BYTES);@@ -728,6 +789,22 @@ public class KmsJsonHandler {return spec;}+ private static final List<String> DATA_KEY_PAIR_SPECS = List.of("RSA_2048", "RSA_3072", "RSA_4096",+ "ECC_NIST_P256", "ECC_NIST_P384", "ECC_NIST_P521", "ECC_SECG_P256K1", "SM2", "ECC_NIST_EDWARDS25519");++ private static String readDataKeyPairSpec(JsonNode request) {+ String spec = request.path("KeyPairSpec").asText(null);+ if (spec == null) {+ throw SmithyValidationErrors.validationException(SmithyValidationErrors.notNull("keyPairSpec"));+ }+ if (!DATA_KEY_PAIR_SPECS.contains(spec)) {+ throw SmithyValidationErrors.validationException("Value '" + spec+ + "' at 'keyPairSpec' failed to satisfy constraint: Member must satisfy enum value set: "+ + DATA_KEY_PAIR_SPECS.toString());+ }+ return spec;+ }+private static Integer readNumberOfBytes(JsonNode request) {JsonNode numberOfBytes = request.path("NumberOfBytes");if (!numberOfBytes.isNumber()) {diff --git a/src/main/java/io/github/hectorvent/floci/services/kms/KmsService.java b/src/main/java/io/github/hectorvent/floci/services/kms/KmsService.javaindex a35b616446..18448b86bc 100644--- a/src/main/java/io/github/hectorvent/floci/services/kms/KmsService.java+++ b/src/main/java/io/github/hectorvent/floci/services/kms/KmsService.java@@ -1668,6 +1668,10 @@ public class KmsService implements ResourceProvider {public record GenerateMacResult(byte[] mac, String keyArn) {}+ /** A data key pair: PKCS#8 private key, its ciphertext under the KMS key, and the X.509 public key. */+ public record DataKeyPair(byte[] privateKeyPlaintext, byte[] privateKeyCiphertextBlob, byte[] publicKey,+ String keyArn, String keyPairSpec) {}+public record VerifyMacResult(String keyArn) {}/**@@ -2132,6 +2136,40 @@ public class KmsService implements ResourceProvider {return result;}+ public DataKeyPair generateDataKeyPair(String keyId, String keyPairSpec, Map<String, String> encryptionContext,+ String region) {+ return generateDataKeyPair(keyId, keyPairSpec, encryptionContext, region, "GenerateDataKeyPair");+ }++ DataKeyPair generateDataKeyPairWithoutPlaintext(String keyId, String keyPairSpec,+ Map<String, String> encryptionContext, String region) {+ DataKeyPair pair = generateDataKeyPair(keyId, keyPairSpec, encryptionContext, region,+ "GenerateDataKeyPairWithoutPlaintext");+ return new DataKeyPair(null, pair.privateKeyCiphertextBlob(), pair.publicKey(), pair.keyArn(),+ pair.keyPairSpec());+ }++ // The pair is generated with the same code that creates asymmetric KMS keys of that spec, but is+ // never stored: only its private key, encrypted under the symmetric KMS key, leaves this method.+ private DataKeyPair generateDataKeyPair(String keyId, String keyPairSpec, Map<String, String> encryptionContext,+ String region, String operation) {+ KmsKey key = resolveKey(keyId, region);+ validateKeyUsage(key, KmsKeyUsage.ENCRYPT_DECRYPT, operation);+ validateKeyIsUsableForCryptoOperations(key);+ if (KmsKeySpec.SYMMETRIC_DEFAULT != key.getKeySpec()) {+ throw new AwsException("InvalidKeyUsageException", "You cannot generate a data key pair with an asymmetric CMK", 400);+ }++ KmsKey pair = new KmsKey();+ pair.setKeySpec(KmsKeySpec.valueOf(keyPairSpec));+ generateKeyMaterial(pair, region);+ byte[] privateKey = Base64.getDecoder().decode(pair.getPrivateKeyEncoded());+ byte[] publicKey = Base64.getDecoder().decode(pair.getPublicKeyEncoded());++ EncryptResult encrypted = encrypt(keyId, privateKey, encryptionContext, null, region, operation);+ return new DataKeyPair(privateKey, encrypted.ciphertext(), publicKey, encrypted.keyArn(), keyPairSpec);+ }+// ──────────────────────────── Tags ────────────────────────────public void tagResource(String keyId, Map<String, String> tags, String region) {diff --git a/src/test/java/io/github/hectorvent/floci/services/kms/KmsIntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/kms/KmsIntegrationTest.javaindex 0c81e7748b..4be44583f3 100644--- a/src/test/java/io/github/hectorvent/floci/services/kms/KmsIntegrationTest.java+++ b/src/test/java/io/github/hectorvent/floci/services/kms/KmsIntegrationTest.java@@ -18,15 +18,19 @@ import java.nio.charset.StandardCharsets;import java.security.KeyFactory;import java.security.KeyPair;import java.security.KeyPairGenerator;+import java.security.PrivateKey;import java.security.PublicKey;+import java.security.Signature;import java.security.spec.ECGenParameterSpec;import java.security.spec.MGF1ParameterSpec;+import java.security.spec.PKCS8EncodedKeySpec;import java.security.spec.X509EncodedKeySpec;import java.util.ArrayList;import java.util.Arrays;import java.util.Base64;import java.util.HashSet;import java.util.List;+import java.util.UUID;import static io.restassured.RestAssured.given;import static org.hamcrest.Matchers.emptyString;@@ -1160,9 +1164,17 @@ class KmsIntegrationTest {@ParameterizedTest@CsvSource(delimiter = '|', value = {- "ListKeys|{}", "ListKeys|[]", "ListKeys|1", "ListKeys|true",- "ListAliases|{}", "ListAliases|[]", "ListAliases|1", "ListAliases|true"})- void listKeysAndAliasesRejectNonStringMarker(String action, String marker) {+ "ListKeys|{}|START_OBJECT",+ "ListKeys|[]|START_ARRAY",+ "ListKeys|1|NUMBER_VALUE",+ "ListKeys|true|TRUE_VALUE",+ "ListKeys|false|FALSE_VALUE",+ "ListAliases|{}|START_OBJECT",+ "ListAliases|[]|START_ARRAY",+ "ListAliases|1|NUMBER_VALUE",+ "ListAliases|true|TRUE_VALUE",+ "ListAliases|false|FALSE_VALUE"})+ void listKeysAndAliasesRejectNonStringMarker(String action, String marker, String token) {given().header("X-Amz-Target", "TrentService." + action).contentType(KMS_CONTENT_TYPE)@@ -1170,7 +1182,8 @@ class KmsIntegrationTest {.when().post("/").then().statusCode(400)- .body("__type", equalTo("SerializationException"));+ .body("__type", equalTo("SerializationException"))+ .body("message", equalTo(token + " can not be converted to a String"));}@ParameterizedTest@@ -2503,6 +2516,141 @@ class KmsIntegrationTest {.body("message", equalTo("Please specify either number of bytes or key spec."));}+ @ParameterizedTest+ @CsvSource({"RSA_2048", "RSA_4096", "ECC_NIST_P256", "ECC_NIST_P521", "ECC_SECG_P256K1", "ECC_NIST_EDWARDS25519"})+ void generateDataKeyPairEncryptsItsPrivateKeyUnderTheKmsKey(String keyPairSpec) {+ String keyArn = createKeyArn("SYMMETRIC_DEFAULT", "ENCRYPT_DECRYPT");++ JsonPath pair = callKms("GenerateDataKeyPair",+ "{\"KeyId\":\"%s\",\"KeyPairSpec\":\"%s\",\"EncryptionContext\":{\"purpose\":\"test\"}}"+ .formatted(keyArn, keyPairSpec))+ .then()+ .statusCode(200)+ .body("KeyId", equalTo(keyArn))+ .body("KeyPairSpec", equalTo(keyPairSpec))+ .body("PublicKey", notNullValue())+ .extract().jsonPath();++ String plaintext = callKms("Decrypt",+ "{\"CiphertextBlob\":\"%s\",\"EncryptionContext\":{\"purpose\":\"test\"}}"+ .formatted(pair.getString("PrivateKeyCiphertextBlob")))+ .then().statusCode(200).extract().path("Plaintext");+ assertEquals(pair.getString("PrivateKeyPlaintext"), plaintext);+ }++ @Test+ void generateDataKeyPairWithoutPlaintextReturnsAPublicKeyThatMatchesTheEncryptedPrivateKey() throws Exception {+ String keyArn = createKeyArn("SYMMETRIC_DEFAULT", "ENCRYPT_DECRYPT");+ String alias = "alias/data-key-pair-" + UUID.randomUUID();+ callKms("CreateAlias", "{\"AliasName\":\"%s\",\"TargetKeyId\":\"%s\"}".formatted(alias, keyArn))+ .then().statusCode(200);++ JsonPath pair = callKms("GenerateDataKeyPairWithoutPlaintext",+ "{\"KeyId\":\"%s\",\"KeyPairSpec\":\"ECC_NIST_P256\",\"EncryptionContext\":{\"kid\":\"1\"}}".formatted(alias))+ .then()+ .statusCode(200)+ .body("KeyId", equalTo(keyArn))+ .body("KeyPairSpec", equalTo("ECC_NIST_P256"))+ .body("$", not(hasKey("PrivateKeyPlaintext")))+ .extract().jsonPath();+ String privateKey = callKms("Decrypt",+ "{\"CiphertextBlob\":\"%s\",\"EncryptionContext\":{\"kid\":\"1\"}}"+ .formatted(pair.getString("PrivateKeyCiphertextBlob")))+ .then().statusCode(200).extract().path("Plaintext");++ KeyFactory keyFactory = KeyFactory.getInstance("EC");+ PrivateKey signingKey = keyFactory.generatePrivate(new PKCS8EncodedKeySpec(Base64.getDecoder().decode(privateKey)));+ PublicKey verifyingKey = keyFactory.generatePublic(+ new X509EncodedKeySpec(Base64.getDecoder().decode(pair.getString("PublicKey"))));+ Signature signer = Signature.getInstance("SHA256withECDSA");+ signer.initSign(signingKey);+ signer.update("message".getBytes(StandardCharsets.UTF_8));+ byte[] signature = signer.sign();+ Signature verifier = Signature.getInstance("SHA256withECDSA");+ verifier.initVerify(verifyingKey);+ verifier.update("message".getBytes(StandardCharsets.UTF_8));+ assertTrue(verifier.verify(signature));+ }++ @Test+ void generateDataKeyPairBindsItsCiphertextToTheEncryptionContext() {+ String ciphertext = callKms("GenerateDataKeyPairWithoutPlaintext",+ "{\"KeyId\":\"%s\",\"KeyPairSpec\":\"ECC_NIST_P256\",\"EncryptionContext\":{\"kid\":\"1\"}}"+ .formatted(createKeyArn("SYMMETRIC_DEFAULT", "ENCRYPT_DECRYPT")))+ .then().statusCode(200).extract().path("PrivateKeyCiphertextBlob");++ callKms("Decrypt", "{\"CiphertextBlob\":\"%s\",\"EncryptionContext\":{\"kid\":\"2\"}}".formatted(ciphertext))+ .then()+ .statusCode(400)+ .body("__type", equalTo("InvalidCiphertextException"));+ }++ @ParameterizedTest+ @CsvSource(delimiter = '|', value = {+ "GenerateDataKeyPair||Value null at 'keyPairSpec' failed to satisfy constraint: Member must not be null",+ "GenerateDataKeyPairWithoutPlaintext||Value null at 'keyPairSpec' failed to satisfy constraint: Member must not be null",+ "GenerateDataKeyPair|,\"KeyPairSpec\":\"AES_256\"|Value 'AES_256' at 'keyPairSpec' failed to satisfy constraint: Member must satisfy enum value set: [RSA_2048, RSA_3072, RSA_4096, ECC_NIST_P256, ECC_NIST_P384, ECC_NIST_P521, ECC_SECG_P256K1, SM2, ECC_NIST_EDWARDS25519]",+ "GenerateDataKeyPairWithoutPlaintext|,\"KeyPairSpec\":\"HMAC_256\"|Value 'HMAC_256' at 'keyPairSpec' failed to satisfy constraint: Member must satisfy enum value set: [RSA_2048, RSA_3072, RSA_4096, ECC_NIST_P256, ECC_NIST_P384, ECC_NIST_P521, ECC_SECG_P256K1, SM2, ECC_NIST_EDWARDS25519]",+ })+ void generateDataKeyPairValidatesItsKeyPairSpecBeforeLookingUpTheKey(String operation, String member, String error) {+ List<String> keysBefore = listKeyIds();++ callKms(operation, "{\"KeyId\":\"00000000-0000-0000-0000-000000000000\"%s}".formatted(member == null ? "" : member))+ .then()+ .statusCode(400)+ .body("__type", equalTo("ValidationException"))+ .body("message", equalTo("1 validation error detected: " + error));++ assertEquals(keysBefore, listKeyIds());+ }++ @ParameterizedTest+ @CsvSource(delimiter = '|', value = {+ "GenerateDataKeyPair|\"kid-1\"|STRING_VALUE can not be converted to a Map",+ "GenerateDataKeyPair|[\"kid\",\"1\"]|START_ARRAY can not be converted to a Map",+ "GenerateDataKeyPairWithoutPlaintext|1|NUMBER_VALUE can not be converted to a Map",+ "GenerateDataKeyPairWithoutPlaintext|true|TRUE_VALUE can not be converted to a Map",+ "GenerateDataKeyPair|{\"kid\":1}|NUMBER_VALUE can not be converted to a String",+ "GenerateDataKeyPairWithoutPlaintext|{\"kid\":{\"nested\":\"1\"}}|START_OBJECT can not be converted to a String",+ })+ void generateDataKeyPairRejectsAnEncryptionContextThatIsNotAStringMap(String operation, String context,+ String error) {+ String keyArn = createKeyArn("SYMMETRIC_DEFAULT", "ENCRYPT_DECRYPT");+ List<String> keysBefore = listKeyIds();++ callKms(operation, "{\"KeyId\":\"%s\",\"KeyPairSpec\":\"ECC_NIST_P256\",\"EncryptionContext\":%s}"+ .formatted(keyArn, context))+ .then()+ .statusCode(400)+ .body("__type", equalTo("SerializationException"))+ .body("message", equalTo(error));++ assertEquals(keysBefore, listKeyIds());+ }++ @ParameterizedTest+ @CsvSource({"GenerateDataKeyPair", "GenerateDataKeyPairWithoutPlaintext"})+ void generateDataKeyPairRejectsAnAsymmetricKey(String operation) {+ callKms(operation, "{\"KeyId\":\"%s\",\"KeyPairSpec\":\"ECC_NIST_P256\"}"+ .formatted(createKeyArn("RSA_2048", "ENCRYPT_DECRYPT")))+ .then()+ .statusCode(400)+ .body("__type", equalTo("InvalidKeyUsageException"))+ .body("message", equalTo("You cannot generate a data key pair with an asymmetric CMK"));+ }++ @ParameterizedTest+ @CsvSource({"GenerateDataKeyPair", "GenerateDataKeyPairWithoutPlaintext"})+ void generateDataKeyPairNamesItsOperationInTheKeyUsageError(String operation) {+ String keyArn = createKeyArn("ECC_NIST_P256", "SIGN_VERIFY");++ callKms(operation, "{\"KeyId\":\"%s\",\"KeyPairSpec\":\"ECC_NIST_P256\"}".formatted(keyArn))+ .then()+ .statusCode(400)+ .body("__type", equalTo("InvalidKeyUsageException"))+ .body("message", equalTo(keyArn + " key usage is SIGN_VERIFY which is not valid for " + operation + "."));+ }+@ParameterizedTest@CsvSource({"Sign", "Verify"})void messageTypeValidationListsTheEnumBeforeLookingUpTheKey(String operation) {@@ -3125,6 +3273,10 @@ class KmsIntegrationTest {.when().post("/");}+ private static List<String> listKeyIds() {+ return callKms("ListKeys", "{}").then().statusCode(200).extract().jsonPath().getList("Keys.KeyId");+ }+private static String createKeyArn(String keySpec, String keyUsage) {return given().header("X-Amz-Target", "TrentService.CreateKey")