author | jjg |
Thu, 27 Apr 2017 09:47:40 -0700 | |
changeset 44848 | e946981dc37c |
parent 41826 | b35ee9b35b09 |
child 45118 | e4258d800b54 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
37348
9ccec3170d5e
8152205: jdk.security.provider.preferred is ambiguously documented
ascarpino
parents:
33241
diff
changeset
|
2 |
* Copyright (c) 1999, 2016, 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 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 |
|
32647 | 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> |
|
44848
e946981dc37c
8179370: Replace use of <tt>, <center> and <font> tags in java.base
jjg
parents:
41826
diff
changeset
|
43 |
* <li>{@code PKIX}</li> |
32647 | 44 |
* </ul> |
45 |
* This algorithm is described in the <a href= |
|
46 |
* "{@docRoot}/../technotes/guides/security/StandardNames.html#TrustManagerFactory"> |
|
47 |
* TrustManagerFactory section</a> of the |
|
48 |
* Java Cryptography Architecture Standard Algorithm Name Documentation. |
|
49 |
* Consult the release documentation for your implementation to see if any |
|
50 |
* other algorithms are supported. |
|
2 | 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 |
|
14775
2ed01c760aea
8004064: Downgrade normative references to ${java.home}/lib/security/java.security
mullan
parents:
14514
diff
changeset
|
69 |
* the value of the {@code ssl.TrustManagerFactory.algorithm} |
2ed01c760aea
8004064: Downgrade normative references to ${java.home}/lib/security/java.security
mullan
parents:
14514
diff
changeset
|
70 |
* security property to the desired algorithm name. |
2 | 71 |
* |
14775
2ed01c760aea
8004064: Downgrade normative references to ${java.home}/lib/security/java.security
mullan
parents:
14514
diff
changeset
|
72 |
* @see java.security.Security security properties |
2ed01c760aea
8004064: Downgrade normative references to ${java.home}/lib/security/java.security
mullan
parents:
14514
diff
changeset
|
73 |
* @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
|
74 |
* {@code ssl.TrustManagerFactory.algorithm} security property, or an |
2ed01c760aea
8004064: Downgrade normative references to ${java.home}/lib/security/java.security
mullan
parents:
14514
diff
changeset
|
75 |
* implementation-specific default if no such property exists. |
2 | 76 |
*/ |
32649
2ee9017c7597
8136583: Core libraries should use blessed modifier order
martin
parents:
32647
diff
changeset
|
77 |
public static final String getDefaultAlgorithm() { |
2 | 78 |
String type; |
30033
b9c86c17164a
8078468: Update security libraries to use diamond with anonymous classes
darcy
parents:
25859
diff
changeset
|
79 |
type = AccessController.doPrivileged(new PrivilegedAction<>() { |
14514 | 80 |
@Override |
2 | 81 |
public String run() { |
82 |
return Security.getProperty( |
|
83 |
"ssl.TrustManagerFactory.algorithm"); |
|
84 |
} |
|
85 |
}); |
|
86 |
if (type == null) { |
|
87 |
type = "SunX509"; |
|
88 |
} |
|
89 |
return type; |
|
90 |
} |
|
91 |
||
92 |
/** |
|
93 |
* Creates a TrustManagerFactory object. |
|
94 |
* |
|
95 |
* @param factorySpi the delegate |
|
96 |
* @param provider the provider |
|
97 |
* @param algorithm the algorithm |
|
98 |
*/ |
|
99 |
protected TrustManagerFactory(TrustManagerFactorySpi factorySpi, |
|
100 |
Provider provider, String algorithm) { |
|
101 |
this.factorySpi = factorySpi; |
|
102 |
this.provider = provider; |
|
103 |
this.algorithm = algorithm; |
|
104 |
} |
|
105 |
||
106 |
/** |
|
107 |
* Returns the algorithm name of this <code>TrustManagerFactory</code> |
|
108 |
* object. |
|
109 |
* |
|
110 |
* <p>This is the same name that was specified in one of the |
|
111 |
* <code>getInstance</code> calls that created this |
|
112 |
* <code>TrustManagerFactory</code> object. |
|
113 |
* |
|
114 |
* @return the algorithm name of this <code>TrustManagerFactory</code> |
|
115 |
* object |
|
116 |
*/ |
|
117 |
public final String getAlgorithm() { |
|
118 |
return this.algorithm; |
|
119 |
} |
|
120 |
||
121 |
/** |
|
122 |
* Returns a <code>TrustManagerFactory</code> object that acts as a |
|
123 |
* factory for trust managers. |
|
124 |
* |
|
125 |
* <p> This method traverses the list of registered security Providers, |
|
126 |
* starting with the most preferred Provider. |
|
127 |
* A new TrustManagerFactory object encapsulating the |
|
128 |
* TrustManagerFactorySpi implementation from the first |
|
129 |
* Provider that supports the specified algorithm is returned. |
|
130 |
* |
|
131 |
* <p> Note that the list of registered providers may be retrieved via |
|
132 |
* the {@link Security#getProviders() Security.getProviders()} method. |
|
133 |
* |
|
33241
27eb2d6abda9
8133151: Preferred provider configuration for JCE
ascarpino
parents:
32649
diff
changeset
|
134 |
* @implNote |
27eb2d6abda9
8133151: Preferred provider configuration for JCE
ascarpino
parents:
32649
diff
changeset
|
135 |
* The JDK Reference Implementation additionally uses the |
37348
9ccec3170d5e
8152205: jdk.security.provider.preferred is ambiguously documented
ascarpino
parents:
33241
diff
changeset
|
136 |
* {@code jdk.security.provider.preferred} |
9ccec3170d5e
8152205: jdk.security.provider.preferred is ambiguously documented
ascarpino
parents:
33241
diff
changeset
|
137 |
* {@link Security#getProperty(String) Security} property to determine |
33241
27eb2d6abda9
8133151: Preferred provider configuration for JCE
ascarpino
parents:
32649
diff
changeset
|
138 |
* the preferred provider order for the specified algorithm. This |
27eb2d6abda9
8133151: Preferred provider configuration for JCE
ascarpino
parents:
32649
diff
changeset
|
139 |
* may be different than the order of providers returned by |
27eb2d6abda9
8133151: Preferred provider configuration for JCE
ascarpino
parents:
32649
diff
changeset
|
140 |
* {@link Security#getProviders() Security.getProviders()}. |
27eb2d6abda9
8133151: Preferred provider configuration for JCE
ascarpino
parents:
32649
diff
changeset
|
141 |
* |
2 | 142 |
* @param algorithm the standard name of the requested trust management |
143 |
* algorithm. See the <a href= |
|
144 |
* "{@docRoot}/../technotes/guides/security/jsse/JSSERefGuide.html"> |
|
145 |
* Java Secure Socket Extension Reference Guide </a> |
|
146 |
* for information about standard algorithm names. |
|
147 |
* |
|
41826
b35ee9b35b09
4985694: Incomplete spec for most of the getInstances
wetmore
parents:
37348
diff
changeset
|
148 |
* @return the new {@code TrustManagerFactory} object |
2 | 149 |
* |
41826
b35ee9b35b09
4985694: Incomplete spec for most of the getInstances
wetmore
parents:
37348
diff
changeset
|
150 |
* @throws NoSuchAlgorithmException if no {@code Provider} supports a |
b35ee9b35b09
4985694: Incomplete spec for most of the getInstances
wetmore
parents:
37348
diff
changeset
|
151 |
* {@code TrustManagerFactorySpi} implementation for the |
b35ee9b35b09
4985694: Incomplete spec for most of the getInstances
wetmore
parents:
37348
diff
changeset
|
152 |
* specified algorithm |
b35ee9b35b09
4985694: Incomplete spec for most of the getInstances
wetmore
parents:
37348
diff
changeset
|
153 |
* |
b35ee9b35b09
4985694: Incomplete spec for most of the getInstances
wetmore
parents:
37348
diff
changeset
|
154 |
* @throws NullPointerException if {@code algorithm} is {@code null} |
2 | 155 |
* |
156 |
* @see java.security.Provider |
|
157 |
*/ |
|
158 |
public static final TrustManagerFactory getInstance(String algorithm) |
|
159 |
throws NoSuchAlgorithmException { |
|
41826
b35ee9b35b09
4985694: Incomplete spec for most of the getInstances
wetmore
parents:
37348
diff
changeset
|
160 |
Objects.requireNonNull(algorithm, "null algorithm name"); |
2 | 161 |
GetInstance.Instance instance = GetInstance.getInstance |
162 |
("TrustManagerFactory", TrustManagerFactorySpi.class, |
|
163 |
algorithm); |
|
164 |
return new TrustManagerFactory((TrustManagerFactorySpi)instance.impl, |
|
165 |
instance.provider, algorithm); |
|
166 |
} |
|
167 |
||
168 |
/** |
|
169 |
* Returns a <code>TrustManagerFactory</code> object that acts as a |
|
170 |
* factory for trust managers. |
|
171 |
* |
|
172 |
* <p> A new KeyManagerFactory object encapsulating the |
|
173 |
* KeyManagerFactorySpi implementation from the specified provider |
|
174 |
* is returned. The specified provider must be registered |
|
175 |
* in the security provider list. |
|
176 |
* |
|
177 |
* <p> Note that the list of registered providers may be retrieved via |
|
178 |
* the {@link Security#getProviders() Security.getProviders()} method. |
|
179 |
* |
|
180 |
* @param algorithm the standard name of the requested trust management |
|
181 |
* algorithm. See the <a href= |
|
182 |
* "{@docRoot}/../technotes/guides/security/jsse/JSSERefGuide.html"> |
|
183 |
* Java Secure Socket Extension Reference Guide </a> |
|
184 |
* for information about standard algorithm names. |
|
185 |
* |
|
186 |
* @param provider the name of the provider. |
|
187 |
* |
|
41826
b35ee9b35b09
4985694: Incomplete spec for most of the getInstances
wetmore
parents:
37348
diff
changeset
|
188 |
* @return the new {@code TrustManagerFactory} object |
2 | 189 |
* |
41826
b35ee9b35b09
4985694: Incomplete spec for most of the getInstances
wetmore
parents:
37348
diff
changeset
|
190 |
* @throws IllegalArgumentException if the provider name is |
b35ee9b35b09
4985694: Incomplete spec for most of the getInstances
wetmore
parents:
37348
diff
changeset
|
191 |
* {@code null} or empty |
b35ee9b35b09
4985694: Incomplete spec for most of the getInstances
wetmore
parents:
37348
diff
changeset
|
192 |
* |
b35ee9b35b09
4985694: Incomplete spec for most of the getInstances
wetmore
parents:
37348
diff
changeset
|
193 |
* @throws NoSuchAlgorithmException if a {@code TrustManagerFactorySpi} |
b35ee9b35b09
4985694: Incomplete spec for most of the getInstances
wetmore
parents:
37348
diff
changeset
|
194 |
* implementation for the specified algorithm is not |
b35ee9b35b09
4985694: Incomplete spec for most of the getInstances
wetmore
parents:
37348
diff
changeset
|
195 |
* available from the specified provider |
2 | 196 |
* |
197 |
* @throws NoSuchProviderException if the specified provider is not |
|
41826
b35ee9b35b09
4985694: Incomplete spec for most of the getInstances
wetmore
parents:
37348
diff
changeset
|
198 |
* registered in the security provider list |
2 | 199 |
* |
41826
b35ee9b35b09
4985694: Incomplete spec for most of the getInstances
wetmore
parents:
37348
diff
changeset
|
200 |
* @throws NullPointerException if {@code algorithm} is {@code null} |
2 | 201 |
* |
202 |
* @see java.security.Provider |
|
203 |
*/ |
|
204 |
public static final TrustManagerFactory getInstance(String algorithm, |
|
205 |
String provider) throws NoSuchAlgorithmException, |
|
206 |
NoSuchProviderException { |
|
41826
b35ee9b35b09
4985694: Incomplete spec for most of the getInstances
wetmore
parents:
37348
diff
changeset
|
207 |
Objects.requireNonNull(algorithm, "null algorithm name"); |
2 | 208 |
GetInstance.Instance instance = GetInstance.getInstance |
209 |
("TrustManagerFactory", TrustManagerFactorySpi.class, |
|
210 |
algorithm, provider); |
|
211 |
return new TrustManagerFactory((TrustManagerFactorySpi)instance.impl, |
|
212 |
instance.provider, algorithm); |
|
213 |
} |
|
214 |
||
215 |
/** |
|
216 |
* Returns a <code>TrustManagerFactory</code> object that acts as a |
|
217 |
* factory for trust managers. |
|
218 |
* |
|
219 |
* <p> A new TrustManagerFactory object encapsulating the |
|
220 |
* TrustManagerFactorySpi implementation from the specified Provider |
|
221 |
* object is returned. Note that the specified Provider object |
|
222 |
* does not have to be registered in the provider list. |
|
223 |
* |
|
224 |
* @param algorithm the standard name of the requested trust management |
|
225 |
* algorithm. See the <a href= |
|
226 |
* "{@docRoot}/../technotes/guides/security/jsse/JSSERefGuide.html"> |
|
227 |
* Java Secure Socket Extension Reference Guide </a> |
|
228 |
* for information about standard algorithm names. |
|
229 |
* |
|
230 |
* @param provider an instance of the provider. |
|
231 |
* |
|
41826
b35ee9b35b09
4985694: Incomplete spec for most of the getInstances
wetmore
parents:
37348
diff
changeset
|
232 |
* @return the new {@code TrustManagerFactory} object |
b35ee9b35b09
4985694: Incomplete spec for most of the getInstances
wetmore
parents:
37348
diff
changeset
|
233 |
* |
b35ee9b35b09
4985694: Incomplete spec for most of the getInstances
wetmore
parents:
37348
diff
changeset
|
234 |
* @throws IllegalArgumentException if the provider is {@code null} |
2 | 235 |
* |
41826
b35ee9b35b09
4985694: Incomplete spec for most of the getInstances
wetmore
parents:
37348
diff
changeset
|
236 |
* @throws NoSuchAlgorithmException if a {@code TrustManagerFactorySpi} |
b35ee9b35b09
4985694: Incomplete spec for most of the getInstances
wetmore
parents:
37348
diff
changeset
|
237 |
* implementation for the specified algorithm is not available |
b35ee9b35b09
4985694: Incomplete spec for most of the getInstances
wetmore
parents:
37348
diff
changeset
|
238 |
* from the specified {@code Provider} object |
2 | 239 |
* |
41826
b35ee9b35b09
4985694: Incomplete spec for most of the getInstances
wetmore
parents:
37348
diff
changeset
|
240 |
* @throws NullPointerException if {@code algorithm} is {@code null} |
2 | 241 |
* |
242 |
* @see java.security.Provider |
|
243 |
*/ |
|
244 |
public static final TrustManagerFactory getInstance(String algorithm, |
|
245 |
Provider provider) throws NoSuchAlgorithmException { |
|
41826
b35ee9b35b09
4985694: Incomplete spec for most of the getInstances
wetmore
parents:
37348
diff
changeset
|
246 |
Objects.requireNonNull(algorithm, "null algorithm name"); |
2 | 247 |
GetInstance.Instance instance = GetInstance.getInstance |
248 |
("TrustManagerFactory", TrustManagerFactorySpi.class, |
|
249 |
algorithm, provider); |
|
250 |
return new TrustManagerFactory((TrustManagerFactorySpi)instance.impl, |
|
251 |
instance.provider, algorithm); |
|
252 |
} |
|
253 |
||
254 |
/** |
|
255 |
* Returns the provider of this <code>TrustManagerFactory</code> object. |
|
256 |
* |
|
257 |
* @return the provider of this <code>TrustManagerFactory</code> object |
|
258 |
*/ |
|
259 |
public final Provider getProvider() { |
|
260 |
return this.provider; |
|
261 |
} |
|
262 |
||
263 |
||
264 |
/** |
|
265 |
* Initializes this factory with a source of certificate |
|
266 |
* authorities and related trust material. |
|
267 |
* <P> |
|
268 |
* The provider typically uses a KeyStore as a basis for making |
|
269 |
* trust decisions. |
|
270 |
* <P> |
|
271 |
* For more flexible initialization, please see |
|
272 |
* {@link #init(ManagerFactoryParameters)}. |
|
273 |
* |
|
274 |
* @param ks the key store, or null |
|
275 |
* @throws KeyStoreException if this operation fails |
|
276 |
*/ |
|
277 |
public final void init(KeyStore ks) throws KeyStoreException { |
|
278 |
factorySpi.engineInit(ks); |
|
279 |
} |
|
280 |
||
281 |
||
282 |
/** |
|
283 |
* Initializes this factory with a source of provider-specific |
|
284 |
* trust material. |
|
285 |
* <P> |
|
286 |
* In some cases, initialization parameters other than a keystore |
|
287 |
* may be needed by a provider. Users of that particular provider |
|
288 |
* are expected to pass an implementation of the appropriate |
|
289 |
* <CODE>ManagerFactoryParameters</CODE> as defined by the |
|
290 |
* provider. The provider can then call the specified methods in |
|
291 |
* the <CODE>ManagerFactoryParameters</CODE> implementation to obtain the |
|
292 |
* needed information. |
|
293 |
* |
|
294 |
* @param spec an implementation of a provider-specific parameter |
|
295 |
* specification |
|
296 |
* @throws InvalidAlgorithmParameterException if an error is |
|
297 |
* encountered |
|
298 |
*/ |
|
299 |
public final void init(ManagerFactoryParameters spec) throws |
|
300 |
InvalidAlgorithmParameterException { |
|
301 |
factorySpi.engineInit(spec); |
|
302 |
} |
|
303 |
||
304 |
||
305 |
/** |
|
306 |
* Returns one trust manager for each type of trust material. |
|
307 |
* |
|
474
8fcc864c9258
6546671: (spec)javax.net.ssl.TrustManagerFactory.getInstance() throws undocumented NP
xuelei
parents:
2
diff
changeset
|
308 |
* @throws IllegalStateException if the factory is not initialized. |
8fcc864c9258
6546671: (spec)javax.net.ssl.TrustManagerFactory.getInstance() throws undocumented NP
xuelei
parents:
2
diff
changeset
|
309 |
* |
2 | 310 |
* @return the trust managers |
311 |
*/ |
|
312 |
public final TrustManager[] getTrustManagers() { |
|
313 |
return factorySpi.engineGetTrustManagers(); |
|
314 |
} |
|
315 |
} |