8000 Detect the format of the JRE truststore by trixpan · Pull Request #54171 · keycloak/keycloak · GitHub
[go: up one dir, main page]

Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.stream.Stream;

Expand All @@ -50,6 +51,8 @@ public class TruststoreBuilder {
private static final String CERT_PROTECTION_ALGORITHM_KEY = "keystore.pkcs12.certProtectionAlgorithm";
public static final String DUMMY_PASSWORD = "keycloakchangeit"; // fips length compliant dummy password
static final String PKCS12 = "PKCS12";
private static final String JKS = "JKS";
private static final String DEFAULT_JRE_TRUSTSTORE_PASSWORD = "changeit";

private static final String KUBERNETES_CA_PATH = "/var/run/secrets/kubernetes.io/serviceaccount/ca.crt";
private static final String SERVICE_CA_PATH = "/var/run/secrets/kubernetes.io/serviceaccount/service-ca.crt";
Expand Down Expand Up @@ -216,10 +219,12 @@ static void includeDefaultTruststore(KeyStore truststore) {
String type = PKCS12;
String password = null;
File defaultTrustStore = null;
boolean jreTruststore = false;
if (trustStorePath == null) {
trustStorePath = System.getProperty(TruststoreBuilder.SYSTEM_TRUSTSTORE_KEY);
if (trustStorePath == null) {
defaultTrustStore = getJRETruststore();
jreTruststore = true;
} else {
type = System.getProperty(TruststoreBuilder.SYSTEM_TRUSTSTORE_TYPE_KEY, KeyStore.getDefaultType());
password = System.getProperty(TruststoreBuilder.SYSTEM_TRUSTSTORE_PASSWORD_KEY);
Expand All @@ -241,7 +246,8 @@ static void includeDefaultTruststore(KeyStore truststore) {

if (defaultTrustStore.exists()) {
String path = defaultTrustStore.getAbsolutePath();
mergeTrustStore(truststore, path, loadStore(path, type, password));
KeyStore store = jreTruststore ? loadJRETruststore(path) : loadStore(path, type, password);
mergeTrustStore(truststore, path, store);
} else {
LOGGER.warnf("Default truststore was to be included, but could not be found at: %s", defaultTrustStore);
}
Expand All @@ -258,6 +264,28 @@ static File getJRETruststore() {
return new File(securityDirectory, "cacerts");
}

/**
* The format of the JRE truststore is not fixed (for example it is JKS on some distributions), so
* try the candidate types, with no password and then the well-known default one.
*/
private static KeyStore loadJRETruststore(String path) {
Collection<String> types = new LinkedHashSet<>(List.of(KeyStore.getDefaultType(), JKS, PKCS12));
Exception failure = null;
for (String type : types) {
for (String password : new String[] { null, DEFAULT_JRE_TRUSTSTORE_PASSWORD }) {
try {
return KeystoreUtil.loadKeyStore(path, password, type);
} catch (Exception e) {
if (failure == null) {
failure = e;
}
}
}
}
throw new RuntimeException("Failed to initialize truststore: " + new File(path).getAbsolutePath()
+ ", tried types " + types, failure);
}

static KeyStore loadStore(String path, String type, String password) {
try {
return KeystoreUtil.loadKeyStore(path, password, type);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,15 @@
package org.keycloak.truststore;

import java.io.File;
import java.io.OutputStream;
import java.net.URL;
import java.nio.file.Files;
import java.nio.file.StandardOpenOption;
import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.KeyStore;
import java.security.Security;
import java.security.cert.X509Certificate;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
Expand All @@ -30,6 +35,7 @@

import org.keycloak.common.crypto.CryptoIntegration;
import org.keycloak.common.crypto.CryptoProvider;
import org.keycloak.common.util.CertificateUtils;
import org.keycloak.common.util.KeystoreUtil.TruststoreFormat;
import org.keycloak.crypto.def.DefaultCryptoProvider;

Expand Down Expand Up @@ -106,6 +112,42 @@ public void testMergedTrustStore() throws Exception {
assertEquals(certs, Collections.list(savedLoaded.aliases()).size());
}

@Test
public void testJksDefaultTruststoreIsMerged() throws Exception {
File javaHome = temporaryFolder.newFolder();
File security = new File(javaHome, "lib" + File.separator + "security");
assertTrue(security.mkdirs());

// a JKS truststore named like the JRE one, so the format cannot be detected from the extension
KeyPairGenerator generator = KeyPairGenerator.getInstance("RSA");
generator.initialize(2048);
KeyPair keyPair = generator.generateKeyPair();
X509Certificate certificate = CertificateUtils.generateV1SelfSignedCertificate(keyPair, "jks-default-ca");
KeyStore jks = KeyStore.getInstance("JKS");
jks.load(null, null);
jks.setCertificateEntry("jks-default-ca", certificate);
try (OutputStream os = Files.newOutputStream(new File(security, "cacerts").toPath())) {
jks.store(os, "changeit".toCharArray());
}

String originalJavaHome = System.getProperty("java.home");
String originalCompat = Security.getProperty("keystore.type.compat");
// mimic providers such as BC-FIPS that reject JKS content when loaded as PKCS12
Security.setProperty("keystore.type.compat", "false");
System.clearProperty(TruststoreBuilder.SYSTEM_TRUSTSTORE_KEY);
System.setProperty("java.home", javaHome.getAbsolutePath());
try {
KeyStore merged = TruststoreBuilder.createMergedTruststore(new String[0], true);
List<String> aliases = Collections.list(merged.aliases());
assertEquals(1, aliases.size());
assertEquals(certificate, merged.getCertificate(aliases.get(0)));
} finally {
// an empty value would disable compatibility mode, so fall back to the JDK default
Security.setProperty("keystore.type.compat", originalCompat == null ? "true" : originalCompat);
System.setProperty("java.home", originalJavaHome);
}
}

@Test
public void testSetSystemTruststoreUsesPkcs12ByDefault() throws Exception {
URL url = TruststoreBuilderTest.class.getResource("/truststores/keycloak.pem");
Expand Down
Loading
0