Repository navigation
Description
Describe the bug
clickhouse-java expects mTLS private keys to be provided as a PEM file with PKCS8 data.
See
Line 89 in 2ca52fc
| PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(encoded); |
But clickhouse-java also expects the key algorithm (RSA, DSA, EC, etc.) to be in the PEM header (i.e. -----BEGIN $ALGO PRIVATE KEY-----).
See
Line 76 in 2ca52fc
| algorithm = getAlgorithm(line, algorithm); |
This is an unreasonable expectation. PKCS8 encoding should never include the algorithm in the header. The only two allowable headers are BEGIN PRIVATE KEY and BEGIN ENCRYPTED PRIVATE KEY.
See https://datatracker.ietf.org/doc/html/rfc7468#section-10
No compliant tool will produce a PEM/PKCS8 file with the format expected by this library.
This is a problem when the PKCS8 data represents anything but an RSA key (which the library assumes as default). For example, the library is unable to read properly represented EC private keys.
With PEM/PKCS8, the algorithm is encoded in the PKCS8 data itself and can be read from the encoded data. Java/JCA does not give a good way of doing this (though there is a proposal - https://openjdk.org/jeps/8300911) but BouncyCastle does.
PEMParser pemParser = new PEMParser(Files.newBufferedReader(Paths.get("private.key")));
var privateKeyInfo = (PrivateKeyInfo) pemParser.readObject();
PrivateKey privateKey = new JcaPEMKeyConverter().getPrivateKey(privateKeyInfo);
String algorithm = privateKey.getAlgorithm();
Steps to reproduce
- Create a PEM/PKCS8 file for an EC private key.
- Specify it via
ClickHouseClientBuilder.option(ClickHouseClientOption.SSL_KEY, "path/to/private.key") - Execute a ClickHouseRequest from the resulting client.
Expected behaviour
An SSL context is established and the request succeeds.
Error log
java.util.concurrent.CompletionException: javax.net.ssl.SSLException: Failed to get SSL context
at com.clickhouse.client.ClickHouseClientBuilder$Agent.handle(ClickHouseClientBuilder.java:272)
at com.clickhouse.client.ClickHouseClientBuilder$Agent.lambda$execute$0(ClickHouseClientBuilder.java:348)
at java.base/java.util.concurrent.CompletableFuture.uniHandle(CompletableFuture.java:934)
at java.base/java.util.concurrent.CompletableFuture$UniHandle.tryFire(CompletableFuture.java:911)
at java.base/java.util.concurrent.CompletableFuture.postComplete(CompletableFuture.java:510)
at java.base/java.util.concurrent.CompletableFuture$AsyncSupply.run(CompletableFuture.java:1773)
at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1144)
at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:642)
at java.base/java.lang.Thread.run(Thread.java:1583)
Caused by: javax.net.ssl.SSLException: Failed to get SSL context
at com.clickhouse.client.config.ClickHouseDefaultSslContextProvider.getJavaSslContext(ClickHouseDefaultSslContextProvider.java:176)
at com.clickhouse.client.config.ClickHouseDefaultSslContextProvider.getSslContext(ClickHouseDefaultSslContextProvider.java:186)
at com.clickhouse.client.http.HttpUrlConnectionImpl.newConnection(HttpUrlConnectionImpl.java:118)
at com.clickhouse.client.http.HttpUrlConnectionImpl.<init>(HttpUrlConnectionImpl.java:200)
at com.clickhouse.client.http.ClickHouseHttpConnectionFactory.createConnection(ClickHouseHttpConnectionFactory.java:30)
at com.clickhouse.client.http.ClickHouseHttpClient.newConnection(ClickHouseHttpClient.java:56)
at com.clickhouse.client.http.ClickHouseHttpClient.newConnection(ClickHouseHttpClient.java:26)
at com.clickhouse.client.AbstractClient.getConnection(AbstractClient.java:198)
at com.clickhouse.client.http.ClickHouseHttpClient.send(ClickHouseHttpClient.java:90)
at com.clickhouse.client.AbstractClient.sendAsync(AbstractClient.java:161)
at com.clickhouse.client.AbstractClient.lambda$execute$0(AbstractClient.java:273)
at java.base/java.util.concurrent.CompletableFuture$AsyncSupply.run(CompletableFuture.java:1768)
... 3 more
Caused by: java.security.spec.InvalidKeySpecException: java.security.InvalidKeyException: Invalid RSA private key
at java.base/sun.security.rsa.RSAKeyFactory.engineGeneratePrivate(RSAKeyFactory.java:253)
at java.base/java.security.KeyFactory.generatePrivate(KeyFactory.java:389)
at com.clickhouse.client.config.ClickHouseDefaultSslContextProvider.getPrivateKey(ClickHouseDefaultSslContextProvider.java:90)
at com.clickhouse.client.config.ClickHouseDefaultSslContextProvider.getKeyStore(ClickHouseDefaultSslContextProvider.java:114)
at com.clickhouse.client.config.ClickHouseDefaultSslContextProvider.getJavaSslContext(ClickHouseDefaultSslContextProvider.java:156)
... 14 more
Caused by: java.security.InvalidKeyException: Invalid RSA private key
at java.base/sun.security.rsa.RSAPrivateCrtKeyImpl.parseKeyBits(RSAPrivateCrtKeyImpl.java:356)
at java.base/sun.security.rsa.RSAPrivateCrtKeyImpl.<init>(RSAPrivateCrtKeyImpl.java:161)
at java.base/sun.security.rsa.RSAPrivateCrtKeyImpl.newKey(RSAPrivateCrtKeyImpl.java:90)
at java.base/sun.security.rsa.RSAKeyFactory.generatePrivate(RSAKeyFactory.java:348)
at java.base/sun.security.rsa.RSAKeyFactory.engineGeneratePrivate(RSAKeyFactory.java:249)
... 18 more
Caused by: java.io.IOException: Version must be 0
at java.base/sun.security.rsa.RSAPrivateCrtKeyImpl.parseASN1(RSAPrivateCrtKeyImpl.java:321)
at java.base/sun.security.rsa.RSAPrivateCrtKeyImpl.parseKeyBits(RSAPrivateCrtKeyImpl.java:346)
... 22 more
Configuration
Environment
- Client version: 0.6.0-patch1
- Language version: 21
- OS: Linux
ClickHouse server
- ClickHouse Server version: N/A
- ClickHouse Server non-default settings, if any: N/A
Activity
github-actions commentedon Jan 10, 2026
This issue has been automatically marked as stale because it has not had activity in the last year. It will be closed in 30 days if no further activity occurs. Please feel free to leave a comment if you believe the issue is still relevant. Thank you for your contributions!
github-actions commentedon Feb 9, 2026
This issue has been automatically closed because it has not had any further activity in the last 30 days. Thank you for your contributions!
polyglotAI-bot commentedon Nov 28, 2026
Recommendation: Keep open — fix needed
The defect is also in client-v2 and jdbc-v2, not only in the v1 provider: a PKCS#8 key of any algorithm other than RSA cannot be used for a client certificate. A fix is needed in both PEM key readers.
main: Reproduced on main — an EC key with the RFC 7468 labelBEGIN PRIVATE KEYfails in client-v2, jdbc-v2, and v1.EC PRIVATE KEY(-----BEGIN EC PRIVATE KEY-----) and keep the PKCS#8 body: the run shows that client-v2 then loads the key and builds the SSL context.Analysis:
getAlgorithm(client-v2/.../api/internal/SslContextProvider.java:97) takes the key algorithm from the text betweenBEGINandPRIVATE KEYin the first line, withRSAas the default (:49), andgetPrivateKeygives that name toKeyFactory(:124). The PKCS#8 label has no algorithm, so each non-RSA PKCS#8 key is decoded as RSA and fails. The v1ClickHouseDefaultSslContextProviderhas the same code (:84,:111), and jdbc-v2 uses the client-v2 code.Reproduction: complete program and output
Changed from the issue's steps: the program makes its own EC key with the JDK
keytool, and adds client-v2 and jdbc-v2 cases; the v1 case callsgetPrivateKeyfrom the issue's stack trace.Output on
main(8480ca001), ClickHouse 26.11.4.11 (the client fails before it connects):Automated triage by Polyglot AI. A maintainer makes the final decision.