Skip to content

Commit 3bc7e1c

Browse files
committed
Correctly handle PKCS#11 tokens for system keystore (#33460)
* Correctly handle NONE keyword for system keystore As defined in the PKCS#11 reference guide https://docs.oracle.com/javase/8/docs/technotes/guides/security/p11guide.html PKCS#11 tokens can be used as the JSSE keystore and truststore and the way to indicate this is to set `javax.net.ssl.keyStore` and `javax.net.ssl.trustStore` to `NONE` (case sensitive). This commits ensures that we honor this convention and do not attempt to load the keystore or truststore if the system property is set to NONE. * Handle password protected system truststore When a PKCS#11 token is used as the system truststore, we need to pass a password when loading it, even if only for reading certificate entries. This commit ensures that if `javax.net.ssl.trustStoreType` is set to `PKCS#11` (as it would when a PKCS#11 token is in use) the password specified in `javax.net.ssl.trustStorePassword` is passed when attempting to load the truststore. Relates #33459
1 parent b6f1c5a commit 3bc7e1c

File tree

2 files changed

+24
-3
lines changed

2 files changed

+24
-3
lines changed

x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ssl/DefaultJDKTrustConfig.java

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,10 @@
1616
import java.io.IOException;
1717
import java.nio.file.Path;
1818
import java.security.GeneralSecurityException;
19+
import java.security.KeyStore;
20+
import java.security.KeyStoreException;
21+
import java.security.NoSuchAlgorithmException;
22+
import java.security.cert.CertificateException;
1923
import java.util.Arrays;
2024
import java.util.Collection;
2125
import java.util.Collections;
@@ -34,7 +38,7 @@ private DefaultJDKTrustConfig() {
3438
@Override
3539
X509ExtendedTrustManager createTrustManager(@Nullable Environment environment) {
3640
try {
37-
return CertParsingUtils.trustManager(null, TrustManagerFactory.getDefaultAlgorithm());
41+
return CertParsingUtils.trustManager(getSystemTrustStore(), TrustManagerFactory.getDefaultAlgorithm());
3842
} catch (Exception e) {
3943
throw new ElasticsearchException("failed to initialize a TrustManagerFactory", e);
4044
}
@@ -81,4 +85,20 @@ static TrustConfig merge(TrustConfig trustConfig) {
8185
return new CombiningTrustConfig(Arrays.asList(INSTANCE, trustConfig));
8286
}
8387
}
88+
89+
/**
90+
* When a PKCS#11 token is used as the system default keystore/truststore, we need to pass the keystore
91+
* password when loading, even for reading certificates only ( as opposed to i.e. JKS keystores where
92+
* we only need to pass the password for reading Private Key entries ).
93+
*
94+
* @return the KeyStore used as truststore for PKCS#11 initialized with the password, null otherwise
95+
*/
96+
private KeyStore getSystemTrustStore() throws KeyStoreException, CertificateException, NoSuchAlgorithmException, IOException {
97+
if (System.getProperty("javax.net.ssl.trustStoreType", "").equalsIgnoreCase("PKCS11")) {
98+
KeyStore keyStore = KeyStore.getInstance("PKCS11");
99+
keyStore.load(null, System.getProperty("javax.net.ssl.trustStorePassword", "").toCharArray());
100+
return keyStore;
101+
}
102+
return null;
103+
}
84104
}

x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ssl/SSLConfiguration.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,7 @@ private static KeyConfig createKeyConfig(Settings settings, SSLConfiguration glo
192192
if (global != null) {
193193
return global.keyConfig();
194194
}
195-
if (System.getProperty("javax.net.ssl.keyStore") != null) {
195+
if (System.getProperty("javax.net.ssl.keyStore") != null && System.getProperty("javax.net.ssl.keyStore").equals("NONE") == false) {
196196
// TODO: we should not support loading a keystore from sysprops...
197197
try (SecureString keystorePassword = new SecureString(System.getProperty("javax.net.ssl.keyStorePassword", ""))) {
198198
return new StoreKeyConfig(System.getProperty("javax.net.ssl.keyStore"), KeyStore.getDefaultType(), keystorePassword,
@@ -233,7 +233,8 @@ private static TrustConfig createCertChainTrustConfig(Settings settings, KeyConf
233233
String trustStoreAlgorithm = SETTINGS_PARSER.truststoreAlgorithm.get(settings);
234234
String trustStoreType = getKeyStoreType(SETTINGS_PARSER.truststoreType, settings, trustStorePath);
235235
return new StoreTrustConfig(trustStorePath, trustStoreType, trustStorePassword, trustStoreAlgorithm);
236-
} else if (global == null && System.getProperty("javax.net.ssl.trustStore") != null) {
236+
} else if (global == null && System.getProperty("javax.net.ssl.trustStore") != null
237+
&& System.getProperty("javax.net.ssl.trustStore").equals("NONE") == false) {
237238
try (SecureString truststorePassword = new SecureString(System.getProperty("javax.net.ssl.trustStorePassword", ""))) {
238239
return new StoreTrustConfig(System.getProperty("javax.net.ssl.trustStore"), KeyStore.getDefaultType(), truststorePassword,
239240
System.getProperty("ssl.TrustManagerFactory.algorithm", TrustManagerFactory.getDefaultAlgorithm()));

0 commit comments

Comments
 (0)