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