Skip to content

armgit / floci

Public Imported from GitHub
0

Light, fluffy, and always free - The AWS Local Emulator alternative

fix(tls): add key identifiers to local CA certificates (#4644)

XuJian600797db1717
diff --git a/docs/configuration/tls.md b/docs/configuration/tls.md
index cd0917630d..2f49b30f73 100644
--- a/docs/configuration/tls.md
+++ b/docs/configuration/tls.md
@@ -65,7 +65,7 @@ When `FLOCI_TLS_ENABLED=true` and no custom certificate is provided, Floci keeps
- Automatically includes custom hostnames from `FLOCI_HOSTNAME`, `FLOCI_BASE_URL` and `FLOCI_SERVICES_IOT_ENDPOINT_ADDRESS` in the SANs
- Is regenerated when hostname configuration changes between restarts, or when it was not issued by the current CA
-The CA is created once and never rotates on its own. If its files are missing, corrupt or do not match each other, Floci generates a new CA, logs a warning, and reissues the server certificate; clients then need the new `ca.pem`.
+The CA key is created once and never rotates on its own. On the first start after upgrading from a version whose CA lacks X.509 key identifiers, Floci reissues the CA certificate with the same key and validity period, then reissues the server certificate. The CA certificate's fingerprint changes, so clients using a previously downloaded CA file must import the current `ca.pem` again. ACM and IoT certificates issued before the upgrade are not rewritten; reissue them if a strict client must verify them. If the CA files are missing, corrupt or do not match each other, Floci generates a new CA and key, logs a warning, and reissues the server certificate; clients then need the new `ca.pem`.
### Custom Hostname Support
diff --git a/src/main/java/io/github/hectorvent/floci/config/FlociCertificateAuthority.java b/src/main/java/io/github/hectorvent/floci/config/FlociCertificateAuthority.java
index c6b43bf977..141bd24b6d 100644
--- a/src/main/java/io/github/hectorvent/floci/config/FlociCertificateAuthority.java
+++ b/src/main/java/io/github/hectorvent/floci/config/FlociCertificateAuthority.java
@@ -7,6 +7,7 @@ import org.bouncycastle.asn1.pkcs.PKCSObjectIdentifiers;
import org.bouncycastle.asn1.pkcs.RSAPublicKey;
import org.bouncycastle.asn1.sec.SECObjectIdentifiers;
import org.bouncycastle.asn1.x500.X500Name;
+import org.bouncycastle.asn1.x509.Extension;
import org.bouncycastle.asn1.x509.SubjectPublicKeyInfo;
import org.bouncycastle.asn1.x9.X962Parameters;
import org.bouncycastle.asn1.x9.X9ObjectIdentifiers;
@@ -20,6 +21,7 @@ import java.io.IOException;
import java.io.StringReader;
import java.nio.file.Files;
import java.nio.file.Path;
+import java.nio.file.StandardCopyOption;
import java.nio.file.attribute.PosixFileAttributeView;
import java.nio.file.attribute.PosixFilePermissions;
import java.security.KeyPair;
@@ -81,6 +83,7 @@ public final class FlociCertificateAuthority {
Path keyFile = tlsDir.resolve(CA_KEY_NAME);
try {
if (Files.exists(certFile) && Files.exists(keyFile)) {
+ FlociCertificateAuthority existing = null;
try {
String pem = Files.readString(certFile);
X509Certificate cert = generator.parseCertificate(pem);
@@ -95,14 +98,17 @@ public final class FlociCertificateAuthority {
}
restrictToOwnerOnly(tlsDir, "rwx------");
restrictToOwnerOnly(keyFile, "rw-------");
- FlociCertificateAuthority ca = new FlociCertificateAuthority(certFile, cert, key, pem, generator);
- LOG.infov("TLS: using local CA {0} ({1}), SHA256 fingerprint {2}", certFile,
- cert.getSubjectX500Principal().getName(), ca.fingerprint());
- return ca;
+ existing = new FlociCertificateAuthority(certFile, cert, key, pem, generator);
} catch (Exception e) {
LOG.warnv(e, "TLS: local CA at {0} is unusable ({1}); generating a new one. Clients that trusted "
+ "the old CA must re-import {2}", tlsDir, e.getMessage(), CA_CERT_NAME);
}
+ if (existing != null) {
+ FlociCertificateAuthority ca = upgradeLegacyCa(existing);
+ LOG.infov("TLS: using local CA {0} ({1}), SHA256 fingerprint {2}", certFile,
+ ca.certificate().getSubjectX500Principal().getName(), ca.fingerprint());
+ return ca;
+ }
}
Files.createDirectories(tlsDir);
restrictToOwnerOnly(tlsDir, "rwx------");
@@ -155,6 +161,43 @@ public final class FlociCertificateAuthority {
return new CertificateGenerator.Issuer(certificate, key);
}
+ private static FlociCertificateAuthority upgradeLegacyCa(FlociCertificateAuthority existing) {
+ X509Certificate cert = existing.certificate();
+ if (cert.getExtensionValue(Extension.subjectKeyIdentifier.getId()) != null
+ && cert.getExtensionValue(Extension.authorityKeyIdentifier.getId()) != null) {
+ return existing;
+ }
+ Path certFile = existing.certificatePath();
+ Path temporary = null;
+ try {
+ X500Name dn = X500Name.getInstance(cert.getSubjectX500Principal().getEncoded());
+ X509Certificate upgraded = existing.generator.signCertificate(dn, cert.getPublicKey(), dn,
+ existing.key(), List.of(), true, null, cert.getNotBefore().toInstant(),
+ cert.getNotAfter().toInstant());
+ upgraded.verify(cert.getPublicKey());
+ String pem = existing.generator.toPem(upgraded);
+ temporary = Files.createTempFile(certFile.getParent(), "floci-root-ca-", ".tmp");
+ Files.writeString(temporary, pem);
+ Files.move(temporary, certFile, StandardCopyOption.ATOMIC_MOVE, StandardCopyOption.REPLACE_EXISTING);
+ FlociCertificateAuthority replacement = new FlociCertificateAuthority(certFile, upgraded,
+ existing.key(), pem, existing.generator);
+ LOG.warnv("TLS: reissued local CA with key identifiers using the existing key. Its SHA256 fingerprint changed from {0} to {1}; clients using the old CA file must re-import {2}",
+ existing.fingerprint(), replacement.fingerprint(), CA_CERT_NAME);
+ return replacement;
+ } catch (Exception e) {
+ throw new IllegalStateException("Failed to upgrade local CA at " + certFile
+ + "; the existing CA key was not replaced", e);
+ } finally {
+ if (temporary != null) {
+ try {
+ Files.deleteIfExists(temporary);
+ } catch (IOException e) {
+ LOG.warnv("TLS: could not remove temporary CA file {0}: {1}", temporary, e.getMessage());
+ }
+ }
+ }
+ }
+
/**
* A {@code serverAuth} leaf for {@code commonName} plus {@code sans}. {@code keyPair} null
* mints a new key of {@code keyAlgorithm}; passing the current pair keeps the public key
@@ -216,7 +259,8 @@ public final class FlociCertificateAuthority {
}
X500Name issuerDn = X500Name.getInstance(certificate.getSubjectX500Principal().getEncoded());
return generator.signCertificate(csr.getSubject(), subjectKey, issuerDn, key, List.of(), false,
- CertificateGenerator.LeafUsage.CLIENT, Instant.now(), deviceCertificateNotAfter());
+ CertificateGenerator.LeafUsage.CLIENT, Instant.now(), deviceCertificateNotAfter(),
+ certificate.getPublicKey());
} catch (IllegalArgumentException e) {
throw e;
} catch (Exception e) {
diff --git a/src/main/java/io/github/hectorvent/floci/config/TlsConfigSource.java b/src/main/java/io/github/hectorvent/floci/config/TlsConfigSource.java
index 3008081c81..4ba7735d0e 100644
--- a/src/main/java/io/github/hectorvent/floci/config/TlsConfigSource.java
+++ b/src/main/java/io/github/hectorvent/floci/config/TlsConfigSource.java
@@ -4,6 +4,7 @@ import com.fasterxml.jackson.databind.ObjectMapper;
import io.github.hectorvent.floci.core.common.AwsRegions;
import io.github.hectorvent.floci.services.acm.CertificateGenerator;
import io.github.hectorvent.floci.services.acm.model.KeyAlgorithm;
+import org.bouncycastle.asn1.x509.Extension;
import org.eclipse.microprofile.config.spi.ConfigSource;
import org.jboss.logging.Logger;
@@ -312,6 +313,11 @@ public class TlsConfigSource implements ConfigSource {
LOG.infov("TLS: existing server certificate was not issued by the local CA; regenerating");
return false;
}
+ if (cert.getExtensionValue(Extension.subjectKeyIdentifier.getId()) == null
+ || cert.getExtensionValue(Extension.authorityKeyIdentifier.getId()) == null) {
+ LOG.info("TLS: existing server certificate lacks key identifiers; regenerating");
+ return false;
+ }
cert.checkValidity();
return true;
} catch (Exception e) {
diff --git a/src/main/java/io/github/hectorvent/floci/services/acm/CertificateGenerator.java b/src/main/java/io/github/hectorvent/floci/services/acm/CertificateGenerator.java
index 9d7770d2fc..6bd44bc83f 100644
--- a/src/main/java/io/github/hectorvent/floci/services/acm/CertificateGenerator.java
+++ b/src/main/java/io/github/hectorvent/floci/services/acm/CertificateGenerator.java
@@ -22,6 +22,7 @@ import org.bouncycastle.asn1.x9.X962Parameters;
import org.bouncycastle.cert.X509CertificateHolder;
import org.bouncycastle.cert.X509v3CertificateBuilder;
import org.bouncycastle.cert.jcajce.JcaX509CertificateConverter;
+import org.bouncycastle.cert.jcajce.JcaX509ExtensionUtils;
import org.bouncycastle.cert.jcajce.JcaX509v3CertificateBuilder;
import org.bouncycastle.openssl.PEMKeyPair;
import org.bouncycastle.openssl.PEMParser;
@@ -181,7 +182,8 @@ public class CertificateGenerator {
X500Name subject = new X500Name("CN=" + domainName);
X500Name issuerDn = X500Name.getInstance(issuer.certificate().getSubjectX500Principal().getEncoded());
X509Certificate cert = signCertificate(subject, keyPair.getPublic(), issuerDn, issuer.key(),
- withDomainFirst(domainName, sans), false, usage, Instant.now(), notAfter);
+ withDomainFirst(domainName, sans), false, usage, Instant.now(), notAfter,
+ issuer.certificate().getPublicKey());
cert.verify(issuer.certificate().getPublicKey());
return toGenerated(cert, keyPair.getPrivate(), subject.toString(),
issuer.certificate().getSubjectX500Principal().getName());
@@ -258,10 +260,28 @@ public class CertificateGenerator {
public X509Certificate signCertificate(X500Name subject, PublicKey subjectKey, X500Name issuerDn,
PrivateKey issuerKey, List<String> sans, boolean asCa,
LeafUsage usage, Instant notBefore, Instant notAfter) throws Exception {
+ PublicKey issuerPublicKey = asCa && subject.equals(issuerDn) ? subjectKey : null;
+ return signCertificate(subject, subjectKey, issuerDn, issuerKey, sans, asCa, usage,
+ notBefore, notAfter, issuerPublicKey);
+ }
+
+ /** Supply the issuer's public key for CA-signed leaves so their AKI matches the CA's SKI. */
+ public X509Certificate signCertificate(X500Name subject, PublicKey subjectKey, X500Name issuerDn,
+ PrivateKey issuerKey, List<String> sans, boolean asCa,
+ LeafUsage usage, Instant notBefore, Instant notAfter,
+ PublicKey issuerPublicKey) throws Exception {
BigInteger serial = new BigInteger(128, SECURE_RANDOM);
X509v3CertificateBuilder certBuilder = new JcaX509v3CertificateBuilder(
issuerDn, serial, Date.from(notBefore), Date.from(notAfter), subject, subjectKey);
+ JcaX509ExtensionUtils extensionUtils = new JcaX509ExtensionUtils();
+ certBuilder.addExtension(Extension.subjectKeyIdentifier, false,
+ extensionUtils.createSubjectKeyIdentifier(subjectKey));
+ if (issuerPublicKey != null) {
+ certBuilder.addExtension(Extension.authorityKeyIdentifier, false,
+ extensionUtils.createAuthorityKeyIdentifier(issuerPublicKey));
+ }
+
if (sans != null && !sans.isEmpty()) {
List<GeneralName> sanList = new ArrayList<>();
for (String san : new LinkedHashSet<>(sans)) {
diff --git a/src/test/java/io/github/hectorvent/floci/config/FlociCertificateAuthorityTest.java b/src/test/java/io/github/hectorvent/floci/config/FlociCertificateAuthorityTest.java
index af5ee540a3..5900001161 100644
--- a/src/test/java/io/github/hectorvent/floci/config/FlociCertificateAuthorityTest.java
+++ b/src/test/java/io/github/hectorvent/floci/config/FlociCertificateAuthorityTest.java
@@ -3,6 +3,11 @@ package io.github.hectorvent.floci.config;
import io.github.hectorvent.floci.services.acm.CertificateGenerator;
import io.github.hectorvent.floci.services.acm.model.KeyAlgorithm;
import org.bouncycastle.asn1.x500.X500Name;
+import org.bouncycastle.asn1.x509.BasicConstraints;
+import org.bouncycastle.asn1.x509.Extension;
+import org.bouncycastle.asn1.x509.KeyUsage;
+import org.bouncycastle.cert.jcajce.JcaX509CertificateConverter;
+import org.bouncycastle.cert.jcajce.JcaX509v3CertificateBuilder;
import org.bouncycastle.openssl.jcajce.JcaPEMWriter;
import org.bouncycastle.operator.jcajce.JcaContentSignerBuilder;
import org.bouncycastle.pkcs.jcajce.JcaPKCS10CertificationRequestBuilder;
@@ -10,6 +15,7 @@ import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir;
import java.io.StringWriter;
+import java.math.BigInteger;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.attribute.PosixFilePermission;
@@ -17,6 +23,9 @@ import java.nio.file.attribute.PosixFilePermissions;
import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.cert.X509Certificate;
+import java.time.Instant;
+import java.time.temporal.ChronoUnit;
+import java.util.Date;
import java.util.List;
import java.util.Set;
import javax.security.auth.x500.X500Principal;
@@ -24,6 +33,7 @@ import javax.security.auth.x500.X500Principal;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNotEquals;
+import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
@@ -77,6 +87,37 @@ class FlociCertificateAuthorityTest {
Files.getPosixFilePermissions(tempDir.resolve("floci-root-ca.key")), "permissions are re-tightened on load");
}
+ @Test
+ void upgradesLegacyCaWithoutChangingItsKeyOrExpiry() throws Exception {
+ CertificateGenerator generator = new CertificateGenerator();
+ KeyPair keyPair = rsaKeyPair(2048);
+ X500Name dn = new X500Name("CN=" + FlociCertificateAuthority.COMMON_NAME);
+ Instant notBefore = Instant.now().minus(1, ChronoUnit.DAYS);
+ Instant notAfter = Instant.now().plus(365, ChronoUnit.DAYS);
+ JcaX509v3CertificateBuilder builder = new JcaX509v3CertificateBuilder(dn, BigInteger.ONE,
+ Date.from(notBefore), Date.from(notAfter), dn, keyPair.getPublic());
+ builder.addExtension(Extension.basicConstraints, true, new BasicConstraints(true));
+ builder.addExtension(Extension.keyUsage, true,
+ new KeyUsage(KeyUsage.digitalSignature | KeyUsage.keyCertSign | KeyUsage.cRLSign));
+ X509Certificate legacy = new JcaX509CertificateConverter().getCertificate(
+ builder.build(new JcaContentSignerBuilder("SHA512WithRSA").build(keyPair.getPrivate())));
+ assertNull(legacy.getExtensionValue(Extension.subjectKeyIdentifier.getId()));
+ Files.writeString(tempDir.resolve(FlociCertificateAuthority.CA_CERT_NAME), generator.toPem(legacy));
+ String privateKeyPem = generator.toPem(keyPair.getPrivate());
+ Files.writeString(tempDir.resolve(FlociCertificateAuthority.CA_KEY_NAME), privateKeyPem);
+
+ FlociCertificateAuthority upgraded = FlociCertificateAuthority.loadOrCreate(tempDir);
+
+ assertEquals(legacy.getPublicKey(), upgraded.certificate().getPublicKey());
+ assertEquals(legacy.getNotAfter(), upgraded.certificate().getNotAfter());
+ assertEquals(privateKeyPem, Files.readString(tempDir.resolve(FlociCertificateAuthority.CA_KEY_NAME)));
+ assertTrue(upgraded.certificate().getExtensionValue(Extension.subjectKeyIdentifier.getId()) != null);
+ assertTrue(upgraded.certificate().getExtensionValue(Extension.authorityKeyIdentifier.getId()) != null);
+ assertNotEquals(legacy, upgraded.certificate());
+ assertEquals(upgraded.fingerprint(), FlociCertificateAuthority.loadOrCreate(tempDir).fingerprint(),
+ "migration must happen only once");
+ }
+
@Test
void corruptCaIsRegeneratedNotSilentlyKept() throws Exception {
FlociCertificateAuthority first = FlociCertificateAuthority.loadOrCreate(tempDir);
@@ -168,6 +209,8 @@ class FlociCertificateAuthorityTest {
cert.verify(ca.certificate().getPublicKey());
assertEquals("CN=AWS IoT Certificate", cert.getSubjectX500Principal().getName());
assertEquals(List.of("1.3.6.1.5.5.7.3.2"), cert.getExtendedKeyUsage());
+ assertTrue(cert.getExtensionValue(Extension.subjectKeyIdentifier.getId()) != null);
+ assertTrue(cert.getExtensionValue(Extension.authorityKeyIdentifier.getId()) != null);
assertTrue(ca.isIssuedByUs(cert));
assertEquals(java.time.Instant.parse("2049-12-31T23:59:59Z"), cert.getNotAfter().toInstant(), "AWS's fixed expiry");
assertEquals(java.time.Instant.parse("2050-12-31T23:59:59Z"), ca.certificate().getNotAfter().toInstant(),
@@ -244,6 +287,8 @@ class FlociCertificateAuthorityTest {
new X500Name("CN=device-42,O=Example").getEncoded()), cert.getSubjectX500Principal());
assertEquals(List.of("1.3.6.1.5.5.7.3.2"), cert.getExtendedKeyUsage());
assertEquals(-1, cert.getBasicConstraints());
+ assertTrue(cert.getExtensionValue(Extension.subjectKeyIdentifier.getId()) != null);
+ assertTrue(cert.getExtensionValue(Extension.authorityKeyIdentifier.getId()) != null);
assertTrue(ca.isIssuedByUs(cert));
}
diff --git a/src/test/java/io/github/hectorvent/floci/config/TlsConfigSourceCertificateReuseTest.java b/src/test/java/io/github/hectorvent/floci/config/TlsConfigSourceCertificateReuseTest.java
index a0a8ba459f..a7ddc2bd5a 100644
--- a/src/test/java/io/github/hectorvent/floci/config/TlsConfigSourceCertificateReuseTest.java
+++ b/src/test/java/io/github/hectorvent/floci/config/TlsConfigSourceCertificateReuseTest.java
@@ -1,5 +1,8 @@
package io.github.hectorvent.floci.config;
+import io.github.hectorvent.floci.services.acm.CertificateGenerator;
+import org.bouncycastle.asn1.x500.X500Name;
+import org.bouncycastle.asn1.x509.Extension;
import org.bouncycastle.jce.provider.BouncyCastleProvider;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeAll;
@@ -8,7 +11,11 @@ import org.junit.jupiter.api.io.TempDir;
import java.nio.file.Files;
import java.nio.file.Path;
+import java.security.KeyPair;
+import java.security.KeyPairGenerator;
import java.security.Security;
+import java.security.cert.X509Certificate;
+import java.util.List;
import static org.junit.jupiter.api.Assertions.*;
@@ -179,6 +186,35 @@ class TlsConfigSourceCertificateReuseTest {
"Metadata should be unchanged");
}
+ @Test
+ void legacyServerLeafWithoutKeyIdentifiersIsReissued() throws Exception {
+ System.setProperty("floci.tls.enabled", "true");
+ System.setProperty("floci.tls.self-signed", "true");
+ System.setProperty("floci.storage.persistent-path", tempDir.toString());
+ new TlsConfigSource();
+
+ Path tlsDir = tempDir.resolve("tls");
+ Path certFile = tlsDir.resolve("floci-server.crt");
+ Path keyFile = tlsDir.resolve("floci-server.key");
+ FlociCertificateAuthority ca = FlociCertificateAuthority.loadOrCreate(tlsDir);
+ KeyPair keyPair = KeyPairGenerator.getInstance("RSA").generateKeyPair();
+ CertificateGenerator generator = new CertificateGenerator();
+ X509Certificate legacy = generator.signCertificate(new X500Name("CN=localhost"), keyPair.getPublic(),
+ new X500Name("CN=Floci Local CA"), ca.key(), List.of("localhost"), false,
+ CertificateGenerator.LeafUsage.SERVER, 365);
+ Files.writeString(certFile, generator.toPem(legacy));
+ Files.writeString(keyFile, generator.toPem(keyPair.getPrivate()));
+ assertNull(legacy.getExtensionValue(Extension.authorityKeyIdentifier.getId()));
+
+ new TlsConfigSource();
+
+ X509Certificate reissued = generator.parseCertificate(Files.readString(certFile));
+ assertNotEquals(legacy, reissued);
+ assertNotNull(reissued.getExtensionValue(Extension.subjectKeyIdentifier.getId()));
+ assertNotNull(reissued.getExtensionValue(Extension.authorityKeyIdentifier.getId()));
+ reissued.verify(ca.certificate().getPublicKey());
+ }
+
/**
* Regression: TlsConfigSource is a MicroProfile ConfigSource, so it runs before CDI and before
* the security provider is registered — BouncyCastle is NOT yet available. Previously
diff --git a/src/test/java/io/github/hectorvent/floci/config/TlsLocalCaIntegrationTest.java b/src/test/java/io/github/hectorvent/floci/config/TlsLocalCaIntegrationTest.java
index 8e388328cc..a2acfd56dc 100644
--- a/src/test/java/io/github/hectorvent/floci/config/TlsLocalCaIntegrationTest.java
+++ b/src/test/java/io/github/hectorvent/floci/config/TlsLocalCaIntegrationTest.java
@@ -4,6 +4,7 @@ import io.github.hectorvent.floci.services.acm.model.KeyAlgorithm;
import io.quarkus.test.junit.QuarkusTest;
import io.quarkus.test.junit.QuarkusTestProfile;
import io.quarkus.test.junit.TestProfile;
+import org.bouncycastle.asn1.x509.Extension;
import org.eclipse.microprofile.config.inject.ConfigProperty;
import org.junit.jupiter.api.Test;
@@ -26,6 +27,7 @@ import java.util.Map;
import static io.restassured.RestAssured.given;
import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertThrows;
/**
@@ -45,6 +47,8 @@ class TlsLocalCaIntegrationTest {
void handshakeSucceedsTrustingOnlyTheServedCa() throws Exception {
String caPem = given().when().get("/_floci/ca.pem").then().statusCode(200).extract().asString();
X509Certificate ca = parse(caPem);
+ assertNotNull(ca.getExtensionValue(Extension.subjectKeyIdentifier.getId()));
+ assertNotNull(ca.getExtensionValue(Extension.authorityKeyIdentifier.getId()));
HttpsURLConnection connection = open(trustOnly(ca));
assertEquals(200, connection.getResponseCode());
@@ -52,6 +56,8 @@ class TlsLocalCaIntegrationTest {
X509Certificate leaf = (X509Certificate) served[0];
assertEquals(ca.getSubjectX500Principal(), leaf.getIssuerX500Principal());
assertEquals(-1, leaf.getBasicConstraints());
+ assertNotNull(leaf.getExtensionValue(Extension.subjectKeyIdentifier.getId()));
+ assertNotNull(leaf.getExtensionValue(Extension.authorityKeyIdentifier.getId()));
leaf.verify(ca.getPublicKey());
connection.disconnect();
}
diff --git a/src/test/java/io/github/hectorvent/floci/services/acm/CertificateGeneratorIssuerTest.java b/src/test/java/io/github/hectorvent/floci/services/acm/CertificateGeneratorIssuerTest.java
index b887f8fdbb..03920a0d37 100644
--- a/src/test/java/io/github/hectorvent/floci/services/acm/CertificateGeneratorIssuerTest.java
+++ b/src/test/java/io/github/hectorvent/floci/services/acm/CertificateGeneratorIssuerTest.java
@@ -4,6 +4,10 @@ import io.github.hectorvent.floci.services.acm.CertificateGenerator.GeneratedCer
import io.github.hectorvent.floci.services.acm.CertificateGenerator.Issuer;
import io.github.hectorvent.floci.services.acm.CertificateGenerator.LeafUsage;
import io.github.hectorvent.floci.services.acm.model.KeyAlgorithm;
+import org.bouncycastle.asn1.ASN1OctetString;
+import org.bouncycastle.asn1.x509.AuthorityKeyIdentifier;
+import org.bouncycastle.asn1.x509.Extension;
+import org.bouncycastle.asn1.x509.SubjectKeyIdentifier;
import org.bouncycastle.jce.provider.BouncyCastleProvider;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
@@ -16,6 +20,7 @@ import java.security.cert.X509Certificate;
import java.security.spec.ECGenParameterSpec;
import java.util.List;
+import static org.junit.jupiter.api.Assertions.assertArrayEquals;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNotEquals;
@@ -58,6 +63,7 @@ class CertificateGeneratorIssuerTest {
assertNull(cert.getExtendedKeyUsage(), "a CA carries no EKU");
assertNull(cert.getSubjectAlternativeNames(), "a CA carries no SAN");
cert.verify(cert.getPublicKey());
+ assertArrayEquals(subjectKeyIdentifier(cert), authorityKeyIdentifier(cert));
assertEquals(ca.subject(), ca.issuer());
}
@@ -89,6 +95,20 @@ class CertificateGeneratorIssuerTest {
assertEquals("CN=api.example.test", leaf.subject());
assertEquals(issuer.certificate().getSubjectX500Principal().getName(), leaf.issuer());
cert.verify(issuer.certificate().getPublicKey());
+ assertTrue(cert.getExtensionValue(Extension.subjectKeyIdentifier.getId()) != null);
+ assertArrayEquals(subjectKeyIdentifier(issuer.certificate()), authorityKeyIdentifier(cert));
+ }
+
+ private static byte[] subjectKeyIdentifier(X509Certificate certificate) throws Exception {
+ byte[] encoded = ASN1OctetString.getInstance(
+ certificate.getExtensionValue(Extension.subjectKeyIdentifier.getId())).getOctets();
+ return SubjectKeyIdentifier.getInstance(encoded).getKeyIdentifier();
+ }
+
+ private static byte[] authorityKeyIdentifier(X509Certificate certificate) throws Exception {
+ byte[] encoded = ASN1OctetString.getInstance(
+ certificate.getExtensionValue(Extension.authorityKeyIdentifier.getId())).getOctets();
+ return AuthorityKeyIdentifier.getInstance(encoded).getKeyIdentifier();
}
@Test