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

* fix(tls): add key identifiers to local certificates

* test(tls): assert served certificate key identifiers

* style(tls): follow logging and import conventions
This commit is contained in:
XuJian authored and GitHub committed 2026-09-29 19:18:37 -05:00
1 parent 1b87ed44c0
commit 600797db17
8 files changed
+184 -7

No files matched your search

@@ -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) {
@@ -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) {
@@ -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)) {