mirror of
https://github.com/floci-io/floci.git
synced 2026-11-27 07:39:24 -05:00
feat(kms): add GenerateDataKeyPair and GenerateDataKeyPairWithoutPlaintext (#6359)
This commit is contained in:
1 parent
be7b0ed62b
commit
5116b3c578
5 files changed
+279
-10
No files matched your search
@@ -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()) {
|
||||
|
||||
@@ -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) {
|
||||
|
||||
Reference in new issue
Block a user