From 284ce958d0d2c08b91fe4a222cf0af43043dd84e Mon Sep 17 00:00:00 2001 From: Andre Kurait Date: Wed, 4 Nov 2026 19:53:18 -0600 Subject: [PATCH] fix(pem): normalize provider-specific EC key aliases (#7555) --- .../hectorvent/floci/core/common/Pem.java | 18 +++++++-- .../hectorvent/floci/core/common/PemTest.java | 39 +++++++++++++++++++ 2 files changed, 54 insertions(+), 3 deletions(-) diff --git a/src/main/java/io/github/hectorvent/floci/core/common/Pem.java b/src/main/java/io/github/hectorvent/floci/core/common/Pem.java index 03d6c9d532..65316899b7 100644 --- a/src/main/java/io/github/hectorvent/floci/core/common/Pem.java +++ b/src/main/java/io/github/hectorvent/floci/core/common/Pem.java @@ -10,11 +10,14 @@ import org.jboss.logging.Logger; import java.io.StringReader; import java.nio.charset.StandardCharsets; +import java.security.KeyFactory; import java.security.PrivateKey; import java.security.PublicKey; import java.security.Signature; import java.security.cert.X509Certificate; import java.security.interfaces.ECKey; +import java.security.spec.PKCS8EncodedKeySpec; +import java.security.spec.X509EncodedKeySpec; /** * Reading PEM certificate and private-key material, and checking that the two belong together. @@ -67,11 +70,20 @@ public final class Pem { /** * True when {@code privateKey} signs what {@code publicKey} verifies. Done by signing a probe - * rather than comparing key fields, so it holds for RSA and EC alike without either being - * special-cased beyond the signature algorithm. + * rather than comparing key fields, so it holds for RSA and EC alike. EC keys with a provider + * algorithm alias are re-encoded through the EC key factory before signing or verification. */ public static boolean isPair(PrivateKey privateKey, PublicKey publicKey) throws Exception { - String algorithm = privateKey instanceof ECKey ? "SHA256withECDSA" : "SHA256withRSA"; + boolean ellipticCurve = privateKey instanceof ECKey; + String algorithm = ellipticCurve ? "SHA256withECDSA" : "SHA256withRSA"; + if (ellipticCurve && !"EC".equals(privateKey.getAlgorithm())) { + privateKey = KeyFactory.getInstance("EC") + .generatePrivate(new PKCS8EncodedKeySpec(privateKey.getEncoded())); + } + if (ellipticCurve && publicKey instanceof ECKey && !"EC".equals(publicKey.getAlgorithm())) { + publicKey = KeyFactory.getInstance("EC") + .generatePublic(new X509EncodedKeySpec(publicKey.getEncoded())); + } byte[] probe = "floci".getBytes(StandardCharsets.US_ASCII); Signature signer = Signature.getInstance(algorithm); signer.initSign(privateKey); diff --git a/src/test/java/io/github/hectorvent/floci/core/common/PemTest.java b/src/test/java/io/github/hectorvent/floci/core/common/PemTest.java index e54983c8f7..151a5a75f8 100644 --- a/src/test/java/io/github/hectorvent/floci/core/common/PemTest.java +++ b/src/test/java/io/github/hectorvent/floci/core/common/PemTest.java @@ -2,9 +2,14 @@ package io.github.hectorvent.floci.core.common; import io.github.hectorvent.floci.services.acm.CertificateGenerator; import io.github.hectorvent.floci.services.acm.model.KeyAlgorithm; +import org.bouncycastle.asn1.pkcs.PrivateKeyInfo; +import org.bouncycastle.asn1.x509.SubjectPublicKeyInfo; +import org.bouncycastle.jce.provider.BouncyCastleProvider; +import org.bouncycastle.openssl.jcajce.JcaPEMKeyConverter; import org.junit.jupiter.api.Test; import java.security.PrivateKey; +import java.security.PublicKey; import java.security.cert.X509Certificate; import java.util.List; @@ -66,6 +71,40 @@ class PemTest { Pem.parseCertificate(material.certificatePem()).getPublicKey())); } + @Test + void recognisesAnEcPrivateKeyWithAProviderAlgorithmAlias() throws Exception { + CertificateGenerator.GeneratedCertificate material = GENERATOR.generateSelfSignedCertificate( + "provider.pem.test.local", List.of(), KeyAlgorithm.EC_prime256v1); + PrivateKey original = Pem.parsePrivateKey(material.privateKeyPem()); + PrivateKey providerKey = new JcaPEMKeyConverter().setProvider(new BouncyCastleProvider()) + .getPrivateKey(PrivateKeyInfo.getInstance(original.getEncoded())); + assertEquals("ECDSA", providerKey.getAlgorithm()); + assertTrue(Pem.isPair(providerKey, Pem.parseCertificate(material.certificatePem()).getPublicKey())); + } + + @Test + void recognisesAnEcPublicKeyWithAProviderAlgorithmAlias() throws Exception { + CertificateGenerator.GeneratedCertificate material = GENERATOR.generateSelfSignedCertificate( + "provider.pem.test.local", List.of(), KeyAlgorithm.EC_prime256v1); + PublicKey original = Pem.parseCertificate(material.certificatePem()).getPublicKey(); + PublicKey providerKey = new JcaPEMKeyConverter().setProvider(new BouncyCastleProvider()) + .getPublicKey(SubjectPublicKeyInfo.getInstance(original.getEncoded())); + assertEquals("ECDSA", providerKey.getAlgorithm()); + assertTrue(Pem.isPair(Pem.parsePrivateKey(material.privateKeyPem()), providerKey)); + } + + @Test + void rejectsAnUnrelatedEcKeyWithAProviderAlgorithmAlias() throws Exception { + CertificateGenerator.GeneratedCertificate one = GENERATOR.generateSelfSignedCertificate( + "one.pem.test.local", List.of(), KeyAlgorithm.EC_prime256v1); + CertificateGenerator.GeneratedCertificate other = GENERATOR.generateSelfSignedCertificate( + "other.pem.test.local", List.of(), KeyAlgorithm.EC_prime256v1); + PrivateKey original = Pem.parsePrivateKey(one.privateKeyPem()); + PrivateKey providerKey = new JcaPEMKeyConverter().setProvider(new BouncyCastleProvider()) + .getPrivateKey(PrivateKeyInfo.getInstance(original.getEncoded())); + assertFalse(Pem.isPair(providerKey, Pem.parseCertificate(other.certificatePem()).getPublicKey())); + } + @Test void refusesSomethingThatIsNotACertificate() { assertThrows(CertificateMaterialException.class, () -> Pem.parseCertificate("not a pem at all"));