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