Added in API level 1
Deprecated in API level 29

SSLCertificateSocketFactory


open class SSLCertificateSocketFactory : SSLSocketFactory
kotlin.Any
   ↳ javax.net.SocketFactory
   ↳ javax.net.ssl.SSLSocketFactory
   ↳ android.net.SSLCertificateSocketFactory

SSLSocketFactory implementation with several extra features:

  • Timeout specification for SSL handshake operations
  • Hostname verification in most cases (see WARNINGs below)
  • Optional SSL session caching with SSLSessionCache
  • Optionally bypass all SSL certificate checks
The handshake timeout does not apply to actual TCP socket connection. If you want a connection timeout as well, use createSocket() and Socket#connect(java.net.SocketAddress, int), after which you must verify the identity of the server you are connected to.

Most SSLSocketFactory implementations do not verify the server's identity, allowing person-in-the-middle attacks. This implementation does check the server's certificate hostname, but only for createSocket variants that specify a hostname. When using methods that use InetAddress or which return an unconnected socket, you MUST verify the server's identity yourself to ensure a secure connection. Refer to Updating Your Security Provider to Protect Against SSL Exploits for further information.

The recommended way to verify the server's identity is to use HttpsURLConnection#getDefaultHostnameVerifier() to get a HostnameVerifier to verify the certificate hostname.

Warning: Some methods on this class return connected sockets and some return unconnected sockets. For the methods that return connected sockets, setting connection- or handshake-related properties on those sockets will have no effect.

On development devices, "setprop socket.relaxsslcheck yes" bypasses all SSL certificate and hostname checks for testing purposes. This setting requires root access.

Summary

Public constructors
SSLCertificateSocketFactory(handshakeTimeoutMillis: Int)

Public methods
open Socket!
createSocket(k: Socket!, host: String!, port: Int, close: Boolean)

Returns a socket layered over an existing socket connected to the named host, at the given port.

open Socket!

Creates a new socket which is not connected to any remote host.

open Socket!
createSocket(addr: InetAddress!, port: Int, localAddr: InetAddress!, localPort: Int)

Creates a socket and connect it to the specified remote address on the specified remote port.

open Socket!
createSocket(addr: InetAddress!, port: Int)

Creates a socket and connects it to the specified port number at the specified address.

open Socket!
createSocket(host: String!, port: Int, localAddr: InetAddress!, localPort: Int)

Creates a socket and connects it to the specified remote host on the specified remote port.

open Socket!
createSocket(host: String!, port: Int)

Creates a socket and connects it to the specified remote host at the specified remote port.

open static SocketFactory!
getDefault(handshakeTimeoutMillis: Int)

Returns a new socket factory instance with an optional handshake timeout.

open static SSLSocketFactory!
getDefault(handshakeTimeoutMillis: Int, cache: SSLSessionCache!)

Returns a new socket factory instance with an optional handshake timeout and SSL session cache.

open Array<String!>!

open static SSLSocketFactory!
getInsecure(handshakeTimeoutMillis: Int, cache: SSLSessionCache!)

Returns a new instance of a socket factory with all SSL security checks disabled, using an optional handshake timeout and SSL session cache.

open ByteArray!

Returns the Next Protocol Negotiation (NPN) protocol selected by client and server, or null if no protocol was negotiated.

open Array<String!>!

open Unit
setHostname(socket: Socket!, hostName: String!)

Turns on Server Name Indication (SNI) on a given socket.

open Unit
setKeyManagers(keyManagers: Array<KeyManager!>!)

Sets the KeyManagers to be used for connections made by this factory.

open Unit
setNpnProtocols(npnProtocols: Array<ByteArray!>!)

Sets the Next Protocol Negotiation (NPN) protocols that this peer is interested in.

open Unit

Sets the TrustManagers to be used for connections made by this factory.

open Unit
setUseSessionTickets(socket: Socket!, useSessionTickets: Boolean)

Enables session ticket support on the given socket.

Inherited functions

Public constructors

SSLCertificateSocketFactory

Added in API level 1
SSLCertificateSocketFactory(handshakeTimeoutMillis: Int)

Deprecated: Use getDefault(int) instead.

Public methods

createSocket

Added in API level 1
open fun createSocket(
    k: Socket!,
    host: String!,
    port: Int,
    close: Boolean
): Socket!

Deprecated: Deprecated in Java.

Returns a socket layered over an existing socket connected to the named host, at the given port. This constructor can be used when tunneling SSL through a proxy or when negotiating the use of SSL over an existing socket. The host and port refer to the logical peer destination. This socket is configured using the socket options established for this factory.

By default, this method returns a connected socket and verifies the peer's certificate hostname after connecting using the HostnameVerifier obtained from HttpsURLConnection.getDefaultHostnameVerifier(); if this instance was created with getInsecure(int,android.net.SSLSessionCache), it returns a socket that is not connected instead.

