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:
TATSUNO “Taz” Yasuhiro authored and GitHub committed 2026-09-03 21:49:52 -04:00
1 parent 21f165b2e9
commit ffe8a92635
10 files changed
+313 -267

No files matched your search

@@ -22,6 +22,8 @@ import java.security.KeyFactory;
import java.security.MessageDigest;
import java.security.PublicKey;
import java.security.Signature;
import java.security.spec.MGF1ParameterSpec;
import java.security.spec.PSSParameterSpec;
import java.security.spec.X509EncodedKeySpec;
import java.util.Arrays;
import java.util.Base64;
@@ -1258,6 +1260,53 @@ class KmsServiceTest {
assertArrayEquals(rawSig, digestSig);
}
@Test
void signWithDigestMessageTypePssVerifiesWithExternalVerifier() throws Exception {
KmsKey key = kmsService.createKey("rsa pss digest key", "SIGN_VERIFY", "RSA_2048", null, Map.of(), REGION);
byte[] message = "floci kms pss round-trip".getBytes(StandardCharsets.UTF_8);
byte[] digest = MessageDigest.getInstance("SHA-256").digest(message);
byte[] sig = kmsService.sign(key.getKeyId(), digest,
"RSASSA_PSS_SHA_256", KmsMessageType.DIGEST, REGION);
// floci's own Verify round-trips, against the digest and against the raw message.
assertTrue(kmsService.verify(key.getKeyId(), digest, sig,
"RSASSA_PSS_SHA_256", KmsMessageType.DIGEST, REGION));
assertTrue(kmsService.verify(key.getKeyId(), message, sig,
"RSASSA_PSS_SHA_256", KmsMessageType.RAW, REGION));
// External verifier with the PSS parameters AWS documents for RSASSA_PSS_SHA_256.
byte[] der = Base64.getDecoder().decode(kmsService.getPublicKey(key.getKeyId(), REGION).getPublicKeyEncoded());
PublicKey pub = KeyFactory.getInstance("RSA").generatePublic(new X509EncodedKeySpec(der));
Signature verifier = Signature.getInstance("RSASSA-PSS");
verifier.setParameter(new PSSParameterSpec("SHA-256", "MGF1", MGF1ParameterSpec.SHA256, 32, 1));
verifier.initVerify(pub);
verifier.update(message);
assertTrue(verifier.verify(sig), "DIGEST PSS signature must verify as real RSASSA-PSS");
}
@Test
void signWithInvalidAlgorithmThrowsInvalidSigningAlgorithm() {
var key = kmsService.createKey("ecdsa key", "SIGN_VERIFY", "ECC_NIST_P256", null, Map.of(), REGION);
var ex = assertThrows(AwsException.class, () ->
kmsService.sign(key.getKeyId(), "sign me".getBytes(StandardCharsets.UTF_8), "NOT_AN_ALGORITHM", REGION));
assertEquals("InvalidSigningAlgorithmException", ex.getErrorCode());
}
@Test
void verifyWithInvalidAlgorithmThrowsInvalidSigningAlgorithm() {
var key = kmsService.createKey("ecdsa key", "SIGN_VERIFY", "ECC_NIST_P256", null, Map.of(), REGION);
var message = "sign me".getBytes(StandardCharsets.UTF_8);
var sig = kmsService.sign(key.getKeyId(), message, "ECDSA_SHA_256", REGION);
var ex = assertThrows(AwsException.class, () ->
kmsService.verify(key.getKeyId(), message, sig, "NOT_AN_ALGORITHM", REGION));
assertEquals("InvalidSigningAlgorithmException", ex.getErrorCode());
}
@Test
void verifyWithWrongSignatureReturnsFalse() {
KmsKey key = kmsService.createKey("ecdsa key", "SIGN_VERIFY", "ECC_NIST_P256", null, Map.of(), REGION);