fix(kms): reject mismatched keys in decryption requests (#1893)

* fix(kms): reject mismatched keys in decryption requests

* fix(kms): reject mismatched keys in reencryption requests

* fix(kms): verify if key is usable for cryptographic operations
This commit is contained in:
Maciej authored and GitHub committed 2026-08-10 23:49:31 -04:00
1 parent 15d6ce8429
commit 0cfffbea00
5 files changed
+632 -10

No files matched your search

@@ -261,7 +261,9 @@ public class KmsJsonHandler {
private Response handleDecrypt(JsonNode request, String region) {
byte[] ciphertext = decodeBlob(request, "CiphertextBlob");
Map<String, String> context = readEncryptionContext(request.path("EncryptionContext"));
KmsService.DecryptResult result = service.decryptAndResolveKey(ciphertext, context, region);
String requestKeyId = request.path("KeyId").asText(null);
KmsService.DecryptResult result = service.decryptAndResolveKey(ciphertext, context, region, requestKeyId);
ObjectNode response = objectMapper.createObjectNode();
response.put("Plaintext", Base64.getEncoder().encodeToString(result.plaintext()));
@@ -306,7 +308,8 @@ public class KmsJsonHandler {
Map<String, String> sourceContext = readEncryptionContext(request.path("SourceEncryptionContext"));
Map<String, String> destContext = readEncryptionContext(request.path("DestinationEncryptionContext"));
KmsService.DecryptResult source = service.decryptAndResolveKey(ciphertext, sourceContext, region);
String sourceKeyId = request.path("SourceKeyId").asText(null);
KmsService.DecryptResult source = service.decryptAndResolveKey(ciphertext, sourceContext, region, sourceKeyId);
byte[] newCiphertext = service.encrypt(destKeyId, source.plaintext(), destContext, region);
ObjectNode response = objectMapper.createObjectNode();
@@ -649,6 +649,7 @@ public class KmsService {
public byte[] encrypt(String keyId, byte[] plaintext, Map<String, String> encryptionContext, String region) {
KmsKey kmsKey = resolveKey(keyId, region);
validateKeyIsUsableForCryptoOperations(kmsKey);
byte[] nonceBytes = new byte[NONCE_BYTES];
SECURE_RANDOM.nextBytes(nonceBytes);
@@ -686,19 +687,43 @@ public class KmsService {
* Single-pass decrypt + source-key-ARN resolution. {@link #decrypt} and
* {@link #decryptToKeyArn} remain independent primitives — neither delegates here.
*/
public DecryptResult decryptAndResolveKey(byte[] ciphertext, Map<String, String> encryptionContext, String region) {
public DecryptResult decryptAndResolveKey(
byte[] ciphertext,
Map<String, String> encryptionContext,
String region,
String requestKeyId
) {
ParsedBlob parsed = parseBlob(ciphertext);
if (!parsed.contextFingerprint.equals(contextFingerprint(encryptionContext))) {
throw new AwsException("InvalidCiphertextException", "The ciphertext is invalid.", 400);
}
byte[] plaintext = Base64.getDecoder().decode(parsed.payload);
String keyArn;
try {
keyArn = resolveKey(parsed.keyId, region).getArn();
} catch (AwsException e) {
keyArn = null;
if (requestKeyId != null && !requestKeyId.isBlank()) {
KmsKey requestKey = resolveKey(requestKeyId, region);
if (!requestKey.getKeyId().equals(parsed.keyId)) {
throw new AwsException(
"IncorrectKeyException",
"The request was rejected because the specified KMS key cannot decrypt the data.",
400
);
}
validateKeyIsUsableForCryptoOperations(requestKey);
return new DecryptResult(plaintext, requestKey.getArn());
}
return new DecryptResult(plaintext, keyArn);
KmsKey key;
try {
key = resolveKey(parsed.keyId, region);
} catch (AwsException e) {
key = null;
}
if (key == null) {
return new DecryptResult(plaintext, null);
}
validateKeyIsUsableForCryptoOperations(key);
return new DecryptResult(plaintext, key.getArn());
}
public record DecryptResult(byte[] plaintext, String keyArn) {}
@@ -1091,4 +1116,22 @@ public class KmsService {
return keyStore.get(region + "::" + id)
.orElseThrow(() -> new AwsException("NotFoundException", "Key not found: " + keyIdOrArn, 404));
}
private static void validateKeyIsUsableForCryptoOperations(KmsKey key) {
if ("PendingDeletion".equals(key.getKeyState())) {
throw new AwsException(
"KMSInvalidStateException",
"KMS key " + key.getKeyId() + " is pending deletion.",
400
);
}
if (!key.isEnabled()) {
throw new AwsException(
"DisabledException",
"The request was rejected because the specified KMS key is not enabled.",
400
);
}
}
}