mirror of
https://github.com/floci-io/floci.git
synced 2026-11-24 06:20:24 -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
@@ -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..." algorithm
|
||||
jcaAlgo = "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";
|
||||
|
||||
Reference in new issue
Block a user