diff --git a/flight/flight-sql-jdbc-core/src/main/java/org/apache/arrow/driver/jdbc/client/utils/ClientAuthenticationUtils.java b/flight/flight-sql-jdbc-core/src/main/java/org/apache/arrow/driver/jdbc/client/utils/ClientAuthenticationUtils.java index 8b3c3a9ed3..9129d2d30b 100644 --- a/flight/flight-sql-jdbc-core/src/main/java/org/apache/arrow/driver/jdbc/client/utils/ClientAuthenticationUtils.java +++ b/flight/flight-sql-jdbc-core/src/main/java/org/apache/arrow/driver/jdbc/client/utils/ClientAuthenticationUtils.java @@ -28,7 +28,6 @@ import java.security.KeyStore; import java.security.KeyStoreException; import java.security.NoSuchAlgorithmException; -import java.security.cert.Certificate; import java.security.cert.CertificateException; import java.util.ArrayList; import java.util.Arrays; @@ -213,7 +212,7 @@ static InputStream getCertificatesInputStream(Collection keyStores) } /** - * Generates an {@link InputStream} that contains certificates for a private key. + * Generates an {@link InputStream} that contains every trusted certificate entry of a KeyStore. * * @param keyStorePath The path of the KeyStore. * @param keyStorePass The password of the KeyStore. @@ -232,7 +231,10 @@ public static InputStream getCertificateStream( keyStore.load(keyStoreStream, keyStorePass.toCharArray()); } - return getSingleCertificateInputStream(keyStore); + if (!hasCertificateEntry(keyStore)) { + throw new CertificateException("Keystore did not have a certificate."); + } + return getCertificatesInputStream(List.of(keyStore)); } /** @@ -283,28 +285,13 @@ public static InputStream getClientKeyStream(final String clientKeyPath) return Files.newInputStream(Paths.get(clientKeyPath)); } - private static InputStream getSingleCertificateInputStream(KeyStore keyStore) - throws KeyStoreException, IOException, CertificateException { + private static boolean hasCertificateEntry(KeyStore keyStore) throws KeyStoreException { final Enumeration aliases = keyStore.aliases(); - while (aliases.hasMoreElements()) { - final String alias = aliases.nextElement(); - if (keyStore.isCertificateEntry(alias)) { - return toInputStream(keyStore.getCertificate(alias)); + if (keyStore.isCertificateEntry(aliases.nextElement())) { + return true; } } - - throw new CertificateException("Keystore did not have a certificate."); - } - - private static InputStream toInputStream(final Certificate certificate) throws IOException { - - try (final StringWriter writer = new StringWriter(); - final JcaPEMWriter pemWriter = new JcaPEMWriter(writer)) { - - pemWriter.writeObject(certificate); - pemWriter.flush(); - return new ByteArrayInputStream(writer.toString().getBytes(StandardCharsets.UTF_8)); - } + return false; } } diff --git a/flight/flight-sql-jdbc-core/src/test/java/org/apache/arrow/driver/jdbc/ConnectionTlsTest.java b/flight/flight-sql-jdbc-core/src/test/java/org/apache/arrow/driver/jdbc/ConnectionTlsTest.java index b6fd045d02..986dc10fa4 100644 --- a/flight/flight-sql-jdbc-core/src/test/java/org/apache/arrow/driver/jdbc/ConnectionTlsTest.java +++ b/flight/flight-sql-jdbc-core/src/test/java/org/apache/arrow/driver/jdbc/ConnectionTlsTest.java @@ -24,6 +24,7 @@ import java.io.IOException; import java.net.URLEncoder; +import java.nio.file.Path; import java.nio.file.Paths; import java.security.UnrecoverableKeyException; import java.sql.Connection; @@ -45,6 +46,7 @@ import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.RegisterExtension; +import org.junit.jupiter.api.io.TempDir; /** Tests encrypted connections. */ public class ConnectionTlsTest { @@ -139,6 +141,38 @@ public void testGetEncryptedClientAuthenticated() throws Exception { } } + /** + * Try to instantiate an encrypted FlightClient with a trust store whose first entry is not the CA + * that signed the server certificate. + * + * @throws Exception on error. + */ + @Test + public void testGetEncryptedClientAuthenticatedWithMultipleCertificatesInTrustStore( + @TempDir Path tempDir) throws Exception { + final Path multipleCertificatesTrustStorePath = tempDir.resolve("trustStore.p12"); + FlightSqlTestCertificates.writeTrustStore( + multipleCertificatesTrustStorePath, + trustStorePass, + FlightSqlTestCertificates.exampleTlsCerts().get(1).cert, + FlightSqlTestCertificates.exampleCACert()); + + try (ArrowFlightSqlClientHandler client = + new ArrowFlightSqlClientHandler.Builder() + .withHost(FLIGHT_SERVER_TEST_EXTENSION.getHost()) + .withPort(FLIGHT_SERVER_TEST_EXTENSION.getPort()) + .withSystemTrustStore(false) + .withUsername(userTest) + .withPassword(passTest) + .withTrustStorePath(multipleCertificatesTrustStorePath.toString()) + .withTrustStorePassword(trustStorePass) + .withBufferAllocator(allocator) + .withEncryption(true) + .build()) { + assertNotNull(client); + } + } + /** * Try to instantiate an encrypted FlightClient providing a keystore without certificate. It's * expected to receive the SQLException. diff --git a/flight/flight-sql-jdbc-core/src/test/java/org/apache/arrow/driver/jdbc/client/utils/ClientAuthenticationUtilsTest.java b/flight/flight-sql-jdbc-core/src/test/java/org/apache/arrow/driver/jdbc/client/utils/ClientAuthenticationUtilsTest.java index 0d1498abe4..f927311109 100644 --- a/flight/flight-sql-jdbc-core/src/test/java/org/apache/arrow/driver/jdbc/client/utils/ClientAuthenticationUtilsTest.java +++ b/flight/flight-sql-jdbc-core/src/test/java/org/apache/arrow/driver/jdbc/client/utils/ClientAuthenticationUtilsTest.java @@ -17,22 +17,27 @@ package org.apache.arrow.driver.jdbc.client.utils; import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertThrows; import static org.mockito.Mockito.mock; import java.io.IOException; import java.io.InputStream; import java.lang.reflect.Method; +import java.nio.file.Path; import java.security.KeyStore; import java.security.KeyStoreException; import java.security.NoSuchAlgorithmException; import java.security.cert.Certificate; import java.security.cert.CertificateException; +import java.security.cert.CertificateFactory; import java.util.Arrays; import java.util.Collections; import java.util.Enumeration; +import org.apache.arrow.driver.jdbc.utils.FlightSqlTestCertificates; import org.bouncycastle.openssl.jcajce.JcaPEMWriter; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith; +import org.junit.jupiter.api.io.TempDir; import org.mockito.Mock; import org.mockito.MockedStatic; import org.mockito.Mockito; @@ -42,6 +47,38 @@ public class ClientAuthenticationUtilsTest { @Mock KeyStore keyStoreMock; + @Test + public void testGetCertificateStreamWithMultipleCertificates(@TempDir Path tempDir) + throws Exception { + final Path trustStorePath = tempDir.resolve("trustStore.p12"); + FlightSqlTestCertificates.writeTrustStore( + trustStorePath, + "flight", + FlightSqlTestCertificates.exampleCACert(), + FlightSqlTestCertificates.exampleTlsCerts().get(0).cert, + FlightSqlTestCertificates.exampleTlsCerts().get(1).cert); + + try (InputStream certificates = + ClientAuthenticationUtils.getCertificateStream(trustStorePath.toString(), "flight")) { + assertEquals( + 3, CertificateFactory.getInstance("X.509").generateCertificates(certificates).size()); + } + } + + @Test + public void testGetCertificateStreamWithNoCertificates(@TempDir Path tempDir) throws Exception { + final Path trustStorePath = tempDir.resolve("empty.p12"); + FlightSqlTestCertificates.writeTrustStore(trustStorePath, "flight"); + + CertificateException e = + assertThrows( + CertificateException.class, + () -> + ClientAuthenticationUtils.getCertificateStream( + trustStorePath.toString(), "flight")); + assertEquals("Keystore did not have a certificate.", e.getMessage()); + } + @Test public void testGetCertificatesInputStream() throws IOException, KeyStoreException { JcaPEMWriter pemWriterMock = mock(JcaPEMWriter.class); diff --git a/flight/flight-sql-jdbc-core/src/test/java/org/apache/arrow/driver/jdbc/utils/FlightSqlTestCertificates.java b/flight/flight-sql-jdbc-core/src/test/java/org/apache/arrow/driver/jdbc/utils/FlightSqlTestCertificates.java index b72dcef743..e97aa00967 100644 --- a/flight/flight-sql-jdbc-core/src/test/java/org/apache/arrow/driver/jdbc/utils/FlightSqlTestCertificates.java +++ b/flight/flight-sql-jdbc-core/src/test/java/org/apache/arrow/driver/jdbc/utils/FlightSqlTestCertificates.java @@ -17,8 +17,15 @@ package org.apache.arrow.driver.jdbc.utils; import java.io.File; +import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStream; +import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; +import java.security.GeneralSecurityException; +import java.security.KeyStore; +import java.security.cert.CertificateFactory; import java.util.Arrays; import java.util.List; import java.util.Objects; @@ -73,6 +80,29 @@ public static List exampleTlsCerts() { new CertKeyPair(root.resolve("cert1.pem").toFile(), root.resolve("cert1.pkcs1").toFile())); } + /** + * Write a PKCS12 trust store with a certificate entry for each certificate, in order. PKCS12 + * keeps insertion order, so the first certificate is the store's first entry. + * + * @param path The path to write the trust store to. + * @param password The password of the trust store. + * @param certificates The PEM files of the certificates. + */ + public static void writeTrustStore(Path path, String password, File... certificates) + throws IOException, GeneralSecurityException { + final CertificateFactory factory = CertificateFactory.getInstance("X.509"); + final KeyStore trustStore = KeyStore.getInstance("PKCS12"); + trustStore.load(null, null); + for (int i = 0; i < certificates.length; i++) { + try (InputStream in = Files.newInputStream(certificates[i].toPath())) { + trustStore.setCertificateEntry("cert" + i, factory.generateCertificate(in)); + } + } + try (OutputStream out = Files.newOutputStream(path)) { + trustStore.store(out, password.toCharArray()); + } + } + public static class CertKeyPair { public final File cert;