Package javax.net.ssl
Class SSLEngine
java.lang.Object
javax.net.ssl.SSLEngine
- Direct Known Subclasses:
SSLEngineImpl
public abstract class SSLEngine extends Object
The abstract implementation of secure communications using SSL, TLS, or other
protocols. It includes the setup, handshake, and encrypt/decrypt
functionality needed to create a secure connection.
- Since:
- 1.5
-
Constructor Summary
-
Method Summary
Modifier and Type Method Description abstract voidbeginHandshake()Initiates a handshake on this engine.abstract voidcloseInbound()Notifies this engine instance that no more inbound network data will be sent to this engine.abstract voidcloseOutbound()Notifies this engine instance that no more outbound application data will be sent to this engine.abstract RunnablegetDelegatedTask()Returns a delegate task for this engine instance.abstract String[]getEnabledCipherSuites()Returns the SSL cipher suite names that are enabled in this engine instance.abstract String[]getEnabledProtocols()Returns the protocol version names that are enabled in this engine instance.abstract booleangetEnableSessionCreation()Returns whether new SSL sessions may be established by this engine.abstract SSLEngineResult.HandshakeStatusgetHandshakeStatus()Returns the status of the handshake of this engine instance.abstract booleangetNeedClientAuth()Returns whether this engine instance will require client authentication.StringgetPeerHost()Returns the name of the peer host.intgetPeerPort()Returns the port number of the peer host.abstract SSLSessiongetSession()Returns the SSL session for this engine instance.SSLParametersgetSSLParameters()Returns a new SSLParameters based on this SSLSocket's current cipher suites, protocols, and client authentication settings.abstract String[]getSupportedCipherSuites()Returns the SSL cipher suite names that are supported by this engine.abstract String[]getSupportedProtocols()Returns the protocol names that are supported by this engine.abstract booleangetUseClientMode()Returns whether this engine is set to act in client mode when handshaking.abstract booleangetWantClientAuth()Returns whether this engine will request client authentication.abstract booleanisInboundDone()Returns whether no more inbound data will be accepted by this engine.abstract booleanisOutboundDone()Returns whether no more outbound data will be produced by this engine.abstract voidsetEnabledCipherSuites(String[] suites)Sets the SSL cipher suite names that should be enabled in this engine instance.abstract voidsetEnabledProtocols(String[] protocols)Sets the protocol version names that should be enabled in this engine instance.abstract voidsetEnableSessionCreation(boolean flag)Sets whether new SSL sessions may be established by this engine instance.abstract voidsetNeedClientAuth(boolean need)Sets whether this engine must require client authentication.voidsetSSLParameters(SSLParameters p)Sets various SSL handshake parameters based on the SSLParameter argument.abstract voidsetUseClientMode(boolean mode)Sets whether this engine should act in client (or server) mode when handshaking.abstract voidsetWantClientAuth(boolean want)Sets whether this engine should request client authentication.SSLEngineResultunwrap(ByteBuffer src, ByteBuffer dst)Decodes the incoming network data buffer into the application data buffer.SSLEngineResultunwrap(ByteBuffer src, ByteBuffer[] dsts)Decodes the incoming network data buffer into the application data buffers.abstract SSLEngineResultunwrap(ByteBuffer src, ByteBuffer[] dsts, int offset, int length)Decodes the incoming network data buffer into application data buffers.abstract SSLEngineResultwrap(ByteBuffer[] srcs, int offset, int length, ByteBuffer dst)Encodes the outgoing application data buffers into the network data buffer.SSLEngineResultwrap(ByteBuffer[] srcs, ByteBuffer dst)Encodes the outgoing application data buffers into the network data buffer.SSLEngineResultwrap(ByteBuffer src, ByteBuffer dst)Encodes the outgoing application data buffer into the network data buffer.
-
Constructor Details
-
SSLEngine
protected SSLEngine()Creates a newSSLEngineinstance. -
SSLEngine
Creates a newSSLEngineinstance with the specified host and port.- Parameters:
host- the name of the host.port- the port of the host.
-
-
Method Details
-
getPeerHost
Returns the name of the peer host.- Returns:
- the name of the peer host, or
nullif none is available.
-
getPeerPort
public int getPeerPort()Returns the port number of the peer host.- Returns:
- the port number of the peer host, or
-1is none is available.
-
beginHandshake
Initiates a handshake on this engine.Calling this method is not needed for the initial handshake: it will be called by
wraporunwrapif the initial handshake has not been started yet.- Throws:
SSLException- if starting the handshake fails.IllegalStateException- if the engine does not have all the needed settings (e.g. client/server mode not set).
-
closeInbound
Notifies this engine instance that no more inbound network data will be sent to this engine.- Throws:
SSLException- if this engine did not receive a needed protocol specific close notification message from the peer.
-
closeOutbound
public abstract void closeOutbound()Notifies this engine instance that no more outbound application data will be sent to this engine. -
getDelegatedTask
Returns a delegate task for this engine instance. Some engine operations may require the results of blocking or long running operations, and theSSLEngineResultinstances returned by this engine may indicate that a delegated task result is needed. In this case therunmethod of the returnedRunnabledelegated task must be called.- Returns:
- a delegate task, or
nullif none are available.
-
getEnabledCipherSuites
Returns the SSL cipher suite names that are enabled in this engine instance.- Returns:
- the SSL cipher suite names that are enabled in this engine instance.
-
getEnabledProtocols
Returns the protocol version names that are enabled in this engine instance.- Returns:
- the protocol version names that are enabled in this engine instance.
-
getEnableSessionCreation
public abstract boolean getEnableSessionCreation()Returns whether new SSL sessions may be established by this engine.- Returns:
trueif new session may be established,falseif existing sessions must be reused.
-
getHandshakeStatus
Returns the status of the handshake of this engine instance.- Returns:
- the status of the handshake of this engine instance.
-
getNeedClientAuth
public abstract boolean getNeedClientAuth()Returns whether this engine instance will require client authentication.- Returns:
trueif this engine will require client authentication,falseif no client authentication is needed.
-
getSession
Returns the SSL session for this engine instance.- Returns:
- the SSL session for this engine instance.
-
getSupportedCipherSuites
Returns the SSL cipher suite names that are supported by this engine. These cipher suites can be enabled usingsetEnabledCipherSuites(String[]).- Returns:
- the SSL cipher suite names that are supported by this engine.
-
getSupportedProtocols
Returns the protocol names that are supported by this engine. These protocols can be enables usingsetEnabledProtocols(String[]).- Returns:
- the protocol names that are supported by this engine.
-
getUseClientMode
public abstract boolean getUseClientMode()Returns whether this engine is set to act in client mode when handshaking.- Returns:
trueif the engine is set to do handshaking in client mode.
-
getWantClientAuth
public abstract boolean getWantClientAuth()Returns whether this engine will request client authentication.- Returns:
trueif client authentication will be requested,falseotherwise.
-
isInboundDone
public abstract boolean isInboundDone()Returns whether no more inbound data will be accepted by this engine.- Returns:
trueif no more inbound data will be accepted by this engine,falseotherwise.
-
isOutboundDone
public abstract boolean isOutboundDone()Returns whether no more outbound data will be produced by this engine.- Returns:
trueif no more outbound data will be producted by this engine,otherwisefalse.
-
setEnabledCipherSuites
Sets the SSL cipher suite names that should be enabled in this engine instance. Only cipher suites listed bygetSupportedCipherSuites()are allowed.- Parameters:
suites- the SSL cipher suite names to be enabled.- Throws:
IllegalArgumentException- if one of the specified cipher suites is not supported, or ifsuitesisnull.
-
setEnabledProtocols
Sets the protocol version names that should be enabled in this engine instance. Only protocols listed bygetSupportedProtocols()are allowed.- Parameters:
protocols- the protocol version names to be enabled.- Throws:
IllegalArgumentException- if one of the protocol version names is not supported, or ifprotocolsisnull.
-
setEnableSessionCreation
public abstract void setEnableSessionCreation(boolean flag)Sets whether new SSL sessions may be established by this engine instance.- Parameters:
flag-trueif new SSL sessions may be established,falseif existing SSL sessions must be reused.
-
setNeedClientAuth
public abstract void setNeedClientAuth(boolean need)Sets whether this engine must require client authentication. The client authentication is one of:- authentication required
- authentication requested
- no authentication needed
setWantClientAuth(boolean).- Parameters:
need-trueif client authentication is required,falseif no authentication is needed.
-
setUseClientMode
public abstract void setUseClientMode(boolean mode)Sets whether this engine should act in client (or server) mode when handshaking.- Parameters:
mode-trueif this engine should act in client mode,falseif not.- Throws:
IllegalArgumentException- if this method is called after starting the initial handshake.
-
setWantClientAuth
public abstract void setWantClientAuth(boolean want)Sets whether this engine should request client authentication. The client authentication is one of the following:- authentication required
- authentication requested
- no authentication needed
setNeedClientAuth(boolean).- Parameters:
want-trueif client authentication should be requested,falseif no authentication is needed.
-
unwrap
public abstract SSLEngineResult unwrap(ByteBuffer src, ByteBuffer[] dsts, int offset, int length) throws SSLExceptionDecodes the incoming network data buffer into application data buffers. If a handshake has not been started yet, it will automatically be started.- Parameters:
src- the buffer with incoming network datadsts- the array of destination buffers for incoming application data.offset- the offset in the array of destination buffers to which data is to be transferred.length- the maximum number of destination buffers to be used.- Returns:
- the result object of this operation.
- Throws:
SSLException- if a problem occurred while processing the data.IndexOutOfBoundsException- iflengthis greater thandsts.length - offset.ReadOnlyBufferException- if one of the destination buffers is read-only.IllegalArgumentException- ifsrc,dsts, or one of the entries indstsisnull.IllegalStateException- if the engine does not have all the needed settings (e.g. client/server mode not set).
-
wrap
public abstract SSLEngineResult wrap(ByteBuffer[] srcs, int offset, int length, ByteBuffer dst) throws SSLExceptionEncodes the outgoing application data buffers into the network data buffer. If a handshake has not been started yet, it will automatically be started.- Parameters:
srcs- the array of source buffers of outgoing application data.offset- the offset in the array of source buffers from which data is to be retrieved.length- the maximum number of source buffers to be used.dst- the destination buffer for network data.- Returns:
- the result object of this operation.
- Throws:
SSLException- if a problem occurred while processing the data.IndexOutOfBoundsException- iflengthis greater thansrcs.length - offset.ReadOnlyBufferException- if the destination buffer is readonly.IllegalArgumentException- ifsrcs,dst, or one the entries insrcsisnull.IllegalStateException- if the engine does not have all the needed settings (e.g. client/server mode not set).
-
unwrap
Decodes the incoming network data buffer into the application data buffer. If a handshake has not been started yet, it will automatically be started.- Parameters:
src- the buffer with incoming network datadst- the destination buffer for incoming application data.- Returns:
- the result object of this operation.
- Throws:
SSLException- if a problem occurred while processing the data.ReadOnlyBufferException- if one of the destination buffers is read-only.IllegalArgumentException- ifsrcordstisnull.IllegalStateException- if the engine does not have all the needed settings (e.g. client/server mode not set).
-
unwrap
Decodes the incoming network data buffer into the application data buffers. If a handshake has not been started yet, it will automatically be started.- Parameters:
src- the buffer with incoming network datadsts- the array of destination buffers for incoming application data.- Returns:
- the result object of this operation.
- Throws:
SSLException- if a problem occurred while processing the data.ReadOnlyBufferException- if one of the destination buffers is read-only.IllegalArgumentException- ifsrcordstsisnull.IllegalStateException- if the engine does not have all the needed settings (e.g. client/server mode not set).
-
wrap
Encodes the outgoing application data buffers into the network data buffer. If a handshake has not been started yet, it will automatically be started.- Parameters:
srcs- the array of source buffers of outgoing application data.dst- the destination buffer for network data.- Returns:
- the result object of this operation.
- Throws:
SSLException- if a problem occurred while processing the data.ReadOnlyBufferException- if the destination buffer is readonly.IllegalArgumentException- ifsrcsordstisnull.IllegalStateException- if the engine does not have all the needed settings (e.g. client/server mode not set).
-
wrap
Encodes the outgoing application data buffer into the network data buffer. If a handshake has not been started yet, it will automatically be started.- Parameters:
src- the source buffers of outgoing application data.dst- the destination buffer for network data.- Returns:
- the result object of this operation.
- Throws:
SSLException- if a problem occurred while processing the data.ReadOnlyBufferException- if the destination buffer is readonly.IllegalArgumentException- ifsrcordstisnull.IllegalStateException- if the engine does not have all the needed settings (e.g. client/server mode not set).
-
getSSLParameters
Returns a new SSLParameters based on this SSLSocket's current cipher suites, protocols, and client authentication settings.- Since:
- 1.6
-
setSSLParameters
Sets various SSL handshake parameters based on the SSLParameter argument. Specifically, sets the SSLEngine's enabled cipher suites if the parameter's cipher suites are non-null. Similarly sets the enabled protocols. If the parameters specify the want or need for client authentication, those requirements are set on the SSLEngine, otherwise both are set to false.- Since:
- 1.6
-