Skip to content

Commit 7b8aba0

Browse files
Support SecureRandom strong algorithms in FIPS mode
This change overrides the default securerandom.strongAlgorithms configuration used by SecureRandom.getInstanceStrong() when it runs in FIPS mode. This PR also includes two additional backports. One removes the provider-specific condition from the SecureRandom strongAlgorithms test(#1225). The other handles the Windows-specific SecureRandom strongAlgorithms defaults used by the test(#1232). Signed-off-by: Mohit Rajbhar <mohit.rajbhar@ibm.com>
1 parent 67d3d1e commit 7b8aba0

4 files changed

Lines changed: 247 additions & 1 deletion

File tree

closed/src/java.base/share/classes/openj9/internal/security/RestrictedSecurity.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -613,6 +613,7 @@ private static void setProperties(Properties props) {
613613
propsMapping.put("jdk.tls.legacyAlgorithms", restricts.jdkTlsLegacyAlgorithms);
614614
propsMapping.put("jdk.certpath.disabledAlgorithms", restricts.jdkCertpathDisabledAlgorithms);
615615
propsMapping.put("jdk.security.legacyAlgorithms", restricts.jdkSecurityLegacyAlgorithms);
616+
propsMapping.put("securerandom.strongAlgorithms", restricts.jdkSecureRandomStrongAlgorithms);
616617

617618
if (restricts.descIsFIPS) {
618619
if (restricts.jdkFipsMode == null) {
@@ -820,6 +821,7 @@ private static final class RestrictedSecurityProperties {
820821
// For SecureRandom.
821822
final String jdkSecureRandomProvider;
822823
final String jdkSecureRandomAlgorithm;
824+
final String jdkSecureRandomStrongAlgorithms;
823825

824826
final String jdkFipsMode;
825827

@@ -855,6 +857,7 @@ private RestrictedSecurityProperties(String profileID, ProfileParser parser) {
855857
// For SecureRandom.
856858
this.jdkSecureRandomProvider = parser.getProperty("jdkSecureRandomProvider");
857859
this.jdkSecureRandomAlgorithm = parser.getProperty("jdkSecureRandomAlgorithm");
860+
this.jdkSecureRandomStrongAlgorithms = parser.getProperty("jdkSecureRandomStrongAlgorithms");
858861

859862
this.jdkFipsMode = parser.getProperty("jdkFipsMode");
860863

@@ -1150,6 +1153,7 @@ private void listUsedProfile() {
11501153
printProperty(profileID + ".javax.net.ssl.keyStore: ", keyStore);
11511154
printProperty(profileID + ".securerandom.provider: ", jdkSecureRandomProvider);
11521155
printProperty(profileID + ".securerandom.algorithm: ", jdkSecureRandomAlgorithm);
1156+
printProperty(profileID + ".securerandom.strongAlgorithms: ", jdkSecureRandomStrongAlgorithms);
11531157
System.out.println();
11541158
}
11551159

@@ -1553,6 +1557,9 @@ private String getExistingValue(String property) {
15531557
case "jdkTlsLegacyAlgorithms":
15541558
propertyKey = "jdk.tls.legacyAlgorithms";
15551559
break;
1560+
case "jdkSecureRandomStrongAlgorithms":
1561+
propertyKey = "securerandom.strongAlgorithms";
1562+
break;
15561563
default:
15571564
return null;
15581565
}
@@ -1608,6 +1615,8 @@ private void loadProperties(String profileID, List<String> allInfo) {
16081615
profileID + ".securerandom.provider", allInfo);
16091616
setProperty("jdkSecureRandomAlgorithm",
16101617
profileID + ".securerandom.algorithm", allInfo);
1618+
setProperty("jdkSecureRandomStrongAlgorithms",
1619+
profileID + ".securerandom.strongAlgorithms", allInfo);
16111620
setProperty("jdkFipsMode",
16121621
profileID + ".fips.mode", allInfo);
16131622

@@ -1953,6 +1962,7 @@ private static boolean isPropertyAppendable(String property) {
19531962
case "jdkTlsDisabledAlgorithms":
19541963
case "jdkTlsDisabledNamedCurves":
19551964
case "jdkTlsLegacyAlgorithms":
1965+
case "jdkSecureRandomStrongAlgorithms":
19561966
return true;
19571967
default:
19581968
return false;

closed/test/jdk/openj9/internal/security/TestProperties.java

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,69 @@ private static Stream<Arguments> patternMatches_propertiesList() {
202202
return tests.build();
203203
}
204204

205+
private static Stream<Arguments> patternMatches_strongAlgorithms() {
206+
Stream.Builder<Arguments> tests = Stream.builder();
207+
208+
// 1 - Test property - base profile with securerandom.strongAlgorithms loads successfully.
209+
tests.add(Arguments.of("Test-Profile-strongAlgorithms",
210+
System.getProperty("test.src") + "/property-java.security",
211+
"(?s)(?=.*OpenJCEPlusFIPS)(?=.*SUN)(?=.*SunJSSE)",
212+
0));
213+
// 2 - Test property - securerandom.strongAlgorithms property with multiple algorithms.
214+
tests.add(Arguments.of("Test-Profile-strongAlgorithms-MultipleEntries",
215+
System.getProperty("test.src") + "/property-java.security",
216+
"securerandom\\.strongAlgorithms: SHA512DRBG:OpenJCEPlusFIPS, SHA256DRBG:OpenJCEPlusFIPS",
217+
0));
218+
// 3 - Test property - securerandom.strongAlgorithms append algorithm in extended profile.
219+
tests.add(Arguments.of("Test-Profile-strongAlgorithms-Extension_1",
220+
System.getProperty("test.src") + "/property-java.security",
221+
"securerandom\\.strongAlgorithms: SHA512DRBG:OpenJCEPlusFIPS, SHA256DRBG:OpenJCEPlusFIPS",
222+
0));
223+
// 4 - Test property - securerandom.strongAlgorithms remove algorithm in extended profile.
224+
tests.add(Arguments.of("Test-Profile-strongAlgorithms-Extension_2",
225+
System.getProperty("test.src") + "/property-java.security",
226+
"securerandom\\.strongAlgorithms: (NativePRNGBlocking:SUN|Windows-PRNG:SunMSCAPI),DRBG:SUN",
227+
0));
228+
// 5 - Test property - securerandom.strongAlgorithms invalid algorithm.
229+
tests.add(Arguments.of("Test-Profile-strongAlgorithms-InvalidFormat",
230+
System.getProperty("test.src") + "/property-java.security",
231+
"FAILED: No strong SecureRandom impls available: .*",
232+
0));
233+
// 6 - Test property - securerandom.strongAlgorithms missing algorithm.
234+
tests.add(Arguments.of("Test-Profile-strongAlgorithms-MissingAlgo",
235+
System.getProperty("test.src") + "/property-java.security",
236+
"FAILED: No strong SecureRandom impls available: .*",
237+
0));
238+
// 7 - Test property - securerandom.strongAlgorithms missing provider.
239+
tests.add(Arguments.of("Test-Profile-strongAlgorithms-MissingProvider",
240+
System.getProperty("test.src") + "/property-java.security",
241+
"FAILED: missing provider",
242+
0));
243+
// 8 - Test property - set invalid provider.
244+
tests.add(Arguments.of("Test-Profile-strongAlgorithms-InvalidProvider",
245+
System.getProperty("test.src") + "/property-java.security",
246+
"FAILED: No strong SecureRandom impls available: .*",
247+
0));
248+
// 9 - Test property - securerandom.strongAlgorithms when only algorithm is present.
249+
tests.add(Arguments.of("Test-Profile-strongAlgorithms-Specify-Algo-Only",
250+
System.getProperty("test.src") + "/property-java.security",
251+
"securerandom\\.strongAlgorithms: SHA(256|512)DRBG$",
252+
0));
253+
// 10 - Test property - invalid algorithm.
254+
tests.add(Arguments.of("Test-Profile-strongAlgorithms-InvalidAlgorithm",
255+
System.getProperty("test.src") + "/property-java.security",
256+
"FAILED: No strong SecureRandom impls available: .*",
257+
0));
258+
// 11 - Test property - securerandom.strongAlgorithms misspelled property name.
259+
tests.add(Arguments.of("Test-Profile-strongAlgorithms-MisspelledPropertyName",
260+
System.getProperty("test.src") + "/property-java.security",
261+
"The property names: RestrictedSecurity.Test-Profile-strongAlgorithms-MisspelledPropertyName.securerandom.strongAlgorithmsWrong "
262+
+ "in profile RestrictedSecurity.Test-Profile-strongAlgorithms-MisspelledPropertyName \\(or a base profile\\) are not recognized",
263+
1));
264+
265+
return tests.build();
266+
}
267+
205268
private static Stream<Arguments> patternMatches_systemProperties() {
206269
return Stream.of(
207270
// 1 - Test property - base profile with javax.net.ssl.keyStore loads successfully.
@@ -256,6 +319,19 @@ public void shouldContain_propertiesList(String customprofile, String securityPr
256319
outputAnalyzer.shouldHaveExitValue(exitValue).shouldMatch(expected);
257320
}
258321

322+
@ParameterizedTest
323+
@MethodSource("patternMatches_strongAlgorithms")
324+
public void shouldContain_strongAlgorithms(String customprofile, String securityPropertyFile, String expected, int exitValue) throws Exception {
325+
OutputAnalyzer outputAnalyzer = ProcessTools.executeTestJava(
326+
"-Dsemeru.fips=true",
327+
"-Dsemeru.customprofile=" + customprofile,
328+
"-Djava.security.properties=" + securityPropertyFile,
329+
"TestProperties"
330+
);
331+
outputAnalyzer.reportDiagnosticSummary();
332+
outputAnalyzer.shouldHaveExitValue(exitValue).shouldMatch(expected);
333+
}
334+
259335
@ParameterizedTest
260336
@MethodSource("patternMatches_systemProperties")
261337
public void shouldContain_systemProperties(String customprofile, String securityPropertyFile, String expected, int exitValue) throws Exception {
@@ -278,6 +354,18 @@ private static boolean isProviderPresent(String providerName) {
278354
return false;
279355
}
280356

357+
private static void testStrongAlgorithms() {
358+
String strongAlgorithms = Security.getProperty("securerandom.strongAlgorithms");
359+
if ((strongAlgorithms != null) && !strongAlgorithms.isEmpty()) {
360+
try {
361+
java.security.SecureRandom.getInstanceStrong();
362+
System.out.println("securerandom.strongAlgorithms: " + strongAlgorithms);
363+
} catch (java.security.NoSuchAlgorithmException | IllegalArgumentException e) {
364+
System.out.println("FAILED: " + e.getMessage());
365+
}
366+
}
367+
}
368+
281369
private static void testSystemProperties() {
282370
// Test javax.net.ssl.keyStore system property.
283371
String keyStore = System.getProperty("javax.net.ssl.keyStore");
@@ -299,6 +387,7 @@ public static void main(String[] args) {
299387
System.out.println("Provider Name: " + provider.getName());
300388
System.out.println("Provider Version: " + provider.getVersionStr());
301389
}
390+
testStrongAlgorithms();
302391
testSystemProperties();
303392
} catch (Exception e) {
304393
System.out.println(e);

0 commit comments

Comments
 (0)