mirror of
https://github.com/floci-io/floci.git
synced 2026-11-22 21:50:46 -05:00
fix(pem): normalize provider-specific EC key aliases (#7555)
This commit is contained in:
1 parent
4e7d4a1104
commit
284ce958d0
2 files changed
+54
-3
No files matched your search
@@ -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);
|
||||
|
||||
@@ -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"));
|
||||
|
||||
Reference in new issue
Block a user