View Javadoc
1   /*
2    * Copyright 2016 The Netty Project
3    *
4    * The Netty Project licenses this file to you under the Apache License,
5    * version 2.0 (the "License"); you may not use this file except in compliance
6    * with the License. You may obtain a copy of the License at:
7    *
8    *   https://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12   * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13   * License for the specific language governing permissions and limitations
14   * under the License.
15   */
16  package io.netty.handler.ssl;
17  
18  import io.netty.buffer.ByteBuf;
19  import io.netty.buffer.ByteBufAllocator;
20  import io.netty.handler.ssl.util.LazyJavaxX509Certificate;
21  import io.netty.handler.ssl.util.LazyX509Certificate;
22  import io.netty.internal.tcnative.AsyncTask;
23  import io.netty.internal.tcnative.Buffer;
24  import io.netty.internal.tcnative.SSL;
25  import io.netty.util.AbstractReferenceCounted;
26  import io.netty.util.CharsetUtil;
27  import io.netty.util.ReferenceCounted;
28  import io.netty.util.ResourceLeakDetector;
29  import io.netty.util.ResourceLeakDetectorFactory;
30  import io.netty.util.ResourceLeakTracker;
31  import io.netty.util.internal.EmptyArrays;
32  import io.netty.util.internal.PlatformDependent;
33  import io.netty.util.internal.StringUtil;
34  import io.netty.util.internal.SuppressJava6Requirement;
35  import io.netty.util.internal.ThrowableUtil;
36  import io.netty.util.internal.UnstableApi;
37  import io.netty.util.internal.logging.InternalLogger;
38  import io.netty.util.internal.logging.InternalLoggerFactory;
39  
40  import java.nio.ByteBuffer;
41  import java.nio.ReadOnlyBufferException;
42  import java.security.Principal;
43  import java.security.cert.Certificate;
44  import java.util.Arrays;
45  import java.util.Collection;
46  import java.util.Collections;
47  import java.util.HashSet;
48  import java.util.LinkedHashSet;
49  import java.util.List;
50  import java.util.Map;
51  import java.util.Set;
52  import java.util.concurrent.ConcurrentHashMap;
53  import java.util.concurrent.locks.Lock;
54  
55  import javax.crypto.spec.SecretKeySpec;
56  import javax.net.ssl.SSLEngine;
57  import javax.net.ssl.SSLEngineResult;
58  import javax.net.ssl.SSLException;
59  import javax.net.ssl.SSLHandshakeException;
60  import javax.net.ssl.SSLParameters;
61  import javax.net.ssl.SSLPeerUnverifiedException;
62  import javax.net.ssl.SSLSession;
63  import javax.net.ssl.SSLSessionBindingEvent;
64  import javax.net.ssl.SSLSessionBindingListener;
65  import javax.security.cert.X509Certificate;
66  
67  import static io.netty.handler.ssl.OpenSsl.memoryAddress;
68  import static io.netty.handler.ssl.SslUtils.SSL_RECORD_HEADER_LENGTH;
69  import static io.netty.util.internal.EmptyArrays.EMPTY_STRINGS;
70  import static io.netty.util.internal.ObjectUtil.checkNotNull;
71  import static io.netty.util.internal.ObjectUtil.checkNotNullArrayParam;
72  import static io.netty.util.internal.ObjectUtil.checkNotNullWithIAE;
73  import static java.lang.Integer.MAX_VALUE;
74  import static java.lang.Math.min;
75  import static javax.net.ssl.SSLEngineResult.HandshakeStatus.FINISHED;
76  import static javax.net.ssl.SSLEngineResult.HandshakeStatus.NEED_TASK;
77  import static javax.net.ssl.SSLEngineResult.HandshakeStatus.NEED_UNWRAP;
78  import static javax.net.ssl.SSLEngineResult.HandshakeStatus.NEED_WRAP;
79  import static javax.net.ssl.SSLEngineResult.HandshakeStatus.NOT_HANDSHAKING;
80  import static javax.net.ssl.SSLEngineResult.Status.BUFFER_OVERFLOW;
81  import static javax.net.ssl.SSLEngineResult.Status.BUFFER_UNDERFLOW;
82  import static javax.net.ssl.SSLEngineResult.Status.CLOSED;
83  import static javax.net.ssl.SSLEngineResult.Status.OK;
84  
85  /**
86   * Implements a {@link SSLEngine} using
87   * <a href="https://www.openssl.org/docs/crypto/BIO_s_bio.html#EXAMPLE">OpenSSL BIO abstractions</a>.
88   * <p>Instances of this class must be {@link #release() released} or else native memory will leak!
89   *
90   * <p>Instances of this class <strong>must</strong> be released before the {@link ReferenceCountedOpenSslContext}
91   * the instance depends upon are released. Otherwise if any method of this class is called which uses the
92   * the {@link ReferenceCountedOpenSslContext} JNI resources the JVM may crash.
93   */
94  public class ReferenceCountedOpenSslEngine extends SSLEngine implements ReferenceCounted, ApplicationProtocolAccessor {
95  
96      private static final InternalLogger logger = InternalLoggerFactory.getInstance(ReferenceCountedOpenSslEngine.class);
97  
98      private static final ResourceLeakDetector<ReferenceCountedOpenSslEngine> leakDetector =
99              ResourceLeakDetectorFactory.instance().newResourceLeakDetector(ReferenceCountedOpenSslEngine.class);
100     private static final int OPENSSL_OP_NO_PROTOCOL_INDEX_SSLV2 = 0;
101     private static final int OPENSSL_OP_NO_PROTOCOL_INDEX_SSLV3 = 1;
102     private static final int OPENSSL_OP_NO_PROTOCOL_INDEX_TLSv1 = 2;
103     private static final int OPENSSL_OP_NO_PROTOCOL_INDEX_TLSv1_1 = 3;
104     private static final int OPENSSL_OP_NO_PROTOCOL_INDEX_TLSv1_2 = 4;
105     private static final int OPENSSL_OP_NO_PROTOCOL_INDEX_TLSv1_3 = 5;
106     private static final int[] OPENSSL_OP_NO_PROTOCOLS = {
107             SSL.SSL_OP_NO_SSLv2,
108             SSL.SSL_OP_NO_SSLv3,
109             SSL.SSL_OP_NO_TLSv1,
110             SSL.SSL_OP_NO_TLSv1_1,
111             SSL.SSL_OP_NO_TLSv1_2,
112             SSL.SSL_OP_NO_TLSv1_3
113     };
114 
115     /**
116      * Depends upon tcnative ... only use if tcnative is available!
117      */
118     static final int MAX_PLAINTEXT_LENGTH = SSL.SSL_MAX_PLAINTEXT_LENGTH;
119     /**
120      * Depends upon tcnative ... only use if tcnative is available!
121      */
122     static final int MAX_RECORD_SIZE = SSL.SSL_MAX_RECORD_LENGTH;
123 
124     private static final SSLEngineResult NEED_UNWRAP_OK = new SSLEngineResult(OK, NEED_UNWRAP, 0, 0);
125     private static final SSLEngineResult NEED_UNWRAP_CLOSED = new SSLEngineResult(CLOSED, NEED_UNWRAP, 0, 0);
126     private static final SSLEngineResult NEED_WRAP_OK = new SSLEngineResult(OK, NEED_WRAP, 0, 0);
127     private static final SSLEngineResult NEED_WRAP_CLOSED = new SSLEngineResult(CLOSED, NEED_WRAP, 0, 0);
128     private static final SSLEngineResult CLOSED_NOT_HANDSHAKING = new SSLEngineResult(CLOSED, NOT_HANDSHAKING, 0, 0);
129 
130     // OpenSSL state
131     private long ssl;
132     private long networkBIO;
133 
134     private enum HandshakeState {
135         /**
136          * Not started yet.
137          */
138         NOT_STARTED,
139         /**
140          * Started via unwrap/wrap.
141          */
142         STARTED_IMPLICITLY,
143         /**
144          * Started via {@link #beginHandshake()}.
145          */
146         STARTED_EXPLICITLY,
147         /**
148          * Handshake is finished.
149          */
150         FINISHED
151     }
152 
153     private HandshakeState handshakeState = HandshakeState.NOT_STARTED;
154     private boolean receivedShutdown;
155     private volatile boolean destroyed;
156     private volatile String applicationProtocol;
157     private volatile boolean needTask;
158     private boolean hasTLSv13Cipher;
159     private boolean sessionSet;
160 
161     // Reference Counting
162     private final ResourceLeakTracker<ReferenceCountedOpenSslEngine> leak;
163     private final AbstractReferenceCounted refCnt = new AbstractReferenceCounted() {
164         @Override
165         public ReferenceCounted touch(Object hint) {
166             if (leak != null) {
167                 leak.record(hint);
168             }
169 
170             return ReferenceCountedOpenSslEngine.this;
171         }
172 
173         @Override
174         protected void deallocate() {
175             shutdown();
176             if (leak != null) {
177                 boolean closed = leak.close(ReferenceCountedOpenSslEngine.this);
178                 assert closed;
179             }
180             parentContext.release();
181         }
182     };
183 
184     private final Set<String> enabledProtocols = new LinkedHashSet<String>();
185 
186     private volatile ClientAuth clientAuth = ClientAuth.NONE;
187 
188     private String endpointIdentificationAlgorithm;
189     // Store as object as AlgorithmConstraints only exists since java 7.
190     private Object algorithmConstraints;
191     private List<String> sniHostNames;
192 
193     // Mark as volatile as accessed by checkSniHostnameMatch(...) and also not specify the SNIMatcher type to allow us
194     // using it with java7.
195     private volatile Collection<?> matchers;
196 
197     // SSL Engine status variables
198     private boolean isInboundDone;
199     private boolean outboundClosed;
200 
201     final boolean jdkCompatibilityMode;
202     private final boolean clientMode;
203     final ByteBufAllocator alloc;
204     private final OpenSslEngineMap engineMap;
205     private final OpenSslApplicationProtocolNegotiator apn;
206     private final ReferenceCountedOpenSslContext parentContext;
207     private final OpenSslSession session;
208     private final ByteBuffer[] singleSrcBuffer = new ByteBuffer[1];
209     private final ByteBuffer[] singleDstBuffer = new ByteBuffer[1];
210     private final boolean enableOcsp;
211     private int maxWrapOverhead;
212     private int maxWrapBufferSize;
213     private Throwable pendingException;
214 
215     /**
216      * Create a new instance.
217      * @param context Reference count release responsibility is not transferred! The callee still owns this object.
218      * @param alloc The allocator to use.
219      * @param peerHost The peer host name.
220      * @param peerPort The peer port.
221      * @param jdkCompatibilityMode {@code true} to behave like described in
222      *                             https://docs.oracle.com/javase/7/docs/api/javax/net/ssl/SSLEngine.html.
223      *                             {@code false} allows for partial and/or multiple packets to be process in a single
224      *                             wrap or unwrap call.
225      * @param leakDetection {@code true} to enable leak detection of this object.
226      */
227     ReferenceCountedOpenSslEngine(ReferenceCountedOpenSslContext context, final ByteBufAllocator alloc, String peerHost,
228                                   int peerPort, boolean jdkCompatibilityMode, boolean leakDetection,
229                                   String endpointIdentificationAlgorithm) {
230         super(peerHost, peerPort);
231         OpenSsl.ensureAvailability();
232         engineMap = context.engineMap;
233         enableOcsp = context.enableOcsp;
234         this.jdkCompatibilityMode = jdkCompatibilityMode;
235         this.alloc = checkNotNull(alloc, "alloc");
236         apn = (OpenSslApplicationProtocolNegotiator) context.applicationProtocolNegotiator();
237         clientMode = context.isClient();
238         this.endpointIdentificationAlgorithm = endpointIdentificationAlgorithm;
239 
240         if (PlatformDependent.javaVersion() >= 7) {
241             session = new ExtendedOpenSslSession(new DefaultOpenSslSession(context.sessionContext())) {
242                 private String[] peerSupportedSignatureAlgorithms;
243                 private List requestedServerNames;
244 
245                 @Override
246                 public List getRequestedServerNames() {
247                     if (clientMode) {
248                         return Java8SslUtils.getSniHostNames(sniHostNames);
249                     } else {
250                         synchronized (ReferenceCountedOpenSslEngine.this) {
251                             if (requestedServerNames == null) {
252                                 if (isDestroyed()) {
253                                     requestedServerNames = Collections.emptyList();
254                                 } else {
255                                     String name = SSL.getSniHostname(ssl);
256                                     if (name == null) {
257                                         requestedServerNames = Collections.emptyList();
258                                     } else {
259                                         // Convert to bytes as we do not want to do any strict validation of the
260                                         // SNIHostName while creating it.
261                                         requestedServerNames =
262                                                 Java8SslUtils.getSniHostName(
263                                                         SSL.getSniHostname(ssl).getBytes(CharsetUtil.UTF_8));
264                                     }
265                                 }
266                             }
267                             return requestedServerNames;
268                         }
269                     }
270                 }
271 
272                 @Override
273                 public String[] getPeerSupportedSignatureAlgorithms() {
274                     synchronized (ReferenceCountedOpenSslEngine.this) {
275                         if (peerSupportedSignatureAlgorithms == null) {
276                             if (isDestroyed()) {
277                                 peerSupportedSignatureAlgorithms = EMPTY_STRINGS;
278                             } else {
279                                 String[] algs = SSL.getSigAlgs(ssl);
280                                 if (algs == null) {
281                                     peerSupportedSignatureAlgorithms = EMPTY_STRINGS;
282                                 } else {
283                                     Set<String> algorithmList = new LinkedHashSet<String>(algs.length);
284                                     for (String alg: algs) {
285                                         String converted = SignatureAlgorithmConverter.toJavaName(alg);
286 
287                                         if (converted != null) {
288                                             algorithmList.add(converted);
289                                         }
290                                     }
291                                     peerSupportedSignatureAlgorithms = algorithmList.toArray(EMPTY_STRINGS);
292                                 }
293                             }
294                         }
295                         return peerSupportedSignatureAlgorithms.clone();
296                     }
297                 }
298 
299                 @Override
300                 public List<byte[]> getStatusResponses() {
301                     byte[] ocspResponse = null;
302                     if (enableOcsp && clientMode) {
303                         synchronized (ReferenceCountedOpenSslEngine.this) {
304                             if (!isDestroyed()) {
305                                 ocspResponse = SSL.getOcspResponse(ssl);
306                             }
307                         }
308                     }
309                     return ocspResponse == null ?
310                             Collections.<byte[]>emptyList() : Collections.singletonList(ocspResponse);
311                 }
312             };
313         } else {
314             session = new DefaultOpenSslSession(context.sessionContext());
315         }
316 
317         if (!context.sessionContext().useKeyManager()) {
318             session.setLocalCertificate(context.keyCertChain);
319         }
320 
321         Lock readerLock = context.ctxLock.readLock();
322         readerLock.lock();
323         final long finalSsl;
324         try {
325             finalSsl = SSL.newSSL(context.ctx, !context.isClient());
326         } finally {
327             readerLock.unlock();
328         }
329         synchronized (this) {
330             ssl = finalSsl;
331             try {
332                 networkBIO = SSL.bioNewByteBuffer(ssl, context.getBioNonApplicationBufferSize());
333 
334                 // Set the client auth mode, this needs to be done via setClientAuth(...) method so we actually call the
335                 // needed JNI methods.
336                 setClientAuth(clientMode ? ClientAuth.NONE : context.clientAuth);
337 
338                 assert context.protocols != null;
339                 this.hasTLSv13Cipher = context.hasTLSv13Cipher;
340 
341                 setEnabledProtocols(context.protocols);
342 
343                 // Use SNI if peerHost was specified and a valid hostname
344                 // See https://github.com/netty/netty/issues/4746
345                 if (clientMode && SslUtils.isValidHostNameForSNI(peerHost)) {
346                     // If on java8 and later we should do some extra validation to ensure we can construct the
347                     // SNIHostName later again.
348                     if (PlatformDependent.javaVersion() >= 8) {
349                         if (Java8SslUtils.isValidHostNameForSNI(peerHost)) {
350                             SSL.setTlsExtHostName(ssl, peerHost);
351                             sniHostNames = Collections.singletonList(peerHost);
352                         }
353                     } else {
354                         SSL.setTlsExtHostName(ssl, peerHost);
355                         sniHostNames = Collections.singletonList(peerHost);
356                     }
357                 }
358 
359                 if (enableOcsp) {
360                     SSL.enableOcsp(ssl);
361                 }
362 
363                 if (!jdkCompatibilityMode) {
364                     SSL.setMode(ssl, SSL.getMode(ssl) | SSL.SSL_MODE_ENABLE_PARTIAL_WRITE);
365                 }
366 
367                 if (isProtocolEnabled(SSL.getOptions(ssl), SSL.SSL_OP_NO_TLSv1_3, SslProtocols.TLS_v1_3)) {
368                     final boolean enableTickets = clientMode ?
369                             ReferenceCountedOpenSslContext.CLIENT_ENABLE_SESSION_TICKET_TLSV13 :
370                             ReferenceCountedOpenSslContext.SERVER_ENABLE_SESSION_TICKET_TLSV13;
371                     if (enableTickets) {
372                         // We should enable session tickets for stateless resumption when TLSv1.3 is enabled. This
373                         // is also done by OpenJDK and without this session resumption does not work at all with
374                         // BoringSSL when TLSv1.3 is used as BoringSSL only supports stateless resumption with TLSv1.3:
375                         //
376                         // See:
377                         //  - https://bugs.openjdk.java.net/browse/JDK-8223922
378                         //  - https://boringssl.googlesource.com/boringssl/+/refs/heads/master/ssl/tls13_server.cc#104
379                         SSL.clearOptions(ssl, SSL.SSL_OP_NO_TICKET);
380                     }
381                 }
382 
383                 if (OpenSsl.isBoringSSL() && clientMode) {
384                     // If in client-mode and BoringSSL let's allow to renegotiate once as the server may use this
385                     // for client auth.
386                     //
387                     // See https://github.com/netty/netty/issues/11529
388                     SSL.setRenegotiateMode(ssl, SSL.SSL_RENEGOTIATE_ONCE);
389                 }
390                 // setMode may impact the overhead.
391                 calculateMaxWrapOverhead();
392 
393                 // Configure any endpoint verification specified by the SslContext.
394                 configureEndpointVerification(endpointIdentificationAlgorithm);
395             } catch (Throwable cause) {
396                 // Call shutdown so we are sure we correctly release all native memory and also guard against the
397                 // case when shutdown() will be called by the finalizer again.
398                 shutdown();
399 
400                 PlatformDependent.throwException(cause);
401             }
402         }
403 
404         // Now that everything looks good and we're going to successfully return the
405         // object so we need to retain a reference to the parent context.
406         parentContext = context;
407         parentContext.retain();
408 
409         // Only create the leak after everything else was executed and so ensure we don't produce a false-positive for
410         // the ResourceLeakDetector.
411         leak = leakDetection ? leakDetector.track(this) : null;
412     }
413 
414     final synchronized String[] authMethods() {
415         if (isDestroyed()) {
416             return EMPTY_STRINGS;
417         }
418         return SSL.authenticationMethods(ssl);
419     }
420 
421     final boolean setKeyMaterial(OpenSslKeyMaterial keyMaterial) throws  Exception {
422         synchronized (this) {
423             if (isDestroyed()) {
424                 return false;
425             }
426             SSL.setKeyMaterial(ssl, keyMaterial.certificateChainAddress(), keyMaterial.privateKeyAddress());
427         }
428         session.setLocalCertificate(keyMaterial.certificateChain());
429         return true;
430     }
431 
432     final synchronized SecretKeySpec masterKey() {
433         if (isDestroyed()) {
434             return null;
435         }
436         return new SecretKeySpec(SSL.getMasterKey(ssl), "AES");
437     }
438 
439     synchronized boolean isSessionReused() {
440         if (isDestroyed()) {
441             return false;
442         }
443         return SSL.isSessionReused(ssl);
444     }
445 
446     /**
447      * Sets the OCSP response.
448      */
449     @UnstableApi
450     public void setOcspResponse(byte[] response) {
451         if (!enableOcsp) {
452             throw new IllegalStateException("OCSP stapling is not enabled");
453         }
454 
455         if (clientMode) {
456             throw new IllegalStateException("Not a server SSLEngine");
457         }
458 
459         synchronized (this) {
460             if (!isDestroyed()) {
461                 SSL.setOcspResponse(ssl, response);
462             }
463         }
464     }
465 
466     /**
467      * Returns the OCSP response or {@code null} if the server didn't provide a stapled OCSP response.
468      */
469     @UnstableApi
470     public byte[] getOcspResponse() {
471         if (!enableOcsp) {
472             throw new IllegalStateException("OCSP stapling is not enabled");
473         }
474 
475         if (!clientMode) {
476             throw new IllegalStateException("Not a client SSLEngine");
477         }
478 
479         synchronized (this) {
480             if (isDestroyed()) {
481                 return EmptyArrays.EMPTY_BYTES;
482             }
483             return SSL.getOcspResponse(ssl);
484         }
485     }
486 
487     @Override
488     public final int refCnt() {
489         return refCnt.refCnt();
490     }
491 
492     @Override
493     public final ReferenceCounted retain() {
494         refCnt.retain();
495         return this;
496     }
497 
498     @Override
499     public final ReferenceCounted retain(int increment) {
500         refCnt.retain(increment);
501         return this;
502     }
503 
504     @Override
505     public final ReferenceCounted touch() {
506         refCnt.touch();
507         return this;
508     }
509 
510     @Override
511     public final ReferenceCounted touch(Object hint) {
512         refCnt.touch(hint);
513         return this;
514     }
515 
516     @Override
517     public final boolean release() {
518         return refCnt.release();
519     }
520 
521     @Override
522     public final boolean release(int decrement) {
523         return refCnt.release(decrement);
524     }
525 
526     // These method will override the method defined by Java 8u251 and later. As we may compile with an earlier
527     // java8 version we don't use @Override annotations here.
528     public String getApplicationProtocol() {
529         return applicationProtocol;
530     }
531 
532     // These method will override the method defined by Java 8u251 and later. As we may compile with an earlier
533     // java8 version we don't use @Override annotations here.
534     public String getHandshakeApplicationProtocol() {
535         return applicationProtocol;
536     }
537 
538     @Override
539     public final synchronized SSLSession getHandshakeSession() {
540         // Javadocs state return value should be:
541         // null if this instance is not currently handshaking, or if the current handshake has not
542         // progressed far enough to create a basic SSLSession. Otherwise, this method returns the
543         // SSLSession currently being negotiated.
544         switch(handshakeState) {
545             case NOT_STARTED:
546             case FINISHED:
547                 return null;
548             default:
549                 return session;
550         }
551     }
552 
553     /**
554      * Returns the pointer to the {@code SSL} object for this {@link ReferenceCountedOpenSslEngine}.
555      * Be aware that it is freed as soon as the {@link #release()} or {@link #shutdown()} methods are called.
556      * At this point {@code 0} will be returned.
557      */
558     public final synchronized long sslPointer() {
559         return ssl;
560     }
561 
562     /**
563      * Destroys this engine.
564      */
565     public final synchronized void shutdown() {
566         if (!destroyed) {
567             destroyed = true;
568             // Let's check if engineMap is null as it could be in theory if we throw an OOME during the construction of
569             // ReferenceCountedOpenSslEngine (before we assign the field). This is needed as shutdown() is called from
570             // the finalizer as well.
571             if (engineMap != null) {
572                 engineMap.remove(ssl);
573             }
574             SSL.freeSSL(ssl);
575             ssl = networkBIO = 0;
576 
577             isInboundDone = outboundClosed = true;
578         }
579 
580         // On shutdown clear all errors
581         SSL.clearError();
582     }
583 
584     /**
585      * Write plaintext data to the OpenSSL internal BIO
586      *
587      * Calling this function with src.remaining == 0 is undefined.
588      */
589     private int writePlaintextData(final ByteBuffer src, int len) {
590         final int pos = src.position();
591         final int limit = src.limit();
592         final int sslWrote;
593 
594         if (src.isDirect()) {
595             sslWrote = SSL.writeToSSL(ssl, bufferAddress(src) + pos, len);
596             if (sslWrote > 0) {
597                 src.position(pos + sslWrote);
598             }
599         } else {
600             ByteBuf buf = alloc.directBuffer(len);
601             try {
602                 src.limit(pos + len);
603 
604                 buf.setBytes(0, src);
605                 src.limit(limit);
606 
607                 sslWrote = SSL.writeToSSL(ssl, memoryAddress(buf), len);
608                 if (sslWrote > 0) {
609                     src.position(pos + sslWrote);
610                 } else {
611                     src.position(pos);
612                 }
613             } finally {
614                 buf.release();
615             }
616         }
617         return sslWrote;
618     }
619 
620    synchronized void bioSetFd(int fd) {
621         if (!isDestroyed()) {
622             SSL.bioSetFd(this.ssl, fd);
623         }
624     }
625 
626     /**
627      * Write encrypted data to the OpenSSL network BIO.
628      */
629     private ByteBuf writeEncryptedData(final ByteBuffer src, int len) throws SSLException {
630         final int pos = src.position();
631         if (src.isDirect()) {
632             SSL.bioSetByteBuffer(networkBIO, bufferAddress(src) + pos, len, false);
633         } else {
634             final ByteBuf buf = alloc.directBuffer(len);
635             try {
636                 final int limit = src.limit();
637                 src.limit(pos + len);
638                 buf.writeBytes(src);
639                 // Restore the original position and limit because we don't want to consume from `src`.
640                 src.position(pos);
641                 src.limit(limit);
642 
643                 SSL.bioSetByteBuffer(networkBIO, memoryAddress(buf), len, false);
644                 return buf;
645             } catch (Throwable cause) {
646                 buf.release();
647                 PlatformDependent.throwException(cause);
648             }
649         }
650         return null;
651     }
652 
653     /**
654      * Read plaintext data from the OpenSSL internal BIO
655      */
656     private int readPlaintextData(final ByteBuffer dst) throws SSLException {
657         final int sslRead;
658         final int pos = dst.position();
659         if (dst.isDirect()) {
660             sslRead = SSL.readFromSSL(ssl, bufferAddress(dst) + pos, dst.limit() - pos);
661             if (sslRead > 0) {
662                 dst.position(pos + sslRead);
663             }
664         } else {
665             final int limit = dst.limit();
666             final int len = min(maxEncryptedPacketLength0(), limit - pos);
667             final ByteBuf buf = alloc.directBuffer(len);
668             try {
669                 sslRead = SSL.readFromSSL(ssl, memoryAddress(buf), len);
670                 if (sslRead > 0) {
671                     dst.limit(pos + sslRead);
672                     buf.getBytes(buf.readerIndex(), dst);
673                     dst.limit(limit);
674                 }
675             } finally {
676                 buf.release();
677             }
678         }
679 
680         return sslRead;
681     }
682 
683     /**
684      * Visible only for testing!
685      */
686     final synchronized int maxWrapOverhead() {
687         return maxWrapOverhead;
688     }
689 
690     /**
691      * Visible only for testing!
692      */
693     final synchronized int maxEncryptedPacketLength() {
694         return maxEncryptedPacketLength0();
695     }
696 
697     /**
698      * This method is intentionally not synchronized, only use if you know you are in the EventLoop
699      * thread and visibility on {@link #maxWrapOverhead} is achieved via other synchronized blocks.
700      */
701     final int maxEncryptedPacketLength0() {
702         return maxWrapOverhead + MAX_PLAINTEXT_LENGTH;
703     }
704 
705     /**
706      * This method is intentionally not synchronized, only use if you know you are in the EventLoop
707      * thread and visibility on {@link #maxWrapBufferSize} and {@link #maxWrapOverhead} is achieved
708      * via other synchronized blocks.
709      * <br>
710      * Calculates the max size of a single wrap operation for the given plaintextLength and
711      * numComponents.
712      */
713     final int calculateMaxLengthForWrap(int plaintextLength, int numComponents) {
714         return (int) min(maxWrapBufferSize, plaintextLength + (long) maxWrapOverhead * numComponents);
715     }
716 
717     /**
718      * This method is intentionally not synchronized, only use if you know you are in the EventLoop
719      * thread and visibility on {@link #maxWrapOverhead} is achieved via other synchronized blocks.
720      * <br>
721      * Calculates the size of the out net buf to create for the given plaintextLength and numComponents.
722      * This is not related to the max size per wrap, as we can wrap chunks at a time into one out net buf.
723      */
724     final int calculateOutNetBufSize(int plaintextLength, int numComponents) {
725         return (int) min(MAX_VALUE, plaintextLength + (long) maxWrapOverhead * numComponents);
726     }
727 
728     final synchronized int sslPending() {
729         return sslPending0();
730     }
731 
732     /**
733      * It is assumed this method is called in a synchronized block (or the constructor)!
734      */
735     private void calculateMaxWrapOverhead() {
736         maxWrapOverhead = SSL.getMaxWrapOverhead(ssl);
737 
738         // maxWrapBufferSize must be set after maxWrapOverhead because there is a dependency on this value.
739         // If jdkCompatibility mode is off we allow enough space to encrypt 16 buffers at a time. This could be
740         // configurable in the future if necessary.
741         maxWrapBufferSize = jdkCompatibilityMode ? maxEncryptedPacketLength0() : maxEncryptedPacketLength0() << 4;
742     }
743 
744     private int sslPending0() {
745         // OpenSSL has a limitation where if you call SSL_pending before the handshake is complete OpenSSL will throw a
746         // "called a function you should not call" error. Using the TLS_method instead of SSLv23_method may solve this
747         // issue but this API is only available in 1.1.0+ [1].
748         // [1] https://www.openssl.org/docs/man1.1.0/ssl/SSL_CTX_new.html
749         return handshakeState != HandshakeState.FINISHED ? 0 : SSL.sslPending(ssl);
750     }
751 
752     private boolean isBytesAvailableEnoughForWrap(int bytesAvailable, int plaintextLength, int numComponents) {
753         return bytesAvailable - (long) maxWrapOverhead * numComponents >= plaintextLength;
754     }
755 
756     @Override
757     public final SSLEngineResult wrap(
758             final ByteBuffer[] srcs, int offset, final int length, final ByteBuffer dst) throws SSLException {
759         // Throw required runtime exceptions
760         checkNotNullWithIAE(srcs, "srcs");
761         checkNotNullWithIAE(dst, "dst");
762 
763         if (offset >= srcs.length || offset + length > srcs.length) {
764             throw new IndexOutOfBoundsException(
765                     "offset: " + offset + ", length: " + length +
766                             " (expected: offset <= offset + length <= srcs.length (" + srcs.length + "))");
767         }
768 
769         if (dst.isReadOnly()) {
770             throw new ReadOnlyBufferException();
771         }
772 
773         synchronized (this) {
774             if (isOutboundDone()) {
775                 // All drained in the outbound buffer
776                 return isInboundDone() || isDestroyed() ? CLOSED_NOT_HANDSHAKING : NEED_UNWRAP_CLOSED;
777             }
778 
779             int bytesProduced = 0;
780             ByteBuf bioReadCopyBuf = null;
781             try {
782                 // Setup the BIO buffer so that we directly write the encryption results into dst.
783                 if (dst.isDirect()) {
784                     SSL.bioSetByteBuffer(networkBIO, bufferAddress(dst) + dst.position(), dst.remaining(),
785                             true);
786                 } else {
787                     bioReadCopyBuf = alloc.directBuffer(dst.remaining());
788                     SSL.bioSetByteBuffer(networkBIO, memoryAddress(bioReadCopyBuf), bioReadCopyBuf.writableBytes(),
789                             true);
790                 }
791 
792                 int bioLengthBefore = SSL.bioLengthByteBuffer(networkBIO);
793 
794                 // Explicitly use outboundClosed as we want to drain any bytes that are still present.
795                 if (outboundClosed) {
796                     // If the outbound was closed we want to ensure we can produce the alert to the destination buffer.
797                     // This is true even if we not using jdkCompatibilityMode.
798                     //
799                     // We use a plaintextLength of 2 as we at least want to have an alert fit into it.
800                     // https://tools.ietf.org/html/rfc5246#section-7.2
801                     if (!isBytesAvailableEnoughForWrap(dst.remaining(), 2, 1)) {
802                         return new SSLEngineResult(BUFFER_OVERFLOW, getHandshakeStatus(), 0, 0);
803                     }
804 
805                     // There is something left to drain.
806                     // See https://github.com/netty/netty/issues/6260
807                     bytesProduced = SSL.bioFlushByteBuffer(networkBIO);
808                     if (bytesProduced <= 0) {
809                         return newResultMayFinishHandshake(NOT_HANDSHAKING, 0, 0);
810                     }
811                     // It is possible when the outbound was closed there was not enough room in the non-application
812                     // buffers to hold the close_notify. We should keep trying to close until we consume all the data
813                     // OpenSSL can give us.
814                     if (!doSSLShutdown()) {
815                         return newResultMayFinishHandshake(NOT_HANDSHAKING, 0, bytesProduced);
816                     }
817                     bytesProduced = bioLengthBefore - SSL.bioLengthByteBuffer(networkBIO);
818                     return newResultMayFinishHandshake(NEED_WRAP, 0, bytesProduced);
819                 }
820 
821                 // Flush any data that may be implicitly generated by OpenSSL (handshake, close, etc..).
822                 SSLEngineResult.HandshakeStatus status = NOT_HANDSHAKING;
823                 HandshakeState oldHandshakeState = handshakeState;
824 
825                 // Prepare OpenSSL to work in server mode and receive handshake
826                 if (handshakeState != HandshakeState.FINISHED) {
827                     if (handshakeState != HandshakeState.STARTED_EXPLICITLY) {
828                         // Update accepted so we know we triggered the handshake via wrap
829                         handshakeState = HandshakeState.STARTED_IMPLICITLY;
830                     }
831 
832                     // Flush any data that may have been written implicitly during the handshake by OpenSSL.
833                     bytesProduced = SSL.bioFlushByteBuffer(networkBIO);
834 
835                     if (pendingException != null) {
836                         // TODO(scott): It is possible that when the handshake failed there was not enough room in the
837                         // non-application buffers to hold the alert. We should get all the data before progressing on.
838                         // However I'm not aware of a way to do this with the OpenSSL APIs.
839                         // See https://github.com/netty/netty/issues/6385.
840 
841                         // We produced / consumed some data during the handshake, signal back to the caller.
842                         // If there is a handshake exception and we have produced data, we should send the data before
843                         // we allow handshake() to throw the handshake exception.
844                         //
845                         // When the user calls wrap() again we will propagate the handshake error back to the user as
846                         // soon as there is no more data to was produced (as part of an alert etc).
847                         if (bytesProduced > 0) {
848                             return newResult(NEED_WRAP, 0, bytesProduced);
849                         }
850                         // Nothing was produced see if there is a handshakeException that needs to be propagated
851                         // to the caller by calling handshakeException() which will return the right HandshakeStatus
852                         // if it can "recover" from the exception for now.
853                         return newResult(handshakeException(), 0, 0);
854                     }
855 
856                     status = handshake();
857 
858                     // Handshake may have generated more data, for example if the internal SSL buffer is small
859                     // we may have freed up space by flushing above.
860                     bytesProduced = bioLengthBefore - SSL.bioLengthByteBuffer(networkBIO);
861 
862                     if (status == NEED_TASK) {
863                         return newResult(status, 0, bytesProduced);
864                     }
865 
866                     if (bytesProduced > 0) {
867                         // If we have filled up the dst buffer and we have not finished the handshake we should try to
868                         // wrap again. Otherwise we should only try to wrap again if there is still data pending in
869                         // SSL buffers.
870                         return newResult(mayFinishHandshake(status != FINISHED ?
871                                          bytesProduced == bioLengthBefore ? NEED_WRAP :
872                                          getHandshakeStatus(SSL.bioLengthNonApplication(networkBIO)) : FINISHED),
873                                          0, bytesProduced);
874                     }
875 
876                     if (status == NEED_UNWRAP) {
877                         // Signal if the outbound is done or not.
878                         return isOutboundDone() ? NEED_UNWRAP_CLOSED : NEED_UNWRAP_OK;
879                     }
880 
881                     // Explicit use outboundClosed and not outboundClosed() as we want to drain any bytes that are
882                     // still present.
883                     if (outboundClosed) {
884                         bytesProduced = SSL.bioFlushByteBuffer(networkBIO);
885                         return newResultMayFinishHandshake(status, 0, bytesProduced);
886                     }
887                 }
888 
889                 final int endOffset = offset + length;
890                 if (jdkCompatibilityMode ||
891                         // If the handshake was not finished before we entered the method, we also ensure we only
892                         // wrap one record. We do this to ensure we not produce any extra data before the caller
893                         // of the method is able to observe handshake completion and react on it.
894                         oldHandshakeState != HandshakeState.FINISHED) {
895                     int srcsLen = 0;
896                     for (int i = offset; i < endOffset; ++i) {
897                         final ByteBuffer src = srcs[i];
898                         if (src == null) {
899                             throw new IllegalArgumentException("srcs[" + i + "] is null");
900                         }
901                         if (srcsLen == MAX_PLAINTEXT_LENGTH) {
902                             continue;
903                         }
904 
905                         srcsLen += src.remaining();
906                         if (srcsLen > MAX_PLAINTEXT_LENGTH || srcsLen < 0) {
907                             // If srcLen > MAX_PLAINTEXT_LENGTH or secLen < 0 just set it to MAX_PLAINTEXT_LENGTH.
908                             // This also help us to guard against overflow.
909                             // We not break out here as we still need to check for null entries in srcs[].
910                             srcsLen = MAX_PLAINTEXT_LENGTH;
911                         }
912                     }
913 
914                     // jdkCompatibilityMode will only produce a single TLS packet, and we don't aggregate src buffers,
915                     // so we always fix the number of buffers to 1 when checking if the dst buffer is large enough.
916                     if (!isBytesAvailableEnoughForWrap(dst.remaining(), srcsLen, 1)) {
917                         return new SSLEngineResult(BUFFER_OVERFLOW, getHandshakeStatus(), 0, 0);
918                     }
919                 }
920 
921                 // There was no pending data in the network BIO -- encrypt any application data
922                 int bytesConsumed = 0;
923                 assert bytesProduced == 0;
924 
925                 // Flush any data that may have been written implicitly by OpenSSL in case a shutdown/alert occurs.
926                 bytesProduced = SSL.bioFlushByteBuffer(networkBIO);
927 
928                 if (bytesProduced > 0) {
929                     return newResultMayFinishHandshake(status, bytesConsumed, bytesProduced);
930                 }
931                 // There was a pending exception that we just delayed because there was something to produce left.
932                 // Throw it now and shutdown the engine.
933                 if (pendingException != null) {
934                     Throwable error = pendingException;
935                     pendingException = null;
936                     shutdown();
937                     // Throw a new exception wrapping the pending exception, so the stacktrace is meaningful and
938                     // contains all the details.
939                     throw new SSLException(error);
940                 }
941 
942                 for (; offset < endOffset; ++offset) {
943                     final ByteBuffer src = srcs[offset];
944                     final int remaining = src.remaining();
945                     if (remaining == 0) {
946                         continue;
947                     }
948 
949                     final int bytesWritten;
950                     if (jdkCompatibilityMode) {
951                         // Write plaintext application data to the SSL engine. We don't have to worry about checking
952                         // if there is enough space if jdkCompatibilityMode because we only wrap at most
953                         // MAX_PLAINTEXT_LENGTH and we loop over the input before hand and check if there is space.
954                         bytesWritten = writePlaintextData(src, min(remaining, MAX_PLAINTEXT_LENGTH - bytesConsumed));
955                     } else {
956                         // OpenSSL's SSL_write keeps state between calls. We should make sure the amount we attempt to
957                         // write is guaranteed to succeed so we don't have to worry about keeping state consistent
958                         // between calls.
959                         final int availableCapacityForWrap = dst.remaining() - bytesProduced - maxWrapOverhead;
960                         if (availableCapacityForWrap <= 0) {
961                             return new SSLEngineResult(BUFFER_OVERFLOW, getHandshakeStatus(), bytesConsumed,
962                                     bytesProduced);
963                         }
964                         bytesWritten = writePlaintextData(src, min(remaining, availableCapacityForWrap));
965                     }
966 
967                     // Determine how much encrypted data was generated.
968                     //
969                     // Even if SSL_write doesn't consume any application data it is possible that OpenSSL will
970                     // produce non-application data into the BIO. For example session tickets....
971                     // See https://github.com/netty/netty/issues/10041
972                     final int pendingNow = SSL.bioLengthByteBuffer(networkBIO);
973                     bytesProduced += bioLengthBefore - pendingNow;
974                     bioLengthBefore = pendingNow;
975 
976                     if (bytesWritten > 0) {
977                         bytesConsumed += bytesWritten;
978 
979                         if (jdkCompatibilityMode || bytesProduced == dst.remaining()) {
980                             return newResultMayFinishHandshake(status, bytesConsumed, bytesProduced);
981                         }
982                     } else {
983                         int sslError = SSL.getError(ssl, bytesWritten);
984                         if (sslError == SSL.SSL_ERROR_ZERO_RETURN) {
985                             // This means the connection was shutdown correctly, close inbound and outbound
986                             if (!receivedShutdown) {
987                                 closeAll();
988 
989                                 bytesProduced += bioLengthBefore - SSL.bioLengthByteBuffer(networkBIO);
990 
991                                 // If we have filled up the dst buffer and we have not finished the handshake we should
992                                 // try to wrap again. Otherwise we should only try to wrap again if there is still data
993                                 // pending in SSL buffers.
994                                 SSLEngineResult.HandshakeStatus hs = mayFinishHandshake(
995                                         status != FINISHED ? bytesProduced == dst.remaining() ? NEED_WRAP
996                                                 : getHandshakeStatus(SSL.bioLengthNonApplication(networkBIO))
997                                                 : FINISHED);
998                                 return newResult(hs, bytesConsumed, bytesProduced);
999                             }
1000 
1001                             return newResult(NOT_HANDSHAKING, bytesConsumed, bytesProduced);
1002                         } else if (sslError == SSL.SSL_ERROR_WANT_READ) {
1003                             // If there is no pending data to read from BIO we should go back to event loop and try
1004                             // to read more data [1]. It is also possible that event loop will detect the socket has
1005                             // been closed. [1] https://www.openssl.org/docs/manmaster/ssl/SSL_write.html
1006                             return newResult(NEED_UNWRAP, bytesConsumed, bytesProduced);
1007                         } else if (sslError == SSL.SSL_ERROR_WANT_WRITE) {
1008                             // SSL_ERROR_WANT_WRITE typically means that the underlying transport is not writable
1009                             // and we should set the "want write" flag on the selector and try again when the
1010                             // underlying transport is writable [1]. However we are not directly writing to the
1011                             // underlying transport and instead writing to a BIO buffer. The OpenSsl documentation
1012                             // says we should do the following [1]:
1013                             //
1014                             // "When using a buffering BIO, like a BIO pair, data must be written into or retrieved
1015                             // out of the BIO before being able to continue."
1016                             //
1017                             // In practice this means the destination buffer doesn't have enough space for OpenSSL
1018                             // to write encrypted data to. This is an OVERFLOW condition.
1019                             // [1] https://www.openssl.org/docs/manmaster/ssl/SSL_write.html
1020                             if (bytesProduced > 0) {
1021                                 // If we produced something we should report this back and let the user call
1022                                 // wrap again.
1023                                 return newResult(NEED_WRAP, bytesConsumed, bytesProduced);
1024                             }
1025                             return newResult(BUFFER_OVERFLOW, status, bytesConsumed, bytesProduced);
1026                         } else if (sslError == SSL.SSL_ERROR_WANT_X509_LOOKUP ||
1027                                 sslError == SSL.SSL_ERROR_WANT_CERTIFICATE_VERIFY ||
1028                                 sslError == SSL.SSL_ERROR_WANT_PRIVATE_KEY_OPERATION) {
1029 
1030                             return newResult(NEED_TASK, bytesConsumed, bytesProduced);
1031                         } else {
1032                             // Everything else is considered as error
1033                             throw shutdownWithError("SSL_write", sslError, SSL.getLastErrorNumber());
1034                         }
1035                     }
1036                 }
1037                 return newResultMayFinishHandshake(status, bytesConsumed, bytesProduced);
1038             } finally {
1039                 SSL.bioClearByteBuffer(networkBIO);
1040                 if (bioReadCopyBuf == null) {
1041                     dst.position(dst.position() + bytesProduced);
1042                 } else {
1043                     assert bioReadCopyBuf.readableBytes() <= dst.remaining() : "The destination buffer " + dst +
1044                             " didn't have enough remaining space to hold the encrypted content in " + bioReadCopyBuf;
1045                     dst.put(bioReadCopyBuf.internalNioBuffer(bioReadCopyBuf.readerIndex(), bytesProduced));
1046                     bioReadCopyBuf.release();
1047                 }
1048             }
1049         }
1050     }
1051 
1052     private SSLEngineResult newResult(SSLEngineResult.HandshakeStatus hs, int bytesConsumed, int bytesProduced) {
1053         return newResult(OK, hs, bytesConsumed, bytesProduced);
1054     }
1055 
1056     private SSLEngineResult newResult(SSLEngineResult.Status status, SSLEngineResult.HandshakeStatus hs,
1057                                       int bytesConsumed, int bytesProduced) {
1058         // If isOutboundDone, then the data from the network BIO
1059         // was the close_notify message and all was consumed we are not required to wait
1060         // for the receipt the peer's close_notify message -- shutdown.
1061         if (isOutboundDone()) {
1062             if (isInboundDone()) {
1063                 // If the inbound was done as well, we need to ensure we return NOT_HANDSHAKING to signal we are done.
1064                 hs = NOT_HANDSHAKING;
1065 
1066                 // As the inbound and the outbound is done we can shutdown the engine now.
1067                 shutdown();
1068             }
1069             return new SSLEngineResult(CLOSED, hs, bytesConsumed, bytesProduced);
1070         }
1071         if (hs == NEED_TASK) {
1072             // Set needTask to true so getHandshakeStatus() will return the correct value.
1073             needTask = true;
1074         }
1075         return new SSLEngineResult(status, hs, bytesConsumed, bytesProduced);
1076     }
1077 
1078     private SSLEngineResult newResultMayFinishHandshake(SSLEngineResult.HandshakeStatus hs,
1079                                                         int bytesConsumed, int bytesProduced) throws SSLException {
1080         return newResult(mayFinishHandshake(hs, bytesConsumed, bytesProduced), bytesConsumed, bytesProduced);
1081     }
1082 
1083     private SSLEngineResult newResultMayFinishHandshake(SSLEngineResult.Status status,
1084                                                         SSLEngineResult.HandshakeStatus hs,
1085                                                         int bytesConsumed, int bytesProduced) throws SSLException {
1086         return newResult(status, mayFinishHandshake(hs, bytesConsumed, bytesProduced), bytesConsumed, bytesProduced);
1087     }
1088 
1089     /**
1090      * Log the error, shutdown the engine and throw an exception.
1091      */
1092     private SSLException shutdownWithError(String operation, int sslError, int error) {
1093         if (logger.isDebugEnabled()) {
1094             String errorString = SSL.getErrorString(error);
1095             logger.debug("{} failed with {}: OpenSSL error: {} {}",
1096                          operation, sslError, error, errorString);
1097         }
1098 
1099         // There was an internal error -- shutdown
1100         shutdown();
1101 
1102         SSLException exception = newSSLExceptionForError(error);
1103         // If we have a pendingException stored already we should include it as well to help the user debug things.
1104         if (pendingException != null) {
1105             exception.initCause(pendingException);
1106             pendingException = null;
1107         }
1108         return exception;
1109     }
1110 
1111     private SSLEngineResult handleUnwrapException(int bytesConsumed, int bytesProduced, SSLException e)
1112             throws SSLException {
1113         int lastError = SSL.getLastErrorNumber();
1114         if (lastError != 0) {
1115             return sslReadErrorResult(SSL.SSL_ERROR_SSL, lastError, bytesConsumed,
1116                     bytesProduced);
1117         }
1118         throw e;
1119     }
1120 
1121     public final SSLEngineResult unwrap(
1122             final ByteBuffer[] srcs, int srcsOffset, final int srcsLength,
1123             final ByteBuffer[] dsts, int dstsOffset, final int dstsLength) throws SSLException {
1124 
1125         // Throw required runtime exceptions
1126         checkNotNullWithIAE(srcs, "srcs");
1127         if (srcsOffset >= srcs.length
1128                 || srcsOffset + srcsLength > srcs.length) {
1129             throw new IndexOutOfBoundsException(
1130                     "offset: " + srcsOffset + ", length: " + srcsLength +
1131                             " (expected: offset <= offset + length <= srcs.length (" + srcs.length + "))");
1132         }
1133         checkNotNullWithIAE(dsts, "dsts");
1134         if (dstsOffset >= dsts.length || dstsOffset + dstsLength > dsts.length) {
1135             throw new IndexOutOfBoundsException(
1136                     "offset: " + dstsOffset + ", length: " + dstsLength +
1137                             " (expected: offset <= offset + length <= dsts.length (" + dsts.length + "))");
1138         }
1139         long capacity = 0;
1140         final int dstsEndOffset = dstsOffset + dstsLength;
1141         for (int i = dstsOffset; i < dstsEndOffset; i ++) {
1142             ByteBuffer dst = checkNotNullArrayParam(dsts[i], i, "dsts");
1143             if (dst.isReadOnly()) {
1144                 throw new ReadOnlyBufferException();
1145             }
1146             capacity += dst.remaining();
1147         }
1148 
1149         final int srcsEndOffset = srcsOffset + srcsLength;
1150         long len = 0;
1151         for (int i = srcsOffset; i < srcsEndOffset; i++) {
1152             ByteBuffer src = checkNotNullArrayParam(srcs[i], i, "srcs");
1153             len += src.remaining();
1154         }
1155 
1156         synchronized (this) {
1157             if (isInboundDone()) {
1158                 return isOutboundDone() || isDestroyed() ? CLOSED_NOT_HANDSHAKING : NEED_WRAP_CLOSED;
1159             }
1160 
1161             SSLEngineResult.HandshakeStatus status = NOT_HANDSHAKING;
1162             HandshakeState oldHandshakeState = handshakeState;
1163             // Prepare OpenSSL to work in server mode and receive handshake
1164             if (handshakeState != HandshakeState.FINISHED) {
1165                 if (handshakeState != HandshakeState.STARTED_EXPLICITLY) {
1166                     // Update accepted so we know we triggered the handshake via wrap
1167                     handshakeState = HandshakeState.STARTED_IMPLICITLY;
1168                 }
1169 
1170                 status = handshake();
1171 
1172                 if (status == NEED_TASK) {
1173                     return newResult(status, 0, 0);
1174                 }
1175 
1176                 if (status == NEED_WRAP) {
1177                     return NEED_WRAP_OK;
1178                 }
1179                 // Check if the inbound is considered to be closed if so let us try to wrap again.
1180                 if (isInboundDone) {
1181                     return NEED_WRAP_CLOSED;
1182                 }
1183             }
1184 
1185             int sslPending = sslPending0();
1186             int packetLength;
1187             // The JDK implies that only a single SSL packet should be processed per unwrap call [1]. If we are in
1188             // JDK compatibility mode then we should honor this, but if not we just wrap as much as possible. If there
1189             // are multiple records or partial records this may reduce thrashing events through the pipeline.
1190             // [1] https://docs.oracle.com/javase/7/docs/api/javax/net/ssl/SSLEngine.html
1191             if (jdkCompatibilityMode ||
1192                     // If the handshake was not finished before we entered the method, we also ensure we only
1193                     // unwrap one record. We do this to ensure we not produce any extra data before the caller
1194                     // of the method is able to observe handshake completion and react on it.
1195                     oldHandshakeState != HandshakeState.FINISHED) {
1196                 if (len < SSL_RECORD_HEADER_LENGTH) {
1197                     return newResultMayFinishHandshake(BUFFER_UNDERFLOW, status, 0, 0);
1198                 }
1199 
1200                 packetLength = SslUtils.getEncryptedPacketLength(srcs, srcsOffset);
1201                 if (packetLength == SslUtils.NOT_ENCRYPTED) {
1202                     throw new NotSslRecordException("not an SSL/TLS record");
1203                 }
1204 
1205                 final int packetLengthDataOnly = packetLength - SSL_RECORD_HEADER_LENGTH;
1206                 if (packetLengthDataOnly > capacity) {
1207                     // Not enough space in the destination buffer so signal the caller that the buffer needs to be
1208                     // increased.
1209                     if (packetLengthDataOnly > MAX_RECORD_SIZE) {
1210                         // The packet length MUST NOT exceed 2^14 [1]. However we do accommodate more data to support
1211                         // legacy use cases which may violate this condition (e.g. OpenJDK's SslEngineImpl). If the max
1212                         // length is exceeded we fail fast here to avoid an infinite loop due to the fact that we
1213                         // won't allocate a buffer large enough.
1214                         // [1] https://tools.ietf.org/html/rfc5246#section-6.2.1
1215                         throw new SSLException("Illegal packet length: " + packetLengthDataOnly + " > " +
1216                                                 session.getApplicationBufferSize());
1217                     } else {
1218                         session.tryExpandApplicationBufferSize(packetLengthDataOnly);
1219                     }
1220                     return newResultMayFinishHandshake(BUFFER_OVERFLOW, status, 0, 0);
1221                 }
1222 
1223                 if (len < packetLength) {
1224                     // We either don't have enough data to read the packet length or not enough for reading the whole
1225                     // packet.
1226                     return newResultMayFinishHandshake(BUFFER_UNDERFLOW, status, 0, 0);
1227                 }
1228             } else if (len == 0 && sslPending <= 0) {
1229                 return newResultMayFinishHandshake(BUFFER_UNDERFLOW, status, 0, 0);
1230             } else if (capacity == 0) {
1231                 return newResultMayFinishHandshake(BUFFER_OVERFLOW, status, 0, 0);
1232             } else {
1233                 packetLength = (int) min(MAX_VALUE, len);
1234             }
1235 
1236             // This must always be the case when we reached here as if not we returned BUFFER_UNDERFLOW.
1237             assert srcsOffset < srcsEndOffset;
1238 
1239             // This must always be the case if we reached here.
1240             assert capacity > 0;
1241 
1242             // Number of produced bytes
1243             int bytesProduced = 0;
1244             int bytesConsumed = 0;
1245             try {
1246                 srcLoop:
1247                 for (;;) {
1248                     ByteBuffer src = srcs[srcsOffset];
1249                     int remaining = src.remaining();
1250                     final ByteBuf bioWriteCopyBuf;
1251                     int pendingEncryptedBytes;
1252                     if (remaining == 0) {
1253                         if (sslPending <= 0) {
1254                             // We must skip empty buffers as BIO_write will return 0 if asked to write something
1255                             // with length 0.
1256                             if (++srcsOffset >= srcsEndOffset) {
1257                                 break;
1258                             }
1259                             continue;
1260                         } else {
1261                             bioWriteCopyBuf = null;
1262                             pendingEncryptedBytes = SSL.bioLengthByteBuffer(networkBIO);
1263                         }
1264                     } else {
1265                         // Write more encrypted data into the BIO. Ensure we only read one packet at a time as
1266                         // stated in the SSLEngine javadocs.
1267                         pendingEncryptedBytes = min(packetLength, remaining);
1268                         try {
1269                             bioWriteCopyBuf = writeEncryptedData(src, pendingEncryptedBytes);
1270                         } catch (SSLException e) {
1271                             // Ensure we correctly handle the error stack.
1272                             return handleUnwrapException(bytesConsumed, bytesProduced, e);
1273                         }
1274                     }
1275                     try {
1276                         for (;;) {
1277                             ByteBuffer dst = dsts[dstsOffset];
1278                             if (!dst.hasRemaining()) {
1279                                 // No space left in the destination buffer, skip it.
1280                                 if (++dstsOffset >= dstsEndOffset) {
1281                                     break srcLoop;
1282                                 }
1283                                 continue;
1284                             }
1285 
1286                             int bytesRead;
1287                             try {
1288                                 bytesRead = readPlaintextData(dst);
1289                             } catch (SSLException e) {
1290                                 // Ensure we correctly handle the error stack.
1291                                 return handleUnwrapException(bytesConsumed, bytesProduced, e);
1292                             }
1293                             // We are directly using the ByteBuffer memory for the write, and so we only know what has
1294                             // been consumed after we let SSL decrypt the data. At this point we should update the
1295                             // number of bytes consumed, update the ByteBuffer position, and release temp ByteBuf.
1296                             int localBytesConsumed = pendingEncryptedBytes - SSL.bioLengthByteBuffer(networkBIO);
1297                             bytesConsumed += localBytesConsumed;
1298                             packetLength -= localBytesConsumed;
1299                             pendingEncryptedBytes -= localBytesConsumed;
1300                             src.position(src.position() + localBytesConsumed);
1301 
1302                             if (bytesRead > 0) {
1303                                 bytesProduced += bytesRead;
1304 
1305                                 if (!dst.hasRemaining()) {
1306                                     sslPending = sslPending0();
1307                                     // Move to the next dst buffer as this one is full.
1308                                     if (++dstsOffset >= dstsEndOffset) {
1309                                         return sslPending > 0 ?
1310                                                 newResult(BUFFER_OVERFLOW, status, bytesConsumed, bytesProduced) :
1311                                                 newResultMayFinishHandshake(isInboundDone() ? CLOSED : OK, status,
1312                                                         bytesConsumed, bytesProduced);
1313                                     }
1314                                 } else if (packetLength == 0 || jdkCompatibilityMode) {
1315                                     // We either consumed all data or we are in jdkCompatibilityMode and have consumed
1316                                     // a single TLS packet and should stop consuming until this method is called again.
1317                                     break srcLoop;
1318                                 }
1319                             } else {
1320                                 int sslError = SSL.getError(ssl, bytesRead);
1321                                 if (sslError == SSL.SSL_ERROR_WANT_READ || sslError == SSL.SSL_ERROR_WANT_WRITE) {
1322                                     // break to the outer loop as we want to read more data which means we need to
1323                                     // write more to the BIO.
1324                                     break;
1325                                 } else if (sslError == SSL.SSL_ERROR_ZERO_RETURN) {
1326                                     // This means the connection was shutdown correctly, close inbound and outbound
1327                                     if (!receivedShutdown) {
1328                                         closeAll();
1329                                     }
1330                                     return newResultMayFinishHandshake(isInboundDone() ? CLOSED : OK, status,
1331                                             bytesConsumed, bytesProduced);
1332                                 } else if (sslError == SSL.SSL_ERROR_WANT_X509_LOOKUP ||
1333                                         sslError == SSL.SSL_ERROR_WANT_CERTIFICATE_VERIFY ||
1334                                         sslError == SSL.SSL_ERROR_WANT_PRIVATE_KEY_OPERATION) {
1335                                     return newResult(isInboundDone() ? CLOSED : OK,
1336                                             NEED_TASK, bytesConsumed, bytesProduced);
1337                                 } else {
1338                                     return sslReadErrorResult(sslError, SSL.getLastErrorNumber(), bytesConsumed,
1339                                             bytesProduced);
1340                                 }
1341                             }
1342                         }
1343 
1344                         if (++srcsOffset >= srcsEndOffset) {
1345                             break;
1346                         }
1347                     } finally {
1348                         if (bioWriteCopyBuf != null) {
1349                             bioWriteCopyBuf.release();
1350                         }
1351                     }
1352                 }
1353             } finally {
1354                 SSL.bioClearByteBuffer(networkBIO);
1355                 rejectRemoteInitiatedRenegotiation();
1356             }
1357 
1358             // Check to see if we received a close_notify message from the peer.
1359             if (!receivedShutdown && (SSL.getShutdown(ssl) & SSL.SSL_RECEIVED_SHUTDOWN) == SSL.SSL_RECEIVED_SHUTDOWN) {
1360                 closeAll();
1361             }
1362 
1363             return newResultMayFinishHandshake(isInboundDone() ? CLOSED : OK, status, bytesConsumed, bytesProduced);
1364         }
1365     }
1366 
1367     private boolean needWrapAgain(int stackError) {
1368         // Check if we have a pending handshakeException and if so see if we need to consume all pending data from the
1369         // BIO first or can just shutdown and throw it now.
1370         // This is needed so we ensure close_notify etc is correctly send to the remote peer.
1371         // See https://github.com/netty/netty/issues/3900
1372         if (SSL.bioLengthNonApplication(networkBIO) > 0) {
1373             // we seem to have data left that needs to be transferred and so the user needs
1374             // call wrap(...). Store the error so we can pick it up later.
1375             if (pendingException == null) {
1376                 pendingException = newSSLExceptionForError(stackError);
1377             } else if (shouldAddSuppressed(pendingException, stackError)) {
1378                 ThrowableUtil.addSuppressed(pendingException, newSSLExceptionForError(stackError));
1379             }
1380             // We need to clear all errors so we not pick up anything that was left on the stack on the next
1381             // operation. Note that shutdownWithError(...) will cleanup the stack as well so its only needed here.
1382             SSL.clearError();
1383             return true;
1384         }
1385         return false;
1386     }
1387 
1388     private SSLException newSSLExceptionForError(int stackError) {
1389         String message = SSL.getErrorString(stackError);
1390         return handshakeState == HandshakeState.FINISHED ?
1391                 new OpenSslException(message, stackError) : new OpenSslHandshakeException(message, stackError);
1392     }
1393 
1394     private static boolean shouldAddSuppressed(Throwable target, int errorCode) {
1395         for (Throwable suppressed: ThrowableUtil.getSuppressed(target)) {
1396             if (suppressed instanceof NativeSslException &&
1397                     ((NativeSslException) suppressed).errorCode() == errorCode) {
1398                 /// An exception with this errorCode was already added before.
1399                 return false;
1400             }
1401         }
1402         return true;
1403     }
1404 
1405     private SSLEngineResult sslReadErrorResult(int error, int stackError, int bytesConsumed, int bytesProduced)
1406             throws SSLException {
1407         if (needWrapAgain(stackError)) {
1408             // There is something that needs to be send to the remote peer before we can teardown.
1409             // This is most likely some alert.
1410             return new SSLEngineResult(OK, NEED_WRAP, bytesConsumed, bytesProduced);
1411         }
1412         throw shutdownWithError("SSL_read", error, stackError);
1413     }
1414 
1415     private void closeAll() throws SSLException {
1416         receivedShutdown = true;
1417         closeOutbound();
1418         closeInbound();
1419     }
1420 
1421     private void rejectRemoteInitiatedRenegotiation() throws SSLHandshakeException {
1422         // As rejectRemoteInitiatedRenegotiation() is called in a finally block we also need to check if we shutdown
1423         // the engine before as otherwise SSL.getHandshakeCount(ssl) will throw an NPE if the passed in ssl is 0.
1424         // See https://github.com/netty/netty/issues/7353
1425         if (!isDestroyed() && (!clientMode && SSL.getHandshakeCount(ssl) > 1 ||
1426                 // Let's allow to renegotiate once for client auth.
1427                 clientMode && SSL.getHandshakeCount(ssl) > 2) &&
1428             // As we may count multiple handshakes when TLSv1.3 is used we should just ignore this here as
1429             // renegotiation is not supported in TLSv1.3 as per spec.
1430             !SslProtocols.TLS_v1_3.equals(session.getProtocol()) && handshakeState == HandshakeState.FINISHED) {
1431             // TODO: In future versions me may also want to send a fatal_alert to the client and so notify it
1432             // that the renegotiation failed.
1433             shutdown();
1434             throw new SSLHandshakeException("remote-initiated renegotiation not allowed");
1435         }
1436     }
1437 
1438     public final SSLEngineResult unwrap(final ByteBuffer[] srcs, final ByteBuffer[] dsts) throws SSLException {
1439         return unwrap(srcs, 0, srcs.length, dsts, 0, dsts.length);
1440     }
1441 
1442     private ByteBuffer[] singleSrcBuffer(ByteBuffer src) {
1443         singleSrcBuffer[0] = src;
1444         return singleSrcBuffer;
1445     }
1446 
1447     private void resetSingleSrcBuffer() {
1448         singleSrcBuffer[0] = null;
1449     }
1450 
1451     private ByteBuffer[] singleDstBuffer(ByteBuffer src) {
1452         singleDstBuffer[0] = src;
1453         return singleDstBuffer;
1454     }
1455 
1456     private void resetSingleDstBuffer() {
1457         singleDstBuffer[0] = null;
1458     }
1459 
1460     @Override
1461     public final synchronized SSLEngineResult unwrap(
1462             final ByteBuffer src, final ByteBuffer[] dsts, final int offset, final int length) throws SSLException {
1463         try {
1464             return unwrap(singleSrcBuffer(src), 0, 1, dsts, offset, length);
1465         } finally {
1466             resetSingleSrcBuffer();
1467         }
1468     }
1469 
1470     @Override
1471     public final synchronized SSLEngineResult wrap(ByteBuffer src, ByteBuffer dst) throws SSLException {
1472         try {
1473             return wrap(singleSrcBuffer(src), dst);
1474         } finally {
1475             resetSingleSrcBuffer();
1476         }
1477     }
1478 
1479     @Override
1480     public final synchronized SSLEngineResult unwrap(ByteBuffer src, ByteBuffer dst) throws SSLException {
1481         try {
1482             return unwrap(singleSrcBuffer(src), singleDstBuffer(dst));
1483         } finally {
1484             resetSingleSrcBuffer();
1485             resetSingleDstBuffer();
1486         }
1487     }
1488 
1489     @Override
1490     public final synchronized SSLEngineResult unwrap(ByteBuffer src, ByteBuffer[] dsts) throws SSLException {
1491         try {
1492             return unwrap(singleSrcBuffer(src), dsts);
1493         } finally {
1494             resetSingleSrcBuffer();
1495         }
1496     }
1497 
1498     private class TaskDecorator<R extends Runnable> implements Runnable {
1499         protected final R task;
1500         TaskDecorator(R task) {
1501             this.task = task;
1502         }
1503 
1504         @Override
1505         public void run() {
1506             runAndResetNeedTask(task);
1507         }
1508     }
1509 
1510     private final class AsyncTaskDecorator extends TaskDecorator<AsyncTask> implements AsyncRunnable {
1511         AsyncTaskDecorator(AsyncTask task) {
1512             super(task);
1513         }
1514 
1515         @Override
1516         public void run(final Runnable runnable) {
1517             if (isDestroyed()) {
1518                 // The engine was destroyed in the meantime, just return.
1519                 return;
1520             }
1521             task.runAsync(new TaskDecorator<Runnable>(runnable));
1522         }
1523     }
1524 
1525     private void runAndResetNeedTask(Runnable task) {
1526         // We need to synchronize on the ReferenceCountedOpenSslEngine, we are sure the SSL object
1527         // will not be freed by the user calling for example shutdown() concurrently.
1528         synchronized (ReferenceCountedOpenSslEngine.this) {
1529             try {
1530                 if (isDestroyed()) {
1531                     // The engine was destroyed in the meantime, just return.
1532                     return;
1533                 }
1534                 task.run();
1535                 if (handshakeState != HandshakeState.FINISHED && !isDestroyed()) {
1536                     // Call SSL.doHandshake(...) If the handshake was not finished yet. This might be needed
1537                     // to fill the application buffer and so have getHandshakeStatus() return the right value
1538                     // in this case.
1539                     if (SSL.doHandshake(ssl) <= 0) {
1540                         SSL.clearError();
1541                     }
1542                 }
1543             } finally {
1544                 // The task was run, reset needTask to false so getHandshakeStatus() returns the correct value.
1545                 needTask = false;
1546             }
1547         }
1548     }
1549 
1550     @Override
1551     public final synchronized Runnable getDelegatedTask() {
1552         if (isDestroyed()) {
1553             return null;
1554         }
1555         final Runnable task = SSL.getTask(ssl);
1556         if (task == null) {
1557             return null;
1558         }
1559         if (task instanceof AsyncTask) {
1560             return new AsyncTaskDecorator((AsyncTask) task);
1561         }
1562         return new TaskDecorator<Runnable>(task);
1563     }
1564 
1565     @Override
1566     public final synchronized void closeInbound() throws SSLException {
1567         if (isInboundDone) {
1568             return;
1569         }
1570 
1571         isInboundDone = true;
1572 
1573         if (isOutboundDone()) {
1574             // Only call shutdown if there is no outbound data pending.
1575             // See https://github.com/netty/netty/issues/6167
1576             shutdown();
1577         }
1578 
1579         if (handshakeState != HandshakeState.NOT_STARTED && !receivedShutdown) {
1580             throw new SSLException(
1581                     "Inbound closed before receiving peer's close_notify: possible truncation attack?");
1582         }
1583     }
1584 
1585     @Override
1586     public final synchronized boolean isInboundDone() {
1587         return isInboundDone;
1588     }
1589 
1590     @Override
1591     public final synchronized void closeOutbound() {
1592         if (outboundClosed) {
1593             return;
1594         }
1595 
1596         outboundClosed = true;
1597 
1598         if (handshakeState != HandshakeState.NOT_STARTED && !isDestroyed()) {
1599             int mode = SSL.getShutdown(ssl);
1600             if ((mode & SSL.SSL_SENT_SHUTDOWN) != SSL.SSL_SENT_SHUTDOWN) {
1601                 doSSLShutdown();
1602             }
1603         } else {
1604             // engine closing before initial handshake
1605             shutdown();
1606         }
1607     }
1608 
1609     /**
1610      * Attempt to call {@link SSL#shutdownSSL(long)}.
1611      * @return {@code false} if the call to {@link SSL#shutdownSSL(long)} was not attempted or returned an error.
1612      */
1613     private boolean doSSLShutdown() {
1614         if (SSL.isInInit(ssl) != 0) {
1615             // Only try to call SSL_shutdown if we are not in the init state anymore.
1616             // Otherwise we will see 'error:140E0197:SSL routines:SSL_shutdown:shutdown while in init' in our logs.
1617             //
1618             // See also https://hg.nginx.org/nginx/rev/062c189fee20
1619             return false;
1620         }
1621         int err = SSL.shutdownSSL(ssl);
1622         if (err < 0) {
1623             int sslErr = SSL.getError(ssl, err);
1624             if (sslErr == SSL.SSL_ERROR_SYSCALL || sslErr == SSL.SSL_ERROR_SSL) {
1625                 if (logger.isDebugEnabled()) {
1626                     int error = SSL.getLastErrorNumber();
1627                     logger.debug("SSL_shutdown failed: OpenSSL error: {} {}", error, SSL.getErrorString(error));
1628                 }
1629                 // There was an internal error -- shutdown
1630                 shutdown();
1631                 return false;
1632             }
1633             SSL.clearError();
1634         }
1635         return true;
1636     }
1637 
1638     @Override
1639     public final synchronized boolean isOutboundDone() {
1640         // Check if there is anything left in the outbound buffer.
1641         // We need to ensure we only call SSL.pendingWrittenBytesInBIO(...) if the engine was not destroyed yet.
1642         return outboundClosed && (networkBIO == 0 || SSL.bioLengthNonApplication(networkBIO) == 0);
1643     }
1644 
1645     @Override
1646     public final String[] getSupportedCipherSuites() {
1647         return OpenSsl.AVAILABLE_CIPHER_SUITES.toArray(EMPTY_STRINGS);
1648     }
1649 
1650     @Override
1651     public final String[] getEnabledCipherSuites() {
1652         final String[] extraCiphers;
1653         final String[] enabled;
1654         final boolean tls13Enabled;
1655         synchronized (this) {
1656             if (!isDestroyed()) {
1657                 enabled = SSL.getCiphers(ssl);
1658                 int opts = SSL.getOptions(ssl);
1659                 if (isProtocolEnabled(opts, SSL.SSL_OP_NO_TLSv1_3, SslProtocols.TLS_v1_3)) {
1660                     extraCiphers = OpenSsl.EXTRA_SUPPORTED_TLS_1_3_CIPHERS;
1661                     tls13Enabled = true;
1662                 } else {
1663                     extraCiphers = EMPTY_STRINGS;
1664                     tls13Enabled = false;
1665                 }
1666             } else {
1667                 return EMPTY_STRINGS;
1668             }
1669         }
1670         if (enabled == null) {
1671             return EMPTY_STRINGS;
1672         } else {
1673             Set<String> enabledSet = new LinkedHashSet<String>(enabled.length + extraCiphers.length);
1674             synchronized (this) {
1675                 for (int i = 0; i < enabled.length; i++) {
1676                     String mapped = toJavaCipherSuite(enabled[i]);
1677                     final String cipher = mapped == null ? enabled[i] : mapped;
1678                     if ((!tls13Enabled || !OpenSsl.isTlsv13Supported()) && SslUtils.isTLSv13Cipher(cipher)) {
1679                         continue;
1680                     }
1681                     enabledSet.add(cipher);
1682                 }
1683                 Collections.addAll(enabledSet, extraCiphers);
1684             }
1685             return enabledSet.toArray(EMPTY_STRINGS);
1686         }
1687     }
1688 
1689     @Override
1690     public final void setEnabledCipherSuites(String[] cipherSuites) {
1691         checkNotNull(cipherSuites, "cipherSuites");
1692 
1693         final StringBuilder buf = new StringBuilder();
1694         final StringBuilder bufTLSv13 = new StringBuilder();
1695 
1696         CipherSuiteConverter.convertToCipherStrings(Arrays.asList(cipherSuites), buf, bufTLSv13, OpenSsl.isBoringSSL());
1697         final String cipherSuiteSpec = buf.toString();
1698         final String cipherSuiteSpecTLSv13 = bufTLSv13.toString();
1699 
1700         if (!OpenSsl.isTlsv13Supported() && !cipherSuiteSpecTLSv13.isEmpty()) {
1701             throw new IllegalArgumentException("TLSv1.3 is not supported by this java version.");
1702         }
1703         synchronized (this) {
1704             hasTLSv13Cipher = !cipherSuiteSpecTLSv13.isEmpty();
1705             if (!isDestroyed()) {
1706                 try {
1707                     // Set non TLSv1.3 ciphers.
1708                     SSL.setCipherSuites(ssl, cipherSuiteSpec, false);
1709                     if (OpenSsl.isTlsv13Supported()) {
1710                         // Set TLSv1.3 ciphers.
1711                         SSL.setCipherSuites(ssl, OpenSsl.checkTls13Ciphers(logger, cipherSuiteSpecTLSv13), true);
1712                     }
1713 
1714                     // We also need to update the enabled protocols to ensure we disable the protocol if there are
1715                     // no compatible ciphers left.
1716                     Set<String> protocols = new HashSet<String>(enabledProtocols);
1717 
1718                     // We have no ciphers that are compatible with none-TLSv1.3, let us explicit disable all other
1719                     // protocols.
1720                     if (cipherSuiteSpec.isEmpty()) {
1721                         protocols.remove(SslProtocols.TLS_v1);
1722                         protocols.remove(SslProtocols.TLS_v1_1);
1723                         protocols.remove(SslProtocols.TLS_v1_2);
1724                         protocols.remove(SslProtocols.SSL_v3);
1725                         protocols.remove(SslProtocols.SSL_v2);
1726                         protocols.remove(SslProtocols.SSL_v2_HELLO);
1727                     }
1728                     // We have no ciphers that are compatible with TLSv1.3, let us explicit disable it.
1729                     if (cipherSuiteSpecTLSv13.isEmpty()) {
1730                         protocols.remove(SslProtocols.TLS_v1_3);
1731                     }
1732                     // Update the protocols but not cache the value. We only cache when we call it from the user
1733                     // code or when we construct the engine.
1734                     setEnabledProtocols0(protocols.toArray(EMPTY_STRINGS), !hasTLSv13Cipher);
1735                 } catch (Exception e) {
1736                     throw new IllegalStateException("failed to enable cipher suites: " + cipherSuiteSpec, e);
1737                 }
1738             } else {
1739                 throw new IllegalStateException("failed to enable cipher suites: " + cipherSuiteSpec);
1740             }
1741         }
1742     }
1743 
1744     @Override
1745     public final String[] getSupportedProtocols() {
1746         return OpenSsl.SUPPORTED_PROTOCOLS_SET.toArray(EMPTY_STRINGS);
1747     }
1748 
1749     @Override
1750     public final String[] getEnabledProtocols() {
1751         return enabledProtocols.toArray(EMPTY_STRINGS);
1752     }
1753 
1754     private static boolean isProtocolEnabled(int opts, int disableMask, String protocolString) {
1755         // We also need to check if the actual protocolString is supported as depending on the openssl API
1756         // implementations it may use a disableMask of 0 (BoringSSL is doing this for example).
1757         return (opts & disableMask) == 0 && OpenSsl.SUPPORTED_PROTOCOLS_SET.contains(protocolString);
1758     }
1759 
1760     /**
1761      * {@inheritDoc}
1762      * TLS doesn't support a way to advertise non-contiguous versions from the client's perspective, and the client
1763      * just advertises the max supported version. The TLS protocol also doesn't support all different combinations of
1764      * discrete protocols, and instead assumes contiguous ranges. OpenSSL has some unexpected behavior
1765      * (e.g. handshake failures) if non-contiguous protocols are used even where there is a compatible set of protocols
1766      * and ciphers. For these reasons this method will determine the minimum protocol and the maximum protocol and
1767      * enabled a contiguous range from [min protocol, max protocol] in OpenSSL.
1768      */
1769     @Override
1770     public final void setEnabledProtocols(String[] protocols) {
1771         checkNotNullWithIAE(protocols, "protocols");
1772         synchronized (this) {
1773             enabledProtocols.clear();
1774             // Seems like there is no way to explicit disable SSLv2Hello in openssl, so it is always enabled
1775             enabledProtocols.add(SslProtocols.SSL_v2_HELLO);
1776 
1777             Collections.addAll(enabledProtocols, protocols);
1778 
1779             setEnabledProtocols0(protocols, !hasTLSv13Cipher);
1780         }
1781     }
1782 
1783     private void setEnabledProtocols0(String[] protocols, boolean explicitDisableTLSv13) {
1784         assert Thread.holdsLock(this);
1785         // This is correct from the API docs
1786         int minProtocolIndex = OPENSSL_OP_NO_PROTOCOLS.length;
1787         int maxProtocolIndex = 0;
1788         for (String p: protocols) {
1789             if (!OpenSsl.SUPPORTED_PROTOCOLS_SET.contains(p)) {
1790                 throw new IllegalArgumentException("Protocol " + p + " is not supported.");
1791             }
1792             if (p.equals(SslProtocols.SSL_v2)) {
1793                 if (minProtocolIndex > OPENSSL_OP_NO_PROTOCOL_INDEX_SSLV2) {
1794                     minProtocolIndex = OPENSSL_OP_NO_PROTOCOL_INDEX_SSLV2;
1795                 }
1796                 if (maxProtocolIndex < OPENSSL_OP_NO_PROTOCOL_INDEX_SSLV2) {
1797                     maxProtocolIndex = OPENSSL_OP_NO_PROTOCOL_INDEX_SSLV2;
1798                 }
1799             } else if (p.equals(SslProtocols.SSL_v3)) {
1800                 if (minProtocolIndex > OPENSSL_OP_NO_PROTOCOL_INDEX_SSLV3) {
1801                     minProtocolIndex = OPENSSL_OP_NO_PROTOCOL_INDEX_SSLV3;
1802                 }
1803                 if (maxProtocolIndex < OPENSSL_OP_NO_PROTOCOL_INDEX_SSLV3) {
1804                     maxProtocolIndex = OPENSSL_OP_NO_PROTOCOL_INDEX_SSLV3;
1805                 }
1806             } else if (p.equals(SslProtocols.TLS_v1)) {
1807                 if (minProtocolIndex > OPENSSL_OP_NO_PROTOCOL_INDEX_TLSv1) {
1808                     minProtocolIndex = OPENSSL_OP_NO_PROTOCOL_INDEX_TLSv1;
1809                 }
1810                 if (maxProtocolIndex < OPENSSL_OP_NO_PROTOCOL_INDEX_TLSv1) {
1811                     maxProtocolIndex = OPENSSL_OP_NO_PROTOCOL_INDEX_TLSv1;
1812                 }
1813             } else if (p.equals(SslProtocols.TLS_v1_1)) {
1814                 if (minProtocolIndex > OPENSSL_OP_NO_PROTOCOL_INDEX_TLSv1_1) {
1815                     minProtocolIndex = OPENSSL_OP_NO_PROTOCOL_INDEX_TLSv1_1;
1816                 }
1817                 if (maxProtocolIndex < OPENSSL_OP_NO_PROTOCOL_INDEX_TLSv1_1) {
1818                     maxProtocolIndex = OPENSSL_OP_NO_PROTOCOL_INDEX_TLSv1_1;
1819                 }
1820             } else if (p.equals(SslProtocols.TLS_v1_2)) {
1821                 if (minProtocolIndex > OPENSSL_OP_NO_PROTOCOL_INDEX_TLSv1_2) {
1822                     minProtocolIndex = OPENSSL_OP_NO_PROTOCOL_INDEX_TLSv1_2;
1823                 }
1824                 if (maxProtocolIndex < OPENSSL_OP_NO_PROTOCOL_INDEX_TLSv1_2) {
1825                     maxProtocolIndex = OPENSSL_OP_NO_PROTOCOL_INDEX_TLSv1_2;
1826                 }
1827             } else if (!explicitDisableTLSv13 && p.equals(SslProtocols.TLS_v1_3)) {
1828                 if (minProtocolIndex > OPENSSL_OP_NO_PROTOCOL_INDEX_TLSv1_3) {
1829                     minProtocolIndex = OPENSSL_OP_NO_PROTOCOL_INDEX_TLSv1_3;
1830                 }
1831                 if (maxProtocolIndex < OPENSSL_OP_NO_PROTOCOL_INDEX_TLSv1_3) {
1832                     maxProtocolIndex = OPENSSL_OP_NO_PROTOCOL_INDEX_TLSv1_3;
1833                 }
1834             }
1835         }
1836         if (!isDestroyed()) {
1837             // Clear out options which disable protocols
1838             SSL.clearOptions(ssl, SSL.SSL_OP_NO_SSLv2 | SSL.SSL_OP_NO_SSLv3 | SSL.SSL_OP_NO_TLSv1 |
1839                     SSL.SSL_OP_NO_TLSv1_1 | SSL.SSL_OP_NO_TLSv1_2 | SSL.SSL_OP_NO_TLSv1_3);
1840 
1841             int opts = 0;
1842             for (int i = 0; i < minProtocolIndex; ++i) {
1843                 opts |= OPENSSL_OP_NO_PROTOCOLS[i];
1844             }
1845             assert maxProtocolIndex != MAX_VALUE;
1846             for (int i = maxProtocolIndex + 1; i < OPENSSL_OP_NO_PROTOCOLS.length; ++i) {
1847                 opts |= OPENSSL_OP_NO_PROTOCOLS[i];
1848             }
1849 
1850             // Disable protocols we do not want
1851             SSL.setOptions(ssl, opts);
1852         } else {
1853             throw new IllegalStateException("failed to enable protocols: " + Arrays.asList(protocols));
1854         }
1855     }
1856 
1857     @Override
1858     public final SSLSession getSession() {
1859         return session;
1860     }
1861 
1862     @Override
1863     public final synchronized void beginHandshake() throws SSLException {
1864         switch (handshakeState) {
1865             case STARTED_IMPLICITLY:
1866                 checkEngineClosed();
1867 
1868                 // A user did not start handshake by calling this method by him/herself,
1869                 // but handshake has been started already by wrap() or unwrap() implicitly.
1870                 // Because it's the user's first time to call this method, it is unfair to
1871                 // raise an exception.  From the user's standpoint, he or she never asked
1872                 // for renegotiation.
1873 
1874                 handshakeState = HandshakeState.STARTED_EXPLICITLY; // Next time this method is invoked by the user,
1875                 calculateMaxWrapOverhead();
1876                 // we should raise an exception.
1877                 break;
1878             case STARTED_EXPLICITLY:
1879                 // Nothing to do as the handshake is not done yet.
1880                 break;
1881             case FINISHED:
1882                 throw new SSLException("renegotiation unsupported");
1883             case NOT_STARTED:
1884                 handshakeState = HandshakeState.STARTED_EXPLICITLY;
1885                 if (handshake() == NEED_TASK) {
1886                     // Set needTask to true so getHandshakeStatus() will return the correct value.
1887                     needTask = true;
1888                 }
1889                 calculateMaxWrapOverhead();
1890                 break;
1891             default:
1892                 throw new Error();
1893         }
1894     }
1895 
1896     private void checkEngineClosed() throws SSLException {
1897         if (isDestroyed()) {
1898             throw new SSLException("engine closed");
1899         }
1900     }
1901 
1902     private static SSLEngineResult.HandshakeStatus pendingStatus(int pendingStatus) {
1903         // Depending on if there is something left in the BIO we need to WRAP or UNWRAP
1904         return pendingStatus > 0 ? NEED_WRAP : NEED_UNWRAP;
1905     }
1906 
1907     private static boolean isEmpty(Object[] arr) {
1908         return arr == null || arr.length == 0;
1909     }
1910 
1911     private static boolean isEmpty(byte[] cert) {
1912         return cert == null || cert.length == 0;
1913     }
1914 
1915     private SSLEngineResult.HandshakeStatus handshakeException() throws SSLException {
1916         if (SSL.bioLengthNonApplication(networkBIO) > 0) {
1917             // There is something pending, we need to consume it first via a WRAP so we don't loose anything.
1918             return NEED_WRAP;
1919         }
1920 
1921         Throwable exception = pendingException;
1922         assert exception != null;
1923         pendingException = null;
1924         shutdown();
1925         if (exception instanceof SSLHandshakeException) {
1926             throw (SSLHandshakeException) exception;
1927         }
1928         SSLHandshakeException e = new SSLHandshakeException("General OpenSslEngine problem");
1929         e.initCause(exception);
1930         throw e;
1931     }
1932 
1933     /**
1934      * Should be called if the handshake will be failed due a callback that throws an exception.
1935      * This cause will then be used to give more details as part of the {@link SSLHandshakeException}.
1936      */
1937     final void initHandshakeException(Throwable cause) {
1938         if (pendingException == null) {
1939             pendingException = cause;
1940         } else {
1941             ThrowableUtil.addSuppressed(pendingException, cause);
1942         }
1943     }
1944 
1945     private SSLEngineResult.HandshakeStatus handshake() throws SSLException {
1946         if (needTask) {
1947             return NEED_TASK;
1948         }
1949         if (handshakeState == HandshakeState.FINISHED) {
1950             return FINISHED;
1951         }
1952 
1953         checkEngineClosed();
1954 
1955         if (pendingException != null) {
1956             // Let's call SSL.doHandshake(...) again in case there is some async operation pending that would fill the
1957             // outbound buffer.
1958             if (SSL.doHandshake(ssl) <= 0) {
1959                 // Clear any error that was put on the stack by the handshake
1960                 SSL.clearError();
1961             }
1962             return handshakeException();
1963         }
1964 
1965         // Adding the OpenSslEngine to the OpenSslEngineMap so it can be used in the AbstractCertificateVerifier.
1966         engineMap.add(this);
1967 
1968         if (!sessionSet) {
1969             if (!parentContext.sessionContext().setSessionFromCache(ssl, session, getPeerHost(), getPeerPort())) {
1970                 // The session was not reused via the cache. Call prepareHandshake() to ensure we remove all previous
1971                 // stored key-value pairs.
1972                 session.prepareHandshake();
1973             }
1974             sessionSet = true;
1975         }
1976 
1977         int code = SSL.doHandshake(ssl);
1978         if (code <= 0) {
1979             int sslError = SSL.getError(ssl, code);
1980             if (sslError == SSL.SSL_ERROR_WANT_READ || sslError == SSL.SSL_ERROR_WANT_WRITE) {
1981                 return pendingStatus(SSL.bioLengthNonApplication(networkBIO));
1982             }
1983 
1984             if (sslError == SSL.SSL_ERROR_WANT_X509_LOOKUP ||
1985                     sslError == SSL.SSL_ERROR_WANT_CERTIFICATE_VERIFY ||
1986                     sslError == SSL.SSL_ERROR_WANT_PRIVATE_KEY_OPERATION) {
1987                 return NEED_TASK;
1988             }
1989 
1990             int errorNumber = SSL.getLastErrorNumber();
1991             if (needWrapAgain(errorNumber)) {
1992                 // There is something that needs to be send to the remote peer before we can teardown.
1993                 // This is most likely some alert.
1994                 return NEED_WRAP;
1995             }
1996             // Check if we have a pending exception that was created during the handshake and if so throw it after
1997             // shutdown the connection.
1998             if (pendingException != null) {
1999                 return handshakeException();
2000             }
2001 
2002             // Everything else is considered as error
2003             throw shutdownWithError("SSL_do_handshake", sslError, errorNumber);
2004         }
2005         // We have produced more data as part of the handshake if this is the case the user should call wrap(...)
2006         if (SSL.bioLengthNonApplication(networkBIO) > 0) {
2007             return NEED_WRAP;
2008         }
2009         // if SSL_do_handshake returns > 0 or sslError == SSL.SSL_ERROR_NAME it means the handshake was finished.
2010         session.handshakeFinished(SSL.getSessionId(ssl), SSL.getCipherForSSL(ssl), SSL.getVersion(ssl),
2011                 SSL.getPeerCertificate(ssl), SSL.getPeerCertChain(ssl),
2012                 SSL.getTime(ssl) * 1000L, parentContext.sessionTimeout() * 1000L);
2013         selectApplicationProtocol();
2014         return FINISHED;
2015     }
2016 
2017     private SSLEngineResult.HandshakeStatus mayFinishHandshake(
2018             SSLEngineResult.HandshakeStatus hs, int bytesConsumed, int bytesProduced) throws SSLException {
2019         return hs == NEED_UNWRAP && bytesProduced > 0 || hs == NEED_WRAP && bytesConsumed > 0 ?
2020             handshake() : mayFinishHandshake(hs != FINISHED ? getHandshakeStatus() : FINISHED);
2021     }
2022 
2023     private SSLEngineResult.HandshakeStatus mayFinishHandshake(SSLEngineResult.HandshakeStatus status)
2024             throws SSLException {
2025         if (status == NOT_HANDSHAKING) {
2026             if (handshakeState != HandshakeState.FINISHED) {
2027                 // If the status was NOT_HANDSHAKING and we not finished the handshake we need to call
2028                 // SSL_do_handshake() again
2029                 return handshake();
2030             }
2031             if (!isDestroyed() && SSL.bioLengthNonApplication(networkBIO) > 0) {
2032                 // We have something left that needs to be wrapped.
2033                 return NEED_WRAP;
2034             }
2035         }
2036         return status;
2037     }
2038 
2039     @Override
2040     public final synchronized SSLEngineResult.HandshakeStatus getHandshakeStatus() {
2041         // Check if we are in the initial handshake phase or shutdown phase
2042         if (needPendingStatus()) {
2043             if (needTask) {
2044                 // There is a task outstanding
2045                 return NEED_TASK;
2046             }
2047             return pendingStatus(SSL.bioLengthNonApplication(networkBIO));
2048         }
2049         return NOT_HANDSHAKING;
2050     }
2051 
2052     private SSLEngineResult.HandshakeStatus getHandshakeStatus(int pending) {
2053         // Check if we are in the initial handshake phase or shutdown phase
2054         if (needPendingStatus()) {
2055             if (needTask) {
2056                 // There is a task outstanding
2057                 return NEED_TASK;
2058             }
2059             return pendingStatus(pending);
2060         }
2061         return NOT_HANDSHAKING;
2062     }
2063 
2064     private boolean needPendingStatus() {
2065         return handshakeState != HandshakeState.NOT_STARTED && !isDestroyed()
2066                 && (handshakeState != HandshakeState.FINISHED || isInboundDone() || isOutboundDone());
2067     }
2068 
2069     /**
2070      * Converts the specified OpenSSL cipher suite to the Java cipher suite.
2071      */
2072     private String toJavaCipherSuite(String openSslCipherSuite) {
2073         if (openSslCipherSuite == null) {
2074             return null;
2075         }
2076 
2077         String version = SSL.getVersion(ssl);
2078         String prefix = toJavaCipherSuitePrefix(version);
2079         return CipherSuiteConverter.toJava(openSslCipherSuite, prefix);
2080     }
2081 
2082     /**
2083      * Converts the protocol version string returned by {@link SSL#getVersion(long)} to protocol family string.
2084      */
2085     private static String toJavaCipherSuitePrefix(String protocolVersion) {
2086         final char c;
2087         if (protocolVersion == null || protocolVersion.isEmpty()) {
2088             c = 0;
2089         } else {
2090             c = protocolVersion.charAt(0);
2091         }
2092 
2093         switch (c) {
2094             case 'T':
2095                 return "TLS";
2096             case 'S':
2097                 return "SSL";
2098             default:
2099                 return "UNKNOWN";
2100         }
2101     }
2102 
2103     @Override
2104     public final void setUseClientMode(boolean clientMode) {
2105         if (clientMode != this.clientMode) {
2106             throw new UnsupportedOperationException();
2107         }
2108     }
2109 
2110     @Override
2111     public final boolean getUseClientMode() {
2112         return clientMode;
2113     }
2114 
2115     @Override
2116     public final void setNeedClientAuth(boolean b) {
2117         setClientAuth(b ? ClientAuth.REQUIRE : ClientAuth.NONE);
2118     }
2119 
2120     @Override
2121     public final boolean getNeedClientAuth() {
2122         return clientAuth == ClientAuth.REQUIRE;
2123     }
2124 
2125     @Override
2126     public final void setWantClientAuth(boolean b) {
2127         setClientAuth(b ? ClientAuth.OPTIONAL : ClientAuth.NONE);
2128     }
2129 
2130     @Override
2131     public final boolean getWantClientAuth() {
2132         return clientAuth == ClientAuth.OPTIONAL;
2133     }
2134 
2135     /**
2136      * See <a href="https://www.openssl.org/docs/man1.0.2/ssl/SSL_set_verify.html">SSL_set_verify</a> and
2137      * {@link SSL#setVerify(long, int, int)}.
2138      */
2139     @UnstableApi
2140     public final synchronized void setVerify(int verifyMode, int depth) {
2141         if (!isDestroyed()) {
2142             SSL.setVerify(ssl, verifyMode, depth);
2143         }
2144     }
2145 
2146     private void setClientAuth(ClientAuth mode) {
2147         if (clientMode) {
2148             return;
2149         }
2150         synchronized (this) {
2151             if (clientAuth == mode) {
2152                 // No need to issue any JNI calls if the mode is the same
2153                 return;
2154             }
2155             if (!isDestroyed()) {
2156                 switch (mode) {
2157                     case NONE:
2158                         SSL.setVerify(ssl, SSL.SSL_CVERIFY_NONE, ReferenceCountedOpenSslContext.VERIFY_DEPTH);
2159                         break;
2160                     case REQUIRE:
2161                         SSL.setVerify(ssl, SSL.SSL_CVERIFY_REQUIRED, ReferenceCountedOpenSslContext.VERIFY_DEPTH);
2162                         break;
2163                     case OPTIONAL:
2164                         SSL.setVerify(ssl, SSL.SSL_CVERIFY_OPTIONAL, ReferenceCountedOpenSslContext.VERIFY_DEPTH);
2165                         break;
2166                     default:
2167                         throw new Error(mode.toString());
2168                 }
2169             }
2170             clientAuth = mode;
2171         }
2172     }
2173 
2174     @Override
2175     public final void setEnableSessionCreation(boolean b) {
2176         if (b) {
2177             throw new UnsupportedOperationException();
2178         }
2179     }
2180 
2181     @Override
2182     public final boolean getEnableSessionCreation() {
2183         return false;
2184     }
2185 
2186     @SuppressJava6Requirement(reason = "Usage guarded by java version check")
2187     @Override
2188     public final synchronized SSLParameters getSSLParameters() {
2189         SSLParameters sslParameters = super.getSSLParameters();
2190 
2191         int version = PlatformDependent.javaVersion();
2192         if (version >= 7) {
2193             Java7SslParametersUtils.setEndpointIdentificationAlgorithm(sslParameters, endpointIdentificationAlgorithm);
2194             Java7SslParametersUtils.setAlgorithmConstraints(sslParameters, algorithmConstraints);
2195             if (version >= 8) {
2196                 if (sniHostNames != null) {
2197                     Java8SslUtils.setSniHostNames(sslParameters, sniHostNames);
2198                 }
2199                 if (!isDestroyed()) {
2200                     Java8SslUtils.setUseCipherSuitesOrder(
2201                             sslParameters, (SSL.getOptions(ssl) & SSL.SSL_OP_CIPHER_SERVER_PREFERENCE) != 0);
2202                 }
2203 
2204                 Java8SslUtils.setSNIMatchers(sslParameters, matchers);
2205             }
2206         }
2207         return sslParameters;
2208     }
2209 
2210     @SuppressJava6Requirement(reason = "Usage guarded by java version check")
2211     @Override
2212     public final synchronized void setSSLParameters(SSLParameters sslParameters) {
2213         int version = PlatformDependent.javaVersion();
2214         if (version >= 7) {
2215             if (sslParameters.getAlgorithmConstraints() != null) {
2216                 throw new IllegalArgumentException("AlgorithmConstraints are not supported.");
2217             }
2218 
2219             boolean isDestroyed = isDestroyed();
2220             if (version >= 8) {
2221                 if (!isDestroyed) {
2222                     if (clientMode) {
2223                         final List<String> sniHostNames = Java8SslUtils.getSniHostNames(sslParameters);
2224                         for (String name: sniHostNames) {
2225                             SSL.setTlsExtHostName(ssl, name);
2226                         }
2227                         this.sniHostNames = sniHostNames;
2228                     }
2229                     if (Java8SslUtils.getUseCipherSuitesOrder(sslParameters)) {
2230                         SSL.setOptions(ssl, SSL.SSL_OP_CIPHER_SERVER_PREFERENCE);
2231                     } else {
2232                         SSL.clearOptions(ssl, SSL.SSL_OP_CIPHER_SERVER_PREFERENCE);
2233                     }
2234                 }
2235                 matchers = sslParameters.getSNIMatchers();
2236             }
2237 
2238             final String endpointIdentificationAlgorithm = sslParameters.getEndpointIdentificationAlgorithm();
2239             if (!isDestroyed) {
2240                 configureEndpointVerification(endpointIdentificationAlgorithm);
2241             }
2242             this.endpointIdentificationAlgorithm = endpointIdentificationAlgorithm;
2243             algorithmConstraints = sslParameters.getAlgorithmConstraints();
2244         }
2245         super.setSSLParameters(sslParameters);
2246     }
2247 
2248     private void configureEndpointVerification(String endpointIdentificationAlgorithm) {
2249         // If the user asks for hostname verification we must ensure we verify the peer.
2250         // If the user disables hostname verification we leave it up to the user to change the mode manually.
2251         if (clientMode && isEndPointVerificationEnabled(endpointIdentificationAlgorithm)) {
2252             SSL.setVerify(ssl, SSL.SSL_CVERIFY_REQUIRED, -1);
2253         }
2254     }
2255 
2256     private static boolean isEndPointVerificationEnabled(String endPointIdentificationAlgorithm) {
2257         return endPointIdentificationAlgorithm != null && !endPointIdentificationAlgorithm.isEmpty();
2258     }
2259 
2260     private boolean isDestroyed() {
2261         return destroyed;
2262     }
2263 
2264     final boolean checkSniHostnameMatch(byte[] hostname) {
2265         return Java8SslUtils.checkSniHostnameMatch(matchers, hostname);
2266     }
2267 
2268     @Override
2269     public String getNegotiatedApplicationProtocol() {
2270         return applicationProtocol;
2271     }
2272 
2273     private static long bufferAddress(ByteBuffer b) {
2274         assert b.isDirect();
2275         if (PlatformDependent.hasUnsafe()) {
2276             return PlatformDependent.directBufferAddress(b);
2277         }
2278         return Buffer.address(b);
2279     }
2280 
2281     /**
2282      * Select the application protocol used.
2283      */
2284     private void selectApplicationProtocol() throws SSLException {
2285         ApplicationProtocolConfig.SelectedListenerFailureBehavior behavior = apn.selectedListenerFailureBehavior();
2286         List<String> protocols = apn.protocols();
2287         String applicationProtocol;
2288         switch (apn.protocol()) {
2289             case NONE:
2290                 break;
2291             // We always need to check for applicationProtocol == null as the remote peer may not support
2292             // the TLS extension or may have returned an empty selection.
2293             case ALPN:
2294                 applicationProtocol = SSL.getAlpnSelected(ssl);
2295                 if (applicationProtocol != null) {
2296                     ReferenceCountedOpenSslEngine.this.applicationProtocol = selectApplicationProtocol(
2297                             protocols, behavior, applicationProtocol);
2298                 }
2299                 break;
2300             case NPN:
2301                 applicationProtocol = SSL.getNextProtoNegotiated(ssl);
2302                 if (applicationProtocol != null) {
2303                     ReferenceCountedOpenSslEngine.this.applicationProtocol = selectApplicationProtocol(
2304                             protocols, behavior, applicationProtocol);
2305                 }
2306                 break;
2307             case NPN_AND_ALPN:
2308                 applicationProtocol = SSL.getAlpnSelected(ssl);
2309                 if (applicationProtocol == null) {
2310                     applicationProtocol = SSL.getNextProtoNegotiated(ssl);
2311                 }
2312                 if (applicationProtocol != null) {
2313                     ReferenceCountedOpenSslEngine.this.applicationProtocol = selectApplicationProtocol(
2314                             protocols, behavior, applicationProtocol);
2315                 }
2316                 break;
2317             default:
2318                 throw new Error();
2319         }
2320     }
2321 
2322     private String selectApplicationProtocol(List<String> protocols,
2323                                              ApplicationProtocolConfig.SelectedListenerFailureBehavior behavior,
2324                                              String applicationProtocol) throws SSLException {
2325         if (behavior == ApplicationProtocolConfig.SelectedListenerFailureBehavior.ACCEPT) {
2326             return applicationProtocol;
2327         } else {
2328             int size = protocols.size();
2329             assert size > 0;
2330             if (protocols.contains(applicationProtocol)) {
2331                 return applicationProtocol;
2332             } else {
2333                 if (behavior == ApplicationProtocolConfig.SelectedListenerFailureBehavior.CHOOSE_MY_LAST_PROTOCOL) {
2334                     return protocols.get(size - 1);
2335                 } else {
2336                     throw new SSLException("unknown protocol " + applicationProtocol);
2337                 }
2338             }
2339         }
2340     }
2341 
2342     private static final X509Certificate[] JAVAX_CERTS_NOT_SUPPORTED = new X509Certificate[0];
2343 
2344     private final class DefaultOpenSslSession implements OpenSslSession  {
2345         private final OpenSslSessionContext sessionContext;
2346 
2347         // These are guarded by synchronized(OpenSslEngine.this) as handshakeFinished() may be triggered by any
2348         // thread.
2349         private X509Certificate[] x509PeerCerts;
2350         private Certificate[] peerCerts;
2351 
2352         private boolean valid = true;
2353         private String protocol;
2354         private String cipher;
2355         private OpenSslSessionId id = OpenSslSessionId.NULL_ID;
2356         private long creationTime;
2357 
2358         // Updated once a new handshake is started and so the SSLSession reused.
2359         private long lastAccessed = -1;
2360 
2361         private volatile int applicationBufferSize = MAX_PLAINTEXT_LENGTH;
2362         private volatile Certificate[] localCertificateChain;
2363         private volatile Map<String, Object> keyValueStorage = new ConcurrentHashMap<String, Object>();
2364 
2365         DefaultOpenSslSession(OpenSslSessionContext sessionContext) {
2366             this.sessionContext = sessionContext;
2367         }
2368 
2369         private SSLSessionBindingEvent newSSLSessionBindingEvent(String name) {
2370             return new SSLSessionBindingEvent(session, name);
2371         }
2372 
2373         @Override
2374         public void prepareHandshake() {
2375             keyValueStorage.clear();
2376         }
2377 
2378         @Override
2379         public void setSessionDetails(
2380                 long creationTime, long lastAccessedTime, OpenSslSessionId sessionId,
2381                 Map<String, Object> keyValueStorage) {
2382             synchronized (ReferenceCountedOpenSslEngine.this) {
2383                 if (this.id == OpenSslSessionId.NULL_ID) {
2384                     this.id = sessionId;
2385                     this.creationTime = creationTime;
2386                     this.lastAccessed = lastAccessedTime;
2387 
2388                     // Update the key value storage. It's fine to just drop the previous stored values on the floor
2389                     // as the JDK does the same in the sense that it will use a new SSLSessionImpl instance once the
2390                     // handshake was done
2391                     this.keyValueStorage = keyValueStorage;
2392                 }
2393             }
2394         }
2395 
2396         @Override
2397         public Map<String, Object> keyValueStorage() {
2398             return keyValueStorage;
2399         }
2400 
2401         @Override
2402         public OpenSslSessionId sessionId() {
2403             synchronized (ReferenceCountedOpenSslEngine.this) {
2404                 if (this.id == OpenSslSessionId.NULL_ID && !isDestroyed()) {
2405                     byte[] sessionId = SSL.getSessionId(ssl);
2406                     if (sessionId != null) {
2407                         id = new OpenSslSessionId(sessionId);
2408                     }
2409                 }
2410 
2411                 return id;
2412             }
2413         }
2414 
2415         @Override
2416         public void setLocalCertificate(Certificate[] localCertificate) {
2417             this.localCertificateChain = localCertificate;
2418         }
2419 
2420         @Override
2421         public byte[] getId() {
2422             return sessionId().cloneBytes();
2423         }
2424 
2425         @Override
2426         public OpenSslSessionContext getSessionContext() {
2427             return sessionContext;
2428         }
2429 
2430         @Override
2431         public long getCreationTime() {
2432             synchronized (ReferenceCountedOpenSslEngine.this) {
2433                 return creationTime;
2434             }
2435         }
2436 
2437         @Override
2438         public void setLastAccessedTime(long time) {
2439             synchronized (ReferenceCountedOpenSslEngine.this) {
2440                 this.lastAccessed = time;
2441             }
2442         }
2443 
2444         @Override
2445         public long getLastAccessedTime() {
2446             // if lastAccessed is -1 we will just return the creation time as the handshake was not started yet.
2447             synchronized (ReferenceCountedOpenSslEngine.this) {
2448                 return lastAccessed == -1 ? creationTime : lastAccessed;
2449             }
2450         }
2451 
2452         @Override
2453         public void invalidate() {
2454             synchronized (ReferenceCountedOpenSslEngine.this) {
2455                 valid = false;
2456                 sessionContext.removeFromCache(id);
2457             }
2458         }
2459 
2460         @Override
2461         public boolean isValid() {
2462             synchronized (ReferenceCountedOpenSslEngine.this) {
2463                 return valid || sessionContext.isInCache(id);
2464             }
2465         }
2466 
2467         @Override
2468         public void putValue(String name, Object value) {
2469             checkNotNull(name, "name");
2470             checkNotNull(value, "value");
2471 
2472             final Object old = keyValueStorage.put(name, value);
2473             if (value instanceof SSLSessionBindingListener) {
2474                 // Use newSSLSessionBindingEvent so we always use the wrapper if needed.
2475                 ((SSLSessionBindingListener) value).valueBound(newSSLSessionBindingEvent(name));
2476             }
2477             notifyUnbound(old, name);
2478         }
2479 
2480         @Override
2481         public Object getValue(String name) {
2482             checkNotNull(name, "name");
2483             return keyValueStorage.get(name);
2484         }
2485 
2486         @Override
2487         public void removeValue(String name) {
2488             checkNotNull(name, "name");
2489             final Object old = keyValueStorage.remove(name);
2490             notifyUnbound(old, name);
2491         }
2492 
2493         @Override
2494         public String[] getValueNames() {
2495             return keyValueStorage.keySet().toArray(EMPTY_STRINGS);
2496         }
2497 
2498         private void notifyUnbound(Object value, String name) {
2499             if (value instanceof SSLSessionBindingListener) {
2500                 // Use newSSLSessionBindingEvent so we always use the wrapper if needed.
2501                 ((SSLSessionBindingListener) value).valueUnbound(newSSLSessionBindingEvent(name));
2502             }
2503         }
2504 
2505         /**
2506          * Finish the handshake and so init everything in the {@link OpenSslSession} that should be accessible by
2507          * the user.
2508          */
2509         @Override
2510         public void handshakeFinished(byte[] id, String cipher, String protocol, byte[] peerCertificate,
2511                                       byte[][] peerCertificateChain, long creationTime, long timeout)
2512                 throws SSLException {
2513             synchronized (ReferenceCountedOpenSslEngine.this) {
2514                 if (!isDestroyed()) {
2515                     if (this.id == OpenSslSessionId.NULL_ID) {
2516                         // if the handshake finished and it was not a resumption let ensure we try to set the id
2517 
2518                         this.id = id == null ? OpenSslSessionId.NULL_ID : new OpenSslSessionId(id);
2519                         // Once the handshake was done the lastAccessed and creationTime should be the same if we
2520                         // did not set it earlier via setSessionDetails(...)
2521                         this.creationTime = lastAccessed = creationTime;
2522                     }
2523                     this.cipher = toJavaCipherSuite(cipher);
2524                     this.protocol = protocol;
2525 
2526                     if (clientMode) {
2527                         if (isEmpty(peerCertificateChain)) {
2528                             peerCerts = EmptyArrays.EMPTY_CERTIFICATES;
2529                             if (OpenSsl.JAVAX_CERTIFICATE_CREATION_SUPPORTED) {
2530                                 x509PeerCerts = EmptyArrays.EMPTY_JAVAX_X509_CERTIFICATES;
2531                             } else {
2532                                 x509PeerCerts = JAVAX_CERTS_NOT_SUPPORTED;
2533                             }
2534                         } else {
2535                             peerCerts = new Certificate[peerCertificateChain.length];
2536                             if (OpenSsl.JAVAX_CERTIFICATE_CREATION_SUPPORTED) {
2537                                 x509PeerCerts = new X509Certificate[peerCertificateChain.length];
2538                             } else {
2539                                 x509PeerCerts = JAVAX_CERTS_NOT_SUPPORTED;
2540                             }
2541                             initCerts(peerCertificateChain, 0);
2542                         }
2543                     } else {
2544                         // if used on the server side SSL_get_peer_cert_chain(...) will not include the remote peer
2545                         // certificate. We use SSL_get_peer_certificate to get it in this case and add it to our
2546                         // array later.
2547                         //
2548                         // See https://www.openssl.org/docs/ssl/SSL_get_peer_cert_chain.html
2549                         if (isEmpty(peerCertificate)) {
2550                             peerCerts = EmptyArrays.EMPTY_CERTIFICATES;
2551                             x509PeerCerts = EmptyArrays.EMPTY_JAVAX_X509_CERTIFICATES;
2552                         } else {
2553                             if (isEmpty(peerCertificateChain)) {
2554                                 peerCerts = new Certificate[] {new LazyX509Certificate(peerCertificate)};
2555                                 if (OpenSsl.JAVAX_CERTIFICATE_CREATION_SUPPORTED) {
2556                                     x509PeerCerts = new X509Certificate[] {
2557                                             new LazyJavaxX509Certificate(peerCertificate)
2558                                     };
2559                                 } else {
2560                                     x509PeerCerts = JAVAX_CERTS_NOT_SUPPORTED;
2561                                 }
2562                             } else {
2563                                 peerCerts = new Certificate[peerCertificateChain.length + 1];
2564                                 peerCerts[0] = new LazyX509Certificate(peerCertificate);
2565 
2566                                 if (OpenSsl.JAVAX_CERTIFICATE_CREATION_SUPPORTED) {
2567                                     x509PeerCerts = new X509Certificate[peerCertificateChain.length + 1];
2568                                     x509PeerCerts[0] = new LazyJavaxX509Certificate(peerCertificate);
2569                                 } else {
2570                                     x509PeerCerts = JAVAX_CERTS_NOT_SUPPORTED;
2571                                 }
2572 
2573                                 initCerts(peerCertificateChain, 1);
2574                             }
2575                         }
2576                     }
2577 
2578                     calculateMaxWrapOverhead();
2579 
2580                     handshakeState = HandshakeState.FINISHED;
2581                 } else {
2582                     throw new SSLException("Already closed");
2583                 }
2584             }
2585         }
2586 
2587         private void initCerts(byte[][] chain, int startPos) {
2588             for (int i = 0; i < chain.length; i++) {
2589                 int certPos = startPos + i;
2590                 peerCerts[certPos] = new LazyX509Certificate(chain[i]);
2591                 if (x509PeerCerts != JAVAX_CERTS_NOT_SUPPORTED) {
2592                     x509PeerCerts[certPos] = new LazyJavaxX509Certificate(chain[i]);
2593                 }
2594             }
2595         }
2596 
2597         @Override
2598         public Certificate[] getPeerCertificates() throws SSLPeerUnverifiedException {
2599             synchronized (ReferenceCountedOpenSslEngine.this) {
2600                 if (isEmpty(peerCerts)) {
2601                     throw new SSLPeerUnverifiedException("peer not verified");
2602                 }
2603                 return peerCerts.clone();
2604             }
2605         }
2606 
2607         @Override
2608         public Certificate[] getLocalCertificates() {
2609             Certificate[] localCerts = this.localCertificateChain;
2610             if (localCerts == null) {
2611                 return null;
2612             }
2613             return localCerts.clone();
2614         }
2615 
2616         @Override
2617         public X509Certificate[] getPeerCertificateChain() throws SSLPeerUnverifiedException {
2618             synchronized (ReferenceCountedOpenSslEngine.this) {
2619                 if (x509PeerCerts == JAVAX_CERTS_NOT_SUPPORTED) {
2620                     // Not supported by the underlying JDK, so just throw. This is fine in terms of the API
2621                     // contract. See SSLSession.html#getPeerCertificateChain().
2622                     throw new UnsupportedOperationException();
2623                 }
2624                 if (isEmpty(x509PeerCerts)) {
2625                     throw new SSLPeerUnverifiedException("peer not verified");
2626                 }
2627                 return x509PeerCerts.clone();
2628             }
2629         }
2630 
2631         @Override
2632         public Principal getPeerPrincipal() throws SSLPeerUnverifiedException {
2633             Certificate[] peer = getPeerCertificates();
2634             // No need for null or length > 0 is needed as this is done in getPeerCertificates()
2635             // already.
2636             return ((java.security.cert.X509Certificate) peer[0]).getSubjectX500Principal();
2637         }
2638 
2639         @Override
2640         public Principal getLocalPrincipal() {
2641             Certificate[] local = this.localCertificateChain;
2642             if (local == null || local.length == 0) {
2643                 return null;
2644             }
2645             return ((java.security.cert.X509Certificate) local[0]).getSubjectX500Principal();
2646         }
2647 
2648         @Override
2649         public String getCipherSuite() {
2650             synchronized (ReferenceCountedOpenSslEngine.this) {
2651                 if (cipher == null) {
2652                     return SslUtils.INVALID_CIPHER;
2653                 }
2654                 return cipher;
2655             }
2656         }
2657 
2658         @Override
2659         public String getProtocol() {
2660             String protocol = this.protocol;
2661             if (protocol == null) {
2662                 synchronized (ReferenceCountedOpenSslEngine.this) {
2663                     if (!isDestroyed()) {
2664                         protocol = SSL.getVersion(ssl);
2665                     } else {
2666                         protocol = StringUtil.EMPTY_STRING;
2667                     }
2668                 }
2669             }
2670             return protocol;
2671         }
2672 
2673         @Override
2674         public String getPeerHost() {
2675             return ReferenceCountedOpenSslEngine.this.getPeerHost();
2676         }
2677 
2678         @Override
2679         public int getPeerPort() {
2680             return ReferenceCountedOpenSslEngine.this.getPeerPort();
2681         }
2682 
2683         @Override
2684         public int getPacketBufferSize() {
2685             return SSL.SSL_MAX_ENCRYPTED_LENGTH;
2686         }
2687 
2688         @Override
2689         public int getApplicationBufferSize() {
2690             return applicationBufferSize;
2691         }
2692 
2693         @Override
2694         public void tryExpandApplicationBufferSize(int packetLengthDataOnly) {
2695             if (packetLengthDataOnly > MAX_PLAINTEXT_LENGTH && applicationBufferSize != MAX_RECORD_SIZE) {
2696                 applicationBufferSize = MAX_RECORD_SIZE;
2697             }
2698         }
2699 
2700         @Override
2701         public String toString() {
2702             return "DefaultOpenSslSession{" +
2703                     "sessionContext=" + sessionContext +
2704                     ", id=" + id +
2705                     '}';
2706         }
2707 
2708         @Override
2709         public int hashCode() {
2710             return sessionId().hashCode();
2711         }
2712 
2713         @Override
2714         public boolean equals(Object o) {
2715             if (o == this) {
2716                 return true;
2717             }
2718             // We trust all sub-types as we use different types but the interface is package-private
2719             if (!(o instanceof OpenSslSession)) {
2720                 return false;
2721             }
2722             return sessionId().equals(((OpenSslSession) o).sessionId());
2723         }
2724     }
2725 
2726     private interface NativeSslException {
2727         int errorCode();
2728     }
2729 
2730     private static final class OpenSslException extends SSLException implements NativeSslException {
2731         private final int errorCode;
2732 
2733         OpenSslException(String reason, int errorCode) {
2734             super(reason);
2735             this.errorCode = errorCode;
2736         }
2737 
2738         @Override
2739         public int errorCode() {
2740             return errorCode;
2741         }
2742     }
2743 
2744     private static final class OpenSslHandshakeException extends SSLHandshakeException implements NativeSslException {
2745         private final int errorCode;
2746 
2747         OpenSslHandshakeException(String reason, int errorCode) {
2748             super(reason);
2749             this.errorCode = errorCode;
2750         }
2751 
2752         @Override
2753         public int errorCode() {
2754             return errorCode;
2755         }
2756     }
2757 }