GlueGen v2.6.0-rc-20250712
GlueGen, Native Binding Generator for Java™ (public API).
SecurityUtil.java
Go to the documentation of this file.
1/**
2 * Copyright 2012 JogAmp Community. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without modification, are
5 * permitted provided that the following conditions are met:
6 *
7 * 1. Redistributions of source code must retain the above copyright notice, this list of
8 * conditions and the following disclaimer.
9 *
10 * 2. Redistributions in binary form must reproduce the above copyright notice, this list
11 * of conditions and the following disclaimer in the documentation and/or other materials
12 * provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY JogAmp Community ``AS IS'' AND ANY EXPRESS OR IMPLIED
15 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
16 * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL JogAmp Community OR
17 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
18 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
19 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
20 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
21 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
22 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
23 *
24 * The views and conclusions contained in the software and documentation are those of the
25 * authors and should not be interpreted as representing official policies, either expressed
26 * or implied, of JogAmp Community.
27 */
28package com.jogamp.common.util;
29
30import java.security.AllPermission;
31import java.security.CodeSource;
32import java.security.Permission;
33import java.security.PrivilegedAction;
34import java.security.ProtectionDomain;
35import java.security.cert.Certificate;
36
37import jogamp.common.os.PlatformPropsImpl;
38
39public class SecurityUtil {
40 @SuppressWarnings("removal")
41 private static final SecurityManager securityManager;
42 private static final Permission allPermissions;
43 private static final boolean DEBUG = false;
44
45 /**
46 * Call wrapper for {@link System#getSecurityManager()}.
47 * <p>
48 * {@link System#getSecurityManager()} is deprecated
49 * since Java 17 (JEP 411) and earmarked to be removed.<br/>
50 * </p>
51 * <p>
52 * On a Java 17 machine, this method will simply return null.
53 * </p>
54 */
55 @SuppressWarnings({ "deprecation", "removal" })
56 public static final SecurityManager getSecurityManager() {
57 if( PlatformPropsImpl.JAVA_17 ) {
58 return null;
59 } else {
60 return System.getSecurityManager();
61 }
62 }
63
64 /**
65 * Call wrapper for {@link java.security.AccessController#doPrivileged(PrivilegedAction)}.
66 * <p>
67 * {@link java.security.AccessController#doPrivileged(PrivilegedAction)} is deprecated
68 * since Java 17 (JEP 411) and earmarked to be removed.<br/>
69 * </p>
70 * <p>
71 * On a Java 17 machine, this method will simply invoke the given PrivilegedAction<T>.
72 * </p>
73 * @param <T> return type of PrivilegedAction<T>
74 * @param o the PrivilegedAction<T>
75 * @return the return type
76 */
77 @SuppressWarnings({ "deprecation", "removal" })
78 public static <T> T doPrivileged(final PrivilegedAction<T> o) {
79 if( PlatformPropsImpl.JAVA_17 ) {
80 return o.run();
81 } else {
82 return java.security.AccessController.doPrivileged( o );
83 }
84 }
85
86 static {
87 allPermissions = new AllPermission();
88 securityManager = getSecurityManager();
89
90 if( DEBUG ) {
91 final boolean hasAllPermissions;
92 {
93 final ProtectionDomain insecPD = doPrivileged(new PrivilegedAction<ProtectionDomain>() {
94 @Override
95 public ProtectionDomain run() {
96 return SecurityUtil.class.getProtectionDomain();
97 } } );
98 boolean _hasAllPermissions;
99 try {
100 insecPD.implies(allPermissions);
101 _hasAllPermissions = true;
102 } catch( final SecurityException ace ) {
103 _hasAllPermissions = false;
104 }
105 hasAllPermissions = _hasAllPermissions;
106 }
107
108 System.err.println("SecurityUtil: Has SecurityManager: "+ ( null != securityManager ) ) ;
109 System.err.println("SecurityUtil: Has AllPermissions: "+hasAllPermissions);
110 final Certificate[] certs = doPrivileged(new PrivilegedAction<Certificate[]>() {
111 @Override
112 public Certificate[] run() {
113 return getCerts(SecurityUtil.class);
114 } } );
115 System.err.println("SecurityUtil: Cert count: "+ ( null != certs ? certs.length : 0 ));
116 if( null != certs ) {
117 for(int i=0; i<certs.length; i++) {
118 System.err.println("\t cert["+i+"]: "+certs[i].toString());
119 }
120 }
121 }
122 }
123
124 /**
125 * Returns <code>true</code> if no {@link SecurityManager} has been installed
126 * or the installed {@link SecurityManager}'s <code>checkPermission(new AllPermission())</code>
127 * passes. Otherwise method returns <code>false</code>.
128 */
129 public static final boolean hasAllPermissions() {
130 return hasPermission(allPermissions);
131 }
132
133 /**
134 * Returns <code>true</code> if no {@link SecurityManager} has been installed
135 * or the installed {@link SecurityManager}'s <code>checkPermission(perm)</code>
136 * passes. Otherwise method returns <code>false</code>.
137 */
138 public static final boolean hasPermission(final Permission perm) {
139 try {
140 checkPermission(perm);
141 return true;
142 } catch( final SecurityException ace ) {
143 return false;
144 }
145 }
146
147 /**
148 * Throws an {@link SecurityException} if an installed {@link SecurityManager}
149 * does not permit the requested {@link AllPermission}.
150 */
151 public static final void checkAllPermissions() throws SecurityException {
152 checkPermission(allPermissions);
153 }
154
155 /**
156 * Throws an {@link SecurityException} if an installed {@link SecurityManager}
157 * does not permit the requested {@link Permission}.
158 */
159 public static final void checkPermission(final Permission perm) throws SecurityException {
160 if( null != securityManager ) {
161 securityManager.checkPermission(perm);
162 }
163 }
164
165 /**
166 * Returns <code>true</code> if no {@link SecurityManager} has been installed
167 * or the installed {@link SecurityManager}'s <code>checkLink(libName)</code>
168 * passes. Otherwise method returns <code>false</code>.
169 */
170 public static final boolean hasLinkPermission(final String libName) {
171 try {
172 checkLinkPermission(libName);
173 return true;
174 } catch( final SecurityException ace ) {
175 return false;
176 }
177 }
178
179 /**
180 * Throws an {@link SecurityException} if an installed {@link SecurityManager}
181 * does not permit to dynamically link the given libName.
182 */
183 public static final void checkLinkPermission(final String libName) throws SecurityException {
184 if( null != securityManager ) {
185 securityManager.checkLink(libName);
186 }
187 }
188
189 /**
190 * Throws an {@link SecurityException} if an installed {@link SecurityManager}
191 * does not permit to dynamically link to all libraries.
192 */
193 public static final void checkAllLinkPermission() throws SecurityException {
194 if( null != securityManager ) {
195 securityManager.checkPermission(allLinkPermission);
196 }
197 }
198 private static final RuntimePermission allLinkPermission = new RuntimePermission("loadLibrary.*");
199
200 /**
201 * @param clz
202 * @return
203 * @throws SecurityException if the caller has no permission to access the ProtectedDomain of the given class.
204 */
205 public static final Certificate[] getCerts(final Class<?> clz) throws SecurityException {
206 final ProtectionDomain pd = clz.getProtectionDomain();
207 final CodeSource cs = (null != pd) ? pd.getCodeSource() : null;
208 final Certificate[] certs = (null != cs) ? cs.getCertificates() : null;
209 return (null != certs && certs.length>0) ? certs : null;
210 }
211
212 public static final boolean equals(final Certificate[] a, final Certificate[] b) {
213 if(a == b) {
214 return true;
215 }
216 if(a==null || b==null) {
217 return false;
218 }
219 if(a.length != b.length) {
220 return false;
221 }
222
223 int i = 0;
224 while( i < a.length && a[i].equals(b[i]) ) {
225 i++;
226 }
227 return i == a.length;
228 }
229}
static final SecurityManager getSecurityManager()
Call wrapper for System#getSecurityManager().
static final boolean hasLinkPermission(final String libName)
Returns true if no SecurityManager has been installed or the installed SecurityManager's checkLink(li...
static< T > T doPrivileged(final PrivilegedAction< T > o)
Call wrapper for java.security.AccessController#doPrivileged(PrivilegedAction).
static final void checkPermission(final Permission perm)
Throws an SecurityException if an installed SecurityManager does not permit the requested Permission.
static final boolean hasAllPermissions()
Returns true if no SecurityManager has been installed or the installed SecurityManager's checkPermiss...
static final boolean hasPermission(final Permission perm)
Returns true if no SecurityManager has been installed or the installed SecurityManager's checkPermiss...
static final boolean equals(final Certificate[] a, final Certificate[] b)
static final Certificate[] getCerts(final Class<?> clz)
static final void checkAllPermissions()
Throws an SecurityException if an installed SecurityManager does not permit the requested AllPermissi...
static final void checkLinkPermission(final String libName)
Throws an SecurityException if an installed SecurityManager does not permit to dynamically link the g...
static final void checkAllLinkPermission()
Throws an SecurityException if an installed SecurityManager does not permit to dynamically link to al...