SSLSocket (Java SE 15 & JDK 15) (original) (raw)

All Implemented Interfaces:

[Closeable](../../../java/io/Closeable.html "interface in java.io"), [AutoCloseable](../../../java/lang/AutoCloseable.html "interface in java.lang")


public abstract class SSLSocket extends Socket

This class extends Socket and provides secure sockets using protocols such as the "Secure Sockets Layer" (SSL) or IETF "Transport Layer Security" (TLS) protocols.

Such sockets are normal stream sockets, but they add a layer of security protections over the underlying network transport protocol, such as TCP. Those protections include:

These kinds of protection are specified by a "cipher suite", which is a combination of cryptographic algorithms used by a given SSL connection. During the negotiation process, the two endpoints must agree on a ciphersuite that is available in both environments. If there is no such suite in common, no SSL connection can be established, and no data can be exchanged.

The cipher suite used is established by a negotiation process called "handshaking". The goal of this process is to create or rejoin a "session", which may protect many connections over time. After handshaking has completed, you can access session attributes by using the getSession method. The initial handshake on this connection can be initiated in one of three ways:

If handshaking fails for any reason, the SSLSocket is closed, and no further communications can be done.

There are two groups of cipher suites which you will need to know about when managing cipher suites:

Implementation defaults require that only cipher suites which authenticate servers and provide confidentiality be enabled by default. Only if both sides explicitly agree to unauthenticated and/or non-private (unencrypted) communications will such a ciphersuite be selected.

When an SSLSocket is first created, no handshaking is done so that applications may first set their communication preferences: what cipher suites to use, whether the socket should be in client or server mode, etc. However, security is always provided by the time that application data is sent over the connection.

You may register to receive event notification of handshake completion. This involves the use of two additional classes. HandshakeCompletedEvent objects are passed to HandshakeCompletedListener instances, which are registered by users of this API. An SSLSocket is created by SSLSocketFactory, or by accepting a connection from aSSLServerSocket.

A SSL socket must choose to operate in the client or server mode. This will determine who begins the handshaking process, as well as which messages should be sent by each party. Each connection must have one client and one server, or handshaking will not progress properly. Once the initial handshaking has started, a socket can not switch between client and server modes, even when performing renegotiations.

API Note:

When the connection is no longer needed, the client and server applications should each close both sides of their respective connection. For SSLSocket objects, for example, an application can callSocket.shutdownOutput() or OutputStream.close() for output stream close and call Socket.shutdownInput() orInputStream.close() for input stream close. Note that in some cases, closing the input stream may depend on the peer's output stream being closed first. If the connection is not closed in an orderly manner (for example Socket.shutdownInput() is called before the peer's write closure notification has been received), exceptions may be raised to indicate that an error has occurred. Once anSSLSocket is closed, it is not reusable: a new SSLSocket must be created.

Since:

1.4

See Also:

Socket, SSLServerSocket, SSLSocketFactory

Constructors

Modifier Constructor Description
protected SSLSocket() Used only by subclasses.
protected SSLSocket​(String host, int port) Used only by subclasses.
protected SSLSocket​(String host, int port,InetAddress clientAddress, int clientPort) Used only by subclasses.
protected SSLSocket​(InetAddress address, int port) Used only by subclasses.
protected SSLSocket​(InetAddress address, int port,InetAddress clientAddress, int clientPort) Used only by subclasses.
Modifier and Type Method Description
abstract void addHandshakeCompletedListener​(HandshakeCompletedListener listener) Registers an event listener to receive notifications that an SSL handshake has completed on this connection.
String getApplicationProtocol() Returns the most recent application protocol value negotiated for this connection.
abstract String[] getEnabledCipherSuites() Returns the names of the SSL cipher suites which are currently enabled for use on this connection.
abstract String[] getEnabledProtocols() Returns the names of the protocol versions which are currently enabled for use on this connection.
abstract boolean getEnableSessionCreation() Returns true if new SSL sessions may be established by this socket.
String getHandshakeApplicationProtocol() Returns the application protocol value negotiated on a SSL/TLS handshake currently in progress.
BiFunction<SSLSocket,​List<String>,​String> getHandshakeApplicationProtocolSelector() Retrieves the callback function that selects an application protocol value during a SSL/TLS/DTLS handshake.
SSLSession getHandshakeSession() Returns the SSLSession being constructed during a SSL/TLS handshake.
abstract boolean getNeedClientAuth() Returns true if the socket will require client authentication.
abstract SSLSession getSession() Returns the SSL Session in use by this connection.
SSLParameters getSSLParameters() Returns the SSLParameters in effect for this SSLSocket.
abstract String[] getSupportedCipherSuites() Returns the names of the cipher suites which could be enabled for use on this connection.
abstract String[] getSupportedProtocols() Returns the names of the protocols which could be enabled for use on an SSL connection.
abstract boolean getUseClientMode() Returns true if the socket is set to use client mode when handshaking.
abstract boolean getWantClientAuth() Returns true if the socket will request client authentication.
abstract void removeHandshakeCompletedListener​(HandshakeCompletedListener listener) Removes a previously registered handshake completion listener.
abstract void setEnabledCipherSuites​(String[] suites) Sets the cipher suites enabled for use on this connection.
abstract void setEnabledProtocols​(String[] protocols) Sets the protocol versions enabled for use on this connection.
abstract void setEnableSessionCreation​(boolean flag) Controls whether new SSL sessions may be established by this socket.
void setHandshakeApplicationProtocolSelector​(BiFunction<SSLSocket,​List<String>,​String> selector) Registers a callback function that selects an application protocol value for a SSL/TLS/DTLS handshake.
abstract void setNeedClientAuth​(boolean need) Configures the socket to require client authentication.
void setSSLParameters​(SSLParameters params) Applies SSLParameters to this socket.
abstract void setUseClientMode​(boolean mode) Configures the socket to use client (or server) mode when handshaking.
abstract void setWantClientAuth​(boolean want) Configures the socket to request client authentication.
abstract void startHandshake() Starts an SSL handshake on this connection.

