author | mullan |
Fri, 12 Jun 2015 12:50:41 -0400 | |
changeset 31141 | ef5ddf021137 |
parent 29492 | a4bf9a570035 |
child 31183 | eb0ffbecb4ba |
permissions | -rw-r--r-- |
2 | 1 |
/* |
29492
a4bf9a570035
8028266: Tidy warnings cleanup for packages java.security/javax.security
avstepan
parents:
25859
diff
changeset
|
2 |
* Copyright (c) 1997, 2015, Oracle and/or its affiliates. All rights reserved. |
2 | 3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
4 |
* |
|
5 |
* This code is free software; you can redistribute it and/or modify it |
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
|
5506 | 7 |
* published by the Free Software Foundation. Oracle designates this |
2 | 8 |
* particular file as subject to the "Classpath" exception as provided |
5506 | 9 |
* by Oracle in the LICENSE file that accompanied this code. |
2 | 10 |
* |
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that |
|
15 |
* accompanied this code). |
|
16 |
* |
|
17 |
* You should have received a copy of the GNU General Public License version |
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation, |
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
20 |
* |
|
5506 | 21 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
22 |
* or visit www.oracle.com if you need additional information or have any |
|
23 |
* questions. |
|
2 | 24 |
*/ |
25 |
||
26 |
package java.security; |
|
27 |
||
28 |
import java.util.HashMap; |
|
29 |
import java.util.ArrayList; |
|
30 |
import java.net.URL; |
|
31 |
||
32 |
import sun.security.util.Debug; |
|
33 |
||
34 |
/** |
|
35 |
* This class extends ClassLoader with additional support for defining |
|
36 |
* classes with an associated code source and permissions which are |
|
37 |
* retrieved by the system policy by default. |
|
38 |
* |
|
39 |
* @author Li Gong |
|
40 |
* @author Roland Schemers |
|
41 |
*/ |
|
42 |
public class SecureClassLoader extends ClassLoader { |
|
43 |
/* |
|
44 |
* If initialization succeed this is set to true and security checks will |
|
45 |
* succeed. Otherwise the object is not initialized and the object is |
|
46 |
* useless. |
|
47 |
*/ |
|
2448
1e8128f3ff61
4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents:
2
diff
changeset
|
48 |
private final boolean initialized; |
2 | 49 |
|
31141
ef5ddf021137
6826789: SecureClassLoader should not use CodeSource URLs as HashMap keys
mullan
parents:
29492
diff
changeset
|
50 |
/* |
ef5ddf021137
6826789: SecureClassLoader should not use CodeSource URLs as HashMap keys
mullan
parents:
29492
diff
changeset
|
51 |
* HashMap that maps the CodeSource URL (as a String) to ProtectionDomain. |
ef5ddf021137
6826789: SecureClassLoader should not use CodeSource URLs as HashMap keys
mullan
parents:
29492
diff
changeset
|
52 |
* We use a String instead of a CodeSource/URL as the key to avoid |
ef5ddf021137
6826789: SecureClassLoader should not use CodeSource URLs as HashMap keys
mullan
parents:
29492
diff
changeset
|
53 |
* potential expensive name service lookups. This does mean that URLs that |
ef5ddf021137
6826789: SecureClassLoader should not use CodeSource URLs as HashMap keys
mullan
parents:
29492
diff
changeset
|
54 |
* are equivalent after nameservice lookup will be placed in separate |
ef5ddf021137
6826789: SecureClassLoader should not use CodeSource URLs as HashMap keys
mullan
parents:
29492
diff
changeset
|
55 |
* ProtectionDomains; however during policy enforcement these URLs will be |
ef5ddf021137
6826789: SecureClassLoader should not use CodeSource URLs as HashMap keys
mullan
parents:
29492
diff
changeset
|
56 |
* canonicalized and resolved resulting in a consistent set of granted |
ef5ddf021137
6826789: SecureClassLoader should not use CodeSource URLs as HashMap keys
mullan
parents:
29492
diff
changeset
|
57 |
* permissions. |
ef5ddf021137
6826789: SecureClassLoader should not use CodeSource URLs as HashMap keys
mullan
parents:
29492
diff
changeset
|
58 |
*/ |
ef5ddf021137
6826789: SecureClassLoader should not use CodeSource URLs as HashMap keys
mullan
parents:
29492
diff
changeset
|
59 |
private final HashMap<String, ProtectionDomain> pdcache = new HashMap<>(11); |
2 | 60 |
|
61 |
private static final Debug debug = Debug.getInstance("scl"); |
|
62 |
||
2448
1e8128f3ff61
4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents:
2
diff
changeset
|
63 |
static { |
1e8128f3ff61
4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents:
2
diff
changeset
|
64 |
ClassLoader.registerAsParallelCapable(); |
1e8128f3ff61
4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents:
2
diff
changeset
|
65 |
} |
1e8128f3ff61
4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents:
2
diff
changeset
|
66 |
|
2 | 67 |
/** |
68 |
* Creates a new SecureClassLoader using the specified parent |
|
69 |
* class loader for delegation. |
|
70 |
* |
|
71 |
* <p>If there is a security manager, this method first |
|
18579
b678846778ad
8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents:
9275
diff
changeset
|
72 |
* calls the security manager's {@code checkCreateClassLoader} |
2 | 73 |
* method to ensure creation of a class loader is allowed. |
29492
a4bf9a570035
8028266: Tidy warnings cleanup for packages java.security/javax.security
avstepan
parents:
25859
diff
changeset
|
74 |
* |
2 | 75 |
* @param parent the parent ClassLoader |
76 |
* @exception SecurityException if a security manager exists and its |
|
18579
b678846778ad
8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents:
9275
diff
changeset
|
77 |
* {@code checkCreateClassLoader} method doesn't allow |
2 | 78 |
* creation of a class loader. |
79 |
* @see SecurityManager#checkCreateClassLoader |
|
80 |
*/ |
|
81 |
protected SecureClassLoader(ClassLoader parent) { |
|
82 |
super(parent); |
|
83 |
// this is to make the stack depth consistent with 1.1 |
|
84 |
SecurityManager security = System.getSecurityManager(); |
|
85 |
if (security != null) { |
|
86 |
security.checkCreateClassLoader(); |
|
87 |
} |
|
88 |
initialized = true; |
|
89 |
} |
|
90 |
||
91 |
/** |
|
92 |
* Creates a new SecureClassLoader using the default parent class |
|
93 |
* loader for delegation. |
|
94 |
* |
|
95 |
* <p>If there is a security manager, this method first |
|
18579
b678846778ad
8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents:
9275
diff
changeset
|
96 |
* calls the security manager's {@code checkCreateClassLoader} |
2 | 97 |
* method to ensure creation of a class loader is allowed. |
98 |
* |
|
99 |
* @exception SecurityException if a security manager exists and its |
|
18579
b678846778ad
8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents:
9275
diff
changeset
|
100 |
* {@code checkCreateClassLoader} method doesn't allow |
2 | 101 |
* creation of a class loader. |
102 |
* @see SecurityManager#checkCreateClassLoader |
|
103 |
*/ |
|
104 |
protected SecureClassLoader() { |
|
105 |
super(); |
|
106 |
// this is to make the stack depth consistent with 1.1 |
|
107 |
SecurityManager security = System.getSecurityManager(); |
|
108 |
if (security != null) { |
|
109 |
security.checkCreateClassLoader(); |
|
110 |
} |
|
111 |
initialized = true; |
|
112 |
} |
|
113 |
||
114 |
/** |
|
115 |
* Converts an array of bytes into an instance of class Class, |
|
116 |
* with an optional CodeSource. Before the |
|
117 |
* class can be used it must be resolved. |
|
118 |
* <p> |
|
119 |
* If a non-null CodeSource is supplied a ProtectionDomain is |
|
120 |
* constructed and associated with the class being defined. |
|
29492
a4bf9a570035
8028266: Tidy warnings cleanup for packages java.security/javax.security
avstepan
parents:
25859
diff
changeset
|
121 |
* |
18579
b678846778ad
8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents:
9275
diff
changeset
|
122 |
* @param name the expected name of the class, or {@code null} |
2 | 123 |
* if not known, using '.' and not '/' as the separator |
124 |
* and without a trailing ".class" suffix. |
|
125 |
* @param b the bytes that make up the class data. The bytes in |
|
18579
b678846778ad
8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents:
9275
diff
changeset
|
126 |
* positions {@code off} through {@code off+len-1} |
9266
121fb370f179
7032960: API files in java.awt need to be updated for references to JVM Spec with editions/hyperlinks
jjh
parents:
7970
diff
changeset
|
127 |
* should have the format of a valid class file as defined by |
121fb370f179
7032960: API files in java.awt need to be updated for references to JVM Spec with editions/hyperlinks
jjh
parents:
7970
diff
changeset
|
128 |
* <cite>The Java™ Virtual Machine Specification</cite>. |
18579
b678846778ad
8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents:
9275
diff
changeset
|
129 |
* @param off the start offset in {@code b} of the class data |
2 | 130 |
* @param len the length of the class data |
18579
b678846778ad
8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents:
9275
diff
changeset
|
131 |
* @param cs the associated CodeSource, or {@code null} if none |
b678846778ad
8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents:
9275
diff
changeset
|
132 |
* @return the {@code Class} object created from the data, |
2 | 133 |
* and optional CodeSource. |
134 |
* @exception ClassFormatError if the data did not contain a valid class |
|
18579
b678846778ad
8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents:
9275
diff
changeset
|
135 |
* @exception IndexOutOfBoundsException if either {@code off} or |
b678846778ad
8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents:
9275
diff
changeset
|
136 |
* {@code len} is negative, or if |
b678846778ad
8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents:
9275
diff
changeset
|
137 |
* {@code off+len} is greater than {@code b.length}. |
2 | 138 |
* |
139 |
* @exception SecurityException if an attempt is made to add this class |
|
140 |
* to a package that contains classes that were signed by |
|
141 |
* a different set of certificates than this class, or if |
|
142 |
* the class name begins with "java.". |
|
143 |
*/ |
|
144 |
protected final Class<?> defineClass(String name, |
|
145 |
byte[] b, int off, int len, |
|
146 |
CodeSource cs) |
|
147 |
{ |
|
2448
1e8128f3ff61
4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents:
2
diff
changeset
|
148 |
return defineClass(name, b, off, len, getProtectionDomain(cs)); |
2 | 149 |
} |
150 |
||
151 |
/** |
|
18579
b678846778ad
8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents:
9275
diff
changeset
|
152 |
* Converts a {@link java.nio.ByteBuffer ByteBuffer} |
b678846778ad
8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents:
9275
diff
changeset
|
153 |
* into an instance of class {@code Class}, with an optional CodeSource. |
2 | 154 |
* Before the class can be used it must be resolved. |
155 |
* <p> |
|
156 |
* If a non-null CodeSource is supplied a ProtectionDomain is |
|
157 |
* constructed and associated with the class being defined. |
|
29492
a4bf9a570035
8028266: Tidy warnings cleanup for packages java.security/javax.security
avstepan
parents:
25859
diff
changeset
|
158 |
* |
18579
b678846778ad
8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents:
9275
diff
changeset
|
159 |
* @param name the expected name of the class, or {@code null} |
2 | 160 |
* if not known, using '.' and not '/' as the separator |
161 |
* and without a trailing ".class" suffix. |
|
162 |
* @param b the bytes that make up the class data. The bytes from positions |
|
18579
b678846778ad
8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents:
9275
diff
changeset
|
163 |
* {@code b.position()} through {@code b.position() + b.limit() -1} |
9266
121fb370f179
7032960: API files in java.awt need to be updated for references to JVM Spec with editions/hyperlinks
jjh
parents:
7970
diff
changeset
|
164 |
* should have the format of a valid class file as defined by |
121fb370f179
7032960: API files in java.awt need to be updated for references to JVM Spec with editions/hyperlinks
jjh
parents:
7970
diff
changeset
|
165 |
* <cite>The Java™ Virtual Machine Specification</cite>. |
18579
b678846778ad
8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents:
9275
diff
changeset
|
166 |
* @param cs the associated CodeSource, or {@code null} if none |
b678846778ad
8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents:
9275
diff
changeset
|
167 |
* @return the {@code Class} object created from the data, |
2 | 168 |
* and optional CodeSource. |
169 |
* @exception ClassFormatError if the data did not contain a valid class |
|
170 |
* @exception SecurityException if an attempt is made to add this class |
|
171 |
* to a package that contains classes that were signed by |
|
172 |
* a different set of certificates than this class, or if |
|
173 |
* the class name begins with "java.". |
|
174 |
* |
|
175 |
* @since 1.5 |
|
176 |
*/ |
|
177 |
protected final Class<?> defineClass(String name, java.nio.ByteBuffer b, |
|
178 |
CodeSource cs) |
|
179 |
{ |
|
2448
1e8128f3ff61
4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents:
2
diff
changeset
|
180 |
return defineClass(name, b, getProtectionDomain(cs)); |
2 | 181 |
} |
182 |
||
183 |
/** |
|
184 |
* Returns the permissions for the given CodeSource object. |
|
185 |
* <p> |
|
186 |
* This method is invoked by the defineClass method which takes |
|
187 |
* a CodeSource as an argument when it is constructing the |
|
188 |
* ProtectionDomain for the class being defined. |
|
29492
a4bf9a570035
8028266: Tidy warnings cleanup for packages java.security/javax.security
avstepan
parents:
25859
diff
changeset
|
189 |
* |
2 | 190 |
* @param codesource the codesource. |
191 |
* |
|
192 |
* @return the permissions granted to the codesource. |
|
193 |
* |
|
194 |
*/ |
|
195 |
protected PermissionCollection getPermissions(CodeSource codesource) |
|
196 |
{ |
|
197 |
check(); |
|
198 |
return new Permissions(); // ProtectionDomain defers the binding |
|
199 |
} |
|
200 |
||
201 |
/* |
|
202 |
* Returned cached ProtectionDomain for the specified CodeSource. |
|
203 |
*/ |
|
204 |
private ProtectionDomain getProtectionDomain(CodeSource cs) { |
|
31141
ef5ddf021137
6826789: SecureClassLoader should not use CodeSource URLs as HashMap keys
mullan
parents:
29492
diff
changeset
|
205 |
if (cs == null) { |
2 | 206 |
return null; |
31141
ef5ddf021137
6826789: SecureClassLoader should not use CodeSource URLs as HashMap keys
mullan
parents:
29492
diff
changeset
|
207 |
} |
2 | 208 |
|
209 |
ProtectionDomain pd = null; |
|
210 |
synchronized (pdcache) { |
|
31141
ef5ddf021137
6826789: SecureClassLoader should not use CodeSource URLs as HashMap keys
mullan
parents:
29492
diff
changeset
|
211 |
// Use a String form of the URL as the key. It should behave in the |
ef5ddf021137
6826789: SecureClassLoader should not use CodeSource URLs as HashMap keys
mullan
parents:
29492
diff
changeset
|
212 |
// same manner as the URL when compared for equality except that no |
ef5ddf021137
6826789: SecureClassLoader should not use CodeSource URLs as HashMap keys
mullan
parents:
29492
diff
changeset
|
213 |
// nameservice lookup is done on the hostname (String comparison |
ef5ddf021137
6826789: SecureClassLoader should not use CodeSource URLs as HashMap keys
mullan
parents:
29492
diff
changeset
|
214 |
// only), and the fragment is not considered. |
ef5ddf021137
6826789: SecureClassLoader should not use CodeSource URLs as HashMap keys
mullan
parents:
29492
diff
changeset
|
215 |
String key = cs.getLocationNoFragString(); |
ef5ddf021137
6826789: SecureClassLoader should not use CodeSource URLs as HashMap keys
mullan
parents:
29492
diff
changeset
|
216 |
pd = pdcache.get(key); |
2 | 217 |
if (pd == null) { |
218 |
PermissionCollection perms = getPermissions(cs); |
|
219 |
pd = new ProtectionDomain(cs, perms, this, null); |
|
31141
ef5ddf021137
6826789: SecureClassLoader should not use CodeSource URLs as HashMap keys
mullan
parents:
29492
diff
changeset
|
220 |
pdcache.put(key, pd); |
2448
1e8128f3ff61
4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents:
2
diff
changeset
|
221 |
if (debug != null) { |
1e8128f3ff61
4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents:
2
diff
changeset
|
222 |
debug.println(" getPermissions "+ pd); |
1e8128f3ff61
4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents:
2
diff
changeset
|
223 |
debug.println(""); |
2 | 224 |
} |
225 |
} |
|
226 |
} |
|
227 |
return pd; |
|
228 |
} |
|
229 |
||
230 |
/* |
|
231 |
* Check to make sure the class loader has been initialized. |
|
232 |
*/ |
|
233 |
private void check() { |
|
234 |
if (!initialized) { |
|
235 |
throw new SecurityException("ClassLoader object not initialized"); |
|
236 |
} |
|
237 |
} |
|
238 |
||
239 |
} |