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 org.jboss.netty.handler.ssl;
17  
18  import org.jboss.netty.buffer.ChannelBuffer;
19  import org.jboss.netty.buffer.ChannelBufferFactory;
20  import org.jboss.netty.buffer.ChannelBuffers;
21  import org.jboss.netty.channel.Channel;
22  import org.jboss.netty.channel.ChannelDownstreamHandler;
23  import org.jboss.netty.channel.ChannelEvent;
24  import org.jboss.netty.channel.ChannelFuture;
25  import org.jboss.netty.channel.ChannelFutureListener;
26  import org.jboss.netty.channel.ChannelHandlerContext;
27  import org.jboss.netty.channel.ChannelPipeline;
28  import org.jboss.netty.channel.ChannelStateEvent;
29  import org.jboss.netty.channel.Channels;
30  import org.jboss.netty.channel.DefaultChannelFuture;
31  import org.jboss.netty.channel.DownstreamMessageEvent;
32  import org.jboss.netty.channel.ExceptionEvent;
33  import org.jboss.netty.channel.MessageEvent;
34  import org.jboss.netty.handler.codec.frame.FrameDecoder;
35  import org.jboss.netty.logging.InternalLogger;
36  import org.jboss.netty.logging.InternalLoggerFactory;
37  import org.jboss.netty.util.Timeout;
38  import org.jboss.netty.util.Timer;
39  import org.jboss.netty.util.TimerTask;
40  import org.jboss.netty.util.internal.DetectionUtil;
41  import org.jboss.netty.util.internal.NonReentrantLock;
42  
43  import javax.net.ssl.SSLEngine;
44  import javax.net.ssl.SSLEngineResult;
45  import javax.net.ssl.SSLEngineResult.HandshakeStatus;
46  import javax.net.ssl.SSLEngineResult.Status;
47  import javax.net.ssl.SSLException;
48  import java.io.IOException;
49  import java.nio.ByteBuffer;
50  import java.nio.channels.ClosedChannelException;
51  import java.nio.channels.DatagramChannel;
52  import java.nio.channels.SocketChannel;
53  import java.util.ArrayList;
54  import java.util.LinkedList;
55  import java.util.List;
56  import java.util.Queue;
57  import java.util.concurrent.ConcurrentLinkedQueue;
58  import java.util.concurrent.TimeUnit;
59  import java.util.concurrent.atomic.AtomicIntegerFieldUpdater;
60  import java.util.regex.Pattern;
61  
62  import static org.jboss.netty.channel.Channels.*;
63  
64  /**
65   * Adds <a href="http://en.wikipedia.org/wiki/Transport_Layer_Security">SSL
66   * &middot; TLS</a> and StartTLS support to a {@link Channel}.  Please refer
67   * to the <strong>"SecureChat"</strong> example in the distribution or the web
68   * site for the detailed usage.
69   *
70   * <h3>Beginning the handshake</h3>
71   * <p>
72   * You must make sure not to write a message while the
73   * {@linkplain #handshake() handshake} is in progress unless you are
74   * renegotiating.  You will be notified by the {@link ChannelFuture} which is
75   * returned by the {@link #handshake()} method when the handshake
76   * process succeeds or fails.
77   *
78   * <h3>Handshake</h3>
79   * <p>
80   * If {@link #isIssueHandshake()} is {@code false}
81   * (default) you will need to take care of calling {@link #handshake()} by your own. In most
82   * situations were {@link SslHandler} is used in 'client mode' you want to issue a handshake once
83   * the connection was established. if {@link #setIssueHandshake(boolean)} is set to {@code true}
84   * you don't need to worry about this as the {@link SslHandler} will take care of it.
85   * <p>
86   *
87   * <h3>Renegotiation</h3>
88   * <p>
89   * If {@link #isEnableRenegotiation() enableRenegotiation} is {@code true}
90   * (default) and the initial handshake has been done successfully, you can call
91   * {@link #handshake()} to trigger the renegotiation.
92   * <p>
93   * If {@link #isEnableRenegotiation() enableRenegotiation} is {@code false},
94   * an attempt to trigger renegotiation will result in the connection closure.
95   * <p>
96   * Please note that TLS renegotiation had a security issue before.  If your
97   * runtime environment did not fix it, please make sure to disable TLS
98   * renegotiation by calling {@link #setEnableRenegotiation(boolean)} with
99   * {@code false}.  For more information, please refer to the following documents:
100  * <ul>
101  *   <li><a href="http://cve.mitre.org/cgi-bin/cvename.cgi?name=CVE-2009-3555">CVE-2009-3555</a></li>
102  *   <li><a href="http://www.ietf.org/rfc/rfc5746.txt">RFC5746</a></li>
103  *   <li><a href="http://www.oracle.com/technetwork/java/javase/documentation/tlsreadme2-176330.html">Phased
104  *       Approach to Fixing the TLS Renegotiation Issue</a></li>
105  * </ul>
106  *
107  * <h3>Closing the session</h3>
108  * <p>
109  * To close the SSL session, the {@link #close()} method should be
110  * called to send the {@code close_notify} message to the remote peer.  One
111  * exception is when you close the {@link Channel} - {@link SslHandler}
112  * intercepts the close request and send the {@code close_notify} message
113  * before the channel closure automatically.  Once the SSL session is closed,
114  * it is not reusable, and consequently you should create a new
115  * {@link SslHandler} with a new {@link SSLEngine} as explained in the
116  * following section.
117  *
118  * <h3>Restarting the session</h3>
119  * <p>
120  * To restart the SSL session, you must remove the existing closed
121  * {@link SslHandler} from the {@link ChannelPipeline}, insert a new
122  * {@link SslHandler} with a new {@link SSLEngine} into the pipeline,
123  * and start the handshake process as described in the first section.
124  *
125  * <h3>Implementing StartTLS</h3>
126  * <p>
127  * <a href="http://en.wikipedia.org/wiki/STARTTLS">StartTLS</a> is the
128  * communication pattern that secures the wire in the middle of the plaintext
129  * connection.  Please note that it is different from SSL &middot; TLS, that
130  * secures the wire from the beginning of the connection.  Typically, StartTLS
131  * is composed of three steps:
132  * <ol>
133  * <li>Client sends a StartTLS request to server.</li>
134  * <li>Server sends a StartTLS response to client.</li>
135  * <li>Client begins SSL handshake.</li>
136  * </ol>
137  * If you implement a server, you need to:
138  * <ol>
139  * <li>create a new {@link SslHandler} instance with {@code startTls} flag set
140  *     to {@code true},</li>
141  * <li>insert the {@link SslHandler} to the {@link ChannelPipeline}, and</li>
142  * <li>write a StartTLS response.</li>
143  * </ol>
144  * Please note that you must insert {@link SslHandler} <em>before</em> sending
145  * the StartTLS response.  Otherwise the client can send begin SSL handshake
146  * before {@link SslHandler} is inserted to the {@link ChannelPipeline}, causing
147  * data corruption.
148  * <p>
149  * The client-side implementation is much simpler.
150  * <ol>
151  * <li>Write a StartTLS request,</li>
152  * <li>wait for the StartTLS response,</li>
153  * <li>create a new {@link SslHandler} instance with {@code startTls} flag set
154  *     to {@code false},</li>
155  * <li>insert the {@link SslHandler} to the {@link ChannelPipeline}, and</li>
156  * <li>Initiate SSL handshake by calling {@link SslHandler#handshake()}.</li>
157  * </ol>
158  *
159  * <h3>Known issues</h3>
160  * <p>
161  * Because of a known issue with the current implementation of the SslEngine that comes
162  * with Java it may be possible that you see blocked IO-Threads while a full GC is done.
163  * <p>
164  * So if you are affected you can workaround this problem by adjust the cache settings
165  * like shown below:
166  *
167  * <pre>
168  *     SslContext context = ...;
169  *     context.getServerSessionContext().setSessionCacheSize(someSaneSize);
170  *     context.getServerSessionContext().setSessionTime(someSameTimeout);
171  * </pre>
172  * <p>
173  * What values to use here depends on the nature of your application and should be set
174  * based on monitoring and debugging of it.
175  * For more details see
176  * <a href="https://github.com/netty/netty/issues/832">#832</a> in our issue tracker.
177  * @apiviz.landmark
178  * @apiviz.uses org.jboss.netty.handler.ssl.SslBufferPool
179  */
180 public class SslHandler extends FrameDecoder
181                         implements ChannelDownstreamHandler {
182 
183     private static final InternalLogger logger = InternalLoggerFactory.getInstance(SslHandler.class);
184 
185     private static final ByteBuffer EMPTY_BUFFER = ByteBuffer.allocate(0);
186 
187     private static final Pattern IGNORABLE_CLASS_IN_STACK = Pattern.compile(
188             "^.*(?:Socket|Datagram|Sctp|Udt)Channel.*$");
189     private static final Pattern IGNORABLE_ERROR_MESSAGE = Pattern.compile(
190             "^.*(?:connection.*(?:reset|closed|abort|broken)|broken.*pipe).*$", Pattern.CASE_INSENSITIVE);
191 
192     private static SslBufferPool defaultBufferPool;
193 
194     /**
195      * Returns the default {@link SslBufferPool} used when no pool is
196      * specified in the constructor.
197      */
198     public static synchronized SslBufferPool getDefaultBufferPool() {
199         if (defaultBufferPool == null) {
200             defaultBufferPool = new SslBufferPool();
201         }
202         return defaultBufferPool;
203     }
204 
205     private volatile ChannelHandlerContext ctx;
206     private final SSLEngine engine;
207     private final SslBufferPool bufferPool;
208     private final boolean startTls;
209 
210     private volatile boolean enableRenegotiation = true;
211 
212     final Object handshakeLock = new Object();
213     private boolean handshaking;
214     private volatile boolean handshaken;
215     private volatile ChannelFuture handshakeFuture;
216 
217     @SuppressWarnings("UnusedDeclaration")
218     private volatile int sentFirstMessage;
219     @SuppressWarnings("UnusedDeclaration")
220     private volatile int sentCloseNotify;
221     @SuppressWarnings("UnusedDeclaration")
222     private volatile int closedOutboundAndChannel;
223 
224     private static final AtomicIntegerFieldUpdater<SslHandler> SENT_FIRST_MESSAGE_UPDATER =
225             AtomicIntegerFieldUpdater.newUpdater(SslHandler.class, "sentFirstMessage");
226     private static final AtomicIntegerFieldUpdater<SslHandler> SENT_CLOSE_NOTIFY_UPDATER =
227             AtomicIntegerFieldUpdater.newUpdater(SslHandler.class, "sentCloseNotify");
228     private static final AtomicIntegerFieldUpdater<SslHandler> CLOSED_OUTBOUND_AND_CHANNEL_UPDATER =
229             AtomicIntegerFieldUpdater.newUpdater(SslHandler.class, "closedOutboundAndChannel");
230 
231     int ignoreClosedChannelException;
232     final Object ignoreClosedChannelExceptionLock = new Object();
233     private final Queue<PendingWrite> pendingUnencryptedWrites = new LinkedList<PendingWrite>();
234     private final NonReentrantLock pendingUnencryptedWritesLock = new NonReentrantLock();
235     private final Queue<MessageEvent> pendingEncryptedWrites = new ConcurrentLinkedQueue<MessageEvent>();
236     private final NonReentrantLock pendingEncryptedWritesLock = new NonReentrantLock();
237 
238     private volatile boolean issueHandshake;
239     private volatile boolean writeBeforeHandshakeDone;
240     private final SSLEngineInboundCloseFuture sslEngineCloseFuture = new SSLEngineInboundCloseFuture();
241 
242     private boolean closeOnSslException;
243 
244     private int packetLength;
245 
246     private final Timer timer;
247     private final long handshakeTimeoutInMillis;
248     private Timeout handshakeTimeout;
249 
250     /**
251      * Creates a new instance.
252      *
253      * @param engine  the {@link SSLEngine} this handler will use
254      */
255     public SslHandler(SSLEngine engine) {
256         this(engine, getDefaultBufferPool(), false, null, 0);
257     }
258 
259     /**
260      * Creates a new instance.
261      *
262      * @param engine      the {@link SSLEngine} this handler will use
263      * @param bufferPool  the {@link SslBufferPool} where this handler will
264      *                    acquire the buffers required by the {@link SSLEngine}
265      */
266     public SslHandler(SSLEngine engine, SslBufferPool bufferPool) {
267         this(engine, bufferPool, false, null, 0);
268     }
269 
270     /**
271      * Creates a new instance.
272      *
273      * @param engine    the {@link SSLEngine} this handler will use
274      * @param startTls  {@code true} if the first write request shouldn't be
275      *                  encrypted by the {@link SSLEngine}
276      */
277     public SslHandler(SSLEngine engine, boolean startTls) {
278         this(engine, getDefaultBufferPool(), startTls);
279     }
280 
281     /**
282      * Creates a new instance.
283      *
284      * @param engine      the {@link SSLEngine} this handler will use
285      * @param bufferPool  the {@link SslBufferPool} where this handler will
286      *                    acquire the buffers required by the {@link SSLEngine}
287      * @param startTls    {@code true} if the first write request shouldn't be
288      *                    encrypted by the {@link SSLEngine}
289      */
290     public SslHandler(SSLEngine engine, SslBufferPool bufferPool, boolean startTls) {
291         this(engine, bufferPool, startTls, null, 0);
292     }
293 
294     /**
295      * Creates a new instance.
296      *
297      * @param engine
298      *        the {@link SSLEngine} this handler will use
299      * @param bufferPool
300      *        the {@link SslBufferPool} where this handler will acquire
301      *        the buffers required by the {@link SSLEngine}
302      * @param startTls
303      *        {@code true} if the first write request shouldn't be encrypted
304      *        by the {@link SSLEngine}
305      * @param timer
306      *        the {@link Timer} which will be used to process the timeout of the {@link #handshake()}.
307      *        Be aware that the given {@link Timer} will not get stopped automaticly, so it is up to you to cleanup
308      *        once you not need it anymore
309      * @param handshakeTimeoutInMillis
310      *        the time in milliseconds after whic the {@link #handshake()}  will be failed, and so the future notified
311      */
312     public SslHandler(SSLEngine engine, SslBufferPool bufferPool, boolean startTls, Timer timer,
313                       long handshakeTimeoutInMillis) {
314         if (engine == null) {
315             throw new NullPointerException("engine");
316         }
317         if (bufferPool == null) {
318             throw new NullPointerException("bufferPool");
319         }
320         if (timer == null && handshakeTimeoutInMillis > 0) {
321             throw new IllegalArgumentException("No Timer was given but a handshakeTimeoutInMillis, need both or none");
322         }
323 
324         this.engine = engine;
325         this.bufferPool = bufferPool;
326         this.startTls = startTls;
327         this.timer = timer;
328         this.handshakeTimeoutInMillis = handshakeTimeoutInMillis;
329     }
330 
331     /**
332      * Returns the {@link SSLEngine} which is used by this handler.
333      */
334     public SSLEngine getEngine() {
335         return engine;
336     }
337 
338     /**
339      * Starts an SSL / TLS handshake for the specified channel.
340      *
341      * @return a {@link ChannelFuture} which is notified when the handshake
342      *         succeeds or fails.
343      */
344     public ChannelFuture handshake() {
345         synchronized (handshakeLock) {
346             if (handshaken && !isEnableRenegotiation()) {
347                 throw new IllegalStateException("renegotiation disabled");
348             }
349 
350             final ChannelHandlerContext ctx = this.ctx;
351             final Channel channel = ctx.getChannel();
352             ChannelFuture handshakeFuture;
353             Exception exception = null;
354 
355             if (handshaking) {
356                 return this.handshakeFuture;
357             }
358 
359             handshaking = true;
360             try {
361                 engine.beginHandshake();
362                 runDelegatedTasks();
363                 handshakeFuture = this.handshakeFuture = future(channel);
364                 if (handshakeTimeoutInMillis > 0) {
365                     handshakeTimeout = timer.newTimeout(new TimerTask() {
366                             public void run(Timeout timeout) throws Exception {
367                             ChannelFuture future = SslHandler.this.handshakeFuture;
368                             if (future != null && future.isDone()) {
369                                 return;
370                             }
371 
372                             setHandshakeFailure(channel, new SSLException("Handshake did not complete within " +
373                                             handshakeTimeoutInMillis + "ms"));
374                         }
375                         }, handshakeTimeoutInMillis, TimeUnit.MILLISECONDS);
376                 }
377             } catch (Exception e) {
378                 handshakeFuture = this.handshakeFuture = failedFuture(channel, e);
379                 exception = e;
380             }
381 
382             if (exception == null) { // Began handshake successfully.
383                 try {
384                     final ChannelFuture hsFuture = handshakeFuture;
385                     wrapNonAppData(ctx, channel).addListener(new ChannelFutureListener() {
386                         public void operationComplete(ChannelFuture future) throws Exception {
387                             if (!future.isSuccess()) {
388                                 Throwable cause = future.getCause();
389                                 hsFuture.setFailure(cause);
390 
391                                 fireExceptionCaught(ctx, cause);
392                                 if (closeOnSslException) {
393                                     Channels.close(ctx, future(channel));
394                                 }
395                             }
396                         }
397                     });
398                 } catch (SSLException e) {
399                     handshakeFuture.setFailure(e);
400 
401                     fireExceptionCaught(ctx, e);
402                     if (closeOnSslException) {
403                         Channels.close(ctx, future(channel));
404                     }
405                 }
406             } else { // Failed to initiate handshake.
407                 fireExceptionCaught(ctx, exception);
408                 if (closeOnSslException) {
409                     Channels.close(ctx, future(channel));
410                 }
411             }
412             return handshakeFuture;
413         }
414     }
415 
416     /**
417      * Sends an SSL {@code close_notify} message to the specified channel and
418      * destroys the underlying {@link SSLEngine}.
419      */
420     public ChannelFuture close() {
421         ChannelHandlerContext ctx = this.ctx;
422         Channel channel = ctx.getChannel();
423         try {
424             engine.closeOutbound();
425             return wrapNonAppData(ctx, channel);
426         } catch (SSLException e) {
427             fireExceptionCaught(ctx, e);
428             if (closeOnSslException) {
429                 Channels.close(ctx, future(channel));
430             }
431             return failedFuture(channel, e);
432         }
433     }
434 
435     /**
436      * Returns {@code true} if and only if TLS renegotiation is enabled.
437      */
438     public boolean isEnableRenegotiation() {
439         return enableRenegotiation;
440     }
441 
442     /**
443      * Enables or disables TLS renegotiation.
444      */
445     public void setEnableRenegotiation(boolean enableRenegotiation) {
446         this.enableRenegotiation = enableRenegotiation;
447     }
448 
449     /**
450      * Enables or disables the automatic handshake once the {@link Channel} is
451      * connected. The value will only have affect if its set before the
452      * {@link Channel} is connected.
453      */
454     public void setIssueHandshake(boolean issueHandshake) {
455         this.issueHandshake = issueHandshake;
456     }
457 
458     /**
459      * Returns {@code true} if the automatic handshake is enabled
460      */
461     public boolean isIssueHandshake() {
462         return issueHandshake;
463     }
464 
465     /**
466      * Return the {@link ChannelFuture} that will get notified if the inbound of the {@link SSLEngine} will get closed.
467      *
468      * This method will return the same {@link ChannelFuture} all the time.
469      *
470      * For more informations see the apidocs of {@link SSLEngine}
471      *
472      */
473     public ChannelFuture getSSLEngineInboundCloseFuture() {
474         return sslEngineCloseFuture;
475     }
476 
477     /**
478      * Return the timeout (in ms) after which the {@link ChannelFuture} of {@link #handshake()} will be failed, while
479      * a handshake is in progress
480      */
481     public long getHandshakeTimeout() {
482         return handshakeTimeoutInMillis;
483     }
484 
485     /**
486      * If set to {@code true}, the {@link Channel} will automatically get closed
487      * one a {@link SSLException} was caught. This is most times what you want, as after this
488      * its almost impossible to recover.
489      *
490      * Anyway the default is {@code false} to not break compatibility with older releases. This
491      * will be changed to {@code true} in the next major release.
492      *
493      */
494     public void setCloseOnSSLException(boolean closeOnSslException) {
495         if (ctx != null) {
496             throw new IllegalStateException("Can only get changed before attached to ChannelPipeline");
497         }
498         this.closeOnSslException = closeOnSslException;
499     }
500 
501     public boolean getCloseOnSSLException() {
502         return closeOnSslException;
503     }
504 
505     public void handleDownstream(
506             final ChannelHandlerContext context, final ChannelEvent evt) throws Exception {
507         if (evt instanceof ChannelStateEvent) {
508             ChannelStateEvent e = (ChannelStateEvent) evt;
509             switch (e.getState()) {
510             case OPEN:
511             case CONNECTED:
512             case BOUND:
513                 if (Boolean.FALSE.equals(e.getValue()) || e.getValue() == null) {
514                     closeOutboundAndChannel(context, e);
515                     return;
516                 }
517             }
518         }
519         if (!(evt instanceof MessageEvent)) {
520             context.sendDownstream(evt);
521             return;
522         }
523 
524         MessageEvent e = (MessageEvent) evt;
525         if (!(e.getMessage() instanceof ChannelBuffer)) {
526             context.sendDownstream(evt);
527             return;
528         }
529 
530         // Do not encrypt the first write request if this handler is
531         // created with startTLS flag turned on.
532         if (startTls && SENT_FIRST_MESSAGE_UPDATER.compareAndSet(this, 0, 1)) {
533             context.sendDownstream(evt);
534             return;
535         }
536 
537         // Otherwise, all messages are encrypted.
538         ChannelBuffer msg = (ChannelBuffer) e.getMessage();
539         PendingWrite pendingWrite;
540 
541         if (msg.readable()) {
542             pendingWrite = new PendingWrite(evt.getFuture(), msg.toByteBuffer(msg.readerIndex(), msg.readableBytes()));
543         } else {
544             pendingWrite = new PendingWrite(evt.getFuture(), null);
545         }
546 
547         pendingUnencryptedWritesLock.lock();
548         try {
549             pendingUnencryptedWrites.add(pendingWrite);
550         } finally {
551             pendingUnencryptedWritesLock.unlock();
552         }
553 
554         if (handshakeFuture == null || !handshakeFuture.isDone()) {
555             writeBeforeHandshakeDone = true;
556         }
557         wrap(context, evt.getChannel());
558     }
559 
560     private void cancelHandshakeTimeout() {
561         if (handshakeTimeout != null) {
562             // cancel the task as we will fail the handshake future now
563             handshakeTimeout.cancel();
564         }
565     }
566 
567     @Override
568     public void channelDisconnected(ChannelHandlerContext ctx, ChannelStateEvent e) throws Exception {
569 
570         // Make sure the handshake future is notified when a connection has
571         // been closed during handshake.
572         synchronized (handshakeLock) {
573             if (handshaking) {
574                 cancelHandshakeTimeout();
575                 handshakeFuture.setFailure(new ClosedChannelException());
576             }
577         }
578 
579         try {
580             super.channelDisconnected(ctx, e);
581         } finally {
582             unwrapNonAppData(ctx, e.getChannel(), false);
583             closeEngine();
584         }
585     }
586 
587     private void closeEngine() {
588         engine.closeOutbound();
589         if (sentCloseNotify == 0 && handshaken) {
590             try {
591                 engine.closeInbound();
592             } catch (SSLException ex) {
593                 if (logger.isDebugEnabled()) {
594                     logger.debug("Failed to clean up SSLEngine.", ex);
595                 }
596             }
597         }
598     }
599 
600     @Override
601     public void exceptionCaught(ChannelHandlerContext ctx, ExceptionEvent e)
602             throws Exception {
603 
604         Throwable cause = e.getCause();
605         if (cause instanceof IOException) {
606             if (cause instanceof ClosedChannelException) {
607                 synchronized (ignoreClosedChannelExceptionLock) {
608                     if (ignoreClosedChannelException > 0) {
609                         ignoreClosedChannelException --;
610                         if (logger.isDebugEnabled()) {
611                             logger.debug(
612                                     "Swallowing an exception raised while " +
613                                     "writing non-app data", cause);
614                         }
615 
616                         return;
617                     }
618                 }
619             } else {
620                 if (ignoreException(cause)) {
621                     return;
622                 }
623             }
624         }
625 
626         ctx.sendUpstream(e);
627     }
628 
629     /**
630      * Checks if the given {@link Throwable} can be ignore and just "swallowed"
631      *
632      * When an ssl connection is closed a close_notify message is sent.
633      * After that the peer also sends close_notify however, it's not mandatory to receive
634      * the close_notify. The party who sent the initial close_notify can close the connection immediately
635      * then the peer will get connection reset error.
636      *
637      */
638     private boolean ignoreException(Throwable t) {
639         if (!(t instanceof SSLException) && t instanceof IOException && engine.isOutboundDone()) {
640             String message = String.valueOf(t.getMessage()).toLowerCase();
641 
642             // first try to match connection reset / broke peer based on the regex. This is the fastest way
643             // but may fail on different jdk impls or OS's
644             if (IGNORABLE_ERROR_MESSAGE.matcher(message).matches()) {
645                 return true;
646             }
647 
648             // Inspect the StackTraceElements to see if it was a connection reset / broken pipe or not
649             StackTraceElement[] elements = t.getStackTrace();
650             for (StackTraceElement element: elements) {
651                 String classname = element.getClassName();
652                 String methodname = element.getMethodName();
653 
654                 // skip all classes that belong to the io.netty package
655                 if (classname.startsWith("org.jboss.netty.")) {
656                     continue;
657                 }
658 
659                 // check if the method name is read if not skip it
660                 if (!"read".equals(methodname)) {
661                     continue;
662                 }
663 
664                 // This will also match against SocketInputStream which is used by openjdk 7 and maybe
665                 // also others
666                 if (IGNORABLE_CLASS_IN_STACK.matcher(classname).matches()) {
667                     return true;
668                 }
669 
670                 try {
671                     // No match by now.. Try to load the class via classloader and inspect it.
672                     // This is mainly done as other JDK implementations may differ in name of
673                     // the impl.
674                     Class<?> clazz = getClass().getClassLoader().loadClass(classname);
675 
676                     if (SocketChannel.class.isAssignableFrom(clazz)
677                             || DatagramChannel.class.isAssignableFrom(clazz)) {
678                         return true;
679                     }
680 
681                     // also match against SctpChannel via String matching as it may not present.
682                     if (DetectionUtil.javaVersion() >= 7
683                             && "com.sun.nio.sctp.SctpChannel".equals(clazz.getSuperclass().getName())) {
684                         return true;
685                     }
686                 } catch (ClassNotFoundException e) {
687                     // This should not happen just ignore
688                 }
689             }
690         }
691 
692         return false;
693     }
694 
695     /**
696      * Returns {@code true} if the given {@link ChannelBuffer} is encrypted. Be aware that this method
697      * will not increase the readerIndex of the given {@link ChannelBuffer}.
698      *
699      * @param   buffer
700      *                  The {@link ChannelBuffer} to read from. Be aware that it must have at least 5 bytes to read,
701      *                  otherwise it will throw an {@link IllegalArgumentException}.
702      * @return encrypted
703      *                  {@code true} if the {@link ChannelBuffer} is encrypted, {@code false} otherwise.
704      * @throws IllegalArgumentException
705      *                  Is thrown if the given {@link ChannelBuffer} has not at least 5 bytes to read.
706      */
707     public static boolean isEncrypted(ChannelBuffer buffer) {
708         return getEncryptedPacketLength(buffer, buffer.readerIndex()) != -1;
709     }
710 
711     /**
712      * Return how much bytes can be read out of the encrypted data. Be aware that this method will not increase
713      * the readerIndex of the given {@link ChannelBuffer}.
714      *
715      * @param   buffer
716      *                  The {@link ChannelBuffer} to read from. Be aware that it must have at least 5 bytes to read,
717      *                  otherwise it will throw an {@link IllegalArgumentException}.
718      * @return length
719      *                  The length of the encrypted packet that is included in the buffer. This will
720      *                  return {@code -1} if the given {@link ChannelBuffer} is not encrypted at all.
721      * @throws IllegalArgumentException
722      *                  Is thrown if the given {@link ChannelBuffer} has not at least 5 bytes to read.
723      */
724     private static int getEncryptedPacketLength(ChannelBuffer buffer, int offset) {
725         int packetLength = 0;
726 
727         // SSLv3 or TLS - Check ContentType
728         boolean tls;
729         switch (buffer.getUnsignedByte(offset)) {
730             case 20:  // change_cipher_spec
731             case 21:  // alert
732             case 22:  // handshake
733             case 23:  // application_data
734                 tls = true;
735                 break;
736             default:
737                 // SSLv2 or bad data
738                 tls = false;
739         }
740 
741         if (tls) {
742             // SSLv3 or TLS - Check ProtocolVersion
743             int majorVersion = buffer.getUnsignedByte(offset + 1);
744             if (majorVersion == 3) {
745                 // SSLv3 or TLS
746                 packetLength = (getShort(buffer, offset + 3) & 0xFFFF) + 5;
747                 if (packetLength <= 5) {
748                     // Neither SSLv3 or TLSv1 (i.e. SSLv2 or bad data)
749                     tls = false;
750                 }
751             } else {
752                 // Neither SSLv3 or TLSv1 (i.e. SSLv2 or bad data)
753                 tls = false;
754             }
755         }
756 
757         if (!tls) {
758             // SSLv2 or bad data - Check the version
759             boolean sslv2 = true;
760             int headerLength = (buffer.getUnsignedByte(offset) & 0x80) != 0 ? 2 : 3;
761             int majorVersion = buffer.getUnsignedByte(offset + headerLength + 1);
762             if (majorVersion == 2 || majorVersion == 3) {
763                 // SSLv2
764                 if (headerLength == 2) {
765                     packetLength = (getShort(buffer, offset) & 0x7FFF) + 2;
766                 } else {
767                     packetLength = (getShort(buffer, offset) & 0x3FFF) + 3;
768                 }
769                 if (packetLength <= headerLength) {
770                     sslv2 = false;
771                 }
772             } else {
773                 sslv2 = false;
774             }
775 
776             if (!sslv2) {
777                 return -1;
778             }
779         }
780         return packetLength;
781     }
782 
783     @Override
784     protected Object decode(
785             final ChannelHandlerContext ctx, Channel channel, ChannelBuffer in) throws Exception {
786 
787         final int startOffset = in.readerIndex();
788         final int endOffset = in.writerIndex();
789         int offset = startOffset;
790         int totalLength = 0;
791 
792         // If we calculated the length of the current SSL record before, use that information.
793         if (packetLength > 0) {
794             if (endOffset - startOffset < packetLength) {
795                 return null;
796             } else {
797                 offset += packetLength;
798                 totalLength = packetLength;
799                 packetLength = 0;
800             }
801         }
802 
803         boolean nonSslRecord = false;
804 
805         while (totalLength < OpenSslEngine.MAX_ENCRYPTED_PACKET_LENGTH) {
806             final int readableBytes = endOffset - offset;
807             if (readableBytes < 5) {
808                 break;
809             }
810 
811             final int packetLength = getEncryptedPacketLength(in, offset);
812             if (packetLength == -1) {
813                 nonSslRecord = true;
814                 break;
815             }
816 
817             assert packetLength > 0;
818 
819             if (packetLength > readableBytes) {
820                 // wait until the whole packet can be read
821                 this.packetLength = packetLength;
822                 break;
823             }
824 
825             int newTotalLength = totalLength + packetLength;
826             if (newTotalLength > OpenSslEngine.MAX_ENCRYPTED_PACKET_LENGTH) {
827                 // Don't read too much.
828                 break;
829             }
830 
831             // We have a whole packet.
832             // Increment the offset to handle the next packet.
833             offset += packetLength;
834             totalLength = newTotalLength;
835         }
836 
837         ChannelBuffer unwrapped = null;
838         if (totalLength > 0) {
839             // The buffer contains one or more full SSL records.
840             // Slice out the whole packet so unwrap will only be called with complete packets.
841             // Also directly reset the packetLength. This is needed as unwrap(..) may trigger
842             // decode(...) again via:
843             // 1) unwrap(..) is called
844             // 2) wrap(...) is called from within unwrap(...)
845             // 3) wrap(...) calls unwrapLater(...)
846             // 4) unwrapLater(...) calls decode(...)
847             //
848             // See https://github.com/netty/netty/issues/1534
849 
850             in.skipBytes(totalLength);
851             final ByteBuffer inNetBuf = in.toByteBuffer(startOffset, totalLength);
852             unwrapped = unwrap(ctx, channel, inNetBuf, totalLength, true);
853         }
854 
855         if (nonSslRecord) {
856             // Not an SSL/TLS packet
857             NotSslRecordException e = new NotSslRecordException(
858                     "not an SSL/TLS record: " + ChannelBuffers.hexDump(in));
859             in.skipBytes(in.readableBytes());
860             if (closeOnSslException) {
861                 // first trigger the exception and then close the channel
862                 fireExceptionCaught(ctx, e);
863                 Channels.close(ctx, future(channel));
864 
865                 // just return null as we closed the channel before, that
866                 // will take care of cleanup etc
867                 return null;
868             } else {
869                 throw e;
870             }
871         }
872 
873         return unwrapped;
874     }
875 
876     /**
877      * Reads a big-endian short integer from the buffer.  Please note that we do not use
878      * {@link ChannelBuffer#getShort(int)} because it might be a little-endian buffer.
879      */
880     private static short getShort(ChannelBuffer buf, int offset) {
881         return (short) (buf.getByte(offset) << 8 | buf.getByte(offset + 1) & 0xFF);
882     }
883 
884     private void wrap(ChannelHandlerContext context, Channel channel) throws SSLException {
885         ChannelBuffer msg;
886         ByteBuffer outNetBuf = bufferPool.acquireBuffer();
887         boolean success = true;
888         boolean offered = false;
889         boolean needsUnwrap = false;
890         PendingWrite pendingWrite = null;
891 
892         try {
893             loop:
894             for (;;) {
895                 // Acquire a lock to make sure unencrypted data is polled
896                 // in order and their encrypted counterpart is offered in
897                 // order.
898                 pendingUnencryptedWritesLock.lock();
899                 try {
900                     pendingWrite = pendingUnencryptedWrites.peek();
901                     if (pendingWrite == null) {
902                         break;
903                     }
904 
905                     ByteBuffer outAppBuf = pendingWrite.outAppBuf;
906                     if (outAppBuf == null) {
907                         // A write request with an empty buffer
908                         pendingUnencryptedWrites.remove();
909                         offerEncryptedWriteRequest(
910                                 new DownstreamMessageEvent(
911                                         channel, pendingWrite.future,
912                                         ChannelBuffers.EMPTY_BUFFER,
913                                         channel.getRemoteAddress()));
914                         offered = true;
915                     } else {
916                         synchronized (handshakeLock) {
917                             SSLEngineResult result = null;
918                             try {
919                                 result = engine.wrap(outAppBuf, outNetBuf);
920                             } finally {
921                                 if (!outAppBuf.hasRemaining()) {
922                                     pendingUnencryptedWrites.remove();
923                                 }
924                             }
925 
926                             if (result.bytesProduced() > 0) {
927                                 outNetBuf.flip();
928                                 int remaining = outNetBuf.remaining();
929                                 msg = ctx.getChannel().getConfig().getBufferFactory().getBuffer(remaining);
930 
931                                 // Transfer the bytes to the new ChannelBuffer using some safe method that will also
932                                 // work with "non" heap buffers
933                                 //
934                                 // See https://github.com/netty/netty/issues/329
935                                 msg.writeBytes(outNetBuf);
936                                 outNetBuf.clear();
937 
938                                 ChannelFuture future;
939                                 if (pendingWrite.outAppBuf.hasRemaining()) {
940                                     // pendingWrite's future shouldn't be notified if
941                                     // only partial data is written.
942                                     future = succeededFuture(channel);
943                                 } else {
944                                     future = pendingWrite.future;
945                                 }
946 
947                                 MessageEvent encryptedWrite = new DownstreamMessageEvent(
948                                         channel, future, msg, channel.getRemoteAddress());
949                                 offerEncryptedWriteRequest(encryptedWrite);
950                                 offered = true;
951                             } else if (result.getStatus() == Status.CLOSED) {
952                                 // SSLEngine has been closed already.
953                                 // Any further write attempts should be denied.
954                                 success = false;
955                                 break;
956                             } else {
957                                 final HandshakeStatus handshakeStatus = result.getHandshakeStatus();
958                                 handleRenegotiation(handshakeStatus);
959                                 switch (handshakeStatus) {
960                                 case NEED_WRAP:
961                                     if (outAppBuf.hasRemaining()) {
962                                         break;
963                                     } else {
964                                         break loop;
965                                     }
966                                 case NEED_UNWRAP:
967                                     needsUnwrap = true;
968                                     break loop;
969                                 case NEED_TASK:
970                                     runDelegatedTasks();
971                                     break;
972                                 case FINISHED:
973                                     setHandshakeSuccess(channel);
974                                     if (result.getStatus() == Status.CLOSED) {
975                                         success = false;
976                                     }
977                                     break loop;
978                                 case NOT_HANDSHAKING:
979                                     setHandshakeSuccessIfStillHandshaking(channel);
980                                     if (result.getStatus() == Status.CLOSED) {
981                                         success = false;
982                                     }
983                                     break loop;
984                                 default:
985                                     throw new IllegalStateException(
986                                             "Unknown handshake status: " +
987                                             handshakeStatus);
988                                 }
989                             }
990                         }
991                     }
992                 } finally {
993                     pendingUnencryptedWritesLock.unlock();
994                 }
995             }
996         } catch (SSLException e) {
997             success = false;
998             setHandshakeFailure(channel, e);
999             throw e;
1000         } finally {
1001             bufferPool.releaseBuffer(outNetBuf);
1002 
1003             if (offered) {
1004                 flushPendingEncryptedWrites(context);
1005             }
1006 
1007             if (!success) {
1008                 Exception cause = channel.isOpen()
1009                         ? new SSLException("SSLEngine already closed")
1010                         : new ClosedChannelException();
1011 
1012                 // Check if we had a pendingWrite in process, if so we need to also notify as otherwise
1013                 // the ChannelFuture will never get notified
1014                 if (pendingWrite != null) {
1015                     pendingWrite.future.setFailure(cause);
1016                 }
1017 
1018                 // Mark all remaining pending writes as failure if anything
1019                 // wrong happened before the write requests are wrapped.
1020                 // Please note that we do not call setFailure while a lock is
1021                 // acquired, to avoid a potential dead lock.
1022                 for (;;) {
1023                     pendingUnencryptedWritesLock.lock();
1024                     try {
1025                         pendingWrite = pendingUnencryptedWrites.poll();
1026                         if (pendingWrite == null) {
1027                             break;
1028                         }
1029                     } finally {
1030                         pendingUnencryptedWritesLock.unlock();
1031                     }
1032 
1033                     pendingWrite.future.setFailure(cause);
1034                 }
1035             }
1036         }
1037 
1038         if (needsUnwrap) {
1039             unwrapNonAppData(ctx, channel, true);
1040         }
1041     }
1042 
1043     private void offerEncryptedWriteRequest(MessageEvent encryptedWrite) {
1044         final boolean locked = pendingEncryptedWritesLock.tryLock();
1045         try {
1046             pendingEncryptedWrites.add(encryptedWrite);
1047         } finally {
1048             if (locked) {
1049                 pendingEncryptedWritesLock.unlock();
1050             }
1051         }
1052     }
1053 
1054     private void flushPendingEncryptedWrites(ChannelHandlerContext ctx) {
1055         while (!pendingEncryptedWrites.isEmpty()) {
1056             // Avoid possible dead lock and data integrity issue
1057             // which is caused by cross communication between more than one channel
1058             // in the same VM.
1059             if (!pendingEncryptedWritesLock.tryLock()) {
1060                 return;
1061             }
1062 
1063             try {
1064                 MessageEvent e;
1065                 while ((e = pendingEncryptedWrites.poll()) != null) {
1066                     ctx.sendDownstream(e);
1067                 }
1068             } finally {
1069                 pendingEncryptedWritesLock.unlock();
1070             }
1071 
1072             // Other thread might have added more elements at this point, so we loop again if the queue got unempty.
1073         }
1074     }
1075 
1076     private ChannelFuture wrapNonAppData(ChannelHandlerContext ctx, Channel channel) throws SSLException {
1077         ChannelFuture future = null;
1078         ByteBuffer outNetBuf = bufferPool.acquireBuffer();
1079 
1080         SSLEngineResult result;
1081         try {
1082             for (;;) {
1083                 synchronized (handshakeLock) {
1084                     result = engine.wrap(EMPTY_BUFFER, outNetBuf);
1085                 }
1086 
1087                 if (result.bytesProduced() > 0) {
1088                     outNetBuf.flip();
1089                     ChannelBuffer msg =
1090                             ctx.getChannel().getConfig().getBufferFactory().getBuffer(outNetBuf.remaining());
1091 
1092                     // Transfer the bytes to the new ChannelBuffer using some safe method that will also
1093                     // work with "non" heap buffers
1094                     //
1095                     // See https://github.com/netty/netty/issues/329
1096                     msg.writeBytes(outNetBuf);
1097                     outNetBuf.clear();
1098 
1099                     future = future(channel);
1100                     future.addListener(new ChannelFutureListener() {
1101                         public void operationComplete(ChannelFuture future)
1102                                 throws Exception {
1103                             if (future.getCause() instanceof ClosedChannelException) {
1104                                 synchronized (ignoreClosedChannelExceptionLock) {
1105                                     ignoreClosedChannelException ++;
1106                                 }
1107                             }
1108                         }
1109                     });
1110 
1111                     write(ctx, future, msg);
1112                 }
1113 
1114                 final HandshakeStatus handshakeStatus = result.getHandshakeStatus();
1115                 handleRenegotiation(handshakeStatus);
1116                 switch (handshakeStatus) {
1117                 case FINISHED:
1118                     setHandshakeSuccess(channel);
1119                     runDelegatedTasks();
1120                     break;
1121                 case NEED_TASK:
1122                     runDelegatedTasks();
1123                     break;
1124                 case NEED_UNWRAP:
1125                     if (!Thread.holdsLock(handshakeLock)) {
1126                         // unwrap shouldn't be called when this method was
1127                         // called by unwrap - unwrap will keep running after
1128                         // this method returns.
1129                         unwrapNonAppData(ctx, channel, true);
1130                     }
1131                     break;
1132                 case NOT_HANDSHAKING:
1133                     if (setHandshakeSuccessIfStillHandshaking(channel)) {
1134                         runDelegatedTasks();
1135                     }
1136                     break;
1137                 case NEED_WRAP:
1138                     break;
1139                 default:
1140                     throw new IllegalStateException(
1141                             "Unexpected handshake status: " + handshakeStatus);
1142                 }
1143 
1144                 if (result.bytesProduced() == 0) {
1145                     break;
1146                 }
1147             }
1148         } catch (SSLException e) {
1149             setHandshakeFailure(channel, e);
1150             throw e;
1151         } finally {
1152             bufferPool.releaseBuffer(outNetBuf);
1153         }
1154 
1155         if (future == null) {
1156             future = succeededFuture(channel);
1157         }
1158 
1159         return future;
1160     }
1161 
1162     /**
1163      * Calls {@link SSLEngine#unwrap(ByteBuffer, ByteBuffer)} with an empty buffer to handle handshakes, etc.
1164      */
1165     private void unwrapNonAppData(
1166             ChannelHandlerContext ctx, Channel channel, boolean mightNeedHandshake) throws SSLException {
1167         unwrap(ctx, channel, EMPTY_BUFFER, -1, mightNeedHandshake);
1168     }
1169 
1170     /**
1171      * Unwraps inbound SSL records.
1172      */
1173     private ChannelBuffer unwrap(
1174             ChannelHandlerContext ctx, Channel channel,
1175             ByteBuffer nioInNetBuf,
1176             int initialNettyOutAppBufCapacity, boolean mightNeedHandshake) throws SSLException {
1177 
1178         final int nioInNetBufStartOffset = nioInNetBuf.position();
1179         final ByteBuffer nioOutAppBuf = bufferPool.acquireBuffer();
1180 
1181         ChannelBuffer nettyOutAppBuf = null;
1182 
1183         try {
1184             boolean needsWrap = false;
1185             for (;;) {
1186                 SSLEngineResult result;
1187                 boolean needsHandshake = false;
1188                 if (mightNeedHandshake) {
1189                     synchronized (handshakeLock) {
1190                         if (!handshaken && !handshaking &&
1191                             !engine.getUseClientMode() &&
1192                             !engine.isInboundDone() && !engine.isOutboundDone()) {
1193                             needsHandshake = true;
1194                         }
1195                     }
1196                 }
1197 
1198                 if (needsHandshake) {
1199                     handshake();
1200                 }
1201 
1202                 synchronized (handshakeLock) {
1203                     // Decrypt at least one record in the inbound network buffer.
1204                     // It is impossible to consume no record here because we made sure the inbound network buffer
1205                     // always contain at least one record in decode().  Therefore, if SSLEngine.unwrap() returns
1206                     // BUFFER_OVERFLOW, it is always resolved by retrying after emptying the application buffer.
1207                     for (;;) {
1208                         final int outAppBufSize = engine.getSession().getApplicationBufferSize();
1209                         final ByteBuffer outAppBuf;
1210                         if (nioOutAppBuf.capacity() < outAppBufSize) {
1211                             // SSLEngine wants a buffer larger than what the pool can provide.
1212                             // Allocate a temporary heap buffer.
1213                             outAppBuf = ByteBuffer.allocate(outAppBufSize);
1214                         } else {
1215                             outAppBuf = nioOutAppBuf;
1216                         }
1217 
1218                         try {
1219                             result = engine.unwrap(nioInNetBuf, outAppBuf);
1220                             switch (result.getStatus()) {
1221                                 case CLOSED:
1222                                     // notify about the CLOSED state of the SSLEngine. See #137
1223                                     sslEngineCloseFuture.setClosed();
1224                                     break;
1225                                 case BUFFER_OVERFLOW:
1226                                     // Flush the unwrapped data in the outAppBuf into frame and try again.
1227                                     // See the finally block.
1228                                     continue;
1229                             }
1230 
1231                             break;
1232                         } finally {
1233                             outAppBuf.flip();
1234 
1235                             // Copy the unwrapped data into a smaller buffer.
1236                             if (outAppBuf.hasRemaining()) {
1237                                 if (nettyOutAppBuf == null) {
1238                                     ChannelBufferFactory factory = ctx.getChannel().getConfig().getBufferFactory();
1239                                     nettyOutAppBuf = factory.getBuffer(initialNettyOutAppBufCapacity);
1240                                 }
1241                                 nettyOutAppBuf.writeBytes(outAppBuf);
1242                             }
1243                             outAppBuf.clear();
1244                         }
1245                     }
1246 
1247                     final HandshakeStatus handshakeStatus = result.getHandshakeStatus();
1248                     handleRenegotiation(handshakeStatus);
1249                     switch (handshakeStatus) {
1250                     case NEED_UNWRAP:
1251                         break;
1252                     case NEED_WRAP:
1253                         wrapNonAppData(ctx, channel);
1254                         break;
1255                     case NEED_TASK:
1256                         runDelegatedTasks();
1257                         break;
1258                     case FINISHED:
1259                         setHandshakeSuccess(channel);
1260                         needsWrap = true;
1261                         continue;
1262                     case NOT_HANDSHAKING:
1263                         if (setHandshakeSuccessIfStillHandshaking(channel)) {
1264                             needsWrap = true;
1265                             continue;
1266                         }
1267                         if (writeBeforeHandshakeDone) {
1268                             // We need to call wrap(...) in case there was a flush done before the handshake completed.
1269                             //
1270                             // See https://github.com/netty/netty/pull/2437
1271                             writeBeforeHandshakeDone = false;
1272                             needsWrap = true;
1273                         }
1274                         break;
1275                     default:
1276                         throw new IllegalStateException(
1277                                 "Unknown handshake status: " + handshakeStatus);
1278                     }
1279 
1280                     if (result.getStatus() == Status.BUFFER_UNDERFLOW ||
1281                         result.bytesConsumed() == 0 && result.bytesProduced() == 0) {
1282                         if (nioInNetBuf.hasRemaining() && !engine.isInboundDone()) {
1283                             // We expect SSLEngine to consume all the bytes we feed it, but
1284                             // empirical evidence indicates that we sometimes end up with leftovers
1285                             // Log when this happens to get a better understanding of this corner
1286                             // case.
1287                             // See https://github.com/netty/netty/pull/3584
1288                             logger.warn("Unexpected leftover data after SSLEngine.unwrap():"
1289                                     + " status=" + result.getStatus()
1290                                     + " handshakeStatus=" + result.getHandshakeStatus()
1291                                     + " consumed=" + result.bytesConsumed()
1292                                     + " produced=" + result.bytesProduced()
1293                                     + " remaining=" + nioInNetBuf.remaining()
1294                                     + " data=" + ChannelBuffers.hexDump(ChannelBuffers.wrappedBuffer(nioInNetBuf)));
1295                         }
1296                         break;
1297                     }
1298                 }
1299             }
1300 
1301             if (needsWrap) {
1302                 // wrap() acquires pendingUnencryptedWrites first and then
1303                 // handshakeLock.  If handshakeLock is already hold by the
1304                 // current thread, calling wrap() will lead to a dead lock
1305                 // i.e. pendingUnencryptedWrites -> handshakeLock vs.
1306                 //      handshakeLock -> pendingUnencryptedLock -> handshakeLock
1307                 //
1308                 // There is also the same issue between pendingEncryptedWrites
1309                 // and pendingUnencryptedWrites.
1310                 if (!Thread.holdsLock(handshakeLock) && !pendingEncryptedWritesLock.isHeldByCurrentThread()) {
1311                     wrap(ctx, channel);
1312                 }
1313             }
1314         } catch (SSLException e) {
1315             setHandshakeFailure(channel, e);
1316             throw e;
1317         } finally {
1318             bufferPool.releaseBuffer(nioOutAppBuf);
1319         }
1320 
1321         if (nettyOutAppBuf != null && nettyOutAppBuf.readable()) {
1322             return nettyOutAppBuf;
1323         } else {
1324             return null;
1325         }
1326     }
1327 
1328     private void handleRenegotiation(HandshakeStatus handshakeStatus) {
1329         synchronized (handshakeLock) {
1330             if (handshakeStatus == HandshakeStatus.NOT_HANDSHAKING ||
1331                 handshakeStatus == HandshakeStatus.FINISHED) {
1332                 // Not handshaking
1333                 return;
1334             }
1335 
1336             if (!handshaken) {
1337                 // Not renegotiation
1338                 return;
1339             }
1340 
1341             final boolean renegotiate;
1342             if (handshaking) {
1343                 // Renegotiation in progress or failed already.
1344                 // i.e. Renegotiation check has been done already below.
1345                 return;
1346             }
1347 
1348             if (engine.isInboundDone() || engine.isOutboundDone()) {
1349                 // Not handshaking but closing.
1350                 return;
1351             }
1352 
1353             if (isEnableRenegotiation()) {
1354                 // Continue renegotiation.
1355                 renegotiate = true;
1356             } else {
1357                 // Do not renegotiate.
1358                 renegotiate = false;
1359                 // Prevent reentrance of this method.
1360                 handshaking = true;
1361             }
1362 
1363             if (renegotiate) {
1364                 // Renegotiate.
1365                 handshake();
1366             } else {
1367                 // Raise an exception.
1368                 fireExceptionCaught(
1369                         ctx, new SSLException(
1370                                 "renegotiation attempted by peer; " +
1371                                 "closing the connection"));
1372 
1373                 // Close the connection to stop renegotiation.
1374                 Channels.close(ctx, succeededFuture(ctx.getChannel()));
1375             }
1376         }
1377     }
1378 
1379     /**
1380      * Fetches all delegated tasks from the {@link SSLEngine} and runs them immediately.
1381      */
1382     private void runDelegatedTasks() {
1383         for (;;) {
1384             final Runnable task;
1385             synchronized (handshakeLock) {
1386                 task = engine.getDelegatedTask();
1387             }
1388 
1389             if (task == null) {
1390                 break;
1391             }
1392 
1393             task.run();
1394         }
1395     }
1396 
1397     /**
1398      * Works around some Android {@link SSLEngine} implementations that skip {@link HandshakeStatus#FINISHED} and
1399      * go straight into {@link HandshakeStatus#NOT_HANDSHAKING} when handshake is finished.
1400      *
1401      * @return {@code true} if and only if the workaround has been applied and thus {@link #handshakeFuture} has been
1402      *         marked as success by this method
1403      */
1404     private boolean setHandshakeSuccessIfStillHandshaking(Channel channel) {
1405         if (handshaking && !handshakeFuture.isDone()) {
1406             setHandshakeSuccess(channel);
1407             return true;
1408         }
1409         return false;
1410     }
1411 
1412     private void setHandshakeSuccess(Channel channel) {
1413         synchronized (handshakeLock) {
1414             handshaking = false;
1415             handshaken = true;
1416 
1417             if (handshakeFuture == null) {
1418                 handshakeFuture = future(channel);
1419             }
1420             cancelHandshakeTimeout();
1421         }
1422 
1423         if (logger.isDebugEnabled()) {
1424             logger.debug(channel + " HANDSHAKEN: " + engine.getSession().getCipherSuite());
1425         }
1426 
1427         handshakeFuture.setSuccess();
1428     }
1429 
1430     private void setHandshakeFailure(Channel channel, SSLException cause) {
1431         synchronized (handshakeLock) {
1432             if (!handshaking) {
1433                 return;
1434             }
1435             handshaking = false;
1436             handshaken = false;
1437 
1438             if (handshakeFuture == null) {
1439                 handshakeFuture = future(channel);
1440             }
1441 
1442             // cancel the timeout now
1443             cancelHandshakeTimeout();
1444 
1445             // Release all resources such as internal buffers that SSLEngine
1446             // is managing.
1447 
1448             engine.closeOutbound();
1449 
1450             try {
1451                 engine.closeInbound();
1452             } catch (SSLException e) {
1453                 if (logger.isDebugEnabled()) {
1454                     logger.debug(
1455                             "SSLEngine.closeInbound() raised an exception after " +
1456                             "a handshake failure.", e);
1457                 }
1458             }
1459         }
1460 
1461         handshakeFuture.setFailure(cause);
1462         if (closeOnSslException) {
1463             Channels.close(ctx, future(channel));
1464         }
1465     }
1466 
1467     private void closeOutboundAndChannel(
1468             final ChannelHandlerContext context, final ChannelStateEvent e) {
1469         if (!e.getChannel().isConnected()) {
1470             context.sendDownstream(e);
1471             return;
1472         }
1473 
1474         // Ensure that the tear-down logic beyond this point is never invoked concurrently nor multiple times.
1475         if (!CLOSED_OUTBOUND_AND_CHANNEL_UPDATER.compareAndSet(this, 0, 1)) {
1476             // The other thread called this method already, and thus the connection will be closed eventually.
1477             // So, just wait until the connection is closed, and then forward the event so that the sink handles
1478             // the duplicate close attempt.
1479             e.getChannel().getCloseFuture().addListener(new ChannelFutureListener() {
1480                 public void operationComplete(ChannelFuture future) throws Exception {
1481                     context.sendDownstream(e);
1482                 }
1483             });
1484             return;
1485         }
1486 
1487         boolean passthrough = true;
1488         try {
1489             try {
1490                 unwrapNonAppData(ctx, e.getChannel(), false);
1491             } catch (SSLException ex) {
1492                 if (logger.isDebugEnabled()) {
1493                     logger.debug("Failed to unwrap before sending a close_notify message", ex);
1494                 }
1495             }
1496 
1497             if (!engine.isOutboundDone()) {
1498                 if (SENT_CLOSE_NOTIFY_UPDATER.compareAndSet(this, 0, 1)) {
1499                     engine.closeOutbound();
1500                     try {
1501                         ChannelFuture closeNotifyFuture = wrapNonAppData(context, e.getChannel());
1502                         closeNotifyFuture.addListener(
1503                                 new ClosingChannelFutureListener(context, e));
1504                         passthrough = false;
1505                     } catch (SSLException ex) {
1506                         if (logger.isDebugEnabled()) {
1507                             logger.debug("Failed to encode a close_notify message", ex);
1508                         }
1509                     }
1510                 }
1511             }
1512         } finally {
1513             if (passthrough) {
1514                 context.sendDownstream(e);
1515             }
1516         }
1517     }
1518 
1519     private static final class PendingWrite {
1520         final ChannelFuture future;
1521         final ByteBuffer outAppBuf;
1522 
1523         PendingWrite(ChannelFuture future, ByteBuffer outAppBuf) {
1524             this.future = future;
1525             this.outAppBuf = outAppBuf;
1526         }
1527     }
1528 
1529     private static final class ClosingChannelFutureListener implements ChannelFutureListener {
1530 
1531         private final ChannelHandlerContext context;
1532         private final ChannelStateEvent e;
1533 
1534         ClosingChannelFutureListener(
1535                 ChannelHandlerContext context, ChannelStateEvent e) {
1536             this.context = context;
1537             this.e = e;
1538         }
1539 
1540         public void operationComplete(ChannelFuture closeNotifyFuture) throws Exception {
1541             if (!(closeNotifyFuture.getCause() instanceof ClosedChannelException)) {
1542                 Channels.close(context, e.getFuture());
1543             } else {
1544                 e.getFuture().setSuccess();
1545             }
1546         }
1547     }
1548 
1549     @Override
1550     public void beforeAdd(ChannelHandlerContext ctx) throws Exception {
1551         super.beforeAdd(ctx);
1552         this.ctx = ctx;
1553     }
1554 
1555     /**
1556      * Fail all pending writes which we were not able to flush out
1557      */
1558     @Override
1559     public void afterRemove(ChannelHandlerContext ctx) throws Exception {
1560         closeEngine();
1561 
1562         // there is no need for synchronization here as we do not receive downstream events anymore
1563         Throwable cause = null;
1564         for (;;) {
1565             PendingWrite pw = pendingUnencryptedWrites.poll();
1566             if (pw == null) {
1567                 break;
1568             }
1569             if (cause == null) {
1570                 cause = new IOException("Unable to write data");
1571             }
1572             pw.future.setFailure(cause);
1573         }
1574 
1575         for (;;) {
1576             MessageEvent ev = pendingEncryptedWrites.poll();
1577             if (ev == null) {
1578                 break;
1579             }
1580             if (cause == null) {
1581                 cause = new IOException("Unable to write data");
1582             }
1583             ev.getFuture().setFailure(cause);
1584         }
1585 
1586         if (cause != null) {
1587             fireExceptionCaughtLater(ctx, cause);
1588         }
1589     }
1590 
1591     /**
1592      * Calls {@link #handshake()} once the {@link Channel} is connected
1593      */
1594     @Override
1595     public void channelConnected(final ChannelHandlerContext ctx, final ChannelStateEvent e) throws Exception {
1596         if (issueHandshake) {
1597             // issue and handshake and add a listener to it which will fire an exception event if
1598             // an exception was thrown while doing the handshake
1599             handshake().addListener(new ChannelFutureListener() {
1600 
1601                 public void operationComplete(ChannelFuture future) throws Exception {
1602                     if (future.isSuccess()) {
1603                         // Send the event upstream after the handshake was completed without an error.
1604                         //
1605                         // See https://github.com/netty/netty/issues/358
1606                         ctx.sendUpstream(e);
1607                     }
1608                 }
1609             });
1610         } else {
1611             super.channelConnected(ctx, e);
1612         }
1613     }
1614 
1615     /**
1616      * Loop over all the pending writes and fail them.
1617      *
1618      * See <a href="https://github.com/netty/netty/issues/305">#305</a> for more details.
1619      */
1620     @Override
1621     public void channelClosed(final ChannelHandlerContext ctx, ChannelStateEvent e) throws Exception {
1622         // Move the fail of the writes to the IO-Thread to prevent possible deadlock
1623         // See https://github.com/netty/netty/issues/989
1624         ctx.getPipeline().execute(new Runnable() {
1625             public void run() {
1626                 if (!pendingUnencryptedWritesLock.tryLock()) {
1627                     return;
1628                 }
1629 
1630                 List<ChannelFuture> futures = null;
1631                 try {
1632                     for (;;) {
1633                         PendingWrite pw = pendingUnencryptedWrites.poll();
1634                         if (pw == null) {
1635                             break;
1636                         }
1637                         if (futures == null) {
1638                             futures = new ArrayList<ChannelFuture>();
1639                         }
1640                         futures.add(pw.future);
1641                     }
1642 
1643                     for (;;) {
1644                         MessageEvent ev = pendingEncryptedWrites.poll();
1645                         if (ev == null) {
1646                             break;
1647                         }
1648                         if (futures == null) {
1649                             futures = new ArrayList<ChannelFuture>();
1650                         }
1651                         futures.add(ev.getFuture());
1652                     }
1653                 } finally {
1654                     pendingUnencryptedWritesLock.unlock();
1655                 }
1656 
1657                 if (futures != null) {
1658                     final ClosedChannelException cause = new ClosedChannelException();
1659                     final int size = futures.size();
1660                     for (int i = 0; i < size; i ++) {
1661                         futures.get(i).setFailure(cause);
1662                     }
1663                     fireExceptionCaught(ctx, cause);
1664                 }
1665             }
1666         });
1667 
1668         super.channelClosed(ctx, e);
1669     }
1670 
1671     private final class SSLEngineInboundCloseFuture extends DefaultChannelFuture {
1672         SSLEngineInboundCloseFuture() {
1673             super(null, true);
1674         }
1675 
1676         void setClosed() {
1677             super.setSuccess();
1678         }
1679 
1680         @Override
1681         public Channel getChannel() {
1682             if (ctx == null) {
1683                 // Maybe we should better throw an IllegalStateException() ?
1684                 return null;
1685             } else {
1686                 return ctx.getChannel();
1687             }
1688         }
1689 
1690         @Override
1691         public boolean setSuccess() {
1692             return false;
1693         }
1694 
1695         @Override
1696         public boolean setFailure(Throwable cause) {
1697             return false;
1698         }
1699     }
1700 }