mirror of
https://github.com/floci-io/floci.git
synced 2026-11-23 22:11:36 -05:00
feat(kms): support RSA imported key material (#4152)
* refactor(kms): encapsulate import key material logic in KmsKeyType implementations * feat(kms): support RSA key import * fix(kms): check aes key length * fix(kms): wrapping algorithm value validation * docs(kms): update docs to reflect asymmetric RSA import support * fix(kms): add private CipherUtils constructor * test(kms): add tests for rsa key material import
This commit is contained in:
1 parent
fafe5476cd
commit
51b6485173
10 files changed
+485
-73
No files matched your search
@@ -0,0 +1,101 @@
|
||||
package io.github.hectorvent.floci.services.kms;
|
||||
|
||||
import javax.crypto.Cipher;
|
||||
import javax.crypto.SecretKey;
|
||||
import javax.crypto.spec.OAEPParameterSpec;
|
||||
import javax.crypto.spec.PSource;
|
||||
import java.security.GeneralSecurityException;
|
||||
import java.security.InvalidKeyException;
|
||||
import java.security.Key;
|
||||
import java.security.KeyFactory;
|
||||
import java.security.PrivateKey;
|
||||
import java.security.PublicKey;
|
||||
import java.security.interfaces.RSAPrivateCrtKey;
|
||||
import java.security.interfaces.RSAPrivateKey;
|
||||
import java.security.spec.MGF1ParameterSpec;
|
||||
import java.security.spec.PKCS8EncodedKeySpec;
|
||||
import java.security.spec.RSAPublicKeySpec;
|
||||
import java.util.Arrays;
|
||||
|
||||
public final class CipherUtils {
|
||||
private CipherUtils() {
|
||||
}
|
||||
|
||||
/** Decodes a PKCS#8-encoded RSA private key. */
|
||||
public static PrivateKey generateRsaPrivateKey(byte[] encodedKey) throws GeneralSecurityException {
|
||||
return KeyFactory.getInstance("RSA").generatePrivate(new PKCS8EncodedKeySpec(encodedKey));
|
||||
}
|
||||
|
||||
/** Derives the public key from the modulus and public exponent of an RSA CRT private key. */
|
||||
public static PublicKey generateRsaPublicKey(RSAPrivateCrtKey privateKey) throws GeneralSecurityException {
|
||||
RSAPublicKeySpec publicKeySpec = new RSAPublicKeySpec(privateKey.getModulus(), privateKey.getPublicExponent());
|
||||
return KeyFactory.getInstance("RSA")
|
||||
.generatePublic(publicKeySpec);
|
||||
}
|
||||
|
||||
public static byte[] encryptRsaOaep(PublicKey key, String digest, byte[] plaintext) throws GeneralSecurityException {
|
||||
return rsaOaepCipher(Cipher.ENCRYPT_MODE, key, digest)
|
||||
.doFinal(plaintext);
|
||||
}
|
||||
|
||||
public static byte[] decryptRsaOaep(PrivateKey key, String digest, byte[] ciphertext) throws GeneralSecurityException {
|
||||
return rsaOaepCipher(Cipher.DECRYPT_MODE, key, digest)
|
||||
.doFinal(ciphertext);
|
||||
}
|
||||
|
||||
/**
|
||||
* Unwraps key material encrypted with an {@code RSA_AES_KEY_WRAP_*} algorithm.
|
||||
*
|
||||
* <p>The input contains an RSA-OAEP-encrypted AES-256 wrapping key followed by key material
|
||||
* encrypted with that key using AES-KWP.
|
||||
*
|
||||
* @param wrappingKey RSA private key issued for import
|
||||
* @param digest digest used by RSA-OAEP
|
||||
* @param encryptedKeyMaterial encrypted key material payload
|
||||
* @return key material
|
||||
* @throws GeneralSecurityException if the wrapping key or key material cannot be unwrapped
|
||||
*/
|
||||
public static byte[] unwrapRsaAes(PrivateKey wrappingKey, String digest, byte[] encryptedKeyMaterial) throws GeneralSecurityException {
|
||||
if (!(wrappingKey instanceof RSAPrivateKey rsaPrivateKey)) {
|
||||
throw new IllegalArgumentException("Wrapping key is not RSA private key.");
|
||||
}
|
||||
|
||||
int wrappedAesKeyLength = (rsaPrivateKey.getModulus().bitLength() + 7) / 8;
|
||||
if (encryptedKeyMaterial.length <= wrappedAesKeyLength) {
|
||||
throw new IllegalArgumentException("Encrypted key material is too short.");
|
||||
}
|
||||
byte[] wrappedAesKey = Arrays.copyOfRange(encryptedKeyMaterial, 0, wrappedAesKeyLength);
|
||||
byte[] wrappedMaterial = Arrays.copyOfRange(encryptedKeyMaterial, wrappedAesKeyLength, encryptedKeyMaterial.length);
|
||||
|
||||
SecretKey aesKey = (SecretKey) CipherUtils.rsaOaepUnwrapAesKey(rsaPrivateKey, digest, Cipher.SECRET_KEY, wrappedAesKey);
|
||||
byte[] aesKeyBytes = aesKey.getEncoded();
|
||||
if (aesKeyBytes == null || aesKeyBytes.length != 32) {
|
||||
throw new InvalidKeyException("RSA_AES_KEY_WRAP_* requires a 256-bit AES key.");
|
||||
}
|
||||
return CipherUtils.aesKwpDecrypt(aesKey, wrappedMaterial);
|
||||
}
|
||||
|
||||
private static Key rsaOaepUnwrapAesKey(PrivateKey privateKey, String digest, int wrappedKeyType, byte[] wrappedKey) throws GeneralSecurityException {
|
||||
return rsaOaepCipher(Cipher.UNWRAP_MODE, privateKey, digest)
|
||||
.unwrap(wrappedKey, "AES", wrappedKeyType);
|
||||
}
|
||||
|
||||
private static byte[] aesKwpDecrypt(SecretKey aesKey, byte[] wrappedMaterial) throws GeneralSecurityException {
|
||||
return aesKwpCipher(Cipher.DECRYPT_MODE, aesKey).doFinal(wrappedMaterial);
|
||||
}
|
||||
|
||||
private static Cipher rsaOaepCipher(int mode, Key key, String digest) throws GeneralSecurityException {
|
||||
Cipher cipher = Cipher.getInstance("RSA/ECB/OAEPPadding");
|
||||
OAEPParameterSpec parameterSpec = new OAEPParameterSpec(digest, "MGF1",
|
||||
new MGF1ParameterSpec(digest), PSource.PSpecified.DEFAULT);
|
||||
|
||||
cipher.init(mode, key, parameterSpec);
|
||||
return cipher;
|
||||
}
|
||||
|
||||
private static Cipher aesKwpCipher(int mode, SecretKey aesKey) throws GeneralSecurityException {
|
||||
Cipher cipher = Cipher.getInstance("AES/KWP/NoPadding");
|
||||
cipher.init(mode, aesKey);
|
||||
return cipher;
|
||||
}
|
||||
}
|
||||
@@ -1,18 +1,13 @@
|
||||
package io.github.hectorvent.floci.services.kms;
|
||||
|
||||
import io.github.hectorvent.floci.core.common.AwsException;
|
||||
import io.github.hectorvent.floci.services.kms.model.KmsKeySpec;
|
||||
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;
|
||||
|
||||
/**
|
||||
@@ -31,6 +26,9 @@ final class KmsKeyImport {
|
||||
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 static final String RSA_AES_KEY_WRAP_SHA_1 = "RSA_AES_KEY_WRAP_SHA_1";
|
||||
private static final String RSA_AES_KEY_WRAP_SHA_256 = "RSA_AES_KEY_WRAP_SHA_256";
|
||||
private static final String SM2PKE = "SM2PKE";
|
||||
|
||||
private KmsKeyImport() {
|
||||
}
|
||||
@@ -73,19 +71,41 @@ final class KmsKeyImport {
|
||||
* 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.
|
||||
* October 10, 2023.
|
||||
*/
|
||||
static void validateWrappingAlgorithm(String wrappingAlgorithm) {
|
||||
switch (wrappingAlgorithm == null ? "" : wrappingAlgorithm) {
|
||||
case RSAES_OAEP_SHA_1, RSAES_OAEP_SHA_256 -> { }
|
||||
static void validateWrappingAlgorithm(KmsKeySpec keySpec, String wrappingAlgorithm) {
|
||||
String algorithm = wrappingAlgorithm == null ? "" : wrappingAlgorithm;
|
||||
validateWrappingAlgorithmValue(algorithm, wrappingAlgorithm);
|
||||
|
||||
switch (keySpec.getKeyType()) {
|
||||
case HMAC, SYMMETRIC -> {
|
||||
if (!RSAES_OAEP_SHA_1.equals(algorithm) && !RSAES_OAEP_SHA_256.equals(algorithm)) {
|
||||
throw new AwsException("UnsupportedOperationException",
|
||||
"WrappingAlgorithm " + wrappingAlgorithm + " is not supported for " + keySpec + " key. "
|
||||
+ "Supported values are: RSAES_OAEP_SHA_1 and RSAES_OAEP_SHA_256.", 400);
|
||||
}
|
||||
}
|
||||
case RSA -> {
|
||||
if (!RSA_AES_KEY_WRAP_SHA_1.equals(algorithm) && !RSA_AES_KEY_WRAP_SHA_256.equals(algorithm)) {
|
||||
throw new AwsException("UnsupportedOperationException",
|
||||
"WrappingAlgorithm " + wrappingAlgorithm + " is not supported for " + keySpec + " key. "
|
||||
+ "Supported values are: RSA_AES_KEY_WRAP_SHA_1 and RSA_AES_KEY_WRAP_SHA_256.", 400);
|
||||
}
|
||||
}
|
||||
default -> throw new AwsException("UnsupportedOperationException",
|
||||
"Importing key material for key spec " + keySpec + " is not supported.", 400);
|
||||
}
|
||||
}
|
||||
|
||||
private static void validateWrappingAlgorithmValue(String algorithm, String wrappingAlgorithm) {
|
||||
switch (algorithm) {
|
||||
case RSAES_OAEP_SHA_1, RSAES_OAEP_SHA_256, RSA_AES_KEY_WRAP_SHA_1, RSA_AES_KEY_WRAP_SHA_256 -> { }
|
||||
case SM2PKE -> throw new AwsException("UnsupportedOperationException",
|
||||
"WrappingAlgorithm SM2PKE is not supported.", 400);
|
||||
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);
|
||||
+ "Use RSA_AES_KEY_WRAP_SHA_1, RSA_AES_KEY_WRAP_SHA_256, "
|
||||
+ "RSAES_OAEP_SHA_1 or RSAES_OAEP_SHA_256.", 400);
|
||||
default -> throw new AwsException("ValidationException",
|
||||
"1 validation error detected: Value '" + wrappingAlgorithm + "' at 'wrappingAlgorithm' failed "
|
||||
+ "to satisfy constraint: Member must satisfy enum value set: "
|
||||
@@ -103,13 +123,18 @@ final class KmsKeyImport {
|
||||
*/
|
||||
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);
|
||||
PrivateKey wrappingKey = CipherUtils.generateRsaPrivateKey(Base64.getDecoder().decode(wrappingPrivateKeyEncoded));
|
||||
|
||||
return switch (wrappingAlgorithm) {
|
||||
case RSA_AES_KEY_WRAP_SHA_1 -> CipherUtils.unwrapRsaAes(wrappingKey, "SHA-1", encryptedKeyMaterial);
|
||||
case RSA_AES_KEY_WRAP_SHA_256 -> CipherUtils.unwrapRsaAes(wrappingKey, "SHA-256", encryptedKeyMaterial);
|
||||
case RSAES_OAEP_SHA_1 -> CipherUtils.decryptRsaOaep(wrappingKey, "SHA-1", encryptedKeyMaterial);
|
||||
case RSAES_OAEP_SHA_256 -> CipherUtils.decryptRsaOaep(wrappingKey, "SHA-256", encryptedKeyMaterial);
|
||||
default -> throw new AwsException("UnsupportedOperationException",
|
||||
"WrappingAlgorithm " + wrappingAlgorithm + " is not supported. Supported values are "
|
||||
+ "RSA_AES_KEY_WRAP_SHA_1, RSA_AES_KEY_WRAP_SHA_256, RSAES_OAEP_SHA_1 and RSAES_OAEP_SHA_256.", 400);
|
||||
|
||||
};
|
||||
} catch (Exception e) {
|
||||
LOG.debugv(e, "Unwrapping imported key material failed for wrapping algorithm {0}", wrappingAlgorithm);
|
||||
throw new AwsException("InvalidCiphertextException", null, 400);
|
||||
|
||||
@@ -11,6 +11,7 @@ import io.github.hectorvent.floci.core.resource.ResourceProvider;
|
||||
import io.github.hectorvent.floci.core.resource.SupportedResourceType;
|
||||
import io.github.hectorvent.floci.core.storage.StorageBackend;
|
||||
import io.github.hectorvent.floci.core.storage.StorageFactory;
|
||||
import io.github.hectorvent.floci.services.kms.keytype.KmsKeyType;
|
||||
import io.github.hectorvent.floci.services.kms.keytype.KmsKeyTypes;
|
||||
import io.github.hectorvent.floci.services.kms.model.KmsAlias;
|
||||
import io.github.hectorvent.floci.services.kms.model.KmsGrant;
|
||||
@@ -866,7 +867,7 @@ public class KmsService implements ResourceProvider {
|
||||
KmsKey key = resolveKey(keyId, region);
|
||||
requireExternalOrigin(key);
|
||||
requireNotPendingDeletion(key);
|
||||
KmsKeyImport.validateWrappingAlgorithm(wrappingAlgorithm);
|
||||
KmsKeyImport.validateWrappingAlgorithm(key.getKeySpec(), wrappingAlgorithm);
|
||||
|
||||
KmsKeyImport.WrappingKeyPair wrappingKeyPair = KmsKeyImport.generateWrappingKeyPair(wrappingKeySpec);
|
||||
KmsImportParameters parameters = new KmsImportParameters();
|
||||
@@ -903,11 +904,12 @@ public class KmsService implements ResourceProvider {
|
||||
|
||||
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));
|
||||
KmsKeyType keyType = keyTypes.of(key.getKeySpec());
|
||||
keyType.importKeyMaterial(key, material);
|
||||
|
||||
if (KmsKeySpec.SYMMETRIC_DEFAULT == key.getKeySpec()) {
|
||||
installImportedBackingKey(key, keyMaterialId, material);
|
||||
}
|
||||
@@ -1081,15 +1083,6 @@ public class KmsService implements ResourceProvider {
|
||||
}
|
||||
}
|
||||
|
||||
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",
|
||||
@@ -1129,14 +1122,15 @@ public class KmsService implements ResourceProvider {
|
||||
}
|
||||
|
||||
/**
|
||||
* 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.
|
||||
* Allows imports for symmetric, HMAC and RSA key specs. Other key specs are not supported.
|
||||
*/
|
||||
private static String requireImportableSpec(KmsKeySpec spec) {
|
||||
if (spec != KmsKeySpec.SYMMETRIC_DEFAULT && spec.getKeyType() != KmsKeySpec.KeyType.HMAC) {
|
||||
if (spec != KmsKeySpec.SYMMETRIC_DEFAULT
|
||||
&& spec.getKeyType() != KmsKeySpec.KeyType.HMAC
|
||||
&& spec.getKeyType() != KmsKeySpec.KeyType.RSA) {
|
||||
|
||||
throw new AwsException("UnsupportedOperationException",
|
||||
"Origin EXTERNAL is only supported for SYMMETRIC_DEFAULT and HMAC key specs, not "
|
||||
"Origin EXTERNAL is only supported for SYMMETRIC_DEFAULT, HMAC and RSA key specs, not "
|
||||
+ spec + ".", 400);
|
||||
}
|
||||
return EXTERNAL_ORIGIN;
|
||||
|
||||
@@ -43,4 +43,19 @@ final class HmacKeyType implements KmsKeyType {
|
||||
default -> throw new AwsException("InvalidMacAlgorithmException", "Unsupported MAC algorithm: " + awsAlgo, 400);
|
||||
};
|
||||
}
|
||||
|
||||
@Override
|
||||
public void importKeyMaterial(KmsKey key, byte[] material) {
|
||||
validateKeyMaterialLength(key, material);
|
||||
key.setPrivateKeyEncoded(Base64.getEncoder().encodeToString(material));
|
||||
}
|
||||
|
||||
private static void validateKeyMaterialLength(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);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -31,4 +31,14 @@ public interface KmsKeyType {
|
||||
default byte[] generateMac(KmsKey key, byte[] message, String algorithm) throws GeneralSecurityException {
|
||||
throw new IllegalStateException(key.getKeySpec() + " does not generate MACs");
|
||||
}
|
||||
|
||||
/**
|
||||
* Validates and stores imported key material for the specified KMS key.
|
||||
*
|
||||
* @param key target KMS key
|
||||
* @param material key material after transport wrapping has been removed
|
||||
*/
|
||||
default void importKeyMaterial(KmsKey key, byte[] material) {
|
||||
throw new IllegalStateException(key.getKeySpec() + " does not import key material");
|
||||
}
|
||||
}
|
||||
@@ -1,6 +1,7 @@
|
||||
package io.github.hectorvent.floci.services.kms.keytype;
|
||||
|
||||
import io.github.hectorvent.floci.core.common.AwsException;
|
||||
import io.github.hectorvent.floci.services.kms.CipherUtils;
|
||||
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.KmsMessageType;
|
||||
@@ -29,7 +30,9 @@ import java.security.KeyPairGenerator;
|
||||
import java.security.PrivateKey;
|
||||
import java.security.PublicKey;
|
||||
import java.security.SecureRandom;
|
||||
import java.security.interfaces.RSAPrivateCrtKey;
|
||||
import java.security.spec.MGF1ParameterSpec;
|
||||
import java.util.Base64;
|
||||
|
||||
import static io.github.hectorvent.floci.services.kms.model.KmsMessageType.RAW;
|
||||
|
||||
@@ -87,6 +90,52 @@ final class RsaKeyType implements KmsKeyType {
|
||||
return oaep(Cipher.DECRYPT_MODE, key, algorithm, ciphertext);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void importKeyMaterial(KmsKey key, byte[] material) {
|
||||
PrivateKey privateKey = parsePrivateKey(material);
|
||||
|
||||
if (!(privateKey instanceof RSAPrivateCrtKey rsaPrivateKey)) {
|
||||
throw new AwsException("IncorrectKeyMaterialException",
|
||||
"Imported RSA key material must be a PKCS#8-encoded two-prime RSA private key.", 400);
|
||||
}
|
||||
|
||||
requireExpectedModulusSize(key.getKeySpec(), rsaPrivateKey);
|
||||
|
||||
PublicKey publicKey = generatePublicKey(rsaPrivateKey);
|
||||
|
||||
Base64.Encoder encoder = Base64.getEncoder();
|
||||
key.setPrivateKeyEncoded(encoder.encodeToString(material));
|
||||
key.setPublicKeyEncoded(encoder.encodeToString(publicKey.getEncoded()));
|
||||
}
|
||||
|
||||
private static PrivateKey parsePrivateKey(byte[] material) {
|
||||
try {
|
||||
return CipherUtils.generateRsaPrivateKey(material);
|
||||
} catch (GeneralSecurityException e) {
|
||||
throw new AwsException("IncorrectKeyMaterialException",
|
||||
"Imported RSA key material is not a valid PKCS#8 RSA private key.", 400);
|
||||
}
|
||||
}
|
||||
|
||||
private static PublicKey generatePublicKey(RSAPrivateCrtKey privateKey) {
|
||||
try {
|
||||
return CipherUtils.generateRsaPublicKey(privateKey);
|
||||
} catch (GeneralSecurityException e) {
|
||||
throw new AwsException("InternalFailure",
|
||||
"Failed to derive RSA public key: " + e.getMessage(), 500);
|
||||
}
|
||||
}
|
||||
|
||||
private static void requireExpectedModulusSize(KmsKeySpec spec, RSAPrivateCrtKey privateKey) {
|
||||
int expectedBits = keySize(spec);
|
||||
int actualBits = privateKey.getModulus().bitLength();
|
||||
if (actualBits != expectedBits) {
|
||||
throw new AwsException("IncorrectKeyMaterialException",
|
||||
"Key material for key spec " + spec + " must have a " + expectedBits
|
||||
+ "-bit RSA modulus but was " + actualBits + " bits.", 400);
|
||||
}
|
||||
}
|
||||
|
||||
private static int keySize(KmsKeySpec spec) {
|
||||
return Integer.parseInt(spec.name().substring("RSA_".length()));
|
||||
}
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
package io.github.hectorvent.floci.services.kms.keytype;
|
||||
|
||||
import io.github.hectorvent.floci.core.common.AwsException;
|
||||
import io.github.hectorvent.floci.services.kms.model.KmsKey;
|
||||
import io.github.hectorvent.floci.services.kms.model.KmsKeySpec;
|
||||
|
||||
@@ -31,4 +32,19 @@ public final class SymmetricKeyType implements KmsKeyType {
|
||||
key.getBackingKeys().put(backingKeyId, Base64.getEncoder().encodeToString(material));
|
||||
key.setCurrentBackingKeyId(backingKeyId);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void importKeyMaterial(KmsKey key, byte[] material) {
|
||||
validateKeyMaterialLength(key, material);
|
||||
key.setPrivateKeyEncoded(Base64.getEncoder().encodeToString(material));
|
||||
}
|
||||
|
||||
private static void validateKeyMaterialLength(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);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user