View Javadoc
1   /*
2    * Copyright 2012 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    *   http://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.buffer.ByteBufUtil;
21  import io.netty.buffer.CompositeByteBuf;
22  import io.netty.buffer.Unpooled;
23  import io.netty.channel.Channel;
24  import io.netty.channel.ChannelConfig;
25  import io.netty.channel.ChannelException;
26  import io.netty.channel.ChannelFuture;
27  import io.netty.channel.ChannelFutureListener;
28  import io.netty.channel.ChannelHandlerContext;
29  import io.netty.channel.ChannelInboundHandler;
30  import io.netty.channel.ChannelOutboundHandler;
31  import io.netty.channel.ChannelPipeline;
32  import io.netty.channel.ChannelPromise;
33  import io.netty.channel.ChannelPromiseNotifier;
34  import io.netty.channel.PendingWriteQueue;
35  import io.netty.handler.codec.ByteToMessageDecoder;
36  import io.netty.handler.codec.UnsupportedMessageTypeException;
37  import io.netty.util.ReferenceCountUtil;
38  import io.netty.util.ReferenceCounted;
39  import io.netty.util.concurrent.DefaultPromise;
40  import io.netty.util.concurrent.EventExecutor;
41  import io.netty.util.concurrent.Future;
42  import io.netty.util.concurrent.FutureListener;
43  import io.netty.util.concurrent.ImmediateExecutor;
44  import io.netty.util.concurrent.Promise;
45  import io.netty.util.internal.PlatformDependent;
46  import io.netty.util.internal.ThrowableUtil;
47  import io.netty.util.internal.logging.InternalLogger;
48  import io.netty.util.internal.logging.InternalLoggerFactory;
49  
50  import java.io.IOException;
51  import java.net.SocketAddress;
52  import java.nio.ByteBuffer;
53  import java.nio.channels.ClosedChannelException;
54  import java.nio.channels.DatagramChannel;
55  import java.nio.channels.SocketChannel;
56  import java.util.ArrayList;
57  import java.util.List;
58  import java.util.concurrent.CountDownLatch;
59  import java.util.concurrent.Executor;
60  import java.util.concurrent.ScheduledFuture;
61  import java.util.concurrent.TimeUnit;
62  import java.util.regex.Pattern;
63  import javax.net.ssl.SSLEngine;
64  import javax.net.ssl.SSLEngineResult;
65  import javax.net.ssl.SSLEngineResult.HandshakeStatus;
66  import javax.net.ssl.SSLEngineResult.Status;
67  import javax.net.ssl.SSLException;
68  import javax.net.ssl.SSLSession;
69  
70  import static io.netty.handler.ssl.SslUtils.getEncryptedPacketLength;
71  
72  /**
73   * Adds <a href="http://en.wikipedia.org/wiki/Transport_Layer_Security">SSL
74   * &middot; TLS</a> and StartTLS support to a {@link Channel}.  Please refer
75   * to the <strong>"SecureChat"</strong> example in the distribution or the web
76   * site for the detailed usage.
77   *
78   * <h3>Beginning the handshake</h3>
79   * <p>
80   * Beside using the handshake {@link ChannelFuture} to get notified about the completion of the handshake it's
81   * also possible to detect it by implement the
82   * {@link ChannelInboundHandler#userEventTriggered(ChannelHandlerContext, Object)}
83   * method and check for a {@link SslHandshakeCompletionEvent}.
84   *
85   * <h3>Handshake</h3>
86   * <p>
87   * The handshake will be automatically issued for you once the {@link Channel} is active and
88   * {@link SSLEngine#getUseClientMode()} returns {@code true}.
89   * So no need to bother with it by your self.
90   *
91   * <h3>Closing the session</h3>
92   * <p>
93   * To close the SSL session, the {@link #close()} method should be
94   * called to send the {@code close_notify} message to the remote peer.  One
95   * exception is when you close the {@link Channel} - {@link SslHandler}
96   * intercepts the close request and send the {@code close_notify} message
97   * before the channel closure automatically.  Once the SSL session is closed,
98   * it is not reusable, and consequently you should create a new
99   * {@link SslHandler} with a new {@link SSLEngine} as explained in the
100  * following section.
101  *
102  * <h3>Restarting the session</h3>
103  * <p>
104  * To restart the SSL session, you must remove the existing closed
105  * {@link SslHandler} from the {@link ChannelPipeline}, insert a new
106  * {@link SslHandler} with a new {@link SSLEngine} into the pipeline,
107  * and start the handshake process as described in the first section.
108  *
109  * <h3>Implementing StartTLS</h3>
110  * <p>
111  * <a href="http://en.wikipedia.org/wiki/STARTTLS">StartTLS</a> is the
112  * communication pattern that secures the wire in the middle of the plaintext
113  * connection.  Please note that it is different from SSL &middot; TLS, that
114  * secures the wire from the beginning of the connection.  Typically, StartTLS
115  * is composed of three steps:
116  * <ol>
117  * <li>Client sends a StartTLS request to server.</li>
118  * <li>Server sends a StartTLS response to client.</li>
119  * <li>Client begins SSL handshake.</li>
120  * </ol>
121  * If you implement a server, you need to:
122  * <ol>
123  * <li>create a new {@link SslHandler} instance with {@code startTls} flag set
124  *     to {@code true},</li>
125  * <li>insert the {@link SslHandler} to the {@link ChannelPipeline}, and</li>
126  * <li>write a StartTLS response.</li>
127  * </ol>
128  * Please note that you must insert {@link SslHandler} <em>before</em> sending
129  * the StartTLS response.  Otherwise the client can send begin SSL handshake
130  * before {@link SslHandler} is inserted to the {@link ChannelPipeline}, causing
131  * data corruption.
132  * <p>
133  * The client-side implementation is much simpler.
134  * <ol>
135  * <li>Write a StartTLS request,</li>
136  * <li>wait for the StartTLS response,</li>
137  * <li>create a new {@link SslHandler} instance with {@code startTls} flag set
138  *     to {@code false},</li>
139  * <li>insert the {@link SslHandler} to the {@link ChannelPipeline}, and</li>
140  * <li>Initiate SSL handshake.</li>
141  * </ol>
142  *
143  * <h3>Known issues</h3>
144  * <p>
145  * Because of a known issue with the current implementation of the SslEngine that comes
146  * with Java it may be possible that you see blocked IO-Threads while a full GC is done.
147  * <p>
148  * So if you are affected you can workaround this problem by adjust the cache settings
149  * like shown below:
150  *
151  * <pre>
152  *     SslContext context = ...;
153  *     context.getServerSessionContext().setSessionCacheSize(someSaneSize);
154  *     context.getServerSessionContext().setSessionTime(someSameTimeout);
155  * </pre>
156  * <p>
157  * What values to use here depends on the nature of your application and should be set
158  * based on monitoring and debugging of it.
159  * For more details see
160  * <a href="https://github.com/netty/netty/issues/832">#832</a> in our issue tracker.
161  */
162 public class SslHandler extends ByteToMessageDecoder implements ChannelOutboundHandler {
163 
164     private static final InternalLogger logger =
165             InternalLoggerFactory.getInstance(SslHandler.class);
166 
167     private static final Pattern IGNORABLE_CLASS_IN_STACK = Pattern.compile(
168             "^.*(?:Socket|Datagram|Sctp|Udt)Channel.*$");
169     private static final Pattern IGNORABLE_ERROR_MESSAGE = Pattern.compile(
170             "^.*(?:connection.*(?:reset|closed|abort|broken)|broken.*pipe).*$", Pattern.CASE_INSENSITIVE);
171 
172     /**
173      * Used in {@link #unwrapNonAppData(ChannelHandlerContext)} as input for
174      * {@link #unwrap(ChannelHandlerContext, ByteBuf, int,  int)}.  Using this static instance reduce object
175      * creation as {@link Unpooled#EMPTY_BUFFER#nioBuffer()} creates a new {@link ByteBuffer} everytime.
176      */
177     private static final SSLException SSLENGINE_CLOSED = ThrowableUtil.unknownStackTrace(
178             new SSLException("SSLEngine closed already"), SslHandler.class, "wrap(...)");
179     private static final SSLException HANDSHAKE_TIMED_OUT = ThrowableUtil.unknownStackTrace(
180             new SSLException("handshake timed out"), SslHandler.class, "handshake(...)");
181     private static final ClosedChannelException CHANNEL_CLOSED = ThrowableUtil.unknownStackTrace(
182             new ClosedChannelException(), SslHandler.class, "channelInactive(...)");
183 
184     private enum SslEngineType {
185         TCNATIVE(true, COMPOSITE_CUMULATOR) {
186             @Override
187             SSLEngineResult unwrap(SslHandler handler, ByteBuf in, int readerIndex, int len, ByteBuf out)
188                     throws SSLException {
189                 int nioBufferCount = in.nioBufferCount();
190                 int writerIndex = out.writerIndex();
191                 final SSLEngineResult result;
192                 if (nioBufferCount > 1) {
193                     /*
194                      * If {@link OpenSslEngine} is in use,
195                      * we can use a special {@link OpenSslEngine#unwrap(ByteBuffer[], ByteBuffer[])} method
196                      * that accepts multiple {@link ByteBuffer}s without additional memory copies.
197                      */
198                     ReferenceCountedOpenSslEngine opensslEngine = (ReferenceCountedOpenSslEngine) handler.engine;
199                     try {
200                         handler.singleBuffer[0] = toByteBuffer(out, writerIndex,
201                             out.writableBytes());
202                         result = opensslEngine.unwrap(in.nioBuffers(readerIndex, len), handler.singleBuffer);
203                     } finally {
204                         handler.singleBuffer[0] = null;
205                     }
206                 } else {
207                     result = handler.engine.unwrap(toByteBuffer(in, readerIndex, len),
208                         toByteBuffer(out, writerIndex, out.writableBytes()));
209                 }
210                 out.writerIndex(writerIndex + result.bytesProduced());
211                 return result;
212             }
213 
214             @Override
215             int getPacketBufferSize(SslHandler handler) {
216                 return ((ReferenceCountedOpenSslEngine) handler.engine).maxEncryptedPacketLength0();
217             }
218 
219             @Override
220             int calculateWrapBufferCapacity(SslHandler handler, int pendingBytes, int numComponents) {
221                 return ((ReferenceCountedOpenSslEngine) handler.engine).calculateMaxLengthForWrap(pendingBytes,
222                                                                                                   numComponents);
223             }
224 
225             @Override
226             int calculatePendingData(SslHandler handler, int guess) {
227                 int sslPending = ((ReferenceCountedOpenSslEngine) handler.engine).sslPending();
228                 return sslPending > 0 ? sslPending : guess;
229             }
230 
231             @Override
232             boolean jdkCompatibilityMode(SSLEngine engine) {
233                 return ((ReferenceCountedOpenSslEngine) engine).jdkCompatibilityMode;
234             }
235         },
236         CONSCRYPT(true, COMPOSITE_CUMULATOR) {
237             @Override
238             SSLEngineResult unwrap(SslHandler handler, ByteBuf in, int readerIndex, int len, ByteBuf out)
239                     throws SSLException {
240                 int nioBufferCount = in.nioBufferCount();
241                 int writerIndex = out.writerIndex();
242                 final SSLEngineResult result;
243                 if (nioBufferCount > 1) {
244                     /*
245                      * Use a special unwrap method without additional memory copies.
246                      */
247                     try {
248                         handler.singleBuffer[0] = toByteBuffer(out, writerIndex, out.writableBytes());
249                         result = ((ConscryptAlpnSslEngine) handler.engine).unwrap(
250                                 in.nioBuffers(readerIndex, len),
251                                 handler.singleBuffer);
252                     } finally {
253                         handler.singleBuffer[0] = null;
254                     }
255                 } else {
256                     result = handler.engine.unwrap(toByteBuffer(in, readerIndex, len),
257                             toByteBuffer(out, writerIndex, out.writableBytes()));
258                 }
259                 out.writerIndex(writerIndex + result.bytesProduced());
260                 return result;
261             }
262 
263             @Override
264             int calculateWrapBufferCapacity(SslHandler handler, int pendingBytes, int numComponents) {
265                 return ((ConscryptAlpnSslEngine) handler.engine).calculateOutNetBufSize(pendingBytes, numComponents);
266             }
267 
268             @Override
269             int calculatePendingData(SslHandler handler, int guess) {
270                 return guess;
271             }
272 
273             @Override
274             boolean jdkCompatibilityMode(SSLEngine engine) {
275                 return true;
276             }
277         },
278         JDK(false, MERGE_CUMULATOR) {
279             @Override
280             SSLEngineResult unwrap(SslHandler handler, ByteBuf in, int readerIndex, int len, ByteBuf out)
281                     throws SSLException {
282                 int writerIndex = out.writerIndex();
283                 final SSLEngineResult result = handler.engine.unwrap(toByteBuffer(in, readerIndex, len),
284                     toByteBuffer(out, writerIndex, out.writableBytes()));
285                 out.writerIndex(writerIndex + result.bytesProduced());
286                 return result;
287             }
288 
289             @Override
290             int calculateWrapBufferCapacity(SslHandler handler, int pendingBytes, int numComponents) {
291                 return handler.engine.getSession().getPacketBufferSize();
292             }
293 
294             @Override
295             int calculatePendingData(SslHandler handler, int guess) {
296                 return guess;
297             }
298 
299             @Override
300             boolean jdkCompatibilityMode(SSLEngine engine) {
301                 return true;
302             }
303         };
304 
305         static SslEngineType forEngine(SSLEngine engine) {
306             return engine instanceof ReferenceCountedOpenSslEngine ? TCNATIVE :
307                    engine instanceof ConscryptAlpnSslEngine ? CONSCRYPT : JDK;
308         }
309 
310         SslEngineType(boolean wantsDirectBuffer, Cumulator cumulator) {
311             this.wantsDirectBuffer = wantsDirectBuffer;
312             this.cumulator = cumulator;
313         }
314 
315         int getPacketBufferSize(SslHandler handler) {
316             return handler.engine.getSession().getPacketBufferSize();
317         }
318 
319         abstract SSLEngineResult unwrap(SslHandler handler, ByteBuf in, int readerIndex, int len, ByteBuf out)
320                 throws SSLException;
321 
322         abstract int calculateWrapBufferCapacity(SslHandler handler, int pendingBytes, int numComponents);
323 
324         abstract int calculatePendingData(SslHandler handler, int guess);
325 
326         abstract boolean jdkCompatibilityMode(SSLEngine engine);
327 
328         // BEGIN Platform-dependent flags
329 
330         /**
331          * {@code true} if and only if {@link SSLEngine} expects a direct buffer.
332          */
333         final boolean wantsDirectBuffer;
334 
335         // END Platform-dependent flags
336 
337         /**
338          * When using JDK {@link SSLEngine}, we use {@link #MERGE_CUMULATOR} because it works only with
339          * one {@link ByteBuffer}.
340          *
341          * When using {@link OpenSslEngine}, we can use {@link #COMPOSITE_CUMULATOR} because it has
342          * {@link OpenSslEngine#unwrap(ByteBuffer[], ByteBuffer[])} which works with multiple {@link ByteBuffer}s
343          * and which does not need to do extra memory copies.
344          */
345         final Cumulator cumulator;
346     }
347 
348     private volatile ChannelHandlerContext ctx;
349     private final SSLEngine engine;
350     private final SslEngineType engineType;
351     private final Executor delegatedTaskExecutor;
352     private final boolean jdkCompatibilityMode;
353 
354     /**
355      * Used if {@link SSLEngine#wrap(ByteBuffer[], ByteBuffer)} and {@link SSLEngine#unwrap(ByteBuffer, ByteBuffer[])}
356      * should be called with a {@link ByteBuf} that is only backed by one {@link ByteBuffer} to reduce the object
357      * creation.
358      */
359     private final ByteBuffer[] singleBuffer = new ByteBuffer[1];
360 
361     private final boolean startTls;
362     private boolean sentFirstMessage;
363     private boolean flushedBeforeHandshake;
364     private boolean readDuringHandshake;
365     private boolean handshakeStarted;
366     private PendingWriteQueue pendingUnencryptedWrites;
367 
368     private Promise<Channel> handshakePromise = new LazyChannelPromise();
369     private final LazyChannelPromise sslClosePromise = new LazyChannelPromise();
370 
371     /**
372      * Set by wrap*() methods when something is produced.
373      * {@link #channelReadComplete(ChannelHandlerContext)} will check this flag, clear it, and call ctx.flush().
374      */
375     private boolean needsFlush;
376 
377     private boolean outboundClosed;
378 
379     private int packetLength;
380 
381     /**
382      * This flag is used to determine if we need to call {@link ChannelHandlerContext#read()} to consume more data
383      * when {@link ChannelConfig#isAutoRead()} is {@code false}.
384      */
385     private boolean firedChannelRead;
386 
387     private volatile long handshakeTimeoutMillis = 10000;
388     private volatile long closeNotifyFlushTimeoutMillis = 3000;
389     private volatile long closeNotifyReadTimeoutMillis;
390 
391     /**
392      * Creates a new instance.
393      *
394      * @param engine  the {@link SSLEngine} this handler will use
395      */
396     public SslHandler(SSLEngine engine) {
397         this(engine, false);
398     }
399 
400     /**
401      * Creates a new instance.
402      *
403      * @param engine    the {@link SSLEngine} this handler will use
404      * @param startTls  {@code true} if the first write request shouldn't be
405      *                  encrypted by the {@link SSLEngine}
406      */
407     @SuppressWarnings("deprecation")
408     public SslHandler(SSLEngine engine, boolean startTls) {
409         this(engine, startTls, ImmediateExecutor.INSTANCE);
410     }
411 
412     /**
413      * @deprecated Use {@link #SslHandler(SSLEngine)} instead.
414      */
415     @Deprecated
416     public SslHandler(SSLEngine engine, Executor delegatedTaskExecutor) {
417         this(engine, false, delegatedTaskExecutor);
418     }
419 
420     /**
421      * @deprecated Use {@link #SslHandler(SSLEngine, boolean)} instead.
422      */
423     @Deprecated
424     public SslHandler(SSLEngine engine, boolean startTls, Executor delegatedTaskExecutor) {
425         if (engine == null) {
426             throw new NullPointerException("engine");
427         }
428         if (delegatedTaskExecutor == null) {
429             throw new NullPointerException("delegatedTaskExecutor");
430         }
431         this.engine = engine;
432         engineType = SslEngineType.forEngine(engine);
433         this.delegatedTaskExecutor = delegatedTaskExecutor;
434         this.startTls = startTls;
435         this.jdkCompatibilityMode = engineType.jdkCompatibilityMode(engine);
436         setCumulator(engineType.cumulator);
437     }
438 
439     public long getHandshakeTimeoutMillis() {
440         return handshakeTimeoutMillis;
441     }
442 
443     public void setHandshakeTimeout(long handshakeTimeout, TimeUnit unit) {
444         if (unit == null) {
445             throw new NullPointerException("unit");
446         }
447 
448         setHandshakeTimeoutMillis(unit.toMillis(handshakeTimeout));
449     }
450 
451     public void setHandshakeTimeoutMillis(long handshakeTimeoutMillis) {
452         if (handshakeTimeoutMillis < 0) {
453             throw new IllegalArgumentException(
454                     "handshakeTimeoutMillis: " + handshakeTimeoutMillis + " (expected: >= 0)");
455         }
456         this.handshakeTimeoutMillis = handshakeTimeoutMillis;
457     }
458 
459     /**
460      * @deprecated use {@link #getCloseNotifyFlushTimeoutMillis()}
461      */
462     @Deprecated
463     public long getCloseNotifyTimeoutMillis() {
464         return getCloseNotifyFlushTimeoutMillis();
465     }
466 
467     /**
468      * @deprecated use {@link #setCloseNotifyFlushTimeout(long, TimeUnit)}
469      */
470     @Deprecated
471     public void setCloseNotifyTimeout(long closeNotifyTimeout, TimeUnit unit) {
472         setCloseNotifyFlushTimeout(closeNotifyTimeout, unit);
473     }
474 
475     /**
476      * @deprecated use {@link #setCloseNotifyFlushTimeoutMillis(long)}
477      */
478     @Deprecated
479     public void setCloseNotifyTimeoutMillis(long closeNotifyFlushTimeoutMillis) {
480         setCloseNotifyFlushTimeoutMillis(closeNotifyFlushTimeoutMillis);
481     }
482 
483     /**
484      * Gets the timeout for flushing the close_notify that was triggered by closing the
485      * {@link Channel}. If the close_notify was not flushed in the given timeout the {@link Channel} will be closed
486      * forcibly.
487      */
488     public final long getCloseNotifyFlushTimeoutMillis() {
489         return closeNotifyFlushTimeoutMillis;
490     }
491 
492     /**
493      * Sets the timeout for flushing the close_notify that was triggered by closing the
494      * {@link Channel}. If the close_notify was not flushed in the given timeout the {@link Channel} will be closed
495      * forcibly.
496      */
497     public final void setCloseNotifyFlushTimeout(long closeNotifyFlushTimeout, TimeUnit unit) {
498         setCloseNotifyFlushTimeoutMillis(unit.toMillis(closeNotifyFlushTimeout));
499     }
500 
501     /**
502      * See {@link #setCloseNotifyFlushTimeout(long, TimeUnit)}.
503      */
504     public final void setCloseNotifyFlushTimeoutMillis(long closeNotifyFlushTimeoutMillis) {
505         if (closeNotifyFlushTimeoutMillis < 0) {
506             throw new IllegalArgumentException(
507                     "closeNotifyFlushTimeoutMillis: " + closeNotifyFlushTimeoutMillis + " (expected: >= 0)");
508         }
509         this.closeNotifyFlushTimeoutMillis = closeNotifyFlushTimeoutMillis;
510     }
511 
512     /**
513      * Gets the timeout (in ms) for receiving the response for the close_notify that was triggered by closing the
514      * {@link Channel}. This timeout starts after the close_notify message was successfully written to the
515      * remote peer. Use {@code 0} to directly close the {@link Channel} and not wait for the response.
516      */
517     public final long getCloseNotifyReadTimeoutMillis() {
518         return closeNotifyReadTimeoutMillis;
519     }
520 
521     /**
522      * Sets the timeout  for receiving the response for the close_notify that was triggered by closing the
523      * {@link Channel}. This timeout starts after the close_notify message was successfully written to the
524      * remote peer. Use {@code 0} to directly close the {@link Channel} and not wait for the response.
525      */
526     public final void setCloseNotifyReadTimeout(long closeNotifyReadTimeout, TimeUnit unit) {
527         setCloseNotifyReadTimeoutMillis(unit.toMillis(closeNotifyReadTimeout));
528     }
529 
530     /**
531      * See {@link #setCloseNotifyReadTimeout(long, TimeUnit)}.
532      */
533     public final void setCloseNotifyReadTimeoutMillis(long closeNotifyReadTimeoutMillis) {
534         if (closeNotifyReadTimeoutMillis < 0) {
535             throw new IllegalArgumentException(
536                     "closeNotifyReadTimeoutMillis: " + closeNotifyReadTimeoutMillis + " (expected: >= 0)");
537         }
538         this.closeNotifyReadTimeoutMillis = closeNotifyReadTimeoutMillis;
539     }
540 
541     /**
542      * Returns the {@link SSLEngine} which is used by this handler.
543      */
544     public SSLEngine engine() {
545         return engine;
546     }
547 
548     /**
549      * Returns the name of the current application-level protocol.
550      *
551      * @return the protocol name or {@code null} if application-level protocol has not been negotiated
552      */
553     public String applicationProtocol() {
554         SSLEngine engine = engine();
555         if (!(engine instanceof ApplicationProtocolAccessor)) {
556             return null;
557         }
558 
559         return ((ApplicationProtocolAccessor) engine).getNegotiatedApplicationProtocol();
560     }
561 
562     /**
563      * Returns a {@link Future} that will get notified once the current TLS handshake completes.
564      *
565      * @return the {@link Future} for the initial TLS handshake if {@link #renegotiate()} was not invoked.
566      *         The {@link Future} for the most recent {@linkplain #renegotiate() TLS renegotiation} otherwise.
567      */
568     public Future<Channel> handshakeFuture() {
569         return handshakePromise;
570     }
571 
572     /**
573      * Sends an SSL {@code close_notify} message to the specified channel and
574      * destroys the underlying {@link SSLEngine}.
575      *
576      * @deprecated use {@link Channel#close()} or {@link ChannelHandlerContext#close()}
577      */
578     @Deprecated
579     public ChannelFuture close() {
580         return close(ctx.newPromise());
581     }
582 
583     /**
584      * See {@link #close()}
585      *
586      * @deprecated use {@link Channel#close()} or {@link ChannelHandlerContext#close()}
587      */
588     @Deprecated
589     public ChannelFuture close(final ChannelPromise promise) {
590         final ChannelHandlerContext ctx = this.ctx;
591         ctx.executor().execute(new Runnable() {
592             @Override
593             public void run() {
594                 outboundClosed = true;
595                 engine.closeOutbound();
596                 try {
597                     flush(ctx, promise);
598                 } catch (Exception e) {
599                     if (!promise.tryFailure(e)) {
600                         logger.warn("{} flush() raised a masked exception.", ctx.channel(), e);
601                     }
602                 }
603             }
604         });
605 
606         return promise;
607     }
608 
609     /**
610      * Return the {@link Future} that will get notified if the inbound of the {@link SSLEngine} is closed.
611      *
612      * This method will return the same {@link Future} all the time.
613      *
614      * @see SSLEngine
615      */
616     public Future<Channel> sslCloseFuture() {
617         return sslClosePromise;
618     }
619 
620     @Override
621     public void handlerRemoved0(ChannelHandlerContext ctx) throws Exception {
622         if (!pendingUnencryptedWrites.isEmpty()) {
623             // Check if queue is not empty first because create a new ChannelException is expensive
624             pendingUnencryptedWrites.removeAndFailAll(new ChannelException("Pending write on removal of SslHandler"));
625         }
626         pendingUnencryptedWrites = null;
627         if (engine instanceof ReferenceCounted) {
628             ((ReferenceCounted) engine).release();
629         }
630     }
631 
632     @Override
633     public void bind(ChannelHandlerContext ctx, SocketAddress localAddress, ChannelPromise promise) throws Exception {
634         ctx.bind(localAddress, promise);
635     }
636 
637     @Override
638     public void connect(ChannelHandlerContext ctx, SocketAddress remoteAddress, SocketAddress localAddress,
639                         ChannelPromise promise) throws Exception {
640         ctx.connect(remoteAddress, localAddress, promise);
641     }
642 
643     @Override
644     public void deregister(ChannelHandlerContext ctx, ChannelPromise promise) throws Exception {
645         ctx.deregister(promise);
646     }
647 
648     @Override
649     public void disconnect(final ChannelHandlerContext ctx,
650                            final ChannelPromise promise) throws Exception {
651         closeOutboundAndChannel(ctx, promise, true);
652     }
653 
654     @Override
655     public void close(final ChannelHandlerContext ctx,
656                       final ChannelPromise promise) throws Exception {
657         closeOutboundAndChannel(ctx, promise, false);
658     }
659 
660     @Override
661     public void read(ChannelHandlerContext ctx) throws Exception {
662         if (!handshakePromise.isDone()) {
663             readDuringHandshake = true;
664         }
665 
666         ctx.read();
667     }
668 
669     private static IllegalStateException newPendingWritesNullException() {
670         return new IllegalStateException("pendingUnencryptedWrites is null, handlerRemoved0 called?");
671     }
672 
673     @Override
674     public void write(final ChannelHandlerContext ctx, Object msg, ChannelPromise promise) throws Exception {
675         if (!(msg instanceof ByteBuf)) {
676             UnsupportedMessageTypeException exception = new UnsupportedMessageTypeException(msg, ByteBuf.class);
677             ReferenceCountUtil.safeRelease(msg);
678             promise.setFailure(exception);
679         } else if (pendingUnencryptedWrites == null) {
680             ReferenceCountUtil.safeRelease(msg);
681             promise.setFailure(newPendingWritesNullException());
682         } else {
683             pendingUnencryptedWrites.add(msg, promise);
684         }
685     }
686 
687     @Override
688     public void flush(ChannelHandlerContext ctx) throws Exception {
689         // Do not encrypt the first write request if this handler is
690         // created with startTLS flag turned on.
691         if (startTls && !sentFirstMessage) {
692             sentFirstMessage = true;
693             pendingUnencryptedWrites.removeAndWriteAll();
694             forceFlush(ctx);
695             return;
696         }
697 
698         try {
699             wrapAndFlush(ctx);
700         } catch (Throwable cause) {
701             setHandshakeFailure(ctx, cause);
702             PlatformDependent.throwException(cause);
703         }
704     }
705 
706     private void wrapAndFlush(ChannelHandlerContext ctx) throws SSLException {
707         if (pendingUnencryptedWrites.isEmpty()) {
708             // It's important to NOT use a voidPromise here as the user
709             // may want to add a ChannelFutureListener to the ChannelPromise later.
710             //
711             // See https://github.com/netty/netty/issues/3364
712             pendingUnencryptedWrites.add(Unpooled.EMPTY_BUFFER, ctx.newPromise());
713         }
714         if (!handshakePromise.isDone()) {
715             flushedBeforeHandshake = true;
716         }
717         try {
718             wrap(ctx, false);
719         } finally {
720             // We may have written some parts of data before an exception was thrown so ensure we always flush.
721             // See https://github.com/netty/netty/issues/3900#issuecomment-172481830
722             forceFlush(ctx);
723         }
724     }
725 
726     // This method will not call setHandshakeFailure(...) !
727     private void wrap(ChannelHandlerContext ctx, boolean inUnwrap) throws SSLException {
728         ByteBuf out = null;
729         ChannelPromise promise = null;
730         ByteBufAllocator alloc = ctx.alloc();
731         boolean needUnwrap = false;
732         try {
733             // Only continue to loop if the handler was not removed in the meantime.
734             // See https://github.com/netty/netty/issues/5860
735             while (!ctx.isRemoved()) {
736                 Object msg = pendingUnencryptedWrites.current();
737                 if (msg == null) {
738                     break;
739                 }
740 
741                 ByteBuf buf = (ByteBuf) msg;
742                 if (out == null) {
743                     out = allocateOutNetBuf(ctx, buf.readableBytes(), buf.nioBufferCount());
744                 }
745 
746                 SSLEngineResult result = wrap(alloc, engine, buf, out);
747 
748                 if (result.getStatus() == Status.CLOSED) {
749                     // SSLEngine has been closed already.
750                     // Any further write attempts should be denied.
751                     pendingUnencryptedWrites.removeAndFailAll(SSLENGINE_CLOSED);
752                     return;
753                 } else {
754                     if (!buf.isReadable()) {
755                         promise = pendingUnencryptedWrites.remove();
756                     } else {
757                         promise = null;
758                     }
759 
760                     switch (result.getHandshakeStatus()) {
761                         case NEED_TASK:
762                             runDelegatedTasks();
763                             break;
764                         case FINISHED:
765                             setHandshakeSuccess();
766                             // deliberate fall-through
767                         case NOT_HANDSHAKING:
768                             setHandshakeSuccessIfStillHandshaking();
769                             // deliberate fall-through
770                         case NEED_WRAP:
771                             finishWrap(ctx, out, promise, inUnwrap, false);
772                             promise = null;
773                             out = null;
774                             break;
775                         case NEED_UNWRAP:
776                             needUnwrap = true;
777                             return;
778                         default:
779                             throw new IllegalStateException(
780                                     "Unknown handshake status: " + result.getHandshakeStatus());
781                     }
782                 }
783             }
784         } finally {
785             finishWrap(ctx, out, promise, inUnwrap, needUnwrap);
786         }
787     }
788 
789     private void finishWrap(ChannelHandlerContext ctx, ByteBuf out, ChannelPromise promise, boolean inUnwrap,
790             boolean needUnwrap) {
791         if (out == null) {
792             out = Unpooled.EMPTY_BUFFER;
793         } else if (!out.isReadable()) {
794             out.release();
795             out = Unpooled.EMPTY_BUFFER;
796         }
797 
798         if (promise != null) {
799             ctx.write(out, promise);
800         } else {
801             ctx.write(out);
802         }
803 
804         if (inUnwrap) {
805             needsFlush = true;
806         }
807 
808         if (needUnwrap) {
809             // The underlying engine is starving so we need to feed it with more data.
810             // See https://github.com/netty/netty/pull/5039
811             readIfNeeded(ctx);
812         }
813     }
814 
815     /**
816      * This method will not call
817      * {@link #setHandshakeFailure(ChannelHandlerContext, Throwable, boolean, boolean)} or
818      * {@link #setHandshakeFailure(ChannelHandlerContext, Throwable)}.
819      * @return {@code true} if this method ends on {@link SSLEngineResult.HandshakeStatus#NOT_HANDSHAKING}.
820      */
821     private boolean wrapNonAppData(ChannelHandlerContext ctx, boolean inUnwrap) throws SSLException {
822         ByteBuf out = null;
823         ByteBufAllocator alloc = ctx.alloc();
824         try {
825             // Only continue to loop if the handler was not removed in the meantime.
826             // See https://github.com/netty/netty/issues/5860
827             while (!ctx.isRemoved()) {
828                 if (out == null) {
829                     // As this is called for the handshake we have no real idea how big the buffer needs to be.
830                     // That said 2048 should give us enough room to include everything like ALPN / NPN data.
831                     // If this is not enough we will increase the buffer in wrap(...).
832                     out = allocateOutNetBuf(ctx, 2048, 1);
833                 }
834                 SSLEngineResult result = wrap(alloc, engine, Unpooled.EMPTY_BUFFER, out);
835 
836                 if (result.bytesProduced() > 0) {
837                     ctx.write(out);
838                     if (inUnwrap) {
839                         needsFlush = true;
840                     }
841                     out = null;
842                 }
843 
844                 switch (result.getHandshakeStatus()) {
845                     case FINISHED:
846                         setHandshakeSuccess();
847                         return false;
848                     case NEED_TASK:
849                         runDelegatedTasks();
850                         break;
851                     case NEED_UNWRAP:
852                         if (inUnwrap) {
853                             // If we asked for a wrap, the engine requested an unwrap, and we are in unwrap there is
854                             // no use in trying to call wrap again because we have already attempted (or will after we
855                             // return) to feed more data to the engine.
856                             return false;
857                         }
858 
859                         unwrapNonAppData(ctx);
860                         break;
861                     case NEED_WRAP:
862                         break;
863                     case NOT_HANDSHAKING:
864                         setHandshakeSuccessIfStillHandshaking();
865                         // Workaround for TLS False Start problem reported at:
866                         // https://github.com/netty/netty/issues/1108#issuecomment-14266970
867                         if (!inUnwrap) {
868                             unwrapNonAppData(ctx);
869                         }
870                         return true;
871                     default:
872                         throw new IllegalStateException("Unknown handshake status: " + result.getHandshakeStatus());
873                 }
874 
875                 if (result.bytesProduced() == 0) {
876                     break;
877                 }
878 
879                 // It should not consume empty buffers when it is not handshaking
880                 // Fix for Android, where it was encrypting empty buffers even when not handshaking
881                 if (result.bytesConsumed() == 0 && result.getHandshakeStatus() == HandshakeStatus.NOT_HANDSHAKING) {
882                     break;
883                 }
884             }
885         }  finally {
886             if (out != null) {
887                 out.release();
888             }
889         }
890         return false;
891     }
892 
893     private SSLEngineResult wrap(ByteBufAllocator alloc, SSLEngine engine, ByteBuf in, ByteBuf out)
894             throws SSLException {
895         ByteBuf newDirectIn = null;
896         try {
897             int readerIndex = in.readerIndex();
898             int readableBytes = in.readableBytes();
899 
900             // We will call SslEngine.wrap(ByteBuffer[], ByteBuffer) to allow efficient handling of
901             // CompositeByteBuf without force an extra memory copy when CompositeByteBuffer.nioBuffer() is called.
902             final ByteBuffer[] in0;
903             if (in.isDirect() || !engineType.wantsDirectBuffer) {
904                 // As CompositeByteBuf.nioBufferCount() can be expensive (as it needs to check all composed ByteBuf
905                 // to calculate the count) we will just assume a CompositeByteBuf contains more then 1 ByteBuf.
906                 // The worst that can happen is that we allocate an extra ByteBuffer[] in CompositeByteBuf.nioBuffers()
907                 // which is better then walking the composed ByteBuf in most cases.
908                 if (!(in instanceof CompositeByteBuf) && in.nioBufferCount() == 1) {
909                     in0 = singleBuffer;
910                     // We know its only backed by 1 ByteBuffer so use internalNioBuffer to keep object allocation
911                     // to a minimum.
912                     in0[0] = in.internalNioBuffer(readerIndex, readableBytes);
913                 } else {
914                     in0 = in.nioBuffers();
915                 }
916             } else {
917                 // We could even go further here and check if its a CompositeByteBuf and if so try to decompose it and
918                 // only replace the ByteBuffer that are not direct. At the moment we just will replace the whole
919                 // CompositeByteBuf to keep the complexity to a minimum
920                 newDirectIn = alloc.directBuffer(readableBytes);
921                 newDirectIn.writeBytes(in, readerIndex, readableBytes);
922                 in0 = singleBuffer;
923                 in0[0] = newDirectIn.internalNioBuffer(newDirectIn.readerIndex(), readableBytes);
924             }
925 
926             for (;;) {
927                 ByteBuffer out0 = out.nioBuffer(out.writerIndex(), out.writableBytes());
928                 SSLEngineResult result = engine.wrap(in0, out0);
929                 in.skipBytes(result.bytesConsumed());
930                 out.writerIndex(out.writerIndex() + result.bytesProduced());
931 
932                 switch (result.getStatus()) {
933                 case BUFFER_OVERFLOW:
934                     out.ensureWritable(engine.getSession().getPacketBufferSize());
935                     break;
936                 default:
937                     return result;
938                 }
939             }
940         } finally {
941             // Null out to allow GC of ByteBuffer
942             singleBuffer[0] = null;
943 
944             if (newDirectIn != null) {
945                 newDirectIn.release();
946             }
947         }
948     }
949 
950     @Override
951     public void channelInactive(ChannelHandlerContext ctx) throws Exception {
952         // Make sure to release SSLEngine,
953         // and notify the handshake future if the connection has been closed during handshake.
954         setHandshakeFailure(ctx, CHANNEL_CLOSED, !outboundClosed, handshakeStarted);
955 
956         // Ensure we always notify the sslClosePromise as well
957         notifyClosePromise(CHANNEL_CLOSED);
958 
959         super.channelInactive(ctx);
960     }
961 
962     @Override
963     public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
964         if (ignoreException(cause)) {
965             // It is safe to ignore the 'connection reset by peer' or
966             // 'broken pipe' error after sending close_notify.
967             if (logger.isDebugEnabled()) {
968                 logger.debug(
969                         "{} Swallowing a harmless 'connection reset by peer / broken pipe' error that occurred " +
970                         "while writing close_notify in response to the peer's close_notify", ctx.channel(), cause);
971             }
972 
973             // Close the connection explicitly just in case the transport
974             // did not close the connection automatically.
975             if (ctx.channel().isActive()) {
976                 ctx.close();
977             }
978         } else {
979             ctx.fireExceptionCaught(cause);
980         }
981     }
982 
983     /**
984      * Checks if the given {@link Throwable} can be ignore and just "swallowed"
985      *
986      * When an ssl connection is closed a close_notify message is sent.
987      * After that the peer also sends close_notify however, it's not mandatory to receive
988      * the close_notify. The party who sent the initial close_notify can close the connection immediately
989      * then the peer will get connection reset error.
990      *
991      */
992     private boolean ignoreException(Throwable t) {
993         if (!(t instanceof SSLException) && t instanceof IOException && sslClosePromise.isDone()) {
994             String message = t.getMessage();
995 
996             // first try to match connection reset / broke peer based on the regex. This is the fastest way
997             // but may fail on different jdk impls or OS's
998             if (message != null && IGNORABLE_ERROR_MESSAGE.matcher(message).matches()) {
999                 return true;
1000             }
1001 
1002             // Inspect the StackTraceElements to see if it was a connection reset / broken pipe or not
1003             StackTraceElement[] elements = t.getStackTrace();
1004             for (StackTraceElement element: elements) {
1005                 String classname = element.getClassName();
1006                 String methodname = element.getMethodName();
1007 
1008                 // skip all classes that belong to the io.netty package
1009                 if (classname.startsWith("io.netty.")) {
1010                     continue;
1011                 }
1012 
1013                 // check if the method name is read if not skip it
1014                 if (!"read".equals(methodname)) {
1015                     continue;
1016                 }
1017 
1018                 // This will also match against SocketInputStream which is used by openjdk 7 and maybe
1019                 // also others
1020                 if (IGNORABLE_CLASS_IN_STACK.matcher(classname).matches()) {
1021                     return true;
1022                 }
1023 
1024                 try {
1025                     // No match by now.. Try to load the class via classloader and inspect it.
1026                     // This is mainly done as other JDK implementations may differ in name of
1027                     // the impl.
1028                     Class<?> clazz = PlatformDependent.getClassLoader(getClass()).loadClass(classname);
1029 
1030                     if (SocketChannel.class.isAssignableFrom(clazz)
1031                             || DatagramChannel.class.isAssignableFrom(clazz)) {
1032                         return true;
1033                     }
1034 
1035                     // also match against SctpChannel via String matching as it may not present.
1036                     if (PlatformDependent.javaVersion() >= 7
1037                             && "com.sun.nio.sctp.SctpChannel".equals(clazz.getSuperclass().getName())) {
1038                         return true;
1039                     }
1040                 } catch (Throwable cause) {
1041                     logger.debug("Unexpected exception while loading class {} classname {}",
1042                                  getClass(), classname, cause);
1043                 }
1044             }
1045         }
1046 
1047         return false;
1048     }
1049 
1050     /**
1051      * Returns {@code true} if the given {@link ByteBuf} is encrypted. Be aware that this method
1052      * will not increase the readerIndex of the given {@link ByteBuf}.
1053      *
1054      * @param   buffer
1055      *                  The {@link ByteBuf} to read from. Be aware that it must have at least 5 bytes to read,
1056      *                  otherwise it will throw an {@link IllegalArgumentException}.
1057      * @return encrypted
1058      *                  {@code true} if the {@link ByteBuf} is encrypted, {@code false} otherwise.
1059      * @throws IllegalArgumentException
1060      *                  Is thrown if the given {@link ByteBuf} has not at least 5 bytes to read.
1061      */
1062     public static boolean isEncrypted(ByteBuf buffer) {
1063         if (buffer.readableBytes() < SslUtils.SSL_RECORD_HEADER_LENGTH) {
1064             throw new IllegalArgumentException(
1065                     "buffer must have at least " + SslUtils.SSL_RECORD_HEADER_LENGTH + " readable bytes");
1066         }
1067         return getEncryptedPacketLength(buffer, buffer.readerIndex()) != SslUtils.NOT_ENCRYPTED;
1068     }
1069 
1070     private void decodeJdkCompatible(ChannelHandlerContext ctx, ByteBuf in) throws NotSslRecordException {
1071         int packetLength = this.packetLength;
1072         // If we calculated the length of the current SSL record before, use that information.
1073         if (packetLength > 0) {
1074             if (in.readableBytes() < packetLength) {
1075                 return;
1076             }
1077         } else {
1078             // Get the packet length and wait until we get a packets worth of data to unwrap.
1079             final int readableBytes = in.readableBytes();
1080             if (readableBytes < SslUtils.SSL_RECORD_HEADER_LENGTH) {
1081                 return;
1082             }
1083             packetLength = getEncryptedPacketLength(in, in.readerIndex());
1084             if (packetLength == SslUtils.NOT_ENCRYPTED) {
1085                 // Not an SSL/TLS packet
1086                 NotSslRecordException e = new NotSslRecordException(
1087                         "not an SSL/TLS record: " + ByteBufUtil.hexDump(in));
1088                 in.skipBytes(in.readableBytes());
1089 
1090                 // First fail the handshake promise as we may need to have access to the SSLEngine which may
1091                 // be released because the user will remove the SslHandler in an exceptionCaught(...) implementation.
1092                 setHandshakeFailure(ctx, e);
1093 
1094                 throw e;
1095             }
1096             assert packetLength > 0;
1097             if (packetLength > readableBytes) {
1098                 // wait until the whole packet can be read
1099                 this.packetLength = packetLength;
1100                 return;
1101             }
1102         }
1103 
1104         // Reset the state of this class so we can get the length of the next packet. We assume the entire packet will
1105         // be consumed by the SSLEngine.
1106         this.packetLength = 0;
1107         try {
1108             int bytesConsumed = unwrap(ctx, in, in.readerIndex(), packetLength);
1109             assert bytesConsumed == packetLength || engine.isInboundDone() :
1110                     "we feed the SSLEngine a packets worth of data: " + packetLength + " but it only consumed: " +
1111                             bytesConsumed;
1112             in.skipBytes(bytesConsumed);
1113         } catch (Throwable cause) {
1114             handleUnwrapThrowable(ctx, cause);
1115         }
1116     }
1117 
1118     private void decodeNonJdkCompatible(ChannelHandlerContext ctx, ByteBuf in) {
1119         try {
1120             in.skipBytes(unwrap(ctx, in, in.readerIndex(), in.readableBytes()));
1121         } catch (Throwable cause) {
1122             handleUnwrapThrowable(ctx, cause);
1123         }
1124     }
1125 
1126     private void handleUnwrapThrowable(ChannelHandlerContext ctx, Throwable cause) {
1127         try {
1128             // We need to flush one time as there may be an alert that we should send to the remote peer because
1129             // of the SSLException reported here.
1130             wrapAndFlush(ctx);
1131         } catch (SSLException ex) {
1132             logger.debug("SSLException during trying to call SSLEngine.wrap(...)" +
1133                     " because of an previous SSLException, ignoring...", ex);
1134         } finally {
1135             setHandshakeFailure(ctx, cause);
1136         }
1137         PlatformDependent.throwException(cause);
1138     }
1139 
1140     @Override
1141     protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) throws SSLException {
1142         if (jdkCompatibilityMode) {
1143             decodeJdkCompatible(ctx, in);
1144         } else {
1145             decodeNonJdkCompatible(ctx, in);
1146         }
1147     }
1148 
1149     @Override
1150     public void channelReadComplete(ChannelHandlerContext ctx) throws Exception {
1151         // Discard bytes of the cumulation buffer if needed.
1152         discardSomeReadBytes();
1153 
1154         flushIfNeeded(ctx);
1155         readIfNeeded(ctx);
1156 
1157         firedChannelRead = false;
1158         ctx.fireChannelReadComplete();
1159     }
1160 
1161     private void readIfNeeded(ChannelHandlerContext ctx) {
1162         // If handshake is not finished yet, we need more data.
1163         if (!ctx.channel().config().isAutoRead() && (!firedChannelRead || !handshakePromise.isDone())) {
1164             // No auto-read used and no message passed through the ChannelPipeline or the handshake was not complete
1165             // yet, which means we need to trigger the read to ensure we not encounter any stalls.
1166             ctx.read();
1167         }
1168     }
1169 
1170     private void flushIfNeeded(ChannelHandlerContext ctx) {
1171         if (needsFlush) {
1172             forceFlush(ctx);
1173         }
1174     }
1175 
1176     /**
1177      * Calls {@link SSLEngine#unwrap(ByteBuffer, ByteBuffer)} with an empty buffer to handle handshakes, etc.
1178      */
1179     private void unwrapNonAppData(ChannelHandlerContext ctx) throws SSLException {
1180         unwrap(ctx, Unpooled.EMPTY_BUFFER, 0, 0);
1181     }
1182 
1183     /**
1184      * Unwraps inbound SSL records.
1185      */
1186     private int unwrap(
1187             ChannelHandlerContext ctx, ByteBuf packet, int offset, int length) throws SSLException {
1188         final int originalLength = length;
1189         boolean wrapLater = false;
1190         boolean notifyClosure = false;
1191         int overflowReadableBytes = -1;
1192         ByteBuf decodeOut = allocate(ctx, length);
1193         try {
1194             // Only continue to loop if the handler was not removed in the meantime.
1195             // See https://github.com/netty/netty/issues/5860
1196             unwrapLoop: while (!ctx.isRemoved()) {
1197                 final SSLEngineResult result = engineType.unwrap(this, packet, offset, length, decodeOut);
1198                 final Status status = result.getStatus();
1199                 final HandshakeStatus handshakeStatus = result.getHandshakeStatus();
1200                 final int produced = result.bytesProduced();
1201                 final int consumed = result.bytesConsumed();
1202 
1203                 // Update indexes for the next iteration
1204                 offset += consumed;
1205                 length -= consumed;
1206 
1207                 switch (status) {
1208                 case BUFFER_OVERFLOW:
1209                     final int readableBytes = decodeOut.readableBytes();
1210                     final int previousOverflowReadableBytes = overflowReadableBytes;
1211                     overflowReadableBytes = readableBytes;
1212                     int bufferSize = engine.getSession().getApplicationBufferSize() - readableBytes;
1213                     if (readableBytes > 0) {
1214                         firedChannelRead = true;
1215                         ctx.fireChannelRead(decodeOut);
1216 
1217                         // This buffer was handled, null it out.
1218                         decodeOut = null;
1219                         if (bufferSize <= 0) {
1220                             // It may happen that readableBytes >= engine.getSession().getApplicationBufferSize()
1221                             // while there is still more to unwrap, in this case we will just allocate a new buffer
1222                             // with the capacity of engine.getSession().getApplicationBufferSize() and call unwrap
1223                             // again.
1224                             bufferSize = engine.getSession().getApplicationBufferSize();
1225                         }
1226                     } else {
1227                         // This buffer was handled, null it out.
1228                         decodeOut.release();
1229                         decodeOut = null;
1230                     }
1231                     if (readableBytes == 0 && previousOverflowReadableBytes == 0) {
1232                         // If there is two consecutive loops where we overflow and are not able to consume any data,
1233                         // assume the amount of data exceeds the maximum amount for the engine and bail
1234                         throw new IllegalStateException("Two consecutive overflows but no content was consumed. " +
1235                                  SSLSession.class.getSimpleName() + " getApplicationBufferSize: " +
1236                                  engine.getSession().getApplicationBufferSize() + " maybe too small.");
1237                     }
1238                     // Allocate a new buffer which can hold all the rest data and loop again.
1239                     // TODO: We may want to reconsider how we calculate the length here as we may
1240                     // have more then one ssl message to decode.
1241                     decodeOut = allocate(ctx, engineType.calculatePendingData(this, bufferSize));
1242                     continue;
1243                 case CLOSED:
1244                     // notify about the CLOSED state of the SSLEngine. See #137
1245                     notifyClosure = true;
1246                     overflowReadableBytes = -1;
1247                     break;
1248                 default:
1249                     overflowReadableBytes = -1;
1250                     break;
1251                 }
1252 
1253                 switch (handshakeStatus) {
1254                     case NEED_UNWRAP:
1255                         break;
1256                     case NEED_WRAP:
1257                         // If the wrap operation transitions the status to NOT_HANDSHAKING and there is no more data to
1258                         // unwrap then the next call to unwrap will not produce any data. We can avoid the potentially
1259                         // costly unwrap operation and break out of the loop.
1260                         if (wrapNonAppData(ctx, true) && length == 0) {
1261                             break unwrapLoop;
1262                         }
1263                         break;
1264                     case NEED_TASK:
1265                         runDelegatedTasks();
1266                         break;
1267                     case FINISHED:
1268                         setHandshakeSuccess();
1269                         wrapLater = true;
1270 
1271                         // We 'break' here and NOT 'continue' as android API version 21 has a bug where they consume
1272                         // data from the buffer but NOT correctly set the SSLEngineResult.bytesConsumed().
1273                         // Because of this it will raise an exception on the next iteration of the for loop on android
1274                         // API version 21. Just doing a break will work here as produced and consumed will both be 0
1275                         // and so we break out of the complete for (;;) loop and so call decode(...) again later on.
1276                         // On other platforms this will have no negative effect as we will just continue with the
1277                         // for (;;) loop if something was either consumed or produced.
1278                         //
1279                         // See:
1280                         //  - https://github.com/netty/netty/issues/4116
1281                         //  - https://code.google.com/p/android/issues/detail?id=198639&thanks=198639&ts=1452501203
1282                         break;
1283                     case NOT_HANDSHAKING:
1284                         if (setHandshakeSuccessIfStillHandshaking()) {
1285                             wrapLater = true;
1286                             continue;
1287                         }
1288                         if (flushedBeforeHandshake) {
1289                             // We need to call wrap(...) in case there was a flush done before the handshake completed.
1290                             //
1291                             // See https://github.com/netty/netty/pull/2437
1292                             flushedBeforeHandshake = false;
1293                             wrapLater = true;
1294                         }
1295                         // If we are not handshaking and there is no more data to unwrap then the next call to unwrap
1296                         // will not produce any data. We can avoid the potentially costly unwrap operation and break
1297                         // out of the loop.
1298                         if (length == 0) {
1299                             break unwrapLoop;
1300                         }
1301                         break;
1302                     default:
1303                         throw new IllegalStateException("unknown handshake status: " + handshakeStatus);
1304                 }
1305 
1306                 if (status == Status.BUFFER_UNDERFLOW || consumed == 0 && produced == 0) {
1307                     if (handshakeStatus == HandshakeStatus.NEED_UNWRAP) {
1308                         // The underlying engine is starving so we need to feed it with more data.
1309                         // See https://github.com/netty/netty/pull/5039
1310                         readIfNeeded(ctx);
1311                     }
1312 
1313                     break;
1314                 }
1315             }
1316 
1317             if (wrapLater) {
1318                 wrap(ctx, true);
1319             }
1320 
1321             if (notifyClosure) {
1322                 notifyClosePromise(null);
1323             }
1324         } finally {
1325             if (decodeOut != null) {
1326                 if (decodeOut.isReadable()) {
1327                     firedChannelRead = true;
1328 
1329                     ctx.fireChannelRead(decodeOut);
1330                 } else {
1331                     decodeOut.release();
1332                 }
1333             }
1334         }
1335         return originalLength - length;
1336     }
1337 
1338     private static ByteBuffer toByteBuffer(ByteBuf out, int index, int len) {
1339         return out.nioBufferCount() == 1 ? out.internalNioBuffer(index, len) :
1340                 out.nioBuffer(index, len);
1341     }
1342 
1343     /**
1344      * Fetches all delegated tasks from the {@link SSLEngine} and runs them via the {@link #delegatedTaskExecutor}.
1345      * If the {@link #delegatedTaskExecutor} is {@link ImmediateExecutor}, just call {@link Runnable#run()} directly
1346      * instead of using {@link Executor#execute(Runnable)}.  Otherwise, run the tasks via
1347      * the {@link #delegatedTaskExecutor} and wait until the tasks are finished.
1348      */
1349     private void runDelegatedTasks() {
1350         if (delegatedTaskExecutor == ImmediateExecutor.INSTANCE) {
1351             for (;;) {
1352                 Runnable task = engine.getDelegatedTask();
1353                 if (task == null) {
1354                     break;
1355                 }
1356 
1357                 task.run();
1358             }
1359         } else {
1360             final List<Runnable> tasks = new ArrayList<Runnable>(2);
1361             for (;;) {
1362                 final Runnable task = engine.getDelegatedTask();
1363                 if (task == null) {
1364                     break;
1365                 }
1366 
1367                 tasks.add(task);
1368             }
1369 
1370             if (tasks.isEmpty()) {
1371                 return;
1372             }
1373 
1374             final CountDownLatch latch = new CountDownLatch(1);
1375             delegatedTaskExecutor.execute(new Runnable() {
1376                 @Override
1377                 public void run() {
1378                     try {
1379                         for (Runnable task: tasks) {
1380                             task.run();
1381                         }
1382                     } catch (Exception e) {
1383                         ctx.fireExceptionCaught(e);
1384                     } finally {
1385                         latch.countDown();
1386                     }
1387                 }
1388             });
1389 
1390             boolean interrupted = false;
1391             while (latch.getCount() != 0) {
1392                 try {
1393                     latch.await();
1394                 } catch (InterruptedException e) {
1395                     // Interrupt later.
1396                     interrupted = true;
1397                 }
1398             }
1399 
1400             if (interrupted) {
1401                 Thread.currentThread().interrupt();
1402             }
1403         }
1404     }
1405 
1406     /**
1407      * Works around some Android {@link SSLEngine} implementations that skip {@link HandshakeStatus#FINISHED} and
1408      * go straight into {@link HandshakeStatus#NOT_HANDSHAKING} when handshake is finished.
1409      *
1410      * @return {@code true} if and only if the workaround has been applied and thus {@link #handshakeFuture} has been
1411      *         marked as success by this method
1412      */
1413     private boolean setHandshakeSuccessIfStillHandshaking() {
1414         if (!handshakePromise.isDone()) {
1415             setHandshakeSuccess();
1416             return true;
1417         }
1418         return false;
1419     }
1420 
1421     /**
1422      * Notify all the handshake futures about the successfully handshake
1423      */
1424     private void setHandshakeSuccess() {
1425         handshakePromise.trySuccess(ctx.channel());
1426 
1427         if (logger.isDebugEnabled()) {
1428             logger.debug("{} HANDSHAKEN: {}", ctx.channel(), engine.getSession().getCipherSuite());
1429         }
1430         ctx.fireUserEventTriggered(SslHandshakeCompletionEvent.SUCCESS);
1431 
1432         if (readDuringHandshake && !ctx.channel().config().isAutoRead()) {
1433             readDuringHandshake = false;
1434             ctx.read();
1435         }
1436     }
1437 
1438     /**
1439      * Notify all the handshake futures about the failure during the handshake.
1440      */
1441     private void setHandshakeFailure(ChannelHandlerContext ctx, Throwable cause) {
1442         setHandshakeFailure(ctx, cause, true, true);
1443     }
1444 
1445     /**
1446      * Notify all the handshake futures about the failure during the handshake.
1447      */
1448     private void setHandshakeFailure(ChannelHandlerContext ctx, Throwable cause, boolean closeInbound, boolean notify) {
1449         try {
1450             // Release all resources such as internal buffers that SSLEngine
1451             // is managing.
1452             engine.closeOutbound();
1453 
1454             if (closeInbound) {
1455                 try {
1456                     engine.closeInbound();
1457                 } catch (SSLException e) {
1458                     if (logger.isDebugEnabled()) {
1459                         // only log in debug mode as it most likely harmless and latest chrome still trigger
1460                         // this all the time.
1461                         //
1462                         // See https://github.com/netty/netty/issues/1340
1463                         String msg = e.getMessage();
1464                         if (msg == null || !msg.contains("possible truncation attack")) {
1465                             logger.debug("{} SSLEngine.closeInbound() raised an exception.", ctx.channel(), e);
1466                         }
1467                     }
1468                 }
1469             }
1470             notifyHandshakeFailure(cause, notify);
1471         } finally {
1472             if (pendingUnencryptedWrites != null) {
1473                 // Ensure we remove and fail all pending writes in all cases and so release memory quickly.
1474                 pendingUnencryptedWrites.removeAndFailAll(cause);
1475             }
1476         }
1477     }
1478 
1479     private void notifyHandshakeFailure(Throwable cause, boolean notify) {
1480         if (handshakePromise.tryFailure(cause)) {
1481             SslUtils.notifyHandshakeFailure(ctx, cause, notify);
1482         }
1483     }
1484 
1485     private void notifyClosePromise(Throwable cause) {
1486         if (cause == null) {
1487             if (sslClosePromise.trySuccess(ctx.channel())) {
1488                 ctx.fireUserEventTriggered(SslCloseCompletionEvent.SUCCESS);
1489             }
1490         } else {
1491             if (sslClosePromise.tryFailure(cause)) {
1492                 ctx.fireUserEventTriggered(new SslCloseCompletionEvent(cause));
1493             }
1494         }
1495     }
1496 
1497     private void closeOutboundAndChannel(
1498             final ChannelHandlerContext ctx, final ChannelPromise promise, boolean disconnect) throws Exception {
1499         if (!ctx.channel().isActive()) {
1500             if (disconnect) {
1501                 ctx.disconnect(promise);
1502             } else {
1503                 ctx.close(promise);
1504             }
1505             return;
1506         }
1507 
1508         outboundClosed = true;
1509         engine.closeOutbound();
1510 
1511         ChannelPromise closeNotifyPromise = ctx.newPromise();
1512         try {
1513             flush(ctx, closeNotifyPromise);
1514         } finally {
1515             // It's important that we do not pass the original ChannelPromise to safeClose(...) as when flush(....)
1516             // throws an Exception it will be propagated to the AbstractChannelHandlerContext which will try
1517             // to fail the promise because of this. This will then fail as it was already completed by safeClose(...).
1518             // We create a new ChannelPromise and try to notify the original ChannelPromise
1519             // once it is complete. If we fail to do so we just ignore it as in this case it was failed already
1520             // because of a propagated Exception.
1521             //
1522             // See https://github.com/netty/netty/issues/5931
1523             safeClose(ctx, closeNotifyPromise, ctx.newPromise().addListener(
1524                     new ChannelPromiseNotifier(false, promise)));
1525         }
1526     }
1527 
1528     private void flush(ChannelHandlerContext ctx, ChannelPromise promise) throws Exception {
1529         if (pendingUnencryptedWrites != null) {
1530             pendingUnencryptedWrites.add(Unpooled.EMPTY_BUFFER, promise);
1531         } else {
1532             promise.setFailure(newPendingWritesNullException());
1533         }
1534         flush(ctx);
1535     }
1536 
1537     @Override
1538     public void handlerAdded(final ChannelHandlerContext ctx) throws Exception {
1539         this.ctx = ctx;
1540         pendingUnencryptedWrites = new PendingWriteQueue(ctx);
1541 
1542         if (ctx.channel().isActive()) {
1543             startHandshakeProcessing();
1544         }
1545     }
1546 
1547     private void startHandshakeProcessing() {
1548         handshakeStarted = true;
1549         if (engine.getUseClientMode()) {
1550             // Begin the initial handshake.
1551             // channelActive() event has been fired already, which means this.channelActive() will
1552             // not be invoked. We have to initialize here instead.
1553             handshake(null);
1554         } else {
1555             applyHandshakeTimeout(null);
1556         }
1557     }
1558 
1559     /**
1560      * Performs TLS renegotiation.
1561      */
1562     public Future<Channel> renegotiate() {
1563         ChannelHandlerContext ctx = this.ctx;
1564         if (ctx == null) {
1565             throw new IllegalStateException();
1566         }
1567 
1568         return renegotiate(ctx.executor().<Channel>newPromise());
1569     }
1570 
1571     /**
1572      * Performs TLS renegotiation.
1573      */
1574     public Future<Channel> renegotiate(final Promise<Channel> promise) {
1575         if (promise == null) {
1576             throw new NullPointerException("promise");
1577         }
1578 
1579         ChannelHandlerContext ctx = this.ctx;
1580         if (ctx == null) {
1581             throw new IllegalStateException();
1582         }
1583 
1584         EventExecutor executor = ctx.executor();
1585         if (!executor.inEventLoop()) {
1586             executor.execute(new Runnable() {
1587                 @Override
1588                 public void run() {
1589                     handshake(promise);
1590                 }
1591             });
1592             return promise;
1593         }
1594 
1595         handshake(promise);
1596         return promise;
1597     }
1598 
1599     /**
1600      * Performs TLS (re)negotiation.
1601      *
1602      * @param newHandshakePromise if {@code null}, use the existing {@link #handshakePromise},
1603      *                            assuming that the current negotiation has not been finished.
1604      *                            Currently, {@code null} is expected only for the initial handshake.
1605      */
1606     private void handshake(final Promise<Channel> newHandshakePromise) {
1607         final Promise<Channel> p;
1608         if (newHandshakePromise != null) {
1609             final Promise<Channel> oldHandshakePromise = handshakePromise;
1610             if (!oldHandshakePromise.isDone()) {
1611                 // There's no need to handshake because handshake is in progress already.
1612                 // Merge the new promise into the old one.
1613                 oldHandshakePromise.addListener(new FutureListener<Channel>() {
1614                     @Override
1615                     public void operationComplete(Future<Channel> future) throws Exception {
1616                         if (future.isSuccess()) {
1617                             newHandshakePromise.setSuccess(future.getNow());
1618                         } else {
1619                             newHandshakePromise.setFailure(future.cause());
1620                         }
1621                     }
1622                 });
1623                 return;
1624             }
1625 
1626             handshakePromise = p = newHandshakePromise;
1627         } else if (engine.getHandshakeStatus() != HandshakeStatus.NOT_HANDSHAKING) {
1628             // Not all SSLEngine implementations support calling beginHandshake multiple times while a handshake
1629             // is in progress. See https://github.com/netty/netty/issues/4718.
1630             return;
1631         } else {
1632             // Forced to reuse the old handshake.
1633             p = handshakePromise;
1634             assert !p.isDone();
1635         }
1636 
1637         // Begin handshake.
1638         final ChannelHandlerContext ctx = this.ctx;
1639         try {
1640             engine.beginHandshake();
1641             wrapNonAppData(ctx, false);
1642         } catch (Throwable e) {
1643             setHandshakeFailure(ctx, e);
1644         } finally {
1645            forceFlush(ctx);
1646         }
1647         applyHandshakeTimeout(p);
1648     }
1649 
1650     private void applyHandshakeTimeout(Promise<Channel> p) {
1651         final Promise<Channel> promise = p == null ? handshakePromise : p;
1652         // Set timeout if necessary.
1653         final long handshakeTimeoutMillis = this.handshakeTimeoutMillis;
1654         if (handshakeTimeoutMillis <= 0 || promise.isDone()) {
1655             return;
1656         }
1657 
1658         final ScheduledFuture<?> timeoutFuture = ctx.executor().schedule(new Runnable() {
1659             @Override
1660             public void run() {
1661                 if (promise.isDone()) {
1662                     return;
1663                 }
1664                 notifyHandshakeFailure(HANDSHAKE_TIMED_OUT, true);
1665             }
1666         }, handshakeTimeoutMillis, TimeUnit.MILLISECONDS);
1667 
1668         // Cancel the handshake timeout when handshake is finished.
1669         promise.addListener(new FutureListener<Channel>() {
1670             @Override
1671             public void operationComplete(Future<Channel> f) throws Exception {
1672                 timeoutFuture.cancel(false);
1673             }
1674         });
1675     }
1676 
1677     private void forceFlush(ChannelHandlerContext ctx) {
1678         needsFlush = false;
1679         ctx.flush();
1680     }
1681 
1682     /**
1683      * Issues an initial TLS handshake once connected when used in client-mode
1684      */
1685     @Override
1686     public void channelActive(final ChannelHandlerContext ctx) throws Exception {
1687         if (!startTls) {
1688             startHandshakeProcessing();
1689         }
1690         ctx.fireChannelActive();
1691     }
1692 
1693     private void safeClose(
1694             final ChannelHandlerContext ctx, final ChannelFuture flushFuture,
1695             final ChannelPromise promise) {
1696         if (!ctx.channel().isActive()) {
1697             ctx.close(promise);
1698             return;
1699         }
1700 
1701         final ScheduledFuture<?> timeoutFuture;
1702         if (!flushFuture.isDone()) {
1703             long closeNotifyTimeout = closeNotifyFlushTimeoutMillis;
1704             if (closeNotifyTimeout > 0) {
1705                 // Force-close the connection if close_notify is not fully sent in time.
1706                 timeoutFuture = ctx.executor().schedule(new Runnable() {
1707                     @Override
1708                     public void run() {
1709                         // May be done in the meantime as cancel(...) is only best effort.
1710                         if (!flushFuture.isDone()) {
1711                             logger.warn("{} Last write attempt timed out; force-closing the connection.",
1712                                     ctx.channel());
1713                             addCloseListener(ctx.close(ctx.newPromise()), promise);
1714                         }
1715                     }
1716                 }, closeNotifyTimeout, TimeUnit.MILLISECONDS);
1717             } else {
1718                 timeoutFuture = null;
1719             }
1720         } else {
1721             timeoutFuture = null;
1722         }
1723 
1724         // Close the connection if close_notify is sent in time.
1725         flushFuture.addListener(new ChannelFutureListener() {
1726             @Override
1727             public void operationComplete(ChannelFuture f)
1728                     throws Exception {
1729                 if (timeoutFuture != null) {
1730                     timeoutFuture.cancel(false);
1731                 }
1732                 final long closeNotifyReadTimeout = closeNotifyReadTimeoutMillis;
1733                 if (closeNotifyReadTimeout <= 0) {
1734                     // Trigger the close in all cases to make sure the promise is notified
1735                     // See https://github.com/netty/netty/issues/2358
1736                     addCloseListener(ctx.close(ctx.newPromise()), promise);
1737                 } else {
1738                     final ScheduledFuture<?> closeNotifyReadTimeoutFuture;
1739 
1740                     if (!sslClosePromise.isDone()) {
1741                         closeNotifyReadTimeoutFuture = ctx.executor().schedule(new Runnable() {
1742                             @Override
1743                             public void run() {
1744                                 if (!sslClosePromise.isDone()) {
1745                                     logger.debug(
1746                                             "{} did not receive close_notify in {}ms; force-closing the connection.",
1747                                             ctx.channel(), closeNotifyReadTimeout);
1748 
1749                                     // Do the close now...
1750                                     addCloseListener(ctx.close(ctx.newPromise()), promise);
1751                                 }
1752                             }
1753                         }, closeNotifyReadTimeout, TimeUnit.MILLISECONDS);
1754                     } else {
1755                         closeNotifyReadTimeoutFuture = null;
1756                     }
1757 
1758                     // Do the close once the we received the close_notify.
1759                     sslClosePromise.addListener(new FutureListener<Channel>() {
1760                         @Override
1761                         public void operationComplete(Future<Channel> future) throws Exception {
1762                             if (closeNotifyReadTimeoutFuture != null) {
1763                                 closeNotifyReadTimeoutFuture.cancel(false);
1764                             }
1765                             addCloseListener(ctx.close(ctx.newPromise()), promise);
1766                         }
1767                     });
1768                 }
1769             }
1770         });
1771     }
1772 
1773     private static void addCloseListener(ChannelFuture future, ChannelPromise promise) {
1774         // We notify the promise in the ChannelPromiseNotifier as there is a "race" where the close(...) call
1775         // by the timeoutFuture and the close call in the flushFuture listener will be called. Because of
1776         // this we need to use trySuccess() and tryFailure(...) as otherwise we can cause an
1777         // IllegalStateException.
1778         // Also we not want to log if the notification happens as this is expected in some cases.
1779         // See https://github.com/netty/netty/issues/5598
1780         future.addListener(new ChannelPromiseNotifier(false, promise));
1781     }
1782 
1783     /**
1784      * Always prefer a direct buffer when it's pooled, so that we reduce the number of memory copies
1785      * in {@link OpenSslEngine}.
1786      */
1787     private ByteBuf allocate(ChannelHandlerContext ctx, int capacity) {
1788         ByteBufAllocator alloc = ctx.alloc();
1789         if (engineType.wantsDirectBuffer) {
1790             return alloc.directBuffer(capacity);
1791         } else {
1792             return alloc.buffer(capacity);
1793         }
1794     }
1795 
1796     /**
1797      * Allocates an outbound network buffer for {@link SSLEngine#wrap(ByteBuffer, ByteBuffer)} which can encrypt
1798      * the specified amount of pending bytes.
1799      */
1800     private ByteBuf allocateOutNetBuf(ChannelHandlerContext ctx, int pendingBytes, int numComponents) {
1801         return allocate(ctx, engineType.calculateWrapBufferCapacity(this, pendingBytes, numComponents));
1802     }
1803 
1804     private final class LazyChannelPromise extends DefaultPromise<Channel> {
1805 
1806         @Override
1807         protected EventExecutor executor() {
1808             if (ctx == null) {
1809                 throw new IllegalStateException();
1810             }
1811             return ctx.executor();
1812         }
1813 
1814         @Override
1815         protected void checkDeadLock() {
1816             if (ctx == null) {
1817                 // If ctx is null the handlerAdded(...) callback was not called, in this case the checkDeadLock()
1818                 // method was called from another Thread then the one that is used by ctx.executor(). We need to
1819                 // guard against this as a user can see a race if handshakeFuture().sync() is called but the
1820                 // handlerAdded(..) method was not yet as it is called from the EventExecutor of the
1821                 // ChannelHandlerContext. If we not guard against this super.checkDeadLock() would cause an
1822                 // IllegalStateException when trying to call executor().
1823                 return;
1824             }
1825             super.checkDeadLock();
1826         }
1827     }
1828 }