Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/src/java.base/share/classes/javax/net/ssl/TrustManagerFactory.java
41159 views
1
/*
2
* Copyright (c) 1999, 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 javax.net.ssl;
27
28
import java.security.Security;
29
import java.security.*;
30
import java.util.Objects;
31
32
import sun.security.jca.GetInstance;
33
34
/**
35
* This class acts as a factory for trust managers based on a
36
* source of trust material. Each trust manager manages a specific
37
* type of trust material for use by secure sockets. The trust
38
* material is based on a KeyStore and/or provider-specific sources.
39
*
40
* <p> Every implementation of the Java platform is required to support the
41
* following standard {@code TrustManagerFactory} algorithm:
42
* <ul>
43
* <li>{@code PKIX}</li>
44
* </ul>
45
* This algorithm is described in the <a href=
46
* "{@docRoot}/../specs/security/standard-names.html#trustmanagerfactory-algorithms">
47
* TrustManagerFactory section</a> of the
48
* Java Security Standard Algorithm Names Specification.
49
* Consult the release documentation for your implementation to see if any
50
* other algorithms are supported.
51
*
52
* @since 1.4
53
* @see TrustManager
54
*/
55
public class TrustManagerFactory {
56
// The provider
57
private Provider provider;
58
59
// The provider implementation (delegate)
60
private TrustManagerFactorySpi factorySpi;
61
62
// The name of the trust management algorithm.
63
private String algorithm;
64
65
/**
66
* Obtains the default TrustManagerFactory algorithm name.
67
*
68
* <p>The default TrustManager can be changed at runtime by setting
69
* the value of the {@code ssl.TrustManagerFactory.algorithm}
70
* security property to the desired algorithm name.
71
*
72
* @see java.security.Security security properties
73
* @return the default algorithm name as specified by the
74
* {@code ssl.TrustManagerFactory.algorithm} security property, or an
75
* implementation-specific default if no such property exists.
76
*/
77
@SuppressWarnings("removal")
78
public static final String getDefaultAlgorithm() {
79
String type;
80
type = AccessController.doPrivileged(new PrivilegedAction<>() {
81
@Override
82
public String run() {
83
return Security.getProperty(
84
"ssl.TrustManagerFactory.algorithm");
85
}
86
});
87
if (type == null) {
88
type = "SunX509";
89
}
90
return type;
91
}
92
93
/**
94
* Creates a TrustManagerFactory object.
95
*
96
* @param factorySpi the delegate
97
* @param provider the provider
98
* @param algorithm the algorithm
99
*/
100
protected TrustManagerFactory(TrustManagerFactorySpi factorySpi,
101
Provider provider, String algorithm) {
102
this.factorySpi = factorySpi;
103
this.provider = provider;
104
this.algorithm = algorithm;
105
}
106
107
/**
108
* Returns the algorithm name of this <code>TrustManagerFactory</code>
109
* object.
110
*
111
* <p>This is the same name that was specified in one of the
112
* <code>getInstance</code> calls that created this
113
* <code>TrustManagerFactory</code> object.
114
*
115
* @return the algorithm name of this <code>TrustManagerFactory</code>
116
* object
117
*/
118
public final String getAlgorithm() {
119
return this.algorithm;
120
}
121
122
/**
123
* Returns a <code>TrustManagerFactory</code> object that acts as a
124
* factory for trust managers.
125
*
126
* <p> This method traverses the list of registered security Providers,
127
* starting with the most preferred Provider.
128
* A new TrustManagerFactory object encapsulating the
129
* TrustManagerFactorySpi implementation from the first
130
* Provider that supports the specified algorithm is returned.
131
*
132
* <p> Note that the list of registered providers may be retrieved via
133
* the {@link Security#getProviders() Security.getProviders()} method.
134
*
135
* @implNote
136
* The JDK Reference Implementation additionally uses the
137
* {@code jdk.security.provider.preferred}
138
* {@link Security#getProperty(String) Security} property to determine
139
* the preferred provider order for the specified algorithm. This
140
* may be different than the order of providers returned by
141
* {@link Security#getProviders() Security.getProviders()}.
142
*
143
* @param algorithm the standard name of the requested trust management
144
* algorithm. See the <a href=
145
* "{@docRoot}/../specs/security/standard-names.html#trustmanagerfactory-algorithms">
146
* TrustManagerFactory section</a> in the Java Security Standard
147
* Algorithm Names Specification for information about standard
148
* algorithm names.
149
*
150
* @return the new {@code TrustManagerFactory} object
151
*
152
* @throws NoSuchAlgorithmException if no {@code Provider} supports a
153
* {@code TrustManagerFactorySpi} implementation for the
154
* specified algorithm
155
*
156
* @throws NullPointerException if {@code algorithm} is {@code null}
157
*
158
* @see java.security.Provider
159
*/
160
public static final TrustManagerFactory getInstance(String algorithm)
161
throws NoSuchAlgorithmException {
162
Objects.requireNonNull(algorithm, "null algorithm name");
163
GetInstance.Instance instance = GetInstance.getInstance
164
("TrustManagerFactory", TrustManagerFactorySpi.class,
165
algorithm);
166
return new TrustManagerFactory((TrustManagerFactorySpi)instance.impl,
167
instance.provider, algorithm);
168
}
169
170
/**
171
* Returns a <code>TrustManagerFactory</code> object that acts as a
172
* factory for trust managers.
173
*
174
* <p> A new KeyManagerFactory object encapsulating the
175
* KeyManagerFactorySpi implementation from the specified provider
176
* is returned. The specified provider must be registered
177
* in the security provider list.
178
*
179
* <p> Note that the list of registered providers may be retrieved via
180
* the {@link Security#getProviders() Security.getProviders()} method.
181
*
182
* @param algorithm the standard name of the requested trust management
183
* algorithm. See the <a href=
184
* "{@docRoot}/../specs/security/standard-names.html#trustmanagerfactory-algorithms">
185
* TrustManagerFactory section</a> in the Java Security Standard
186
* Algorithm Names Specification for information about standard
187
* algorithm names.
188
*
189
* @param provider the name of the provider.
190
*
191
* @return the new {@code TrustManagerFactory} object
192
*
193
* @throws IllegalArgumentException if the provider name is
194
* {@code null} or empty
195
*
196
* @throws NoSuchAlgorithmException if a {@code TrustManagerFactorySpi}
197
* implementation for the specified algorithm is not
198
* available from the specified provider
199
*
200
* @throws NoSuchProviderException if the specified provider is not
201
* registered in the security provider list
202
*
203
* @throws NullPointerException if {@code algorithm} is {@code null}
204
*
205
* @see java.security.Provider
206
*/
207
public static final TrustManagerFactory getInstance(String algorithm,
208
String provider) throws NoSuchAlgorithmException,
209
NoSuchProviderException {
210
Objects.requireNonNull(algorithm, "null algorithm name");
211
GetInstance.Instance instance = GetInstance.getInstance
212
("TrustManagerFactory", TrustManagerFactorySpi.class,
213
algorithm, provider);
214
return new TrustManagerFactory((TrustManagerFactorySpi)instance.impl,
215
instance.provider, algorithm);
216
}
217
218
/**
219
* Returns a <code>TrustManagerFactory</code> object that acts as a
220
* factory for trust managers.
221
*
222
* <p> A new TrustManagerFactory object encapsulating the
223
* TrustManagerFactorySpi implementation from the specified Provider
224
* object is returned. Note that the specified Provider object
225
* does not have to be registered in the provider list.
226
*
227
* @param algorithm the standard name of the requested trust management
228
* algorithm. See the <a href=
229
* "{@docRoot}/../specs/security/standard-names.html#trustmanagerfactory-algorithms">
230
* TrustManagerFactory section</a> in the Java Security Standard
231
* Algorithm Names Specification for information about standard
232
* algorithm names.
233
*
234
* @param provider an instance of the provider.
235
*
236
* @return the new {@code TrustManagerFactory} object
237
*
238
* @throws IllegalArgumentException if the provider is {@code null}
239
*
240
* @throws NoSuchAlgorithmException if a {@code TrustManagerFactorySpi}
241
* implementation for the specified algorithm is not available
242
* from the specified {@code Provider} object
243
*
244
* @throws NullPointerException if {@code algorithm} is {@code null}
245
*
246
* @see java.security.Provider
247
*/
248
public static final TrustManagerFactory getInstance(String algorithm,
249
Provider provider) throws NoSuchAlgorithmException {
250
Objects.requireNonNull(algorithm, "null algorithm name");
251
GetInstance.Instance instance = GetInstance.getInstance
252
("TrustManagerFactory", TrustManagerFactorySpi.class,
253
algorithm, provider);
254
return new TrustManagerFactory((TrustManagerFactorySpi)instance.impl,
255
instance.provider, algorithm);
256
}
257
258
/**
259
* Returns the provider of this <code>TrustManagerFactory</code> object.
260
*
261
* @return the provider of this <code>TrustManagerFactory</code> object
262
*/
263
public final Provider getProvider() {
264
return this.provider;
265
}
266
267
268
/**
269
* Initializes this factory with a source of certificate
270
* authorities and related trust material.
271
* <P>
272
* The provider typically uses a KeyStore as a basis for making
273
* trust decisions.
274
* <P>
275
* For more flexible initialization, please see
276
* {@link #init(ManagerFactoryParameters)}.
277
*
278
* @param ks the key store, or null
279
* @throws KeyStoreException if this operation fails
280
*/
281
public final void init(KeyStore ks) throws KeyStoreException {
282
factorySpi.engineInit(ks);
283
}
284
285
286
/**
287
* Initializes this factory with a source of provider-specific
288
* trust material.
289
* <P>
290
* In some cases, initialization parameters other than a keystore
291
* may be needed by a provider. Users of that particular provider
292
* are expected to pass an implementation of the appropriate
293
* <CODE>ManagerFactoryParameters</CODE> as defined by the
294
* provider. The provider can then call the specified methods in
295
* the <CODE>ManagerFactoryParameters</CODE> implementation to obtain the
296
* needed information.
297
*
298
* @param spec an implementation of a provider-specific parameter
299
* specification
300
* @throws InvalidAlgorithmParameterException if an error is
301
* encountered
302
*/
303
public final void init(ManagerFactoryParameters spec) throws
304
InvalidAlgorithmParameterException {
305
factorySpi.engineInit(spec);
306
}
307
308
309
/**
310
* Returns one trust manager for each type of trust material.
311
*
312
* @throws IllegalStateException if the factory is not initialized.
313
*
314
* @return the trust managers
315
*/
316
public final TrustManager[] getTrustManagers() {
317
return factorySpi.engineGetTrustManagers();
318
}
319
}
320
321