The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 1 | /* |
| 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 | |
| 18 | package javax.net.ssl; |
| 19 | |
| 20 | import java.security.KeyManagementException; |
| 21 | import java.security.NoSuchAlgorithmException; |
| 22 | import java.security.NoSuchProviderException; |
| 23 | import java.security.Provider; |
| 24 | import java.security.SecureRandom; |
| 25 | import java.security.Security; |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 26 | import 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 Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 32 | */ |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 33 | public class SSLContext { |
| 34 | // StoreSSLContext service name |
| 35 | private static final String SERVICE = "SSLContext"; |
| 36 | |
| 37 | // Used to access common engine functionality |
Brian Carlstrom | 0c131a2 | 2010-05-20 15:27:31 -0700 | [diff] [blame] | 38 | 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 Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 81 | |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 82 | /** |
| 83 | * Creates a new {@code SSLContext} instance for the specified protocol. |
Jesse Wilson | f921579 | 2009-08-25 16:30:17 -0700 | [diff] [blame] | 84 | * |
Alex Klyubin | d53b56b | 2013-12-13 12:57:12 -0800 | [diff] [blame] | 85 | * <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 Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 125 | * @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 Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 133 | */ |
Jesse Wilson | f921579 | 2009-08-25 16:30:17 -0700 | [diff] [blame] | 134 | public static SSLContext getInstance(String protocol) throws NoSuchAlgorithmException { |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 135 | if (protocol == null) { |
Kenny Root | 86acc04 | 2012-09-12 10:32:58 -0700 | [diff] [blame] | 136 | throw new NullPointerException("protocol == null"); |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 137 | } |
Brian Carlstrom | 6cdb6b7 | 2010-10-19 11:27:15 -0700 | [diff] [blame] | 138 | Engine.SpiAndProvider sap = ENGINE.getInstance(protocol, null); |
| 139 | return new SSLContext((SSLContextSpi) sap.spi, sap.provider, protocol); |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 140 | } |
| 141 | |
| 142 | /** |
| 143 | * Creates a new {@code SSLContext} instance for the specified protocol from |
| 144 | * the specified provider. |
Jesse Wilson | f921579 | 2009-08-25 16:30:17 -0700 | [diff] [blame] | 145 | * |
Alex Klyubin | d53b56b | 2013-12-13 12:57:12 -0800 | [diff] [blame] | 146 | * <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 Klyubin | 2cca77a | 2013-12-13 16:33:12 -0800 | [diff] [blame] | 169 | * <td>9-19</td> |
Alex Klyubin | d53b56b | 2013-12-13 12:57:12 -0800 | [diff] [blame] | 170 | * </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 Klyubin | 2cca77a | 2013-12-13 16:33:12 -0800 | [diff] [blame] | 179 | * <td>9-19</td> |
Alex Klyubin | d53b56b | 2013-12-13 12:57:12 -0800 | [diff] [blame] | 180 | * </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 Klyubin | 2cca77a | 2013-12-13 16:33:12 -0800 | [diff] [blame] | 189 | * <td>1-19</td> |
Alex Klyubin | d53b56b | 2013-12-13 12:57:12 -0800 | [diff] [blame] | 190 | * </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 Klyubin | 2cca77a | 2013-12-13 16:33:12 -0800 | [diff] [blame] | 199 | * <td>1-19</td> |
Alex Klyubin | d53b56b | 2013-12-13 12:57:12 -0800 | [diff] [blame] | 200 | * </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 Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 219 | * @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 Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 232 | */ |
| 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 Wilson | f921579 | 2009-08-25 16:30:17 -0700 | [diff] [blame] | 251 | * |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 252 | * @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 Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 263 | */ |
| 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 Root | 86acc04 | 2012-09-12 10:32:58 -0700 | [diff] [blame] | 270 | throw new NullPointerException("protocol == null"); |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 271 | } |
Brian Carlstrom | 6cdb6b7 | 2010-10-19 11:27:15 -0700 | [diff] [blame] | 272 | Object spi = ENGINE.getInstance(protocol, provider, null); |
| 273 | return new SSLContext((SSLContextSpi) spi, provider, protocol); |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 274 | } |
| 275 | |
Jesse Wilson | f921579 | 2009-08-25 16:30:17 -0700 | [diff] [blame] | 276 | 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 Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 298 | /** |
| 299 | * Returns the name of the secure socket protocol of this instance. |
Jesse Wilson | f921579 | 2009-08-25 16:30:17 -0700 | [diff] [blame] | 300 | * |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 301 | * @return the name of the secure socket protocol of this instance. |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 302 | */ |
| 303 | public final String getProtocol() { |
| 304 | return protocol; |
| 305 | } |
| 306 | |
| 307 | /** |
| 308 | * Returns the provider of this {@code SSLContext} instance. |
Jesse Wilson | f921579 | 2009-08-25 16:30:17 -0700 | [diff] [blame] | 309 | * |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 310 | * @return the provider of this {@code SSLContext} instance. |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 311 | */ |
| 312 | public final Provider getProvider() { |
| 313 | return provider; |
| 314 | } |
| 315 | |
| 316 | /** |
| 317 | * Initializes this {@code SSLContext} instance. All of the arguments are |
| 318 | * optional, and the security providers will be searched for the required |
| 319 | * implementations of the needed algorithms. |
Jesse Wilson | f921579 | 2009-08-25 16:30:17 -0700 | [diff] [blame] | 320 | * |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 321 | * @param km |
| 322 | * the key sources or {@code null}. |
| 323 | * @param tm |
| 324 | * the trust decision sources or {@code null}. |
| 325 | * @param sr |
| 326 | * the randomness source or {@code null.} |
| 327 | * @throws KeyManagementException |
| 328 | * if initializing this instance fails. |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 329 | */ |
| 330 | public final void init(KeyManager[] km, TrustManager[] tm, SecureRandom sr) |
| 331 | throws KeyManagementException { |
| 332 | spiImpl.engineInit(km, tm, sr); |
| 333 | } |
| 334 | |
| 335 | /** |
| 336 | * Returns a socket factory for this instance. |
Jesse Wilson | f921579 | 2009-08-25 16:30:17 -0700 | [diff] [blame] | 337 | * |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 338 | * @return a socket factory for this instance. |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 339 | */ |
| 340 | public final SSLSocketFactory getSocketFactory() { |
| 341 | return spiImpl.engineGetSocketFactory(); |
| 342 | } |
| 343 | |
| 344 | /** |
| 345 | * Returns a server socket factory for this instance. |
Jesse Wilson | f921579 | 2009-08-25 16:30:17 -0700 | [diff] [blame] | 346 | * |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 347 | * @return a server socket factory for this instance. |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 348 | */ |
| 349 | public final SSLServerSocketFactory getServerSocketFactory() { |
| 350 | return spiImpl.engineGetServerSocketFactory(); |
| 351 | } |
| 352 | |
| 353 | /** |
| 354 | * Creates an {@code SSLEngine} instance from this context. |
Jesse Wilson | f921579 | 2009-08-25 16:30:17 -0700 | [diff] [blame] | 355 | * |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 356 | * @return an {@code SSLEngine} instance from this context. |
| 357 | * @throws UnsupportedOperationException |
| 358 | * if the provider does not support the operation. |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 359 | */ |
| 360 | public final SSLEngine createSSLEngine() { |
| 361 | return spiImpl.engineCreateSSLEngine(); |
| 362 | } |
| 363 | |
| 364 | /** |
| 365 | * Creates an {@code SSLEngine} instance from this context with the |
| 366 | * specified hostname and port. |
Jesse Wilson | f921579 | 2009-08-25 16:30:17 -0700 | [diff] [blame] | 367 | * |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 368 | * @param peerHost |
| 369 | * the name of the host |
| 370 | * @param peerPort |
| 371 | * the port |
| 372 | * @return an {@code SSLEngine} instance from this context. |
| 373 | * @throws UnsupportedOperationException |
| 374 | * if the provider does not support the operation. |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 375 | */ |
| 376 | public final SSLEngine createSSLEngine(String peerHost, int peerPort) { |
| 377 | return spiImpl.engineCreateSSLEngine(peerHost, peerPort); |
| 378 | } |
| 379 | |
| 380 | /** |
| 381 | * Returns the SSL session context that encapsulates the set of SSL sessions |
| 382 | * that can be used for handshake of server-side SSL sockets. |
Jesse Wilson | f921579 | 2009-08-25 16:30:17 -0700 | [diff] [blame] | 383 | * |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 384 | * @return the SSL server session context for this context or {@code null} |
| 385 | * if the underlying provider does not provide an implementation of |
| 386 | * the {@code SSLSessionContext} interface. |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 387 | */ |
| 388 | public final SSLSessionContext getServerSessionContext() { |
| 389 | return spiImpl.engineGetServerSessionContext(); |
| 390 | } |
| 391 | |
| 392 | /** |
| 393 | * Returns the SSL session context that encapsulates the set of SSL sessions |
| 394 | * that can be used for handshake of client-side SSL sockets. |
Jesse Wilson | f921579 | 2009-08-25 16:30:17 -0700 | [diff] [blame] | 395 | * |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 396 | * @return the SSL client session context for this context or {@code null} |
| 397 | * if the underlying provider does not provide an implementation of |
| 398 | * the {@code SSLSessionContext} interface. |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 399 | */ |
| 400 | public final SSLSessionContext getClientSessionContext() { |
| 401 | return spiImpl.engineGetClientSessionContext(); |
| 402 | } |
Brian Carlstrom | 0c131a2 | 2010-05-20 15:27:31 -0700 | [diff] [blame] | 403 | |
| 404 | /** |
| 405 | * Returns the default SSL handshake parameters for SSLSockets |
| 406 | * created by this SSLContext. |
| 407 | * |
| 408 | * @throws UnsupportedOperationException |
| 409 | * @since 1.6 |
| 410 | */ |
| 411 | public final SSLParameters getDefaultSSLParameters() { |
| 412 | return spiImpl.engineGetDefaultSSLParameters(); |
| 413 | } |
| 414 | |
| 415 | /** |
| 416 | * Returns SSL handshake parameters for SSLSockets that includes |
| 417 | * all supported cipher suites and protocols. |
| 418 | * |
| 419 | * @throws UnsupportedOperationException |
| 420 | * @since 1.6 |
| 421 | */ |
| 422 | public final SSLParameters getSupportedSSLParameters() { |
| 423 | return spiImpl.engineGetSupportedSSLParameters(); |
| 424 | } |
Jesse Wilson | f921579 | 2009-08-25 16:30:17 -0700 | [diff] [blame] | 425 | } |