Light, fluffy, and always free - The AWS Local Emulator alternative
fix(kms): 🐛 wrap RSA DIGEST signatures in PKCS#1 DigestInfo (#1396)
Lorenzo Galassi4520e75176a1
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.javaindex c53d46c028..01b56ce0d9 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@@ -12,10 +12,15 @@ import com.fasterxml.jackson.core.type.TypeReference;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.params.ECDomainParameters;import org.bouncycastle.crypto.params.ECPrivateKeyParameters;@@ -34,6 +39,7 @@ import org.jboss.logging.Logger;import javax.crypto.Mac;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;@@ -741,6 +747,12 @@ public class KmsService {if ("DIGEST".equals(messageType)) {// If message is already a digest, we need a "NONEwith..." algorithmjcaAlgo = "NONEwith" + (kmsKey.getCustomerMasterKeySpec().startsWith("RSA") ? "RSA" : "ECDSA");+ if (algorithm.startsWith("RSASSA_PKCS1_V1_5")) {+ // 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.getCustomerMasterKeySpec())) {@@ -771,6 +783,10 @@ public class KmsService {if ("DIGEST".equals(messageType)) {jcaAlgo = "NONEwith" + (kmsKey.getCustomerMasterKeySpec().startsWith("RSA") ? "RSA" : "ECDSA");+ if (algorithm.startsWith("RSASSA_PKCS1_V1_5")) {+ // Mirror sign(): verify against DigestInfo{hashOID, digest} (RFC 8017 9.2).+ message = wrapInDigestInfo(message, algorithm);+ }}if (isSecgP256k1(kmsKey.getCustomerMasterKeySpec())) {@@ -896,6 +912,29 @@ public class KmsService {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);+ }+ }+private String mapAlgorithm(String awsAlgo) {return switch (awsAlgo) {case "ECDSA_SHA_256" -> "SHA256withECDSA";diff --git a/src/test/java/io/github/hectorvent/floci/services/kms/KmsServiceTest.java b/src/test/java/io/github/hectorvent/floci/services/kms/KmsServiceTest.javaindex a6c4936819..d3c5002e56 100644--- a/src/test/java/io/github/hectorvent/floci/services/kms/KmsServiceTest.java+++ b/src/test/java/io/github/hectorvent/floci/services/kms/KmsServiceTest.java@@ -15,7 +15,9 @@ import org.junit.jupiter.api.Test;import java.nio.charset.StandardCharsets;import java.security.Security;import java.security.KeyFactory;+import java.security.MessageDigest;import java.security.PublicKey;+import java.security.Signature;import java.security.spec.X509EncodedKeySpec;import java.util.Arrays;import java.util.Base64;@@ -780,6 +782,49 @@ class KmsServiceTest {assertTrue(kmsService.verify(key.getKeyId(), message, sig, "RSASSA_PKCS1_V1_5_SHA_256", REGION));}+ @Test+ void signWithDigestMessageTypeVerifiesWithExternalVerifier() throws Exception {+ KmsKey key = kmsService.createKey("rsa digest key", "SIGN_VERIFY", "RSA_2048", null, Map.of(), REGION);+ byte[] message = "floci kms round-trip".getBytes(StandardCharsets.UTF_8);+ byte[] digest = MessageDigest.getInstance("SHA-512").digest(message);++ byte[] sig = kmsService.sign(key.getKeyId(), digest,+ "RSASSA_PKCS1_V1_5_SHA_512", "DIGEST", REGION);++ // floci's own Verify round-trips.+ assertTrue(kmsService.verify(key.getKeyId(), digest, sig,+ "RSASSA_PKCS1_V1_5_SHA_512", "DIGEST", REGION));++ // External verifier (standard JCA, standing in for openssl/python) reconstructs+ // DigestInfo from the message hash and must validate the DIGEST signature (#1345).+ byte[] der = Base64.getDecoder().decode(kmsService.getPublicKey(key.getKeyId(), REGION).getPublicKeyEncoded());+ PublicKey pub = KeyFactory.getInstance("RSA").generatePublic(new X509EncodedKeySpec(der));+ Signature verifier = Signature.getInstance("SHA512withRSA");+ verifier.initVerify(pub);+ verifier.update(message);+ assertTrue(verifier.verify(sig), "DIGEST signature must verify with standard SHA512withRSA");+ }++ @Test+ void signWithDigestMessageTypeMatchesRawSignature() {+ KmsKey key = kmsService.createKey("rsa digest key", "SIGN_VERIFY", "RSA_2048", null, Map.of(), REGION);+ byte[] message = "deterministic pkcs1".getBytes(StandardCharsets.UTF_8);+ byte[] digest;+ try {+ digest = MessageDigest.getInstance("SHA-256").digest(message);+ } catch (Exception e) {+ throw new RuntimeException(e);+ }++ // PKCS#1 v1.5 is deterministic, so signing the digest (DIGEST) and signing the+ // message (RAW) with the same algorithm must produce identical signatures.+ byte[] digestSig = kmsService.sign(key.getKeyId(), digest,+ "RSASSA_PKCS1_V1_5_SHA_256", "DIGEST", REGION);+ byte[] rawSig = kmsService.sign(key.getKeyId(), message,+ "RSASSA_PKCS1_V1_5_SHA_256", "RAW", REGION);+ assertArrayEquals(rawSig, digestSig);+ }+@Testvoid verifyWithWrongSignatureReturnsFalse() {KmsKey key = kmsService.createKey("ecdsa key", "SIGN_VERIFY", "ECC_NIST_P256", null, Map.of(), REGION);