1 /*
2  * Copyright (c) 2018, 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      */
privilegedGetOverridable(String propName)45     public static String privilegedGetOverridable(String propName) {
46         return AccessController.doPrivileged((PrivilegedAction<String>)
47             () -> {
48                 String val = System.getProperty(propName);
49                 if (val == null) {
50                     return Security.getProperty(propName);
51                 } else {
52                     return val;
53                 }
54             });
55     }
56 
57     /**
58      * Returns true in case the system or security property "jdk.includeInExceptions"
59      * contains the category refName
60      *
61      * @param refName the category to check
62      * @return true in case the system or security property "jdk.includeInExceptions"
63      *         contains refName, false otherwise
64      */
includedInExceptions(String refName)65     public static boolean includedInExceptions(String refName) {
66         String val = privilegedGetOverridable("jdk.includeInExceptions");
67         if (val == null) {
68             return false;
69         }
70 
71         String[] tokens = val.split(",");
72         for (String token : tokens) {
73             token = token.trim();
74             if (token.equalsIgnoreCase(refName)) {
75                 return true;
76             }
77         }
78         return false;
79     }
80 }
81