Skip to content

Most visited

Recently visited

navigation
Added in API level 1

SSLContext

public class SSLContext
extends Object

java.lang.Object
   ↳ javax.net.ssl.SSLContext


The public API for secure socket protocol implementations. It acts as factory for SSLSocketFactory's and SSLEngines.

Summary

Protected constructors

SSLContext(SSLContextSpi contextSpi, Provider provider, String protocol)

Creates a new SSLContext.

Public methods

final SSLEngine createSSLEngine(String peerHost, int peerPort)

Creates an SSLEngine instance from this context with the specified hostname and port.

final SSLEngine createSSLEngine()

Creates an SSLEngine instance from this context.

final SSLSessionContext getClientSessionContext()

Returns the SSL session context that encapsulates the set of SSL sessions that can be used for handshake of client-side SSL sockets.

static SSLContext getDefault()

Returns the default SSLContext.

final SSLParameters getDefaultSSLParameters()

Returns the default SSL handshake parameters for SSLSockets created by this SSLContext.

static SSLContext getInstance(String protocol, String provider)

Creates a new SSLContext instance for the specified protocol from the specified provider.

static SSLContext getInstance(String protocol, Provider provider)

Creates a new SSLContext instance for the specified protocol from the specified provider.

static SSLContext getInstance(String protocol)

Creates a new SSLContext instance for the specified protocol.

final String getProtocol()

Returns the name of the secure socket protocol of this instance.

final Provider getProvider()

Returns the provider of this SSLContext instance.

final SSLSessionContext getServerSessionContext()

Returns the SSL session context that encapsulates the set of SSL sessions that can be used for handshake of server-side SSL sockets.

final SSLServerSocketFactory getServerSocketFactory()

Returns a server socket factory for this instance.

final SSLSocketFactory getSocketFactory()

Returns a socket factory for this instance.

final SSLParameters getSupportedSSLParameters()

Returns SSL handshake parameters for SSLSockets that includes all supported cipher suites and protocols.

final void init(KeyManager[] km, TrustManager[] tm, SecureRandom sr)

Initializes this SSLContext instance.

static void setDefault(SSLContext sslContext)

Sets the default SSLContext instance as returned by getDefault() to a non-null initialized value.

Inherited methods

From class java.lang.Object

Protected constructors

SSLContext

Added in API level 1
SSLContext (SSLContextSpi contextSpi, 
                Provider provider, 
                String protocol)

Creates a new SSLContext.

Parameters
contextSpi SSLContextSpi: the implementation delegate.
provider Provider: the provider.
protocol String: the protocol name.

Public methods

createSSLEngine

Added in API level 1
SSLEngine createSSLEngine (String peerHost, 
                int peerPort)

Creates an SSLEngine instance from this context with the specified hostname and port.

Parameters
peerHost String: the name of the host
peerPort int: the port
Returns
SSLEngine an SSLEngine instance from this context.
Throws
UnsupportedOperationException if the provider does not support the operation.

createSSLEngine

Added in API level 1
SSLEngine createSSLEngine ()

Creates an SSLEngine instance from this context.

Returns
SSLEngine an SSLEngine instance from this context.
Throws
UnsupportedOperationException if the provider does not support the operation.

getClientSessionContext

Added in API level 1
SSLSessionContext getClientSessionContext ()

Returns the SSL session context that encapsulates the set of SSL sessions that can be used for handshake of client-side SSL sockets.

Returns
SSLSessionContext the SSL client session context for this context or null if the underlying provider does not provide an implementation of the SSLSessionContext interface.

getDefault

Added in API level 9
SSLContext getDefault ()

Returns the default SSLContext. The default SSL context can be set with setDefault(SSLContext). If not, one will be created with SSLContext.getInstance("Default"), which will already be initialized.

Returns
SSLContext
Throws
NoSuchAlgorithmException if there is a problem creating the default instance.

getDefaultSSLParameters

Added in API level 9
SSLParameters getDefaultSSLParameters ()

Returns the default SSL handshake parameters for SSLSockets created by this SSLContext.

Returns
SSLParameters
Throws
UnsupportedOperationException

getInstance

Added in API level 1
SSLContext getInstance (String protocol, 
                String provider)

Creates a new SSLContext instance for the specified protocol from the specified provider.

The following combinations are supported:

Protocol Provider API Levels
Default AndroidOpenSSL 9+
SSL AndroidOpenSSL 9+
SSL HarmonyJSSE 9-19
SSLv3 AndroidOpenSSL 9+
SSLv3 HarmonyJSSE 9-19
TLS AndroidOpenSSL 9+
TLS HarmonyJSSE 1-19
TLSv1 AndroidOpenSSL 9+
TLSv1 HarmonyJSSE 1-19
TLSv1.1 AndroidOpenSSL 16+
TLSv1.2 AndroidOpenSSL 16+

