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


public abstract class SSLEngine extends Object

A class which enables secure communications using protocols such as the Secure Sockets Layer (SSL) or IETF RFC 2246 "Transport Layer Security" (TLS) protocols, but is transport independent.

The secure communications modes 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 cipher suite 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 SSLSocket class provides much of the same security functionality, but all of the inbound and outbound data is automatically transported using the underlying Socket, which by design uses a blocking model. While this is appropriate for many applications, this model does not provide the scalability required by large servers.

The primary distinction of an SSLEngine is that it operates on inbound and outbound byte streams, independent of the transport mechanism. It is the responsibility of theSSLEngine user to arrange for reliable I/O transport to the peer. By separating the SSL/TLS/DTLS abstraction from the I/O transport mechanism, the SSLEngine can be used for a wide variety of I/O types, such as non-blocking I/O (polling), selectable non-blocking I/O, Socket and the traditional Input/OutputStreams, local ByteBuffers or byte arrays, future asynchronous I/O models , and so on.

At a high level, the SSLEngine appears thus:

               app data

            |           ^
            |     |     |
            v     |     |
       +----+-----|-----+----+
       |          |          |
       |       SSL|Engine    |

wrap() | | | unwrap() | OUTBOUND | INBOUND | | | | +----+-----|-----+----+ | | ^ | | | v |

               net data

Application data (also known as plaintext or cleartext) is data which is produced or consumed by an application. Its counterpart is network data, which consists of either handshaking and/or ciphertext (encrypted) data, and destined to be transported via an I/O mechanism. Inbound data is data which has been received from the peer, and outbound data is destined for the peer.

(In the context of an SSLEngine, the term "handshake data" is taken to mean any data exchanged to establish and control a secure connection. Handshake data includes the SSL/TLS/DTLS messages "alert", "change_cipher_spec," and "handshake.")

