mirror of
https://github.com/floci-io/floci.git
synced 2026-12-06 11:38:06 -05:00
fix(kms): 🐛 wrap RSA DIGEST signatures in PKCS#1 DigestInfo (#1396)
Sign/Verify with MessageType=DIGEST and an RSASSA_PKCS1_V1_5_* algorithm fed
the bare digest to NONEwithRSA, which PKCS#1-pads only the bytes it is given.
Real KMS (and any external verifier) signs DigestInfo{hashOID, digest} per
RFC 8017 9.2, so floci's DIGEST signatures verified internally but failed with
openssl / python cryptography.
Wrap the digest in the ASN.1 DigestInfo structure before NONEwithRSA on both
sign and verify. ECDSA DIGEST (no DigestInfo) and the RAW path are unchanged.
Closes #1345
This commit is contained in:
1 parent
0cb90bc739
commit
4520e75176
2 files changed
+84
No files matched your search
@@ -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);
|
||||
}
|
||||
|
||||
@Test
|
||||
void verifyWithWrongSignatureReturnsFalse() {
|
||||
KmsKey key = kmsService.createKey("ecdsa key", "SIGN_VERIFY", "ECC_NIST_P256", null, Map.of(), REGION);
|
||||
|
||||
Reference in new issue
Block a user