Repository navigation
Description
Describe the bug, including details regarding any error messages, version, and platform.
With trustStore set, the Flight SQL JDBC driver trusts one CA: the first certificate entry KeyStore.aliases()
returns, in no defined order. A server whose chain ends at any other CA in the store fails with "unable to find valid
certification path to requested target". Reproduced with flight-sql-jdbc-driver 20.0.0 on JDK 24. The code is the same
on main and has been since the driver was added (ARROW-7744).
// java -cp flight-sql-jdbc-driver-20.0.0.jar Repro.java
import java.io.InputStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.security.KeyStore;
import java.security.cert.CertificateFactory;
import org.apache.arrow.driver.jdbc.client.utils.ClientAuthenticationUtils;
public class Repro {
public static void main(String[] args) throws Exception {
Path cacerts = Path.of(System.getProperty("java.home"), "lib", "security", "cacerts");
KeyStore store = KeyStore.getInstance(KeyStore.getDefaultType());
try (InputStream in = Files.newInputStream(cacerts)) {
store.load(in, "changeit".toCharArray());
}
try (InputStream in = ClientAuthenticationUtils.getCertificateStream(cacerts.toString(), "changeit")) {
System.out.println("entries: " + store.size()
+ ", trusted: " + CertificateFactory.getInstance("X.509").generateCertificates(in).size());
}
}
}Output: entries: 162, trusted: 1.
- Cause:
ClientAuthenticationUtils.getCertificateStreamreturnsgetSingleCertificateInputStream(keyStore), which
stops at the firstisCertificateEntryalias.useSystemTrustStoregoes throughgetCertificatesInputStream, which
writes every entry. - Not documented: the docs call
trustStore"the path to the certificate store", and the Javadoc says
"certificates for a private key". - Impact: in 20.0.0 the driver reads
trustStoreonly whenuseSystemTrustStore=falseis also set, because the
defaultuseSystemTrustStore=truetakes precedence. Since GH-1499: [JDBC] Prefer an explicit trustStore over the system trust store #1496,trustStorealone takes precedence, so the next
release exposes everyone who setstrustStorewithout disabling the system store. - Not a security issue: the driver trusts fewer CAs than configured, so it fails closed.
Fix, in ClientAuthenticationUtils. It applies to both 20.0.0 and main, and was checked against the 20.0.0 JAR:
162 of 162 trusted, and the empty-store error is unchanged.
- return getSingleCertificateInputStream(keyStore);
+ if (!hasCertificateEntry(keyStore)) {
+ throw new CertificateException("Keystore did not have a certificate.");
+ }
+ return getCertificatesInputStream(List.of(keyStore)); private static boolean hasCertificateEntry(KeyStore keyStore) throws KeyStoreException {
final Enumeration<String> aliases = keyStore.aliases();
while (aliases.hasMoreElements()) {
if (keyStore.isCertificateEntry(aliases.nextElement())) {
return true;
}
}
return false;
}The fix also removes getSingleCertificateInputStream, toInputStream and the java.security.cert.Certificate
import, and corrects the Javadoc.
Tests: the only trustStore fixture, keys/keyStore.jks, holds one entry, so no existing test catches this.
ClientAuthenticationUtilsTest: a store with N ≥ 2 entries yields N certificates.ConnectionTlsTest: TLS succeeds with a PKCS12trustStorethat has an unrelated CA first and the server's CA
second. PKCS12 keeps insertion order, so the test fails deterministically on the current code.
Workaround: export the store to PEM and pass it as tlsRootCerts.
Happy to open a PR.
Activity