blob: efc1947fb0c03736e55f0f98240802ef199fa856 [file] [log] [blame]
The Android Open Source Projectadc854b2009-03-03 19:28:47 -08001/*
2 * Licensed to the Apache Software Foundation (ASF) under one or more
3 * contributor license agreements. See the NOTICE file distributed with
4 * this work for additional information regarding copyright ownership.
5 * The ASF licenses this file to You under the Apache License, Version 2.0
6 * (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
18package javax.net.ssl;
19
20import java.security.KeyManagementException;
21import java.security.NoSuchAlgorithmException;
22import java.security.NoSuchProviderException;
23import java.security.Provider;
24import java.security.SecureRandom;
25import java.security.Security;
The Android Open Source Projectadc854b2009-03-03 19:28:47 -080026import org.apache.harmony.security.fortress.Engine;
27
28
29/**
30 * The public API for secure socket protocol implementations. It acts as factory
31 * for {@code SSLSocketFactory}'s and {@code SSLEngine}s.
The Android Open Source Projectadc854b2009-03-03 19:28:47 -080032 */
The Android Open Source Projectadc854b2009-03-03 19:28:47 -080033public class SSLContext {
34 // StoreSSLContext service name
35 private static final String SERVICE = "SSLContext";
36
37 // Used to access common engine functionality
Brian Carlstrom0c131a22010-05-20 15:27:31 -070038 private static final Engine ENGINE = new Engine(SERVICE);
39
40 /**
41 * Default SSLContext that can be replaced with SSLContext.setDefault()
42 */
43 private static SSLContext DEFAULT;
44
45 /**
46 * Returns the default SSLContext.
47 *
48 * The default SSL context can be set with {@link #setDefault}. If
49 * not, one will be created with {@code
50 * SSLContext.getInstance("Default")}, which will already be
51 * initialized.
52 *
53 * @throws NoSuchAlgorithmException if there is a problem creating
54 * the default instance.
55 * @since 1.6
56 */
57 public static SSLContext getDefault() throws NoSuchAlgorithmException {
58 synchronized (ENGINE) {
59 if (DEFAULT == null) {
60 DEFAULT = SSLContext.getInstance("Default");
61 }
62 return DEFAULT;
63 }
64 }
65
66 /**
67 * Sets the default SSLContext instance as returned by {@link
68 * #getDefault()} to a non-null initialized value.
69 *
70 * @throws NullPointerException on a null argument
71 * @since 1.6
72 */
73 public static void setDefault(SSLContext sslContext) {
74 if (sslContext == null) {
75 throw new NullPointerException("sslContext == null");
76 }
77 synchronized (ENGINE) {
78 DEFAULT = sslContext;
79 }
80 }
The Android Open Source Projectadc854b2009-03-03 19:28:47 -080081
The Android Open Source Projectadc854b2009-03-03 19:28:47 -080082 /**
83 * Creates a new {@code SSLContext} instance for the specified protocol.
Jesse Wilsonf9215792009-08-25 16:30:17 -070084 *
Alex Klyubind53b56b2013-12-13 12:57:12 -080085 * <p>The following protocols are supported:
86 * <table>
87 * <thead>
88 * <tr>
89 * <th>Protocol</th>
90 * <th>API Levels</th>
91 * </tr>
92 * </thead>
93 * <tbody>
94 * <tr>
95 * <td>Default</td>
96 * <td>9+</td>
97 * </tr>
98 * <tr>
99 * <td>SSL</td>
100 * <td>9+</td>
101 * </tr>
102 * <tr>
103 * <td>SSLv3</td>
104 * <td>9+</td>
105 * </tr>
106 * <tr>
107 * <td>TLS</td>
108 * <td>1+</td>
109 * </tr>
110 * <tr>
111 * <td>TLSv1</td>
112 * <td>1+</td>
113 * </tr>
114 * <tr>
115 * <td>TLSv1.1</td>
116 * <td>16+</td>
117 * </tr>
118 * <tr>
119 * <td>TLSv1.2</td>
120 * <td>16+</td>
121 * </tr>
122 * </tbody>
123 * </table>
124 *
The Android Open Source Projectadc854b2009-03-03 19:28:47 -0800125 * @param protocol
126 * the requested protocol to create a context for.
127 * @return the created {@code SSLContext} instance.
128 * @throws NoSuchAlgorithmException
129 * if no installed provider can provide the requested protocol
130 * @throws NullPointerException
131 * if {@code protocol} is {@code null} (instead of
132 * NoSuchAlgorithmException as in 1.4 release)
The Android Open Source Projectadc854b2009-03-03 19:28:47 -0800133 */
Jesse Wilsonf9215792009-08-25 16:30:17 -0700134 public static SSLContext getInstance(String protocol) throws NoSuchAlgorithmException {
The Android Open Source Projectadc854b2009-03-03 19:28:47 -0800135 if (protocol == null) {
Kenny Root86acc042012-09-12 10:32:58 -0700136 throw new NullPointerException("protocol == null");
The Android Open Source Projectadc854b2009-03-03 19:28:47 -0800137 }
Brian Carlstrom6cdb6b72010-10-19 11:27:15 -0700138 Engine.SpiAndProvider sap = ENGINE.getInstance(protocol, null);
139 return new SSLContext((SSLContextSpi) sap.spi, sap.provider, protocol);
The Android Open Source Projectadc854b2009-03-03 19:28:47 -0800140 }
141
142 /**
143 * Creates a new {@code SSLContext} instance for the specified protocol from
144 * the specified provider.
Jesse Wilsonf9215792009-08-25 16:30:17 -0700145 *
Alex Klyubind53b56b2013-12-13 12:57:12 -0800146 * <p>The following combinations are supported:
147 * <table>
148 * <thead>
149 * <tr>
150 * <th>Protocol</th>
151 * <th>Provider</th>
152 * <th>API Levels</th>
153 * </tr>
154 * </thead>
155 * <tbody>
156 * <tr>
157 * <td>Default</td>
158 * <td>AndroidOpenSSL</td>
159 * <td>9+</td>
160 * </tr>
161 * <tr>
162 * <td>SSL</td>
163 * <td>AndroidOpenSSL</td>
164 * <td>9+</td>
165 * </tr>
166 * <tr>
167 * <td>SSL</td>
168 * <td>HarmonyJSSE</td>
Alex Klyubin2cca77a2013-12-13 16:33:12 -0800169 * <td>9-19</td>
Alex Klyubind53b56b2013-12-13 12:57:12 -0800170 * </tr>
171 * <tr>
172 * <td>SSLv3</td>
173 * <td>AndroidOpenSSL</td>
174 * <td>9+</td>
175 * </tr>
176 * <tr>
177 * <td>SSLv3</td>
178 * <td>HarmonyJSSE</td>
Alex Klyubin2cca77a2013-12-13 16:33:12 -0800179 * <td>9-19</td>
Alex Klyubind53b56b2013-12-13 12:57:12 -0800180 * </tr>
181 * <tr>
182 * <td>TLS</td>
183 * <td>AndroidOpenSSL</td>
184 * <td>9+</td>
185 * </tr>
186 * <tr>
187 * <td>TLS</td>
188 * <td>HarmonyJSSE</td>
Alex Klyubin2cca77a2013-12-13 16:33:12 -0800189 * <td>1-19</td>
Alex Klyubind53b56b2013-12-13 12:57:12 -0800190 * </tr>
191 * <tr>
192 * <td>TLSv1</td>
193 * <td>AndroidOpenSSL</td>
194 * <td>9+</td>
195 * </tr>
196 * <tr>
197 * <td>TLSv1</td>
198 * <td>HarmonyJSSE</td>
Alex Klyubin2cca77a2013-12-13 16:33:12 -0800199 * <td>1-19</td>
Alex Klyubind53b56b2013-12-13 12:57:12 -0800200 * </tr>
201 * <tr>
202 * <td>TLSv1.1</td>
203 * <td>AndroidOpenSSL</td>
204 * <td>16+</td>
205 * </tr>
206 * <tr>
207 * <td>TLSv1.2</td>
208 * <td>AndroidOpenSSL</td>
209 * <td>16+</td>
210 * </tr>
211 * </tbody>
212 * </table>
213 *
214 * <p><strong>NOTE:</strong> The best practice is to rely on platform
215 * defaults rather than explicitly specify a provider.
216 * {@link #getDefault()} and {@link #getInstance(String)} are normally
217 * preferred over this method.
218 *
The Android Open Source Projectadc854b2009-03-03 19:28:47 -0800219 * @param protocol
220 * the requested protocol to create a context for.
221 * @param provider
222 * the name of the provider that provides the requested protocol.
223 * @return an {@code SSLContext} for the requested protocol.
224 * @throws NoSuchAlgorithmException
225 * if the specified provider cannot provider the requested
226 * protocol.
227 * @throws NoSuchProviderException
228 * if the specified provider does not exits.
229 * @throws NullPointerException
230 * if {@code protocol} is {@code null} (instead of
231 * NoSuchAlgorithmException as in 1.4 release)
The Android Open Source Projectadc854b2009-03-03 19:28:47 -0800232 */
233 public static SSLContext getInstance(String protocol, String provider)
234 throws NoSuchAlgorithmException, NoSuchProviderException {
235 if (provider == null) {
236 throw new IllegalArgumentException("Provider is null");
237 }
238 if (provider.length() == 0) {
239 throw new IllegalArgumentException("Provider is empty");
240 }
241 Provider impProvider = Security.getProvider(provider);
242 if (impProvider == null) {
243 throw new NoSuchProviderException(provider);
244 }
245 return getInstance(protocol, impProvider);
246 }
247
248 /**
249 * Creates a new {@code SSLContext} instance for the specified protocol from
250 * the specified provider.
Jesse Wilsonf9215792009-08-25 16:30:17 -0700251 *
The Android Open Source Projectadc854b2009-03-03 19:28:47 -0800252 * @param protocol
253 * the requested protocol to create a context for
254 * @param provider
255 * the provider that provides the requested protocol.
256 * @return an {@code SSLContext} for the requested protocol.
257 * @throws NoSuchAlgorithmException
258 * if the specified provider cannot provide the requested
259 * protocol.
260 * @throws NullPointerException
261 * if {@code protocol} is {@code null} (instead of
262 * NoSuchAlgorithmException as in 1.4 release)
The Android Open Source Projectadc854b2009-03-03 19:28:47 -0800263 */
264 public static SSLContext getInstance(String protocol, Provider provider)
265 throws NoSuchAlgorithmException {
266 if (provider == null) {
267 throw new IllegalArgumentException("provider is null");
268 }
269 if (protocol == null) {
Kenny Root86acc042012-09-12 10:32:58 -0700270 throw new NullPointerException("protocol == null");
The Android Open Source Projectadc854b2009-03-03 19:28:47 -0800271 }
Brian Carlstrom6cdb6b72010-10-19 11:27:15 -0700272 Object spi = ENGINE.getInstance(protocol, provider, null);
273 return new SSLContext((SSLContextSpi) spi, provider, protocol);
The Android Open Source Projectadc854b2009-03-03 19:28:47 -0800274 }
275
Jesse Wilsonf9215792009-08-25 16:30:17 -0700276 private final Provider provider;
277
278 private final SSLContextSpi spiImpl;
279
280 private final String protocol;
281
282 /**
283 * Creates a new {@code SSLContext}.
284 *
285 * @param contextSpi
286 * the implementation delegate.
287 * @param provider
288 * the provider.
289 * @param protocol
290 * the protocol name.
291 */
292 protected SSLContext(SSLContextSpi contextSpi, Provider provider, String protocol) {
293 this.provider = provider;
294 this.protocol = protocol;
295 this.spiImpl = contextSpi;
296 }
297
The Android Open Source Projectadc854b2009-03-03 19:28:47 -0800298 /**
299 * Returns the name of the secure socket protocol of this instance.
Jesse Wilsonf9215792009-08-25 16:30:17 -0700300 *
The Android Open Source Projectadc854b2009-03-03 19:28:47 -0800301 * @return the name of the secure socket protocol of this instance.
The Android Open Source Projectadc854b2009-03-03 19:28:47 -0800302 */
303 public final String getProtocol() {
304 return protocol;
305 }
306
307 /**
308 * Returns the provider of this {@code SSLContext} instance.
Jesse Wilsonf9215792009-08-25 16:30:17 -0700309 *
The Android Open Source Projectadc854b2009-03-03 19:28:47 -0800310 * @return the provider of this {@code SSLContext} instance.
The Android Open Source Projectadc854b2009-03-03 19:28:47 -0800311 */
312 public final Provider getProvider() {
313 return provider;
314 }
315
316 /**
Alex Klyubin668fb552014-03-24 11:43:01 -0700317 * Initializes this {@code SSLContext} instance. Three aspects of the context can be configured
318 * during initialization:
319 * <ul>
320 * <li>Providers of key material for key exchange and peer authentication
321 * ({@link KeyManager} instances),</li>
322 * <li>Providers of trust decisions about peers ({@link TrustManager} instances),
323 * </li>
324 * <li>Provider of randomness ({@link SecureRandom} instance).</li>
325 * </ul>
326 *
327 * <p>For each type of {@code KeyManager} or {@code TrustManager} used by this context, only the
328 * first matching instance from {@code km} or {@code tm} will be used. For example, only the
329 * first instance of {@link X509TrustManager} from {@code tm} will be used.
330 *
331 * <p>For any parameter set to {@code null} defaults will be used. In that case, the installed
332 * security providers will be searched for the highest priority implementation of the required
333 * primitives. For {@code km} and {@code tm}, the highest priority implementation
334 * of {@link KeyManagerFactory} and {@link TrustManagerFactory} will be used to obtain the
335 * required types of {@code KeyManager} and {@code TrustManager}. For {@code sr}, the default
336 * {@code SecureRandom} implementation will be used.
Jesse Wilsonf9215792009-08-25 16:30:17 -0700337 *
The Android Open Source Projectadc854b2009-03-03 19:28:47 -0800338 * @param km
Alex Klyubin668fb552014-03-24 11:43:01 -0700339 * the key sources or {@code null} for default.
The Android Open Source Projectadc854b2009-03-03 19:28:47 -0800340 * @param tm
Alex Klyubin668fb552014-03-24 11:43:01 -0700341 * the trust decision sources or {@code null} for default.
The Android Open Source Projectadc854b2009-03-03 19:28:47 -0800342 * @param sr
Alex Klyubin668fb552014-03-24 11:43:01 -0700343 * the randomness source or {@code null} for default.
The Android Open Source Projectadc854b2009-03-03 19:28:47 -0800344 * @throws KeyManagementException
345 * if initializing this instance fails.
The Android Open Source Projectadc854b2009-03-03 19:28:47 -0800346 */
347 public final void init(KeyManager[] km, TrustManager[] tm, SecureRandom sr)
348 throws KeyManagementException {
349 spiImpl.engineInit(km, tm, sr);
350 }
351
352 /**
353 * Returns a socket factory for this instance.
Jesse Wilsonf9215792009-08-25 16:30:17 -0700354 *
The Android Open Source Projectadc854b2009-03-03 19:28:47 -0800355 * @return a socket factory for this instance.
The Android Open Source Projectadc854b2009-03-03 19:28:47 -0800356 */
357 public final SSLSocketFactory getSocketFactory() {
358 return spiImpl.engineGetSocketFactory();
359 }
360
361 /**
362 * Returns a server socket factory for this instance.
Jesse Wilsonf9215792009-08-25 16:30:17 -0700363 *
The Android Open Source Projectadc854b2009-03-03 19:28:47 -0800364 * @return a server socket factory for this instance.
The Android Open Source Projectadc854b2009-03-03 19:28:47 -0800365 */
366 public final SSLServerSocketFactory getServerSocketFactory() {
367 return spiImpl.engineGetServerSocketFactory();
368 }
369
370 /**
371 * Creates an {@code SSLEngine} instance from this context.
Jesse Wilsonf9215792009-08-25 16:30:17 -0700372 *
The Android Open Source Projectadc854b2009-03-03 19:28:47 -0800373 * @return an {@code SSLEngine} instance from this context.
374 * @throws UnsupportedOperationException
375 * if the provider does not support the operation.
The Android Open Source Projectadc854b2009-03-03 19:28:47 -0800376 */
377 public final SSLEngine createSSLEngine() {
378 return spiImpl.engineCreateSSLEngine();
379 }
380
381 /**
382 * Creates an {@code SSLEngine} instance from this context with the
383 * specified hostname and port.
Jesse Wilsonf9215792009-08-25 16:30:17 -0700384 *
The Android Open Source Projectadc854b2009-03-03 19:28:47 -0800385 * @param peerHost
386 * the name of the host
387 * @param peerPort
388 * the port
389 * @return an {@code SSLEngine} instance from this context.
390 * @throws UnsupportedOperationException
391 * if the provider does not support the operation.
The Android Open Source Projectadc854b2009-03-03 19:28:47 -0800392 */
393 public final SSLEngine createSSLEngine(String peerHost, int peerPort) {
394 return spiImpl.engineCreateSSLEngine(peerHost, peerPort);
395 }
396
397 /**
398 * Returns the SSL session context that encapsulates the set of SSL sessions
399 * that can be used for handshake of server-side SSL sockets.
Jesse Wilsonf9215792009-08-25 16:30:17 -0700400 *
The Android Open Source Projectadc854b2009-03-03 19:28:47 -0800401 * @return the SSL server session context for this context or {@code null}
402 * if the underlying provider does not provide an implementation of
403 * the {@code SSLSessionContext} interface.
The Android Open Source Projectadc854b2009-03-03 19:28:47 -0800404 */
405 public final SSLSessionContext getServerSessionContext() {
406 return spiImpl.engineGetServerSessionContext();
407 }
408
409 /**
410 * Returns the SSL session context that encapsulates the set of SSL sessions
411 * that can be used for handshake of client-side SSL sockets.
Jesse Wilsonf9215792009-08-25 16:30:17 -0700412 *
The Android Open Source Projectadc854b2009-03-03 19:28:47 -0800413 * @return the SSL client session context for this context or {@code null}
414 * if the underlying provider does not provide an implementation of
415 * the {@code SSLSessionContext} interface.
The Android Open Source Projectadc854b2009-03-03 19:28:47 -0800416 */
417 public final SSLSessionContext getClientSessionContext() {
418 return spiImpl.engineGetClientSessionContext();
419 }
Brian Carlstrom0c131a22010-05-20 15:27:31 -0700420
421 /**
422 * Returns the default SSL handshake parameters for SSLSockets
423 * created by this SSLContext.
424 *
425 * @throws UnsupportedOperationException
426 * @since 1.6
427 */
428 public final SSLParameters getDefaultSSLParameters() {
429 return spiImpl.engineGetDefaultSSLParameters();
430 }
431
432 /**
433 * Returns SSL handshake parameters for SSLSockets that includes
434 * all supported cipher suites and protocols.
435 *
436 * @throws UnsupportedOperationException
437 * @since 1.6
438 */
439 public final SSLParameters getSupportedSSLParameters() {
440 return spiImpl.engineGetSupportedSSLParameters();
441 }
Jesse Wilsonf9215792009-08-25 16:30:17 -0700442}