8000 Override getTrustStore() in DefaultCryptoProvider to avoid BC/JDK mis… by Pepo48 · Pull Request #53900 · keycloak/keycloak · GitHub
[go: up one dir, main page]

Skip to content

Override getTrustStore() in DefaultCryptoProvider to avoid BC/JDK mis… - #53900

Closed
Pepo48 wants to merge 1 commit into
keycloak:mainfrom
Pepo48:issue-53729
Closed

Pepo48 wants to merge 1 commit into
keycloak:mainfrom
Pepo48:issue-53729

Conversation

@Pepo48
@Pepo48 Pepo48 commented Oct 13, 2026
Copy link
Copy Markdown
Contributor

…match

Closes #53729

This was introduced in #51405, which fixed the truststore init ordering so that FileTruststoreProviderFactory actually reads the generated keycloak-truststore.p12. Before that fix, the read never happened against the generated file, so the bug was hidden.

The fix overrides getTrustStore() in DefaultCryptoProvider to return a JDK KeyStore for PKCS12 truststores (they only hold certs, no private keys, so BC is not needed). The JDK implementation honours the NONE property and stores certs unencrypted.

@Pepo48
Pepo48 requested a review from shawkins October 13, 2026 08:31
@Pepo48
Pepo48 requested a review from a team as a code owner October 13, 2026 08:31
Copilot AI lite review requested due to automatic review settings October 13, 2026 08:31
Copilot AI left a comment
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.

🟡 Changes recommended

Unresolved moderate issues remain in provider selection and regression coverage.

1 open finding
What changed in this PR

Fixes generated PKCS12 truststore compatibility by selecting the JDK implementation and adding regression coverage.

Changes:

  • Override PKCS12 truststore creation in DefaultCryptoProvider.
  • Add provider-selection regression coverage.
File Summary
services/​src/​test/​java/​org/​keycloak/​truststore/​TruststoreBuilderTest.java Verifies PKCS12 provider selection; round-trip coverage is still needed.
crypto/​default/​src/​main/​java/​org/​keycloak/​crypto/​def/​DefaultCryptoProvider.java Selects the PKCS12 implementation; explicit JDK provider selection is still needed.

🧠 Review effort: Lite


💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

…match

Closes keycloak#53729

Signed-off-by: Peter Zaoral <pzaoral@redhat.com>
Copilot AI lite review requested due to automatic review settings October 13, 2026 08:54
Copilot AI left a comment
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.

🟢 Approval recommended

No unresolved review issues were identified.

0 open findings

1 resolved since last review

🧠 Review effort: Lite

@shawkins
Copy link
Copy Markdown
Contributor

Before that fix, the read never happened against the generated file, so the bug was hidden.

My understanding is that it's the combination of #51405 with #50110.

The essential problem seems to be ensuring every path that loads the truststore uses a provider that is compatible.

#50110 (comment) indicated: "To sum it up - PKCS12 is absolutely fine; the problem is the mix of OpenJDK 21 defaults and how Keycloak configures it."

So if my understanding is correct there, with JDK 21 the created PKCS12 truststore is not usable by the BC provider.

The fix overrides getTrustStore() in DefaultCryptoProvider to return a JDK KeyStore for PKCS12 truststores (they only hold certs, no private keys, so BC is not needed). The JDK implementation honours the NONE property and stores certs unencrypted.

I think that will work for paths that specify the JDK provider or use CryptoIntegration - but could still fail if the logic is directly picking up the BC provider.

