1 /*
2  * Copyright (c) 2018, 2021, Oracle and/or its affiliates. All rights reserved.
3  * Copyright (c) 2018 SAP SE. All rights reserved.
4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5  *
6  * This code is free software; you can redistribute it and/or modify it
7  * under the terms of the GNU General Public License version 2 only, as
8  * published by the Free Software Foundation.  Oracle designates this
9  * particular file as subject to the "Classpath" exception as provided
10  * by Oracle in the LICENSE file that accompanied this code.
11  *
12  * This code is distributed in the hope that it will be useful, but WITHOUT
13  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
15  * version 2 for more details (a copy is included in the LICENSE file that
16  * accompanied this code).
17  *
18  * You should have received a copy of the GNU General Public License version
19  * 2 along with this work; if not, write to the Free Software Foundation,
20  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
21  *
22  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
23  * or visit www.oracle.com if you need additional information or have any
24  * questions.
25  */
26 
27 package sun.security.util;
28 
29 import java.security.AccessController;
30 import java.security.PrivilegedAction;
31 import java.security.Security;
32 
33 public class SecurityProperties {
34 
35     public static final boolean INCLUDE_JAR_NAME_IN_EXCEPTIONS
36         = includedInExceptions("jar");
37 
38     /**
39      * Returns the value of the security property propName, which can be overridden
40      * by a system property of the same name
41      *
42      * @param  propName the name of the system or security property
43      * @return the value of the system or security property
44      */
45     @SuppressWarnings("removal")
privilegedGetOverridable(String propName)46     public static String privilegedGetOverridable(String propName) {
47         if (System.getSecurityManager() == null) {
48             return getOverridableProperty(propName);
49         } else {
50             return AccessController.doPrivileged((PrivilegedAction<String>) () -> getOverridableProperty(propName));
51         }
52     }
53 
getOverridableProperty(String propName)54     private static String getOverridableProperty(String propName) {
55         String val = System.getProperty(propName);
56         if (val == null) {
57             return Security.getProperty(propName);
58         } else {
59             return val;
60         }
61     }
62 
63     /**
64      * Returns true in case the system or security property "jdk.includeInExceptions"
65      * contains the category refName
66      *
67      * @param refName the category to check
68      * @return true in case the system or security property "jdk.includeInExceptions"
69      *         contains refName, false otherwise
70      */
includedInExceptions(String refName)71     public static boolean includedInExceptions(String refName) {
72         String val = privilegedGetOverridable("jdk.includeInExceptions");
73         if (val == null) {
74             return false;
75         }
76 
77         String[] tokens = val.split(",");
78         for (String token : tokens) {
79             token = token.trim();
80             if (token.equalsIgnoreCase(refName)) {
81                 return true;
82             }
83         }
84         return false;
85     }
86 }
87