mirror of
https://github.com/floci-io/floci.git
synced 2026-12-04 10:38:00 -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)
|
||||
|
||||
|
||||
Reference in new issue
Block a user