There are five distinct phases to an SSLEngine.

  1. Creation - The SSLEngine has been created and initialized, but has not yet been used. During this phase, an application may set any SSLEngine-specific settings (enabled cipher suites, whether the SSLEngine should handshake in client or server mode, and so on). Once handshaking has begun, though, any new settings (except client/server mode, see below) will be used for the next handshake.
  2. Initial Handshake - The initial handshake is a procedure by which the two peers exchange communication parameters until an SSLSession is established. Application data can not be sent during this phase.
  3. Application Data - Once the communication parameters have been established and the handshake is complete, application data may flow through the SSLEngine. Outbound application messages are encrypted and integrity protected, and inbound messages reverse the process.
  4. Rehandshaking - Either side may request a renegotiation of the session at any time during the Application Data phase. New handshaking data can be intermixed among the application data. Before starting the rehandshake phase, the application may reset the SSL/TLS/DTLS communication parameters such as the list of enabled ciphersuites and whether to use client authentication, but can not change between client/server modes. As before, once handshaking has begun, any new SSLEngine configuration settings will not be used until the next handshake.
  5. Closure - When the connection is no longer needed, the client and the server applications should each close both sides of their respective connections. For SSLEngine objects, an application should call closeOutbound() and send any remaining messages to the peer. Likewise, an application should receive any remaining messages from the peer before callingcloseInbound(). The underlying transport mechanism can then be closed after both sides of the SSLEngine have been closed. If the connection is not closed in an orderly manner (for example closeInbound() is called before the peer's write closure notification has been received), exceptions will be raised to indicate that an error has occurred. Once an engine is closed, it is not reusable: a new SSLEngine must be created.

An SSLEngine is created by calling SSLContext.createSSLEngine() from an initializedSSLContext. Any configuration parameters should be set before making the first call towrap(), unwrap(), orbeginHandshake(). These methods all trigger the initial handshake.

Data moves through the engine by calling wrap() or unwrap() on outbound or inbound data, respectively. Depending on the state of the SSLEngine, a wrap() call may consume application data from the source buffer and may produce network data in the destination buffer. The outbound data may contain application and/or handshake data. A call tounwrap() will examine the source buffer and may advance the handshake if the data is handshaking information, or may place application data in the destination buffer if the data is application. The state of the underlying SSL/TLS/DTLS algorithm will determine when data is consumed and produced.

Calls to wrap() and unwrap() return anSSLEngineResult which indicates the status of the operation, and (optionally) how to interact with the engine to make progress.

The SSLEngine produces/consumes complete SSL/TLS/DTLS packets only, and does not store application data internally between calls to wrap()/unwrap(). Thus input and outputByteBuffers must be sized appropriately to hold the maximum record that can be produced. Calls to SSLSession.getPacketBufferSize() and SSLSession.getApplicationBufferSize() should be used to determine the appropriate buffer sizes. The size of the outbound application data buffer generally does not matter. If buffer conditions do not allow for the proper consumption/production of data, the application must determine (via SSLEngineResult) and correct the problem, and then try the call again.

For example, unwrap() will return a SSLEngineResult.Status.BUFFER_OVERFLOW result if the engine determines that there is not enough destination buffer space available. Applications should call SSLSession.getApplicationBufferSize() and compare that value with the space available in the destination buffer, enlarging the buffer if necessary. Similarly, if unwrap() were to return a SSLEngineResult.Status.BUFFER_UNDERFLOW, the application should call SSLSession.getPacketBufferSize() to ensure that the source buffer has enough room to hold a record (enlarging if necessary), and then obtain more inbound data.


   SSLEngineResult r = engine.unwrap(src, dst);
   switch (r.getStatus()) {
   BUFFER_OVERFLOW:
       // Could attempt to drain the dst buffer of any already obtained
       // data, but we'll just increase it to the size needed.
       int appSize = engine.getSession().getApplicationBufferSize();
       ByteBuffer b = ByteBuffer.allocate(appSize + dst.position());
       dst.flip();
       b.put(dst);
       dst = b;
       // retry the operation.
       break;
   BUFFER_UNDERFLOW:
       int netSize = engine.getSession().getPacketBufferSize();
       // Resize buffer if needed.
       if (netSize > src.capacity()) {
           ByteBuffer b = ByteBuffer.allocate(netSize);
           src.flip();
           b.put(src);
           src = b;
       }
       // Obtain more inbound network data for src,
       // then retry the operation.
       break;
   // other cases: CLOSED, OK.
   }
 

Unlike SSLSocket, all methods of SSLEngine are non-blocking. SSLEngine implementations may require the results of tasks that may take an extended period of time to complete, or may even block. For example, a TrustManager may need to connect to a remote certificate validation service, or a KeyManager might need to prompt a user to determine which certificate to use as part of client authentication. Additionally, creating cryptographic signatures and verifying them can be slow, seemingly blocking.

For any operation which may potentially block, theSSLEngine will create a Runnable delegated task. When SSLEngineResult indicates that a delegated task result is needed, the application must call getDelegatedTask() to obtain an outstanding delegated task and call its run() method (possibly using a different thread depending on the compute strategy). The application should continue obtaining delegated tasks until no more exist, and try the original operation again.

At the end of a communication session, applications should properly close the SSL/TLS/DTLS link. The SSL/TLS/DTLS protocols have closure handshake messages, and these messages should be communicated to the peer before releasing the SSLEngine and closing the underlying transport mechanism. A close can be initiated by one of: an SSLException, an inbound closure handshake message, or one of the close methods. In all cases, closure handshake messages are generated by the engine, and wrap() should be repeatedly called until the resulting SSLEngineResult's status returns "CLOSED", or isOutboundDone() returns true. All data obtained from the wrap() method should be sent to the peer.

closeOutbound() is used to signal the engine that the application will not be sending any more data.

A peer will signal its intent to close by sending its own closure handshake message. After this message has been received and processed by the local SSLEngine's unwrap() call, the application can detect the close by callingunwrap() and looking for a SSLEngineResult with status "CLOSED", or if isInboundDone() returns true. If for some reason the peer closes the communication link without sending the proper SSL/TLS/DTLS closure message, the application can detect the end-of-stream and can signal the engine via closeInbound() that there will no more inbound messages to process. Some applications might choose to require orderly shutdown messages from a peer, in which case they can check that the closure was generated by a handshake message and not by an end-of-stream condition.

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 cipher suite be selected.

Each SSL/TLS/DTLS connection must have one client and one server, thus each endpoint must decide which role to assume. This choice determines who begins the handshaking process as well as which type of messages should be sent by each party. The method setUseClientMode(boolean) configures the mode. Note that the default mode for a new SSLEngine is provider-specific. Applications should set the mode explicitly before invoking other methods of the SSLEngine. Once the initial handshaking has started, an SSLEngine can not switch between client and server modes, even when performing renegotiations.

Applications might choose to process delegated tasks in different threads. When an SSLEngine is created, the current AccessControlContext is saved. All future delegated tasks will be processed using this context: that is, all access control decisions will be made using the context captured at engine creation.


Concurrency Notes: There are two concurrency issues to be aware of:

  1. The wrap() and unwrap() methods may execute concurrently of each other.
  2. The SSL/TLS/DTLS protocols employ ordered packets. Applications must take care to ensure that generated packets are delivered in sequence. If packets arrive out-of-order, unexpected or fatal results may occur.
    For example:
    synchronized (outboundLock) {
    sslEngine.wrap(src, dst);
    outboundQueue.put(dst);
    }

As a corollary, two threads must not attempt to call the same method (either wrap() or unwrap()) concurrently, because there is no way to guarantee the eventual packet ordering.

Since:

1.5

See Also:

SSLContext, SSLSocket, SSLServerSocket, SSLSession, Socket

Constructors

Modifier Constructor Description
protected SSLEngine() Constructor for an SSLEngine providing no hints for an internal session reuse strategy.
protected SSLEngine​(String peerHost, int peerPort) Constructor for an SSLEngine.
Modifier and Type Method Description
abstract void beginHandshake() Initiates handshaking (initial or renegotiation) on this SSLEngine.
abstract void closeInbound() Signals that no more inbound network data will be sent to this SSLEngine.
abstract void closeOutbound() Signals that no more outbound application data will be sent on this SSLEngine.
String getApplicationProtocol() Returns the most recent application protocol value negotiated for this connection.
abstract Runnable getDelegatedTask() Returns a delegated Runnable task for this SSLEngine.
abstract String[] getEnabledCipherSuites() Returns the names of the SSL cipher suites which are currently enabled for use on this engine.
abstract String[] getEnabledProtocols() Returns the names of the protocol versions which are currently enabled for use with this SSLEngine.
abstract boolean getEnableSessionCreation() Returns true if new SSL sessions may be established by this engine.
String getHandshakeApplicationProtocol() Returns the application protocol value negotiated on a SSL/TLS handshake currently in progress.
BiFunction<SSLEngine,​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/DTLS handshake.
abstract SSLEngineResult.HandshakeStatus getHandshakeStatus() Returns the current handshake status for this SSLEngine.
abstract boolean getNeedClientAuth() Returns true if the engine will require client authentication.
String getPeerHost() Returns the host name of the peer.
int getPeerPort() Returns the port number of the peer.
abstract SSLSession getSession() Returns the SSLSession in use in thisSSLEngine.
SSLParameters getSSLParameters() Returns the SSLParameters in effect for this SSLEngine.
abstract String[] getSupportedCipherSuites() Returns the names of the cipher suites which could be enabled for use on this engine.
abstract String[] getSupportedProtocols() Returns the names of the protocols which could be enabled for use with this SSLEngine.
abstract boolean getUseClientMode() Returns true if the engine is set to use client mode when handshaking.
abstract boolean getWantClientAuth() Returns true if the engine will request client authentication.
abstract boolean isInboundDone() Returns whether unwrap(ByteBuffer, ByteBuffer) will accept any more inbound data messages.
abstract boolean isOutboundDone() Returns whether wrap(ByteBuffer, ByteBuffer) will produce any more outbound data messages.
abstract void setEnabledCipherSuites​(String[] suites) Sets the cipher suites enabled for use on this engine.
abstract void setEnabledProtocols​(String[] protocols) Set the protocol versions enabled for use on this engine.
abstract void setEnableSessionCreation​(boolean flag) Controls whether new SSL sessions may be established by this engine.
void setHandshakeApplicationProtocolSelector​(BiFunction<SSLEngine,​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 engine to require client authentication.
void setSSLParameters​(SSLParameters params) Applies SSLParameters to this engine.
abstract void setUseClientMode​(boolean mode) Configures the engine to use client (or server) mode when handshaking.
abstract void setWantClientAuth​(boolean want) Configures the engine to request client authentication.
SSLEngineResult unwrap​(ByteBuffer src,ByteBuffer dst) Attempts to decode SSL/TLS/DTLS network data into a plaintext application data buffer.
SSLEngineResult unwrap​(ByteBuffer src,ByteBuffer[] dsts) Attempts to decode SSL/TLS/DTLS network data into a sequence of plaintext application data buffers.
abstract SSLEngineResult unwrap​(ByteBuffer src,ByteBuffer[] dsts, int offset, int length) Attempts to decode SSL/TLS/DTLS network data into a subsequence of plaintext application data buffers.
abstract SSLEngineResult wrap​(ByteBuffer[] srcs, int offset, int length,ByteBuffer dst) Attempts to encode plaintext bytes from a subsequence of data buffers into SSL/TLS/DTLS network data.
SSLEngineResult wrap​(ByteBuffer[] srcs,ByteBuffer dst) Attempts to encode plaintext bytes from a sequence of data buffers into SSL/TLS/DTLS network data.
SSLEngineResult wrap​(ByteBuffer src,ByteBuffer dst) Attempts to encode a buffer of plaintext application data into SSL/TLS/DTLS network data.

Methods declared in class java.lang.Object

[clone](../../../java/lang/Object.html#clone%28%29), [equals](../../../java/lang/Object.html#equals%28java.lang.Object%29), [finalize](../../../java/lang/Object.html#finalize%28%29), [getClass](../../../java/lang/Object.html#getClass%28%29), [hashCode](../../../java/lang/Object.html#hashCode%28%29), [notify](../../../java/lang/Object.html#notify%28%29), [notifyAll](../../../java/lang/Object.html#notifyAll%28%29), [toString](../../../java/lang/Object.html#toString%28%29), [wait](../../../java/lang/Object.html#wait%28%29), [wait](../../../java/lang/Object.html#wait%28long%29), [wait](../../../java/lang/Object.html#wait%28long,int%29)