author | xuelei |
Tue, 02 Jun 2015 04:01:04 +0000 | |
changeset 30904 | ec0224270f90 |
parent 25859 | 3317bb8137f4 |
child 32210 | 958d823579c3 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
30904 | 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.*; |
|
29 |
||
30 |
import sun.security.jca.GetInstance; |
|
31 |
||
32 |
/** |
|
33 |
* Instances of this class represent a secure socket protocol |
|
34 |
* implementation which acts as a factory for secure socket |
|
30904 | 35 |
* factories or {@code SSLEngine}s. This class is initialized |
2 | 36 |
* with an optional set of key and trust managers and source of |
37 |
* secure random bytes. |
|
38 |
* |
|
8152
94e5966bdf22
5001004: Required Security Algorithms need to be defined
mullan
parents:
5820
diff
changeset
|
39 |
* <p> Every implementation of the Java platform is required to support the |
30904 | 40 |
* following standard {@code SSLContext} protocol: |
8152
94e5966bdf22
5001004: Required Security Algorithms need to be defined
mullan
parents:
5820
diff
changeset
|
41 |
* <ul> |
94e5966bdf22
5001004: Required Security Algorithms need to be defined
mullan
parents:
5820
diff
changeset
|
42 |
* <li><tt>TLSv1</tt></li> |
94e5966bdf22
5001004: Required Security Algorithms need to be defined
mullan
parents:
5820
diff
changeset
|
43 |
* </ul> |
94e5966bdf22
5001004: Required Security Algorithms need to be defined
mullan
parents:
5820
diff
changeset
|
44 |
* This protocol is described in the <a href= |
94e5966bdf22
5001004: Required Security Algorithms need to be defined
mullan
parents:
5820
diff
changeset
|
45 |
* "{@docRoot}/../technotes/guides/security/StandardNames.html#SSLContext"> |
94e5966bdf22
5001004: Required Security Algorithms need to be defined
mullan
parents:
5820
diff
changeset
|
46 |
* SSLContext section</a> of the |
94e5966bdf22
5001004: Required Security Algorithms need to be defined
mullan
parents:
5820
diff
changeset
|
47 |
* Java Cryptography Architecture Standard Algorithm Name Documentation. |
94e5966bdf22
5001004: Required Security Algorithms need to be defined
mullan
parents:
5820
diff
changeset
|
48 |
* Consult the release documentation for your implementation to see if any |
94e5966bdf22
5001004: Required Security Algorithms need to be defined
mullan
parents:
5820
diff
changeset
|
49 |
* other algorithms are supported. |
94e5966bdf22
5001004: Required Security Algorithms need to be defined
mullan
parents:
5820
diff
changeset
|
50 |
* |
2 | 51 |
* @since 1.4 |
52 |
*/ |
|
53 |
public class SSLContext { |
|
54 |
private final Provider provider; |
|
55 |
||
56 |
private final SSLContextSpi contextSpi; |
|
57 |
||
58 |
private final String protocol; |
|
59 |
||
60 |
/** |
|
61 |
* Creates an SSLContext object. |
|
62 |
* |
|
63 |
* @param contextSpi the delegate |
|
64 |
* @param provider the provider |
|
65 |
* @param protocol the protocol |
|
66 |
*/ |
|
67 |
protected SSLContext(SSLContextSpi contextSpi, Provider provider, |
|
68 |
String protocol) { |
|
69 |
this.contextSpi = contextSpi; |
|
70 |
this.provider = provider; |
|
71 |
this.protocol = protocol; |
|
72 |
} |
|
73 |
||
74 |
private static SSLContext defaultContext; |
|
75 |
||
76 |
/** |
|
77 |
* Returns the default SSL context. |
|
78 |
* |
|
79 |
* <p>If a default context was set using the {@link #setDefault |
|
80 |
* SSLContext.setDefault()} method, it is returned. Otherwise, the first |
|
81 |
* call of this method triggers the call |
|
30904 | 82 |
* {@code SSLContext.getInstance("Default")}. |
2 | 83 |
* If successful, that object is made the default SSL context and returned. |
84 |
* |
|
85 |
* <p>The default context is immediately |
|
86 |
* usable and does not require {@linkplain #init initialization}. |
|
87 |
* |
|
88 |
* @return the default SSL context |
|
89 |
* @throws NoSuchAlgorithmException if the |
|
90 |
* {@link SSLContext#getInstance SSLContext.getInstance()} call fails |
|
91 |
* @since 1.6 |
|
92 |
*/ |
|
93 |
public static synchronized SSLContext getDefault() |
|
94 |
throws NoSuchAlgorithmException { |
|
95 |
if (defaultContext == null) { |
|
96 |
defaultContext = SSLContext.getInstance("Default"); |
|
97 |
} |
|
98 |
return defaultContext; |
|
99 |
} |
|
100 |
||
101 |
/** |
|
102 |
* Sets the default SSL context. It will be returned by subsequent calls |
|
103 |
* to {@link #getDefault}. The default context must be immediately usable |
|
104 |
* and not require {@linkplain #init initialization}. |
|
105 |
* |
|
106 |
* @param context the SSLContext |
|
107 |
* @throws NullPointerException if context is null |
|
108 |
* @throws SecurityException if a security manager exists and its |
|
30904 | 109 |
* {@code checkPermission} method does not allow |
110 |
* {@code SSLPermission("setDefaultSSLContext")} |
|
2 | 111 |
* @since 1.6 |
112 |
*/ |
|
113 |
public static synchronized void setDefault(SSLContext context) { |
|
114 |
if (context == null) { |
|
115 |
throw new NullPointerException(); |
|
116 |
} |
|
117 |
SecurityManager sm = System.getSecurityManager(); |
|
118 |
if (sm != null) { |
|
119 |
sm.checkPermission(new SSLPermission("setDefaultSSLContext")); |
|
120 |
} |
|
121 |
defaultContext = context; |
|
122 |
} |
|
123 |
||
124 |
/** |
|
30904 | 125 |
* Returns a {@code SSLContext} object that implements the |
2 | 126 |
* specified secure socket protocol. |
127 |
* |
|
128 |
* <p> This method traverses the list of registered security Providers, |
|
129 |
* starting with the most preferred Provider. |
|
130 |
* A new SSLContext object encapsulating the |
|
131 |
* SSLContextSpi implementation from the first |
|
132 |
* Provider that supports the specified protocol is returned. |
|
133 |
* |
|
134 |
* <p> Note that the list of registered providers may be retrieved via |
|
135 |
* the {@link Security#getProviders() Security.getProviders()} method. |
|
136 |
* |
|
137 |
* @param protocol the standard name of the requested protocol. |
|
8152
94e5966bdf22
5001004: Required Security Algorithms need to be defined
mullan
parents:
5820
diff
changeset
|
138 |
* See the SSLContext section in the <a href= |
94e5966bdf22
5001004: Required Security Algorithms need to be defined
mullan
parents:
5820
diff
changeset
|
139 |
* "{@docRoot}/../technotes/guides/security/StandardNames.html#SSLContext"> |
94e5966bdf22
5001004: Required Security Algorithms need to be defined
mullan
parents:
5820
diff
changeset
|
140 |
* Java Cryptography Architecture Standard Algorithm Name |
94e5966bdf22
5001004: Required Security Algorithms need to be defined
mullan
parents:
5820
diff
changeset
|
141 |
* Documentation</a> |
2 | 142 |
* for information about standard protocol names. |
143 |
* |
|
30904 | 144 |
* @return the new {@code SSLContext} object. |
2 | 145 |
* |
146 |
* @exception NoSuchAlgorithmException if no Provider supports a |
|
12557 | 147 |
* SSLContextSpi implementation for the |
2 | 148 |
* specified protocol. |
473
80bf10b052ee
6546639: (spec)javax.net.ssl.SSLContext.getInstance(null) throws undocumented NPE
xuelei
parents:
2
diff
changeset
|
149 |
* @exception NullPointerException if protocol is null. |
2 | 150 |
* |
151 |
* @see java.security.Provider |
|
152 |
*/ |
|
153 |
public static SSLContext getInstance(String protocol) |
|
154 |
throws NoSuchAlgorithmException { |
|
155 |
GetInstance.Instance instance = GetInstance.getInstance |
|
156 |
("SSLContext", SSLContextSpi.class, protocol); |
|
157 |
return new SSLContext((SSLContextSpi)instance.impl, instance.provider, |
|
158 |
protocol); |
|
159 |
} |
|
160 |
||
161 |
/** |
|
30904 | 162 |
* Returns a {@code SSLContext} object that implements the |
2 | 163 |
* specified secure socket protocol. |
164 |
* |
|
165 |
* <p> A new SSLContext object encapsulating the |
|
166 |
* SSLContextSpi implementation from the specified provider |
|
167 |
* is returned. The specified provider must be registered |
|
168 |
* in the security provider list. |
|
169 |
* |
|
170 |
* <p> Note that the list of registered providers may be retrieved via |
|
171 |
* the {@link Security#getProviders() Security.getProviders()} method. |
|
172 |
* |
|
173 |
* @param protocol the standard name of the requested protocol. |
|
8152
94e5966bdf22
5001004: Required Security Algorithms need to be defined
mullan
parents:
5820
diff
changeset
|
174 |
* See the SSLContext section in the <a href= |
94e5966bdf22
5001004: Required Security Algorithms need to be defined
mullan
parents:
5820
diff
changeset
|
175 |
* "{@docRoot}/../technotes/guides/security/StandardNames.html#SSLContext"> |
94e5966bdf22
5001004: Required Security Algorithms need to be defined
mullan
parents:
5820
diff
changeset
|
176 |
* Java Cryptography Architecture Standard Algorithm Name |
94e5966bdf22
5001004: Required Security Algorithms need to be defined
mullan
parents:
5820
diff
changeset
|
177 |
* Documentation</a> |
2 | 178 |
* for information about standard protocol names. |
179 |
* |
|
180 |
* @param provider the name of the provider. |
|
181 |
* |
|
30904 | 182 |
* @return the new {@code SSLContext} object. |
2 | 183 |
* |
184 |
* @throws NoSuchAlgorithmException if a SSLContextSpi |
|
185 |
* implementation for the specified protocol is not |
|
186 |
* available from the specified provider. |
|
187 |
* |
|
188 |
* @throws NoSuchProviderException if the specified provider is not |
|
189 |
* registered in the security provider list. |
|
190 |
* |
|
191 |
* @throws IllegalArgumentException if the provider name is null or empty. |
|
473
80bf10b052ee
6546639: (spec)javax.net.ssl.SSLContext.getInstance(null) throws undocumented NPE
xuelei
parents:
2
diff
changeset
|
192 |
* @throws NullPointerException if protocol is null. |
2 | 193 |
* |
194 |
* @see java.security.Provider |
|
195 |
*/ |
|
196 |
public static SSLContext getInstance(String protocol, String provider) |
|
197 |
throws NoSuchAlgorithmException, NoSuchProviderException { |
|
198 |
GetInstance.Instance instance = GetInstance.getInstance |
|
199 |
("SSLContext", SSLContextSpi.class, protocol, provider); |
|
200 |
return new SSLContext((SSLContextSpi)instance.impl, instance.provider, |
|
201 |
protocol); |
|
202 |
} |
|
203 |
||
204 |
/** |
|
30904 | 205 |
* Returns a {@code SSLContext} object that implements the |
2 | 206 |
* specified secure socket protocol. |
207 |
* |
|
208 |
* <p> A new SSLContext object encapsulating the |
|
209 |
* SSLContextSpi 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 protocol the standard name of the requested protocol. |
|
8152
94e5966bdf22
5001004: Required Security Algorithms need to be defined
mullan
parents:
5820
diff
changeset
|
214 |
* See the SSLContext section in the <a href= |
94e5966bdf22
5001004: Required Security Algorithms need to be defined
mullan
parents:
5820
diff
changeset
|
215 |
* "{@docRoot}/../technotes/guides/security/StandardNames.html#SSLContext"> |
94e5966bdf22
5001004: Required Security Algorithms need to be defined
mullan
parents:
5820
diff
changeset
|
216 |
* Java Cryptography Architecture Standard Algorithm Name |
94e5966bdf22
5001004: Required Security Algorithms need to be defined
mullan
parents:
5820
diff
changeset
|
217 |
* Documentation</a> |
2 | 218 |
* for information about standard protocol names. |
219 |
* |
|
220 |
* @param provider an instance of the provider. |
|
221 |
* |
|
30904 | 222 |
* @return the new {@code SSLContext} object. |
2 | 223 |
* |
12557 | 224 |
* @throws NoSuchAlgorithmException if a SSLContextSpi |
2 | 225 |
* implementation for the specified protocol is not available |
226 |
* from the specified Provider object. |
|
227 |
* |
|
12557 | 228 |
* @throws IllegalArgumentException if the provider is null. |
473
80bf10b052ee
6546639: (spec)javax.net.ssl.SSLContext.getInstance(null) throws undocumented NPE
xuelei
parents:
2
diff
changeset
|
229 |
* @throws NullPointerException if protocol is null. |
2 | 230 |
* |
231 |
* @see java.security.Provider |
|
232 |
*/ |
|
233 |
public static SSLContext getInstance(String protocol, Provider provider) |
|
234 |
throws NoSuchAlgorithmException { |
|
235 |
GetInstance.Instance instance = GetInstance.getInstance |
|
236 |
("SSLContext", SSLContextSpi.class, protocol, provider); |
|
237 |
return new SSLContext((SSLContextSpi)instance.impl, instance.provider, |
|
238 |
protocol); |
|
239 |
} |
|
240 |
||
241 |
/** |
|
30904 | 242 |
* Returns the protocol name of this {@code SSLContext} object. |
2 | 243 |
* |
244 |
* <p>This is the same name that was specified in one of the |
|
30904 | 245 |
* {@code getInstance} calls that created this |
246 |
* {@code SSLContext} object. |
|
2 | 247 |
* |
30904 | 248 |
* @return the protocol name of this {@code SSLContext} object. |
2 | 249 |
*/ |
250 |
public final String getProtocol() { |
|
251 |
return this.protocol; |
|
252 |
} |
|
253 |
||
254 |
/** |
|
30904 | 255 |
* Returns the provider of this {@code SSLContext} object. |
2 | 256 |
* |
30904 | 257 |
* @return the provider of this {@code SSLContext} object |
2 | 258 |
*/ |
259 |
public final Provider getProvider() { |
|
260 |
return this.provider; |
|
261 |
} |
|
262 |
||
263 |
/** |
|
264 |
* Initializes this context. Either of the first two parameters |
|
265 |
* may be null in which case the installed security providers will |
|
266 |
* be searched for the highest priority implementation of the |
|
267 |
* appropriate factory. Likewise, the secure random parameter may |
|
268 |
* be null in which case the default implementation will be used. |
|
269 |
* <P> |
|
270 |
* Only the first instance of a particular key and/or trust manager |
|
271 |
* implementation type in the array is used. (For example, only |
|
272 |
* the first javax.net.ssl.X509KeyManager in the array will be used.) |
|
273 |
* |
|
274 |
* @param km the sources of authentication keys or null |
|
275 |
* @param tm the sources of peer authentication trust decisions or null |
|
276 |
* @param random the source of randomness for this generator or null |
|
277 |
* @throws KeyManagementException if this operation fails |
|
278 |
*/ |
|
279 |
public final void init(KeyManager[] km, TrustManager[] tm, |
|
280 |
SecureRandom random) |
|
281 |
throws KeyManagementException { |
|
282 |
contextSpi.engineInit(km, tm, random); |
|
283 |
} |
|
284 |
||
285 |
/** |
|
30904 | 286 |
* Returns a {@code SocketFactory} object for this |
2 | 287 |
* context. |
288 |
* |
|
30904 | 289 |
* @return the {@code SocketFactory} object |
290 |
* @throws UnsupportedOperationException if the underlying provider |
|
291 |
* does not implement the operation. |
|
2 | 292 |
* @throws IllegalStateException if the SSLContextImpl requires |
30904 | 293 |
* initialization and the {@code init()} has not been called |
2 | 294 |
*/ |
295 |
public final SSLSocketFactory getSocketFactory() { |
|
296 |
return contextSpi.engineGetSocketFactory(); |
|
297 |
} |
|
298 |
||
299 |
/** |
|
30904 | 300 |
* Returns a {@code ServerSocketFactory} object for |
2 | 301 |
* this context. |
302 |
* |
|
30904 | 303 |
* @return the {@code ServerSocketFactory} object |
304 |
* @throws UnsupportedOperationException if the underlying provider |
|
305 |
* does not implement the operation. |
|
2 | 306 |
* @throws IllegalStateException if the SSLContextImpl requires |
30904 | 307 |
* initialization and the {@code init()} has not been called |
2 | 308 |
*/ |
309 |
public final SSLServerSocketFactory getServerSocketFactory() { |
|
310 |
return contextSpi.engineGetServerSocketFactory(); |
|
311 |
} |
|
312 |
||
313 |
/** |
|
30904 | 314 |
* Creates a new {@code SSLEngine} using this context. |
2 | 315 |
* <P> |
316 |
* Applications using this factory method are providing no hints |
|
317 |
* for an internal session reuse strategy. If hints are desired, |
|
318 |
* {@link #createSSLEngine(String, int)} should be used |
|
319 |
* instead. |
|
320 |
* <P> |
|
321 |
* Some cipher suites (such as Kerberos) require remote hostname |
|
322 |
* information, in which case this factory method should not be used. |
|
323 |
* |
|
30904 | 324 |
* @return the {@code SSLEngine} object |
2 | 325 |
* @throws UnsupportedOperationException if the underlying provider |
326 |
* does not implement the operation. |
|
327 |
* @throws IllegalStateException if the SSLContextImpl requires |
|
30904 | 328 |
* initialization and the {@code init()} has not been called |
2 | 329 |
* @since 1.5 |
330 |
*/ |
|
331 |
public final SSLEngine createSSLEngine() { |
|
332 |
try { |
|
333 |
return contextSpi.engineCreateSSLEngine(); |
|
334 |
} catch (AbstractMethodError e) { |
|
335 |
UnsupportedOperationException unsup = |
|
336 |
new UnsupportedOperationException( |
|
337 |
"Provider: " + getProvider() + |
|
338 |
" doesn't support this operation"); |
|
339 |
unsup.initCause(e); |
|
340 |
throw unsup; |
|
341 |
} |
|
342 |
} |
|
343 |
||
344 |
/** |
|
30904 | 345 |
* Creates a new {@code SSLEngine} using this context using |
2 | 346 |
* advisory peer information. |
347 |
* <P> |
|
348 |
* Applications using this factory method are providing hints |
|
349 |
* for an internal session reuse strategy. |
|
350 |
* <P> |
|
351 |
* Some cipher suites (such as Kerberos) require remote hostname |
|
352 |
* information, in which case peerHost needs to be specified. |
|
353 |
* |
|
354 |
* @param peerHost the non-authoritative name of the host |
|
355 |
* @param peerPort the non-authoritative port |
|
30904 | 356 |
* @return the new {@code SSLEngine} object |
2 | 357 |
* @throws UnsupportedOperationException if the underlying provider |
358 |
* does not implement the operation. |
|
359 |
* @throws IllegalStateException if the SSLContextImpl requires |
|
30904 | 360 |
* initialization and the {@code init()} has not been called |
2 | 361 |
* @since 1.5 |
362 |
*/ |
|
363 |
public final SSLEngine createSSLEngine(String peerHost, int peerPort) { |
|
364 |
try { |
|
365 |
return contextSpi.engineCreateSSLEngine(peerHost, peerPort); |
|
366 |
} catch (AbstractMethodError e) { |
|
367 |
UnsupportedOperationException unsup = |
|
368 |
new UnsupportedOperationException( |
|
369 |
"Provider: " + getProvider() + |
|
370 |
" does not support this operation"); |
|
371 |
unsup.initCause(e); |
|
372 |
throw unsup; |
|
373 |
} |
|
374 |
} |
|
375 |
||
376 |
/** |
|
377 |
* Returns the server session context, which represents the set of |
|
378 |
* SSL sessions available for use during the handshake phase of |
|
379 |
* server-side SSL sockets. |
|
380 |
* <P> |
|
381 |
* This context may be unavailable in some environments, in which |
|
382 |
* case this method returns null. For example, when the underlying |
|
383 |
* SSL provider does not provide an implementation of SSLSessionContext |
|
384 |
* interface, this method returns null. A non-null session context |
|
385 |
* is returned otherwise. |
|
386 |
* |
|
387 |
* @return server session context bound to this SSL context |
|
388 |
*/ |
|
389 |
public final SSLSessionContext getServerSessionContext() { |
|
390 |
return contextSpi.engineGetServerSessionContext(); |
|
391 |
} |
|
392 |
||
393 |
/** |
|
394 |
* Returns the client session context, which represents the set of |
|
395 |
* SSL sessions available for use during the handshake phase of |
|
396 |
* client-side SSL sockets. |
|
397 |
* <P> |
|
398 |
* This context may be unavailable in some environments, in which |
|
399 |
* case this method returns null. For example, when the underlying |
|
400 |
* SSL provider does not provide an implementation of SSLSessionContext |
|
401 |
* interface, this method returns null. A non-null session context |
|
402 |
* is returned otherwise. |
|
403 |
* |
|
404 |
* @return client session context bound to this SSL context |
|
405 |
*/ |
|
406 |
public final SSLSessionContext getClientSessionContext() { |
|
407 |
return contextSpi.engineGetClientSessionContext(); |
|
408 |
} |
|
409 |
||
410 |
/** |
|
411 |
* Returns a copy of the SSLParameters indicating the default |
|
412 |
* settings for this SSL context. |
|
413 |
* |
|
414 |
* <p>The parameters will always have the ciphersuites and protocols |
|
415 |
* arrays set to non-null values. |
|
416 |
* |
|
417 |
* @return a copy of the SSLParameters object with the default settings |
|
418 |
* @throws UnsupportedOperationException if the default SSL parameters |
|
419 |
* could not be obtained. |
|
420 |
* @since 1.6 |
|
421 |
*/ |
|
422 |
public final SSLParameters getDefaultSSLParameters() { |
|
423 |
return contextSpi.engineGetDefaultSSLParameters(); |
|
424 |
} |
|
425 |
||
426 |
/** |
|
427 |
* Returns a copy of the SSLParameters indicating the supported |
|
428 |
* settings for this SSL context. |
|
429 |
* |
|
430 |
* <p>The parameters will always have the ciphersuites and protocols |
|
431 |
* arrays set to non-null values. |
|
432 |
* |
|
433 |
* @return a copy of the SSLParameters object with the supported |
|
434 |
* settings |
|
435 |
* @throws UnsupportedOperationException if the supported SSL parameters |
|
436 |
* could not be obtained. |
|
437 |
* @since 1.6 |
|
438 |
*/ |
|
439 |
public final SSLParameters getSupportedSSLParameters() { |
|
440 |
return contextSpi.engineGetSupportedSSLParameters(); |
|
441 |
} |
|
442 |
||
443 |
} |