Package org.mozilla.jss.ssl.javax
Class JSSSocket
java.lang.Object
java.net.Socket
javax.net.ssl.SSLSocket
org.mozilla.jss.ssl.javax.JSSSocket
- All Implemented Interfaces:
Closeable,AutoCloseable
SSL-enabled socket following the javax.net.ssl.SSLSocket interface.
Most users will want to use the JSSSocketFactory provided by the Java
Provider interface instead of using this class directly.
This SSLSocket implementation is a wrapped implementation. In particular, we
need to consume an existing Socket (via the consumeSocket(...) call) which
we actually send data over. When called from a socket factory, this
additional socket will be automatically created for the caller. This
is necessary because SSLSocketFactory includes a mode which wraps an
existing socket.
All JSSSocket instances have a underlying SocketChannel, of type
JSSSocketChannel. Notably lacking is a javax.net.ssl.SSLSocketChannel type,
so JSSSocketChannel includes no additional SSL-specific options. However,
the core of the SSLEngine wrapping logic exists there.
In order to interoperate with JSSEngine, many of the adjacent methods have
been included in this class as well.
This socket can either be a client or a server, depending on how it was
created. For more information, see the javax.net.ssl.SSLSocket
documentation.
To construct a (useful) new instance, the following calls must be made:
- new JSSSocket();
- consumeSocket(inst);
- initSSLEngine(...);
- setKeyManagers(...);
- setTrustManagers(...);
Optionally, setSSLContext(...) could be called to provide the SSLContext
from which the SSLEngine should be constructed. This should be called prior
to initSSLEngine(...) being called.
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoidAdd a callback to fire on handshake completion.voidbind(SocketAddress bindpoint) voidclose()voidconnect(SocketAddress endpoint) voidconnect(SocketAddress endpoint, int timeout) voidconsumeSocket(Socket parent) Consume a parent socket, utilizing it for all read/write operations.booleanGet the autoClose status of this socket, that is, whether or not its parent socket will be automatically closed.Get the underlying SocketChannel for this Socket.String[]Get the set of enabled cipher suites for this SSLSocket.String[]Get the set of enabled protocol versions for this SSLSocket.booleanGet whether or not this SSLSocket enables creation of new sessions.Get the underlying JSSEngine instance.Get the initial session constructed during handshaking.Get a copy of an input stream for this Socket.protected JSSSocketChannelHelper to always return the channel for this socket, initializing it if it isn't yet created.booleanCollection<? extends EventListener>Gets the current list of event listeners this SSLSocket will fire on certain events.intbooleanGet whether or not this SSLSocket needs client authentication.boolean<T> TgetOption(SocketOption<T> name) Get a copy of an output stream for this Socket.intgetPort()intbooleanintGet the established session for this SSLSocket.intintprotected SSLContextGet the SSLContext if one exists or create a new instance.Get the configuration of this SSLSocket as a JSSParameters object.String[]Get the set of supported cipher suites for this SSLSocket.String[]Get the set of supported protocol versions for this SSLSocket.booleanintbooleanGet whether or not this SSLSocket is handshaking as a client.booleanGet whether or not this SSLSocket wants client authentication.voidExplicitly initialize the SSLEngine with no session resumption information.voidinitEngine(String host, int port) Explicitly initialize the SSLEngine with information for session resumption, including peer's hostname and port.booleanisBound()booleanisClosed()booleanbooleanbooleanprotected voidInternal helper to fire callbacks on handshake completion.voidRemove a callback from firing on handshake completion.voidsendUrgentData(int data) voidsetAutoClose(boolean on) Set the autoClose status of this socket, that is, whether or not its parent socket will be automatically closed.voidsetCertFromAlias(String alias) Set the certificate this SSLSocket will utilize from an alias in the NSS DB.voidsetConsumedData(InputStream consumed) Helper to inform this socket of data already consumed from the wrapped socket.voidsetEnabledCipherSuites(String[] suites) Set the list of enabled cipher suites for this SSLSocket.voidsetEnabledProtocols(String[] protocols) Set the list of enabled protocol versions for this SSLSocket.voidsetEnableSessionCreation(boolean enabled) Set whether or not this SSLSocket enables creation of new sessions.voidsetHostname(String name) Set the hostname this client socket is connecting to, for HTTPS TLS certificate validation purposes.voidsetKeepAlive(boolean on) voidSet the KeyManager this SSLSocket will utilize to select a key.voidsetKeyManagers(X509KeyManager[] xkms) Set the key managers this SSLSocket will utilize to select a key.voidsetKeyMaterials(PK11Cert ourCert, PK11PrivKey ourKey) Set the certificate this SSLSocket will utilize from a certificate and its matching private key.voidsetListeners(Collection<? extends EventListener> listeners) Set the listeners this SSLSocket will fire on certain events.voidsetNeedClientAuth(boolean need) Set whether or not this SSLSocket needs client authentication.voidsetOOBInline(boolean on) <T> SocketsetOption(SocketOption<T> name, T value) voidsetPerformancePreferences(int connectionTime, int latency, int bandwidth) voidsetReceiveBufferSize(int size) voidsetReuseAddress(boolean on) voidsetSendBufferSize(int size) voidsetSoLinger(boolean on, int linger) voidsetSoTimeout(int timeout) voidsetSSLContext(SSLContext ctx) Explicitly set the SSLContext utilized by this JSSSocket instance.voidsetSSLParameters(SSLParameters params) Set the configuration of this SSLSocket from the given SSLParameters instance.voidsetTcpNoDelay(boolean on) voidsetTrafficClass(int tc) voidSet the trust manager this SSLSocket will utilize to validate a peer's certificate.voidsetTrustManagers(X509TrustManager[] xtms) Set the trust managers this SSLSocket will utilize to validate a peer's certificate.voidsetUseClientMode(boolean client) Set whether or not this SSLSocket is handshaking as a client.voidsetWantClientAuth(boolean want) Set whether or not this SSLSocket wants client authentication.voidvoidvoidBegin a handshake, blocking to completion; this will begin a new handshake when one has already been issued.Set<SocketOption<?>>toString()Methods inherited from class javax.net.ssl.SSLSocket
getApplicationProtocol, getHandshakeApplicationProtocol, getHandshakeApplicationProtocolSelector, setHandshakeApplicationProtocolSelectorMethods inherited from class java.net.Socket
setSocketImplFactory
-
Constructor Details
-
JSSSocket
public JSSSocket()Start building a new JSSSocket. We specifically avoid creating any other constructors as we wish to consume an existing socket rather than creating a new one.
-
-
Method Details
-
consumeSocket
Consume a parent socket, utilizing it for all read/write operations. This JSSSocket instance will inherit all information about the connection from this underlying socket. When utilized in a JSSSocket, callers should refrain from interacting with the underlying socket directly until the TLS connection is closed. Otherwise, messages may get dropped.- Throws:
IOException
-
getSSLContext
Get the SSLContext if one exists or create a new instance. This is used by initSSLEngine(..) to create the underlying SSLEngine.- Throws:
IOException
-
setSSLContext
Explicitly set the SSLContext utilized by this JSSSocket instance. This enables JSSServerSocket to copy its SSLContext over to the accepted JSSSocket. -
initEngine
Explicitly initialize the SSLEngine with no session resumption information.- Throws:
IOException
-
initEngine
Explicitly initialize the SSLEngine with information for session resumption, including peer's hostname and port.- Throws:
IOException
-
getEngine
Get the underlying JSSEngine instance. Note that, just like accessing the underlying Socket instance while the JSSSocket instance is still open is dangerous, performing other TLS operations directly via JSSEngine is also dangerous. This is mostly exposed to enable advanced configuration of the JSSEngine that isn't otherwise allowed by JSSSocket, and to facilitate the accept() method on JSSServerSocket. -
getChannel
Get the underlying SocketChannel for this Socket.- Overrides:
getChannelin classSocket- See Also:
-
getInternalChannel
Helper to always return the channel for this socket, initializing it if it isn't yet created. -
getInputStream
Get a copy of an input stream for this Socket.- Overrides:
getInputStreamin classSocket- Throws:
IOException- See Also:
-
getOutputStream
Get a copy of an output stream for this Socket.- Overrides:
getOutputStreamin classSocket- Throws:
IOException- See Also:
-
setConsumedData
Helper to inform this socket of data already consumed from the wrapped socket. This is provided to facilitate a SSLSocketFactory call which allows construction of SSLSocket instances from a non-SSL ServerSocket, allowing the application to check SNI information directly. -
getAutoClose
public boolean getAutoClose()Get the autoClose status of this socket, that is, whether or not its parent socket will be automatically closed. -
setAutoClose
public void setAutoClose(boolean on) Set the autoClose status of this socket, that is, whether or not its parent socket will be automatically closed. -
setHostname
Set the hostname this client socket is connecting to, for HTTPS TLS certificate validation purposes.- See Also:
-
setCertFromAlias
Set the certificate this SSLSocket will utilize from an alias in the NSS DB.- Throws:
IllegalArgumentException- See Also:
-
setKeyMaterials
Set the certificate this SSLSocket will utilize from a certificate and its matching private key.- Throws:
IllegalArgumentException- See Also:
-
setKeyManager
Set the KeyManager this SSLSocket will utilize to select a key.- See Also:
-
setKeyManagers
Set the key managers this SSLSocket will utilize to select a key. -
setTrustManager
Set the trust manager this SSLSocket will utilize to validate a peer's certificate. -
setTrustManagers
Set the trust managers this SSLSocket will utilize to validate a peer's certificate. -
setListeners
Set the listeners this SSLSocket will fire on certain events.- See Also:
-
getListeners
Gets the current list of event listeners this SSLSocket will fire on certain events.- See Also:
-
startHandshake
Begin a handshake, blocking to completion; this will begin a new handshake when one has already been issued.- Specified by:
startHandshakein classSSLSocket- Throws:
IOException- See Also:
-
addHandshakeCompletedListener
public void addHandshakeCompletedListener(HandshakeCompletedListener callback) throws IllegalArgumentException Add a callback to fire on handshake completion.- Specified by:
addHandshakeCompletedListenerin classSSLSocket- Throws:
IllegalArgumentException- See Also:
-
notifyHandshakeCompletedListeners
protected void notifyHandshakeCompletedListeners()Internal helper to fire callbacks on handshake completion. -
removeHandshakeCompletedListener
public void removeHandshakeCompletedListener(HandshakeCompletedListener callback) throws IllegalArgumentException Remove a callback from firing on handshake completion.- Specified by:
removeHandshakeCompletedListenerin classSSLSocket- Throws:
IllegalArgumentException- See Also:
-
getEnabledCipherSuites
Get the set of enabled cipher suites for this SSLSocket.- Specified by:
getEnabledCipherSuitesin classSSLSocket- See Also:
-
getSupportedCipherSuites
Get the set of supported cipher suites for this SSLSocket.- Specified by:
getSupportedCipherSuitesin classSSLSocket- See Also:
-
setEnabledCipherSuites
Set the list of enabled cipher suites for this SSLSocket.- Specified by:
setEnabledCipherSuitesin classSSLSocket- See Also:
-
getEnabledProtocols
Get the set of enabled protocol versions for this SSLSocket.- Specified by:
getEnabledProtocolsin classSSLSocket- See Also:
-
getSupportedProtocols
Get the set of supported protocol versions for this SSLSocket.- Specified by:
getSupportedProtocolsin classSSLSocket- See Also:
-
setEnabledProtocols
Set the list of enabled protocol versions for this SSLSocket.- Specified by:
setEnabledProtocolsin classSSLSocket- See Also:
-
getEnableSessionCreation
public boolean getEnableSessionCreation()Get whether or not this SSLSocket enables creation of new sessions.- Specified by:
getEnableSessionCreationin classSSLSocket- See Also:
-
setEnableSessionCreation
public void setEnableSessionCreation(boolean enabled) Set whether or not this SSLSocket enables creation of new sessions.- Specified by:
setEnableSessionCreationin classSSLSocket- See Also:
-
getHandshakeSession
Get the initial session constructed during handshaking.- Overrides:
getHandshakeSessionin classSSLSocket- See Also:
-
getSession
Get the established session for this SSLSocket.- Specified by:
getSessionin classSSLSocket- See Also:
-
getUseClientMode
public boolean getUseClientMode()Get whether or not this SSLSocket is handshaking as a client.- Specified by:
getUseClientModein classSSLSocket- See Also:
-
setUseClientMode
public void setUseClientMode(boolean client) Set whether or not this SSLSocket is handshaking as a client.- Specified by:
setUseClientModein classSSLSocket- See Also:
-
getWantClientAuth
public boolean getWantClientAuth()Get whether or not this SSLSocket wants client authentication.- Specified by:
getWantClientAuthin classSSLSocket- See Also:
-
setWantClientAuth
public void setWantClientAuth(boolean want) Set whether or not this SSLSocket wants client authentication.- Specified by:
setWantClientAuthin classSSLSocket- See Also:
-
getNeedClientAuth
public boolean getNeedClientAuth()Get whether or not this SSLSocket needs client authentication.- Specified by:
getNeedClientAuthin classSSLSocket- See Also:
-
setNeedClientAuth
public void setNeedClientAuth(boolean need) Set whether or not this SSLSocket needs client authentication.- Specified by:
setNeedClientAuthin classSSLSocket- See Also:
-
getSSLParameters
Get the configuration of this SSLSocket as a JSSParameters object.- Overrides:
getSSLParametersin classSSLSocket- See Also:
-
setSSLParameters
Set the configuration of this SSLSocket from the given SSLParameters instance.- Overrides:
setSSLParametersin classSSLSocket- See Also:
-
connect
- Overrides:
connectin classSocket- Throws:
IOException
-
connect
- Overrides:
connectin classSocket- Throws:
IOException
-
bind
- Overrides:
bindin classSocket- Throws:
IOException
-
close
- Specified by:
closein interfaceAutoCloseable- Specified by:
closein interfaceCloseable- Overrides:
closein classSocket- Throws:
IOException
-
shutdownInput
- Overrides:
shutdownInputin classSocket- Throws:
IOException
-
shutdownOutput
- Overrides:
shutdownOutputin classSocket- Throws:
IOException
-
getInetAddress
- Overrides:
getInetAddressin classSocket
-
getLocalAddress
- Overrides:
getLocalAddressin classSocket
-
getPort
public int getPort() -
getLocalPort
public int getLocalPort()- Overrides:
getLocalPortin classSocket
-
getLocalSocketAddress
- Overrides:
getLocalSocketAddressin classSocket
-
getRemoteSocketAddress
- Overrides:
getRemoteSocketAddressin classSocket
-
getTcpNoDelay
- Overrides:
getTcpNoDelayin classSocket- Throws:
SocketException
-
setTcpNoDelay
- Overrides:
setTcpNoDelayin classSocket- Throws:
SocketException
-
getSoLinger
- Overrides:
getSoLingerin classSocket- Throws:
SocketException
-
setSoLinger
- Overrides:
setSoLingerin classSocket- Throws:
SocketException
-
getSoTimeout
- Overrides:
getSoTimeoutin classSocket- Throws:
SocketException
-
setSoTimeout
- Overrides:
setSoTimeoutin classSocket- Throws:
SocketException
-
getOOBInline
- Overrides:
getOOBInlinein classSocket- Throws:
SocketException
-
setOOBInline
- Overrides:
setOOBInlinein classSocket- Throws:
SocketException
-
sendUrgentData
- Overrides:
sendUrgentDatain classSocket- Throws:
IOException
-
getSendBufferSize
- Overrides:
getSendBufferSizein classSocket- Throws:
SocketException
-
setSendBufferSize
- Overrides:
setSendBufferSizein classSocket- Throws:
SocketException
-
getReceiveBufferSize
- Overrides:
getReceiveBufferSizein classSocket- Throws:
SocketException
-
setReceiveBufferSize
- Overrides:
setReceiveBufferSizein classSocket- Throws:
SocketException
-
getKeepAlive
- Overrides:
getKeepAlivein classSocket- Throws:
SocketException
-
setKeepAlive
- Overrides:
setKeepAlivein classSocket- Throws:
SocketException
-
getTrafficClass
- Overrides:
getTrafficClassin classSocket- Throws:
SocketException
-
setTrafficClass
- Overrides:
setTrafficClassin classSocket- Throws:
SocketException
-
getReuseAddress
- Overrides:
getReuseAddressin classSocket- Throws:
SocketException
-
setReuseAddress
- Overrides:
setReuseAddressin classSocket- Throws:
SocketException
-
isConnected
public boolean isConnected()- Overrides:
isConnectedin classSocket
-
isBound
public boolean isBound() -
isClosed
public boolean isClosed() -
isInputShutdown
public boolean isInputShutdown()- Overrides:
isInputShutdownin classSocket
-
isOutputShutdown
public boolean isOutputShutdown()- Overrides:
isOutputShutdownin classSocket
-
setPerformancePreferences
public void setPerformancePreferences(int connectionTime, int latency, int bandwidth) - Overrides:
setPerformancePreferencesin classSocket
-
toString
-
setOption
- Overrides:
setOptionin classSocket- Throws:
IOException
-
getOption
- Overrides:
getOptionin classSocket- Throws:
IOException
-
supportedOptions
- Overrides:
supportedOptionsin classSocket
-