mirror of
https://github.com/floci-io/floci.git
synced 2026-11-26 23:29:28 -05:00
perf(build): replace BouncyCastle with JDK crypto API as much as possible (#2988)
* perf(security): stop registering the BouncyCastle JCE provider ACM signs through the JDK and builds PBES2 there, and KMS PSS uses RSASSA-PSS with the parameters AWS specifies. Downsize Docker image from 88.7 to 83.9 MB gzipped. * test(compat): verify the native image's KMS and ACM crypto from outside Floci Floci verifying its own signature proves nothing. Signatures now check against GetPublicKey with an outside library, exported keys are decrypted, and issued certificates verify their own signature. * fix(kms): apply real PSS padding when signing a digest with RSASSA_PSS * fix(acm): set the PBES2 salt and iteration count explicitly * perf(kms): reuse the shared SecureRandom when signing a PSS digest * fix(kms): let AwsException pass through the sign and verify catch blocks * perf(native): drop the native-image config the BouncyCastle provider needed Five --initialize-at-run-time entries and 21 reflect-config entries existed for the BouncyCastle JCE provider. Nothing registers that provider any more, so they only kept dead initialization code in the image. The run-time initialization entries are what cost size. Removing them takes the binary from 204.6 to 203.1 MB and gzip -9 from 79.5 to 78.9 MB. The reflect-config entries measured as noise, because those classes stay registered through another path, but they still describe a provider Floci no longer installs. Verified on a native build. The Python suite passes 354 tests, the Java SDK suite passes 61 including CloudHSM v2 and ACM, and a TLS run generates its self-signed certificate and signs with secp256k1 over HTTPS.
This commit is contained in:
1 parent
21f165b2e9
commit
ffe8a92635
10 files changed
+313
-267
No files matched your search
@@ -7,7 +7,7 @@ import pytest
|
||||
from botocore.exceptions import ClientError
|
||||
from cryptography import x509
|
||||
from cryptography.hazmat.primitives import hashes, serialization
|
||||
from cryptography.hazmat.primitives.asymmetric import rsa
|
||||
from cryptography.hazmat.primitives.asymmetric import ec, padding, rsa
|
||||
from cryptography.x509.oid import NameOID
|
||||
|
||||
FAKE_ARN = "arn:aws:acm:us-east-1:000000000000:certificate/00000000-0000-0000-0000-000000000000"
|
||||
@@ -27,6 +27,38 @@ class TestACMCertificateLifecycle:
|
||||
finally:
|
||||
acm_client.delete_certificate(CertificateArn=arn)
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
"key_algorithm,key_type",
|
||||
[("RSA_2048", rsa.RSAPublicKey), ("EC_prime256v1", ec.EllipticCurvePublicKey)],
|
||||
)
|
||||
def test_request_certificate_key_algorithms(
|
||||
self, acm_client, key_algorithm, key_type
|
||||
):
|
||||
"""Test RequestCertificate issues a parseable certificate for each key algorithm."""
|
||||
response = acm_client.request_certificate(
|
||||
DomainName=f"{key_algorithm.lower().replace('_', '-')}.example.com",
|
||||
KeyAlgorithm=key_algorithm,
|
||||
)
|
||||
arn = response["CertificateArn"]
|
||||
|
||||
try:
|
||||
pem = acm_client.get_certificate(CertificateArn=arn)["Certificate"]
|
||||
certificate = x509.load_pem_x509_certificate(pem.encode())
|
||||
assert isinstance(certificate.public_key(), key_type)
|
||||
# A certificate whose signature does not match its own contents would
|
||||
# still parse, so check the self-signature rather than trusting the PEM.
|
||||
certificate.public_key().verify(
|
||||
certificate.signature,
|
||||
certificate.tbs_certificate_bytes,
|
||||
*(
|
||||
(ec.ECDSA(certificate.signature_hash_algorithm),)
|
||||
if isinstance(certificate.public_key(), ec.EllipticCurvePublicKey)
|
||||
else (padding.PKCS1v15(), certificate.signature_hash_algorithm)
|
||||
),
|
||||
)
|
||||
finally:
|
||||
acm_client.delete_certificate(CertificateArn=arn)
|
||||
|
||||
def test_describe_certificate(self, acm_client):
|
||||
"""Test DescribeCertificate returns certificate details."""
|
||||
response = acm_client.request_certificate(DomainName="describe.example.com")
|
||||
@@ -164,8 +196,9 @@ class TestACMImportExport:
|
||||
acm_client.delete_certificate(CertificateArn=arn)
|
||||
|
||||
def test_export_certificate(self, acm_client):
|
||||
"""Test ExportCertificate on imported cert returns cert and key."""
|
||||
"""Test ExportCertificate returns the imported key, encrypted under the passphrase."""
|
||||
cert_pem, key_pem = self._generate_self_signed_cert()
|
||||
passphrase = b"test-passphrase"
|
||||
|
||||
import_response = acm_client.import_certificate(
|
||||
Certificate=cert_pem, PrivateKey=key_pem
|
||||
@@ -174,10 +207,27 @@ class TestACMImportExport:
|
||||
|
||||
try:
|
||||
response = acm_client.export_certificate(
|
||||
CertificateArn=arn, Passphrase=b"test-passphrase"
|
||||
CertificateArn=arn, Passphrase=passphrase
|
||||
)
|
||||
assert response["Certificate"]
|
||||
assert response["PrivateKey"]
|
||||
|
||||
# Asserting on the PEM header alone would pass for a key no client can
|
||||
# open. Decrypt it with the passphrase and check it is the key that was
|
||||
# imported, which is the whole point of the export.
|
||||
exported_key = serialization.load_pem_private_key(
|
||||
response["PrivateKey"].encode(), password=passphrase
|
||||
)
|
||||
imported_key = serialization.load_pem_private_key(key_pem, password=None)
|
||||
assert (
|
||||
exported_key.private_numbers() == imported_key.private_numbers()
|
||||
)
|
||||
|
||||
# The passphrase has to actually be doing the work.
|
||||
with pytest.raises(ValueError):
|
||||
serialization.load_pem_private_key(
|
||||
response["PrivateKey"].encode(), password=b"wrong-passphrase"
|
||||
)
|
||||
finally:
|
||||
acm_client.delete_certificate(CertificateArn=arn)
|
||||
|
||||
|
||||
@@ -2,9 +2,38 @@
|
||||
|
||||
import hashlib
|
||||
|
||||
import boto3
|
||||
import pytest
|
||||
from botocore.exceptions import ClientError
|
||||
from cryptography.hazmat.primitives import serialization
|
||||
from cryptography.hazmat.primitives import hashes, serialization
|
||||
from cryptography.hazmat.primitives.asymmetric import ec, padding
|
||||
|
||||
|
||||
# Every signing algorithm each key spec declares. RSA key generation is the only
|
||||
# real cost here and the fixture below creates one key per spec, so covering the
|
||||
# full matrix is cheaper than the per-case keys it replaced.
|
||||
RSA_SIGNING_ALGORITHMS = [
|
||||
("RSASSA_PSS_SHA_256", hashes.SHA256),
|
||||
("RSASSA_PSS_SHA_384", hashes.SHA384),
|
||||
("RSASSA_PSS_SHA_512", hashes.SHA512),
|
||||
("RSASSA_PKCS1_V1_5_SHA_256", hashes.SHA256),
|
||||
("RSASSA_PKCS1_V1_5_SHA_384", hashes.SHA384),
|
||||
("RSASSA_PKCS1_V1_5_SHA_512", hashes.SHA512),
|
||||
]
|
||||
|
||||
SIGNING_CASES = [
|
||||
(key_spec, algorithm, digest)
|
||||
for key_spec in ("RSA_2048", "RSA_3072", "RSA_4096")
|
||||
for algorithm, digest in RSA_SIGNING_ALGORITHMS
|
||||
] + [
|
||||
("ECC_NIST_P256", "ECDSA_SHA_256", hashes.SHA256),
|
||||
("ECC_NIST_P384", "ECDSA_SHA_384", hashes.SHA384),
|
||||
("ECC_NIST_P521", "ECDSA_SHA_512", hashes.SHA512),
|
||||
# secp256k1 is signed with BouncyCastle's lightweight signer, reached directly
|
||||
# rather than through a JCA provider, and the JDK dropped the curve. It is the
|
||||
# crypto path most likely to break in a native image.
|
||||
("ECC_SECG_P256K1", "ECDSA_SHA_256", hashes.SHA256),
|
||||
]
|
||||
|
||||
|
||||
class TestKMSKey:
|
||||
@@ -207,37 +236,73 @@ class TestKMSEncryption:
|
||||
class TestKMSSigning:
|
||||
"""Test KMS signing operations."""
|
||||
|
||||
def test_sign_and_verify(self, kms_client):
|
||||
"""Test Sign and Verify with RSA key."""
|
||||
# Create an asymmetric signing key
|
||||
response = kms_client.create_key(
|
||||
Description="pytest-signing-key",
|
||||
KeyUsage="SIGN_VERIFY",
|
||||
KeySpec="RSA_2048",
|
||||
)
|
||||
key_id = response["KeyMetadata"]["KeyId"]
|
||||
@pytest.fixture(scope="class")
|
||||
@staticmethod
|
||||
def signing_key(aws_config, client_config):
|
||||
"""Return one signing key per key spec, created on first use.
|
||||
|
||||
RSA key generation dominates the runtime of this class, so the key is
|
||||
shared across every algorithm tested against it.
|
||||
"""
|
||||
client = boto3.client("kms", config=client_config, **aws_config)
|
||||
created = {}
|
||||
|
||||
def key_for(key_spec):
|
||||
if key_spec not in created:
|
||||
created[key_spec] = client.create_key(
|
||||
Description="pytest-signing-key",
|
||||
KeyUsage="SIGN_VERIFY",
|
||||
KeySpec=key_spec,
|
||||
)["KeyMetadata"]["KeyId"]
|
||||
return created[key_spec]
|
||||
|
||||
yield key_for
|
||||
for key_id in created.values():
|
||||
client.schedule_key_deletion(KeyId=key_id, PendingWindowInDays=7)
|
||||
|
||||
@pytest.mark.parametrize("key_spec,signing_algorithm,digest", SIGNING_CASES)
|
||||
def test_sign_and_verify(
|
||||
self, kms_client, signing_key, key_spec, signing_algorithm, digest
|
||||
):
|
||||
"""Test Sign and Verify, and check the signature against an outside library."""
|
||||
key_id = signing_key(key_spec)
|
||||
message = b"message to sign"
|
||||
|
||||
try:
|
||||
# Sign the message
|
||||
sign_response = kms_client.sign(
|
||||
KeyId=key_id,
|
||||
Message=message,
|
||||
SigningAlgorithm="RSASSA_PSS_SHA_256",
|
||||
)
|
||||
signature = sign_response["Signature"]
|
||||
assert signature
|
||||
# Sign the message
|
||||
sign_response = kms_client.sign(
|
||||
KeyId=key_id,
|
||||
Message=message,
|
||||
SigningAlgorithm=signing_algorithm,
|
||||
)
|
||||
signature = sign_response["Signature"]
|
||||
assert signature
|
||||
|
||||
# Verify the signature
|
||||
verify_response = kms_client.verify(
|
||||
KeyId=key_id,
|
||||
Message=message,
|
||||
Signature=signature,
|
||||
SigningAlgorithm="RSASSA_PSS_SHA_256",
|
||||
)
|
||||
assert verify_response["SignatureValid"]
|
||||
finally:
|
||||
kms_client.schedule_key_deletion(KeyId=key_id, PendingWindowInDays=7)
|
||||
# Verify the signature
|
||||
verify_response = kms_client.verify(
|
||||
KeyId=key_id,
|
||||
Message=message,
|
||||
Signature=signature,
|
||||
SigningAlgorithm=signing_algorithm,
|
||||
)
|
||||
assert verify_response["SignatureValid"]
|
||||
|
||||
# Sign and Verify are both Floci, so agreeing with each other proves
|
||||
# nothing about the padding, the mask generation function or the salt
|
||||
# length. Check the signature against GetPublicKey with the parameters
|
||||
# AWS documents for this algorithm instead.
|
||||
public_key = serialization.load_der_public_key(
|
||||
kms_client.get_public_key(KeyId=key_id)["PublicKey"]
|
||||
)
|
||||
if signing_algorithm.startswith("ECDSA"):
|
||||
public_key.verify(signature, message, ec.ECDSA(digest()))
|
||||
else:
|
||||
if signing_algorithm.startswith("RSASSA_PSS"):
|
||||
scheme = padding.PSS(
|
||||
mgf=padding.MGF1(digest()), salt_length=digest.digest_size
|
||||
)
|
||||
else:
|
||||
scheme = padding.PKCS1v15()
|
||||
public_key.verify(signature, message, scheme, digest())
|
||||
|
||||
|
||||
def test_ed25519_sign_and_verify(self, kms_client):
|
||||
|
||||
Reference in new issue
Block a user