Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Workaround unknown SecureRandom strong algorithm with OpenJDK17 & RHEL & BCFIPS provider when run in FIPS-enabled environment #40665

Merged
Merged
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 @@ -6,16 +6,27 @@
import static io.quarkus.security.runtime.SecurityProviderUtils.loadProvider;
import static io.quarkus.security.runtime.SecurityProviderUtils.loadProviderWithParams;

import java.security.NoSuchAlgorithmException;
import java.security.Provider;
import java.security.SecureRandom;
import java.security.Security;

import org.jboss.logging.Logger;

import io.quarkus.runtime.annotations.Recorder;

@Recorder
public class SecurityProviderRecorder {

private static final Logger LOG = Logger.getLogger(SecurityProviderRecorder.class);

public void addBouncyCastleProvider(boolean inFipsMode) {
final String providerName = inFipsMode ? SecurityProviderUtils.BOUNCYCASTLE_FIPS_PROVIDER_CLASS_NAME
: SecurityProviderUtils.BOUNCYCASTLE_PROVIDER_CLASS_NAME;
addProvider(loadProvider(providerName));
if (inFipsMode) {
setSecureRandomStrongAlgorithmIfNecessary();
}
}

public void addBouncyCastleJsseProvider() {
Expand All @@ -33,5 +44,23 @@ public void addBouncyCastleFipsJsseProvider() {
Provider bcJsse = loadProviderWithParams(SecurityProviderUtils.BOUNCYCASTLE_JSSE_PROVIDER_CLASS_NAME,
new Class[] { boolean.class, Provider.class }, new Object[] { true, bc });
insertProvider(bcJsse, sunIndex + 1);
setSecureRandomStrongAlgorithmIfNecessary();
}

private void setSecureRandomStrongAlgorithmIfNecessary() {
try {
// workaround for the issue on OpenJDK 17 & RHEL8 & FIPS
// see https://github.com/bcgit/bc-java/issues/1285#issuecomment-2068958587
// we can remove this when OpenJDK 17 support is dropped or if it starts working on newer versions of RHEL8+
SecureRandom.getInstanceStrong();
} catch (NoSuchAlgorithmException e) {
SecureRandom secRandom = new SecureRandom();
String origStrongAlgorithms = Security.getProperty("securerandom.strongAlgorithms");
String usedAlgorithm = secRandom.getAlgorithm() + ":" + secRandom.getProvider().getName();
String strongAlgorithms = origStrongAlgorithms == null ? usedAlgorithm : usedAlgorithm + "," + origStrongAlgorithms;
LOG.debugf("Strong SecureRandom algorithm '%s' is not available. "
+ "Using fallback algorithm '%s'.", origStrongAlgorithms, usedAlgorithm);
Security.setProperty("securerandom.strongAlgorithms", strongAlgorithms);
}
}
}
Loading