Module java.base
Package javax.net.ssl

Class SSLContext

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

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

Every implementation of the Java platform is required to support the following standard SSLContext protocol:

  • TLSv1.2
This protocol is described in the SSLContext section of the Java Security Standard Algorithm Names Specification. Consult the release documentation for your implementation to see if any other protocols are supported.

Since:
1.4
  • Constructor Details

    • SSLContext

      protected SSLContext(SSLContextSpi contextSpi, Provider provider, String protocol)
      Creates an SSLContext object.
      Parameters:
      contextSpi - the delegate
      provider - the provider
      protocol - the protocol
  • Method Details

    • getDefault

      public static SSLContext getDefault() throws NoSuchAlgorithmException
      Returns the default SSL context.

      If a default context was set using the SSLContext.setDefault() 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.

      Returns:
      the default SSL context
      Throws:
      NoSuchAlgorithmException - if the SSLContext.getInstance() call fails
      Since:
      1.6
    • setDefault

      public static void setDefault(SSLContext context)
      Sets the default SSL context. It will be returned by subsequent calls to getDefault(). The default context must be immediately usable and not require initialization.
      Parameters:
      context - the SSLContext
      Throws:
      NullPointerException - if context is null
      SecurityException - if a security manager exists and its checkPermission method does not allow SSLPermission("setDefaultSSLContext")
      Since:
      1.6
    • getInstance

      public static SSLContext getInstance(String protocol) throws NoSuchAlgorithmException
      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 Security.getProviders() method.

      Implementation Note:
      The JDK Reference Implementation additionally uses the jdk.security.provider.preferred Security property to determine the preferred provider order for the specified algorithm. This may be different from the order of providers returned by Security.getProviders().
      Parameters:
      protocol - the standard name of the requested protocol. See the SSLContext section in the Java Security Standard Algorithm Names Specification for information about standard protocol names.
      Returns:
      the new SSLContext object
      Throws:
      NoSuchAlgorithmException - if no Provider supports a SSLContextSpi implementation for the specified protocol
      NullPointerException - if protocol is null
      See Also:
    • getInstance

      public static SSLContext getInstance(String protocol, String provider) throws NoSuchAlgorithmException, NoSuchProviderException
      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 Security.getProviders() method.

      Parameters:
      protocol - the standard name of the requested protocol. See the SSLContext section in the Java Security Standard Algorithm Names Specification for information about standard protocol names.
      provider - the name of the provider.
      Returns:
      the new SSLContext object
      Throws:
      IllegalArgumentException - if the provider name is null or empty
      NoSuchAlgorithmException - if a SSLContextSpi implementation for the specified protocol is not available from the specified provider
      NoSuchProviderException - if the specified provider is not registered in the security provider list
      NullPointerException - if protocol is null
      See Also:
    • getInstance

      public static SSLContext getInstance(String protocol, Provider provider) throws NoSuchAlgorithmException
      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.

      Parameters:
      protocol - the standard name of the requested protocol. See the SSLContext section in the Java Security Standard Algorithm Names Specification for information about standard protocol names.
      provider - an instance of the provider.
      Returns:
      the new SSLContext object
      Throws: