mirror of
https://github.com/floci-io/floci.git
synced 2026-11-24 06:20:24 -05:00
feat(kms): support ImportKeyMaterial for Origin=EXTERNAL keys (#3068)
CreateKey accepts Origin=EXTERNAL, GetParametersForImport issues a real RSA wrapping key, and ImportKeyMaterial unwraps and installs the material. Expiry and DeleteImportedKeyMaterial return the key to PendingImport. Fixes #1916 Signed-off-by: Matheus Oliveira <matheus.6148@gmail.com>
This commit is contained in:
1 parent
532e27986e
commit
37faaa8abe
10 files changed
+1415
-22
No files matched your search
@@ -76,6 +76,9 @@ public class KmsJsonHandler {
|
||||
case "EnableKey" -> handleEnableKey(request, region);
|
||||
case "DisableKey" -> handleDisableKey(request, region);
|
||||
case "RotateKeyOnDemand" -> handleRotateKeyOnDemand(request, region);
|
||||
case "GetParametersForImport" -> handleGetParametersForImport(request, region);
|
||||
case "ImportKeyMaterial" -> handleImportKeyMaterial(request, region);
|
||||
case "DeleteImportedKeyMaterial" -> handleDeleteImportedKeyMaterial(request, region);
|
||||
default -> Response.status(400)
|
||||
.entity(new AwsErrorResponse("UnsupportedOperation", "Operation " + action + " is not supported."))
|
||||
.build();
|
||||
@@ -92,7 +95,8 @@ public class KmsJsonHandler {
|
||||
Map<String, String> tags = new HashMap<>();
|
||||
request.path("Tags").forEach(t -> tags.put(t.path("TagKey").asText(), t.path("TagValue").asText()));
|
||||
rejectUnknownReservedTags(tags,"TagException");
|
||||
KmsKey key = service.createKey(description, keyUsage, keySpec, policy, tags, region);
|
||||
String origin = request.path("Origin").asText(null);
|
||||
KmsKey key = service.createKey(description, keyUsage, keySpec, policy, tags, origin, region);
|
||||
ObjectNode response = objectMapper.createObjectNode();
|
||||
response.set("KeyMetadata", addKeyMetadata(key));
|
||||
return Response.ok(response).build();
|
||||
@@ -101,16 +105,16 @@ public class KmsJsonHandler {
|
||||
private Response handleGetPublicKey(JsonNode request, String region) {
|
||||
String keyId = request.path("KeyId").asText();
|
||||
KmsKey key = service.getPublicKey(keyId, region);
|
||||
|
||||
|
||||
ObjectNode response = objectMapper.createObjectNode();
|
||||
response.put("KeyId", key.getArn());
|
||||
response.put("PublicKey", key.getPublicKeyEncoded());
|
||||
response.put("CustomerMasterKeySpec", key.getKeySpec().name());
|
||||
response.put("KeySpec", key.getKeySpec().name());
|
||||
response.put("KeyUsage", key.getKeyUsage().name());
|
||||
|
||||
|
||||
addAlgorithms(key, response);
|
||||
|
||||
|
||||
return Response.ok(response).build();
|
||||
}
|
||||
|
||||
@@ -595,6 +599,68 @@ public class KmsJsonHandler {
|
||||
return Response.ok(response).build();
|
||||
}
|
||||
|
||||
private Response handleGetParametersForImport(JsonNode request, String region) {
|
||||
String keyId = requireKeyId(request);
|
||||
KmsService.ImportParameters parameters = service.getParametersForImport(
|
||||
keyId,
|
||||
request.path("WrappingAlgorithm").asText(null),
|
||||
request.path("WrappingKeySpec").asText(null),
|
||||
region);
|
||||
|
||||
ObjectNode response = objectMapper.createObjectNode();
|
||||
response.put("KeyId", parameters.keyArn());
|
||||
response.put("PublicKey", parameters.publicKeyEncoded());
|
||||
response.put("ImportToken", parameters.importToken());
|
||||
response.put("ParametersValidTo", parameters.parametersValidTo());
|
||||
return Response.ok(response).build();
|
||||
}
|
||||
|
||||
private Response handleImportKeyMaterial(JsonNode request, String region) {
|
||||
String keyId = requireKeyId(request);
|
||||
String importToken = request.path("ImportToken").asText(null);
|
||||
byte[] encryptedKeyMaterial = requireBlob(request, "EncryptedKeyMaterial");
|
||||
Long validTo = request.hasNonNull("ValidTo") ? request.path("ValidTo").asLong() : null;
|
||||
|
||||
KmsKey key = service.importKeyMaterial(keyId, importToken, encryptedKeyMaterial,
|
||||
request.path("ExpirationModel").asText(null), validTo,
|
||||
request.path("ImportType").asText(null), region);
|
||||
|
||||
ObjectNode response = objectMapper.createObjectNode();
|
||||
response.put("KeyId", key.getArn());
|
||||
response.put("KeyMaterialId", key.getKeyMaterialId());
|
||||
return Response.ok(response).build();
|
||||
}
|
||||
|
||||
private Response handleDeleteImportedKeyMaterial(JsonNode request, String region) {
|
||||
KmsKey key = service.deleteImportedKeyMaterial(requireKeyId(request), region);
|
||||
ObjectNode response = objectMapper.createObjectNode();
|
||||
response.put("KeyId", key.getArn());
|
||||
if (key.getKeyMaterialId() != null) {
|
||||
response.put("KeyMaterialId", key.getKeyMaterialId());
|
||||
}
|
||||
return Response.ok(response).build();
|
||||
}
|
||||
|
||||
private static String requireKeyId(JsonNode request) {
|
||||
String keyId = request.path("KeyId").asText(null);
|
||||
if (keyId == null || keyId.isBlank()) {
|
||||
throw new AwsException("ValidationException", "KeyId is required.", 400);
|
||||
}
|
||||
return keyId;
|
||||
}
|
||||
|
||||
/**
|
||||
* A required blob member. {@link #decodeBlob} reads a missing member as an empty blob, which
|
||||
* would reach the operation as key material of the wrong length instead of the absent member
|
||||
* it actually is.
|
||||
*/
|
||||
private static byte[] requireBlob(JsonNode request, String member) {
|
||||
if (!request.path(member).isTextual()) {
|
||||
throw new AwsException("ValidationException", member + " is required.", 400);
|
||||
}
|
||||
return decodeBlob(request, member);
|
||||
}
|
||||
|
||||
private ObjectNode addKeyMetadata(KmsKey k) {
|
||||
ObjectNode keyMetadata = objectMapper.createObjectNode();
|
||||
keyMetadata.put("AWSAccountId", regionResolver.getAccountId());
|
||||
@@ -605,7 +671,13 @@ public class KmsJsonHandler {
|
||||
keyMetadata.put("Description", k.getDescription());
|
||||
keyMetadata.put("KeyUsage", k.getKeyUsage().name());
|
||||
keyMetadata.put("KeyState", k.getKeyState());
|
||||
keyMetadata.put("Origin", "AWS_KMS");
|
||||
keyMetadata.put("Origin", k.getOrigin());
|
||||
if (k.getExpirationModel() != null) {
|
||||
keyMetadata.put("ExpirationModel", k.getExpirationModel());
|
||||
}
|
||||
if (k.getValidTo() > 0) {
|
||||
keyMetadata.put("ValidTo", k.getValidTo());
|
||||
}
|
||||
keyMetadata.put("KeyManager", "CUSTOMER");
|
||||
keyMetadata.put("CustomerMasterKeySpec", k.getKeySpec().name());
|
||||
keyMetadata.put("KeySpec", k.getKeySpec().name());
|
||||
|
||||
@@ -0,0 +1,118 @@
|
||||
package io.github.hectorvent.floci.services.kms;
|
||||
|
||||
import io.github.hectorvent.floci.core.common.AwsException;
|
||||
import org.jboss.logging.Logger;
|
||||
|
||||
import javax.crypto.Cipher;
|
||||
import javax.crypto.spec.OAEPParameterSpec;
|
||||
import javax.crypto.spec.PSource;
|
||||
import java.security.KeyFactory;
|
||||
import java.security.KeyPair;
|
||||
import java.security.KeyPairGenerator;
|
||||
import java.security.NoSuchAlgorithmException;
|
||||
import java.security.PrivateKey;
|
||||
import java.security.spec.MGF1ParameterSpec;
|
||||
import java.security.spec.PKCS8EncodedKeySpec;
|
||||
import java.util.Base64;
|
||||
|
||||
/**
|
||||
* The cryptography behind the KMS import-key-material flow: the RSA wrapping key pair
|
||||
* GetParametersForImport hands out, and the unwrap ImportKeyMaterial performs on what comes
|
||||
* back.
|
||||
*
|
||||
* <p>Stateless on purpose. {@link KmsService} owns the import state machine, the token and the
|
||||
* store; this class only knows how to mint a wrapping key and how to open material wrapped
|
||||
* with one.
|
||||
*/
|
||||
final class KmsKeyImport {
|
||||
|
||||
private static final Logger LOG = Logger.getLogger(KmsKeyImport.class);
|
||||
|
||||
private static final String RSAES_PKCS1_V1_5 = "RSAES_PKCS1_V1_5";
|
||||
private static final String RSAES_OAEP_SHA_1 = "RSAES_OAEP_SHA_1";
|
||||
private static final String RSAES_OAEP_SHA_256 = "RSAES_OAEP_SHA_256";
|
||||
|
||||
private KmsKeyImport() {
|
||||
}
|
||||
|
||||
/**
|
||||
* An RSA wrapping key pair, both halves Base64-encoded. The public half goes to the caller
|
||||
* so it can wrap its key material; the private half stays on the KMS key so a later
|
||||
* ImportKeyMaterial can unwrap what the caller sends back.
|
||||
*/
|
||||
record WrappingKeyPair(String publicKeyEncoded, String privateKeyEncoded) {
|
||||
}
|
||||
|
||||
static WrappingKeyPair generateWrappingKeyPair(String wrappingKeySpec) {
|
||||
int keySize = switch (wrappingKeySpec == null ? "" : wrappingKeySpec) {
|
||||
case "RSA_2048" -> 2048;
|
||||
case "RSA_3072" -> 3072;
|
||||
case "RSA_4096" -> 4096;
|
||||
case "SM2" -> throw new AwsException("UnsupportedOperationException",
|
||||
"WrappingKeySpec SM2 is not supported.", 400);
|
||||
default -> throw new AwsException("ValidationException",
|
||||
"1 validation error detected: Value '" + wrappingKeySpec + "' at 'wrappingKeySpec' failed to "
|
||||
+ "satisfy constraint: Member must satisfy enum value set: "
|
||||
+ "[RSA_2048, RSA_3072, RSA_4096, SM2]", 400);
|
||||
};
|
||||
try {
|
||||
KeyPairGenerator generator = KeyPairGenerator.getInstance("RSA");
|
||||
generator.initialize(keySize);
|
||||
KeyPair pair = generator.generateKeyPair();
|
||||
return new WrappingKeyPair(
|
||||
Base64.getEncoder().encodeToString(pair.getPublic().getEncoded()),
|
||||
Base64.getEncoder().encodeToString(pair.getPrivate().getEncoded()));
|
||||
} catch (NoSuchAlgorithmException e) {
|
||||
throw new AwsException("InternalFailure", "Failed to generate wrapping key: " + e.getMessage(), 500);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Rejects a WrappingAlgorithm this emulator cannot honour before a key pair is generated for
|
||||
* it, so a caller never wraps material against parameters that ImportKeyMaterial would then
|
||||
* refuse.
|
||||
*
|
||||
* <p>RSAES_PKCS1_V1_5 stays in the modelled enum but AWS KMS stopped honouring it on
|
||||
* October 10, 2023. The RSA_AES variants exist for material longer than an RSA modulus can
|
||||
* hold; every spec that can be imported here carries at most 64 bytes.
|
||||
*/
|
||||
static void validateWrappingAlgorithm(String wrappingAlgorithm) {
|
||||
switch (wrappingAlgorithm == null ? "" : wrappingAlgorithm) {
|
||||
case RSAES_OAEP_SHA_1, RSAES_OAEP_SHA_256 -> { }
|
||||
case RSAES_PKCS1_V1_5 -> throw new AwsException("UnsupportedOperationException",
|
||||
"AWS KMS stopped supporting the RSAES_PKCS1_V1_5 wrapping algorithm on October 10, 2023. "
|
||||
+ "Use RSAES_OAEP_SHA_256 or RSAES_OAEP_SHA_1.", 400);
|
||||
case "RSA_AES_KEY_WRAP_SHA_1", "RSA_AES_KEY_WRAP_SHA_256", "SM2PKE" ->
|
||||
throw new AwsException("UnsupportedOperationException",
|
||||
"WrappingAlgorithm " + wrappingAlgorithm + " is not supported. Supported values are "
|
||||
+ "RSAES_OAEP_SHA_256 and RSAES_OAEP_SHA_1.", 400);
|
||||
default -> throw new AwsException("ValidationException",
|
||||
"1 validation error detected: Value '" + wrappingAlgorithm + "' at 'wrappingAlgorithm' failed "
|
||||
+ "to satisfy constraint: Member must satisfy enum value set: "
|
||||
+ "[RSAES_OAEP_SHA_256, RSAES_OAEP_SHA_1, RSA_AES_KEY_WRAP_SHA_256, "
|
||||
+ "RSA_AES_KEY_WRAP_SHA_1, SM2PKE, RSAES_PKCS1_V1_5]", 400);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Unwraps key material with the private half of the pair issued for this import.
|
||||
*
|
||||
* <p>Every failure answers InvalidCiphertextException, matching real KMS: whether the bytes
|
||||
* were garbage, wrapped for a different key, or wrapped with a different algorithm than the
|
||||
* one requested is not something the caller gets to distinguish.
|
||||
*/
|
||||
static byte[] unwrap(String wrappingPrivateKeyEncoded, String wrappingAlgorithm, byte[] encryptedKeyMaterial) {
|
||||
try {
|
||||
PrivateKey wrappingKey = KeyFactory.getInstance("RSA").generatePrivate(
|
||||
new PKCS8EncodedKeySpec(Base64.getDecoder().decode(wrappingPrivateKeyEncoded)));
|
||||
String digest = RSAES_OAEP_SHA_1.equals(wrappingAlgorithm) ? "SHA-1" : "SHA-256";
|
||||
Cipher cipher = Cipher.getInstance("RSA/ECB/OAEPPadding");
|
||||
cipher.init(Cipher.DECRYPT_MODE, wrappingKey, new OAEPParameterSpec(digest, "MGF1",
|
||||
new MGF1ParameterSpec(digest), PSource.PSpecified.DEFAULT));
|
||||
return cipher.doFinal(encryptedKeyMaterial);
|
||||
} catch (Exception e) {
|
||||
LOG.debugv(e, "Unwrapping imported key material failed for wrapping algorithm {0}", wrappingAlgorithm);
|
||||
throw new AwsException("InvalidCiphertextException", "The ciphertext is invalid.", 400);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -12,6 +12,7 @@ import io.github.hectorvent.floci.core.storage.StorageBackend;
|
||||
import io.github.hectorvent.floci.core.storage.StorageFactory;
|
||||
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.KmsImportParameters;
|
||||
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;
|
||||
@@ -71,6 +72,7 @@ import java.security.spec.MGF1ParameterSpec;
|
||||
import java.security.spec.PKCS8EncodedKeySpec;
|
||||
import java.security.spec.PSSParameterSpec;
|
||||
import java.security.spec.X509EncodedKeySpec;
|
||||
import java.time.Duration;
|
||||
import java.time.Instant;
|
||||
import java.util.*;
|
||||
import java.util.concurrent.ThreadLocalRandom;
|
||||
@@ -83,6 +85,16 @@ public class KmsService implements ResourceProvider {
|
||||
|
||||
private static final Logger LOG = Logger.getLogger(KmsService.class);
|
||||
|
||||
private static final String AWS_KMS_ORIGIN = "AWS_KMS";
|
||||
private static final String EXTERNAL_ORIGIN = "EXTERNAL";
|
||||
private static final String PENDING_IMPORT = "PendingImport";
|
||||
private static final String PENDING_DELETION = "PendingDeletion";
|
||||
private static final String KEY_MATERIAL_EXPIRES = "KEY_MATERIAL_EXPIRES";
|
||||
private static final String KEY_MATERIAL_DOES_NOT_EXPIRE = "KEY_MATERIAL_DOES_NOT_EXPIRE";
|
||||
private static final Duration IMPORT_PARAMETERS_VALIDITY = Duration.ofHours(24);
|
||||
private static final Duration MAX_KEY_MATERIAL_VALIDITY = Duration.ofDays(365);
|
||||
private static final int IMPORT_TOKEN_BYTES = 32;
|
||||
|
||||
private final StorageBackend<String, KmsKey> keyStore;
|
||||
private final StorageBackend<String, KmsAlias> aliasStore;
|
||||
private final StorageBackend<String, KmsGrant> grantStore;
|
||||
@@ -146,6 +158,11 @@ public class KmsService implements ResourceProvider {
|
||||
}
|
||||
|
||||
public KmsKey createKey(String description, String keyUsage, String keySpec, String policy, Map<String, String> tags, String region) {
|
||||
return createKey(description, keyUsage, keySpec, policy, tags, null, region);
|
||||
}
|
||||
|
||||
public KmsKey createKey(String description, String keyUsage, String keySpec, String policy,
|
||||
Map<String, String> tags, String origin, String region) {
|
||||
String keyId = resolveKeyId(tags);
|
||||
if (keyStore.get(region + "::" + keyId).isPresent()) {
|
||||
throw new AwsException("AlreadyExistsException", "Key already exists", 400);
|
||||
@@ -168,11 +185,18 @@ public class KmsService implements ResourceProvider {
|
||||
key.setKeySpec(effectiveSpec);
|
||||
key.setPolicy(policy != null ? policy : buildDefaultKeyPolicy());
|
||||
key.getTags().putAll(ReservedTags.stripReservedTags(tags));
|
||||
key.setOrigin(resolveOrigin(origin, effectiveSpec));
|
||||
|
||||
generateKeyMaterial(key);
|
||||
if (EXTERNAL_ORIGIN.equals(key.getOrigin())) {
|
||||
key.setKeyState(PENDING_IMPORT);
|
||||
key.setEnabled(false);
|
||||
} else {
|
||||
generateKeyMaterial(key);
|
||||
}
|
||||
|
||||
keyStore.put(region + "::" + keyId, key);
|
||||
LOG.infov("Created KMS key: {0} ({1}/{2}) in {3}", keyId, key.getKeyUsage(), key.getKeySpec(), region);
|
||||
LOG.infov("Created KMS key: {0} ({1}/{2}, origin {3}) in {4}",
|
||||
keyId, key.getKeyUsage(), key.getKeySpec(), key.getOrigin(), region);
|
||||
return key;
|
||||
}
|
||||
|
||||
@@ -268,14 +292,11 @@ public class KmsService implements ResourceProvider {
|
||||
}
|
||||
|
||||
private static int hmacKeyByteLength(KmsKeySpec spec) {
|
||||
return switch (spec) {
|
||||
case HMAC_224 -> 28;
|
||||
case HMAC_256 -> 32;
|
||||
case HMAC_384 -> 48;
|
||||
case HMAC_512 -> 64;
|
||||
default -> throw new AwsException("InvalidCustomerMasterKeySpecException",
|
||||
if (!isHmac(spec)) {
|
||||
throw new AwsException("InvalidCustomerMasterKeySpecException",
|
||||
"Unsupported HMAC key spec: " + spec, 400);
|
||||
};
|
||||
}
|
||||
return spec.materialByteLength();
|
||||
}
|
||||
|
||||
public KmsKey describeKey(String keyId, String region) {
|
||||
@@ -595,13 +616,27 @@ public class KmsService implements ResourceProvider {
|
||||
keyStore.put(region + "::" + key.getKeyId(), key);
|
||||
}
|
||||
|
||||
/**
|
||||
* A key whose material was never imported, or was deleted or expired while it sat pending
|
||||
* deletion, has nothing to come back to: it returns to PendingImport rather than to a usable
|
||||
* state that no cryptographic operation could actually serve.
|
||||
*/
|
||||
public void cancelKeyDeletion(String keyId, String region) {
|
||||
KmsKey key = resolveKey(keyId, region);
|
||||
key.setKeyState("Enabled");
|
||||
if (lacksImportedKeyMaterial(key)) {
|
||||
key.setKeyState(PENDING_IMPORT);
|
||||
key.setEnabled(false);
|
||||
} else {
|
||||
key.setKeyState("Enabled");
|
||||
}
|
||||
key.setDeletionDate(0);
|
||||
keyStore.put(region + "::" + key.getKeyId(), key);
|
||||
}
|
||||
|
||||
private static boolean lacksImportedKeyMaterial(KmsKey key) {
|
||||
return EXTERNAL_ORIGIN.equals(key.getOrigin()) && key.getPrivateKeyEncoded() == null;
|
||||
}
|
||||
|
||||
public Map<String, Object> getKeyPolicy(String keyId, String region) {
|
||||
KmsKey key = resolveKey(keyId, region);
|
||||
Map<String, Object> result = new HashMap<>();
|
||||
@@ -674,10 +709,11 @@ public class KmsService implements ResourceProvider {
|
||||
|
||||
public void enableKey(String keyId, String region) {
|
||||
KmsKey key = resolveKey(keyId, region);
|
||||
if ("PendingDeletion".equals(key.getKeyState())) {
|
||||
if (PENDING_DELETION.equals(key.getKeyState())) {
|
||||
throw new AwsException("KMSInvalidStateException",
|
||||
"KMS key " + key.getKeyId() + " is pending deletion.", 400);
|
||||
}
|
||||
requireImportedKeyMaterial(key, "EnableKey");
|
||||
key.setEnabled(true);
|
||||
key.setKeyState("Enabled");
|
||||
keyStore.put(region + "::" + key.getKeyId(), key);
|
||||
@@ -686,6 +722,7 @@ public class KmsService implements ResourceProvider {
|
||||
|
||||
public void disableKey(String keyId, String region) {
|
||||
KmsKey key = resolveKey(keyId, region);
|
||||
requireImportedKeyMaterial(key, "DisableKey");
|
||||
key.setEnabled(false);
|
||||
key.setKeyState("Disabled");
|
||||
keyStore.put(region + "::" + key.getKeyId(), key);
|
||||
@@ -710,6 +747,12 @@ public class KmsService implements ResourceProvider {
|
||||
}
|
||||
|
||||
private void validateRotationSupported(KmsKey key) {
|
||||
if (EXTERNAL_ORIGIN.equals(key.getOrigin())) {
|
||||
throw new AwsException(
|
||||
"UnsupportedOperationException",
|
||||
"You cannot enable automatic rotation of imported key material.",
|
||||
400);
|
||||
}
|
||||
if (KmsKeyUsage.ENCRYPT_DECRYPT != key.getKeyUsage()
|
||||
|| KmsKeySpec.SYMMETRIC_DEFAULT != key.getKeySpec()) {
|
||||
throw new AwsException(
|
||||
@@ -719,6 +762,294 @@ public class KmsService implements ResourceProvider {
|
||||
}
|
||||
}
|
||||
|
||||
/** The wrapping parameters GetParametersForImport hands back to the caller. */
|
||||
public record ImportParameters(String keyArn, String publicKeyEncoded, String importToken,
|
||||
long parametersValidTo) {
|
||||
}
|
||||
|
||||
public ImportParameters getParametersForImport(String keyId, String wrappingAlgorithm,
|
||||
String wrappingKeySpec, String region) {
|
||||
KmsKey key = resolveKey(keyId, region);
|
||||
requireExternalOrigin(key, "GetParametersForImport");
|
||||
requireNotPendingDeletion(key);
|
||||
KmsKeyImport.validateWrappingAlgorithm(wrappingAlgorithm);
|
||||
|
||||
KmsKeyImport.WrappingKeyPair wrappingKeyPair = KmsKeyImport.generateWrappingKeyPair(wrappingKeySpec);
|
||||
KmsImportParameters parameters = new KmsImportParameters();
|
||||
parameters.setWrappingPrivateKeyEncoded(wrappingKeyPair.privateKeyEncoded());
|
||||
parameters.setWrappingAlgorithm(wrappingAlgorithm);
|
||||
parameters.setImportToken(newImportToken());
|
||||
parameters.setParametersValidTo(Instant.now().plus(IMPORT_PARAMETERS_VALIDITY).getEpochSecond());
|
||||
key.setImportParameters(parameters);
|
||||
keyStore.put(region + "::" + key.getKeyId(), key);
|
||||
|
||||
LOG.infov("Issued import parameters for KMS key {0} in {1} ({2}/{3})",
|
||||
key.getKeyId(), region, wrappingAlgorithm, wrappingKeySpec);
|
||||
return new ImportParameters(key.getArn(), wrappingKeyPair.publicKeyEncoded(),
|
||||
parameters.getImportToken(), parameters.getParametersValidTo());
|
||||
}
|
||||
|
||||
/**
|
||||
* Unwraps and installs key material, which takes the key from PendingImport to Enabled. The
|
||||
* import token is spent by the call that uses it, as it is on real KMS.
|
||||
*/
|
||||
public KmsKey importKeyMaterial(String keyId, String importToken, byte[] encryptedKeyMaterial,
|
||||
String expirationModel, Long validTo, String importType, String region) {
|
||||
KmsKey key = resolveKey(keyId, region);
|
||||
requireExternalOrigin(key, "ImportKeyMaterial");
|
||||
requireNotPendingDeletion(key);
|
||||
validateImportType(importType, key);
|
||||
|
||||
KmsImportParameters parameters = requireCurrentImportToken(key);
|
||||
if (!parameters.getImportToken().equals(importToken)) {
|
||||
throw new AwsException("InvalidImportTokenException",
|
||||
"The import token is invalid or was not issued for this KMS key.", 400);
|
||||
}
|
||||
String effectiveExpirationModel = resolveExpirationModel(expirationModel, validTo);
|
||||
|
||||
byte[] material = KmsKeyImport.unwrap(parameters.getWrappingPrivateKeyEncoded(),
|
||||
parameters.getWrappingAlgorithm(), encryptedKeyMaterial);
|
||||
validateMaterialLength(key, material);
|
||||
String keyMaterialId = keyMaterialId(key.getKeyId(), material);
|
||||
requireSameMaterialAsFirstImport(key, keyMaterialId);
|
||||
|
||||
key.setPrivateKeyEncoded(Base64.getEncoder().encodeToString(material));
|
||||
key.setKeyMaterialId(keyMaterialId);
|
||||
key.setExpirationModel(effectiveExpirationModel);
|
||||
key.setValidTo(KEY_MATERIAL_EXPIRES.equals(effectiveExpirationModel) ? validTo : 0L);
|
||||
key.setKeyState("Enabled");
|
||||
key.setEnabled(true);
|
||||
key.setImportParameters(null);
|
||||
keyStore.put(region + "::" + key.getKeyId(), key);
|
||||
|
||||
LOG.infov("Imported key material into KMS key {0} in {1} ({2})",
|
||||
key.getKeyId(), region, effectiveExpirationModel);
|
||||
return key;
|
||||
}
|
||||
|
||||
/**
|
||||
* Deleting material from a key that is already pending deletion leaves the key state alone,
|
||||
* as it does on AWS: PendingDeletion outranks the PendingImport this would otherwise set.
|
||||
*/
|
||||
public KmsKey deleteImportedKeyMaterial(String keyId, String region) {
|
||||
KmsKey key = resolveKey(keyId, region);
|
||||
requireExternalOrigin(key, "DeleteImportedKeyMaterial");
|
||||
clearImportedKeyMaterial(key);
|
||||
keyStore.put(region + "::" + key.getKeyId(), key);
|
||||
LOG.infov("Deleted imported key material for KMS key {0} in {1}", key.getKeyId(), region);
|
||||
return key;
|
||||
}
|
||||
|
||||
/**
|
||||
* Real KMS deletes expired imported key material on its own schedule. With no scheduler here
|
||||
* the check runs on the next read of the key instead, which is not observable from outside:
|
||||
* nothing can reach a key without going through this path first.
|
||||
*/
|
||||
private KmsKey expireImportedKeyMaterialIfDue(KmsKey key, String region) {
|
||||
boolean expires = EXTERNAL_ORIGIN.equals(key.getOrigin())
|
||||
&& KEY_MATERIAL_EXPIRES.equals(key.getExpirationModel())
|
||||
&& key.getValidTo() > 0;
|
||||
boolean holdsMaterial = !PENDING_IMPORT.equals(key.getKeyState())
|
||||
&& !PENDING_DELETION.equals(key.getKeyState());
|
||||
if (!expires || !holdsMaterial || key.getValidTo() > Instant.now().getEpochSecond()) {
|
||||
return key;
|
||||
}
|
||||
clearImportedKeyMaterial(key);
|
||||
keyStore.put(region + "::" + key.getKeyId(), key);
|
||||
LOG.infov("Imported key material for KMS key {0} in {1} expired; key is back in PendingImport",
|
||||
key.getKeyId(), region);
|
||||
return key;
|
||||
}
|
||||
|
||||
/**
|
||||
* Drops the material but keeps {@code keyMaterialId}: KMS still refuses different material on
|
||||
* a later re-import, so what the key was originally given has to outlive the material itself.
|
||||
*/
|
||||
private static void clearImportedKeyMaterial(KmsKey key) {
|
||||
key.setPrivateKeyEncoded(null);
|
||||
key.setExpirationModel(null);
|
||||
key.setValidTo(0);
|
||||
key.setImportParameters(null);
|
||||
if (PENDING_DELETION.equals(key.getKeyState())) {
|
||||
return;
|
||||
}
|
||||
key.setEnabled(false);
|
||||
key.setKeyState(PENDING_IMPORT);
|
||||
}
|
||||
|
||||
private String newImportToken() {
|
||||
byte[] token = new byte[IMPORT_TOKEN_BYTES];
|
||||
SECURE_RANDOM.nextBytes(token);
|
||||
return Base64.getEncoder().encodeToString(token);
|
||||
}
|
||||
|
||||
private static KmsImportParameters requireCurrentImportToken(KmsKey key) {
|
||||
KmsImportParameters parameters = key.getImportParameters();
|
||||
if (parameters == null) {
|
||||
throw new AwsException("InvalidImportTokenException",
|
||||
"No import parameters are outstanding for this KMS key. "
|
||||
+ "Call GetParametersForImport first.", 400);
|
||||
}
|
||||
if (parameters.getParametersValidTo() < Instant.now().getEpochSecond()) {
|
||||
throw new AwsException("ExpiredImportTokenException",
|
||||
"The import token has expired. Call GetParametersForImport for new parameters.", 400);
|
||||
}
|
||||
return parameters;
|
||||
}
|
||||
|
||||
private static String resolveExpirationModel(String expirationModel, Long validTo) {
|
||||
String effective = (expirationModel == null || expirationModel.isBlank())
|
||||
? KEY_MATERIAL_EXPIRES : expirationModel;
|
||||
switch (effective) {
|
||||
case KEY_MATERIAL_EXPIRES -> {
|
||||
if (validTo == null) {
|
||||
throw new AwsException("ValidationException",
|
||||
"ValidTo is required when ExpirationModel is KEY_MATERIAL_EXPIRES.", 400);
|
||||
}
|
||||
validateValidTo(validTo);
|
||||
}
|
||||
case KEY_MATERIAL_DOES_NOT_EXPIRE -> {
|
||||
if (validTo != null) {
|
||||
throw new AwsException("ValidationException",
|
||||
"ValidTo must not be set when ExpirationModel is "
|
||||
+ "KEY_MATERIAL_DOES_NOT_EXPIRE.", 400);
|
||||
}
|
||||
}
|
||||
default -> throw new AwsException("ValidationException",
|
||||
"1 validation error detected: Value '" + expirationModel + "' at 'expirationModel' failed to "
|
||||
+ "satisfy constraint: Member must satisfy enum value set: "
|
||||
+ "[KEY_MATERIAL_EXPIRES, KEY_MATERIAL_DOES_NOT_EXPIRE]", 400);
|
||||
}
|
||||
return effective;
|
||||
}
|
||||
|
||||
private static void validateValidTo(long validTo) {
|
||||
long now = Instant.now().getEpochSecond();
|
||||
if (validTo <= now) {
|
||||
throw new AwsException("ValidationException",
|
||||
"ValidTo must be a future date and time.", 400);
|
||||
}
|
||||
if (validTo > now + MAX_KEY_MATERIAL_VALIDITY.toSeconds()) {
|
||||
throw new AwsException("ValidationException",
|
||||
"ValidTo must be no more than 365 days from the request date.", 400);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Multi-material rotation, where a symmetric key holds several imported materials at once, is
|
||||
* not emulated. NEW_KEY_MATERIAL on a key that already has material is refused outright rather
|
||||
* than reported as material that fails to match.
|
||||
*/
|
||||
private static void validateImportType(String importType, KmsKey key) {
|
||||
if (importType == null || importType.isBlank()) {
|
||||
return;
|
||||
}
|
||||
switch (importType) {
|
||||
case "NEW_KEY_MATERIAL" -> {
|
||||
if (key.getKeyMaterialId() != null) {
|
||||
throw new AwsException("UnsupportedOperationException",
|
||||
"Importing additional key material into a KMS key that already has key material "
|
||||
+ "is not supported. Reimport the existing key material instead.", 400);
|
||||
}
|
||||
}
|
||||
case "EXISTING_KEY_MATERIAL" -> {
|
||||
if (key.getKeyMaterialId() == null) {
|
||||
throw new AwsException("IncorrectKeyMaterialException",
|
||||
"No key material has ever been imported into this KMS key, so there is no "
|
||||
+ "existing key material to reimport.", 400);
|
||||
}
|
||||
}
|
||||
default -> throw new AwsException("ValidationException",
|
||||
"1 validation error detected: Value '" + importType + "' at 'importType' failed to "
|
||||
+ "satisfy constraint: Member must satisfy enum value set: "
|
||||
+ "[NEW_KEY_MATERIAL, EXISTING_KEY_MATERIAL]", 400);
|
||||
}
|
||||
}
|
||||
|
||||
private static void validateMaterialLength(KmsKey key, byte[] material) {
|
||||
int expected = key.getKeySpec().materialByteLength();
|
||||
if (material.length != expected) {
|
||||
throw new AwsException("IncorrectKeyMaterialException",
|
||||
"Key material for key spec " + key.getKeySpec() + " must be " + expected
|
||||
+ " bytes but was " + material.length + " bytes.", 400);
|
||||
}
|
||||
}
|
||||
|
||||
private static void requireSameMaterialAsFirstImport(KmsKey key, String keyMaterialId) {
|
||||
if (key.getKeyMaterialId() != null && !key.getKeyMaterialId().equals(keyMaterialId)) {
|
||||
throw new AwsException("IncorrectKeyMaterialException",
|
||||
"The key material does not match the key material that was previously imported "
|
||||
+ "into this KMS key.", 400);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* KMS derives a key material id from the KMS key id and the material itself. Deriving it the
|
||||
* same way identifies imported material without keeping a second copy of it: a re-import only
|
||||
* has to prove it carries the same bytes, never to have them read back.
|
||||
*/
|
||||
private static String keyMaterialId(String keyId, byte[] material) {
|
||||
try {
|
||||
MessageDigest digest = MessageDigest.getInstance("SHA-256");
|
||||
digest.update(keyId.getBytes(StandardCharsets.UTF_8));
|
||||
digest.update(material);
|
||||
return HexFormat.of().formatHex(digest.digest());
|
||||
} catch (NoSuchAlgorithmException e) {
|
||||
throw new AwsException("InternalFailure", "SHA-256 unavailable", 500);
|
||||
}
|
||||
}
|
||||
|
||||
private static String resolveOrigin(String origin, KmsKeySpec spec) {
|
||||
String effective = (origin == null || origin.isBlank()) ? AWS_KMS_ORIGIN : origin;
|
||||
return switch (effective) {
|
||||
case AWS_KMS_ORIGIN -> effective;
|
||||
case EXTERNAL_ORIGIN -> requireImportableSpec(spec);
|
||||
case "AWS_CLOUDHSM", "EXTERNAL_KEY_STORE" -> throw new AwsException("UnsupportedOperationException",
|
||||
"Origin " + effective + " is not supported.", 400);
|
||||
default -> throw new AwsException("ValidationException",
|
||||
"1 validation error detected: Value '" + origin + "' at 'origin' failed to satisfy "
|
||||
+ "constraint: Member must satisfy enum value set: "
|
||||
+ "[AWS_KMS, EXTERNAL, AWS_CLOUDHSM, EXTERNAL_KEY_STORE]", 400);
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Imported material here is a raw byte string, which covers SYMMETRIC_DEFAULT and the HMAC
|
||||
* specs. Real KMS also imports asymmetric material as a DER key pair; refusing it outright
|
||||
* beats accepting a key that could never sign or decrypt anything.
|
||||
*/
|
||||
private static String requireImportableSpec(KmsKeySpec spec) {
|
||||
if (spec != KmsKeySpec.SYMMETRIC_DEFAULT && spec.getKeyType() != KmsKeySpec.KeyType.HMAC) {
|
||||
throw new AwsException("UnsupportedOperationException",
|
||||
"Origin EXTERNAL is only supported for SYMMETRIC_DEFAULT and HMAC key specs, not "
|
||||
+ spec + ".", 400);
|
||||
}
|
||||
return EXTERNAL_ORIGIN;
|
||||
}
|
||||
|
||||
private static void requireExternalOrigin(KmsKey key, String operation) {
|
||||
if (!EXTERNAL_ORIGIN.equals(key.getOrigin())) {
|
||||
throw new AwsException("UnsupportedOperationException",
|
||||
operation + " is only supported for KMS keys with Origin EXTERNAL; key "
|
||||
+ key.getKeyId() + " has origin " + key.getOrigin() + ".", 400);
|
||||
}
|
||||
}
|
||||
|
||||
private static void requireNotPendingDeletion(KmsKey key) {
|
||||
if (PENDING_DELETION.equals(key.getKeyState())) {
|
||||
throw new AwsException("KMSInvalidStateException",
|
||||
"KMS key " + key.getKeyId() + " is pending deletion.", 400);
|
||||
}
|
||||
}
|
||||
|
||||
private static void requireImportedKeyMaterial(KmsKey key, String operation) {
|
||||
if (PENDING_IMPORT.equals(key.getKeyState())) {
|
||||
throw new AwsException("KMSInvalidStateException",
|
||||
operation + " is not valid for KMS key " + key.getKeyId()
|
||||
+ " because it has no key material. Its state is PendingImport.", 400);
|
||||
}
|
||||
}
|
||||
|
||||
// ──────────────────────────── Aliases ────────────────────────────
|
||||
|
||||
public void createAlias(String aliasName, String targetKeyId, String region) {
|
||||
@@ -1240,6 +1571,7 @@ public class KmsService implements ResourceProvider {
|
||||
|
||||
private KmsKey validateMacOperationKey(String keyId, String algorithm, String region) {
|
||||
KmsKey kmsKey = resolveKey(keyId, region);
|
||||
requireImportedKeyMaterial(kmsKey, "MAC operations");
|
||||
KmsKeySpec spec = kmsKey.getKeySpec();
|
||||
if (!isHmac(spec) || !KmsKeyUsage.GENERATE_VERIFY_MAC.equals(kmsKey.getKeyUsage())) {
|
||||
throw new AwsException("InvalidKeyUsageException",
|
||||
@@ -1600,18 +1932,20 @@ public class KmsService implements ResourceProvider {
|
||||
}
|
||||
|
||||
// Key id
|
||||
return keyStore.get(region + "::" + id)
|
||||
KmsKey key = keyStore.get(region + "::" + id)
|
||||
.orElseThrow(() -> new AwsException("NotFoundException", "Key not found: " + keyIdOrArn, 404));
|
||||
return expireImportedKeyMaterialIfDue(key, region);
|
||||
}
|
||||
|
||||
private static void validateKeyIsUsableForCryptoOperations(KmsKey key) {
|
||||
if ("PendingDeletion".equals(key.getKeyState())) {
|
||||
if (PENDING_DELETION.equals(key.getKeyState())) {
|
||||
throw new AwsException(
|
||||
"KMSInvalidStateException",
|
||||
"KMS key " + key.getKeyId() + " is pending deletion.",
|
||||
400
|
||||
);
|
||||
}
|
||||
requireImportedKeyMaterial(key, "This operation");
|
||||
if (!key.isEnabled()) {
|
||||
throw new AwsException(
|
||||
"DisabledException",
|
||||
|
||||
@@ -0,0 +1,37 @@
|
||||
package io.github.hectorvent.floci.services.kms.model;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
|
||||
import io.quarkus.runtime.annotations.RegisterForReflection;
|
||||
|
||||
/**
|
||||
* The wrapping material GetParametersForImport issued for one KMS key, held until
|
||||
* ImportKeyMaterial consumes it.
|
||||
*
|
||||
* <p>It lives on the key rather than in a store of its own because KMS scopes an import
|
||||
* token to a single key and lets only the most recent one work: a second
|
||||
* GetParametersForImport call replaces these parameters, which invalidates the token the
|
||||
* first call handed out.
|
||||
*/
|
||||
@RegisterForReflection
|
||||
@JsonIgnoreProperties(ignoreUnknown = true)
|
||||
public class KmsImportParameters {
|
||||
|
||||
private String wrappingPrivateKeyEncoded;
|
||||
private String wrappingAlgorithm;
|
||||
private String importToken;
|
||||
private long parametersValidTo;
|
||||
|
||||
public String getWrappingPrivateKeyEncoded() { return wrappingPrivateKeyEncoded; }
|
||||
public void setWrappingPrivateKeyEncoded(String wrappingPrivateKeyEncoded) {
|
||||
this.wrappingPrivateKeyEncoded = wrappingPrivateKeyEncoded;
|
||||
}
|
||||
|
||||
public String getWrappingAlgorithm() { return wrappingAlgorithm; }
|
||||
public void setWrappingAlgorithm(String wrappingAlgorithm) { this.wrappingAlgorithm = wrappingAlgorithm; }
|
||||
|
||||
public String getImportToken() { return importToken; }
|
||||
public void setImportToken(String importToken) { this.importToken = importToken; }
|
||||
|
||||
public long getParametersValidTo() { return parametersValidTo; }
|
||||
public void setParametersValidTo(long parametersValidTo) { this.parametersValidTo = parametersValidTo; }
|
||||
}
|
||||
@@ -14,7 +14,12 @@ public class KmsKey {
|
||||
private String arn;
|
||||
private String description;
|
||||
private boolean enabled = true;
|
||||
private String keyState = "Enabled"; // Enabled, Disabled, PendingDeletion
|
||||
private String keyState = "Enabled"; // Enabled, Disabled, PendingDeletion, PendingImport
|
||||
private String origin = "AWS_KMS";
|
||||
private String expirationModel;
|
||||
private long validTo;
|
||||
private KmsImportParameters importParameters;
|
||||
private String keyMaterialId;
|
||||
private KmsKeyUsage keyUsage = KmsKeyUsage.ENCRYPT_DECRYPT;
|
||||
private KmsKeySpec keySpec = KmsKeySpec.SYMMETRIC_DEFAULT;
|
||||
private long creationDate;
|
||||
@@ -45,6 +50,23 @@ public class KmsKey {
|
||||
public String getKeyState() { return keyState; }
|
||||
public void setKeyState(String keyState) { this.keyState = keyState; }
|
||||
|
||||
public String getOrigin() { return origin; }
|
||||
public void setOrigin(String origin) { this.origin = origin; }
|
||||
|
||||
public String getExpirationModel() { return expirationModel; }
|
||||
public void setExpirationModel(String expirationModel) { this.expirationModel = expirationModel; }
|
||||
|
||||
public long getValidTo() { return validTo; }
|
||||
public void setValidTo(long validTo) { this.validTo = validTo; }
|
||||
|
||||
public KmsImportParameters getImportParameters() { return importParameters; }
|
||||
public void setImportParameters(KmsImportParameters importParameters) {
|
||||
this.importParameters = importParameters;
|
||||
}
|
||||
|
||||
public String getKeyMaterialId() { return keyMaterialId; }
|
||||
public void setKeyMaterialId(String keyMaterialId) { this.keyMaterialId = keyMaterialId; }
|
||||
|
||||
public KmsKeyUsage getKeyUsage() { return keyUsage; }
|
||||
public void setKeyUsage(KmsKeyUsage keyUsage) { this.keyUsage = keyUsage; }
|
||||
|
||||
|
||||
@@ -102,6 +102,23 @@ public enum KmsKeySpec {
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Length in bytes of the raw key material for the specs whose material is a byte string:
|
||||
* SYMMETRIC_DEFAULT and the HMAC family. Asymmetric specs carry a DER-encoded key pair with
|
||||
* no single length, so they have none.
|
||||
*/
|
||||
public int materialByteLength() {
|
||||
return switch (this) {
|
||||
case SYMMETRIC_DEFAULT -> 32;
|
||||
case HMAC_224 -> 28;
|
||||
case HMAC_256 -> 32;
|
||||
case HMAC_384 -> 48;
|
||||
case HMAC_512 -> 64;
|
||||
default -> throw new AwsException("InvalidCustomerMasterKeySpecException",
|
||||
"Key spec " + this + " has no fixed key material length.", 400);
|
||||
};
|
||||
}
|
||||
|
||||
public enum KeyType {
|
||||
RSA, ECC, ED25519, SYMMETRIC, HMAC, ML_DSA, SM2
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user