NOTE: The best practice is to rely on platform defaults rather than explicitly specify a provider. getDefault() and getInstance(String) are normally preferred over this method.

Parameters
protocol String: the requested protocol to create a context for.
provider String: the name of the provider that provides the requested protocol.
Returns
SSLContext an SSLContext for the requested protocol.
Throws
NoSuchAlgorithmException if the specified provider cannot provider the requested protocol.
NoSuchProviderException if the specified provider does not exits.
NullPointerException if protocol is null (instead of NoSuchAlgorithmException as in 1.4 release)

getInstance

Added in API level 1
SSLContext getInstance (String protocol, 
                Provider provider)

Creates a new SSLContext instance for the specified protocol from the specified provider.

Parameters
protocol String: the requested protocol to create a context for
provider Provider: the provider that provides the requested protocol.
Returns
SSLContext an SSLContext for the requested protocol.
Throws
NoSuchAlgorithmException if the specified provider cannot provide the requested protocol.
NullPointerException if protocol is null (instead of NoSuchAlgorithmException as in 1.4 release)

getInstance

Added in API level 1
SSLContext getInstance (String protocol)

Creates a new SSLContext instance for the specified protocol.

The following protocols are supported:

Protocol API Levels
Default 9+
SSL 9+
SSLv3 9+
TLS 1+
TLSv1 1+
TLSv1.1 16+
TLSv1.2 16+

Parameters
protocol String: the requested protocol to create a context for.
Returns
SSLContext the created SSLContext instance.
Throws
NoSuchAlgorithmException if no installed provider can provide the requested protocol
NullPointerException if protocol is null (instead of NoSuchAlgorithmException as in 1.4 release)

getProtocol

Added in API level 1
String getProtocol ()

Returns the name of the secure socket protocol of this instance.

Returns
String the name of the secure socket protocol of this instance.

getProvider

Added in API level 1
Provider getProvider ()

Returns the provider of this SSLContext instance.

Returns
Provider the provider of this SSLContext instance.

getServerSessionContext

Added in API level 1
SSLSessionContext getServerSessionContext ()

Returns the SSL session context that encapsulates the set of SSL sessions that can be used for handshake of server-side SSL sockets.

Returns
SSLSessionContext the SSL server session context for this context or null if the underlying provider does not provide an implementation of the SSLSessionContext interface.

getServerSocketFactory

Added in API level 1
SSLServerSocketFactory getServerSocketFactory ()

Returns a server socket factory for this instance.

Returns
SSLServerSocketFactory a server socket factory for this instance.

getSocketFactory

Added in API level 1
SSLSocketFactory getSocketFactory ()

Returns a socket factory for this instance.

Returns
SSLSocketFactory a socket factory for this instance.

getSupportedSSLParameters

Added in API level 9
SSLParameters getSupportedSSLParameters ()

Returns SSL handshake parameters for SSLSockets that includes all supported cipher suites and protocols.

Returns
SSLParameters
Throws
UnsupportedOperationException

init

Added in API level 1
void init (KeyManager[] km, 
                TrustManager[] tm, 
                SecureRandom sr)

Initializes this SSLContext instance. Three aspects of the context can be configured during initialization:

  • Providers of key material for key exchange and peer authentication (KeyManager instances),
  • Providers of trust decisions about peers (TrustManager instances),
  • Provider of randomness (SecureRandom instance).

For each type of KeyManager or TrustManager used by this context, only the first matching instance from km or tm will be used. For example, only the first instance of X509TrustManager from tm will be used.

For any parameter set to null defaults will be used. In that case, the installed security providers will be searched for the highest priority implementation of the required primitives. For km and tm, the highest priority implementation of KeyManagerFactory and TrustManagerFactory will be used to obtain the required types of KeyManager and TrustManager. For sr, the default SecureRandom implementation will be used.

Parameters
km KeyManager: the key sources or null for default.
tm TrustManager: the trust decision sources or null for default.
sr SecureRandom: the randomness source or null for default.
Throws
KeyManagementException if initializing this instance fails.

setDefault

Added in API level 9
void setDefault (SSLContext sslContext)

Sets the default SSLContext instance as returned by getDefault() to a non-null initialized value.

Parameters
sslContext SSLContext
Throws
NullPointerException on a null argument
This site uses cookies to store your preferences for site-specific language and display options.

Hooray!

This class requires API level or higher

This doc is hidden because your selected API level for the documentation is . You can change the documentation API level with the selector above the left navigation.

For more information about specifying the API level your app requires, read Supporting Different Platform Versions.