Path: blob/master/src/java.base/share/classes/javax/net/ssl/SSLEngine.java
41159 views
/*1* Copyright (c) 2003, 2021, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package javax.net.ssl;2627import java.nio.ByteBuffer;28import java.nio.ReadOnlyBufferException;29import java.util.List;30import java.util.function.BiFunction;313233/**34* A class which enables secure communications using protocols such as35* the Secure Sockets Layer (SSL) or36* <A HREF="http://www.ietf.org/rfc/rfc2246.txt"> IETF RFC 2246 "Transport37* Layer Security" (TLS) </A> protocols, but is transport independent.38* <P>39* The secure communications modes include: <UL>40*41* <LI> <em>Integrity Protection</em>. SSL/TLS/DTLS protects against42* modification of messages by an active wiretapper.43*44* <LI> <em>Authentication</em>. In most modes, SSL/TLS/DTLS provides45* peer authentication. Servers are usually authenticated, and46* clients may be authenticated as requested by servers.47*48* <LI> <em>Confidentiality (Privacy Protection)</em>. In most49* modes, SSL/TLS/DTLS encrypts data being sent between client and50* server. This protects the confidentiality of data, so that51* passive wiretappers won't see sensitive data such as financial52* information or personal information of many kinds.53*54* </UL>55*56* These kinds of protection are specified by a "cipher suite", which57* is a combination of cryptographic algorithms used by a given SSL58* connection. During the negotiation process, the two endpoints must59* agree on a cipher suite that is available in both environments. If60* there is no such suite in common, no SSL connection can be61* established, and no data can be exchanged.62* <P>63* The cipher suite used is established by a negotiation process called64* "handshaking". The goal of this process is to create or rejoin a65* "session", which may protect many connections over time. After66* handshaking has completed, you can access session attributes by67* using the {@link #getSession()} method.68* <P>69* The {@code SSLSocket} class provides much of the same security70* functionality, but all of the inbound and outbound data is71* automatically transported using the underlying {@link72* java.net.Socket Socket}, which by design uses a blocking model.73* While this is appropriate for many applications, this model does not74* provide the scalability required by large servers.75* <P>76* The primary distinction of an {@code SSLEngine} is that it77* operates on inbound and outbound byte streams, independent of the78* transport mechanism. It is the responsibility of the79* {@code SSLEngine} user to arrange for reliable I/O transport to80* the peer. By separating the SSL/TLS/DTLS abstraction from the I/O81* transport mechanism, the {@code SSLEngine} can be used for a82* wide variety of I/O types, such as {@link83* java.nio.channels.spi.AbstractSelectableChannel#configureBlocking(boolean)84* non-blocking I/O (polling)}, {@link java.nio.channels.Selector85* selectable non-blocking I/O}, {@link java.net.Socket Socket} and the86* traditional Input/OutputStreams, local {@link java.nio.ByteBuffer87* ByteBuffers} or byte arrays, <A88* HREF="http://www.jcp.org/en/jsr/detail?id=203"> future asynchronous89* I/O models </A>, and so on.90* <P>91* At a high level, the {@code SSLEngine} appears thus:92*93* <pre>94* app data95*96* | ^97* | | |98* v | |99* +----+-----|-----+----+100* | | |101* | SSL|Engine |102* wrap() | | | unwrap()103* | OUTBOUND | INBOUND |104* | | |105* +----+-----|-----+----+106* | | ^107* | | |108* v |109*110* net data111* </pre>112* Application data (also known as plaintext or cleartext) is data which113* is produced or consumed by an application. Its counterpart is114* network data, which consists of either handshaking and/or ciphertext115* (encrypted) data, and destined to be transported via an I/O116* mechanism. Inbound data is data which has been received from the117* peer, and outbound data is destined for the peer.118* <P>119* (In the context of an {@code SSLEngine}, the term "handshake120* data" is taken to mean any data exchanged to establish and control a121* secure connection. Handshake data includes the SSL/TLS/DTLS messages122* "alert", "change_cipher_spec," and "handshake.")123* <P>124* There are five distinct phases to an {@code SSLEngine}.125*126* <OL>127* <li> Creation - The {@code SSLEngine} has been created and128* initialized, but has not yet been used. During this phase, an129* application may set any {@code SSLEngine}-specific settings130* (enabled cipher suites, whether the {@code SSLEngine} should131* handshake in client or server mode, and so on). Once132* handshaking has begun, though, any new settings (except133* client/server mode, see below) will be used for134* the next handshake.135*136* <li> Initial Handshake - The initial handshake is a procedure by137* which the two peers exchange communication parameters until an138* SSLSession is established. Application data can not be sent during139* this phase.140*141* <li> Application Data - Once the communication parameters have142* been established and the handshake is complete, application data143* may flow through the {@code SSLEngine}. Outbound144* application messages are encrypted and integrity protected,145* and inbound messages reverse the process.146*147* <li> Rehandshaking - Either side may request a renegotiation of148* the session at any time during the Application Data phase. New149* handshaking data can be intermixed among the application data.150* Before starting the rehandshake phase, the application may151* reset the SSL/TLS/DTLS communication parameters such as the list of152* enabled ciphersuites and whether to use client authentication,153* but can not change between client/server modes. As before, once154* handshaking has begun, any new {@code SSLEngine}155* configuration settings will not be used until the next156* handshake.157*158* <li> Closure - When the connection is no longer needed, the client159* and the server applications should each close both sides of their160* respective connections. For {@code SSLEngine} objects, an161* application should call {@link SSLEngine#closeOutbound()} and162* send any remaining messages to the peer. Likewise, an application163* should receive any remaining messages from the peer before calling164* {@link SSLEngine#closeInbound()}. The underlying transport mechanism165* can then be closed after both sides of the {@code SSLEngine} have166* been closed. If the connection is not closed in an orderly manner167* (for example {@link SSLEngine#closeInbound()} is called before the168* peer's write closure notification has been received), exceptions169* will be raised to indicate that an error has occurred. Once an170* engine is closed, it is not reusable: a new {@code SSLEngine}171* must be created.172* </OL>173* An {@code SSLEngine} is created by calling {@link174* SSLContext#createSSLEngine()} from an initialized175* {@code SSLContext}. Any configuration176* parameters should be set before making the first call to177* {@code wrap()}, {@code unwrap()}, or178* {@code beginHandshake()}. These methods all trigger the179* initial handshake.180* <P>181* Data moves through the engine by calling {@link #wrap(ByteBuffer,182* ByteBuffer) wrap()} or {@link #unwrap(ByteBuffer, ByteBuffer)183* unwrap()} on outbound or inbound data, respectively. Depending on184* the state of the {@code SSLEngine}, a {@code wrap()} call185* may consume application data from the source buffer and may produce186* network data in the destination buffer. The outbound data187* may contain application and/or handshake data. A call to188* {@code unwrap()} will examine the source buffer and may189* advance the handshake if the data is handshaking information, or190* may place application data in the destination buffer if the data191* is application. The state of the underlying SSL/TLS/DTLS algorithm192* will determine when data is consumed and produced.193* <P>194* Calls to {@code wrap()} and {@code unwrap()} return an195* {@code SSLEngineResult} which indicates the status of the196* operation, and (optionally) how to interact with the engine to make197* progress.198* <P>199* The {@code SSLEngine} produces/consumes complete SSL/TLS/DTLS200* packets only, and does not store application data internally between201* calls to {@code wrap()/unwrap()}. Thus input and output202* {@code ByteBuffer}s must be sized appropriately to hold the203* maximum record that can be produced. Calls to {@link204* SSLSession#getPacketBufferSize()} and {@link205* SSLSession#getApplicationBufferSize()} should be used to determine206* the appropriate buffer sizes. The size of the outbound application207* data buffer generally does not matter. If buffer conditions do not208* allow for the proper consumption/production of data, the application209* must determine (via {@link SSLEngineResult}) and correct the210* problem, and then try the call again.211* <P>212* For example, {@code unwrap()} will return a {@link213* SSLEngineResult.Status#BUFFER_OVERFLOW} result if the engine214* determines that there is not enough destination buffer space available.215* Applications should call {@link SSLSession#getApplicationBufferSize()}216* and compare that value with the space available in the destination buffer,217* enlarging the buffer if necessary. Similarly, if {@code unwrap()}218* were to return a {@link SSLEngineResult.Status#BUFFER_UNDERFLOW}, the219* application should call {@link SSLSession#getPacketBufferSize()} to ensure220* that the source buffer has enough room to hold a record (enlarging if221* necessary), and then obtain more inbound data.222*223* <pre>{@code224* SSLEngineResult r = engine.unwrap(src, dst);225* switch (r.getStatus()) {226* case BUFFER_OVERFLOW:227* // Could attempt to drain the dst buffer of any already obtained228* // data, but we'll just increase it to the size needed.229* int appSize = engine.getSession().getApplicationBufferSize();230* ByteBuffer b = ByteBuffer.allocate(appSize + dst.position());231* dst.flip();232* b.put(dst);233* dst = b;234* // retry the operation.235* break;236* case BUFFER_UNDERFLOW:237* int netSize = engine.getSession().getPacketBufferSize();238* // Resize buffer if needed.239* if (netSize > src.capacity()) {240* ByteBuffer b = ByteBuffer.allocate(netSize);241* src.flip();242* b.put(src);243* src = b;244* }245* // Obtain more inbound network data for src,246* // then retry the operation.247* break;248* // other cases: CLOSED, OK.249* }250* }</pre>251*252* <P>253* Unlike {@code SSLSocket}, all methods of SSLEngine are254* non-blocking. {@code SSLEngine} implementations may255* require the results of tasks that may take an extended period of256* time to complete, or may even block. For example, a TrustManager257* may need to connect to a remote certificate validation service,258* or a KeyManager might need to prompt a user to determine which259* certificate to use as part of client authentication. Additionally,260* creating cryptographic signatures and verifying them can be slow,261* seemingly blocking.262* <P>263* For any operation which may potentially block, the264* {@code SSLEngine} will create a {@link java.lang.Runnable}265* delegated task. When {@code SSLEngineResult} indicates that a266* delegated task result is needed, the application must call {@link267* #getDelegatedTask()} to obtain an outstanding delegated task and268* call its {@link java.lang.Runnable#run() run()} method (possibly using269* a different thread depending on the compute strategy). The270* application should continue obtaining delegated tasks until no more271* exist, and try the original operation again.272* <P>273* At the end of a communication session, applications should properly274* close the SSL/TLS/DTLS link. The SSL/TLS/DTLS protocols have closure275* handshake messages, and these messages should be communicated to the276* peer before releasing the {@code SSLEngine} and closing the277* underlying transport mechanism. A close can be initiated by one of:278* an SSLException, an inbound closure handshake message, or one of the279* close methods. In all cases, closure handshake messages are280* generated by the engine, and {@code wrap()} should be repeatedly281* called until the resulting {@code SSLEngineResult}'s status282* returns "CLOSED", or {@link #isOutboundDone()} returns true. All283* data obtained from the {@code wrap()} method should be sent to the284* peer.285* <P>286* {@link #closeOutbound()} is used to signal the engine that the287* application will not be sending any more data.288* <P>289* A peer will signal its intent to close by sending its own closure290* handshake message. After this message has been received and291* processed by the local {@code SSLEngine}'s {@code unwrap()}292* call, the application can detect the close by calling293* {@code unwrap()} and looking for a {@code SSLEngineResult}294* with status "CLOSED", or if {@link #isInboundDone()} returns true.295* If for some reason the peer closes the communication link without296* sending the proper SSL/TLS/DTLS closure message, the application can297* detect the end-of-stream and can signal the engine via {@link298* #closeInbound()} that there will no more inbound messages to299* process. Some applications might choose to require orderly shutdown300* messages from a peer, in which case they can check that the closure301* was generated by a handshake message and not by an end-of-stream302* condition.303* <P>304* There are two groups of cipher suites which you will need to know305* about when managing cipher suites:306*307* <UL>308* <LI> <em>Supported</em> cipher suites: all the suites which are309* supported by the SSL implementation. This list is reported310* using {@link #getSupportedCipherSuites()}.311*312* <LI> <em>Enabled</em> cipher suites, which may be fewer than313* the full set of supported suites. This group is set using the314* {@link #setEnabledCipherSuites(String [])} method, and315* queried using the {@link #getEnabledCipherSuites()} method.316* Initially, a default set of cipher suites will be enabled on a317* new engine that represents the minimum suggested318* configuration.319* </UL>320*321* Implementation defaults require that only cipher suites which322* authenticate servers and provide confidentiality be enabled by323* default. Only if both sides explicitly agree to unauthenticated324* and/or non-private (unencrypted) communications will such a325* cipher suite be selected.326* <P>327* Each SSL/TLS/DTLS connection must have one client and one server, thus328* each endpoint must decide which role to assume. This choice determines329* who begins the handshaking process as well as which type of messages330* should be sent by each party. The method {@link331* #setUseClientMode(boolean)} configures the mode. Note that the332* default mode for a new {@code SSLEngine} is provider-specific.333* Applications should set the mode explicitly before invoking other334* methods of the {@code SSLEngine}. Once the initial handshaking has335* started, an {@code SSLEngine} can not switch between client and server336* modes, even when performing renegotiations.337* <P>338* The ApplicationProtocol {@code String} values returned by the methods339* in this class are in the network byte representation sent by the peer.340* The bytes could be directly compared, or converted to its Unicode341* {code String} format for comparison.342*343* <blockquote><pre>344* String networkString = sslEngine.getHandshakeApplicationProtocol();345* byte[] bytes = networkString.getBytes(StandardCharsets.ISO_8859_1);346*347* //348* // Match using bytes:349* //350* // "http/1.1" (7-bit ASCII values same in UTF-8)351* // MEETEI MAYEK LETTERS "HUK UN I" (Unicode 0xabcd->0xabcf)352* //353* String HTTP1_1 = "http/1.1";354* byte[] HTTP1_1_BYTES = HTTP1_1.getBytes(StandardCharsets.UTF_8);355*356* byte[] HUK_UN_I_BYTES = new byte[] {357* (byte) 0xab, (byte) 0xcd,358* (byte) 0xab, (byte) 0xce,359* (byte) 0xab, (byte) 0xcf};360*361* if ((Arrays.compare(bytes, HTTP1_1_BYTES) == 0 )362* || Arrays.compare(bytes, HUK_UN_I_BYTES) == 0) {363* ...364* }365*366* //367* // Alternatively match using string.equals() if we know the ALPN value368* // was encoded from a {@code String} using a certain character set,369* // for example {@code UTF-8}. The ALPN value must first be properly370* // decoded to a Unicode {@code String} before use.371* //372* String unicodeString = new String(bytes, StandardCharsets.UTF_8);373* if (unicodeString.equals(HTTP1_1)374* || unicodeString.equals("\u005cuabcd\u005cuabce\u005cuabcf")) {375* ...376* }377* </pre></blockquote>378*379* <P>380* Applications might choose to process delegated tasks in different381* threads. When an {@code SSLEngine}382* is created, the current {@link java.security.AccessControlContext}383* is saved. All future delegated tasks will be processed using this384* context: that is, all access control decisions will be made using the385* context captured at engine creation.386*387* <HR>388*389* <B>Concurrency Notes</B>:390* There are two concurrency issues to be aware of:391*392* <OL>393* <li>The {@code wrap()} and {@code unwrap()} methods394* may execute concurrently of each other.395*396* <li> The SSL/TLS/DTLS protocols employ ordered packets.397* Applications must take care to ensure that generated packets398* are delivered in sequence. If packets arrive399* out-of-order, unexpected or fatal results may occur.400* <P>401* For example:402*403* <pre>404* synchronized (outboundLock) {405* sslEngine.wrap(src, dst);406* outboundQueue.put(dst);407* }408* </pre>409*410* As a corollary, two threads must not attempt to call the same method411* (either {@code wrap()} or {@code unwrap()}) concurrently,412* because there is no way to guarantee the eventual packet ordering.413* </OL>414*415* @see SSLContext416* @see SSLSocket417* @see SSLServerSocket418* @see SSLSession419* @see java.net.Socket420*421* @since 1.5422* @author Brad R. Wetmore423*/424425public abstract class SSLEngine {426427private String peerHost = null;428private int peerPort = -1;429430/**431* Constructor for an {@code SSLEngine} providing no hints432* for an internal session reuse strategy.433*434* @see SSLContext#createSSLEngine()435* @see SSLSessionContext436*/437protected SSLEngine() {438}439440/**441* Constructor for an {@code SSLEngine}.442* <P>443* {@code SSLEngine} implementations may use the444* {@code peerHost} and {@code peerPort} parameters as hints445* for their internal session reuse strategy.446* <P>447* Some cipher suites (such as Kerberos) require remote hostname448* information. Implementations of this class should use this449* constructor to use Kerberos.450* <P>451* The parameters are not authenticated by the452* {@code SSLEngine}.453*454* @param peerHost the name of the peer host455* @param peerPort the port number of the peer456* @see SSLContext#createSSLEngine(String, int)457* @see SSLSessionContext458*/459protected SSLEngine(String peerHost, int peerPort) {460this.peerHost = peerHost;461this.peerPort = peerPort;462}463464/**465* Returns the host name of the peer.466* <P>467* Note that the value is not authenticated, and should not be468* relied upon.469*470* @return the host name of the peer, or null if nothing is471* available.472*/473public String getPeerHost() {474return peerHost;475}476477/**478* Returns the port number of the peer.479* <P>480* Note that the value is not authenticated, and should not be481* relied upon.482*483* @return the port number of the peer, or -1 if nothing is484* available.485*/486public int getPeerPort() {487return peerPort;488}489490/**491* Attempts to encode a buffer of plaintext application data into492* SSL/TLS/DTLS network data.493* <P>494* An invocation of this method behaves in exactly the same manner495* as the invocation:496* <blockquote><pre>497* {@link #wrap(ByteBuffer [], int, int, ByteBuffer)498* engine.wrap(new ByteBuffer [] { src }, 0, 1, dst);}499* </pre></blockquote>500*501* @param src502* a {@code ByteBuffer} containing outbound application data503* @param dst504* a {@code ByteBuffer} to hold outbound network data505* @return an {@code SSLEngineResult} describing the result506* of this operation.507* @throws SSLException508* A problem was encountered while processing the509* data that caused the {@code SSLEngine} to abort.510* See the class description for more information on511* engine closure.512* @throws ReadOnlyBufferException513* if the {@code dst} buffer is read-only.514* @throws IllegalArgumentException515* if either {@code src} or {@code dst}516* is null.517* @throws IllegalStateException if the client/server mode518* has not yet been set.519* @see #wrap(ByteBuffer [], int, int, ByteBuffer)520*/521public SSLEngineResult wrap(ByteBuffer src,522ByteBuffer dst) throws SSLException {523return wrap(new ByteBuffer [] { src }, 0, 1, dst);524}525526/**527* Attempts to encode plaintext bytes from a sequence of data528* buffers into SSL/TLS/DTLS network data.529* <P>530* An invocation of this method behaves in exactly the same manner531* as the invocation:532* <blockquote><pre>533* {@link #wrap(ByteBuffer [], int, int, ByteBuffer)534* engine.wrap(srcs, 0, srcs.length, dst);}535* </pre></blockquote>536*537* @param srcs538* an array of {@code ByteBuffers} containing the539* outbound application data540* @param dst541* a {@code ByteBuffer} to hold outbound network data542* @return an {@code SSLEngineResult} describing the result543* of this operation.544* @throws SSLException545* A problem was encountered while processing the546* data that caused the {@code SSLEngine} to abort.547* See the class description for more information on548* engine closure.549* @throws ReadOnlyBufferException550* if the {@code dst} buffer is read-only.551* @throws IllegalArgumentException552* if either {@code srcs} or {@code dst}553* is null, or if any element in {@code srcs} is null.554* @throws IllegalStateException if the client/server mode555* has not yet been set.556* @see #wrap(ByteBuffer [], int, int, ByteBuffer)557*/558public SSLEngineResult wrap(ByteBuffer [] srcs,559ByteBuffer dst) throws SSLException {560if (srcs == null) {561throw new IllegalArgumentException("src == null");562}563return wrap(srcs, 0, srcs.length, dst);564}565566567/**568* Attempts to encode plaintext bytes from a subsequence of data569* buffers into SSL/TLS/DTLS network data. This <i>"gathering"</i>570* operation encodes, in a single invocation, a sequence of bytes571* from one or more of a given sequence of buffers. Gathering572* wraps are often useful when implementing network protocols or573* file formats that, for example, group data into segments574* consisting of one or more fixed-length headers followed by a575* variable-length body. See576* {@link java.nio.channels.GatheringByteChannel} for more577* information on gathering, and {@link578* java.nio.channels.GatheringByteChannel#write(ByteBuffer[],579* int, int)} for more information on the subsequence580* behavior.581* <P>582* Depending on the state of the SSLEngine, this method may produce583* network data without consuming any application data (for example,584* it may generate handshake data.)585* <P>586* The application is responsible for reliably transporting the587* network data to the peer, and for ensuring that data created by588* multiple calls to wrap() is transported in the same order in which589* it was generated. The application must properly synchronize590* multiple calls to this method.591* <P>592* If this {@code SSLEngine} has not yet started its initial593* handshake, this method will automatically start the handshake.594* <P>595* This method will attempt to produce SSL/TLS/DTLS records, and will596* consume as much source data as possible, but will never consume597* more than the sum of the bytes remaining in each buffer. Each598* {@code ByteBuffer}'s position is updated to reflect the599* amount of data consumed or produced. The limits remain the600* same.601* <P>602* The underlying memory used by the {@code srcs} and603* {@code dst ByteBuffer}s must not be the same.604* <P>605* See the class description for more information on engine closure.606*607* @param srcs608* an array of {@code ByteBuffers} containing the609* outbound application data610* @param offset611* The offset within the buffer array of the first buffer from612* which bytes are to be retrieved; it must be non-negative613* and no larger than {@code srcs.length}614* @param length615* The maximum number of buffers to be accessed; it must be616* non-negative and no larger than617* {@code srcs.length} - {@code offset}618* @param dst619* a {@code ByteBuffer} to hold outbound network data620* @return an {@code SSLEngineResult} describing the result621* of this operation.622* @throws SSLException623* A problem was encountered while processing the624* data that caused the {@code SSLEngine} to abort.625* See the class description for more information on626* engine closure.627* @throws IndexOutOfBoundsException628* if the preconditions on the {@code offset} and629* {@code length} parameters do not hold.630* @throws ReadOnlyBufferException631* if the {@code dst} buffer is read-only.632* @throws IllegalArgumentException633* if either {@code srcs} or {@code dst}634* is null, or if any element in the {@code srcs}635* subsequence specified is null.636* @throws IllegalStateException if the client/server mode637* has not yet been set.638* @see java.nio.channels.GatheringByteChannel639* @see java.nio.channels.GatheringByteChannel#write(640* ByteBuffer[], int, int)641*/642public abstract SSLEngineResult wrap(ByteBuffer [] srcs, int offset,643int length, ByteBuffer dst) throws SSLException;644645/**646* Attempts to decode SSL/TLS/DTLS network data into a plaintext647* application data buffer.648* <P>649* An invocation of this method behaves in exactly the same manner650* as the invocation:651* <blockquote><pre>652* {@link #unwrap(ByteBuffer, ByteBuffer [], int, int)653* engine.unwrap(src, new ByteBuffer [] { dst }, 0, 1);}654* </pre></blockquote>655*656* @param src657* a {@code ByteBuffer} containing inbound network data.658* @param dst659* a {@code ByteBuffer} to hold inbound application data.660* @return an {@code SSLEngineResult} describing the result661* of this operation.662* @throws SSLException663* A problem was encountered while processing the664* data that caused the {@code SSLEngine} to abort.665* See the class description for more information on666* engine closure.667* @throws ReadOnlyBufferException668* if the {@code dst} buffer is read-only.669* @throws IllegalArgumentException670* if either {@code src} or {@code dst}671* is null.672* @throws IllegalStateException if the client/server mode673* has not yet been set.674* @see #unwrap(ByteBuffer, ByteBuffer [], int, int)675*/676public SSLEngineResult unwrap(ByteBuffer src,677ByteBuffer dst) throws SSLException {678return unwrap(src, new ByteBuffer [] { dst }, 0, 1);679}680681/**682* Attempts to decode SSL/TLS/DTLS network data into a sequence of plaintext683* application data buffers.684* <P>685* An invocation of this method behaves in exactly the same manner686* as the invocation:687* <blockquote><pre>688* {@link #unwrap(ByteBuffer, ByteBuffer [], int, int)689* engine.unwrap(src, dsts, 0, dsts.length);}690* </pre></blockquote>691*692* @param src693* a {@code ByteBuffer} containing inbound network data.694* @param dsts695* an array of {@code ByteBuffer}s to hold inbound696* application data.697* @return an {@code SSLEngineResult} describing the result698* of this operation.699* @throws SSLException700* A problem was encountered while processing the701* data that caused the {@code SSLEngine} to abort.702* See the class description for more information on703* engine closure.704* @throws ReadOnlyBufferException705* if any of the {@code dst} buffers are read-only.706* @throws IllegalArgumentException707* if either {@code src} or {@code dsts}708* is null, or if any element in {@code dsts} is null.709* @throws IllegalStateException if the client/server mode710* has not yet been set.711* @see #unwrap(ByteBuffer, ByteBuffer [], int, int)712*/713public SSLEngineResult unwrap(ByteBuffer src,714ByteBuffer [] dsts) throws SSLException {715if (dsts == null) {716throw new IllegalArgumentException("dsts == null");717}718return unwrap(src, dsts, 0, dsts.length);719}720721/**722* Attempts to decode SSL/TLS/DTLS network data into a subsequence of723* plaintext application data buffers. This <i>"scattering"</i>724* operation decodes, in a single invocation, a sequence of bytes725* into one or more of a given sequence of buffers. Scattering726* unwraps are often useful when implementing network protocols or727* file formats that, for example, group data into segments728* consisting of one or more fixed-length headers followed by a729* variable-length body. See730* {@link java.nio.channels.ScatteringByteChannel} for more731* information on scattering, and {@link732* java.nio.channels.ScatteringByteChannel#read(ByteBuffer[],733* int, int)} for more information on the subsequence734* behavior.735* <P>736* Depending on the state of the SSLEngine, this method may consume737* network data without producing any application data (for example,738* it may consume handshake data.)739* <P>740* The application is responsible for reliably obtaining the network741* data from the peer, and for invoking unwrap() on the data in the742* order it was received. The application must properly synchronize743* multiple calls to this method.744* <P>745* If this {@code SSLEngine} has not yet started its initial746* handshake, this method will automatically start the handshake.747* <P>748* This method will attempt to consume one complete SSL/TLS/DTLS network749* packet, but will never consume more than the sum of the bytes750* remaining in the buffers. Each {@code ByteBuffer}'s751* position is updated to reflect the amount of data consumed or752* produced. The limits remain the same.753* <P>754* The underlying memory used by the {@code src} and755* {@code dsts ByteBuffer}s must not be the same.756* <P>757* The inbound network buffer may be modified as a result of this758* call: therefore if the network data packet is required for some759* secondary purpose, the data should be duplicated before calling this760* method. Note: the network data will not be useful to a second761* SSLEngine, as each SSLEngine contains unique random state which762* influences the SSL/TLS/DTLS messages.763* <P>764* See the class description for more information on engine closure.765*766* @param src767* a {@code ByteBuffer} containing inbound network data.768* @param dsts769* an array of {@code ByteBuffer}s to hold inbound770* application data.771* @param offset772* The offset within the buffer array of the first buffer from773* which bytes are to be transferred; it must be non-negative774* and no larger than {@code dsts.length}.775* @param length776* The maximum number of buffers to be accessed; it must be777* non-negative and no larger than778* {@code dsts.length} - {@code offset}.779* @return an {@code SSLEngineResult} describing the result780* of this operation.781* @throws SSLException782* A problem was encountered while processing the783* data that caused the {@code SSLEngine} to abort.784* See the class description for more information on785* engine closure.786* @throws IndexOutOfBoundsException787* If the preconditions on the {@code offset} and788* {@code length} parameters do not hold.789* @throws ReadOnlyBufferException790* if any of the {@code dst} buffers are read-only.791* @throws IllegalArgumentException792* if either {@code src} or {@code dsts}793* is null, or if any element in the {@code dsts}794* subsequence specified is null.795* @throws IllegalStateException if the client/server mode796* has not yet been set.797* @see java.nio.channels.ScatteringByteChannel798* @see java.nio.channels.ScatteringByteChannel#read(799* ByteBuffer[], int, int)800*/801public abstract SSLEngineResult unwrap(ByteBuffer src,802ByteBuffer [] dsts, int offset, int length) throws SSLException;803804805/**806* Returns a delegated {@code Runnable} task for807* this {@code SSLEngine}.808* <P>809* {@code SSLEngine} operations may require the results of810* operations that block, or may take an extended period of time to811* complete. This method is used to obtain an outstanding {@link812* java.lang.Runnable} operation (task). Each task must be assigned813* a thread (possibly the current) to perform the {@link814* java.lang.Runnable#run() run} operation. Once the815* {@code run} method returns, the {@code Runnable} object816* is no longer needed and may be discarded.817* <P>818* Delegated tasks run in the {@code AccessControlContext}819* in place when this object was created.820* <P>821* A call to this method will return each outstanding task822* exactly once.823* <P>824* Multiple delegated tasks can be run in parallel.825*826* @return a delegated {@code Runnable} task, or null827* if none are available.828*/829public abstract Runnable getDelegatedTask();830831832/**833* Signals that no more inbound network data will be sent834* to this {@code SSLEngine}.835* <P>836* If the application initiated the closing process by calling837* {@link #closeOutbound()}, under some circumstances it is not838* required that the initiator wait for the peer's corresponding839* close message. (See section 7.2.1 of the TLS specification (<A840* HREF="http://www.ietf.org/rfc/rfc2246.txt">RFC 2246</A>) for more841* information on waiting for closure alerts.) In such cases, this842* method need not be called.843* <P>844* But if the application did not initiate the closure process, or845* if the circumstances above do not apply, this method should be846* called whenever the end of the SSL/TLS/DTLS data stream is reached.847* This ensures closure of the inbound side, and checks that the848* peer followed the SSL/TLS/DTLS close procedure properly, thus849* detecting possible truncation attacks.850* <P>851* This method is idempotent: if the inbound side has already852* been closed, this method does not do anything.853* <P>854* {@link #wrap(ByteBuffer, ByteBuffer) wrap()} should be855* called to flush any remaining handshake data.856*857* @throws SSLException858* if this engine has not received the proper SSL/TLS/DTLS close859* notification message from the peer.860*861* @see #isInboundDone()862* @see #isOutboundDone()863*/864public abstract void closeInbound() throws SSLException;865866867/**868* Returns whether {@link #unwrap(ByteBuffer, ByteBuffer)} will869* accept any more inbound data messages.870*871* @return true if the {@code SSLEngine} will not872* consume anymore network data (and by implication,873* will not produce any more application data.)874* @see #closeInbound()875*/876public abstract boolean isInboundDone();877878879/**880* Signals that no more outbound application data will be sent881* on this {@code SSLEngine}.882* <P>883* This method is idempotent: if the outbound side has already884* been closed, this method does not do anything.885* <P>886* {@link #wrap(ByteBuffer, ByteBuffer)} should be887* called to flush any remaining handshake data.888*889* @see #isOutboundDone()890*/891public abstract void closeOutbound();892893894/**895* Returns whether {@link #wrap(ByteBuffer, ByteBuffer)} will896* produce any more outbound data messages.897* <P>898* Note that during the closure phase, a {@code SSLEngine} may899* generate handshake closure data that must be sent to the peer.900* {@code wrap()} must be called to generate this data. When901* this method returns true, no more outbound data will be created.902*903* @return true if the {@code SSLEngine} will not produce904* any more network data905*906* @see #closeOutbound()907* @see #closeInbound()908*/909public abstract boolean isOutboundDone();910911912/**913* Returns the names of the cipher suites which could be enabled for use914* on this engine. Normally, only a subset of these will actually915* be enabled by default, since this list may include cipher suites which916* do not meet quality of service requirements for those defaults. Such917* cipher suites might be useful in specialized applications.918* <P>919* The returned array includes cipher suites from the list of standard920* cipher suite names in the <a href=921* "{@docRoot}/../specs/security/standard-names.html#jsse-cipher-suite-names">922* JSSE Cipher Suite Names</a> section of the Java Cryptography923* Architecture Standard Algorithm Name Documentation, and may also924* include other cipher suites that the provider supports.925*926* @return an array of cipher suite names927* @see #getEnabledCipherSuites()928* @see #setEnabledCipherSuites(String [])929*/930public abstract String [] getSupportedCipherSuites();931932933/**934* Returns the names of the SSL cipher suites which are currently935* enabled for use on this engine. When an SSLEngine is first936* created, all enabled cipher suites support a minimum quality of937* service. Thus, in some environments this value might be empty.938* <P>939* Note that even if a suite is enabled, it may never be used. This940* can occur if the peer does not support it, or its use is restricted,941* or the requisite certificates (and private keys) for the suite are942* not available, or an anonymous suite is enabled but authentication943* is required.944* <P>945* The returned array includes cipher suites from the list of standard946* cipher suite names in the <a href=947* "{@docRoot}/../specs/security/standard-names.html#jsse-cipher-suite-names">948* JSSE Cipher Suite Names</a> section of the Java Cryptography949* Architecture Standard Algorithm Name Documentation, and may also950* include other cipher suites that the provider supports.951*952* @return an array of cipher suite names953* @see #getSupportedCipherSuites()954* @see #setEnabledCipherSuites(String [])955*/956public abstract String [] getEnabledCipherSuites();957958959/**960* Sets the cipher suites enabled for use on this engine.961* <P>962* Each cipher suite in the {@code suites} parameter must have963* been listed by getSupportedCipherSuites(), or the method will964* fail. Following a successful call to this method, only suites965* listed in the {@code suites} parameter are enabled for use.966* <P>967* Note that the standard list of cipher suite names may be found in the968* <a href=969* "{@docRoot}/../specs/security/standard-names.html#jsse-cipher-suite-names">970* JSSE Cipher Suite Names</a> section of the Java Cryptography971* Architecture Standard Algorithm Name Documentation. Providers972* may support cipher suite names not found in this list or might not973* use the recommended name for a certain cipher suite.974* <P>975* See {@link #getEnabledCipherSuites()} for more information976* on why a specific cipher suite may never be used on a engine.977*978* @param suites Names of all the cipher suites to enable979* @throws IllegalArgumentException when one or more of the ciphers980* named by the parameter is not supported, or when the981* parameter is null.982* @see #getSupportedCipherSuites()983* @see #getEnabledCipherSuites()984*/985public abstract void setEnabledCipherSuites(String suites []);986987988/**989* Returns the names of the protocols which could be enabled for use990* with this {@code SSLEngine}.991*992* @return an array of protocols supported993*/994public abstract String [] getSupportedProtocols();995996997/**998* Returns the names of the protocol versions which are currently999* enabled for use with this {@code SSLEngine}.1000* <P>1001* Note that even if a protocol is enabled, it may never be used.1002* This can occur if the peer does not support the protocol, or its1003* use is restricted, or there are no enabled cipher suites supported1004* by the protocol.1005*1006* @return an array of protocols1007* @see #setEnabledProtocols(String [])1008*/1009public abstract String [] getEnabledProtocols();101010111012/**1013* Set the protocol versions enabled for use on this engine.1014* <P>1015* The protocols must have been listed by getSupportedProtocols()1016* as being supported. Following a successful call to this method,1017* only protocols listed in the {@code protocols} parameter1018* are enabled for use.1019*1020* @param protocols Names of all the protocols to enable.1021* @throws IllegalArgumentException when one or more of1022* the protocols named by the parameter is not supported or1023* when the protocols parameter is null.1024* @see #getEnabledProtocols()1025*/1026public abstract void setEnabledProtocols(String protocols[]);102710281029/**1030* Returns the {@code SSLSession} in use in this1031* {@code SSLEngine}.1032* <P>1033* These can be long lived, and frequently correspond to an entire1034* login session for some user. The session specifies a particular1035* cipher suite which is being actively used by all connections in1036* that session, as well as the identities of the session's client1037* and server.1038* <P>1039* Unlike {@link SSLSocket#getSession()}1040* this method does not block until handshaking is complete.1041* <P>1042* Until the initial handshake has completed, this method returns1043* a session object which reports an invalid cipher suite of1044* "SSL_NULL_WITH_NULL_NULL".1045*1046* @return the {@code SSLSession} for this {@code SSLEngine}1047* @see SSLSession1048*/1049public abstract SSLSession getSession();105010511052/**1053* Returns the {@code SSLSession} being constructed during a SSL/TLS/DTLS1054* handshake.1055* <p>1056* TLS/DTLS protocols may negotiate parameters that are needed when using1057* an instance of this class, but before the {@code SSLSession} has1058* been completely initialized and made available via {@code getSession}.1059* For example, the list of valid signature algorithms may restrict1060* the type of certificates that can be used during TrustManager1061* decisions, or the maximum TLS/DTLS fragment packet sizes can be1062* resized to better support the network environment.1063* <p>1064* This method provides early access to the {@code SSLSession} being1065* constructed. Depending on how far the handshake has progressed,1066* some data may not yet be available for use. For example, if a1067* remote server will be sending a Certificate chain, but that chain1068* has yet not been processed, the {@code getPeerCertificates}1069* method of {@code SSLSession} will throw a1070* SSLPeerUnverifiedException. Once that chain has been processed,1071* {@code getPeerCertificates} will return the proper value.1072*1073* @see SSLSocket1074* @see SSLSession1075* @see ExtendedSSLSession1076* @see X509ExtendedKeyManager1077* @see X509ExtendedTrustManager1078*1079* @return null if this instance is not currently handshaking, or1080* if the current handshake has not progressed far enough to1081* create a basic SSLSession. Otherwise, this method returns the1082* {@code SSLSession} currently being negotiated.1083* @throws UnsupportedOperationException if the underlying provider1084* does not implement the operation.1085*1086* @since 1.71087*/1088public SSLSession getHandshakeSession() {1089throw new UnsupportedOperationException();1090}109110921093/**1094* Initiates handshaking (initial or renegotiation) on this SSLEngine.1095* <P>1096* This method is not needed for the initial handshake, as the1097* {@code wrap()} and {@code unwrap()} methods will1098* implicitly call this method if handshaking has not already begun.1099* <P>1100* Note that the peer may also request a session renegotiation with1101* this {@code SSLEngine} by sending the appropriate1102* session renegotiate handshake message.1103* <P>1104* Unlike the {@link SSLSocket#startHandshake()1105* SSLSocket#startHandshake()} method, this method does not block1106* until handshaking is completed.1107* <P>1108* To force a complete SSL/TLS/DTLS session renegotiation, the current1109* session should be invalidated prior to calling this method.1110* <P>1111* Some protocols may not support multiple handshakes on an existing1112* engine and may throw an {@code SSLException}.1113*1114* @throws SSLException1115* if a problem was encountered while signaling the1116* {@code SSLEngine} to begin a new handshake.1117* See the class description for more information on1118* engine closure.1119* @throws IllegalStateException if the client/server mode1120* has not yet been set.1121* @see SSLSession#invalidate()1122*/1123public abstract void beginHandshake() throws SSLException;112411251126/**1127* Returns the current handshake status for this {@code SSLEngine}.1128*1129* @return the current {@code SSLEngineResult.HandshakeStatus}.1130*/1131public abstract SSLEngineResult.HandshakeStatus getHandshakeStatus();113211331134/**1135* Configures the engine to use client (or server) mode when1136* handshaking.1137* <P>1138* This method must be called before any handshaking occurs.1139* Once handshaking has begun, the mode can not be reset for the1140* life of this engine.1141* <P>1142* Servers normally authenticate themselves, and clients1143* are not required to do so.1144*1145* @implNote1146* The JDK SunJSSE provider implementation default for this mode is false.1147*1148* @param mode true if the engine should start its handshaking1149* in "client" mode1150* @throws IllegalArgumentException if a mode change is attempted1151* after the initial handshake has begun.1152* @see #getUseClientMode()1153*/1154public abstract void setUseClientMode(boolean mode);115511561157/**1158* Returns true if the engine is set to use client mode when1159* handshaking.1160*1161* @implNote1162* The JDK SunJSSE provider implementation returns false unless1163* {@link setUseClientMode(boolean)} is used to change the mode to true.1164*1165* @return true if the engine should do handshaking1166* in "client" mode1167* @see #setUseClientMode(boolean)1168*/1169public abstract boolean getUseClientMode();117011711172/**1173* Configures the engine to <i>require</i> client authentication. This1174* option is only useful for engines in the server mode.1175* <P>1176* An engine's client authentication setting is one of the following:1177* <ul>1178* <li> client authentication required1179* <li> client authentication requested1180* <li> no client authentication desired1181* </ul>1182* <P>1183* Unlike {@link #setWantClientAuth(boolean)}, if this option is set and1184* the client chooses not to provide authentication information1185* about itself, <i>the negotiations will stop and the engine will1186* begin its closure procedure</i>.1187* <P>1188* Calling this method overrides any previous setting made by1189* this method or {@link #setWantClientAuth(boolean)}.1190*1191* @param need set to true if client authentication is required,1192* or false if no client authentication is desired.1193* @see #getNeedClientAuth()1194* @see #setWantClientAuth(boolean)1195* @see #getWantClientAuth()1196* @see #setUseClientMode(boolean)1197*/1198public abstract void setNeedClientAuth(boolean need);119912001201/**1202* Returns true if the engine will <i>require</i> client authentication.1203* This option is only useful to engines in the server mode.1204*1205* @return true if client authentication is required,1206* or false if no client authentication is desired.1207* @see #setNeedClientAuth(boolean)1208* @see #setWantClientAuth(boolean)1209* @see #getWantClientAuth()1210* @see #setUseClientMode(boolean)1211*/1212public abstract boolean getNeedClientAuth();121312141215/**1216* Configures the engine to <i>request</i> client authentication.1217* This option is only useful for engines in the server mode.1218* <P>1219* An engine's client authentication setting is one of the following:1220* <ul>1221* <li> client authentication required1222* <li> client authentication requested1223* <li> no client authentication desired1224* </ul>1225* <P>1226* Unlike {@link #setNeedClientAuth(boolean)}, if this option is set and1227* the client chooses not to provide authentication information1228* about itself, <i>the negotiations will continue</i>.1229* <P>1230* Calling this method overrides any previous setting made by1231* this method or {@link #setNeedClientAuth(boolean)}.1232*1233* @param want set to true if client authentication is requested,1234* or false if no client authentication is desired.1235* @see #getWantClientAuth()1236* @see #setNeedClientAuth(boolean)1237* @see #getNeedClientAuth()1238* @see #setUseClientMode(boolean)1239*/1240public abstract void setWantClientAuth(boolean want);124112421243/**1244* Returns true if the engine will <i>request</i> client authentication.1245* This option is only useful for engines in the server mode.1246*1247* @return true if client authentication is requested,1248* or false if no client authentication is desired.1249* @see #setNeedClientAuth(boolean)1250* @see #getNeedClientAuth()1251* @see #setWantClientAuth(boolean)1252* @see #setUseClientMode(boolean)1253*/1254public abstract boolean getWantClientAuth();125512561257/**1258* Controls whether new SSL sessions may be established by this engine.1259* If session creations are not allowed, and there are no1260* existing sessions to resume, there will be no successful1261* handshaking.1262*1263* @param flag true indicates that sessions may be created; this1264* is the default. false indicates that an existing session1265* must be resumed1266* @see #getEnableSessionCreation()1267*/1268public abstract void setEnableSessionCreation(boolean flag);126912701271/**1272* Returns true if new SSL sessions may be established by this engine.1273*1274* @return true indicates that sessions may be created; this1275* is the default. false indicates that an existing session1276* must be resumed1277* @see #setEnableSessionCreation(boolean)1278*/1279public abstract boolean getEnableSessionCreation();12801281/**1282* Returns the SSLParameters in effect for this SSLEngine.1283* The ciphersuites and protocols of the returned SSLParameters1284* are always non-null.1285*1286* @return the SSLParameters in effect for this SSLEngine.1287* @since 1.61288*/1289public SSLParameters getSSLParameters() {1290SSLParameters params = new SSLParameters();1291params.setCipherSuites(getEnabledCipherSuites());1292params.setProtocols(getEnabledProtocols());1293if (getNeedClientAuth()) {1294params.setNeedClientAuth(true);1295} else if (getWantClientAuth()) {1296params.setWantClientAuth(true);1297}1298return params;1299}13001301/**1302* Applies SSLParameters to this engine.1303*1304* <p>This means:1305* <ul>1306* <li>If {@code params.getCipherSuites()} is non-null,1307* {@code setEnabledCipherSuites()} is called with that value.</li>1308* <li>If {@code params.getProtocols()} is non-null,1309* {@code setEnabledProtocols()} is called with that value.</li>1310* <li>If {@code params.getNeedClientAuth()} or1311* {@code params.getWantClientAuth()} return {@code true},1312* {@code setNeedClientAuth(true)} and1313* {@code setWantClientAuth(true)} are called, respectively;1314* otherwise {@code setWantClientAuth(false)} is called.</li>1315* <li>If {@code params.getServerNames()} is non-null, the engine will1316* configure its server names with that value.</li>1317* <li>If {@code params.getSNIMatchers()} is non-null, the engine will1318* configure its SNI matchers with that value.</li>1319* </ul>1320*1321* @param params the parameters1322* @throws IllegalArgumentException if the setEnabledCipherSuites() or1323* the setEnabledProtocols() call fails1324* @since 1.61325*/1326public void setSSLParameters(SSLParameters params) {1327String[] s;1328s = params.getCipherSuites();1329if (s != null) {1330setEnabledCipherSuites(s);1331}1332s = params.getProtocols();1333if (s != null) {1334setEnabledProtocols(s);1335}1336if (params.getNeedClientAuth()) {1337setNeedClientAuth(true);1338} else if (params.getWantClientAuth()) {1339setWantClientAuth(true);1340} else {1341setWantClientAuth(false);1342}1343}13441345/**1346* Returns the most recent application protocol value negotiated for this1347* connection.1348* <p>1349* If supported by the underlying SSL/TLS/DTLS implementation,1350* application name negotiation mechanisms such as <a1351* href="http://www.ietf.org/rfc/rfc7301.txt"> RFC 7301 </a>, the1352* Application-Layer Protocol Negotiation (ALPN), can negotiate1353* application-level values between peers.1354*1355* @implSpec1356* The implementation in this class throws1357* {@code UnsupportedOperationException} and performs no other action.1358*1359* @return null if it has not yet been determined if application1360* protocols might be used for this connection, an empty1361* {@code String} if application protocols values will not1362* be used, or a non-empty application protocol {@code String}1363* if a value was successfully negotiated.1364* @throws UnsupportedOperationException if the underlying provider1365* does not implement the operation.1366* @since 91367*/1368public String getApplicationProtocol() {1369throw new UnsupportedOperationException();1370}13711372/**1373* Returns the application protocol value negotiated on a SSL/TLS1374* handshake currently in progress.1375* <p>1376* Like {@link #getHandshakeSession()},1377* a connection may be in the middle of a handshake. The1378* application protocol may or may not yet be available.1379*1380* @implSpec1381* The implementation in this class throws1382* {@code UnsupportedOperationException} and performs no other action.1383*1384* @return null if it has not yet been determined if application1385* protocols might be used for this handshake, an empty1386* {@code String} if application protocols values will not1387* be used, or a non-empty application protocol {@code String}1388* if a value was successfully negotiated.1389* @throws UnsupportedOperationException if the underlying provider1390* does not implement the operation.1391* @since 91392*/1393public String getHandshakeApplicationProtocol() {1394throw new UnsupportedOperationException();1395}13961397/**1398* Registers a callback function that selects an application protocol1399* value for a SSL/TLS/DTLS handshake.1400* The function overrides any values supplied using1401* {@link SSLParameters#setApplicationProtocols1402* SSLParameters.setApplicationProtocols} and it supports the following1403* type parameters:1404* <blockquote>1405* <dl>1406* <dt> {@code SSLEngine}1407* <dd> The function's first argument allows the current {@code SSLEngine}1408* to be inspected, including the handshake session and configuration1409* settings.1410* <dt> {@code List<String>}1411* <dd> The function's second argument lists the application protocol names1412* advertised by the TLS peer.1413* <dt> {@code String}1414* <dd> The function's result is an application protocol name, or null to1415* indicate that none of the advertised names are acceptable.1416* If the return value is an empty {@code String} then application1417* protocol indications will not be used.1418* If the return value is null (no value chosen) or is a value that1419* was not advertised by the peer, the underlying protocol will1420* determine what action to take. (For example, ALPN will send a1421* "no_application_protocol" alert and terminate the connection.)1422* </dl>1423* </blockquote>1424*1425* For example, the following call registers a callback function that1426* examines the TLS handshake parameters and selects an application protocol1427* name:1428* <pre>{@code1429* serverEngine.setHandshakeApplicationProtocolSelector(1430* (serverEngine, clientProtocols) -> {1431* SSLSession session = serverEngine.getHandshakeSession();1432* return chooseApplicationProtocol(1433* serverEngine,1434* clientProtocols,1435* session.getProtocol(),1436* session.getCipherSuite());1437* });1438* }</pre>1439*1440* @apiNote1441* This method should be called by TLS server applications before the TLS1442* handshake begins. Also, this {@code SSLEngine} should be configured with1443* parameters that are compatible with the application protocol selected by1444* the callback function. For example, enabling a poor choice of cipher1445* suites could result in no suitable application protocol.1446* See {@link SSLParameters}.1447*1448* @implSpec1449* The implementation in this class throws1450* {@code UnsupportedOperationException} and performs no other action.1451*1452* @param selector the callback function, or null to disable the callback1453* functionality.1454* @throws UnsupportedOperationException if the underlying provider1455* does not implement the operation.1456* @since 91457*/1458public void setHandshakeApplicationProtocolSelector(1459BiFunction<SSLEngine, List<String>, String> selector) {1460throw new UnsupportedOperationException();1461}14621463/**1464* Retrieves the callback function that selects an application protocol1465* value during a SSL/TLS/DTLS handshake.1466* See {@link #setHandshakeApplicationProtocolSelector1467* setHandshakeApplicationProtocolSelector}1468* for the function's type parameters.1469*1470* @implSpec1471* The implementation in this class throws1472* {@code UnsupportedOperationException} and performs no other action.1473*1474* @return the callback function, or null if none has been set.1475* @throws UnsupportedOperationException if the underlying provider1476* does not implement the operation.1477* @since 91478*/1479public BiFunction<SSLEngine, List<String>, String>1480getHandshakeApplicationProtocolSelector() {1481throw new UnsupportedOperationException();1482}1483}148414851486