public class OpenSSLSocketImpl extends SSLSocket implements NativeCrypto.SSLHandshakeCallbacks, SSLParametersImpl.AliasChooser, SSLParametersImpl.PSKCallbacks
Extensions to SSLSocket include:
| Modifier | Constructor and Description |
|---|---|
protected |
OpenSSLSocketImpl(InetAddress address,
int port,
InetAddress clientAddress,
int clientPort,
SSLParametersImpl sslParameters) |
protected |
OpenSSLSocketImpl(InetAddress address,
int port,
SSLParametersImpl sslParameters) |
protected |
OpenSSLSocketImpl(Socket socket,
String hostname,
int port,
boolean autoClose,
SSLParametersImpl sslParameters)
Create an SSL socket that wraps another socket.
|
protected |
OpenSSLSocketImpl(SSLParametersImpl sslParameters) |
protected |
OpenSSLSocketImpl(String hostname,
int port,
InetAddress clientAddress,
int clientPort,
SSLParametersImpl sslParameters) |
protected |
OpenSSLSocketImpl(String hostname,
int port,
SSLParametersImpl sslParameters) |
| Modifier and Type | Method and Description |
|---|---|
void |
addHandshakeCompletedListener(HandshakeCompletedListener listener)
Registers the specified listener to receive notification on completion of
a handshake on this connection.
|
String |
chooseClientAlias(X509KeyManager keyManager,
X500Principal[] issuers,
String[] keyTypes) |
String |
chooseClientPSKIdentity(PSKKeyManager keyManager,
String identityHint) |
String |
chooseServerAlias(X509KeyManager keyManager,
String keyType) |
String |
chooseServerPSKIdentityHint(PSKKeyManager keyManager) |
void |
clientCertificateRequested(byte[] keyTypeBytes,
byte[][] asn1DerEncodedPrincipals)
Called on an SSL client when the server requests (or
requires a certificate).
|
int |
clientPSKKeyRequested(String identityHint,
byte[] identity,
byte[] key)
Gets the key to be used in client mode for this connection in Pre-Shared Key (PSK) key
exchange.
|
void |
close()
Closes the socket.
|
protected void |
finalize()
Invoked when the garbage collector has detected that this instance is no longer reachable.
|
byte[] |
getAlpnSelectedProtocol()
Returns the protocol agreed upon by client and server, or
null if
no protocol was agreed upon. |
byte[] |
getChannelId()
Gets the TLS Channel ID for this server socket.
|
String[] |
getEnabledCipherSuites()
Returns the names of the enabled cipher suites.
|
String[] |
getEnabledProtocols()
Returns the names of the enabled protocols.
|
boolean |
getEnableSessionCreation()
Returns whether new SSL sessions may be created by this socket or if
existing sessions must be reused.
|
FileDescriptor |
getFileDescriptor$() |
InputStream |
getInputStream()
Returns an input stream to read data from this socket.
|
boolean |
getNeedClientAuth()
Returns true if the server socket should require client authentication.
|
byte[] |
getNpnSelectedProtocol()
Returns the protocol agreed upon by client and server, or null if no
protocol was agreed upon.
|
OutputStream |
getOutputStream()
Returns an output stream to write data into this socket.
|
int |
getPort()
Returns the port number of the target host this socket is connected to, or 0 if this socket
is not yet connected.
|
SecretKey |
getPSKKey(PSKKeyManager keyManager,
String identityHint,
String identity) |
SSLSession |
getSession()
Returns the
SSLSession for this connection. |
int |
getSoTimeout()
Returns this socket's
receive timeout. |
int |
getSoWriteTimeout()
Note write timeouts are not part of the javax.net.ssl.SSLSocket API
|
String[] |
getSupportedCipherSuites()
Returns the names of the supported cipher suites.
|
String[] |
getSupportedProtocols()
Returns the names of the supported protocols.
|
boolean |
getUseClientMode()
Returns true if this connection will act in client mode when handshaking.
|
boolean |
getWantClientAuth()
Returns true if the server should request client authentication.
|
void |
onSSLStateChange(long sslSessionNativePtr,
int type,
int val)
Called when SSL state changes.
|
void |
removeHandshakeCompletedListener(HandshakeCompletedListener listener)
Removes the specified handshake completion listener.
|
void |
sendUrgentData(int data)
Sends the given single byte data which is represented by the lowest octet
of
value as "TCP urgent data". |
int |
serverPSKKeyRequested(String identityHint,
String identity,
byte[] key)
Gets the key to be used in server mode for this connection in Pre-Shared Key (PSK) key
exchange.
|
void |
setAlpnProtocols(byte[] alpnProtocols)
Sets the list of protocols this peer is interested in.
|
void |
setChannelIdEnabled(boolean enabled)
Enables/disables TLS Channel ID for this server socket.
|
void |
setChannelIdPrivateKey(PrivateKey privateKey)
Sets the
PrivateKey to be used for TLS Channel ID by this client socket. |
void |
setEnabledCipherSuites(String[] suites)
Sets the names of the cipher suites to be enabled.
|
void |
setEnabledProtocols(String[] protocols)
Sets the names of the protocols to be enabled.
|
void |
setEnableSessionCreation(boolean flag)
Sets whether new SSL sessions may be created by this socket or if
existing sessions must be reused.
|
void |
setHandshakeTimeout(int handshakeTimeoutMilliseconds)
Set the handshake timeout on this socket.
|
void |
setHostname(String hostname)
This method enables Server Name Indication
|
void |
setNeedClientAuth(boolean need)
Sets whether the server should require client authentication.
|
void |
setNpnProtocols(byte[] npnProtocols)
Sets the list of protocols this peer is interested in.
|
void |
setOOBInline(boolean on)
Sets this socket's
SocketOptions.SO_OOBINLINE option. |
void |
setSoTimeout(int readTimeoutMilliseconds)
Sets this socket's
read timeout in milliseconds. |
void |
setSoWriteTimeout(int writeTimeoutMilliseconds)
Note write timeouts are not part of the javax.net.ssl.SSLSocket API
|
void |
setUseClientMode(boolean mode)
Sets whether this connection should act in client mode when handshaking.
|
void |
setUseSessionTickets(boolean useSessionTickets)
This method enables session ticket support.
|
void |
setWantClientAuth(boolean want)
Sets whether the server should request client authentication.
|
void |
startHandshake()
Starts a TLS/SSL handshake on this connection using some native methods
from the OpenSSL library.
|
void |
verifyCertificateChain(long sslSessionNativePtr,
long[] certRefs,
String authMethod)
Verify that we trust the certificate chain is trusted.
|
getSSLParameters, setSSLParameters, shutdownInput, shutdownOutputbind, connect, connect, getChannel, getInetAddress, getKeepAlive, getLocalAddress, getLocalPort, getLocalSocketAddress, getOOBInline, getReceiveBufferSize, getRemoteSocketAddress, getReuseAddress, getReusePort, getSendBufferSize, getSoLinger, getTcpNoDelay, getTrafficClass, isBound, isClosed, isConnected, isInputShutdown, isOutputShutdown, onBind, onClose, onConnect, setKeepAlive, setPerformancePreferences, setReceiveBufferSize, setReuseAddress, setReusePort, setSendBufferSize, setSocketImplFactory, setSoLinger, setTcpNoDelay, setTrafficClass, toStringprotected OpenSSLSocketImpl(SSLParametersImpl sslParameters) throws IOException
IOExceptionprotected OpenSSLSocketImpl(String hostname, int port, SSLParametersImpl sslParameters) throws IOException
IOExceptionprotected OpenSSLSocketImpl(InetAddress address, int port, SSLParametersImpl sslParameters) throws IOException
IOExceptionprotected OpenSSLSocketImpl(String hostname, int port, InetAddress clientAddress, int clientPort, SSLParametersImpl sslParameters) throws IOException
IOExceptionprotected OpenSSLSocketImpl(InetAddress address, int port, InetAddress clientAddress, int clientPort, SSLParametersImpl sslParameters) throws IOException
IOExceptionprotected OpenSSLSocketImpl(Socket socket, String hostname, int port, boolean autoClose, SSLParametersImpl sslParameters) throws IOException
IOExceptionpublic void startHandshake()
throws IOException
startHandshake in class SSLSocketIOException - if an error occurs.public int getPort()
Socketpublic void clientCertificateRequested(byte[] keyTypeBytes,
byte[][] asn1DerEncodedPrincipals)
throws CertificateEncodingException,
SSLException
NativeCrypto.SSLHandshakeCallbacksclientCertificateRequested in interface NativeCrypto.SSLHandshakeCallbackskeyTypeBytes - key types supported by the server,
convertible to strings with #keyTypeasn1DerEncodedPrincipals - CAs known to the serverCertificateEncodingExceptionSSLExceptionpublic int clientPSKKeyRequested(String identityHint, byte[] identity, byte[] key)
NativeCrypto.SSLHandshakeCallbacksclientPSKKeyRequested in interface NativeCrypto.SSLHandshakeCallbacksidentityHint - PSK identity hint provided by the server or null if no hint
provided.identity - buffer to be populated with PSK identity (NULL-terminated modified UTF-8)
by this method. This identity will be provided to the server.key - buffer to be populated with key material by this method.key buffer or 0 if an
error occurred in which case the handshake will be aborted.public int serverPSKKeyRequested(String identityHint, String identity, byte[] key)
NativeCrypto.SSLHandshakeCallbacksserverPSKKeyRequested in interface NativeCrypto.SSLHandshakeCallbacksidentityHint - PSK identity hint provided by this server to the client or
null if no hint was provided.identity - PSK identity provided by the client.key - buffer to be populated with key material by this method.key buffer or 0 if an
error occurred in which case the handshake will be aborted.public void onSSLStateChange(long sslSessionNativePtr,
int type,
int val)
NativeCrypto.SSLHandshakeCallbacksonSSLStateChange in interface NativeCrypto.SSLHandshakeCallbackspublic void verifyCertificateChain(long sslSessionNativePtr,
long[] certRefs,
String authMethod)
throws CertificateException
NativeCrypto.SSLHandshakeCallbacksverifyCertificateChain in interface NativeCrypto.SSLHandshakeCallbackssslSessionNativePtr - pointer to a reference of the SSL_SESSIONcertRefs - chain of X.509 certificate referencesauthMethod - auth algorithm nameCertificateException - if the certificate is untrustedpublic InputStream getInputStream() throws IOException
SocketSocketChannel and that channel is in non-blocking mode then reads from the
stream will throw a IllegalBlockingModeException.getInputStream in class SocketIOException - if an error occurs while creating the input stream or the
socket is in an invalid state.public OutputStream getOutputStream() throws IOException
SocketSocketChannel and that channel is in non-blocking mode then writes to the
stream will throw a IllegalBlockingModeException.getOutputStream in class SocketIOException - if an error occurs while creating the output stream or the
socket is in an invalid state.public SSLSession getSession()
SSLSocketSSLSession for this connection. If necessary, a
handshake will be initiated, in which case this method will block until the handshake
has been established. If the handshake fails, an invalid session object
will be returned.getSession in class SSLSocketpublic void addHandshakeCompletedListener(HandshakeCompletedListener listener)
SSLSocketaddHandshakeCompletedListener in class SSLSocketlistener - the listener to register.public void removeHandshakeCompletedListener(HandshakeCompletedListener listener)
SSLSocketremoveHandshakeCompletedListener in class SSLSocketlistener - the listener to remove.public boolean getEnableSessionCreation()
SSLSocketgetEnableSessionCreation in class SSLSockettrue if new sessions may be created, otherwise
false.public void setEnableSessionCreation(boolean flag)
SSLSocketflag is false and there are
no sessions to resume, handshaking will fail.setEnableSessionCreation in class SSLSocketflag - true if new sessions may be created.public String[] getSupportedCipherSuites()
SSLSocketgetSupportedCipherSuites in class SSLSocketpublic String[] getEnabledCipherSuites()
SSLSocketgetEnabledCipherSuites in class SSLSocketpublic void setEnabledCipherSuites(String[] suites)
SSLSocketSSLSocket.getSupportedCipherSuites() are
allowed.setEnabledCipherSuites in class SSLSocketsuites - the names of the to be enabled cipher suites.public String[] getSupportedProtocols()
SSLSocketgetSupportedProtocols in class SSLSocketpublic String[] getEnabledProtocols()
SSLSocketgetEnabledProtocols in class SSLSocketpublic void setEnabledProtocols(String[] protocols)
SSLSocketSSLSocket.getSupportedProtocols() are allowed.setEnabledProtocols in class SSLSocketprotocols - the names of the to be enabled protocols.public void setUseSessionTickets(boolean useSessionTickets)
useSessionTickets - True to enable session ticketspublic void setHostname(String hostname)
hostname - the desired SNI hostname, or null to disablepublic void setChannelIdEnabled(boolean enabled)
This method needs to be invoked before the handshake starts.
IllegalStateException - if this is a client socket or if the handshake has already
started.public byte[] getChannelId()
throws SSLException
null if not available.IllegalStateException - if this is a client socket or if the handshake has not yet
completed.SSLException - if channel ID is available but could not be obtained.public void setChannelIdPrivateKey(PrivateKey privateKey)
PrivateKey to be used for TLS Channel ID by this client socket.
This method needs to be invoked before the handshake starts.
privateKey - private key (enables TLS Channel ID) or null for no key (disables
TLS Channel ID). The private key must be an Elliptic Curve (EC) key based on the NIST
P-256 curve (aka SECG secp256r1 or ANSI X9.62 prime256v1).IllegalStateException - if this is a server socket or if the handshake has already
started.public boolean getUseClientMode()
SSLSocketgetUseClientMode in class SSLSocketpublic void setUseClientMode(boolean mode)
SSLSocketsetUseClientMode in class SSLSocketmode - true if this connection should act in client mode,
false if not.public boolean getWantClientAuth()
SSLSocketclient mode.getWantClientAuth in class SSLSocketpublic boolean getNeedClientAuth()
SSLSocketclient
mode.getNeedClientAuth in class SSLSocketpublic void setNeedClientAuth(boolean need)
SSLSocketclient mode.
Client authentication is one of the following:
SSLSocket.setWantClientAuth(boolean).setNeedClientAuth in class SSLSocketpublic void setWantClientAuth(boolean want)
SSLSocketSSLSocket.setNeedClientAuth(boolean) this won't stop the negotiation if the client
doesn't authenticate. This does not apply to sockets in client mode.The client authentication is one of:
SSLSocket.setNeedClientAuth(boolean).setWantClientAuth in class SSLSocketpublic void sendUrgentData(int data)
throws IOException
Socketvalue as "TCP urgent data".sendUrgentData in class Socketdata - the byte of urgent data to be sent.IOException - if an error occurs while sending urgent data.public void setOOBInline(boolean on)
throws SocketException
SocketSocketOptions.SO_OOBINLINE option.setOOBInline in class SocketSocketExceptionpublic void setSoTimeout(int readTimeoutMilliseconds)
throws SocketException
Socketread timeout in milliseconds.
Use 0 for no timeout.
To take effect, this option must be set before the blocking method was called.setSoTimeout in class SocketSocketExceptionpublic int getSoTimeout()
throws SocketException
Socketreceive timeout.getSoTimeout in class SocketSocketExceptionpublic void setSoWriteTimeout(int writeTimeoutMilliseconds)
throws SocketException
SocketExceptionpublic int getSoWriteTimeout()
throws SocketException
SocketExceptionpublic void setHandshakeTimeout(int handshakeTimeoutMilliseconds)
throws SocketException
SocketExceptionpublic void close()
throws IOException
Socketclose in interface Closeableclose in interface AutoCloseableclose in class SocketIOException - if an error occurs while closing the socket.protected void finalize()
throws Throwable
ObjectNote that objects that override finalize are significantly more expensive than
objects that don't. Finalizers may be run a long time after the object is no longer
reachable, depending on memory pressure, so it's a bad idea to rely on them for cleanup.
Note also that finalizers are run on a single VM-wide finalizer thread,
so doing blocking work in a finalizer is a bad idea. A finalizer is usually only necessary
for a class that has a native peer and needs to call a native method to destroy that peer.
Even then, it's better to provide an explicit close method (and implement
Closeable), and insist that callers manually dispose of instances. This
works well for something like files, but less well for something like a BigInteger
where typical calling code would have to deal with lots of temporaries. Unfortunately,
code that creates lots of temporaries is the worst kind of code from the point of view of
the single finalizer thread.
If you must use finalizers, consider at least providing your own
ReferenceQueue and having your own thread process that queue.
Unlike constructors, finalizers are not automatically chained. You are responsible for
calling super.finalize() yourself.
Uncaught exceptions thrown by finalizers are ignored and do not terminate the finalizer thread. See Effective Java Item 7, "Avoid finalizers" for more.
public FileDescriptor getFileDescriptor$()
getFileDescriptor$ in class Socketpublic byte[] getNpnSelectedProtocol()
public byte[] getAlpnSelectedProtocol()
null if
no protocol was agreed upon.public void setNpnProtocols(byte[] npnProtocols)
npnProtocols - a non-empty array of protocol names. From
SSL_select_next_proto, "vector of 8-bit, length prefixed byte
strings. The length byte itself is not included in the length. A byte
string of length 0 is invalid. No byte string may be truncated.".public void setAlpnProtocols(byte[] alpnProtocols)
null, no protocols will be used.alpnProtocols - a non-empty array of protocol names. From
SSL_select_next_proto, "vector of 8-bit, length prefixed byte
strings. The length byte itself is not included in the length.
A byte string of length 0 is invalid. No byte string may be
truncated.".public String chooseServerAlias(X509KeyManager keyManager, String keyType)
chooseServerAlias in interface SSLParametersImpl.AliasChooserpublic String chooseClientAlias(X509KeyManager keyManager, X500Principal[] issuers, String[] keyTypes)
chooseClientAlias in interface SSLParametersImpl.AliasChooserpublic String chooseServerPSKIdentityHint(PSKKeyManager keyManager)
chooseServerPSKIdentityHint in interface SSLParametersImpl.PSKCallbackspublic String chooseClientPSKIdentity(PSKKeyManager keyManager, String identityHint)
chooseClientPSKIdentity in interface SSLParametersImpl.PSKCallbackspublic SecretKey getPSKKey(PSKKeyManager keyManager, String identityHint, String identity)
getPSKKey in interface SSLParametersImpl.PSKCallbacks