author | mullan |
Mon, 24 Jun 2019 10:11:17 -0400 | |
changeset 55483 | 00c08fae63e8 |
parent 47216 | 71c04702a3d5 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
55483
00c08fae63e8
8180005: Provide specific links in KeyManagerFactory and TrustManagerFactory to the Standard Algorithm Names Specification
mullan
parents:
47216
diff
changeset
|
2 |
* Copyright (c) 1999, 2019, 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 javax.net.ssl; |
|
27 |
||
28 |
import java.security.Security; |
|
29 |
import java.security.*; |
|
41826
b35ee9b35b09
4985694: Incomplete spec for most of the getInstances
wetmore
parents:
37348
diff
changeset
|
30 |
import java.util.Objects; |
2 | 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 |
|
14775
2ed01c760aea
8004064: Downgrade normative references to ${java.home}/lib/security/java.security
mullan
parents:
14514
diff
changeset
|
57 |
* the value of the {@code ssl.KeyManagerFactory.algorithm} |
2ed01c760aea
8004064: Downgrade normative references to ${java.home}/lib/security/java.security
mullan
parents:
14514
diff
changeset
|
58 |
* security property to the desired algorithm name. |
2 | 59 |
* |
14775
2ed01c760aea
8004064: Downgrade normative references to ${java.home}/lib/security/java.security
mullan
parents:
14514
diff
changeset
|
60 |
* @see java.security.Security security properties |
2ed01c760aea
8004064: Downgrade normative references to ${java.home}/lib/security/java.security
mullan
parents:
14514
diff
changeset
|
61 |
* @return the default algorithm name as specified by the |
2ed01c760aea
8004064: Downgrade normative references to ${java.home}/lib/security/java.security
mullan
parents:
14514
diff
changeset
|
62 |
* {@code ssl.KeyManagerFactory.algorithm} security property, or an |
2ed01c760aea
8004064: Downgrade normative references to ${java.home}/lib/security/java.security
mullan
parents:
14514
diff
changeset
|
63 |
* implementation-specific default if no such property exists. |
2 | 64 |
*/ |
32649
2ee9017c7597
8136583: Core libraries should use blessed modifier order
martin
parents:
30033
diff
changeset
|
65 |
public static final String getDefaultAlgorithm() { |
2 | 66 |
String type; |
30033
b9c86c17164a
8078468: Update security libraries to use diamond with anonymous classes
darcy
parents:
29492
diff
changeset
|
67 |
type = AccessController.doPrivileged(new PrivilegedAction<>() { |
14514 | 68 |
@Override |
2 | 69 |
public String run() { |
70 |
return Security.getProperty( |
|
71 |
"ssl.KeyManagerFactory.algorithm"); |
|
72 |
} |
|
73 |
}); |
|
74 |
if (type == null) { |
|
75 |
type = "SunX509"; |
|
76 |
} |
|
77 |
return type; |
|
78 |
} |
|
79 |
||
80 |
/** |
|
81 |
* Creates a KeyManagerFactory object. |
|
82 |
* |
|
83 |
* @param factorySpi the delegate |
|
84 |
* @param provider the provider |
|
85 |
* @param algorithm the algorithm |
|
86 |
*/ |
|
87 |
protected KeyManagerFactory(KeyManagerFactorySpi factorySpi, |
|
88 |
Provider provider, String algorithm) { |
|
89 |
this.factorySpi = factorySpi; |
|
90 |
this.provider = provider; |
|
91 |
this.algorithm = algorithm; |
|
92 |
} |
|
93 |
||
94 |
/** |
|
95 |
* Returns the algorithm name of this <code>KeyManagerFactory</code> object. |
|
96 |
* |
|
97 |
* <p>This is the same name that was specified in one of the |
|
98 |
* <code>getInstance</code> calls that created this |
|
99 |
* <code>KeyManagerFactory</code> object. |
|
100 |
* |
|
101 |
* @return the algorithm name of this <code>KeyManagerFactory</code> object. |
|
102 |
*/ |
|
103 |
public final String getAlgorithm() { |
|
104 |
return this.algorithm; |
|
105 |
} |
|
106 |
||
107 |
/** |
|
108 |
* Returns a <code>KeyManagerFactory</code> object that acts as a |
|
109 |
* factory for key managers. |
|
110 |
* |
|
111 |
* <p> This method traverses the list of registered security Providers, |
|
112 |
* starting with the most preferred Provider. |
|
113 |
* A new KeyManagerFactory object encapsulating the |
|
114 |
* KeyManagerFactorySpi implementation from the first |
|
115 |
* Provider that supports the specified algorithm is returned. |
|
116 |
* |
|
117 |
* <p> Note that the list of registered providers may be retrieved via |
|
118 |
* the {@link Security#getProviders() Security.getProviders()} method. |
|
119 |
* |
|
33241
27eb2d6abda9
8133151: Preferred provider configuration for JCE
ascarpino
parents:
32649
diff
changeset
|
120 |
* @implNote |
27eb2d6abda9
8133151: Preferred provider configuration for JCE
ascarpino
parents:
32649
diff
changeset
|
121 |
* The JDK Reference Implementation additionally uses the |
37348
9ccec3170d5e
8152205: jdk.security.provider.preferred is ambiguously documented
ascarpino
parents:
33241
diff
changeset
|
122 |
* {@code jdk.security.provider.preferred} |
9ccec3170d5e
8152205: jdk.security.provider.preferred is ambiguously documented
ascarpino
parents:
33241
diff
changeset
|
123 |
* {@link Security#getProperty(String) Security} property to determine |
33241
27eb2d6abda9
8133151: Preferred provider configuration for JCE
ascarpino
parents:
32649
diff
changeset
|
124 |
* the preferred provider order for the specified algorithm. This |
27eb2d6abda9
8133151: Preferred provider configuration for JCE
ascarpino
parents:
32649
diff
changeset
|
125 |
* may be different than the order of providers returned by |
27eb2d6abda9
8133151: Preferred provider configuration for JCE
ascarpino
parents:
32649
diff
changeset
|
126 |
* {@link Security#getProviders() Security.getProviders()}. |
27eb2d6abda9
8133151: Preferred provider configuration for JCE
ascarpino
parents:
32649
diff
changeset
|
127 |
* |
2 | 128 |
* @param algorithm the standard name of the requested algorithm. |
129 |
* See the <a href= |
|
55483
00c08fae63e8
8180005: Provide specific links in KeyManagerFactory and TrustManagerFactory to the Standard Algorithm Names Specification
mullan
parents:
47216
diff
changeset
|
130 |
* "{@docRoot}/../specs/security/standard-names.html#keymanagerfactory-algorithms"> |
00c08fae63e8
8180005: Provide specific links in KeyManagerFactory and TrustManagerFactory to the Standard Algorithm Names Specification
mullan
parents:
47216
diff
changeset
|
131 |
* KeyManagerFactory section</a> in the Java Security Standard |
00c08fae63e8
8180005: Provide specific links in KeyManagerFactory and TrustManagerFactory to the Standard Algorithm Names Specification
mullan
parents:
47216
diff
changeset
|
132 |
* Algorithm Names Specification for information about standard |
00c08fae63e8
8180005: Provide specific links in KeyManagerFactory and TrustManagerFactory to the Standard Algorithm Names Specification
mullan
parents:
47216
diff
changeset
|
133 |
* algorithm names. |
2 | 134 |
* |
41826
b35ee9b35b09
4985694: Incomplete spec for most of the getInstances
wetmore
parents:
37348
diff
changeset
|
135 |
* @return the new {@code KeyManagerFactory} object |
2 | 136 |
* |
41826
b35ee9b35b09
4985694: Incomplete spec for most of the getInstances
wetmore
parents:
37348
diff
changeset
|
137 |
* @throws NoSuchAlgorithmException if no {@code Provider} supports a |
b35ee9b35b09
4985694: Incomplete spec for most of the getInstances
wetmore
parents:
37348
diff
changeset
|
138 |
* {@code KeyManagerFactorySpi} implementation for the |
b35ee9b35b09
4985694: Incomplete spec for most of the getInstances
wetmore
parents:
37348
diff
changeset
|
139 |
* specified algorithm |
b35ee9b35b09
4985694: Incomplete spec for most of the getInstances
wetmore
parents:
37348
diff
changeset
|
140 |
* |
b35ee9b35b09
4985694: Incomplete spec for most of the getInstances
wetmore
parents:
37348
diff
changeset
|
141 |
* @throws NullPointerException if {@code algorithm} is {@code null} |
2 | 142 |
* |
143 |
* @see java.security.Provider |
|
144 |
*/ |
|
145 |
public static final KeyManagerFactory getInstance(String algorithm) |
|
146 |
throws NoSuchAlgorithmException { |
|
41826
b35ee9b35b09
4985694: Incomplete spec for most of the getInstances
wetmore
parents:
37348
diff
changeset
|
147 |
Objects.requireNonNull(algorithm, "null algorithm name"); |
2 | 148 |
GetInstance.Instance instance = GetInstance.getInstance |
149 |
("KeyManagerFactory", KeyManagerFactorySpi.class, |
|
150 |
algorithm); |
|
151 |
return new KeyManagerFactory((KeyManagerFactorySpi)instance.impl, |
|
152 |
instance.provider, algorithm); |
|
153 |
} |
|
154 |
||
155 |
/** |
|
156 |
* Returns a <code>KeyManagerFactory</code> object that acts as a |
|
157 |
* factory for key managers. |
|
158 |
* |
|
159 |
* <p> A new KeyManagerFactory object encapsulating the |
|
160 |
* KeyManagerFactorySpi implementation from the specified provider |
|
161 |
* is returned. The specified provider must be registered |
|
162 |
* in the security provider list. |
|
163 |
* |
|
164 |
* <p> Note that the list of registered providers may be retrieved via |
|
165 |
* the {@link Security#getProviders() Security.getProviders()} method. |
|
166 |
||
167 |
* @param algorithm the standard name of the requested algorithm. |
|
168 |
* See the <a href= |
|
55483
00c08fae63e8
8180005: Provide specific links in KeyManagerFactory and TrustManagerFactory to the Standard Algorithm Names Specification
mullan
parents:
47216
diff
changeset
|
169 |
* "{@docRoot}/../specs/security/standard-names.html#keymanagerfactory-algorithms"> |
00c08fae63e8
8180005: Provide specific links in KeyManagerFactory and TrustManagerFactory to the Standard Algorithm Names Specification
mullan
parents:
47216
diff
changeset
|
170 |
* KeyManagerFactory section</a> in the Java Security Standard |
00c08fae63e8
8180005: Provide specific links in KeyManagerFactory and TrustManagerFactory to the Standard Algorithm Names Specification
mullan
parents:
47216
diff
changeset
|
171 |
* Algorithm Names Specification for information about standard |
00c08fae63e8
8180005: Provide specific links in KeyManagerFactory and TrustManagerFactory to the Standard Algorithm Names Specification
mullan
parents:
47216
diff
changeset
|
172 |
* algorithm names. |
2 | 173 |
* |
174 |
* @param provider the name of the provider. |
|
175 |
* |
|
41826
b35ee9b35b09
4985694: Incomplete spec for most of the getInstances
wetmore
parents:
37348
diff
changeset
|
176 |
* @return the new {@code KeyManagerFactory} object |
2 | 177 |
* |
41826
b35ee9b35b09
4985694: Incomplete spec for most of the getInstances
wetmore
parents:
37348
diff
changeset
|
178 |
* @throws IllegalArgumentException if the provider name is {@code null} |
b35ee9b35b09
4985694: Incomplete spec for most of the getInstances
wetmore
parents:
37348
diff
changeset
|
179 |
* or empty |
b35ee9b35b09
4985694: Incomplete spec for most of the getInstances
wetmore
parents:
37348
diff
changeset
|
180 |
* |
b35ee9b35b09
4985694: Incomplete spec for most of the getInstances
wetmore
parents:
37348
diff
changeset
|
181 |
* @throws NoSuchAlgorithmException if a {@code KeyManagerFactorySpi} |
b35ee9b35b09
4985694: Incomplete spec for most of the getInstances
wetmore
parents:
37348
diff
changeset
|
182 |
* implementation for the specified algorithm is not |
b35ee9b35b09
4985694: Incomplete spec for most of the getInstances
wetmore
parents:
37348
diff
changeset
|
183 |
* available from the specified provider |
2 | 184 |
* |
185 |
* @throws NoSuchProviderException if the specified provider is not |
|
41826
b35ee9b35b09
4985694: Incomplete spec for most of the getInstances
wetmore
parents:
37348
diff
changeset
|
186 |
* registered in the security provider list |
2 | 187 |
* |
41826
b35ee9b35b09
4985694: Incomplete spec for most of the getInstances
wetmore
parents:
37348
diff
changeset
|
188 |
* @throws NullPointerException if {@code algorithm} is {@code null} |
2 | 189 |
* |
190 |
* @see java.security.Provider |
|
191 |
*/ |
|
192 |
public static final KeyManagerFactory getInstance(String algorithm, |
|
193 |
String provider) throws NoSuchAlgorithmException, |
|
194 |
NoSuchProviderException { |
|
41826
b35ee9b35b09
4985694: Incomplete spec for most of the getInstances
wetmore
parents:
37348
diff
changeset
|
195 |
Objects.requireNonNull(algorithm, "null algorithm name"); |
2 | 196 |
GetInstance.Instance instance = GetInstance.getInstance |
197 |
("KeyManagerFactory", KeyManagerFactorySpi.class, |
|
198 |
algorithm, provider); |
|
199 |
return new KeyManagerFactory((KeyManagerFactorySpi)instance.impl, |
|
200 |
instance.provider, algorithm); |
|
201 |
} |
|
202 |
||
203 |
/** |
|
204 |
* Returns a <code>KeyManagerFactory</code> object that acts as a |
|
205 |
* factory for key managers. |
|
206 |
* |
|
207 |
* <p> A new KeyManagerFactory object encapsulating the |
|
208 |
* KeyManagerFactorySpi implementation from the specified Provider |
|
209 |
* object is returned. Note that the specified Provider object |
|
210 |
* does not have to be registered in the provider list. |
|
211 |
* |
|
212 |
* @param algorithm the standard name of the requested algorithm. |
|
213 |
* See the <a href= |
|
55483
00c08fae63e8
8180005: Provide specific links in KeyManagerFactory and TrustManagerFactory to the Standard Algorithm Names Specification
mullan
parents:
47216
diff
changeset
|
214 |
* "{@docRoot}/../specs/security/standard-names.html#keymanagerfactory-algorithms"> |
00c08fae63e8
8180005: Provide specific links in KeyManagerFactory and TrustManagerFactory to the Standard Algorithm Names Specification
mullan
parents:
47216
diff
changeset
|
215 |
* KeyManagerFactory section</a> in the Java Security Standard |
00c08fae63e8
8180005: Provide specific links in KeyManagerFactory and TrustManagerFactory to the Standard Algorithm Names Specification
mullan
parents:
47216
diff
changeset
|
216 |
* Algorithm Names Specification for information about standard |
00c08fae63e8
8180005: Provide specific links in KeyManagerFactory and TrustManagerFactory to the Standard Algorithm Names Specification
mullan
parents:
47216
diff
changeset
|
217 |
* algorithm names. |
2 | 218 |
* |
219 |
* @param provider an instance of the provider. |
|
220 |
* |
|
41826
b35ee9b35b09
4985694: Incomplete spec for most of the getInstances
wetmore
parents:
37348
diff
changeset
|
221 |
* @return the new {@code KeyManagerFactory} object |
b35ee9b35b09
4985694: Incomplete spec for most of the getInstances
wetmore
parents:
37348
diff
changeset
|
222 |
* |
b35ee9b35b09
4985694: Incomplete spec for most of the getInstances
wetmore
parents:
37348
diff
changeset
|
223 |
* @throws IllegalArgumentException if provider is {@code null} |
2 | 224 |
* |
41826
b35ee9b35b09
4985694: Incomplete spec for most of the getInstances
wetmore
parents:
37348
diff
changeset
|
225 |
* @throws NoSuchAlgorithmException if a {@code @KeyManagerFactorySpi} |
b35ee9b35b09
4985694: Incomplete spec for most of the getInstances
wetmore
parents:
37348
diff
changeset
|
226 |
* implementation for the specified algorithm is not available |
b35ee9b35b09
4985694: Incomplete spec for most of the getInstances
wetmore
parents:
37348
diff
changeset
|
227 |
* from the specified Provider object |
2 | 228 |
* |
41826
b35ee9b35b09
4985694: Incomplete spec for most of the getInstances
wetmore
parents:
37348
diff
changeset
|
229 |
* @throws NullPointerException if {@code algorithm} is {@code null} |
2 | 230 |
* |
231 |
* @see java.security.Provider |
|
232 |
*/ |
|
233 |
public static final KeyManagerFactory getInstance(String algorithm, |
|
234 |
Provider provider) throws NoSuchAlgorithmException { |
|
41826
b35ee9b35b09
4985694: Incomplete spec for most of the getInstances
wetmore
parents:
37348
diff
changeset
|
235 |
Objects.requireNonNull(algorithm, "null algorithm name"); |
2 | 236 |
GetInstance.Instance instance = GetInstance.getInstance |
237 |
("KeyManagerFactory", KeyManagerFactorySpi.class, |
|
238 |
algorithm, provider); |
|
239 |
return new KeyManagerFactory((KeyManagerFactorySpi)instance.impl, |
|
240 |
instance.provider, algorithm); |
|
241 |
} |
|
242 |
||
243 |
/** |
|
244 |
* Returns the provider of this <code>KeyManagerFactory</code> object. |
|
245 |
* |
|
246 |
* @return the provider of this <code>KeyManagerFactory</code> object |
|
247 |
*/ |
|
248 |
public final Provider getProvider() { |
|
249 |
return this.provider; |
|
250 |
} |
|
251 |
||
252 |
||
253 |
/** |
|
254 |
* Initializes this factory with a source of key material. |
|
255 |
* <P> |
|
256 |
* The provider typically uses a KeyStore for obtaining |
|
257 |
* key material for use during secure socket negotiations. |
|
258 |
* The KeyStore is generally password-protected. |
|
259 |
* <P> |
|
260 |
* For more flexible initialization, please see |
|
261 |
* {@link #init(ManagerFactoryParameters)}. |
|
262 |
* |
|
263 |
* @param ks the key store or null |
|
264 |
* @param password the password for recovering keys in the KeyStore |
|
265 |
* @throws KeyStoreException if this operation fails |
|
266 |
* @throws NoSuchAlgorithmException if the specified algorithm is not |
|
267 |
* available from the specified provider. |
|
268 |
* @throws UnrecoverableKeyException if the key cannot be recovered |
|
269 |
* (e.g. the given password is wrong). |
|
270 |
*/ |
|
271 |
public final void init(KeyStore ks, char[] password) throws |
|
272 |
KeyStoreException, NoSuchAlgorithmException, |
|
273 |
UnrecoverableKeyException { |
|
274 |
factorySpi.engineInit(ks, password); |
|
275 |
} |
|
276 |
||
277 |
||
278 |
/** |
|
279 |
* Initializes this factory with a source of provider-specific |
|
280 |
* key material. |
|
281 |
* <P> |
|
282 |
* In some cases, initialization parameters other than a keystore |
|
283 |
* and password may be needed by a provider. Users of that |
|
284 |
* particular provider are expected to pass an implementation of |
|
285 |
* the appropriate <CODE>ManagerFactoryParameters</CODE> as |
|
286 |
* defined by the provider. The provider can then call the |
|
287 |
* specified methods in the <CODE>ManagerFactoryParameters</CODE> |
|
288 |
* implementation to obtain the needed information. |
|
289 |
* |
|
290 |
* @param spec an implementation of a provider-specific parameter |
|
291 |
* specification |
|
292 |
* @throws InvalidAlgorithmParameterException if an error is encountered |
|
293 |
*/ |
|
294 |
public final void init(ManagerFactoryParameters spec) throws |
|
295 |
InvalidAlgorithmParameterException { |
|
296 |
factorySpi.engineInit(spec); |
|
297 |
} |
|
298 |
||
299 |
||
300 |
/** |
|
301 |
* Returns one key manager for each type of key material. |
|
302 |
* |
|
303 |
* @return the key managers |
|
304 |
* @throws IllegalStateException if the KeyManagerFactory is not initialized |
|
305 |
*/ |
|
306 |
public final KeyManager[] getKeyManagers() { |
|
307 |
return factorySpi.engineGetKeyManagers(); |
|
308 |
} |
|
309 |
} |