8000 fix: allowing cacerts and passwordless pkcs12 in non-strict by shawkins · Pull Request #54394 · keycloak/keycloak · GitHub
[go: up one dir, main page]

Skip to content
Merged
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 @@ -118,6 +118,20 @@ void testUnencryptedPkcs12TrustStoreInStrictMode(KeycloakRunner runner) {
cliResult.assertStarted();
});
}

@Test
void testUnencryptedPkcs12TrustStoreInNonStrictMode(KeycloakRunner runner) {
runOnFipsEnabledDistribution(runner, () -> {
String truststoreName = "keycloak-truststore.p12";
RawKeycloakDistribution rawDist = runner.getDistribution(RawKeycloakDistribution.class);
rawDist.copyOrReplaceFileFromClasspath("/" + truststoreName, Path.of("conf", truststoreName));

Path truststorePath = rawDist.getDistPath().resolve("conf").resolve(truststoreName).toAbsolutePath();

CLIResult cliResult = runner.run("--verbose", "start", "--fips-mode=non-strict", "--truststore-paths=" + truststorePath);
cliResult.assertStarted();
});
}

@Test
void testUnsupportedHttpsPkcs12KeyStoreInStrictMode(KeycloakRunner runner) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ public void init(Config.Scope config) {
if (storepath == null) {
storepath = System.getProperty(TruststoreBuilder.SYSTEM_TRUSTSTORE_KEY);
if (storepath == null) {
File defaultTrustStore = TruststoreBuilder.getJRETruststore();
File defaultTrustStore = TruststoreBuilder.getImplicitJRETruststore();
if (!defaultTrustStore.exists()) {
throw new RuntimeException("Attribute 'file' missing in 'truststore':'file' configuration, and could not find the system truststore");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import java.io.FileOutputStream;
import java.security.KeyStore;
import java.security.KeyStoreException;
import java.security.Security;
import java.security.cert.Certificate;
import java.security.cert.CertificateException;
import java.security.cert.CertificateFactory;
Expand All @@ -36,6 +37,7 @@
import org.keycloak.common.util.KeystoreUtil;
import org.keycloak.common.util.KeystoreUtil.KeystoreFormat;
import org.keycloak.common.util.KeystoreUtil.TruststoreFormat;
import org.keycloak.utils.StringUtil;

import org.jboss.logging.Logger;

Expand Down Expand Up @@ -220,10 +222,12 @@ static void includeDefaultTruststore(KeyStore truststore) {
String password = null;
String provider = null;
File defaultTrustStore = null;
boolean isImplicitJreTruststore = false;
if (trustStorePath == null) {
trustStorePath = System.getProperty(TruststoreBuilder.SYSTEM_TRUSTSTORE_KEY);
if (trustStorePath == null) {
defaultTrustStore = getJRETruststore();
defaultTrustStore = getImplicitJRETruststore();
isImplicitJreTruststore = true;
} else {
type = System.getProperty(TruststoreBuilder.SYSTEM_TRUSTSTORE_TYPE_KEY, KeyStore.getDefaultType());
password = System.getProperty(TruststoreBuilder.SYSTEM_TRUSTSTORE_PASSWORD_KEY);
Expand Down Expand Up @@ -252,13 +256,13 @@ static void includeDefaultTruststore(KeyStore truststore) {

if (defaultTrustStore.exists()) {
String path = defaultTrustStore.getAbsolutePath();
mergeTrustStore(truststore, path, loadStore(path, type, password, provider));
mergeTrustStore(truststore, path, loadDefaultTruststore(path, type, password, provider, isImplicitJreTruststore));
} else {
LOGGER.warnf("Default truststore was to be included, but could not be found at: %s", defaultTrustStore);
}
}

static File getJRETruststore() {
static File getImplicitJRETruststore() {
// try jre locations - there doesn't seem to be a good default mechanism for this
String securityDirectory = System.getProperty("java.home") + File.separator + "lib" + File.separator
+ "security";
Expand All @@ -269,6 +273,29 @@ static File getJRETruststore() {
return new File(securityDirectory, "cacerts");
}

/**
* Load a truststore, with extra fallbacks for the JRE cacerts file.
* cacerts uses "changeit" as a conventional default password, and may be JKS on older JDKs.
* When isImplicitJreTruststore is true, password and provider are always null.
*/
private static KeyStore loadDefaultTruststore(String path, String type, String password, String provider, boolean isImplicitJreTruststore) {
try {
return loadStore(path, type, password, provider);
} catch (Exception e) {
if (isImplicitJreTruststore) {
// cacerts in JRE 17 and earlier conventionally uses "changeit" as its password; try that before giving up
try {
return loadStore(path, KeyStore.getDefaultType(), "changeit", null);
} catch (Exception e1) {
LOGGER.debugf("Could not load jre truststore %s with default password", path, e1);
// didn't work either - just let the original exception happen
}
}
throw new RuntimeException(
"Failed to initialize truststore: " + new File(path).getAbsolutePath() + ", type: " + type, e);
}
}

static KeyStore loadStore(String path, String type, String password) {
return loadStore(path, type, password, null);
}
Expand All @@ -277,6 +304,17 @@ static KeyStore loadStore(String path, String type, String password, String prov
try {
return KeystoreUtil.loadKeyStore(path, password, type, provider);
} catch (Exception e) {
// passwordless pkcs12 is not supported by all providers (e.g. BCFIPS non-strict mode);
// try SUN if available - it may not be present on OS-level FIPS JVMs
if (StringUtil.isNullOrEmpty(password) && StringUtil.isNullOrEmpty(provider)
&& PKCS12.equalsIgnoreCase(type) && Security.getProvider("SUN") != null) {
try {
return KeystoreUtil.loadKeyStore(path, password, type, "SUN");
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not too happy about ad-hoc hardcoding SUN here. Can't we somehow try and find the first registered provider that supports passwordless keystore.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@vmuzikar added a guard and updated the comment. In practical terms, we only expect this to be SUN - we only fully support OpenJDKs, and the other common VM implementations have a SUN provider. The only situation that wouldn't work is if the user has disabled the SUN provider and added another provider (other than bc / bcfips). Are you trying to cover that case?

} catch (Exception e1) {
LOGGER.debugf("Could not load passwordless %s with SUN provider", path, e1);
// didn't work either - just let the original exception happen
}
}
throw new RuntimeException(
"Failed to initialize truststore: " + new File(path).getAbsolutePath() + ", type: " + type, e);
}
Expand Down
Loading