Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/src/java.base/share/classes/sun/security/jca/Providers.java
41159 views
1
/*
2
* Copyright (c) 2003, 2021, Oracle and/or its affiliates. All rights reserved.
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
7
* published by the Free Software Foundation. Oracle designates this
8
* particular file as subject to the "Classpath" exception as provided
9
* by Oracle in the LICENSE file that accompanied this code.
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
*
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.
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 =
40
new ThreadLocal<>();
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
// After the switch to modules, JDK providers are all in modules and JDK
62
// no longer needs to load signed jars during start up.
63
//
64
// However, for earlier releases, it need special handling to resolve
65
// circularities when loading signed JAR files during startup. The code
66
// below is part of that.
67
//
68
// Basically, before we load data from a signed JAR file, we parse
69
// the PKCS#7 file and verify the signature. We need a
70
// CertificateFactory, Signatures, etc. to do that. We have to make
71
// sure that we do not try to load the implementation from the JAR
72
// file we are just verifying.
73
//
74
// To avoid that, we use different provider settings during JAR
75
// verification. However, we do not want those provider settings to
76
// interfere with other parts of the system. Therefore, we make them local
77
// to the Thread executing the JAR verification code.
78
//
79
// The code here is used by sun.security.util.SignatureFileVerifier.
80
// See there for details.
81
82
// Hardcoded names of providers to use for JAR verification.
83
// MUST NOT be on the bootclasspath and not in signed JAR files.
84
private static final String[] jarVerificationProviders = {
85
"SUN",
86
"SunRsaSign",
87
// Note: when SunEC is in a signed JAR file, it's not signed
88
// by EC algorithms. So it's still safe to be listed here.
89
"SunEC",
90
"SunJCE",
91
};
92
93
// Return Sun provider.
94
// This method should only be called by
95
// sun.security.util.ManifestEntryVerifier and java.security.SecureRandom.
96
public static Provider getSunProvider() {
97
return new sun.security.provider.Sun();
98
}
99
100
/**
101
* Start JAR verification. This sets a special provider list for
102
* the current thread. You MUST save the return value from this
103
* method and you MUST call stopJarVerification() with that object
104
* once you are done.
105
*/
106
public static Object startJarVerification() {
107
ProviderList currentList = getProviderList();
108
ProviderList jarList = currentList.getJarList(jarVerificationProviders);
109
if (jarList.getProvider("SUN") == null) {
110
// add backup provider
111
Provider p;
112
try {
113
p = new sun.security.provider.VerificationProvider();
114
} catch (Exception e) {
115
throw new RuntimeException("Missing provider for jar verification", e);
116
}
117
ProviderList.add(jarList, p);
118
}
119
// return the old thread-local provider list, usually null
120
return beginThreadProviderList(jarList);
121
}
122
123
/**
124
* Stop JAR verification. Call once you have completed JAR verification.
125
*/
126
public static void stopJarVerification(Object obj) {
127
// restore old thread-local provider list
128
endThreadProviderList((ProviderList)obj);
129
}
130
131
/**
132
* Return the current ProviderList. If the thread-local list is set,
133
* it is returned. Otherwise, the system wide list is returned.
134
*/
135
public static ProviderList getProviderList() {
136
ProviderList list = getThreadProviderList();
137
if (list == null) {
138
list = getSystemProviderList();
139
}
140
return list;
141
}
142
143
/**
144
* Set the current ProviderList. Affects the thread-local list if set,
145
* otherwise the system wide list.
146
*/
147
public static void setProviderList(ProviderList newList) {
148
if (getThreadProviderList() == null) {
149
setSystemProviderList(newList);
150
} else {
151
changeThreadProviderList(newList);
152
}
153
JCAUtil.clearDefSecureRandom();
154
}
155
156
/**
157
* Get the full provider list with invalid providers (those that
158
* could not be loaded) removed. This is the list we need to
159
* present to applications.
160
*/
161
public static ProviderList getFullProviderList() {
162
ProviderList list;
163
synchronized (Providers.class) {
164
list = getThreadProviderList();
165
if (list != null) {
166
ProviderList newList = list.removeInvalid();
167
if (newList != list) {
168
changeThreadProviderList(newList);
169
list = newList;
170
}
171
return list;
172
}
173
}
174
list = getSystemProviderList();
175
ProviderList newList = list.removeInvalid();
176
if (newList != list) {
177
setSystemProviderList(newList);
178
list = newList;
179
}
180
return list;
181
}
182
183
private static ProviderList getSystemProviderList() {
184
return providerList;
185
}
186
187
private static void setSystemProviderList(ProviderList list) {
188
providerList = list;
189
}
190
191
public static ProviderList getThreadProviderList() {
192
// avoid accessing the threadlocal if none are currently in use
193
// (first use of ThreadLocal.get() for a Thread allocates a Map)
194
if (threadListsUsed == 0) {
195
return null;
196
}
197
return threadLists.get();
198
}
199
200
// Change the thread local provider list. Use only if the current thread
201
// is already using a thread local list and you want to change it in place.
202
// In other cases, use the begin/endThreadProviderList() methods.
203
private static void changeThreadProviderList(ProviderList list) {
204
threadLists.set(list);
205
}
206
207
/**
208
* Methods to manipulate the thread local provider list. It is for use by
209
* JAR verification (see above).
210
*
211
* It should be used as follows:
212
*
213
* ProviderList list = ...;
214
* ProviderList oldList = Providers.beginThreadProviderList(list);
215
* try {
216
* // code that needs thread local provider list
217
* } finally {
218
* Providers.endThreadProviderList(oldList);
219
* }
220
*
221
*/
222
223
public static synchronized ProviderList beginThreadProviderList(ProviderList list) {
224
if (ProviderList.debug != null) {
225
ProviderList.debug.println("ThreadLocal providers: " + list);
226
}
227
ProviderList oldList = threadLists.get();
228
threadListsUsed++;
229
threadLists.set(list);
230
return oldList;
231
}
232
233
public static synchronized void endThreadProviderList(ProviderList list) {
234
if (list == null) {
235
if (ProviderList.debug != null) {
236
ProviderList.debug.println("Disabling ThreadLocal providers");
237
}
238
threadLists.remove();
239
} else {
240
if (ProviderList.debug != null) {
241
ProviderList.debug.println
242
("Restoring previous ThreadLocal providers: " + list);
243
}
244
threadLists.set(list);
245
}
246
threadListsUsed--;
247
}
248
249
}
250
251