Skip to content

[JDBC] trustStore trusts only the first certificate entry in the store #1550

Backlog

Description

@fredjoonpark

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.getCertificateStream returns getSingleCertificateInputStream(keyStore), which
    stops at the first isCertificateEntry alias. useSystemTrustStore goes through getCertificatesInputStream, 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 trustStore only when useSystemTrustStore=false is also set, because the
    default useSystemTrustStore=true takes precedence. Since GH-1499: [JDBC] Prefer an explicit trustStore over the system trust store #1496, trustStore alone takes precedence, so the next
    release exposes everyone who sets trustStore without 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 PKCS12 trustStore that 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

  1. added
    Type: bug
    on Dec 3, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type: bug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions