mirror of
https://github.com/floci-io/floci.git
synced 2026-11-24 06:20:24 -05:00
fix(kms): support SM2 signing in China regions (#3046)
* fix(kms): report SM2 as region unsupported * fix(kms): support SM2 in China regions * fix(kms): enable SM2 signing in China regions * fix(kms): restrict SM2 to China regions * fix(kms): accept the SM2DSA wire algorithm * test(kms): cover SM2 signing validation errors
This commit is contained in:
1 parent
2fd55b53df
commit
5198cf29ec
6 files changed
+205
-7
No files matched your search
@@ -44,6 +44,7 @@ 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;
|
||||
@@ -191,7 +192,7 @@ public class KmsService implements ResourceProvider {
|
||||
key.setKeyState(PENDING_IMPORT);
|
||||
key.setEnabled(false);
|
||||
} else {
|
||||
generateKeyMaterial(key);
|
||||
generateKeyMaterial(key, region);
|
||||
}
|
||||
|
||||
keyStore.put(region + "::" + keyId, key);
|
||||
@@ -216,7 +217,7 @@ public class KmsService implements ResourceProvider {
|
||||
return normalized;
|
||||
}
|
||||
|
||||
private void generateKeyMaterial(KmsKey key) {
|
||||
private void generateKeyMaterial(KmsKey key, String region) {
|
||||
KmsKeySpec spec = key.getKeySpec();
|
||||
try {
|
||||
switch (spec.getKeyType()) {
|
||||
@@ -262,6 +263,17 @@ public class KmsService implements ResourceProvider {
|
||||
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);
|
||||
}
|
||||
@@ -1444,18 +1456,25 @@ public class KmsService implements ResourceProvider {
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
@@ -1495,18 +1514,25 @@ public class KmsService implements ResourceProvider {
|
||||
}
|
||||
|
||||
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)) {
|
||||
@@ -1626,7 +1652,7 @@ public class KmsService implements ResourceProvider {
|
||||
|
||||
private PrivateKey loadPrivateKey(String encoded, KmsKeySpec spec) throws Exception {
|
||||
byte[] decoded = Base64.getDecoder().decode(encoded);
|
||||
if (isSecgP256k1(spec)) {
|
||||
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.
|
||||
@@ -1638,7 +1664,7 @@ public class KmsService implements ResourceProvider {
|
||||
|
||||
private PublicKey loadPublicKey(String encoded, KmsKeySpec spec) throws Exception {
|
||||
byte[] decoded = Base64.getDecoder().decode(encoded);
|
||||
if (isSecgP256k1(spec)) {
|
||||
if (isSecgP256k1(spec) || isSm2(spec)) {
|
||||
AsymmetricKeyInfoConverter converter = new KeyFactorySpi.EC();
|
||||
return converter.generatePublic(SubjectPublicKeyInfo.getInstance(decoded));
|
||||
}
|
||||
@@ -1710,6 +1736,43 @@ public class KmsService implements ResourceProvider {
|
||||
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.
|
||||
*
|
||||
|
||||
@@ -27,7 +27,7 @@ public enum KmsKeySpec {
|
||||
HMAC_384(KeyType.HMAC, Algorithm.HMAC_SHA_384),
|
||||
HMAC_512(KeyType.HMAC, Algorithm.HMAC_SHA_512),
|
||||
|
||||
SM2(KeyType.SM2, Algorithm.SM2_DSA),
|
||||
SM2(KeyType.SM2, Algorithm.SM2DSA),
|
||||
|
||||
ML_DSA_44(KeyType.ML_DSA, Algorithm.ML_DSA_SHAKE_256),
|
||||
ML_DSA_65(KeyType.ML_DSA, Algorithm.ML_DSA_SHAKE_256),
|
||||
@@ -152,7 +152,7 @@ public enum KmsKeySpec {
|
||||
ECDSA_SHA_512("ECDSA_SHA_512", "SHA512withECDSA", KmsKeyUsage.SIGN_VERIFY),
|
||||
ED25519_SHA_512("ED25519_SHA_512","Ed25519", KmsKeyUsage.SIGN_VERIFY),
|
||||
ED25519_PH_SHA_512("ED25519_PH_SHA_512", "Ed25519", KmsKeyUsage.SIGN_VERIFY),
|
||||
SM2_DSA("SM2DSA","", KmsKeyUsage.SIGN_VERIFY),
|
||||
SM2DSA("SM2DSA","", KmsKeyUsage.SIGN_VERIFY),
|
||||
ML_DSA_SHAKE_256("ML_DSA_SHAKE_256", "ML-DSA", KmsKeyUsage.SIGN_VERIFY),
|
||||
HMAC_SHA_224("HMAC_SHA_224",""),
|
||||
HMAC_SHA_256("HMAC_SHA_256",""),
|
||||
|
||||
Reference in new issue
Block a user