From ec4943380b72839f34d60ca2e525e84266ebd7ca Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?TATSUNO=20=E2=80=9CTaz=E2=80=9D=20Yasuhiro?= Date: Sat, 19 Sep 2026 12:24:22 +0900 Subject: [PATCH] refactor(kms): split KmsService by key type (#3882) * refactor(kms): split crypto per key type * refactor(kms): reduce redundant comments --- .../floci/services/kms/KmsService.java | 616 +----------------- .../services/kms/keytype/AsymmetricKeys.java | 90 +++ .../floci/services/kms/keytype/BcEcKeys.java | 54 ++ .../services/kms/keytype/EccNistKeyType.java | 45 ++ .../kms/keytype/EccSecgP256k1KeyType.java | 90 +++ .../services/kms/keytype/Ed25519KeyType.java | 83 +++ .../services/kms/keytype/HmacKeyType.java | 46 ++ .../services/kms/keytype/KmsKeyType.java | 35 + .../services/kms/keytype/KmsKeyTypes.java | 44 ++ .../services/kms/keytype/MlDsaKeyType.java | 39 ++ .../services/kms/keytype/RsaKeyType.java | 182 ++++++ .../services/kms/keytype/Sm2KeyType.java | 54 ++ .../kms/keytype/SymmetricKeyType.java | 34 + .../services/kms/keytype/KmsKeyTypesTest.java | 259 ++++++++ 14 files changed, 1070 insertions(+), 601 deletions(-) create mode 100644 src/main/java/io/github/hectorvent/floci/services/kms/keytype/AsymmetricKeys.java create mode 100644 src/main/java/io/github/hectorvent/floci/services/kms/keytype/BcEcKeys.java create mode 100644 src/main/java/io/github/hectorvent/floci/services/kms/keytype/EccNistKeyType.java create mode 100644 src/main/java/io/github/hectorvent/floci/services/kms/keytype/EccSecgP256k1KeyType.java create mode 100644 src/main/java/io/github/hectorvent/floci/services/kms/keytype/Ed25519KeyType.java create mode 100644 src/main/java/io/github/hectorvent/floci/services/kms/keytype/HmacKeyType.java create mode 100644 src/main/java/io/github/hectorvent/floci/services/kms/keytype/KmsKeyType.java create mode 100644 src/main/java/io/github/hectorvent/floci/services/kms/keytype/KmsKeyTypes.java create mode 100644 src/main/java/io/github/hectorvent/floci/services/kms/keytype/MlDsaKeyType.java create mode 100644 src/main/java/io/github/hectorvent/floci/services/kms/keytype/RsaKeyType.java create mode 100644 src/main/java/io/github/hectorvent/floci/services/kms/keytype/Sm2KeyType.java create mode 100644 src/main/java/io/github/hectorvent/floci/services/kms/keytype/SymmetricKeyType.java create mode 100644 src/test/java/io/github/hectorvent/floci/services/kms/keytype/KmsKeyTypesTest.java 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.java index 741a496226..0b640dc3a9 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 @@ -10,6 +10,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.KmsKeyTypes; 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; @@ -19,67 +20,19 @@ import io.github.hectorvent.floci.services.kms.model.KmsKeyUsage; import io.github.hectorvent.floci.services.kms.model.KmsMessageType; import jakarta.enterprise.context.ApplicationScoped; import jakarta.inject.Inject; -import org.bouncycastle.asn1.ASN1Integer; -import org.bouncycastle.asn1.ASN1ObjectIdentifier; -import org.bouncycastle.asn1.ASN1Primitive; -import org.bouncycastle.asn1.ASN1Sequence; -import org.bouncycastle.asn1.DERNull; -import org.bouncycastle.asn1.DERSequenceGenerator; -import org.bouncycastle.asn1.nist.NISTObjectIdentifiers; -import org.bouncycastle.asn1.pkcs.PrivateKeyInfo; -import org.bouncycastle.asn1.x509.AlgorithmIdentifier; -import org.bouncycastle.asn1.x509.DigestInfo; -import org.bouncycastle.asn1.x509.SubjectPublicKeyInfo; -import org.bouncycastle.crypto.Digest; -import org.bouncycastle.crypto.digests.SHA256Digest; -import org.bouncycastle.crypto.digests.SHA384Digest; -import org.bouncycastle.crypto.digests.SHA512Digest; -import org.bouncycastle.crypto.engines.RSABlindedEngine; -import org.bouncycastle.crypto.params.ECDomainParameters; -import org.bouncycastle.crypto.params.Ed25519PublicKeyParameters; -import org.bouncycastle.crypto.params.Ed25519PrivateKeyParameters; -import org.bouncycastle.crypto.params.ECPrivateKeyParameters; -import org.bouncycastle.crypto.params.ECPublicKeyParameters; -import org.bouncycastle.crypto.params.ParametersWithRandom; -import org.bouncycastle.crypto.signers.ECDSASigner; -import org.bouncycastle.crypto.signers.Ed25519phSigner; -import org.bouncycastle.crypto.signers.PSSSigner; -import org.bouncycastle.crypto.signers.SM2Signer; -import org.bouncycastle.crypto.util.PrivateKeyFactory; -import org.bouncycastle.crypto.util.PublicKeyFactory; -import org.bouncycastle.jcajce.provider.asymmetric.ec.BCECPrivateKey; -import org.bouncycastle.jcajce.provider.asymmetric.ec.BCECPublicKey; -import org.bouncycastle.jcajce.provider.asymmetric.ec.KeyFactorySpi; -import org.bouncycastle.jcajce.provider.asymmetric.ec.KeyPairGeneratorSpi; -import org.bouncycastle.jcajce.provider.util.AsymmetricKeyInfoConverter; -import org.bouncycastle.jce.ECNamedCurveTable; -import org.bouncycastle.jce.spec.ECNamedCurveParameterSpec; import org.jboss.logging.Logger; import javax.crypto.Cipher; -import javax.crypto.Mac; import javax.crypto.spec.GCMParameterSpec; -import javax.crypto.spec.OAEPParameterSpec; -import javax.crypto.spec.PSource; import javax.crypto.spec.SecretKeySpec; -import java.io.ByteArrayOutputStream; -import java.io.IOException; -import java.math.BigInteger; import java.nio.ByteBuffer; import java.nio.charset.StandardCharsets; import java.security.*; -import java.security.interfaces.EdECPrivateKey; -import java.security.spec.ECGenParameterSpec; -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; -import static io.github.hectorvent.floci.services.kms.model.KmsMessageType.DIGEST; import static io.github.hectorvent.floci.services.kms.model.KmsMessageType.RAW; @ApplicationScoped @@ -102,6 +55,7 @@ public class KmsService implements ResourceProvider { private final StorageBackend grantStore; private final RegionResolver regionResolver; private final SecureRandom secureRandom; + private final KmsKeyTypes keyTypes; // Guards the check-generate-put sequence in ensureBackingKeyMaterial so two concurrent // first uses of the same legacy key cannot each mint a different backing key. private final Object backingKeyMaterialLock = new Object(); @@ -134,6 +88,7 @@ public class KmsService implements ResourceProvider { this.grantStore = grantStore; this.regionResolver = regionResolver; this.secureRandom = secureRandom; + this.keyTypes = new KmsKeyTypes(secureRandom); } public byte[] generateRandom(int numberOfBytes) { @@ -222,86 +177,13 @@ public class KmsService implements ResourceProvider { } private void generateKeyMaterial(KmsKey key, String region) { - KmsKeySpec spec = key.getKeySpec(); try { - switch (spec.getKeyType()) { - case HMAC -> { - // HMAC keys are symmetric byte strings; generate outside the try block - // so ValidationException (400) isn't rewrapped as InternalFailure (500). - byte[] material = new byte[hmacKeyByteLength(spec)]; - new SecureRandom().nextBytes(material); - key.setPrivateKeyEncoded(Base64.getEncoder().encodeToString(material)); - } - case SYMMETRIC -> generateBackingKey(key); - case RSA -> { - KeyPairGenerator generator = KeyPairGenerator.getInstance("RSA"); - int size = Integer.parseInt(spec.name().substring(4)); - generator.initialize(size); - KeyPair pair = generator.generateKeyPair(); - key.setPrivateKeyEncoded(Base64.getEncoder().encodeToString(pair.getPrivate().getEncoded())); - key.setPublicKeyEncoded(Base64.getEncoder().encodeToString(pair.getPublic().getEncoded())); - } - case ED25519 -> { - var pair = KeyPairGenerator.getInstance("Ed25519").generateKeyPair(); - key.setPrivateKeyEncoded(Base64.getEncoder().encodeToString(pair.getPrivate().getEncoded())); - key.setPublicKeyEncoded(Base64.getEncoder().encodeToString(pair.getPublic().getEncoded())); - } - case ML_DSA -> { - String algorithm = spec.name().replace('_', '-'); - var pair = KeyPairGenerator.getInstance(algorithm).generateKeyPair(); - key.setPrivateKeyEncoded(Base64.getEncoder().encodeToString(pair.getPrivate().getEncoded())); - key.setPublicKeyEncoded(Base64.getEncoder().encodeToString(pair.getPublic().getEncoded())); - } - case ECC -> { - String curveName = spec.curveName(); - - // For secp256k1 (ECC_SECG_P256K1), instantiate BC's SPI directly. - // JCA's ClassLoader.loadClass cannot find BC SPI classes in GraalVM native image - // unless they are allocated directly in code (GraalVM escape analysis eliminates - // unused allocations, keeping them out of the native image type registry). - KeyPairGenerator generator = isSecgP256k1(spec) - ? new KeyPairGeneratorSpi.EC() - : KeyPairGenerator.getInstance("EC"); - generator.initialize(new ECGenParameterSpec(curveName)); - KeyPair pair = generator.generateKeyPair(); - key.setPrivateKeyEncoded(Base64.getEncoder().encodeToString(pair.getPrivate().getEncoded())); - key.setPublicKeyEncoded(Base64.getEncoder().encodeToString(pair.getPublic().getEncoded())); - } - case SM2 -> { - if (!region.equals("cn-north-1") && !region.equals("cn-northwest-1")) { - throw new AwsException("UnsupportedOperationException", - "KeySpec SM2 is not supported in this Region", 400); - } - KeyPairGenerator generator = new KeyPairGeneratorSpi.EC(); - generator.initialize(new ECGenParameterSpec("sm2p256v1")); - KeyPair pair = generator.generateKeyPair(); - key.setPrivateKeyEncoded(Base64.getEncoder().encodeToString(pair.getPrivate().getEncoded())); - key.setPublicKeyEncoded(Base64.getEncoder().encodeToString(pair.getPublic().getEncoded())); - } - default -> - throw new AwsException("InvalidCustomerMasterKeySpecException", "Unsupported key spec: " + spec, 400); - } - } catch (NoSuchAlgorithmException | InvalidAlgorithmParameterException e) { + keyTypes.of(key.getKeySpec()).generateKeyMaterial(key, region); + } catch (GeneralSecurityException e) { throw new AwsException("InternalFailure", "Failed to generate key material: " + e.getMessage(), 500); } } - /** - * Mints a new AES-256 backing key for the AES-GCM ciphertext envelope and makes it the - * current one, keeping any earlier backing keys in the map so ciphertext produced under - * them keeps decrypting (AWS KMS never discards prior backing keys either). - */ - private void generateBackingKey(KmsKey key) { - byte[] material = new byte[AES_KEY_BYTES]; - SECURE_RANDOM.nextBytes(material); - String backingKeyId = UUID.randomUUID().toString(); - if (key.getBackingKeys() == null) { - key.setBackingKeys(new HashMap<>()); - } - key.getBackingKeys().put(backingKeyId, Base64.getEncoder().encodeToString(material)); - key.setCurrentBackingKeyId(backingKeyId); - } - /** * Self-healing for keys persisted before the AES-GCM envelope existed: their JSON has no * {@code backingKeys}/{@code currentBackingKeyId}, so the field defaults to an empty map on @@ -348,7 +230,7 @@ public class KmsService implements ResourceProvider { if (hasBackingKeyMaterial(current)) { return current; } - generateBackingKey(current); + keyTypes.symmetric().addBackingKey(current); keyStore.put(region + "::" + current.getKeyId(), current); LOG.infov("Generated backing key material for legacy KMS key: {0} in {1}", current.getKeyId(), region); return current; @@ -393,14 +275,6 @@ public class KmsService implements ResourceProvider { } } - private static int hmacKeyByteLength(KmsKeySpec spec) { - if (!isHmac(spec)) { - throw new AwsException("InvalidCustomerMasterKeySpecException", - "Unsupported HMAC key spec: " + spec, 400); - } - return spec.materialByteLength(); - } - public KmsKey describeKey(String keyId, String region) { return resolveKey(keyId, region); } @@ -846,8 +720,8 @@ public class KmsService implements ResourceProvider { } key.setOnDemandRotationCount(key.getOnDemandRotationCount() + 1); // AWS keeps prior backing keys after rotation so ciphertext encrypted under them keeps - // decrypting; generateBackingKey adds a new entry rather than replacing the map. - generateBackingKey(key); + // decrypting. + keyTypes.symmetric().addBackingKey(key); keyStore.put(region + "::" + key.getKeyId(), key); return key.getKeyId(); } @@ -1261,10 +1135,9 @@ public class KmsService implements ResourceProvider { private static final String AES_GCM_TRANSFORMATION = "AES/GCM/NoPadding"; private static final int GCM_IV_BYTES = 12; private static final int GCM_TAG_BITS = 128; - private static final int AES_KEY_BYTES = 32; + private static final int AES_KEY_BYTES = KmsKeySpec.SYMMETRIC_DEFAULT.materialByteLength(); private static final String BLOB_PREFIX_V2 = "kms:v2:"; private static final String BLOB_PREFIX_V1 = "kms:"; - private static final int SHA_512_DIGEST_BYTES = 64; private static final int MIN_MAC_MESSAGE_BYTES = 1; private static final int MAX_MAC_MESSAGE_BYTES = 4096; private static final int MIN_ENCRYPT_PLAINTEXT_BYTES = 1; @@ -1295,10 +1168,9 @@ public class KmsService implements ResourceProvider { validateKeyUsageForEncryptionOperation(kmsKey, operation); validateEncryptionAlgorithmForSpec(algorithm, kmsKey.getKeySpec()); - if (kmsKey.getKeySpec().getKeyType() == KmsKeySpec.KeyType.RSA) { + if (algorithm != KmsKeySpec.Algorithm.SYMMETRIC_DEFAULT) { rejectEncryptionContextForAsymmetricKey(encryptionContext); - validateRsaPlaintextLength(plaintext, algorithm, kmsKey.getKeySpec()); - byte[] ciphertext = rsaOaep(Cipher.ENCRYPT_MODE, kmsKey, algorithm, plaintext); + byte[] ciphertext = keyTypes.of(kmsKey.getKeySpec()).encrypt(kmsKey, algorithm, plaintext); return new EncryptResult(ciphertext, kmsKey.getArn(), algorithm.getAlgName()); } @@ -1363,7 +1235,7 @@ public class KmsService implements ResourceProvider { validateKeyUsageForEncryptionOperation(requestKey, "Decrypt"); validateEncryptionAlgorithmForSpec(algorithm, requestKey.getKeySpec()); rejectEncryptionContextForAsymmetricKey(encryptionContext); - byte[] plaintext = rsaOaep(Cipher.DECRYPT_MODE, requestKey, algorithm, ciphertext); + byte[] plaintext = keyTypes.of(requestKey.getKeySpec()).decrypt(requestKey, algorithm, ciphertext); return new DecryptResult(plaintext, requestKey.getArn(), algorithm.getAlgName()); } @@ -1705,105 +1577,14 @@ public class KmsService implements ResourceProvider { } } - /** RFC 8017 7.1.1: OAEP holds at most k - 2*hLen - 2 bytes, k being the modulus length. */ - private static void validateRsaPlaintextLength(byte[] plaintext, KmsKeySpec.Algorithm algorithm, KmsKeySpec spec) { - int modulusBytes = switch (spec) { - case RSA_2048 -> 256; - case RSA_3072 -> 384; - case RSA_4096 -> 512; - default -> throw new AwsException("InvalidKeyUsageException", - "Algorithm " + algorithm.getAlgName() + " is incompatible with key spec " + spec.name() + ".", 400); - }; - int digestBytes = algorithm == KmsKeySpec.Algorithm.RSAES_OAEP_SHA_1 ? 20 : 32; - int maxBytes = modulusBytes - 2 * digestBytes - 2; - if (plaintext.length > maxBytes) { - throw new AwsException("ValidationException", - "Algorithm " + algorithm.getAlgName() + " and key spec " + spec.name() - + " cannot encrypt data larger than " + maxBytes + " bytes.", 400); - } - } - - /** - * RSAES-OAEP with an explicit OAEPParameterSpec. The JDK's named OAEP transformations - * default MGF1 to SHA-1 whatever the main digest is, while KMS RSAES_OAEP_SHA_256 uses - * MGF1 over SHA-256, so the parameters are always spelled out. - */ - private byte[] rsaOaep(int mode, KmsKey key, KmsKeySpec.Algorithm algorithm, byte[] input) { - try { - var digest = algorithm == KmsKeySpec.Algorithm.RSAES_OAEP_SHA_1 ? "SHA-1" : "SHA-256"; - var cipher = Cipher.getInstance("RSA/ECB/OAEPPadding"); - var params = new OAEPParameterSpec(digest, "MGF1", new MGF1ParameterSpec(digest), - PSource.PSpecified.DEFAULT); - if (mode == Cipher.ENCRYPT_MODE) { - cipher.init(mode, loadPublicKey(key.getPublicKeyEncoded(), key.getKeySpec()), params); - } else { - cipher.init(mode, loadPrivateKey(key.getPrivateKeyEncoded(), key.getKeySpec()), params); - } - return cipher.doFinal(input); - } catch (Exception e) { - if (mode == Cipher.DECRYPT_MODE) { - // Real KMS answers any OAEP failure the same way: the padding check hides - // whether the bytes were garbage, the wrong length, or made for another key. - // The log line keeps broken key material or a missing cipher diagnosable. - LOG.debugv(e, "RSA OAEP decrypt failed for key {0}", key.getKeyId()); - throw new AwsException("InvalidCiphertextException", "The ciphertext is invalid.", 400); - } - LOG.warnv(e, "RSA OAEP encrypt failed for key {0}", key.getKeyId()); - throw new AwsException("InternalFailure", "Failed to encrypt: " + e.getMessage(), 500); - } - } - public byte[] sign(String keyId, byte[] message, String algorithm, String region) { return sign(keyId, message, algorithm, RAW, region); } public byte[] sign(String keyId, byte[] message, String algorithm, KmsMessageType messageType, String region) { KmsKey kmsKey = resolveKey(keyId, region); - if (KmsKeySpec.SYMMETRIC_DEFAULT == kmsKey.getKeySpec()) { - throw new AwsException("UnsupportedOperationException", "Unsupported key spec for signing.", 400); - } - - var ed25519 = kmsKey.getKeySpec().getKeyType() == KmsKeySpec.KeyType.ED25519; - var sm2 = kmsKey.getKeySpec().getKeyType() == KmsKeySpec.KeyType.SM2; - if (ed25519) { - validateEd25519Request(kmsKey.getKeySpec(), algorithm, messageType, message); - } - if (kmsKey.getKeySpec().getKeyType() == KmsKeySpec.KeyType.ML_DSA) { - validateMlDsaRequest(kmsKey.getKeySpec(), algorithm, messageType); - } - if (sm2) { - validateSm2Request(kmsKey.getKeySpec(), algorithm, messageType); - } - try { - PrivateKey privateKey = loadPrivateKey(kmsKey.getPrivateKeyEncoded(), kmsKey.getKeySpec()); - if (ed25519) { - return signEd25519(privateKey, message, algorithm); - } - if (sm2) { - return signSm2(privateKey, message); - } - if (messageType == DIGEST && isRsaPssRequest(kmsKey.getKeySpec(), algorithm)) { - return signRsaPssDigest(privateKey, message, algorithm); - } - String jcaAlgo = switch (messageType) { - // If message is already a digest, we need a "NONEwith..." algorithm - case DIGEST -> "NONEwith" + (kmsKey.getKeySpec().getKeyType() == KmsKeySpec.KeyType.RSA ? "RSA" : "ECDSA"); - case RAW -> KmsKeySpec.getSignVerifyAlgorithm(algorithm).getJavaName(); - }; - if (messageType == DIGEST && isPKCS1v1_5(kmsKey.getKeySpec().getAlgorithm().getFirst())) { - // RFC 8017 9.2: PKCS#1 v1.5 signs DigestInfo{hashOID, digest}, not the - // bare digest, so the signature validates with external verifiers and - // real KMS (NONEwithRSA only pads the bytes it is given). - message = wrapInDigestInfo(message, algorithm); - } - if (isSecgP256k1(kmsKey.getKeySpec())) { - return signSecgP256k1(privateKey, message, jcaAlgo); - } - var sig = signatureFor(jcaAlgo); - sig.initSign(privateKey); - sig.update(message); - return sig.sign(); + return keyTypes.of(kmsKey.getKeySpec()).sign(kmsKey, message, algorithm, messageType); } catch (AwsException e) { throw e; } catch (Exception e) { @@ -1817,49 +1598,8 @@ public class KmsService implements ResourceProvider { public boolean verify(String keyId, byte[] message, byte[] signature, String algorithm, KmsMessageType messageType, String region) { KmsKey kmsKey = resolveKey(keyId, region); - if (KmsKeySpec.SYMMETRIC_DEFAULT == kmsKey.getKeySpec()) { - return false; - } - - var ed25519 = kmsKey.getKeySpec().getKeyType() == KmsKeySpec.KeyType.ED25519; - var sm2 = kmsKey.getKeySpec().getKeyType() == KmsKeySpec.KeyType.SM2; - if (ed25519) { - validateEd25519Request(kmsKey.getKeySpec(), algorithm, messageType, message); - } - if (kmsKey.getKeySpec().getKeyType() == KmsKeySpec.KeyType.ML_DSA) { - validateMlDsaRequest(kmsKey.getKeySpec(), algorithm, messageType); - } - if (sm2) { - validateSm2Request(kmsKey.getKeySpec(), algorithm, messageType); - } - try { - PublicKey publicKey = loadPublicKey(kmsKey.getPublicKeyEncoded(), kmsKey.getKeySpec()); - if (ed25519) { - return verifyEd25519(publicKey, message, signature, algorithm); - } - if (sm2) { - return verifySm2(publicKey, message, signature); - } - String jcaAlgo = KmsKeySpec.getSignVerifyAlgorithm(algorithm).getJavaName(); - - if (DIGEST.equals(messageType)) { - if (isRsaPssRequest(kmsKey.getKeySpec(), algorithm)) { - return verifyRsaPssDigest(publicKey, message, signature, algorithm); - } - jcaAlgo = "NONEwith" + (kmsKey.getKeySpec().getKeyType() == KmsKeySpec.KeyType.RSA ? "RSA" : "ECDSA"); - if (isPKCS1v1_5(kmsKey.getKeySpec().getAlgorithm().getFirst())) { - // Mirror sign(): verify against DigestInfo{hashOID, digest} (RFC 8017 9.2). - message = wrapInDigestInfo(message, algorithm); - } - } - if (isSecgP256k1(kmsKey.getKeySpec())) { - return verifySecgP256k1(publicKey, message, signature, jcaAlgo); - } - var sig = signatureFor(jcaAlgo); - sig.initVerify(publicKey); - sig.update(message); - return sig.verify(signature); + return keyTypes.of(kmsKey.getKeySpec()).verify(kmsKey, message, signature, algorithm, messageType); } catch (AwsException e) { throw e; } catch (Exception e) { @@ -1882,12 +1622,7 @@ public class KmsService implements ResourceProvider { validateMacMessageLength(message); try { - byte[] keyBytes = Base64.getDecoder().decode(kmsKey.getPrivateKeyEncoded()); - String jcaAlgorithm = mapMacAlgorithm(algorithm); - Mac mac = Mac.getInstance(jcaAlgorithm); - mac.init(new SecretKeySpec(keyBytes, jcaAlgorithm)); - mac.update(message); - return mac.doFinal(); + return keyTypes.of(kmsKey.getKeySpec()).generateMac(kmsKey, message, algorithm); } catch (AwsException e) { throw e; } catch (Exception e) { @@ -1932,16 +1667,6 @@ public class KmsService implements ResourceProvider { return kmsKey; } - private String mapMacAlgorithm(String awsAlgo) { - return switch (awsAlgo) { - case "HMAC_SHA_224" -> "HmacSHA224"; - case "HMAC_SHA_256" -> "HmacSHA256"; - case "HMAC_SHA_384" -> "HmacSHA384"; - case "HMAC_SHA_512" -> "HmacSHA512"; - default -> throw new AwsException("InvalidMacAlgorithmException", "Unsupported MAC algorithm: " + awsAlgo, 400); - }; - } - private static void validateMacMessageLength(byte[] message) { int length = message == null ? 0 : message.length; if (length < MIN_MAC_MESSAGE_BYTES || length > MAX_MAC_MESSAGE_BYTES) { @@ -1958,50 +1683,6 @@ public class KmsService implements ResourceProvider { } } - private PrivateKey loadPrivateKey(String encoded, KmsKeySpec spec) throws Exception { - byte[] decoded = Base64.getDecoder().decode(encoded); - if (isSecgP256k1(spec) || isSm2(spec)) { - // For secp256k1, use BC's KeyFactorySpi.EC directly as AsymmetricKeyInfoConverter. - // This bypasses JCA and ClassLoader.loadClass; the allocation is live (generatePrivate - // is called), so GraalVM's escape analysis keeps the class in the native image. - AsymmetricKeyInfoConverter converter = new KeyFactorySpi.EC(); - return converter.generatePrivate(PrivateKeyInfo.getInstance(decoded)); - } - return buildKeyFactory(spec).generatePrivate(new PKCS8EncodedKeySpec(decoded)); - } - - private PublicKey loadPublicKey(String encoded, KmsKeySpec spec) throws Exception { - byte[] decoded = Base64.getDecoder().decode(encoded); - if (isSecgP256k1(spec) || isSm2(spec)) { - AsymmetricKeyInfoConverter converter = new KeyFactorySpi.EC(); - return converter.generatePublic(SubjectPublicKeyInfo.getInstance(decoded)); - } - return buildKeyFactory(spec).generatePublic(new X509EncodedKeySpec(decoded)); - } - - /** - * Wraps a pre-computed digest in the ASN.1 {@code DigestInfo} structure that PKCS#1 - * v1.5 signing prepends before padding (RFC 8017 9.2). Needed for {@code MessageType=DIGEST} - * RSA signatures because {@code NONEwithRSA} pads only the bytes it is given. - */ - private byte[] wrapInDigestInfo(byte[] digest, String algorithm) { - ASN1ObjectIdentifier hashOid; - if (algorithm.endsWith("SHA_256")) { - hashOid = NISTObjectIdentifiers.id_sha256; - } else if (algorithm.endsWith("SHA_384")) { - hashOid = NISTObjectIdentifiers.id_sha384; - } else if (algorithm.endsWith("SHA_512")) { - hashOid = NISTObjectIdentifiers.id_sha512; - } else { - throw new AwsException("InvalidSigningAlgorithmException", "Unsupported algorithm: " + algorithm, 400); - } - try { - return new DigestInfo(new AlgorithmIdentifier(hashOid, DERNull.INSTANCE), digest).getEncoded(); - } catch (IOException e) { - throw new AwsException("InternalFailure", "Failed to encode DigestInfo: " + e.getMessage(), 500); - } - } - public Map generateDataKey(String keyId, String keySpec, int numberOfBytes, String region) { return generateDataKey(keyId, keySpec, numberOfBytes, Map.of(), region); } @@ -2040,273 +1721,6 @@ public class KmsService implements ResourceProvider { // ──────────────────────────── Helpers ──────────────────────────── - private static boolean isSecgP256k1(KmsKeySpec spec) { - return KmsKeySpec.ECC_SECG_P256K1 == spec; - } - - private static boolean isSm2(KmsKeySpec spec) { - return KmsKeySpec.SM2 == spec; - } - - private static void validateSm2Request(KmsKeySpec spec, String algorithm, KmsMessageType messageType) { - if (KmsKeySpec.getSignVerifyAlgorithm(algorithm) != KmsKeySpec.Algorithm.SM2DSA) { - throw new AwsException("InvalidKeyUsageException", - "Algorithm " + algorithm + " is incompatible with key spec " + spec.name() + ".", 400); - } - if (messageType != RAW) { - throw new AwsException("ValidationException", - "Message type " + messageType + " is incompatible with key spec " + spec.name() + ".", 400); - } - } - - private static byte[] signSm2(PrivateKey privateKey, byte[] message) throws Exception { - ECNamedCurveParameterSpec spec = ECNamedCurveTable.getParameterSpec("sm2p256v1"); - ECDomainParameters domain = new ECDomainParameters(spec.getCurve(), spec.getG(), spec.getN(), spec.getH()); - ECPrivateKeyParameters parameters = new ECPrivateKeyParameters(((BCECPrivateKey) privateKey).getD(), domain); - - var signer = new SM2Signer(); - signer.init(true, new ParametersWithRandom(parameters, SECURE_RANDOM)); - signer.update(message, 0, message.length); - return signer.generateSignature(); - } - - private static boolean verifySm2(PublicKey publicKey, byte[] message, byte[] signature) { - ECNamedCurveParameterSpec spec = ECNamedCurveTable.getParameterSpec("sm2p256v1"); - ECDomainParameters domain = new ECDomainParameters(spec.getCurve(), spec.getG(), spec.getN(), spec.getH()); - ECPublicKeyParameters parameters = new ECPublicKeyParameters(((BCECPublicKey) publicKey).getQ(), domain); - - var verifier = new SM2Signer(); - verifier.init(false, parameters); - verifier.update(message, 0, message.length); - return verifier.verifySignature(signature); - } - - /** - * Validates what an Ed25519 key accepts, matching the errors real KMS returns. - * - *

ED25519_SHA_512 only takes {@code MessageType=RAW} and ED25519_PH_SHA_512 only takes - * {@code MessageType=DIGEST}, whose value has to be exactly one SHA-512 digest. Real KMS - * rejects the other pairing and a wrong digest length with a ValidationException, and rejects - * any other signing algorithm with an InvalidKeyUsageException. Sign and Verify both enforce - * all three. - */ - private static void validateEd25519Request(KmsKeySpec spec, String algorithm, KmsMessageType messageType, - byte[] message) { - var algo = KmsKeySpec.getSignVerifyAlgorithm(algorithm); - if (algo != KmsKeySpec.Algorithm.ED25519_SHA_512 && algo != KmsKeySpec.Algorithm.ED25519_PH_SHA_512) { - throw new AwsException("InvalidKeyUsageException", - "Algorithm " + algorithm + " is incompatible with key spec " + spec.name() + ".", 400); - } - var required = algo == KmsKeySpec.Algorithm.ED25519_SHA_512 ? KmsMessageType.RAW : KmsMessageType.DIGEST; - if (messageType != required) { - throw new AwsException("ValidationException", - "Message type " + messageType + " is incompatible with algorithm " + algorithm + ".", 400); - } - if (algo == KmsKeySpec.Algorithm.ED25519_PH_SHA_512 && message.length != SHA_512_DIGEST_BYTES) { - throw new AwsException("ValidationException", - "Digest is invalid length for algorithm " + algorithm + ".", 400); - } - } - - private static void validateMlDsaRequest(KmsKeySpec spec, String algorithm, KmsMessageType messageType) { - var signingAlgorithm = KmsKeySpec.getSignVerifyAlgorithm(algorithm); - if (signingAlgorithm != KmsKeySpec.Algorithm.ML_DSA_SHAKE_256) { - throw new AwsException("InvalidKeyUsageException", - "Algorithm " + algorithm + " is incompatible with key spec " + spec.name() + ".", 400); - } - if (messageType != RAW) { - throw new AwsException("ValidationException", - "Message type " + messageType + " is incompatible with key spec " + spec.name() + ".", 400); - } - } - - /** - * Signs with an Ed25519 key. - * - *

ED25519_SHA_512 is pure Ed25519 over the message. ED25519_PH_SHA_512 is RFC 8032 - * Ed25519ph, and real KMS applies the SHA-512 pre-hash to the bytes the caller sends rather - * than treating them as an already computed digest. That is measurably different from - * MessageType=DIGEST on RSA and ECDSA keys, where the bytes are signed as they arrive. - * - *

The JDK has no Ed25519ph, so that branch uses BouncyCastle's lightweight signer, - * instantiated directly rather than resolved through a JCA provider, the same way the - * secp256k1 path does. - */ - private static byte[] signEd25519(PrivateKey privateKey, byte[] message, String algorithm) throws Exception { - if (KmsKeySpec.Algorithm.ED25519_SHA_512.name().equals(algorithm)) { - var signature = Signature.getInstance("Ed25519"); - signature.initSign(privateKey); - signature.update(message); - return signature.sign(); - } - var signer = new Ed25519phSigner(new byte[0]); - signer.init(true, new Ed25519PrivateKeyParameters(ed25519Seed(privateKey), 0)); - signer.update(message, 0, message.length); - return signer.generateSignature(); - } - - private static boolean verifyEd25519(PublicKey publicKey, byte[] message, byte[] signature, String algorithm) - throws Exception { - if (KmsKeySpec.Algorithm.ED25519_SHA_512.name().equals(algorithm)) { - var verifier = Signature.getInstance("Ed25519"); - verifier.initVerify(publicKey); - verifier.update(message); - return verifier.verify(signature); - } - var verifier = new Ed25519phSigner(new byte[0]); - verifier.init(false, new Ed25519PublicKeyParameters(ed25519Point(publicKey), 0)); - verifier.update(message, 0, message.length); - return verifier.verifySignature(signature); - } - - private static byte[] ed25519Seed(PrivateKey privateKey) throws InvalidKeyException { - if (privateKey instanceof EdECPrivateKey edEC) { - return edEC.getBytes().orElseThrow(() -> new InvalidKeyException("Ed25519 private key is not extractable")); - } - throw new InvalidKeyException("Expected an Ed25519 private key but got " + privateKey.getAlgorithm()); - } - - private static byte[] ed25519Point(PublicKey publicKey) { - return SubjectPublicKeyInfo.getInstance(publicKey.getEncoded()).getPublicKeyData().getBytes(); - } - - /** - * Builds the {@link Signature} for a KMS signing algorithm. - * - *

BouncyCastle names PSS signatures {@code SHAnnnwithRSA/PSS}, and only its provider - * answers to that name. The JDK exposes one {@code RSASSA-PSS} Signature whose digest, - * mask generation function and salt length come from a parameter spec instead. AWS KMS - * RSASSA_PSS_SHA_nnn uses MGF1 over the same digest with a salt as long as that digest, - * which is what BouncyCastle's alias defaults to, so a signature made either way verifies - * against the other. Every other name Floci asks for is a standard JCA name. - */ - private static Signature signatureFor(String jcaAlgorithm) throws GeneralSecurityException { - if (!jcaAlgorithm.endsWith("withRSA/PSS")) { - return Signature.getInstance(jcaAlgorithm); - } - var digest = "SHA-" + jcaAlgorithm.substring("SHA".length(), jcaAlgorithm.indexOf("with")); - var maskGeneration = switch (digest) { - case "SHA-256" -> MGF1ParameterSpec.SHA256; - case "SHA-384" -> MGF1ParameterSpec.SHA384; - case "SHA-512" -> MGF1ParameterSpec.SHA512; - default -> throw new NoSuchAlgorithmException("Unsupported PSS digest: " + digest); - }; - var saltLength = MessageDigest.getInstance(digest).getDigestLength(); - var signature = Signature.getInstance("RSASSA-PSS"); - signature.setParameter(new PSSParameterSpec(digest, "MGF1", maskGeneration, saltLength, 1)); - return signature; - } - - private static boolean isRsaPssRequest(KmsKeySpec spec, String algorithm) { - return spec.getKeyType() == KmsKeySpec.KeyType.RSA && algorithm.startsWith("RSASSA_PSS"); - } - - /** - * Signs a pre-computed digest with RSASSA-PSS. - * - *

Real KMS applies the PSS encoding directly to the digest a {@code MessageType=DIGEST} - * caller sends. The JDK's {@code RSASSA-PSS} Signature always hashes its input first, so - * this uses BouncyCastle's lightweight raw PSS signer, instantiated directly like the - * secp256k1 and Ed25519ph paths. The raw signer defaults to MGF1 over the same digest with - * a salt as long as that digest, matching {@link #signatureFor(String)}, so RAW and DIGEST - * signatures verify against each other. - */ - private static byte[] signRsaPssDigest(PrivateKey privateKey, byte[] digest, String algorithm) throws Exception { - var signer = rawPssSigner(algorithm); - signer.init(true, new ParametersWithRandom(PrivateKeyFactory.createKey(privateKey.getEncoded()), SECURE_RANDOM)); - signer.update(digest, 0, digest.length); - return signer.generateSignature(); - } - - private static boolean verifyRsaPssDigest(PublicKey publicKey, byte[] digest, byte[] signature, String algorithm) throws Exception { - var signer = rawPssSigner(algorithm); - signer.init(false, PublicKeyFactory.createKey(publicKey.getEncoded())); - signer.update(digest, 0, digest.length); - return signer.verifySignature(signature); - } - - private static PSSSigner rawPssSigner(String algorithm) { - Digest digest = switch (algorithm) { - case "RSASSA_PSS_SHA_256" -> new SHA256Digest(); - case "RSASSA_PSS_SHA_384" -> new SHA384Digest(); - case "RSASSA_PSS_SHA_512" -> new SHA512Digest(); - default -> throw new AwsException("InvalidSigningAlgorithmException", "Unsupported algorithm: " + algorithm, 400); - }; - return PSSSigner.createRawSigner(new RSABlindedEngine(), digest); - } - - private static boolean isPKCS1v1_5(KmsKeySpec.Algorithm spec) { - return spec == KmsKeySpec.Algorithm.RSASSA_PKCS1_V1_5_SHA_256 - || spec == KmsKeySpec.Algorithm.RSASSA_PKCS1_V1_5_SHA_384 - || spec == KmsKeySpec.Algorithm.RSASSA_PKCS1_V1_5_SHA_512; - } - - /** - * Signs {@code message} with secp256k1 using BC's lightweight {@link ECDSASigner}. - * - *

BC's {@code SignatureSpi} subclasses extend {@code java.security.SignatureSpi} (not - * {@code java.security.Signature}), so they cannot be used as a drop-in {@code Signature}. - * Using the lightweight API avoids JCA's {@code ClassLoader.loadClass} entirely — every - * class referenced here is directly allocated in reachable code and is always in GraalVM's - * native image type registry.

- */ - private static byte[] signSecgP256k1(PrivateKey privateKey, byte[] message, String jcaAlgo) throws Exception { - ECNamedCurveParameterSpec spec = ECNamedCurveTable.getParameterSpec("secp256k1"); - ECDomainParameters domain = new ECDomainParameters(spec.getCurve(), spec.getG(), spec.getN(), spec.getH()); - ECPrivateKeyParameters privParams = new ECPrivateKeyParameters(((BCECPrivateKey) privateKey).getD(), domain); - - byte[] hash = "NONEwithECDSA".equals(jcaAlgo) ? message : hashForEcdsa(message, jcaAlgo); - - ECDSASigner signer = new ECDSASigner(); - signer.init(true, new ParametersWithRandom(privParams, new SecureRandom())); - BigInteger[] rs = signer.generateSignature(hash); - - ByteArrayOutputStream bOut = new ByteArrayOutputStream(); - DERSequenceGenerator seq = new DERSequenceGenerator(bOut); - seq.addObject(new ASN1Integer(rs[0])); - seq.addObject(new ASN1Integer(rs[1])); - seq.close(); - return bOut.toByteArray(); - } - - /** - * Verifies a DER-encoded ECDSA signature over secp256k1. - */ - private static boolean verifySecgP256k1(PublicKey publicKey, byte[] message, byte[] signature, String jcaAlgo) throws Exception { - ECNamedCurveParameterSpec spec = ECNamedCurveTable.getParameterSpec("secp256k1"); - ECDomainParameters domain = new ECDomainParameters(spec.getCurve(), spec.getG(), spec.getN(), spec.getH()); - ECPublicKeyParameters pubParams = new ECPublicKeyParameters(((BCECPublicKey) publicKey).getQ(), domain); - - byte[] hash = "NONEwithECDSA".equals(jcaAlgo) ? message : hashForEcdsa(message, jcaAlgo); - - ASN1Sequence asn1 = ASN1Sequence.getInstance(ASN1Primitive.fromByteArray(signature)); - BigInteger r = ASN1Integer.getInstance(asn1.getObjectAt(0)).getValue(); - BigInteger s = ASN1Integer.getInstance(asn1.getObjectAt(1)).getValue(); - - ECDSASigner verifier = new ECDSASigner(); - verifier.init(false, pubParams); - return verifier.verifySignature(hash, r, s); - } - - private static byte[] hashForEcdsa(byte[] message, String jcaAlgo) throws Exception { - String mdAlgo = switch (jcaAlgo) { - case "SHA256withECDSA" -> "SHA-256"; - case "SHA384withECDSA" -> "SHA-384"; - case "SHA512withECDSA" -> "SHA-512"; - default -> throw new AwsException("InvalidSigningAlgorithmException", "Unsupported EC algorithm: " + jcaAlgo, 400); - }; - return MessageDigest.getInstance(mdAlgo).digest(message); - } - - private static KeyFactory buildKeyFactory(KmsKeySpec spec) throws Exception { - return KeyFactory.getInstance(switch (spec.getKeyType()) { - case RSA -> "RSA"; - case ED25519 -> "Ed25519"; - case ML_DSA -> "ML-DSA"; - default -> "EC"; - }); - } - private KmsKey resolveKey(String keyIdOrArn, String region) { String id = keyIdOrArn; // Alias arn diff --git a/src/main/java/io/github/hectorvent/floci/services/kms/keytype/AsymmetricKeys.java b/src/main/java/io/github/hectorvent/floci/services/kms/keytype/AsymmetricKeys.java new file mode 100644 index 0000000000..ebbfb16aff --- /dev/null +++ b/src/main/java/io/github/hectorvent/floci/services/kms/keytype/AsymmetricKeys.java @@ -0,0 +1,90 @@ +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; +import io.github.hectorvent.floci.services.kms.model.KmsMessageType; + +import java.security.GeneralSecurityException; +import java.security.KeyFactory; +import java.security.KeyPair; +import java.security.MessageDigest; +import java.security.NoSuchAlgorithmException; +import java.security.PrivateKey; +import java.security.PublicKey; +import java.security.Signature; +import java.security.spec.MGF1ParameterSpec; +import java.security.spec.PKCS8EncodedKeySpec; +import java.security.spec.PSSParameterSpec; +import java.security.spec.X509EncodedKeySpec; +import java.util.Base64; + +final class AsymmetricKeys { + + private AsymmetricKeys() { + } + + static KmsKeySpec.Algorithm requireSpecAlgorithm(KmsKeySpec spec, String algorithm) { + KmsKeySpec.Algorithm signingAlgorithm = KmsKeySpec.getSignVerifyAlgorithm(algorithm); + if (!spec.getAlgorithm().contains(signingAlgorithm)) { + throw new AwsException("InvalidKeyUsageException", + "Algorithm " + algorithm + " is incompatible with key spec " + spec.name() + ".", 400); + } + return signingAlgorithm; + } + + static void requireRawMessage(KmsKeySpec spec, KmsMessageType messageType) { + if (messageType != KmsMessageType.RAW) { + throw new AwsException("ValidationException", + "Message type " + messageType + " is incompatible with key spec " + spec.name() + ".", 400); + } + } + + static void store(KmsKey key, KeyPair pair) { + key.setPrivateKeyEncoded(Base64.getEncoder().encodeToString(pair.getPrivate().getEncoded())); + key.setPublicKeyEncoded(Base64.getEncoder().encodeToString(pair.getPublic().getEncoded())); + } + + static PrivateKey privateKey(KmsKey key, String keyAlgorithm) throws GeneralSecurityException { + byte[] decoded = Base64.getDecoder().decode(key.getPrivateKeyEncoded()); + return KeyFactory.getInstance(keyAlgorithm).generatePrivate(new PKCS8EncodedKeySpec(decoded)); + } + + static PublicKey publicKey(KmsKey key, String keyAlgorithm) throws GeneralSecurityException { + byte[] decoded = Base64.getDecoder().decode(key.getPublicKeyEncoded()); + return KeyFactory.getInstance(keyAlgorithm).generatePublic(new X509EncodedKeySpec(decoded)); + } + + static byte[] sign(PrivateKey privateKey, String jcaAlgorithm, byte[] message) throws GeneralSecurityException { + Signature signature = signatureFor(jcaAlgorithm); + signature.initSign(privateKey); + signature.update(message); + return signature.sign(); + } + + static boolean verify(PublicKey publicKey, String jcaAlgorithm, byte[] message, byte[] signature) + throws GeneralSecurityException { + Signature verifier = signatureFor(jcaAlgorithm); + verifier.initVerify(publicKey); + verifier.update(message); + return verifier.verify(signature); + } + + // KMS RSASSA_PSS uses MGF1 over the same digest, with a salt as long as the digest. + private static Signature signatureFor(String jcaAlgorithm) throws GeneralSecurityException { + if (!jcaAlgorithm.endsWith("withRSA/PSS")) { + return Signature.getInstance(jcaAlgorithm); + } + String digest = "SHA-" + jcaAlgorithm.substring("SHA".length(), jcaAlgorithm.indexOf("with")); + MGF1ParameterSpec maskGeneration = switch (digest) { + case "SHA-256" -> MGF1ParameterSpec.SHA256; + case "SHA-384" -> MGF1ParameterSpec.SHA384; + case "SHA-512" -> MGF1ParameterSpec.SHA512; + default -> throw new NoSuchAlgorithmException("Unsupported PSS digest: " + digest); + }; + int saltLength = MessageDigest.getInstance(digest).getDigestLength(); + Signature signature = Signature.getInstance("RSASSA-PSS"); + signature.setParameter(new PSSParameterSpec(digest, "MGF1", maskGeneration, saltLength, 1)); + return signature; + } +} diff --git a/src/main/java/io/github/hectorvent/floci/services/kms/keytype/BcEcKeys.java b/src/main/java/io/github/hectorvent/floci/services/kms/keytype/BcEcKeys.java new file mode 100644 index 0000000000..921ce56ca1 --- /dev/null +++ b/src/main/java/io/github/hectorvent/floci/services/kms/keytype/BcEcKeys.java @@ -0,0 +1,54 @@ +package io.github.hectorvent.floci.services.kms.keytype; + +import io.github.hectorvent.floci.services.kms.model.KmsKey; +import org.bouncycastle.asn1.pkcs.PrivateKeyInfo; +import org.bouncycastle.asn1.x509.SubjectPublicKeyInfo; +import org.bouncycastle.crypto.params.ECDomainParameters; +import org.bouncycastle.crypto.params.ECPrivateKeyParameters; +import org.bouncycastle.crypto.params.ECPublicKeyParameters; +import org.bouncycastle.jcajce.provider.asymmetric.ec.BCECPrivateKey; +import org.bouncycastle.jcajce.provider.asymmetric.ec.BCECPublicKey; +import org.bouncycastle.jcajce.provider.asymmetric.ec.KeyFactorySpi; +import org.bouncycastle.jcajce.provider.asymmetric.ec.KeyPairGeneratorSpi; +import org.bouncycastle.jcajce.provider.util.AsymmetricKeyInfoConverter; +import org.bouncycastle.jce.ECNamedCurveTable; +import org.bouncycastle.jce.spec.ECNamedCurveParameterSpec; + +import java.io.IOException; +import java.security.InvalidAlgorithmParameterException; +import java.security.KeyPair; +import java.security.KeyPairGenerator; +import java.security.spec.ECGenParameterSpec; +import java.util.Base64; + +// The BouncyCastle SPI classes are allocated directly. JCA lookup cannot find them in the native image. +final class BcEcKeys { + + private BcEcKeys() { + } + + static KeyPair generateKeyPair(String curveName) throws InvalidAlgorithmParameterException { + KeyPairGenerator generator = new KeyPairGeneratorSpi.EC(); + generator.initialize(new ECGenParameterSpec(curveName)); + return generator.generateKeyPair(); + } + + static ECPrivateKeyParameters privateKeyParameters(KmsKey key, String curveName) throws IOException { + AsymmetricKeyInfoConverter converter = new KeyFactorySpi.EC(); + byte[] decoded = Base64.getDecoder().decode(key.getPrivateKeyEncoded()); + BCECPrivateKey privateKey = (BCECPrivateKey) converter.generatePrivate(PrivateKeyInfo.getInstance(decoded)); + return new ECPrivateKeyParameters(privateKey.getD(), domainParameters(curveName)); + } + + static ECPublicKeyParameters publicKeyParameters(KmsKey key, String curveName) throws IOException { + AsymmetricKeyInfoConverter converter = new KeyFactorySpi.EC(); + byte[] decoded = Base64.getDecoder().decode(key.getPublicKeyEncoded()); + BCECPublicKey publicKey = (BCECPublicKey) converter.generatePublic(SubjectPublicKeyInfo.getInstance(decoded)); + return new ECPublicKeyParameters(publicKey.getQ(), domainParameters(curveName)); + } + + private static ECDomainParameters domainParameters(String curveName) { + ECNamedCurveParameterSpec spec = ECNamedCurveTable.getParameterSpec(curveName); + return new ECDomainParameters(spec.getCurve(), spec.getG(), spec.getN(), spec.getH()); + } +} diff --git a/src/main/java/io/github/hectorvent/floci/services/kms/keytype/EccNistKeyType.java b/src/main/java/io/github/hectorvent/floci/services/kms/keytype/EccNistKeyType.java new file mode 100644 index 0000000000..5f96689cac --- /dev/null +++ b/src/main/java/io/github/hectorvent/floci/services/kms/keytype/EccNistKeyType.java @@ -0,0 +1,45 @@ +package io.github.hectorvent.floci.services.kms.keytype; + +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; + +import java.security.GeneralSecurityException; +import java.security.KeyPairGenerator; +import java.security.PrivateKey; +import java.security.PublicKey; +import java.security.spec.ECGenParameterSpec; + +final class EccNistKeyType implements KmsKeyType { + + @Override + public void generateKeyMaterial(KmsKey key, String region) throws GeneralSecurityException { + KeyPairGenerator generator = KeyPairGenerator.getInstance("EC"); + generator.initialize(new ECGenParameterSpec(key.getKeySpec().curveName())); + AsymmetricKeys.store(key, generator.generateKeyPair()); + } + + @Override + public byte[] sign(KmsKey key, byte[] message, String algorithm, KmsMessageType messageType) + throws GeneralSecurityException { + PrivateKey privateKey = AsymmetricKeys.privateKey(key, "EC"); + String jcaAlgorithm = switch (messageType) { + case DIGEST -> "NONEwithECDSA"; + case RAW -> KmsKeySpec.getSignVerifyAlgorithm(algorithm).getJavaName(); + }; + return AsymmetricKeys.sign(privateKey, jcaAlgorithm, message); + } + + @Override + public boolean verify(KmsKey key, byte[] message, byte[] signature, String algorithm, + KmsMessageType messageType) throws GeneralSecurityException { + PublicKey publicKey = AsymmetricKeys.publicKey(key, "EC"); + // An unknown algorithm name fails even for DIGEST. + KmsKeySpec.Algorithm signingAlgorithm = KmsKeySpec.getSignVerifyAlgorithm(algorithm); + String jcaAlgorithm = switch (messageType) { + case DIGEST -> "NONEwithECDSA"; + case RAW -> signingAlgorithm.getJavaName(); + }; + return AsymmetricKeys.verify(publicKey, jcaAlgorithm, message, signature); + } +} diff --git a/src/main/java/io/github/hectorvent/floci/services/kms/keytype/EccSecgP256k1KeyType.java b/src/main/java/io/github/hectorvent/floci/services/kms/keytype/EccSecgP256k1KeyType.java new file mode 100644 index 0000000000..212c192b3b --- /dev/null +++ b/src/main/java/io/github/hectorvent/floci/services/kms/keytype/EccSecgP256k1KeyType.java @@ -0,0 +1,90 @@ +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; +import io.github.hectorvent.floci.services.kms.model.KmsMessageType; +import org.bouncycastle.asn1.ASN1Integer; +import org.bouncycastle.asn1.ASN1Primitive; +import org.bouncycastle.asn1.ASN1Sequence; +import org.bouncycastle.asn1.DERSequenceGenerator; +import org.bouncycastle.crypto.params.ECPrivateKeyParameters; +import org.bouncycastle.crypto.params.ECPublicKeyParameters; +import org.bouncycastle.crypto.params.ParametersWithRandom; +import org.bouncycastle.crypto.signers.ECDSASigner; + +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.math.BigInteger; +import java.security.GeneralSecurityException; +import java.security.MessageDigest; +import java.security.SecureRandom; + +final class EccSecgP256k1KeyType implements KmsKeyType { + + private static final String CURVE = "secp256k1"; + + private final SecureRandom random; + + EccSecgP256k1KeyType(SecureRandom random) { + this.random = random; + } + + @Override + public void generateKeyMaterial(KmsKey key, String region) throws GeneralSecurityException { + AsymmetricKeys.store(key, BcEcKeys.generateKeyPair(CURVE)); + } + + @Override + public byte[] sign(KmsKey key, byte[] message, String algorithm, KmsMessageType messageType) + throws GeneralSecurityException, IOException { + ECPrivateKeyParameters privateKey = BcEcKeys.privateKeyParameters(key, CURVE); + byte[] hash = switch (messageType) { + case DIGEST -> message; + case RAW -> hashForEcdsa(message, KmsKeySpec.getSignVerifyAlgorithm(algorithm)); + }; + + ECDSASigner signer = new ECDSASigner(); + signer.init(true, new ParametersWithRandom(privateKey, random)); + BigInteger[] rs = signer.generateSignature(hash); + + ByteArrayOutputStream bOut = new ByteArrayOutputStream(); + DERSequenceGenerator seq = new DERSequenceGenerator(bOut); + seq.addObject(new ASN1Integer(rs[0])); + seq.addObject(new ASN1Integer(rs[1])); + seq.close(); + return bOut.toByteArray(); + } + + @Override + public boolean verify(KmsKey key, byte[] message, byte[] signature, String algorithm, + KmsMessageType messageType) throws GeneralSecurityException, IOException { + ECPublicKeyParameters publicKey = BcEcKeys.publicKeyParameters(key, CURVE); + // An unknown algorithm name fails even for DIGEST. + KmsKeySpec.Algorithm signingAlgorithm = KmsKeySpec.getSignVerifyAlgorithm(algorithm); + byte[] hash = switch (messageType) { + case DIGEST -> message; + case RAW -> hashForEcdsa(message, signingAlgorithm); + }; + + ASN1Sequence asn1 = ASN1Sequence.getInstance(ASN1Primitive.fromByteArray(signature)); + BigInteger r = ASN1Integer.getInstance(asn1.getObjectAt(0)).getValue(); + BigInteger s = ASN1Integer.getInstance(asn1.getObjectAt(1)).getValue(); + + ECDSASigner verifier = new ECDSASigner(); + verifier.init(false, publicKey); + return verifier.verifySignature(hash, r, s); + } + + private static byte[] hashForEcdsa(byte[] message, KmsKeySpec.Algorithm algorithm) + throws GeneralSecurityException { + String digest = switch (algorithm) { + case ECDSA_SHA_256 -> "SHA-256"; + case ECDSA_SHA_384 -> "SHA-384"; + case ECDSA_SHA_512 -> "SHA-512"; + default -> throw new AwsException("InvalidSigningAlgorithmException", + "Unsupported EC algorithm: " + algorithm.getJavaName(), 400); + }; + return MessageDigest.getInstance(digest).digest(message); + } +} diff --git a/src/main/java/io/github/hectorvent/floci/services/kms/keytype/Ed25519KeyType.java b/src/main/java/io/github/hectorvent/floci/services/kms/keytype/Ed25519KeyType.java new file mode 100644 index 0000000000..19678d28bf --- /dev/null +++ b/src/main/java/io/github/hectorvent/floci/services/kms/keytype/Ed25519KeyType.java @@ -0,0 +1,83 @@ +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; +import io.github.hectorvent.floci.services.kms.model.KmsMessageType; +import org.bouncycastle.asn1.x509.SubjectPublicKeyInfo; +import org.bouncycastle.crypto.CryptoException; +import org.bouncycastle.crypto.params.Ed25519PrivateKeyParameters; +import org.bouncycastle.crypto.params.Ed25519PublicKeyParameters; +import org.bouncycastle.crypto.signers.Ed25519phSigner; + +import java.security.GeneralSecurityException; +import java.security.InvalidKeyException; +import java.security.KeyPairGenerator; +import java.security.PrivateKey; +import java.security.PublicKey; +import java.security.interfaces.EdECPrivateKey; + +final class Ed25519KeyType implements KmsKeyType { + + private static final int SHA_512_DIGEST_BYTES = 64; + + @Override + public void generateKeyMaterial(KmsKey key, String region) throws GeneralSecurityException { + AsymmetricKeys.store(key, KeyPairGenerator.getInstance("Ed25519").generateKeyPair()); + } + + // KMS runs Ed25519ph over the digest the caller sends, so the digest is hashed again. + @Override + public byte[] sign(KmsKey key, byte[] message, String algorithm, KmsMessageType messageType) + throws GeneralSecurityException, CryptoException { + KmsKeySpec.Algorithm signingAlgorithm = validateRequest(key.getKeySpec(), algorithm, messageType, message); + PrivateKey privateKey = AsymmetricKeys.privateKey(key, "Ed25519"); + if (signingAlgorithm == KmsKeySpec.Algorithm.ED25519_SHA_512) { + return AsymmetricKeys.sign(privateKey, "Ed25519", message); + } + Ed25519phSigner signer = new Ed25519phSigner(new byte[0]); + signer.init(true, new Ed25519PrivateKeyParameters(seed(privateKey), 0)); + signer.update(message, 0, message.length); + return signer.generateSignature(); + } + + @Override + public boolean verify(KmsKey key, byte[] message, byte[] signature, String algorithm, + KmsMessageType messageType) throws GeneralSecurityException { + KmsKeySpec.Algorithm signingAlgorithm = validateRequest(key.getKeySpec(), algorithm, messageType, message); + PublicKey publicKey = AsymmetricKeys.publicKey(key, "Ed25519"); + if (signingAlgorithm == KmsKeySpec.Algorithm.ED25519_SHA_512) { + return AsymmetricKeys.verify(publicKey, "Ed25519", message, signature); + } + Ed25519phSigner verifier = new Ed25519phSigner(new byte[0]); + verifier.init(false, new Ed25519PublicKeyParameters(point(publicKey), 0)); + verifier.update(message, 0, message.length); + return verifier.verifySignature(signature); + } + + private static KmsKeySpec.Algorithm validateRequest(KmsKeySpec spec, String algorithm, + KmsMessageType messageType, byte[] message) { + KmsKeySpec.Algorithm algo = AsymmetricKeys.requireSpecAlgorithm(spec, algorithm); + KmsMessageType required = algo == KmsKeySpec.Algorithm.ED25519_SHA_512 ? KmsMessageType.RAW : KmsMessageType.DIGEST; + if (messageType != required) { + throw new AwsException("ValidationException", + "Message type " + messageType + " is incompatible with algorithm " + algorithm + ".", 400); + } + if (algo == KmsKeySpec.Algorithm.ED25519_PH_SHA_512 && message.length != SHA_512_DIGEST_BYTES) { + throw new AwsException("ValidationException", + "Digest is invalid length for algorithm " + algorithm + ".", 400); + } + return algo; + } + + private static byte[] seed(PrivateKey privateKey) throws InvalidKeyException { + if (privateKey instanceof EdECPrivateKey edEC) { + return edEC.getBytes().orElseThrow(() -> new InvalidKeyException("Ed25519 private key is not extractable")); + } + throw new InvalidKeyException("Expected an Ed25519 private key but got " + privateKey.getAlgorithm()); + } + + private static byte[] point(PublicKey publicKey) { + return SubjectPublicKeyInfo.getInstance(publicKey.getEncoded()).getPublicKeyData().getBytes(); + } +} diff --git a/src/main/java/io/github/hectorvent/floci/services/kms/keytype/HmacKeyType.java b/src/main/java/io/github/hectorvent/floci/services/kms/keytype/HmacKeyType.java new file mode 100644 index 0000000000..ca6029acf8 --- /dev/null +++ b/src/main/java/io/github/hectorvent/floci/services/kms/keytype/HmacKeyType.java @@ -0,0 +1,46 @@ +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 javax.crypto.Mac; +import javax.crypto.spec.SecretKeySpec; +import java.security.GeneralSecurityException; +import java.security.SecureRandom; +import java.util.Base64; + +final class HmacKeyType implements KmsKeyType { + + private final SecureRandom random; + + HmacKeyType(SecureRandom random) { + this.random = random; + } + + @Override + public void generateKeyMaterial(KmsKey key, String region) { + byte[] material = new byte[key.getKeySpec().materialByteLength()]; + random.nextBytes(material); + key.setPrivateKeyEncoded(Base64.getEncoder().encodeToString(material)); + } + + @Override + public byte[] generateMac(KmsKey key, byte[] message, String algorithm) throws GeneralSecurityException { + byte[] keyBytes = Base64.getDecoder().decode(key.getPrivateKeyEncoded()); + String jcaAlgorithm = mapMacAlgorithm(algorithm); + Mac mac = Mac.getInstance(jcaAlgorithm); + mac.init(new SecretKeySpec(keyBytes, jcaAlgorithm)); + mac.update(message); + return mac.doFinal(); + } + + private static String mapMacAlgorithm(String awsAlgo) { + return switch (awsAlgo) { + case "HMAC_SHA_224" -> "HmacSHA224"; + case "HMAC_SHA_256" -> "HmacSHA256"; + case "HMAC_SHA_384" -> "HmacSHA384"; + case "HMAC_SHA_512" -> "HmacSHA512"; + default -> throw new AwsException("InvalidMacAlgorithmException", "Unsupported MAC algorithm: " + awsAlgo, 400); + }; + } +} diff --git a/src/main/java/io/github/hectorvent/floci/services/kms/keytype/KmsKeyType.java b/src/main/java/io/github/hectorvent/floci/services/kms/keytype/KmsKeyType.java new file mode 100644 index 0000000000..bab9c85c7e --- /dev/null +++ b/src/main/java/io/github/hectorvent/floci/services/kms/keytype/KmsKeyType.java @@ -0,0 +1,35 @@ +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; +import io.github.hectorvent.floci.services.kms.model.KmsMessageType; + +import java.security.GeneralSecurityException; + +public interface KmsKeyType { + + void generateKeyMaterial(KmsKey key, String region) throws GeneralSecurityException; + + default byte[] sign(KmsKey key, byte[] message, String algorithm, KmsMessageType messageType) + throws Exception { + throw new AwsException("UnsupportedOperationException", "Unsupported key spec for signing.", 400); + } + + default boolean verify(KmsKey key, byte[] message, byte[] signature, String algorithm, + KmsMessageType messageType) throws Exception { + return false; + } + + default byte[] encrypt(KmsKey key, KmsKeySpec.Algorithm algorithm, byte[] plaintext) { + throw new IllegalStateException(key.getKeySpec() + " does not encrypt with " + algorithm); + } + + default byte[] decrypt(KmsKey key, KmsKeySpec.Algorithm algorithm, byte[] ciphertext) { + throw new IllegalStateException(key.getKeySpec() + " does not decrypt with " + algorithm); + } + + default byte[] generateMac(KmsKey key, byte[] message, String algorithm) throws GeneralSecurityException { + throw new IllegalStateException(key.getKeySpec() + " does not generate MACs"); + } +} diff --git a/src/main/java/io/github/hectorvent/floci/services/kms/keytype/KmsKeyTypes.java b/src/main/java/io/github/hectorvent/floci/services/kms/keytype/KmsKeyTypes.java new file mode 100644 index 0000000000..82b4261a72 --- /dev/null +++ b/src/main/java/io/github/hectorvent/floci/services/kms/keytype/KmsKeyTypes.java @@ -0,0 +1,44 @@ +package io.github.hectorvent.floci.services.kms.keytype; + +import io.github.hectorvent.floci.services.kms.model.KmsKeySpec; + +import java.security.SecureRandom; + +public final class KmsKeyTypes { + + private final SymmetricKeyType symmetric; + private final KmsKeyType hmac; + private final KmsKeyType rsa; + private final KmsKeyType eccNist; + private final KmsKeyType eccSecgP256k1; + private final KmsKeyType ed25519; + private final KmsKeyType mlDsa; + private final KmsKeyType sm2; + + public KmsKeyTypes(SecureRandom random) { + this.symmetric = new SymmetricKeyType(random); + this.hmac = new HmacKeyType(random); + this.rsa = new RsaKeyType(random); + this.eccNist = new EccNistKeyType(); + this.eccSecgP256k1 = new EccSecgP256k1KeyType(random); + this.ed25519 = new Ed25519KeyType(); + this.mlDsa = new MlDsaKeyType(); + this.sm2 = new Sm2KeyType(random); + } + + public SymmetricKeyType symmetric() { + return symmetric; + } + + public KmsKeyType of(KmsKeySpec spec) { + return switch (spec.getKeyType()) { + case SYMMETRIC -> symmetric; + case HMAC -> hmac; + case RSA -> rsa; + case ECC -> spec == KmsKeySpec.ECC_SECG_P256K1 ? eccSecgP256k1 : eccNist; + case ED25519 -> ed25519; + case ML_DSA -> mlDsa; + case SM2 -> sm2; + }; + } +} diff --git a/src/main/java/io/github/hectorvent/floci/services/kms/keytype/MlDsaKeyType.java b/src/main/java/io/github/hectorvent/floci/services/kms/keytype/MlDsaKeyType.java new file mode 100644 index 0000000000..a5c3da45fa --- /dev/null +++ b/src/main/java/io/github/hectorvent/floci/services/kms/keytype/MlDsaKeyType.java @@ -0,0 +1,39 @@ +package io.github.hectorvent.floci.services.kms.keytype; + +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; + +import java.security.GeneralSecurityException; +import java.security.KeyPairGenerator; +import java.security.PrivateKey; +import java.security.PublicKey; + +final class MlDsaKeyType implements KmsKeyType { + + private static final String JCA_ALGORITHM = KmsKeySpec.Algorithm.ML_DSA_SHAKE_256.getJavaName(); + + @Override + public void generateKeyMaterial(KmsKey key, String region) throws GeneralSecurityException { + String algorithm = key.getKeySpec().name().replace('_', '-'); + AsymmetricKeys.store(key, KeyPairGenerator.getInstance(algorithm).generateKeyPair()); + } + + @Override + public byte[] sign(KmsKey key, byte[] message, String algorithm, KmsMessageType messageType) + throws GeneralSecurityException { + AsymmetricKeys.requireSpecAlgorithm(key.getKeySpec(), algorithm); + AsymmetricKeys.requireRawMessage(key.getKeySpec(), messageType); + PrivateKey privateKey = AsymmetricKeys.privateKey(key, "ML-DSA"); + return AsymmetricKeys.sign(privateKey, JCA_ALGORITHM, message); + } + + @Override + public boolean verify(KmsKey key, byte[] message, byte[] signature, String algorithm, + KmsMessageType messageType) throws GeneralSecurityException { + AsymmetricKeys.requireSpecAlgorithm(key.getKeySpec(), algorithm); + AsymmetricKeys.requireRawMessage(key.getKeySpec(), messageType); + PublicKey publicKey = AsymmetricKeys.publicKey(key, "ML-DSA"); + return AsymmetricKeys.verify(publicKey, JCA_ALGORITHM, message, signature); + } +} diff --git a/src/main/java/io/github/hectorvent/floci/services/kms/keytype/RsaKeyType.java b/src/main/java/io/github/hectorvent/floci/services/kms/keytype/RsaKeyType.java new file mode 100644 index 0000000000..938925ef6c --- /dev/null +++ b/src/main/java/io/github/hectorvent/floci/services/kms/keytype/RsaKeyType.java @@ -0,0 +1,182 @@ +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; +import io.github.hectorvent.floci.services.kms.model.KmsMessageType; +import org.bouncycastle.asn1.ASN1ObjectIdentifier; +import org.bouncycastle.asn1.DERNull; +import org.bouncycastle.asn1.nist.NISTObjectIdentifiers; +import org.bouncycastle.asn1.x509.AlgorithmIdentifier; +import org.bouncycastle.asn1.x509.DigestInfo; +import org.bouncycastle.crypto.Digest; +import org.bouncycastle.crypto.digests.SHA256Digest; +import org.bouncycastle.crypto.digests.SHA384Digest; +import org.bouncycastle.crypto.digests.SHA512Digest; +import org.bouncycastle.crypto.engines.RSABlindedEngine; +import org.bouncycastle.crypto.params.ParametersWithRandom; +import org.bouncycastle.crypto.signers.PSSSigner; +import org.bouncycastle.crypto.util.PrivateKeyFactory; +import org.bouncycastle.crypto.util.PublicKeyFactory; +import org.jboss.logging.Logger; + +import javax.crypto.Cipher; +import javax.crypto.spec.OAEPParameterSpec; +import javax.crypto.spec.PSource; +import java.io.IOException; +import java.security.GeneralSecurityException; +import java.security.KeyPairGenerator; +import java.security.PrivateKey; +import java.security.PublicKey; +import java.security.SecureRandom; +import java.security.spec.MGF1ParameterSpec; + +import static io.github.hectorvent.floci.services.kms.model.KmsMessageType.DIGEST; + +final class RsaKeyType implements KmsKeyType { + + private static final Logger LOG = Logger.getLogger(RsaKeyType.class); + + private final SecureRandom random; + + RsaKeyType(SecureRandom random) { + this.random = random; + } + + @Override + public void generateKeyMaterial(KmsKey key, String region) throws GeneralSecurityException { + KeyPairGenerator generator = KeyPairGenerator.getInstance("RSA"); + generator.initialize(keySize(key.getKeySpec())); + AsymmetricKeys.store(key, generator.generateKeyPair()); + } + + @Override + public byte[] sign(KmsKey key, byte[] message, String algorithm, KmsMessageType messageType) throws Exception { + PrivateKey privateKey = AsymmetricKeys.privateKey(key, "RSA"); + if (messageType == DIGEST) { + if (isPss(algorithm)) { + return signPssDigest(privateKey, message, algorithm); + } + // RFC 8017 9.2: PKCS#1 v1.5 signs DigestInfo{hashOID, digest}, not the + // bare digest, so the signature validates with external verifiers and + // real KMS (NONEwithRSA only pads the bytes it is given). + return AsymmetricKeys.sign(privateKey, "NONEwithRSA", wrapInDigestInfo(message, algorithm)); + } + return AsymmetricKeys.sign(privateKey, KmsKeySpec.getSignVerifyAlgorithm(algorithm).getJavaName(), message); + } + + @Override + public boolean verify(KmsKey key, byte[] message, byte[] signature, String algorithm, + KmsMessageType messageType) throws Exception { + PublicKey publicKey = AsymmetricKeys.publicKey(key, "RSA"); + // An unknown algorithm name fails even for DIGEST. + KmsKeySpec.Algorithm signingAlgorithm = KmsKeySpec.getSignVerifyAlgorithm(algorithm); + if (messageType == DIGEST) { + if (isPss(algorithm)) { + return verifyPssDigest(publicKey, message, signature, algorithm); + } + // Mirror sign(): verify against DigestInfo{hashOID, digest} (RFC 8017 9.2). + return AsymmetricKeys.verify(publicKey, "NONEwithRSA", wrapInDigestInfo(message, algorithm), signature); + } + return AsymmetricKeys.verify(publicKey, signingAlgorithm.getJavaName(), message, signature); + } + + @Override + public byte[] encrypt(KmsKey key, KmsKeySpec.Algorithm algorithm, byte[] plaintext) { + validatePlaintextLength(plaintext, algorithm, key.getKeySpec()); + return oaep(Cipher.ENCRYPT_MODE, key, algorithm, plaintext); + } + + @Override + public byte[] decrypt(KmsKey key, KmsKeySpec.Algorithm algorithm, byte[] ciphertext) { + return oaep(Cipher.DECRYPT_MODE, key, algorithm, ciphertext); + } + + private static int keySize(KmsKeySpec spec) { + return Integer.parseInt(spec.name().substring("RSA_".length())); + } + + /** RFC 8017 7.1.1: OAEP holds at most k - 2*hLen - 2 bytes, k being the modulus length. */ + private static void validatePlaintextLength(byte[] plaintext, KmsKeySpec.Algorithm algorithm, KmsKeySpec spec) { + int modulusBytes = keySize(spec) / 8; + int digestBytes = algorithm == KmsKeySpec.Algorithm.RSAES_OAEP_SHA_1 ? 20 : 32; + int maxBytes = modulusBytes - 2 * digestBytes - 2; + if (plaintext.length > maxBytes) { + throw new AwsException("ValidationException", + "Algorithm " + algorithm.getAlgName() + " and key spec " + spec.name() + + " cannot encrypt data larger than " + maxBytes + " bytes.", 400); + } + } + + // The JDK OAEP transformations default MGF1 to SHA-1. KMS RSAES_OAEP_SHA_256 uses MGF1 over SHA-256. + private static byte[] oaep(int mode, KmsKey key, KmsKeySpec.Algorithm algorithm, byte[] input) { + try { + String digest = algorithm == KmsKeySpec.Algorithm.RSAES_OAEP_SHA_1 ? "SHA-1" : "SHA-256"; + Cipher cipher = Cipher.getInstance("RSA/ECB/OAEPPadding"); + OAEPParameterSpec params = new OAEPParameterSpec(digest, "MGF1", new MGF1ParameterSpec(digest), + PSource.PSpecified.DEFAULT); + if (mode == Cipher.ENCRYPT_MODE) { + cipher.init(mode, AsymmetricKeys.publicKey(key, "RSA"), params); + } else { + cipher.init(mode, AsymmetricKeys.privateKey(key, "RSA"), params); + } + return cipher.doFinal(input); + } catch (Exception e) { + if (mode == Cipher.DECRYPT_MODE) { + LOG.debugv(e, "RSA OAEP decrypt failed for key {0}", key.getKeyId()); + throw new AwsException("InvalidCiphertextException", "The ciphertext is invalid.", 400); + } + LOG.warnv(e, "RSA OAEP encrypt failed for key {0}", key.getKeyId()); + throw new AwsException("InternalFailure", "Failed to encrypt: " + e.getMessage(), 500); + } + } + + private static boolean isPss(String algorithm) { + return algorithm.startsWith("RSASSA_PSS"); + } + + // NONEwithRSA pads only the bytes it gets, so PKCS#1 v1.5 needs the DigestInfo wrapper (RFC 8017 9.2). + private static byte[] wrapInDigestInfo(byte[] digest, String algorithm) { + ASN1ObjectIdentifier hashOid; + if (algorithm.endsWith("SHA_256")) { + hashOid = NISTObjectIdentifiers.id_sha256; + } else if (algorithm.endsWith("SHA_384")) { + hashOid = NISTObjectIdentifiers.id_sha384; + } else if (algorithm.endsWith("SHA_512")) { + hashOid = NISTObjectIdentifiers.id_sha512; + } else { + throw new AwsException("InvalidSigningAlgorithmException", "Unsupported algorithm: " + algorithm, 400); + } + try { + return new DigestInfo(new AlgorithmIdentifier(hashOid, DERNull.INSTANCE), digest).getEncoded(); + } catch (IOException e) { + throw new AwsException("InternalFailure", "Failed to encode DigestInfo: " + e.getMessage(), 500); + } + } + + // The JDK RSASSA-PSS Signature always hashes its input, so a DIGEST request needs BC's raw PSS signer. + private byte[] signPssDigest(PrivateKey privateKey, byte[] digest, String algorithm) throws Exception { + PSSSigner signer = rawPssSigner(algorithm); + signer.init(true, new ParametersWithRandom(PrivateKeyFactory.createKey(privateKey.getEncoded()), random)); + signer.update(digest, 0, digest.length); + return signer.generateSignature(); + } + + private static boolean verifyPssDigest(PublicKey publicKey, byte[] digest, byte[] signature, String algorithm) + throws IOException { + PSSSigner signer = rawPssSigner(algorithm); + signer.init(false, PublicKeyFactory.createKey(publicKey.getEncoded())); + signer.update(digest, 0, digest.length); + return signer.verifySignature(signature); + } + + private static PSSSigner rawPssSigner(String algorithm) { + Digest digest = switch (algorithm) { + case "RSASSA_PSS_SHA_256" -> new SHA256Digest(); + case "RSASSA_PSS_SHA_384" -> new SHA384Digest(); + case "RSASSA_PSS_SHA_512" -> new SHA512Digest(); + default -> throw new AwsException("InvalidSigningAlgorithmException", "Unsupported algorithm: " + algorithm, 400); + }; + return PSSSigner.createRawSigner(new RSABlindedEngine(), digest); + } +} diff --git a/src/main/java/io/github/hectorvent/floci/services/kms/keytype/Sm2KeyType.java b/src/main/java/io/github/hectorvent/floci/services/kms/keytype/Sm2KeyType.java new file mode 100644 index 0000000000..e8185c33ef --- /dev/null +++ b/src/main/java/io/github/hectorvent/floci/services/kms/keytype/Sm2KeyType.java @@ -0,0 +1,54 @@ +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.KmsMessageType; +import org.bouncycastle.crypto.CryptoException; +import org.bouncycastle.crypto.params.ParametersWithRandom; +import org.bouncycastle.crypto.signers.SM2Signer; + +import java.io.IOException; +import java.security.GeneralSecurityException; +import java.security.SecureRandom; + +final class Sm2KeyType implements KmsKeyType { + + private static final String CURVE = "sm2p256v1"; + + private final SecureRandom random; + + Sm2KeyType(SecureRandom random) { + this.random = random; + } + + @Override + public void generateKeyMaterial(KmsKey key, String region) throws GeneralSecurityException { + if (!region.equals("cn-north-1") && !region.equals("cn-northwest-1")) { + throw new AwsException("UnsupportedOperationException", + "KeySpec SM2 is not supported in this Region", 400); + } + AsymmetricKeys.store(key, BcEcKeys.generateKeyPair(CURVE)); + } + + @Override + public byte[] sign(KmsKey key, byte[] message, String algorithm, KmsMessageType messageType) + throws IOException, CryptoException { + AsymmetricKeys.requireSpecAlgorithm(key.getKeySpec(), algorithm); + AsymmetricKeys.requireRawMessage(key.getKeySpec(), messageType); + SM2Signer signer = new SM2Signer(); + signer.init(true, new ParametersWithRandom(BcEcKeys.privateKeyParameters(key, CURVE), random)); + signer.update(message, 0, message.length); + return signer.generateSignature(); + } + + @Override + public boolean verify(KmsKey key, byte[] message, byte[] signature, String algorithm, + KmsMessageType messageType) throws IOException { + AsymmetricKeys.requireSpecAlgorithm(key.getKeySpec(), algorithm); + AsymmetricKeys.requireRawMessage(key.getKeySpec(), messageType); + SM2Signer verifier = new SM2Signer(); + verifier.init(false, BcEcKeys.publicKeyParameters(key, CURVE)); + verifier.update(message, 0, message.length); + return verifier.verifySignature(signature); + } +} diff --git a/src/main/java/io/github/hectorvent/floci/services/kms/keytype/SymmetricKeyType.java b/src/main/java/io/github/hectorvent/floci/services/kms/keytype/SymmetricKeyType.java new file mode 100644 index 0000000000..5938fb870f --- /dev/null +++ b/src/main/java/io/github/hectorvent/floci/services/kms/keytype/SymmetricKeyType.java @@ -0,0 +1,34 @@ +package io.github.hectorvent.floci.services.kms.keytype; + +import io.github.hectorvent.floci.services.kms.model.KmsKey; +import io.github.hectorvent.floci.services.kms.model.KmsKeySpec; + +import java.security.SecureRandom; +import java.util.Base64; +import java.util.HashMap; +import java.util.UUID; + +public final class SymmetricKeyType implements KmsKeyType { + + private final SecureRandom random; + + SymmetricKeyType(SecureRandom random) { + this.random = random; + } + + @Override + public void generateKeyMaterial(KmsKey key, String region) { + addBackingKey(key); + } + + public void addBackingKey(KmsKey key) { + byte[] material = new byte[KmsKeySpec.SYMMETRIC_DEFAULT.materialByteLength()]; + random.nextBytes(material); + String backingKeyId = UUID.randomUUID().toString(); + if (key.getBackingKeys() == null) { + key.setBackingKeys(new HashMap<>()); + } + key.getBackingKeys().put(backingKeyId, Base64.getEncoder().encodeToString(material)); + key.setCurrentBackingKeyId(backingKeyId); + } +} diff --git a/src/test/java/io/github/hectorvent/floci/services/kms/keytype/KmsKeyTypesTest.java b/src/test/java/io/github/hectorvent/floci/services/kms/keytype/KmsKeyTypesTest.java new file mode 100644 index 0000000000..77af8a0ff7 --- /dev/null +++ b/src/test/java/io/github/hectorvent/floci/services/kms/keytype/KmsKeyTypesTest.java @@ -0,0 +1,259 @@ +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; +import io.github.hectorvent.floci.services.kms.model.KmsMessageType; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.CsvSource; +import org.junit.jupiter.params.provider.EnumSource; +import org.junit.jupiter.params.provider.ValueSource; + +import javax.crypto.Mac; +import javax.crypto.spec.SecretKeySpec; +import java.nio.charset.StandardCharsets; +import java.security.GeneralSecurityException; +import java.security.MessageDigest; +import java.security.SecureRandom; +import java.util.Base64; +import java.util.EnumMap; +import java.util.HashMap; +import java.util.Map; + +import static org.junit.jupiter.api.Assertions.*; + +class KmsKeyTypesTest { + + private static final String REGION = "us-east-1"; + private static final byte[] MESSAGE = "per key type".getBytes(StandardCharsets.UTF_8); + + private final KmsKeyTypes keyTypes = new KmsKeyTypes(new SecureRandom()); + + @Test + void everyKeySpecHasItsOwnKeyType() { + Map> expected = new EnumMap<>(KmsKeySpec.class); + expected.put(KmsKeySpec.SYMMETRIC_DEFAULT, SymmetricKeyType.class); + expected.put(KmsKeySpec.RSA_2048, RsaKeyType.class); + expected.put(KmsKeySpec.RSA_3072, RsaKeyType.class); + expected.put(KmsKeySpec.RSA_4096, RsaKeyType.class); + expected.put(KmsKeySpec.ECC_NIST_P256, EccNistKeyType.class); + expected.put(KmsKeySpec.ECC_NIST_P384, EccNistKeyType.class); + expected.put(KmsKeySpec.ECC_NIST_P521, EccNistKeyType.class); + expected.put(KmsKeySpec.ECC_NIST_EDWARDS25519, Ed25519KeyType.class); + expected.put(KmsKeySpec.ECC_SECG_P256K1, EccSecgP256k1KeyType.class); + expected.put(KmsKeySpec.HMAC_224, HmacKeyType.class); + expected.put(KmsKeySpec.HMAC_256, HmacKeyType.class); + expected.put(KmsKeySpec.HMAC_384, HmacKeyType.class); + expected.put(KmsKeySpec.HMAC_512, HmacKeyType.class); + expected.put(KmsKeySpec.SM2, Sm2KeyType.class); + expected.put(KmsKeySpec.ML_DSA_44, MlDsaKeyType.class); + expected.put(KmsKeySpec.ML_DSA_65, MlDsaKeyType.class); + expected.put(KmsKeySpec.ML_DSA_87, MlDsaKeyType.class); + + assertEquals(KmsKeySpec.values().length, expected.size()); + for (Map.Entry> entry : expected.entrySet()) { + assertEquals(entry.getValue(), keyTypes.of(entry.getKey()).getClass(), entry.getKey().name()); + } + } + + @Test + void addingABackingKeyKeepsEarlierOnes() throws GeneralSecurityException { + KmsKey key = generatedKey(KmsKeySpec.SYMMETRIC_DEFAULT, REGION); + String first = key.getCurrentBackingKeyId(); + + keyTypes.symmetric().addBackingKey(key); + + assertNotEquals(first, key.getCurrentBackingKeyId()); + assertEquals(2, key.getBackingKeys().size()); + assertTrue(key.getBackingKeys().containsKey(first)); + for (String material : key.getBackingKeys().values()) { + assertEquals(32, Base64.getDecoder().decode(material).length); + } + } + + @Test + void addingABackingKeyStartsAMissingMap() { + KmsKey key = newKey(KmsKeySpec.SYMMETRIC_DEFAULT); + key.setBackingKeys(null); + + keyTypes.symmetric().addBackingKey(key); + + assertEquals(1, key.getBackingKeys().size()); + assertTrue(key.getBackingKeys().containsKey(key.getCurrentBackingKeyId())); + } + + @ParameterizedTest + @CsvSource({ + "HMAC_224, HMAC_SHA_224, HmacSHA224", + "HMAC_256, HMAC_SHA_256, HmacSHA256", + "HMAC_384, HMAC_SHA_384, HmacSHA384", + "HMAC_512, HMAC_SHA_512, HmacSHA512", + }) + void hmacMatchesTheJdkMacOverTheGeneratedMaterial(KmsKeySpec spec, String algorithm, String jcaAlgorithm) + throws GeneralSecurityException { + KmsKey key = generatedKey(spec, REGION); + byte[] material = Base64.getDecoder().decode(key.getPrivateKeyEncoded()); + Mac mac = Mac.getInstance(jcaAlgorithm); + mac.init(new SecretKeySpec(material, jcaAlgorithm)); + + assertEquals(spec.materialByteLength(), material.length); + assertArrayEquals(mac.doFinal(MESSAGE), keyTypes.of(spec).generateMac(key, MESSAGE, algorithm)); + } + + @ParameterizedTest + @CsvSource({ + "RSA_2048, RSASSA_PKCS1_V1_5_SHA_256", + "RSA_2048, RSASSA_PKCS1_V1_5_SHA_384", + "RSA_2048, RSASSA_PSS_SHA_256", + "RSA_2048, RSASSA_PSS_SHA_512", + "ECC_NIST_P256, ECDSA_SHA_256", + "ECC_NIST_P384, ECDSA_SHA_384", + "ECC_NIST_P521, ECDSA_SHA_512", + "ECC_SECG_P256K1, ECDSA_SHA_256", + "ECC_NIST_EDWARDS25519, ED25519_SHA_512", + "ML_DSA_44, ML_DSA_SHAKE_256", + }) + void rawSignatureVerifiesOnlyForTheSignedMessage(KmsKeySpec spec, String algorithm) throws Exception { + KmsKey key = generatedKey(spec, REGION); + KmsKeyType keyType = keyTypes.of(spec); + + byte[] signature = keyType.sign(key, MESSAGE, algorithm, KmsMessageType.RAW); + + assertTrue(keyType.verify(key, MESSAGE, signature, algorithm, KmsMessageType.RAW)); + assertFalse(keyType.verify(key, "another message".getBytes(StandardCharsets.UTF_8), signature, + algorithm, KmsMessageType.RAW)); + } + + @ParameterizedTest + @CsvSource({ + "RSA_2048, RSASSA_PKCS1_V1_5_SHA_256, SHA-256", + "RSA_2048, RSASSA_PSS_SHA_384, SHA-384", + "ECC_NIST_P256, ECDSA_SHA_256, SHA-256", + "ECC_SECG_P256K1, ECDSA_SHA_256, SHA-256", + }) + void digestSignatureVerifiesAsARawSignature(KmsKeySpec spec, String algorithm, String digestAlgorithm) + throws Exception { + KmsKey key = generatedKey(spec, REGION); + KmsKeyType keyType = keyTypes.of(spec); + byte[] digest = MessageDigest.getInstance(digestAlgorithm).digest(MESSAGE); + + byte[] signature = keyType.sign(key, digest, algorithm, KmsMessageType.DIGEST); + + assertTrue(keyType.verify(key, MESSAGE, signature, algorithm, KmsMessageType.RAW)); + assertTrue(keyType.verify(key, digest, signature, algorithm, KmsMessageType.DIGEST)); + } + + @Test + void ed25519PreHashSignsTheSha512DigestOfTheMessage() throws Exception { + KmsKey key = generatedKey(KmsKeySpec.ECC_NIST_EDWARDS25519, REGION); + KmsKeyType keyType = keyTypes.of(KmsKeySpec.ECC_NIST_EDWARDS25519); + byte[] digest = MessageDigest.getInstance("SHA-512").digest(MESSAGE); + + byte[] signature = keyType.sign(key, digest, "ED25519_PH_SHA_512", KmsMessageType.DIGEST); + + assertTrue(keyType.verify(key, digest, signature, "ED25519_PH_SHA_512", KmsMessageType.DIGEST)); + } + + @ParameterizedTest + @EnumSource(value = KmsKeySpec.class, names = {"ECC_NIST_EDWARDS25519", "ML_DSA_65", "SM2"}) + void keyTypesWithOneSigningAlgorithmRejectAnyOther(KmsKeySpec spec) throws GeneralSecurityException { + KmsKey key = generatedKey(spec, "cn-north-1"); + + AwsException exception = assertThrows(AwsException.class, + () -> keyTypes.of(spec).sign(key, MESSAGE, "ECDSA_SHA_256", KmsMessageType.RAW)); + + assertEquals("InvalidKeyUsageException", exception.getErrorCode()); + assertEquals("Algorithm ECDSA_SHA_256 is incompatible with key spec " + spec.name() + ".", + exception.getMessage()); + } + + @ParameterizedTest + @ValueSource(strings = {"RSAES_OAEP_SHA_1", "RSAES_OAEP_SHA_256"}) + void rsaEncryptionRoundTrips(String algorithmName) throws GeneralSecurityException { + KmsKeySpec.Algorithm algorithm = KmsKeySpec.Algorithm.valueOf(algorithmName); + KmsKey key = generatedKey(KmsKeySpec.RSA_2048, REGION); + KmsKeyType keyType = keyTypes.of(KmsKeySpec.RSA_2048); + + byte[] ciphertext = keyType.encrypt(key, algorithm, MESSAGE); + + assertEquals(256, ciphertext.length); + assertArrayEquals(MESSAGE, keyType.decrypt(key, algorithm, ciphertext)); + } + + @Test + void rsaEncryptionRejectsPlaintextOverTheOaepLimit() throws GeneralSecurityException { + KmsKey key = generatedKey(KmsKeySpec.RSA_2048, REGION); + KmsKeyType keyType = keyTypes.of(KmsKeySpec.RSA_2048); + + keyType.encrypt(key, KmsKeySpec.Algorithm.RSAES_OAEP_SHA_256, new byte[190]); + AwsException exception = assertThrows(AwsException.class, + () -> keyType.encrypt(key, KmsKeySpec.Algorithm.RSAES_OAEP_SHA_256, new byte[191])); + + assertEquals("ValidationException", exception.getErrorCode()); + assertEquals("Algorithm RSAES_OAEP_SHA_256 and key spec RSA_2048 cannot encrypt data larger than 190 bytes.", + exception.getMessage()); + } + + @Test + void rsaDecryptionOfForeignCiphertextIsInvalidCiphertext() throws GeneralSecurityException { + KmsKey key = generatedKey(KmsKeySpec.RSA_2048, REGION); + KmsKey other = generatedKey(KmsKeySpec.RSA_2048, REGION); + KmsKeyType keyType = keyTypes.of(KmsKeySpec.RSA_2048); + byte[] ciphertext = keyType.encrypt(other, KmsKeySpec.Algorithm.RSAES_OAEP_SHA_256, MESSAGE); + + AwsException exception = assertThrows(AwsException.class, + () -> keyType.decrypt(key, KmsKeySpec.Algorithm.RSAES_OAEP_SHA_256, ciphertext)); + + assertEquals("InvalidCiphertextException", exception.getErrorCode()); + } + + @ParameterizedTest + @ValueSource(strings = {"cn-north-1", "cn-northwest-1"}) + void sm2SignsInChinaRegions(String region) throws Exception { + KmsKey key = generatedKey(KmsKeySpec.SM2, region); + KmsKeyType keyType = keyTypes.of(KmsKeySpec.SM2); + + byte[] signature = keyType.sign(key, MESSAGE, "SM2DSA", KmsMessageType.RAW); + + assertTrue(keyType.verify(key, MESSAGE, signature, "SM2DSA", KmsMessageType.RAW)); + } + + @Test + void sm2KeyMaterialIsRefusedOutsideChinaRegions() { + KmsKey key = newKey(KmsKeySpec.SM2); + + AwsException exception = assertThrows(AwsException.class, + () -> keyTypes.of(KmsKeySpec.SM2).generateKeyMaterial(key, REGION)); + + assertEquals("UnsupportedOperationException", exception.getErrorCode()); + assertNull(key.getPrivateKeyEncoded()); + } + + @ParameterizedTest + @EnumSource(value = KmsKeySpec.class, names = {"SYMMETRIC_DEFAULT", "HMAC_256"}) + void keyTypesWithoutSigningRefuseToSignAndNeverVerify(KmsKeySpec spec) throws Exception { + KmsKey key = generatedKey(spec, REGION); + KmsKeyType keyType = keyTypes.of(spec); + + AwsException exception = assertThrows(AwsException.class, + () -> keyType.sign(key, MESSAGE, "RSASSA_PSS_SHA_256", KmsMessageType.RAW)); + + assertEquals("UnsupportedOperationException", exception.getErrorCode()); + assertFalse(keyType.verify(key, MESSAGE, new byte[64], "RSASSA_PSS_SHA_256", KmsMessageType.RAW)); + } + + private KmsKey generatedKey(KmsKeySpec spec, String region) throws GeneralSecurityException { + KmsKey key = newKey(spec); + keyTypes.of(spec).generateKeyMaterial(key, region); + return key; + } + + private static KmsKey newKey(KmsKeySpec spec) { + KmsKey key = new KmsKey(); + key.setKeyId("key-" + spec.name()); + key.setKeySpec(spec); + key.setBackingKeys(new HashMap<>()); + return key; + } +}