23
23
*/
24
24
package openj9 .internal .security ;
25
25
26
- import java .lang .StackWalker .Option ;
27
26
import java .nio .charset .StandardCharsets ;
28
27
import java .security .MessageDigest ;
29
28
import java .security .NoSuchAlgorithmException ;
@@ -162,6 +161,21 @@ private RestrictedSecurity() {
162
161
super ();
163
162
}
164
163
164
+ /**
165
+ * Check loaded profiles' hash values.
166
+ *
167
+ * In order to avoid unintentional changes in profiles and incentivize
168
+ * extending profiles, instead of altering them, a digest of the profile
169
+ * is calculated and compared to the expected value.
170
+ */
171
+ private static void checkHashValues () {
172
+ ProfileParser parser = profileParser ;
173
+ if ((parser != null ) && !isJarVerifierInStackTrace ()) {
174
+ profileParser = null ;
175
+ parser .checkHashValues ();
176
+ }
177
+ }
178
+
165
179
/**
166
180
* Check if restricted security mode is enabled.
167
181
*
@@ -231,11 +245,7 @@ public static boolean isFIPSEnabled() {
231
245
*/
232
246
public static boolean isServiceAllowed (Service service ) {
233
247
if (securityEnabled ) {
234
- ProfileParser parser = profileParser ;
235
- if ((parser != null ) && !isJarVerifierInStackTrace ()) {
236
- profileParser = null ;
237
- parser .checkHashValues ();
238
- }
248
+ checkHashValues ();
239
249
return restricts .isRestrictedServiceAllowed (service , true );
240
250
}
241
251
return true ;
@@ -249,11 +259,7 @@ public static boolean isServiceAllowed(Service service) {
249
259
*/
250
260
public static boolean canServiceBeRegistered (Service service ) {
251
261
if (securityEnabled ) {
252
- ProfileParser parser = profileParser ;
253
- if ((parser != null ) && !isJarVerifierInStackTrace ()) {
254
- profileParser = null ;
255
- parser .checkHashValues ();
256
- }
262
+ checkHashValues ();
257
263
return restricts .isRestrictedServiceAllowed (service , false );
258
264
}
259
265
return true ;
@@ -267,11 +273,7 @@ public static boolean canServiceBeRegistered(Service service) {
267
273
*/
268
274
public static boolean isProviderAllowed (String providerName ) {
269
275
if (securityEnabled ) {
270
- ProfileParser parser = profileParser ;
271
- if ((parser != null ) && !isJarVerifierInStackTrace ()) {
272
- profileParser = null ;
273
- parser .checkHashValues ();
274
- }
276
+ checkHashValues ();
275
277
// Remove argument, e.g. -NSS-FIPS, if present.
276
278
int pos = providerName .indexOf ('-' );
277
279
if (pos >= 0 ) {
@@ -291,11 +293,7 @@ public static boolean isProviderAllowed(String providerName) {
291
293
*/
292
294
public static boolean isProviderAllowed (Class <?> providerClazz ) {
293
295
if (securityEnabled ) {
294
- ProfileParser parser = profileParser ;
295
- if ((parser != null ) && !isJarVerifierInStackTrace ()) {
296
- profileParser = null ;
297
- parser .checkHashValues ();
298
- }
296
+ checkHashValues ();
299
297
String providerClassName = providerClazz .getName ();
300
298
301
299
// Check if the specified class extends java.security.Provider.
@@ -386,11 +384,11 @@ private static void getProfileID(Properties props) {
386
384
}
387
385
388
386
private static boolean isJarVerifierInStackTrace () {
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
- )
387
+ return StackWalker .getInstance (StackWalker . Option .RETAIN_CLASS_REFERENCE )
388
+ .walk (sf -> sf .map (s -> s .getDeclaringClass ())
389
+ .anyMatch (s -> ("java.util.jar.JarVerifier" .equals (s .getName ())
390
+ && "java.base" .equals (s .getModule (). getName ()))
391
+ )
394
392
);
395
393
}
396
394
0 commit comments