Reproduce a leaf-only TLS chain with keytool and the JDK HTTPS server, measure six cases on JDK 23, and see why AIA fetching is not the fix.
The symptom: fine in a browser, broken in a service
A common TLS incident goes like this. A team renews the certificate on an internal API, checks the endpoint in a browser, sees the padlock, and closes the change request. An hour later a Java service that calls the API starts failing with javax.net.ssl.SSLHandshakeException and the message "PKIX path building failed: unable to find valid certification path to requested target". Nothing about the Java service changed. The certificate is valid, unexpired and issued by a trusted public authority.
The usual cause is that the server sends its leaf certificate but not the intermediate certificate that signed it. A TLS server is expected to send the chain needed to connect its leaf to a trust anchor the client already has; the root itself may be omitted, but intermediates should not be. Many browsers tolerate the omission because they can fill the gap, for example from intermediates they have cached or by fetching the issuer from the URL in the certificate's Authority Information Access extension. The JDK's default validator does neither.
This guide reproduces that difference locally with keytool and the JDK's built-in HTTPS server, measures six configurations on JDK 23.0.2, and explains why turning on the JDK's AIA option is not the fix.
What the protocol asks the server to send
TLS 1.3, in RFC 8446 section 4.4.2, says the sender's certificate must come first in the certificate list and each following certificate should directly certify the one before it. It then makes one exception: because trust anchors are distributed independently, a certificate that specifies a trust anchor may be omitted, provided supported peers are known to possess it. The root is the trust anchor and may be left out. The intermediate is not a trust anchor for ordinary clients, so leaving it out relies on each client finding it some other way. The same RFC asks implementations to tolerate extraneous certificates and arbitrary ordering, which is why sending an extra intermediate is usually harmless while sending too few is not.
Building a synthetic chain
The companion example creates three throwaway EC keys and certificates with keytool: a root named Book Store Demo Root, an intermediate signed by the root with basicConstraints ca:true and pathlen 0, and a leaf for localhost signed by the intermediate with a subjectAlternativeName and the serverAuth extended key usage. The client's trust store contains only the root, exactly as a real client trusts only roots from its distribution.
# Author: Sandeep Belgavi. Excerpt from run.sh; all keys are synthetic and discarded.
kt -gencert -alias root -keystore root.p12 -infile inter.csr -outfile inter.pem -rfc \
-ext bc:c=ca:true,pathlen:0 -ext ku:c=keyCertSign,cRLSign -validity 30
kt -gencert -alias inter -keystore inter.p12 -infile leaf.csr -outfile leaf.pem -rfc \
-ext san=dns:localhost -ext eku=serverAuth -validity 7
# keytool writes the issued certificate followed by the issuer chain; keep the first
# PEM block alone to build the misconfigured server.
awk '/BEGIN CERTIFICATE/{n++} n==1' leaf.pem > served-leaf-only.pem
cp leaf.pem served-full-chain.pemOne detail surfaced while building the example. keytool's -gencert output contains the newly issued certificate followed by the issuer's own chain, so leaf.pem already holds two certificates. A first attempt that imported it into a key store produced a "leaf-only" server that was actually sending the full chain, and the client succeeded. The probe now prints the length of the chain it serves, and the script asserts the expected certificate counts, so that mistake cannot recur silently. Checking what you think you are serving is the first lesson of the exercise.
The probe
ChainProbe.java builds an in-memory PKCS12 key store from the leaf's private key and exactly the certificates in a given PEM file, in file order, starts com.sun.net.httpserver.HttpsServer on 127.0.0.1, and makes one request with java.net.http.HttpClient using an SSLContext that trusts only the root.
// Author: Sandeep Belgavi. Excerpt: serve exactly the certificates in the PEM file.
Certificate[] served;
try (InputStream in = new FileInputStream(args[1])) {
served = CertificateFactory.getInstance("X.509")
.generateCertificates(in).toArray(new Certificate[0]);
}
KeyStore serverStore = KeyStore.getInstance("PKCS12");
serverStore.load(null, null);
serverStore.setKeyEntry("leaf", key, password, served);The probe runs as a single-file source program, so no build tool is needed.
Six measured cases
All six ran with JDK 23.0.2 on the author's workstation; the run script reproduces them.
Leaf only: served chain length 1, result PKIX path building failed. The client has the root but cannot connect the leaf to it.
Leaf plus intermediate: served chain length 2, HTTP 200. This is the correct configuration and the fix.
Leaf only, with the system property com.sun.security.enableAIAcaIssuers=true: still fails. The leaf in this case has no AIA extension, so there is nothing to fetch.
Leaf with an AIA caIssuers URL, property off: fails. The JDK ignores the extension by default.
Leaf with an AIA URL, property on, and a local HTTP server serving the intermediate at that URL: HTTP 200. The validator downloaded the intermediate and built the path.
Leaf with an AIA URL, property on, URL unreachable: fails.
The Java PKI Programmer's Guide documents this behaviour: support for the caIssuers access method is disabled by default for compatibility, and when enabled the CertPathBuilder uses the AIA extension to find the issuing certificate, provided it is an ldap, http or ftp URI. The guide adds that networking proxy configuration may be needed.
Why the AIA property is not the fix
The fifth case shows that AIA fetching can rescue the handshake, which makes it tempting to set the property and move on. It is the wrong place to fix a server misconfiguration, for four reasons visible in the measurements.
It moves a dependency into the handshake. The client now needs outbound HTTP access to the certificate authority's download host at connection time. Services in restricted networks, and most production egress policies, will not have it, which is the sixth case.
It applies to the whole JVM. The property changes path building for every TLS connection the process makes, not only the misconfigured endpoint.
It adds latency and a failure mode to every cold handshake. Whether results are cached, and for how long, is an implementation detail you would then depend on.
It hides the real problem from every other client. Whether a client can fill the gap depends on its TLS library and platform, so a server that sends an incomplete chain works for some clients and fails for others, which is harder to diagnose than a server that is simply correct.
The fix is on the server: configure it to send the leaf followed by the intermediates. Most web servers and load balancers accept a full-chain file for exactly this purpose, and ACME clients typically produce one.
Diagnosing a live endpoint
The same question, how many certificates does the server send, can be answered from any machine with OpenSSL.
openssl s_client -connect api.example.com:443 -servername api.example.com -showcerts </dev/null \
| grep -c 'BEGIN CERTIFICATE'A count of one for a certificate issued by an intermediate authority is the misconfiguration. From Java, running the client with -Djavax.net.debug=ssl:handshake prints the received chain. Check the chain on each listener separately: a load balancer, a sidecar and the application may each terminate TLS with their own configuration, and only one of them may be wrong.
Prevention
Monitor chain length, not only expiry. A synthetic check that connects with a trust store containing only roots, and without AIA fetching, catches a missing intermediate on the first deployment rather than when the first strict client calls.
Automate renewal with full-chain output, and fail the deployment if the served chain does not validate against a roots-only store. The companion probe can serve as that test in a pipeline: point it at the candidate PEM bundle and require a 200.
Avoid trusting intermediates on the client as a workaround. Adding the intermediate to a Java trust store makes the error disappear, but it pins the client to one intermediate. When the authority rotates intermediates, as public authorities do, the client breaks again in a new way.
Limits of this example
The probe uses EC P-256 keys, short validity periods and a local HTTP stand-in for the AIA URL. It does not test revocation checking, OCSP stapling, cross-signed chains or name constraints, and it measures one JDK version. Browser behaviour is described from general documentation rather than measured here; the point of the guide is the JDK side, which is measured.