Parameters
s the existing socket
host String!: the server host
port Int: the server port
autoClose close the underlying socket when this socket is closed
Return
Socket! a socket connected to the specified host and port
Exceptions
java.io.IOException if an I/O error occurs when creating the socket
java.lang.NullPointerException if the parameter s is null

createSocket

Added in API level 1
open fun createSocket(): Socket!

Deprecated: Deprecated in Java.

Creates a new socket which is not connected to any remote host. You must use java.net.Socket#connect to connect the socket.

Warning: Hostname verification is not performed with this method. You MUST verify the server's identity after connecting the socket to avoid person-in-the-middle attacks.

Return
Socket! the unconnected socket
Exceptions
java.io.IOException if the socket cannot be created

createSocket

Added in API level 1
open fun createSocket(
    addr: InetAddress!,
    port: Int,
    localAddr: InetAddress!,
    localPort: Int
): Socket!

Deprecated: Deprecated in Java.

Creates a socket and connect it to the specified remote address on the specified remote port. The socket will also be bound to the local address and port suplied. The socket is configured using the socket options established for this factory.

If there is a security manager, its checkConnect method is called with the host address and port as its arguments. This could result in a SecurityException.

This method returns a socket that is not connected.

Warning: Hostname verification is not performed with this method. You MUST verify the server's identity after connecting the socket to avoid person-in-the-middle attacks.

Parameters
address the server network address
port Int: the server port
localAddress the client network address
localPort Int: the client port
Return
Socket! the Socket
Exceptions
java.io.IOException if an I/O error occurs when creating the socket
java.lang.SecurityException if a security manager exists and its checkConnect method doesn't allow the operation.
java.lang.IllegalArgumentException if the port parameter or localPort parameter is outside the specified range of valid port values, which is between 0 and 65535, inclusive.
java.lang.NullPointerException if address is null.

createSocket

Added in API level 1
open fun createSocket(
    addr: InetAddress!,
    port: Int
): Socket!

Deprecated: Deprecated in Java.

Creates a socket and connects it to the specified port number at the specified address. This socket is configured using the socket options established for this factory.

If there is a security manager, its checkConnect method is called with the host address and port as its arguments. This could result in a SecurityException.

This method returns a socket that is not connected.

Warning: Hostname verification is not performed with this method. You MUST verify the server's identity after connecting the socket to avoid person-in-the-middle attacks.

Parameters
host the server host
port Int: the server port
Return
Socket! the Socket
Exceptions
java.io.IOException if an I/O error occurs when creating the socket
java.lang.SecurityException if a security manager exists and its checkConnect method doesn't allow the operation.
java.lang.IllegalArgumentException if the port parameter is outside the specified range of valid port values, which is between 0 and 65535, inclusive.
java.lang.NullPointerException if host is null.

createSocket

Added in API level 1
open fun createSocket(
    host: String!,
    port: Int,
    localAddr: InetAddress!,
    localPort: Int
): Socket!

Deprecated: Deprecated in Java.

Creates a socket and connects it to the specified remote host on the specified remote port. The socket will also be bound to the local address and port supplied. This socket is configured using the socket options established for this factory.

If there is a security manager, its checkConnect method is called with the host address and port as its arguments. This could result in a SecurityException.

By default, this method returns a connected socket and verifies the peer's certificate hostname after connecting using the HostnameVerifier obtained from HttpsURLConnection.getDefaultHostnameVerifier(); if this instance was created with getInsecure(int,android.net.SSLSessionCache), it returns a socket that is not connected instead.

Parameters
host String!: the server host name with which to connect, or null for the loopback address.
port Int: the server port
localHost the local address the socket is bound to
localPort Int: the local port the socket is bound to
Return
Socket! the Socket
Exceptions
java.io.IOException if an I/O error occurs when creating the socket
java.lang.SecurityException if a security manager exists and its checkConnect method doesn't allow the operation.
java.net.UnknownHostException if the host is not known
java.lang.IllegalArgumentException if the port parameter or localPort parameter is outside the specified range of valid port values, which is between 0 and 65535, inclusive.

createSocket

Added in API level 1
open fun createSocket(
    host: String!,
    port: Int
): Socket!

Deprecated: Deprecated in Java.

Creates a socket and connects it to the specified remote host at the specified remote port. This socket is configured using the socket options established for this factory.

If there is a security manager, its checkConnect method is called with the host address and port as its arguments. This could result in a SecurityException.

By default, this method returns a connected socket and verifies the peer's certificate hostname after connecting using the HostnameVerifier obtained from HttpsURLConnection.getDefaultHostnameVerifier(); if this instance was created with getInsecure(int,android.net.SSLSessionCache), it returns a socket that is not connected instead.

Parameters
host String!: the server host name with which to connect, or null for the loopback address.
port Int: the server port
Return
Socket! the Socket
Exceptions
java.io.IOException if an I/O error occurs when creating the socket
java.lang.SecurityException if a security manager exists and its checkConnect method doesn't allow the operation.
java.net.UnknownHostException if the host is not known
java.lang.IllegalArgumentException if the port parameter is outside the specified range of valid port values, which is between 0 and 65535, inclusive.

