public abstract class SSLSocket extends Socket
Socket
s and provides secure
socket 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:
startHandshake
which explicitly
begins handshakes, or
getSession
tries to set up a session
if there is no currently valid session, and
an implicit handshake is done.
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 SSLSocket
s are 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.
SSLSocket
s are created by SSLSocketFactory
s,
or by accept
ing a connection from a
SSLServerSocket
.
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.
Socket
,
SSLSocketFactory
Modifier | Constructor and Description |
---|---|
protected |
SSLSocket()
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.
|
protected |
SSLSocket(String host,
int port)
Used only by subclasses.
|
protected |
SSLSocket(String host,
int port,
InetAddress clientAddress,
int clientPort)
Used only by subclasses.
|
Modifier and Type | Method and Description |
---|---|
abstract boolean |
getNeedClientAuth()
Returns true if the socket will require client authentication.
|
abstract boolean |
getUseClientMode()
Returns true if the socket is set to use client mode when
handshaking.
|
abstract void |
setNeedClientAuth(boolean need)
Configures the socket to require client authentication.
|
abstract void |
setUseClientMode(boolean mode)
Configures the socket to use client (or server) mode when
handshaking.
|
abstract void |
startHandshake()
Starts an SSL handshake on this connection.
|
bind, close, connect, connect, getInetAddress, getInputStream, getKeepAlive, getLocalAddress, getLocalPort, getLocalSocketAddress, getOOBInline, getOutputStream, getPort, getReceiveBufferSize, getRemoteSocketAddress, getReuseAddress, getSendBufferSize, getSoLinger, getSoTimeout, getTcpNoDelay, getTrafficClass, isBound, isClosed, isConnected, setKeepAlive, setOOBInline, setReceiveBufferSize, setReuseAddress, setSendBufferSize, setSoLinger, setSoTimeout, setTcpNoDelay, setTrafficClass, toString
protected SSLSocket()
protected SSLSocket(InetAddress address, int port) throws IOException
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.
address
- the server's hostport
- its portIOException
- if an I/O error occurs when creating the socketSecurityException
- if a security manager exists and its
checkConnect
method doesn't allow the operation.IllegalArgumentException
- if the port parameter is outside the
specified range of valid port values, which is between 0 and
65535, inclusive.NullPointerException
- if address
is null.protected SSLSocket(InetAddress address, int port, InetAddress clientAddress, int clientPort) throws IOException
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.
address
- the server's hostport
- its portclientAddress
- the client's address the socket is bound to, or
null
for the anyLocal
address.clientPort
- the client's port the socket is bound to, or
zero
for a system selected free port.IOException
- if an I/O error occurs when creating the socketSecurityException
- if a security manager exists and its
checkConnect
method doesn't allow the operation.IllegalArgumentException
- if the port parameter or clientPort
parameter is outside the specified range of valid port values,
which is between 0 and 65535, inclusive.NullPointerException
- if address
is null.protected SSLSocket(String host, int port) throws IOException, UnknownHostException
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.
host
- name of the host with which to connect, or
null
for the loopback address.port
- number of the server's portIOException
- if an I/O error occurs when creating the socketSecurityException
- if a security manager exists and its
checkConnect
method doesn't allow the operation.UnknownHostException
- if the host is not knownIllegalArgumentException
- if the port parameter is outside the
specified range of valid port values, which is between 0 and
65535, inclusive.protected SSLSocket(String host, int port, InetAddress clientAddress, int clientPort) throws IOException, UnknownHostException
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.
host
- name of the host with which to connect, or
null
for the loopback address.port
- number of the server's portclientAddress
- the client's address the socket is bound to, or
null
for the anyLocal
address.clientPort
- the client's port the socket is bound to, or
zero
for a system selected free port.IOException
- if an I/O error occurs when creating the socketSecurityException
- if a security manager exists and its
checkConnect
method doesn't allow the operation.UnknownHostException
- if the host is not knownIllegalArgumentException
- if the port parameter or clientPort
parameter is outside the specified range of valid port values,
which is between 0 and 65535, inclusive.public abstract boolean getNeedClientAuth()
setNeedClientAuth(boolean)
,
setUseClientMode(boolean)
public abstract boolean getUseClientMode()
setUseClientMode(boolean)
public abstract void setNeedClientAuth(boolean need)
A socket's client authentication setting is one of the following:
If this option is set and the client chooses not to provide authentication information about itself, the negotiations will stop and the connection will be dropped.
need
- set to true if client authentication is required,
or false if no client authentication is desired.getNeedClientAuth()
,
setUseClientMode(boolean)
public abstract void setUseClientMode(boolean mode)
This method must be called before any handshaking occurs. Once handshaking has begun, the mode can not be reset for the life of this socket.
Servers normally authenticate themselves, and clients are not required to do so.
mode
- true if the socket should start its handshaking
in "client" modeIllegalArgumentException
- if a mode change is attempted
after the initial handshake has begun.getUseClientMode()
public abstract void startHandshake() throws IOException
If data has already been sent on the connection, it continues to flow during this handshake. When the handshake completes, this will be signaled with an event. This method is synchronous for the initial handshake on a connection and returns when the negotiated handshake is complete. Some protocols may not support multiple handshakes on an existing socket and may throw an IOException.
IOException
- on a network level error