author | jjg |
Mon, 15 Aug 2011 11:48:20 -0700 | |
changeset 10336 | 0bb1999251f8 |
parent 9035 | 1255eb81cc2f |
permissions | -rw-r--r-- |
2 | 1 |
/* |
9035
1255eb81cc2f
7033660: Update copyright year to 2011 on any files changed in 2011
ohair
parents:
7973
diff
changeset
|
2 |
* Copyright (c) 2003, 2011, 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 sun.security.jca; |
|
27 |
||
28 |
import java.security.Provider; |
|
29 |
||
30 |
/** |
|
31 |
* Collection of methods to get and set provider list. Also includes |
|
32 |
* special code for the provider list during JAR verification. |
|
33 |
* |
|
34 |
* @author Andreas Sterbenz |
|
35 |
* @since 1.5 |
|
36 |
*/ |
|
37 |
public class Providers { |
|
38 |
||
39 |
private static final ThreadLocal<ProviderList> threadLists = |
|
7973
dffe8439eb20
7005608: diamond conversion of JCA and crypto providers
smarks
parents:
7668
diff
changeset
|
40 |
new InheritableThreadLocal<>(); |
2 | 41 |
|
42 |
// number of threads currently using thread-local provider lists |
|
43 |
// tracked to allow an optimization if == 0 |
|
44 |
private static volatile int threadListsUsed; |
|
45 |
||
46 |
// current system-wide provider list |
|
47 |
// Note volatile immutable object, so no synchronization needed. |
|
48 |
private static volatile ProviderList providerList; |
|
49 |
||
50 |
static { |
|
51 |
// set providerList to empty list first in case initialization somehow |
|
52 |
// triggers a getInstance() call (although that should not happen) |
|
53 |
providerList = ProviderList.EMPTY; |
|
54 |
providerList = ProviderList.fromSecurityProperties(); |
|
55 |
} |
|
56 |
||
57 |
private Providers() { |
|
58 |
// empty |
|
59 |
} |
|
60 |
||
61 |
// we need special handling to resolve circularities when loading |
|
62 |
// signed JAR files during startup. The code below is part of that. |
|
63 |
||
64 |
// Basically, before we load data from a signed JAR file, we parse |
|
65 |
// the PKCS#7 file and verify the signature. We need a |
|
66 |
// CertificateFactory, Signatures, etc. to do that. We have to make |
|
67 |
// sure that we do not try to load the implementation from the JAR |
|
68 |
// file we are just verifying. |
|
69 |
// |
|
70 |
// To avoid that, we use different provider settings during JAR |
|
71 |
// verification. However, we do not want those provider settings to |
|
72 |
// interfere with other parts of the system. Therefore, we make them local |
|
73 |
// to the Thread executing the JAR verification code. |
|
74 |
// |
|
75 |
// The code here is used by sun.security.util.SignatureFileVerifier. |
|
76 |
// See there for details. |
|
77 |
||
78 |
private static final String BACKUP_PROVIDER_CLASSNAME = |
|
79 |
"sun.security.provider.VerificationProvider"; |
|
80 |
||
81 |
// Hardcoded classnames of providers to use for JAR verification. |
|
82 |
// MUST NOT be on the bootclasspath and not in signed JAR files. |
|
83 |
private static final String[] jarVerificationProviders = { |
|
84 |
"sun.security.provider.Sun", |
|
85 |
"sun.security.rsa.SunRsaSign", |
|
4152 | 86 |
// Note: SunEC *is* in a signed JAR file, but it's not signed |
87 |
// by EC itself. So it's still safe to be listed here. |
|
88 |
"sun.security.ec.SunEC", |
|
2 | 89 |
BACKUP_PROVIDER_CLASSNAME, |
90 |
}; |
|
91 |
||
92 |
// Return to Sun provider or its backup. |
|
93 |
// This method should only be called by |
|
94 |
// sun.security.util.ManifestEntryVerifier and java.security.SecureRandom. |
|
95 |
public static Provider getSunProvider() { |
|
96 |
try { |
|
10336
0bb1999251f8
7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents:
9035
diff
changeset
|
97 |
Class<?> clazz = Class.forName(jarVerificationProviders[0]); |
2 | 98 |
return (Provider)clazz.newInstance(); |
99 |
} catch (Exception e) { |
|
100 |
try { |
|
10336
0bb1999251f8
7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents:
9035
diff
changeset
|
101 |
Class<?> clazz = Class.forName(BACKUP_PROVIDER_CLASSNAME); |
2 | 102 |
return (Provider)clazz.newInstance(); |
103 |
} catch (Exception ee) { |
|
104 |
throw new RuntimeException("Sun provider not found", e); |
|
105 |
} |
|
106 |
} |
|
107 |
} |
|
108 |
||
109 |
/** |
|
110 |
* Start JAR verification. This sets a special provider list for |
|
111 |
* the current thread. You MUST save the return value from this |
|
112 |
* method and you MUST call stopJarVerification() with that object |
|
113 |
* once you are done. |
|
114 |
*/ |
|
115 |
public static Object startJarVerification() { |
|
116 |
ProviderList currentList = getProviderList(); |
|
117 |
ProviderList jarList = currentList.getJarList(jarVerificationProviders); |
|
118 |
// return the old thread-local provider list, usually null |
|
119 |
return beginThreadProviderList(jarList); |
|
120 |
} |
|
121 |
||
122 |
/** |
|
123 |
* Stop JAR verification. Call once you have completed JAR verification. |
|
124 |
*/ |
|
125 |
public static void stopJarVerification(Object obj) { |
|
126 |
// restore old thread-local provider list |
|
127 |
endThreadProviderList((ProviderList)obj); |
|
128 |
} |
|
129 |
||
130 |
/** |
|
131 |
* Return the current ProviderList. If the thread-local list is set, |
|
132 |
* it is returned. Otherwise, the system wide list is returned. |
|
133 |
*/ |
|
134 |
public static ProviderList getProviderList() { |
|
135 |
ProviderList list = getThreadProviderList(); |
|
136 |
if (list == null) { |
|
137 |
list = getSystemProviderList(); |
|
138 |
} |
|
139 |
return list; |
|
140 |
} |
|
141 |
||
142 |
/** |
|
143 |
* Set the current ProviderList. Affects the thread-local list if set, |
|
144 |
* otherwise the system wide list. |
|
145 |
*/ |
|
146 |
public static void setProviderList(ProviderList newList) { |
|
147 |
if (getThreadProviderList() == null) { |
|
148 |
setSystemProviderList(newList); |
|
149 |
} else { |
|
150 |
changeThreadProviderList(newList); |
|
151 |
} |
|
152 |
} |
|
153 |
||
154 |
/** |
|
155 |
* Get the full provider list with invalid providers (those that |
|
156 |
* could not be loaded) removed. This is the list we need to |
|
157 |
* present to applications. |
|
158 |
*/ |
|
6895
92894a4882e0
6850402: Deadlock on sun.security.jca.ProviderConfig starting from jdk7-b55
valeriep
parents:
5506
diff
changeset
|
159 |
public static ProviderList getFullProviderList() { |
92894a4882e0
6850402: Deadlock on sun.security.jca.ProviderConfig starting from jdk7-b55
valeriep
parents:
5506
diff
changeset
|
160 |
ProviderList list; |
92894a4882e0
6850402: Deadlock on sun.security.jca.ProviderConfig starting from jdk7-b55
valeriep
parents:
5506
diff
changeset
|
161 |
synchronized (Providers.class) { |
92894a4882e0
6850402: Deadlock on sun.security.jca.ProviderConfig starting from jdk7-b55
valeriep
parents:
5506
diff
changeset
|
162 |
list = getThreadProviderList(); |
92894a4882e0
6850402: Deadlock on sun.security.jca.ProviderConfig starting from jdk7-b55
valeriep
parents:
5506
diff
changeset
|
163 |
if (list != null) { |
92894a4882e0
6850402: Deadlock on sun.security.jca.ProviderConfig starting from jdk7-b55
valeriep
parents:
5506
diff
changeset
|
164 |
ProviderList newList = list.removeInvalid(); |
92894a4882e0
6850402: Deadlock on sun.security.jca.ProviderConfig starting from jdk7-b55
valeriep
parents:
5506
diff
changeset
|
165 |
if (newList != list) { |
92894a4882e0
6850402: Deadlock on sun.security.jca.ProviderConfig starting from jdk7-b55
valeriep
parents:
5506
diff
changeset
|
166 |
changeThreadProviderList(newList); |
92894a4882e0
6850402: Deadlock on sun.security.jca.ProviderConfig starting from jdk7-b55
valeriep
parents:
5506
diff
changeset
|
167 |
list = newList; |
92894a4882e0
6850402: Deadlock on sun.security.jca.ProviderConfig starting from jdk7-b55
valeriep
parents:
5506
diff
changeset
|
168 |
} |
92894a4882e0
6850402: Deadlock on sun.security.jca.ProviderConfig starting from jdk7-b55
valeriep
parents:
5506
diff
changeset
|
169 |
return list; |
2 | 170 |
} |
171 |
} |
|
172 |
list = getSystemProviderList(); |
|
173 |
ProviderList newList = list.removeInvalid(); |
|
174 |
if (newList != list) { |
|
175 |
setSystemProviderList(newList); |
|
176 |
list = newList; |
|
177 |
} |
|
178 |
return list; |
|
179 |
} |
|
180 |
||
181 |
private static ProviderList getSystemProviderList() { |
|
182 |
return providerList; |
|
183 |
} |
|
184 |
||
185 |
private static void setSystemProviderList(ProviderList list) { |
|
186 |
providerList = list; |
|
187 |
} |
|
188 |
||
189 |
public static ProviderList getThreadProviderList() { |
|
190 |
// avoid accessing the threadlocal if none are currently in use |
|
191 |
// (first use of ThreadLocal.get() for a Thread allocates a Map) |
|
192 |
if (threadListsUsed == 0) { |
|
193 |
return null; |
|
194 |
} |
|
195 |
return threadLists.get(); |
|
196 |
} |
|
197 |
||
198 |
// Change the thread local provider list. Use only if the current thread |
|
199 |
// is already using a thread local list and you want to change it in place. |
|
200 |
// In other cases, use the begin/endThreadProviderList() methods. |
|
201 |
private static void changeThreadProviderList(ProviderList list) { |
|
202 |
threadLists.set(list); |
|
203 |
} |
|
204 |
||
205 |
/** |
|
206 |
* Methods to manipulate the thread local provider list. It is for use by |
|
207 |
* JAR verification (see above) and the SunJSSE FIPS mode only. |
|
208 |
* |
|
209 |
* It should be used as follows: |
|
210 |
* |
|
211 |
* ProviderList list = ...; |
|
212 |
* ProviderList oldList = Providers.beginThreadProviderList(list); |
|
213 |
* try { |
|
214 |
* // code that needs thread local provider list |
|
215 |
* } finally { |
|
216 |
* Providers.endThreadProviderList(oldList); |
|
217 |
* } |
|
218 |
* |
|
219 |
*/ |
|
220 |
||
221 |
public static synchronized ProviderList beginThreadProviderList(ProviderList list) { |
|
222 |
if (ProviderList.debug != null) { |
|
223 |
ProviderList.debug.println("ThreadLocal providers: " + list); |
|
224 |
} |
|
225 |
ProviderList oldList = threadLists.get(); |
|
226 |
threadListsUsed++; |
|
227 |
threadLists.set(list); |
|
228 |
return oldList; |
|
229 |
} |
|
230 |
||
231 |
public static synchronized void endThreadProviderList(ProviderList list) { |
|
232 |
if (list == null) { |
|
233 |
if (ProviderList.debug != null) { |
|
234 |
ProviderList.debug.println("Disabling ThreadLocal providers"); |
|
235 |
} |
|
236 |
threadLists.remove(); |
|
237 |
} else { |
|
238 |
if (ProviderList.debug != null) { |
|
239 |
ProviderList.debug.println |
|
240 |
("Restoring previous ThreadLocal providers: " + list); |
|
241 |
} |
|
242 |
threadLists.set(list); |
|
243 |
} |
|
244 |
threadListsUsed--; |
|
245 |
} |
|
246 |
||
247 |
} |