Methods declared in class java.net.Socket

[bind](../../../java/net/Socket.html#bind%28java.net.SocketAddress%29), [close](../../../java/net/Socket.html#close%28%29), [connect](../../../java/net/Socket.html#connect%28java.net.SocketAddress%29), [connect](../../../java/net/Socket.html#connect%28java.net.SocketAddress,int%29), [getChannel](../../../java/net/Socket.html#getChannel%28%29), [getInetAddress](../../../java/net/Socket.html#getInetAddress%28%29), [getInputStream](../../../java/net/Socket.html#getInputStream%28%29), [getKeepAlive](../../../java/net/Socket.html#getKeepAlive%28%29), [getLocalAddress](../../../java/net/Socket.html#getLocalAddress%28%29), [getLocalPort](../../../java/net/Socket.html#getLocalPort%28%29), [getLocalSocketAddress](../../../java/net/Socket.html#getLocalSocketAddress%28%29), [getOOBInline](../../../java/net/Socket.html#getOOBInline%28%29), [getOption](../../../java/net/Socket.html#getOption%28java.net.SocketOption%29), [getOutputStream](../../../java/net/Socket.html#getOutputStream%28%29), [getPort](../../../java/net/Socket.html#getPort%28%29), [getReceiveBufferSize](../../../java/net/Socket.html#getReceiveBufferSize%28%29), [getRemoteSocketAddress](../../../java/net/Socket.html#getRemoteSocketAddress%28%29), [getReuseAddress](../../../java/net/Socket.html#getReuseAddress%28%29), [getSendBufferSize](../../../java/net/Socket.html#getSendBufferSize%28%29), [getSoLinger](../../../java/net/Socket.html#getSoLinger%28%29), [getSoTimeout](../../../java/net/Socket.html#getSoTimeout%28%29), [getTcpNoDelay](../../../java/net/Socket.html#getTcpNoDelay%28%29), [getTrafficClass](../../../java/net/Socket.html#getTrafficClass%28%29), [isBound](../../../java/net/Socket.html#isBound%28%29), [isClosed](../../../java/net/Socket.html#isClosed%28%29), [isConnected](../../../java/net/Socket.html#isConnected%28%29), [isInputShutdown](../../../java/net/Socket.html#isInputShutdown%28%29), [isOutputShutdown](../../../java/net/Socket.html#isOutputShutdown%28%29), [sendUrgentData](../../../java/net/Socket.html#sendUrgentData%28int%29), [setKeepAlive](../../../java/net/Socket.html#setKeepAlive%28boolean%29), [setOOBInline](../../../java/net/Socket.html#setOOBInline%28boolean%29), [setOption](../../../java/net/Socket.html#setOption%28java.net.SocketOption,T%29), [setPerformancePreferences](../../../java/net/Socket.html#setPerformancePreferences%28int,int,int%29), [setReceiveBufferSize](../../../java/net/Socket.html#setReceiveBufferSize%28int%29), [setReuseAddress](../../../java/net/Socket.html#setReuseAddress%28boolean%29), [setSendBufferSize](../../../java/net/Socket.html#setSendBufferSize%28int%29), [setSocketImplFactory](../../../java/net/Socket.html#setSocketImplFactory%28java.net.SocketImplFactory%29), [setSoLinger](../../../java/net/Socket.html#setSoLinger%28boolean,int%29), [setSoTimeout](../../../java/net/Socket.html#setSoTimeout%28int%29), [setTcpNoDelay](../../../java/net/Socket.html#setTcpNoDelay%28boolean%29), [setTrafficClass](../../../java/net/Socket.html#setTrafficClass%28int%29), [shutdownInput](../../../java/net/Socket.html#shutdownInput%28%29), [shutdownOutput](../../../java/net/Socket.html#shutdownOutput%28%29), [supportedOptions](../../../java/net/Socket.html#supportedOptions%28%29), [toString](../../../java/net/Socket.html#toString%28%29)