23
23
*/
24
24
package openj9 .internal .security ;
25
25
26
+ import java .lang .StackWalker .Option ;
26
27
import java .nio .charset .StandardCharsets ;
27
28
import java .security .MessageDigest ;
28
29
import java .security .NoSuchAlgorithmException ;
@@ -79,8 +80,6 @@ public final class RestrictedSecurity {
79
80
80
81
private static RestrictedSecurityProperties restricts ;
81
82
82
- private static boolean profileHashChecked = false ;
83
-
84
83
private static final Set <String > unmodifiableProperties = new HashSet <>();
85
84
86
85
private static final Map <String , List <String >> supportedPlatformsNSS = new HashMap <>();
@@ -163,20 +162,6 @@ private RestrictedSecurity() {
163
162
super ();
164
163
}
165
164
166
- /**
167
- * Check loaded profiles' hash values.
168
- *
169
- * In order to avoid unintentional changes in profiles and incentivize
170
- * extending profiles, instead of altering them, a digest of the profile
171
- * is calculated and compared to the expected value.
172
- */
173
- public static void checkHashValues () {
174
- if (profileParser != null ) {
175
- profileParser .checkHashValues ();
176
- profileParser = null ;
177
- }
178
- }
179
-
180
165
/**
181
166
* Check if restricted security mode is enabled.
182
167
*
@@ -246,9 +231,10 @@ public static boolean isFIPSEnabled() {
246
231
*/
247
232
public static boolean isServiceAllowed (Service service ) {
248
233
if (securityEnabled ) {
249
- if (!(profileHashChecked || isJarVerifierinStackTrace ())) {
250
- profileHashChecked = true ;
251
- checkHashValues ();
234
+ ProfileParser parser = profileParser ;
235
+ if ((parser != null ) && !isJarVerifierInStackTrace ()) {
236
+ profileParser = null ;
237
+ parser .checkHashValues ();
252
238
}
253
239
return restricts .isRestrictedServiceAllowed (service , true );
254
240
}
@@ -263,9 +249,10 @@ public static boolean isServiceAllowed(Service service) {
263
249
*/
264
250
public static boolean canServiceBeRegistered (Service service ) {
265
251
if (securityEnabled ) {
266
- if (!(profileHashChecked || isJarVerifierinStackTrace ())) {
267
- profileHashChecked = true ;
268
- checkHashValues ();
252
+ ProfileParser parser = profileParser ;
253
+ if ((parser != null ) && !isJarVerifierInStackTrace ()) {
254
+ profileParser = null ;
255
+ parser .checkHashValues ();
269
256
}
270
257
return restricts .isRestrictedServiceAllowed (service , false );
271
258
}
@@ -280,9 +267,10 @@ public static boolean canServiceBeRegistered(Service service) {
280
267
*/
281
268
public static boolean isProviderAllowed (String providerName ) {
282
269
if (securityEnabled ) {
283
- if (!(profileHashChecked || isJarVerifierinStackTrace ())) {
284
- profileHashChecked = true ;
285
- checkHashValues ();
270
+ ProfileParser parser = profileParser ;
271
+ if ((parser != null ) && !isJarVerifierInStackTrace ()) {
272
+ profileParser = null ;
273
+ parser .checkHashValues ();
286
274
}
287
275
// Remove argument, e.g. -NSS-FIPS, if present.
288
276
int pos = providerName .indexOf ('-' );
@@ -303,9 +291,10 @@ public static boolean isProviderAllowed(String providerName) {
303
291
*/
304
292
public static boolean isProviderAllowed (Class <?> providerClazz ) {
305
293
if (securityEnabled ) {
306
- if (!(profileHashChecked || isJarVerifierinStackTrace ())) {
307
- profileHashChecked = true ;
308
- checkHashValues ();
294
+ ProfileParser parser = profileParser ;
295
+ if ((parser != null ) && !isJarVerifierInStackTrace ()) {
296
+ profileParser = null ;
297
+ parser .checkHashValues ();
309
298
}
310
299
String providerClassName = providerClazz .getName ();
311
300
@@ -397,15 +386,12 @@ private static void getProfileID(Properties props) {
397
386
}
398
387
399
388
private static boolean isJarVerifierinStackTrace () {
400
- StackTraceElement [] elements = Thread .currentThread ().getStackTrace ();
401
- for (int i = 1 ; i < elements .length ; i ++) {
402
- StackTraceElement stackTraceElement = elements [i ];
403
- if ("java.util.jar.JarVerifier" .equals (stackTraceElement .getClassName ())
404
- && "java.base" .equals (stackTraceElement .getModuleName ())) {
405
- return true ;
406
- }
407
- }
408
- return false ;
389
+ return StackWalker .getInstance (Option .RETAIN_CLASS_REFERENCE )
390
+ .walk (sf -> sf .map (s -> s .toStackTraceElement ())
391
+ .anyMatch (s -> ("java.util.jar.JarVerifier" .equals (s .getClassName ())
392
+ && "java.base" .equals (s .getModuleName ()))
393
+ )
394
+ );
409
395
}
410
396
411
397
private static void checkIfKnownProfileSupported () {
0 commit comments