An alternative narrow fix (that would preserve the intent of #50110) would be to change the file truststore provider to use CryptoIntegration - as then the truststore would be loaded by BC as well.

Or should we consider going further to ensure:

  • that the truststore created by the JDK is compatible with BC
  • or that the truststore created by BC is compatible with the JDK

@slaskawi WDYT?

@slaskawi
Copy link
Copy Markdown
Contributor

My understanding is that it's the combination of #51405 with #50110.

The essential problem seems to be ensuring every path that loads the truststore uses a provider that is compatible.

#50110 (comment) indicated: "To sum it up - PKCS12 is absolutely fine; the problem is the mix of OpenJDK 21 defaults and how Keycloak configures it."

So if my understanding is correct there, with JDK 21 the created PKCS12 truststore is not usable by the BC provider.

This is my understanding too. Here's the order of events as I understand it:

BC writes the PKCS12 truststore
              |
              v
Certificates are encrypted using pbeWithSHA1AndRC2_40 
(ignoring the JDK specific keystore.pkcs12.certProtectionAlgorithm=NONE)
              |
              v
The JDK implementation reads it <-- Why always JDK? Is that a mistake?
              |
              v
The reported JVM cannot obtain the pbeWithSHA1AndRC2_40 algorithm
(ERROR: failed to decrypt safe contents entry: java.security.NoSuchAlgorithmException: Cannot find any provider supporting PBEWithSHA1AndRC2_40)

@Pepo48’s approach explicitly selects SUN for PKCS12 truststores in DefaultCryptoProvider. This should address the reported RC2 failure because SUN honors certProtectionAlgorithm=NONE.

So if my understanding is correct there, with JDK 21 the created PKCS12 truststore is not usable by the BC provider.

@shawkins I think that's just a typo and you probably mean that BC created PKCS12 cannot be read by JDK (because of pbeWithSHA1AndRC2_40 being used).

As a side note - I did a small experiment and BC seems to be able to read a PKCS12 created by JDK.

An alternative narrow fix (that would preserve the intent of #50110) would be to change the file truststore provider to use CryptoIntegration - as then the truststore would be loaded by BC as well.

I would lean into this approach as well. So essentially we'd have:


TruststoreBuilder                 FileTruststoreProviderFactory
    |                                 |
CryptoIntegration                 CryptoIntegration
    |                                 |
BC writes                         BC reads
    |                                 |
    +---------- generated file -------+

This essentially would mean refactoring the FileTruststoreProviderFactory and implementing something like this (in pseudo-code):

// inside FileTruststoreProviderFactory.init()
try {
    truststore = loadTrustStore(storepath, pass, resolvedFormat);
} catch (Exception failure) {
    ...
}

// new method(s) in FileTruststoreProviderFactory
KeyStore loadTrustStore(path, password, resolvedFormat) {
    KeyStore store = CryptoIntegration.getProvider()
        .getTrustStore(resolvedFormat);  // replaces ambient provider selection

    try (InputStream input = /* existing filesystem/classpath resolution */) {
        store.load(
            input,
            password == null ? null : password.toCharArray()
        );
    }

    return store;
}

@shawkins
Copy link
Copy Markdown
Contributor

@shawkins I think that's just a typo and you probably mean that BC created PKCS12 cannot be read by JDK (because of pbeWithSHA1AndRC2_40 being used).

I mean it both ways. #53729 is because the JDK provider cannot read the BC FIPS created PKCS12, while #50110 was due to the JDK created truststore not being usable by BC FIPS.

As a side note - I did a small experiment and BC seems to be able to read a PKCS12 created by JDK.

Does that apply to BC FIPS as well? If so, how does that relate to #50110?

@slaskawi
Copy link
Copy Markdown
Contributor

I mean it both ways. #53729 is because the JDK provider cannot read the BC FIPS created PKCS12, while #50110 was due to the JDK created truststore not being usable by BC FIPS.

Understood and agreed! I misread your previous comment.

Does that apply to BC FIPS as well? If so, how does that relate to #50110?

I think the root cause is that #50110 introduced a misalignment between TruststoreBuilder and FileTruststoreProviderFactory. In other words, the #50110 should have also changed the FileTruststoreProviderFactory to use the CryptoIntegration. I probably missed it as there's no test that generates a Trust Store in a TruststoreBuilder and reads it through the FileTruststoreProviderFactory

I can definitely put this on my plate and should be able to get a PR up either tomorrow or on Monday. If we're short on time, I'm OK with merging this PR as a "stop-the-bleeding" approach. I can revert it and introduce FileTruststoreProviderFactory changes along with additional testing.

@Pepo48 @shawkins What do you think about this approach?

@Pepo48
Pepo48 commented Oct 15, 2026
Copy link
Copy Markdown
Contributor Author

@slaskawi I'm fine with it. We can either merge it and proceed with the follow-up as you suggest or you can takeover and reuse this PR directly - whatever you prefer.

Thank you!

@slaskawi
Copy link
Copy Markdown
Contributor

Thanks @Pepo48 ! Let's sync on Monday and see how far I will get. What's the cut-off date for 26.8.2? This probably should aim to squeeze it in.

@shawkins
Copy link
Copy Markdown
Contributor

I think the root cause is that #50110 introduced a misalignment between TruststoreBuilder and FileTruststoreProviderFactory

I'm not talking about what 50110 introduced - I mean what it was trying to address. Under what circumstance was a truststore created by the JDK provider not usable in BC FIPS mode?

@slaskawi
Copy link
Copy Markdown
Contributor

I mean what it was trying to address. Under what circumstance was a truststore created by the JDK provider not usable in BC FIPS mode?

The #50110 aimed to solve the problematic combination of the JDK-generated System TrustStore (which it automatically created) with the Strict FIPS runtime.

A shorter version of my long comment from here would be:

  1. JDK PKCS12 implementation writes the generated truststore
  2. Certificate encryption disabled (keystore.pkcs12.certProtectionAlgorithm=NONE)
  3. Integrity MAC still enabled (keystore.pkcs12.macAlgorithm = HmacPBESHA256 set at the JDK, not Keycloak level)
  4. Strict-FIPS doesn't support it and emits an error

The #50110 changes the Trust Store type in Strict FIPS mode from OpenJDK PKCS12 to BCFKS. BCFKS guarantees that you won't be able to use anything non-FIPS compliant.

@shawkins
Copy link
Copy Markdown
Contributor

Thanks @slaskawi I just wanted to make sure the scope of the statement As a side note - I did a small experiment and BC seems to be able to read a PKCS12 created by JDK. was well understood.

Locally on main with JDK 25 in a non-fips environment I see:

Truststore Create Provider Truststore Read Provider fips-mode outcome
SunJSSE BC disable As per your comment this is ok, and local testing this seems fine.
SunJSSE BCFIPS disable I don't think this is expected to work - errors out prior to the truststore logic with java.lang.NoSuchMethodError: 'void org.bouncycastle.crypto.CryptoServicesRegistrar.checkConstraints(org.bouncycastle.crypto.CryptoServiceProperties)'
SunJSSE BCFIPS non-strict A simple creation / read works - however there is a problem in our production code with a failure merging cacerts, or using password less pkcs12 referenced in truststore paths in general - BCFIPS requires a password. We are missing this test case, and I believe this means that @Pepo48 fix in this PR is needed also.
SunJSSE BCFIPS strict Fails, PKCS12 is not known to the BCFIPS provider.
BC SunJSSE disable In local testing this seems fine, so I think we need a more complete description of the reproducing scenario for #53729
BCFIPS SunJSSE non-strict Seemed to fail with a password problem on the load. Will be addressed by either fix for our production code, but does imply that any custom code must also ensure that the creation provider is matched or we have to further look into ways to ensure compatibility.

@slaskawi I don't think I've ever really fully reproduced the exception scenario with #50110 - I still must be missing something. Perhaps it's a JDK version issue, requires running in a fips enabled environment, or changes to java.security?

@slaskawi
Copy link
Copy Markdown
Contributor

@shawkins We might want to wait for the issue author to tell us how he reproduced it presiesly but I suspect it's a FIPS-configured OS (with Red Hat OpenJDK) with a non-FIPS Keycloak. I described this in detail here.

If that's the case, the solution is like you descibed earlier - "ensuring every path that loads the truststore uses a provider that is compatible". I took this approach and implemented a potential fix: #54017

I will mark it for review once I tidy up the testsuite (which should happen today or over the weekend).

@vmuzikar
Copy link
Copy Markdown
Contributor

With @shawkins we discussed offline that we should really try and reproduce it the original issue (#53729). The fix proposed in #54017 is correcting the behaviour with selecting the crypto providers, however technically it might not be needed if #53729 is not really a real issue. Perhaps I'm missing something?

@slaskawi
Copy link
Copy Markdown
Contributor

With @shawkins we discussed offline that we should really try and reproduce it the original issue (#53729). The fix proposed in #54017 is correcting the behaviour with selecting the crypto providers, however technically it might not be needed if #53729 is not really a real issue. Perhaps I'm missing something?

Thanks for chiming in @vmuzikar! I agree - we might want to wait for the end user comment on this. At the same time, I discovered that this happens on FIPS-enabled systems with a non-FIPS Keyclaok containers running on them. I posted the minimal reproducer (that can be evaluated on a non-FIPS system) here.

If you guys have access to a properly configured Red Hat RHEL system with OpenShift installed on it - you can quickly check if running non-FIPS Keycloak causes the same failure. Unfortunately, I have no such system to test on (that's why I came up with that reproducer).

@shawkins
Copy link
Copy Markdown
Contributor

#54171 is also calling out the non-working case for BCFIPS mentioned in the table above. I will see if this PR and that one should be merged. As the changes that @slaskawi are proposing should be enough to address #53729, we'll likely move forward with these separately.

@shawkins
Copy link
Copy Markdown
Contributor

I think we can close this now as these changes are no longer needed nor address what #53729 is trying to do.

@shawkins shawkins closed this Oct 27, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

5 participants

0