Added in [API level 1](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels) Summary: [Protected Ctors](https://developer.android.com/reference/javax/net/ssl/SSLContext#proctors) \| [Methods](https://developer.android.com/reference/javax/net/ssl/SSLContext#pubmethods) \| [Inherited Methods](https://developer.android.com/reference/javax/net/ssl/SSLContext#inhmethods)

# SSLContext

` public class SSLContext `   
` extends https://developer.android.com/reference/java/lang/Object ` ` `

|---|---|
| [java.lang.Object](https://developer.android.com/reference/java/lang/Object) ||
| ↳ | javax.net.ssl.SSLContext |

<br />

*** ** * ** ***

Instances of this class represent a secure socket protocol implementation which acts as a factory for secure socket factories or `SSLEngine`s. This class is initialized with an optional set of key and trust managers and source of secure random bytes.

Android provides the following `SSLContext` protocols:

| Algorithm | Supported API Levels |
|---|---|
| Default | 10+ |
| SSL | 10+ |
| SSLv3 | 10-25 |
| TLS | 1+ |
| TLSv1 | 10+ |
| TLSv1.1 | 16+ |
| TLSv1.2 | 16+ |
| TLSv1.3 | 29+ |

This protocol is described in the [SSLContext section](https://docs.oracle.com/javase/8/docs/technotes/guides/security/StandardNames.html#SSLContext) of the Java Cryptography Architecture Standard Algorithm Name Documentation.

<br />

## Summary

| ### Protected constructors ||
|---|---|
| ` https://developer.android.com/reference/javax/net/ssl/SSLContext#SSLContext(javax.net.ssl.SSLContextSpi,%20java.security.Provider,%20java.lang.String)(https://developer.android.com/reference/javax/net/ssl/SSLContextSpi contextSpi, https://developer.android.com/reference/java/security/Provider provider, https://developer.android.com/reference/java/lang/String protocol) ` Creates an SSLContext object. |

| ### Public methods ||
|---|---|
| ` final https://developer.android.com/reference/javax/net/ssl/SSLEngine` | ` https://developer.android.com/reference/javax/net/ssl/SSLContext#createSSLEngine()() ` Creates a new `SSLEngine` using this context. |
| ` final https://developer.android.com/reference/javax/net/ssl/SSLEngine` | ` https://developer.android.com/reference/javax/net/ssl/SSLContext#createSSLEngine(java.lang.String,%20int)(https://developer.android.com/reference/java/lang/String peerHost, int peerPort) ` Creates a new `SSLEngine` using this context using advisory peer information. |
| ` final https://developer.android.com/reference/javax/net/ssl/SSLSessionContext` | ` https://developer.android.com/reference/javax/net/ssl/SSLContext#getClientSessionContext()() ` Returns the client session context, which represents the set of SSL sessions available for use during the handshake phase of client-side SSL sockets. |
| ` static https://developer.android.com/reference/javax/net/ssl/SSLContext` | ` https://developer.android.com/reference/javax/net/ssl/SSLContext#getDefault()() ` Returns the default SSL context. |
| ` final https://developer.android.com/reference/javax/net/ssl/SSLParameters` | ` https://developer.android.com/reference/javax/net/ssl/SSLContext#getDefaultSSLParameters()() ` Returns a copy of the SSLParameters indicating the default settings for this SSL context. |
| ` static https://developer.android.com/reference/javax/net/ssl/SSLContext` | ` https://developer.android.com/reference/javax/net/ssl/SSLContext#getInstance(java.lang.String)(https://developer.android.com/reference/java/lang/String protocol) ` Returns a `SSLContext` object that implements the specified secure socket protocol. |
| ` static https://developer.android.com/reference/javax/net/ssl/SSLContext` | ` https://developer.android.com/reference/javax/net/ssl/SSLContext#getInstance(java.lang.String,%20java.lang.String)(https://developer.android.com/reference/java/lang/String protocol, https://developer.android.com/reference/java/lang/String provider) ` Returns a `SSLContext` object that implements the specified secure socket protocol. |
| ` static https://developer.android.com/reference/javax/net/ssl/SSLContext` | ` https://developer.android.com/reference/javax/net/ssl/SSLContext#getInstance(java.lang.String,%20java.security.Provider)(https://developer.android.com/reference/java/lang/String protocol, https://developer.android.com/reference/java/security/Provider provider) ` Returns a `SSLContext` object that implements the specified secure socket protocol. |
| ` final https://developer.android.com/reference/java/lang/String` | ` https://developer.android.com/reference/javax/net/ssl/SSLContext#getProtocol()() ` Returns the protocol name of this `SSLContext` object. |
| ` final https://developer.android.com/reference/java/security/Provider` | ` https://developer.android.com/reference/javax/net/ssl/SSLContext#getProvider()() ` Returns the provider of this `SSLContext` object. |
| ` final https://developer.android.com/reference/javax/net/ssl/SSLSessionContext` | ` https://developer.android.com/reference/javax/net/ssl/SSLContext#getServerSessionContext()() ` Returns the server session context, which represents the set of SSL sessions available for use during the handshake phase of server-side SSL sockets. |
| ` final https://developer.android.com/reference/javax/net/ssl/SSLServerSocketFactory` | ` https://developer.android.com/reference/javax/net/ssl/SSLContext#getServerSocketFactory()() ` Returns a `ServerSocketFactory` object for this context. |
| ` final https://developer.android.com/reference/javax/net/ssl/SSLSocketFactory` | ` https://developer.android.com/reference/javax/net/ssl/SSLContext#getSocketFactory()() ` Returns a `SocketFactory` object for this context. |
| ` final https://developer.android.com/reference/javax/net/ssl/SSLParameters` | ` https://developer.android.com/reference/javax/net/ssl/SSLContext#getSupportedSSLParameters()() ` Returns a copy of the SSLParameters indicating the supported settings for this SSL context. |
| ` final void` | ` https://developer.android.com/reference/javax/net/ssl/SSLContext#init(javax.net.ssl.KeyManager[],%20javax.net.ssl.TrustManager[],%20java.security.SecureRandom)(https://developer.android.com/reference/javax/net/ssl/KeyManager km, https://developer.android.com/reference/javax/net/ssl/TrustManager tm, https://developer.android.com/reference/java/security/SecureRandom random) ` Initializes this context. |
| ` static void` | ` https://developer.android.com/reference/javax/net/ssl/SSLContext#setDefault(javax.net.ssl.SSLContext)(https://developer.android.com/reference/javax/net/ssl/SSLContext context) ` Sets the default SSL context. |

### Inherited methods

From class ` https://developer.android.com/reference/java/lang/Object ` ` https://developer.android.com/reference/java/lang/Object` ` https://developer.android.com/reference/java/lang/Object#clone()() `

Creates and returns a copy of this object.
` boolean` ` https://developer.android.com/reference/java/lang/Object#equals(java.lang.Object)(https://developer.android.com/reference/java/lang/Object obj) `

Indicates whether some other object is "equal to" this one.
` void` ` https://developer.android.com/reference/java/lang/Object#finalize()() `

Called by the garbage collector on an object when garbage collection determines that there are no more references to the object.
` final https://developer.android.com/reference/java/lang/Class<?>` ` https://developer.android.com/reference/java/lang/Object#getClass()() `

Returns the runtime class of this `Object`.
` int` ` https://developer.android.com/reference/java/lang/Object#hashCode()() `

Returns a hash code value for the object.
` final void` ` https://developer.android.com/reference/java/lang/Object#notify()() `

Wakes up a single thread that is waiting on this object's monitor.
` final void` ` https://developer.android.com/reference/java/lang/Object#notifyAll()() `

Wakes up all threads that are waiting on this object's monitor.
` https://developer.android.com/reference/java/lang/String` ` https://developer.android.com/reference/java/lang/Object#toString()() `

Returns a string representation of the object.
` final void` ` https://developer.android.com/reference/java/lang/Object#wait(long,%20int)(long timeoutMillis, int nanos) `

Causes the current thread to wait until it is awakened, typically by being *notified* or *interrupted*, or until a certain amount of real time has elapsed.
` final void` ` https://developer.android.com/reference/java/lang/Object#wait(long)(long timeoutMillis) `

Causes the current thread to wait until it is awakened, typically by being *notified* or *interrupted*, or until a certain amount of real time has elapsed.
` final void` ` https://developer.android.com/reference/java/lang/Object#wait()() `

Causes the current thread to wait until it is awakened, typically by being *notified* or *interrupted*.

## Protected constructors

### SSLContext

Added in [API level 1](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
protected SSLContext (SSLContextSpi contextSpi, 
                Provider provider, 
                String protocol)
```

Creates an SSLContext object.

<br />

| Parameters ||
|---|---|
| `contextSpi` | `SSLContextSpi`: the delegate <br /> |
| `provider` | `Provider`: the provider <br /> |
| `protocol` | `String`: the protocol <br /> |

## Public methods

### createSSLEngine

Added in [API level 1](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
public final SSLEngine createSSLEngine ()
```

Creates a new `SSLEngine` using this context.

Applications using this factory method are providing no hints for an internal session reuse strategy. If hints are desired, `https://developer.android.com/reference/javax/net/ssl/SSLContext#createSSLEngine(java.lang.String,%20int)` should be used instead.

Some cipher suites (such as Kerberos) require remote hostname information, in which case this factory method should not be used.

<br />

| Returns ||
|---|---|
| `https://developer.android.com/reference/javax/net/ssl/SSLEngine` | the `SSLEngine` object <br /> |

| Throws ||
|---|---|
| `https://developer.android.com/reference/java/lang/IllegalStateException` | if the SSLContextImpl requires initialization and the `init()` has not been called |
| `https://developer.android.com/reference/java/lang/UnsupportedOperationException` | if the underlying provider does not implement the operation. |

### createSSLEngine

Added in [API level 1](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
public final SSLEngine createSSLEngine (String peerHost, 
                int peerPort)
```

Creates a new `SSLEngine` using this context using advisory peer information.

Applications using this factory method are providing hints for an internal session reuse strategy.

Some cipher suites (such as Kerberos) require remote hostname information, in which case peerHost needs to be specified.

<br />

| Parameters ||
|---|---|
| `peerHost` | `String`: the non-authoritative name of the host <br /> |
| `peerPort` | `int`: the non-authoritative port <br /> |

| Returns ||
|---|---|
| `https://developer.android.com/reference/javax/net/ssl/SSLEngine` | the new `SSLEngine` object <br /> |

| Throws ||
|---|---|
| `https://developer.android.com/reference/java/lang/IllegalStateException` | if the SSLContextImpl requires initialization and the `init()` has not been called |
| `https://developer.android.com/reference/java/lang/UnsupportedOperationException` | if the underlying provider does not implement the operation. |

### getClientSessionContext

Added in [API level 1](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
public final SSLSessionContext getClientSessionContext ()
```

Returns the client session context, which represents the set of SSL sessions available for use during the handshake phase of client-side SSL sockets.

This context may be unavailable in some environments, in which case this method returns null. For example, when the underlying SSL provider does not provide an implementation of SSLSessionContext interface, this method returns null. A non-null session context is returned otherwise.

<br />

| Returns ||
|---|---|
| `https://developer.android.com/reference/javax/net/ssl/SSLSessionContext` | client session context bound to this SSL context <br /> |

### getDefault

Added in [API level 9](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
public static SSLContext getDefault ()
```

Returns the default SSL context.

If a default context was set using the `https://developer.android.com/reference/javax/net/ssl/SSLContext#setDefault(javax.net.ssl.SSLContext)` method, it is returned. Otherwise, the first call of this method triggers the call `SSLContext.getInstance("Default")`. If successful, that object is made the default SSL context and returned.

The default context is immediately usable and does not require [initialization](https://developer.android.com/reference/javax/net/ssl/SSLContext#init(javax.net.ssl.KeyManager[],%20javax.net.ssl.TrustManager[],%20java.security.SecureRandom)).

<br />

| Returns ||
|---|---|
| `https://developer.android.com/reference/javax/net/ssl/SSLContext` | the default SSL context <br /> |

| Throws ||
|---|---|
| `https://developer.android.com/reference/java/security/NoSuchAlgorithmException` | if the `https://developer.android.com/reference/javax/net/ssl/SSLContext#getInstance(java.lang.String)` call fails |

### getDefaultSSLParameters

Added in [API level 9](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
public final SSLParameters getDefaultSSLParameters ()
```

Returns a copy of the SSLParameters indicating the default settings for this SSL context.

The parameters will always have the ciphersuites and protocols arrays set to non-null values.

<br />

| Returns ||
|---|---|
| `https://developer.android.com/reference/javax/net/ssl/SSLParameters` | a copy of the SSLParameters object with the default settings <br /> |

| Throws ||
|---|---|
| `https://developer.android.com/reference/java/lang/UnsupportedOperationException` | if the default SSL parameters could not be obtained. |

### getInstance

Added in [API level 1](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
public static SSLContext getInstance (String protocol)
```

Returns a `SSLContext` object that implements the specified secure socket protocol.

This method traverses the list of registered security Providers, starting with the most preferred Provider. A new SSLContext object encapsulating the SSLContextSpi implementation from the first Provider that supports the specified protocol is returned.

Note that the list of registered providers may be retrieved via the `https://developer.android.com/reference/java/security/Security#getProviders()` method.

<br />

| Parameters ||
|---|---|
| `protocol` | `String`: the standard name of the requested protocol. See the SSLContext section in the [Java Cryptography Architecture Standard Algorithm Name Documentation](https://docs.oracle.com/javase/8/docs/technotes/guides/security/StandardNames.html#SSLContext) for information about standard protocol names. <br /> |

| Returns ||
|---|---|
| `https://developer.android.com/reference/javax/net/ssl/SSLContext` | the new `SSLContext` object. <br /> |

| Throws ||
|---|---|
| `https://developer.android.com/reference/java/lang/NullPointerException` | if protocol is null. |
| `https://developer.android.com/reference/java/security/NoSuchAlgorithmException` | if no Provider supports a SSLContextSpi implementation for the specified protocol. |

**See also:**

- `https://developer.android.com/reference/java/security/Provider`

### getInstance

Added in [API level 1](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
public static SSLContext getInstance (String protocol, 
                String provider)
```

Returns a `SSLContext` object that implements the specified secure socket protocol.

A new SSLContext object encapsulating the SSLContextSpi implementation from the specified provider is returned. The specified provider must be registered in the security provider list.

Note that the list of registered providers may be retrieved via the `https://developer.android.com/reference/java/security/Security#getProviders()` method.

<br />

| Parameters ||
|---|---|
| `protocol` | `String`: the standard name of the requested protocol. See the SSLContext section in the [Java Cryptography Architecture Standard Algorithm Name Documentation](https://docs.oracle.com/javase/8/docs/technotes/guides/security/StandardNames.html#SSLContext) for information about standard protocol names. <br /> |
| `provider` | `String`: the name of the provider. <br /> |

| Returns ||
|---|---|
| `https://developer.android.com/reference/javax/net/ssl/SSLContext` | the new `SSLContext` object. <br /> |

| Throws ||
|---|---|
| `https://developer.android.com/reference/java/lang/IllegalArgumentException` | if the provider name is null or empty. |
| `https://developer.android.com/reference/java/lang/NullPointerException` | if protocol is null. |
| `https://developer.android.com/reference/java/security/NoSuchAlgorithmException` | if a SSLContextSpi implementation for the specified protocol is not available from the specified provider. |
| `https://developer.android.com/reference/java/security/NoSuchProviderException` | if the specified provider is not registered in the security provider list. |

**See also:**

- `https://developer.android.com/reference/java/security/Provider`

### getInstance

Added in [API level 1](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
public static SSLContext getInstance (String protocol, 
                Provider provider)
```

Returns a `SSLContext` object that implements the specified secure socket protocol.

A new SSLContext object encapsulating the SSLContextSpi implementation from the specified Provider object is returned. Note that the specified Provider object does not have to be registered in the provider list.

<br />

| Parameters ||
|---|---|
| `protocol` | `String`: the standard name of the requested protocol. See the SSLContext section in the [Java Cryptography Architecture Standard Algorithm Name Documentation](https://docs.oracle.com/javase/8/docs/technotes/guides/security/StandardNames.html#SSLContext) for information about standard protocol names. <br /> |
| `provider` | `Provider`: an instance of the provider. <br /> |

| Returns ||
|---|---|
| `https://developer.android.com/reference/javax/net/ssl/SSLContext` | the new `SSLContext` object. <br /> |

| Throws ||
|---|---|
| `https://developer.android.com/reference/java/lang/IllegalArgumentException` | if the provider is null. |
| `https://developer.android.com/reference/java/lang/NullPointerException` | if protocol is null. |
| `https://developer.android.com/reference/java/security/NoSuchAlgorithmException` | if a SSLContextSpi implementation for the specified protocol is not available from the specified Provider object. |

**See also:**

- `https://developer.android.com/reference/java/security/Provider`

### getProtocol

Added in [API level 1](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
public final String getProtocol ()
```

Returns the protocol name of this `SSLContext` object.

This is the same name that was specified in one of the `getInstance` calls that created this `SSLContext` object.

<br />

| Returns ||
|---|---|
| `https://developer.android.com/reference/java/lang/String` | the protocol name of this `SSLContext` object. <br /> |

### getProvider

Added in [API level 1](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
public final Provider getProvider ()
```

Returns the provider of this `SSLContext` object.

<br />

| Returns ||
|---|---|
| `https://developer.android.com/reference/java/security/Provider` | the provider of this `SSLContext` object <br /> |

### getServerSessionContext

Added in [API level 1](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
public final SSLSessionContext getServerSessionContext ()
```

Returns the server session context, which represents the set of SSL sessions available for use during the handshake phase of server-side SSL sockets.

This context may be unavailable in some environments, in which case this method returns null. For example, when the underlying SSL provider does not provide an implementation of SSLSessionContext interface, this method returns null. A non-null session context is returned otherwise.

<br />

| Returns ||
|---|---|
| `https://developer.android.com/reference/javax/net/ssl/SSLSessionContext` | server session context bound to this SSL context <br /> |

### getServerSocketFactory

Added in [API level 1](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
public final SSLServerSocketFactory getServerSocketFactory ()
```

Returns a `ServerSocketFactory` object for this context.

<br />

| Returns ||
|---|---|
| `https://developer.android.com/reference/javax/net/ssl/SSLServerSocketFactory` | the `ServerSocketFactory` object <br /> |

| Throws ||
|---|---|
| `https://developer.android.com/reference/java/lang/IllegalStateException` | if the SSLContextImpl requires initialization and the `init()` has not been called |

### getSocketFactory

Added in [API level 1](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
public final SSLSocketFactory getSocketFactory ()
```

Returns a `SocketFactory` object for this context.

<br />

| Returns ||
|---|---|
| `https://developer.android.com/reference/javax/net/ssl/SSLSocketFactory` | the `SocketFactory` object <br /> |

| Throws ||
|---|---|
| `https://developer.android.com/reference/java/lang/IllegalStateException` | if the SSLContextImpl requires initialization and the `init()` has not been called |

### getSupportedSSLParameters

Added in [API level 9](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
public final SSLParameters getSupportedSSLParameters ()
```

Returns a copy of the SSLParameters indicating the supported settings for this SSL context.

The parameters will always have the ciphersuites and protocols arrays set to non-null values.

<br />

| Returns ||
|---|---|
| `https://developer.android.com/reference/javax/net/ssl/SSLParameters` | a copy of the SSLParameters object with the supported settings <br /> |

| Throws ||
|---|---|
| `https://developer.android.com/reference/java/lang/UnsupportedOperationException` | if the supported SSL parameters could not be obtained. |

### init

Added in [API level 1](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
public final void init (KeyManager[] km, 
                TrustManager[] tm, 
                SecureRandom random)
```

Initializes this context. Either of the first two parameters may be null in which case the installed security providers will be searched for the highest priority implementation of the appropriate factory. Likewise, the secure random parameter may be null in which case the default implementation will be used.

Only the first instance of a particular key and/or trust manager implementation type in the array is used. (For example, only the first javax.net.ssl.X509KeyManager in the array will be used.)

<br />

| Parameters ||
|---|---|
| `km` | `KeyManager`: the sources of authentication keys or null <br /> |
| `tm` | `TrustManager`: the sources of peer authentication trust decisions or null <br /> |
| `random` | `SecureRandom`: the source of randomness for this generator or null <br /> |

| Throws ||
|---|---|
| `https://developer.android.com/reference/java/security/KeyManagementException` | if this operation fails |

### setDefault

Added in [API level 9](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
public static void setDefault (SSLContext context)
```

Sets the default SSL context. It will be returned by subsequent calls to `https://developer.android.com/reference/javax/net/ssl/SSLContext#getDefault()`. The default context must be immediately usable and not require [initialization](https://developer.android.com/reference/javax/net/ssl/SSLContext#init(javax.net.ssl.KeyManager[],%20javax.net.ssl.TrustManager[],%20java.security.SecureRandom)).

Developers are *strongly* discouraged from changing the default `SSLContext` as it is used as the Android default for secure communication by APIs like `https://developer.android.com/reference/javax/net/ssl/SSLSocketFactory#getDefault()`, `https://developer.android.com/reference/javax/net/ssl/SSLServerSocketFactory#getDefault()` and `https://developer.android.com/reference/javax/net/ssl/HttpsURLConnection`.

<br />

| Parameters ||
|---|---|
| `context` | `SSLContext`: the SSLContext <br /> |

| Throws ||
|---|---|
| `https://developer.android.com/reference/java/lang/NullPointerException` | if context is null |
| `https://developer.android.com/reference/java/lang/SecurityException` | if a security manager exists and its `checkPermission` method does not allow `SSLPermission("setDefaultSSLContext")` |