mirror of
https://github.com/floci-io/floci.git
synced 2026-11-27 07:39:24 -05:00
feat(kms): support ECC imported key material (#4428)
* feat(kms): support ECC imported key material * test(kms): cover verify, digest signing and import lifecycle for ECC keys * docs(kms): explain why ECC keys accept every supported wrapping algorithm * fix(kms): reject ECC key material whose inner curve parameters differ * fix(kms): report malformed ECC key fields as IncorrectKeyMaterialException * fix(kms): reject RSAES_OAEP wrapping of ECC_NIST_P521 material under an RSA_2048 key
This commit is contained in:
1 parent
60d112542d
commit
5cb973c561
8 files changed
+561
-9
No files matched your search
@@ -73,7 +73,7 @@ final class KmsKeyImport {
|
||||
* <p>RSAES_PKCS1_V1_5 stays in the modelled enum but AWS KMS stopped honouring it on
|
||||
* October 10, 2023.
|
||||
*/
|
||||
static void validateWrappingAlgorithm(KmsKeySpec keySpec, String wrappingAlgorithm) {
|
||||
static void validateWrappingAlgorithm(KmsKeySpec keySpec, String wrappingAlgorithm, String wrappingKeySpec) {
|
||||
String algorithm = wrappingAlgorithm == null ? "" : wrappingAlgorithm;
|
||||
validateWrappingAlgorithmValue(algorithm, wrappingAlgorithm);
|
||||
|
||||
@@ -92,6 +92,18 @@ final class KmsKeyImport {
|
||||
+ "Supported values are: RSA_AES_KEY_WRAP_SHA_1 and RSA_AES_KEY_WRAP_SHA_256.", 400);
|
||||
}
|
||||
}
|
||||
case ECC -> {
|
||||
// AWS accepts both RSAES_OAEP_* and RSA_AES_KEY_WRAP_* for ECC material, which is
|
||||
// everything validateWrappingAlgorithmValue lets through, except RSAES_OAEP_* under an
|
||||
// RSA_2048 wrapping key for ECC_NIST_P521, whose material is too long for that payload.
|
||||
boolean rsaesOaep = RSAES_OAEP_SHA_1.equals(algorithm) || RSAES_OAEP_SHA_256.equals(algorithm);
|
||||
if (keySpec == KmsKeySpec.ECC_NIST_P521 && rsaesOaep && "RSA_2048".equals(wrappingKeySpec)) {
|
||||
throw new AwsException("UnsupportedOperationException",
|
||||
"WrappingAlgorithm " + wrappingAlgorithm + " with WrappingKeySpec RSA_2048 is not "
|
||||
+ "supported for " + keySpec + " key. Use a larger wrapping key spec or "
|
||||
+ "RSA_AES_KEY_WRAP_SHA_1 or RSA_AES_KEY_WRAP_SHA_256.", 400);
|
||||
}
|
||||
}
|
||||
default -> throw new AwsException("UnsupportedOperationException",
|
||||
"Importing key material for key spec " + keySpec + " is not supported.", 400);
|
||||
}
|
||||
|
||||
@@ -870,7 +870,7 @@ public class KmsService implements ResourceProvider {
|
||||
KmsKey key = resolveKey(keyId, region);
|
||||
requireExternalOrigin(key);
|
||||
requireNotPendingDeletion(key);
|
||||
KmsKeyImport.validateWrappingAlgorithm(key.getKeySpec(), wrappingAlgorithm);
|
||||
KmsKeyImport.validateWrappingAlgorithm(key.getKeySpec(), wrappingAlgorithm, wrappingKeySpec);
|
||||
|
||||
KmsKeyImport.WrappingKeyPair wrappingKeyPair = KmsKeyImport.generateWrappingKeyPair(wrappingKeySpec);
|
||||
KmsImportParameters parameters = new KmsImportParameters();
|
||||
@@ -1125,15 +1125,16 @@ public class KmsService implements ResourceProvider {
|
||||
}
|
||||
|
||||
/**
|
||||
* Allows imports for symmetric, HMAC and RSA key specs. Other key specs are not supported.
|
||||
* Allows imports for symmetric, HMAC, RSA and ECC key specs. Other key specs are not supported.
|
||||
*/
|
||||
private static String requireImportableSpec(KmsKeySpec spec) {
|
||||
if (spec != KmsKeySpec.SYMMETRIC_DEFAULT
|
||||
&& spec.getKeyType() != KmsKeySpec.KeyType.HMAC
|
||||
&& spec.getKeyType() != KmsKeySpec.KeyType.RSA) {
|
||||
&& spec.getKeyType() != KmsKeySpec.KeyType.RSA
|
||||
&& spec.getKeyType() != KmsKeySpec.KeyType.ECC) {
|
||||
|
||||
throw new AwsException("UnsupportedOperationException",
|
||||
"Origin EXTERNAL is only supported for SYMMETRIC_DEFAULT, HMAC and RSA key specs, not "
|
||||
"Origin EXTERNAL is only supported for SYMMETRIC_DEFAULT, HMAC, RSA and ECC key specs, not "
|
||||
+ spec + ".", 400);
|
||||
}
|
||||
return EXTERNAL_ORIGIN;
|
||||
|
||||
@@ -1,8 +1,16 @@
|
||||
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 org.bouncycastle.asn1.ASN1BitString;
|
||||
import org.bouncycastle.asn1.ASN1Encoding;
|
||||
import org.bouncycastle.asn1.ASN1Object;
|
||||
import org.bouncycastle.asn1.ASN1ObjectIdentifier;
|
||||
import org.bouncycastle.asn1.pkcs.PrivateKeyInfo;
|
||||
import org.bouncycastle.asn1.sec.ECPrivateKey;
|
||||
import org.bouncycastle.asn1.x509.AlgorithmIdentifier;
|
||||
import org.bouncycastle.asn1.x509.SubjectPublicKeyInfo;
|
||||
import org.bouncycastle.asn1.x9.X9ObjectIdentifiers;
|
||||
import org.bouncycastle.crypto.params.ECDomainParameters;
|
||||
import org.bouncycastle.crypto.params.ECPrivateKeyParameters;
|
||||
import org.bouncycastle.crypto.params.ECPublicKeyParameters;
|
||||
@@ -10,15 +18,19 @@ 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.asymmetric.util.ECUtil;
|
||||
import org.bouncycastle.jcajce.provider.util.AsymmetricKeyInfoConverter;
|
||||
import org.bouncycastle.jce.ECNamedCurveTable;
|
||||
import org.bouncycastle.jce.spec.ECNamedCurveParameterSpec;
|
||||
import org.bouncycastle.math.ec.ECPoint;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.math.BigInteger;
|
||||
import java.security.InvalidAlgorithmParameterException;
|
||||
import java.security.KeyPair;
|
||||
import java.security.KeyPairGenerator;
|
||||
import java.security.spec.ECGenParameterSpec;
|
||||
import java.security.spec.InvalidKeySpecException;
|
||||
import java.util.Base64;
|
||||
|
||||
// The BouncyCastle SPI classes are allocated directly. JCA lookup cannot find them in the native image.
|
||||
@@ -47,6 +59,93 @@ final class BcEcKeys {
|
||||
return new ECPublicKeyParameters(publicKey.getQ(), domainParameters(curveName));
|
||||
}
|
||||
|
||||
/**
|
||||
* Stores imported ECC key material: a PKCS#8 private key (RFC 5208) holding an RFC 5915
|
||||
* ECPrivateKey on the key spec's named curve. As on AWS, only the private key is taken from
|
||||
* the material and the public key is derived from it; an embedded public key must match.
|
||||
*/
|
||||
static void importPrivateKey(KmsKey key, byte[] material, String curveName) {
|
||||
try {
|
||||
ImportedEcKey imported = parseImportedPrivateKey(material, curveName);
|
||||
Base64.Encoder encoder = Base64.getEncoder();
|
||||
key.setPrivateKeyEncoded(encoder.encodeToString(imported.privateKeyEncoded()));
|
||||
key.setPublicKeyEncoded(encoder.encodeToString(imported.publicKeyEncoded()));
|
||||
} catch (InvalidKeySpecException e) {
|
||||
throw new AwsException("IncorrectKeyMaterialException",
|
||||
"Key material for key spec " + key.getKeySpec() + " must be a PKCS#8-encoded "
|
||||
+ curveName + " private key: " + e.getMessage(), 400);
|
||||
}
|
||||
}
|
||||
|
||||
private static ImportedEcKey parseImportedPrivateKey(byte[] material, String curveName)
|
||||
throws InvalidKeySpecException {
|
||||
// BouncyCastle decodes the ECPrivateKey fields lazily, so a malformed field only fails when it
|
||||
// is read: read them all here, where a failure maps to IncorrectKeyMaterialException.
|
||||
PrivateKeyInfo privateKeyInfo;
|
||||
BigInteger privateScalar;
|
||||
ASN1Object innerParameters;
|
||||
ASN1BitString embeddedPublicKey;
|
||||
try {
|
||||
privateKeyInfo = PrivateKeyInfo.getInstance(material);
|
||||
ECPrivateKey ecPrivateKey = ECPrivateKey.getInstance(privateKeyInfo.parsePrivateKey());
|
||||
privateScalar = ecPrivateKey.getKey();
|
||||
innerParameters = ecPrivateKey.getParametersObject();
|
||||
embeddedPublicKey = ecPrivateKey.getPublicKey();
|
||||
} catch (IOException | RuntimeException e) {
|
||||
throw new InvalidKeySpecException("the material is not a PKCS#8 EC private key", e);
|
||||
}
|
||||
|
||||
AlgorithmIdentifier algorithm = privateKeyInfo.getPrivateKeyAlgorithm();
|
||||
if (!X9ObjectIdentifiers.id_ecPublicKey.equals(algorithm.getAlgorithm())) {
|
||||
throw new InvalidKeySpecException("the material is not an EC private key");
|
||||
}
|
||||
ASN1ObjectIdentifier curveOid = ECUtil.getNamedCurveOid(curveName);
|
||||
if (!curveOid.equals(algorithm.getParameters())) {
|
||||
throw new InvalidKeySpecException("the material must name the " + curveName + " curve");
|
||||
}
|
||||
if (innerParameters != null && !curveOid.equals(innerParameters)) {
|
||||
throw new InvalidKeySpecException("the EC private key's own parameters must also name the "
|
||||
+ curveName + " curve");
|
||||
}
|
||||
|
||||
ECNamedCurveParameterSpec spec = ECNamedCurveTable.getParameterSpec(curveName);
|
||||
if (privateScalar.signum() <= 0 || privateScalar.compareTo(spec.getN()) >= 0) {
|
||||
throw new InvalidKeySpecException("the private key is out of range for " + curveName);
|
||||
}
|
||||
ECPoint publicPoint = spec.getG().multiply(privateScalar).normalize();
|
||||
requireMatchingEmbeddedPublicKey(embeddedPublicKey, publicPoint, spec);
|
||||
|
||||
try {
|
||||
SubjectPublicKeyInfo publicKeyInfo = new SubjectPublicKeyInfo(
|
||||
new AlgorithmIdentifier(X9ObjectIdentifiers.id_ecPublicKey, curveOid),
|
||||
publicPoint.getEncoded(false));
|
||||
return new ImportedEcKey(privateKeyInfo.getEncoded(ASN1Encoding.DER),
|
||||
publicKeyInfo.getEncoded(ASN1Encoding.DER));
|
||||
} catch (IOException e) {
|
||||
throw new InvalidKeySpecException("the key could not be re-encoded", e);
|
||||
}
|
||||
}
|
||||
|
||||
private static void requireMatchingEmbeddedPublicKey(ASN1BitString embedded, ECPoint derived,
|
||||
ECNamedCurveParameterSpec spec)
|
||||
throws InvalidKeySpecException {
|
||||
if (embedded == null) {
|
||||
return;
|
||||
}
|
||||
ECPoint embeddedPoint;
|
||||
try {
|
||||
embeddedPoint = spec.getCurve().decodePoint(embedded.getOctets());
|
||||
} catch (RuntimeException e) {
|
||||
throw new InvalidKeySpecException("the embedded public key is not a point on the curve", e);
|
||||
}
|
||||
if (!embeddedPoint.equals(derived)) {
|
||||
throw new InvalidKeySpecException("the embedded public key does not match the private key");
|
||||
}
|
||||
}
|
||||
|
||||
private record ImportedEcKey(byte[] privateKeyEncoded, byte[] publicKeyEncoded) {
|
||||
}
|
||||
|
||||
private static ECDomainParameters domainParameters(String curveName) {
|
||||
ECNamedCurveParameterSpec spec = ECNamedCurveTable.getParameterSpec(curveName);
|
||||
return new ECDomainParameters(spec.getCurve(), spec.getG(), spec.getN(), spec.getH());
|
||||
|
||||
@@ -19,6 +19,11 @@ final class EccNistKeyType implements KmsKeyType {
|
||||
AsymmetricKeys.store(key, generator.generateKeyPair());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void importKeyMaterial(KmsKey key, byte[] material) {
|
||||
BcEcKeys.importPrivateKey(key, material, key.getKeySpec().curveName());
|
||||
}
|
||||
|
||||
@Override
|
||||
public byte[] sign(KmsKey key, byte[] message, KmsKeySpec.Algorithm algorithm, KmsMessageType messageType)
|
||||
throws GeneralSecurityException {
|
||||
|
||||
+5
@@ -33,6 +33,11 @@ final class EccSecgP256k1KeyType implements KmsKeyType {
|
||||
AsymmetricKeys.store(key, BcEcKeys.generateKeyPair(CURVE));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void importKeyMaterial(KmsKey key, byte[] material) {
|
||||
BcEcKeys.importPrivateKey(key, material, CURVE);
|
||||
}
|
||||
|
||||
@Override
|
||||
public byte[] sign(KmsKey key, byte[] message, KmsKeySpec.Algorithm algorithm, KmsMessageType messageType)
|
||||
throws GeneralSecurityException, IOException {
|
||||
|
||||
Reference in new issue
Block a user