getDefault

Added in API level 1
open static fun getDefault(handshakeTimeoutMillis: Int): SocketFactory!

Deprecated: Deprecated in Java.

Returns a new socket factory instance with an optional handshake timeout.

Parameters
handshakeTimeoutMillis Int: to use for SSL connection handshake, or 0 for none. The socket timeout is reset to 0 after the handshake.
Return
SocketFactory! a new SSLSocketFactory with the specified parameters

getDefault

Added in API level 8
Deprecated in API level 29
open static fun getDefault(
    handshakeTimeoutMillis: Int,
    cache: SSLSessionCache!
): SSLSocketFactory!

Deprecated: Deprecated in Java.

Returns a new socket factory instance with an optional handshake timeout and SSL session cache.

Parameters
handshakeTimeoutMillis Int: to use for SSL connection handshake, or 0 for none. The socket timeout is reset to 0 after the handshake.
cache SSLSessionCache!: The SSLSessionCache to use, or null for no cache.
Return
SSLSocketFactory! a new SSLSocketFactory with the specified parameters

getDefaultCipherSuites

Added in API level 1
open fun getDefaultCipherSuites(): Array<String!>!

Deprecated: Deprecated in Java.

Return
Array<String!>! array of the cipher suites enabled by default

getInsecure

Added in API level 8
Deprecated in API level 29
open static fun getInsecure(
    handshakeTimeoutMillis: Int,
    cache: SSLSessionCache!
): SSLSocketFactory!

Deprecated: Deprecated in Java.

Returns a new instance of a socket factory with all SSL security checks disabled, using an optional handshake timeout and SSL session cache.

Warning: Sockets created using this factory are vulnerable to person-in-the-middle attacks!

Parameters
handshakeTimeoutMillis Int: to use for SSL connection handshake, or 0 for none. The socket timeout is reset to 0 after the handshake.
cache SSLSessionCache!: The SSLSessionCache to use, or null for no cache.
Return
SSLSocketFactory! an insecure SSLSocketFactory with the specified parameters

getNpnSelectedProtocol

Added in API level 16
Deprecated in API level 29
open fun getNpnSelectedProtocol(socket: Socket!): ByteArray!

Deprecated: Deprecated in Java.

Returns the Next Protocol Negotiation (NPN) protocol selected by client and server, or null if no protocol was negotiated.

Parameters
socket Socket!: a socket created by this factory.
Exceptions
java.lang.IllegalArgumentException if the socket was not created by this factory.

getSupportedCipherSuites

Added in API level 1
open fun getSupportedCipherSuites(): Array<String!>!

Deprecated: Deprecated in Java.

Return
Array<String!>! an array of cipher suite names

setHostname

Added in API level 17
Deprecated in API level 29
open fun setHostname(
    socket: Socket!,
    hostName: String!
): Unit

Deprecated: Deprecated in Java.

Turns on Server Name Indication (SNI) on a given socket.

Parameters
socket Socket!: a socket created by this factory.
hostName String!: the desired SNI hostname, null to disable.
Exceptions
java.lang.IllegalArgumentException if the socket was not created by this factory.

setKeyManagers

Added in API level 14
Deprecated in API level 29
open fun setKeyManagers(keyManagers: Array<KeyManager!>!): Unit

Deprecated: Deprecated in Java.

Sets the KeyManagers to be used for connections made by this factory.

setNpnProtocols

Added in API level 16
Deprecated in API level 29
open fun setNpnProtocols(npnProtocols: Array<ByteArray!>!): Unit

Deprecated: Deprecated in Java.

Sets the Next Protocol Negotiation (NPN) protocols that this peer is interested in.

For servers this is the sequence of protocols to advertise as supported, in order of preference. This list is sent unencrypted to all clients that support NPN.

For clients this is a list of supported protocols to match against the server's list. If there is no protocol supported by both client and server then the first protocol in the client's list will be selected. The order of the client's protocols is otherwise insignificant.

Parameters
npnProtocols Array<ByteArray!>!: a non-empty list of protocol byte arrays. All arrays must be non-empty and of length less than 256.

setTrustManagers

Added in API level 14
Deprecated in API level 29
open fun setTrustManagers(trustManager: Array<TrustManager!>!): Unit

Deprecated: Deprecated in Java.

Sets the TrustManagers to be used for connections made by this factory.

setUseSessionTickets

Added in API level 17
Deprecated in API level 29
open fun setUseSessionTickets(
    socket: Socket!,
    useSessionTickets: Boolean
): Unit

Deprecated: Deprecated in Java.

Enables session ticket support on the given socket.

Parameters
socket Socket!: a socket created by this factory
useSessionTickets Boolean: true to enable session ticket support on this socket.
Exceptions
java.lang.IllegalArgumentException if the socket was not created by this factory.