2 // ========================================================================
3 // Copyright (c) 1995-2014 Mort Bay Consulting Pty. Ltd.
4 // ------------------------------------------------------------------------
5 // All rights reserved. This program and the accompanying materials
6 // are made available under the terms of the Eclipse Public License v1.0
7 // and Apache License v2.0 which accompanies this distribution.
9 // The Eclipse Public License is available at
10 // http://www.eclipse.org/legal/epl-v10.html
12 // The Apache License v2.0 is available at
13 // http://www.opensource.org/licenses/apache2.0.php
15 // You may elect to redistribute this code under either of these licenses.
16 // ========================================================================
19 package org.eclipse.jetty.io.ssl;
21 import java.io.IOException;
22 import java.nio.ByteBuffer;
23 import java.nio.channels.ClosedChannelException;
24 import java.util.Arrays;
25 import java.util.concurrent.Executor;
27 import javax.net.ssl.SSLEngine;
28 import javax.net.ssl.SSLEngineResult;
29 import javax.net.ssl.SSLEngineResult.HandshakeStatus;
30 import javax.net.ssl.SSLEngineResult.Status;
31 import javax.net.ssl.SSLException;
33 import org.eclipse.jetty.io.AbstractConnection;
34 import org.eclipse.jetty.io.AbstractEndPoint;
35 import org.eclipse.jetty.io.ByteBufferPool;
36 import org.eclipse.jetty.io.Connection;
37 import org.eclipse.jetty.io.EndPoint;
38 import org.eclipse.jetty.io.EofException;
39 import org.eclipse.jetty.io.FillInterest;
40 import org.eclipse.jetty.io.RuntimeIOException;
41 import org.eclipse.jetty.io.SelectChannelEndPoint;
42 import org.eclipse.jetty.io.WriteFlusher;
43 import org.eclipse.jetty.util.BufferUtil;
44 import org.eclipse.jetty.util.Callback;
45 import org.eclipse.jetty.util.log.Log;
46 import org.eclipse.jetty.util.log.Logger;
49 * A Connection that acts as an interceptor between an EndPoint providing SSL encrypted data
50 * and another consumer of an EndPoint (typically an {@link Connection} like HttpConnection) that
51 * wants unencrypted data.
53 * The connector uses an {@link EndPoint} (typically {@link SelectChannelEndPoint}) as
54 * it's source/sink of encrypted data. It then provides an endpoint via {@link #getDecryptedEndPoint()} to
55 * expose a source/sink of unencrypted data to another connection (eg HttpConnection).
57 * The design of this class is based on a clear separation between the passive methods, which do not block nor schedule any
58 * asynchronous callbacks, and active methods that do schedule asynchronous callbacks.
60 * The passive methods are {@link DecryptedEndPoint#fill(ByteBuffer)} and {@link DecryptedEndPoint#flush(ByteBuffer...)}. They make best
61 * effort attempts to progress the connection using only calls to the encrypted {@link EndPoint#fill(ByteBuffer)} and {@link EndPoint#flush(ByteBuffer...)}
62 * methods. They will never block nor schedule any readInterest or write callbacks. If a fill/flush cannot progress either because
63 * of network congestion or waiting for an SSL handshake message, then the fill/flush will simply return with zero bytes filled/flushed.
64 * Specifically, if a flush cannot proceed because it needs to receive a handshake message, then the flush will attempt to fill bytes from the
65 * encrypted endpoint, but if insufficient bytes are read it will NOT call {@link EndPoint#fillInterested(Callback)}.
67 * It is only the active methods : {@link DecryptedEndPoint#fillInterested(Callback)} and
68 * {@link DecryptedEndPoint#write(Callback, ByteBuffer...)} that may schedule callbacks by calling the encrypted
69 * {@link EndPoint#fillInterested(Callback)} and {@link EndPoint#write(Callback, ByteBuffer...)}
70 * methods. For normal data handling, the decrypted fillInterest method will result in an encrypted fillInterest and a decrypted
71 * write will result in an encrypted write. However, due to SSL handshaking requirements, it is also possible for a decrypted fill
72 * to call the encrypted write and for the decrypted flush to call the encrypted fillInterested methods.
74 * MOST IMPORTANTLY, the encrypted callbacks from the active methods (#onFillable() and WriteFlusher#completeWrite()) do no filling or flushing
75 * themselves. Instead they simple make the callbacks to the decrypted callbacks, so that the passive encrypted fill/flush will
76 * be called again and make another best effort attempt to progress the connection.
79 public class SslConnection extends AbstractConnection
81 private static final Logger LOG = Log.getLogger(SslConnection.class);
82 private static final boolean DEBUG = LOG.isDebugEnabled(); // Easy for the compiler to remove the code if DEBUG==false
83 private static final ByteBuffer __FILL_CALLED_FLUSH= BufferUtil.allocate(0);
84 private static final ByteBuffer __FLUSH_CALLED_FILL= BufferUtil.allocate(0);
85 private final ByteBufferPool _bufferPool;
86 private final SSLEngine _sslEngine;
87 private final DecryptedEndPoint _decryptedEndPoint;
88 private ByteBuffer _decryptedInput;
89 private ByteBuffer _encryptedInput;
90 private ByteBuffer _encryptedOutput;
91 private final boolean _encryptedDirectBuffers = false;
92 private final boolean _decryptedDirectBuffers = false;
93 private final Runnable _runCompletWrite = new Runnable()
98 _decryptedEndPoint.getWriteFlusher().completeWrite();
101 private boolean _renegotiationAllowed;
103 public SslConnection(ByteBufferPool byteBufferPool, Executor executor, EndPoint endPoint, SSLEngine sslEngine)
105 // This connection does not execute calls to onfillable, so they will be called by the selector thread.
106 // onfillable does not block and will only wakeup another thread to do the actual reading and handling.
107 super(endPoint, executor, !EXECUTE_ONFILLABLE);
108 this._bufferPool = byteBufferPool;
109 this._sslEngine = sslEngine;
110 this._decryptedEndPoint = newDecryptedEndPoint();
113 protected DecryptedEndPoint newDecryptedEndPoint()
115 return new DecryptedEndPoint();
118 public SSLEngine getSSLEngine()
123 public DecryptedEndPoint getDecryptedEndPoint()
125 return _decryptedEndPoint;
128 public boolean isRenegotiationAllowed()
130 return _renegotiationAllowed;
133 public void setRenegotiationAllowed(boolean renegotiationAllowed)
135 this._renegotiationAllowed = renegotiationAllowed;
143 // Begin the handshake
144 _sslEngine.beginHandshake();
146 getDecryptedEndPoint().getConnection().onOpen();
148 catch (SSLException x)
150 getEndPoint().close();
151 throw new RuntimeIOException(x);
156 public void onClose()
158 _decryptedEndPoint.getConnection().onClose();
165 getDecryptedEndPoint().getConnection().close();
169 public void onFillable()
171 // onFillable means that there are encrypted bytes ready to be filled.
172 // however we do not fill them here on this callback, but instead wakeup
173 // the decrypted readInterest and/or writeFlusher so that they will attempt
174 // to do the fill and/or flush again and these calls will do the actually
178 LOG.debug("onFillable enter {}", _decryptedEndPoint);
180 // We have received a close handshake, close the end point to send FIN.
181 if (_decryptedEndPoint.isInputShutdown())
182 _decryptedEndPoint.close();
184 // wake up whoever is doing the fill or the flush so they can
185 // do all the filling, unwrapping, wrapping and flushing
186 _decryptedEndPoint.getFillInterest().fillable();
188 // If we are handshaking, then wake up any waiting write as well as it may have been blocked on the read
189 synchronized(_decryptedEndPoint)
191 if (_decryptedEndPoint._flushRequiresFillToProgress)
193 _decryptedEndPoint._flushRequiresFillToProgress = false;
194 getExecutor().execute(_runCompletWrite);
199 LOG.debug("onFillable exit {}", _decryptedEndPoint);
203 public void onFillInterestedFailed(Throwable cause)
205 // this means that the fill interest in encrypted bytes has failed.
206 // However we do not handle that here on this callback, but instead wakeup
207 // the decrypted readInterest and/or writeFlusher so that they will attempt
208 // to do the fill and/or flush again and these calls will do the actually
210 _decryptedEndPoint.getFillInterest().onFail(cause);
212 boolean failFlusher = false;
213 synchronized(_decryptedEndPoint)
215 if (_decryptedEndPoint._flushRequiresFillToProgress)
217 _decryptedEndPoint._flushRequiresFillToProgress = false;
222 _decryptedEndPoint.getWriteFlusher().onFail(cause);
226 public String toString()
228 ByteBuffer b = _encryptedInput;
229 int ei=b==null?-1:b.remaining();
230 b = _encryptedOutput;
231 int eo=b==null?-1:b.remaining();
233 int di=b==null?-1:b.remaining();
235 return String.format("SslConnection@%x{%s,eio=%d/%d,di=%d} -> %s",
237 _sslEngine.getHandshakeStatus(),
239 _decryptedEndPoint.getConnection());
242 public class DecryptedEndPoint extends AbstractEndPoint
244 private boolean _fillRequiresFlushToProgress;
245 private boolean _flushRequiresFillToProgress;
246 private boolean _cannotAcceptMoreAppDataToFlush;
247 private boolean _handshaken;
248 private boolean _underFlown;
250 private final Callback _writeCallback = new Callback()
253 public void succeeded()
255 // This means that a write of encrypted data has completed. Writes are done
256 // only if there is a pending writeflusher or a read needed to write
257 // data. In either case the appropriate callback is passed on.
258 boolean fillable = false;
259 synchronized (DecryptedEndPoint.this)
262 LOG.debug("write.complete {}", SslConnection.this.getEndPoint());
264 releaseEncryptedOutputBuffer();
266 _cannotAcceptMoreAppDataToFlush = false;
268 if (_fillRequiresFlushToProgress)
270 _fillRequiresFlushToProgress = false;
275 getFillInterest().fillable();
276 getExecutor().execute(_runCompletWrite);
280 public void failed(final Throwable x)
282 // This means that a write of data has failed. Writes are done
283 // only if there is an active writeflusher or a read needed to write
284 // data. In either case the appropriate callback is passed on.
285 boolean fail_filler = false;
286 synchronized (DecryptedEndPoint.this)
289 LOG.debug("{} write.failed", SslConnection.this, x);
290 BufferUtil.clear(_encryptedOutput);
291 releaseEncryptedOutputBuffer();
293 _cannotAcceptMoreAppDataToFlush = false;
295 if (_fillRequiresFlushToProgress)
297 _fillRequiresFlushToProgress = false;
302 final boolean filler_failed=fail_filler;
304 failedCallback(new Callback()
307 public void succeeded()
312 public void failed(Throwable x)
315 getFillInterest().onFail(x);
316 getWriteFlusher().onFail(x);
323 public DecryptedEndPoint()
325 super(null,getEndPoint().getLocalAddress(), getEndPoint().getRemoteAddress());
326 setIdleTimeout(getEndPoint().getIdleTimeout());
330 protected FillInterest getFillInterest()
332 return super.getFillInterest();
336 public void setIdleTimeout(long idleTimeout)
338 super.setIdleTimeout(idleTimeout);
339 getEndPoint().setIdleTimeout(idleTimeout);
343 protected WriteFlusher getWriteFlusher()
345 return super.getWriteFlusher();
349 protected void onIncompleteFlush()
351 // This means that the decrypted endpoint write method was called and not
352 // all data could be wrapped. So either we need to write some encrypted data,
353 // OR if we are handshaking we need to read some encrypted data OR
354 // if neither then we should just try the flush again.
355 boolean flush = false;
356 synchronized (DecryptedEndPoint.this)
359 LOG.debug("onIncompleteFlush {}", getEndPoint());
360 // If we have pending output data,
361 if (BufferUtil.hasContent(_encryptedOutput))
364 _cannotAcceptMoreAppDataToFlush = true;
365 getEndPoint().write(_writeCallback, _encryptedOutput);
367 // If we are handshaking and need to read,
368 else if (_sslEngine.getHandshakeStatus() == HandshakeStatus.NEED_UNWRAP)
370 // check if we are actually read blocked in order to write
371 _flushRequiresFillToProgress = true;
372 SslConnection.this.fillInterested();
381 // If the output is closed,
382 if (isOutputShutdown())
384 // don't bother writing, just notify of close
385 getWriteFlusher().onClose();
390 // try to flush what is pending
391 getWriteFlusher().completeWrite();
397 protected boolean needsFill() throws IOException
399 // This means that the decrypted data consumer has called the fillInterested
400 // method on the DecryptedEndPoint, so we have to work out if there is
401 // decrypted data to be filled or what callbacks to setup to be told when there
402 // might be more encrypted data available to attempt another call to fill
404 synchronized (DecryptedEndPoint.this)
406 // Do we already have some app data, then app can fill now so return true
407 if (BufferUtil.hasContent(_decryptedInput))
410 // If we have no encrypted data to decrypt OR we have some, but it is not enough
411 if (BufferUtil.isEmpty(_encryptedInput) || _underFlown)
413 // We are not ready to read data
415 // Are we actually write blocked?
416 if (_fillRequiresFlushToProgress)
418 // we must be blocked trying to write before we can read
420 // Do we have data to write
421 if (BufferUtil.hasContent(_encryptedOutput))
424 _cannotAcceptMoreAppDataToFlush = true;
425 getEndPoint().write(_writeCallback, _encryptedOutput);
429 // we have already written the net data
430 // pretend we are readable so the wrap is done by next readable callback
431 _fillRequiresFlushToProgress = false;
437 // Normal readable callback
438 // Get called back on onfillable when then is more data to fill
439 SslConnection.this.fillInterested();
446 // We are ready to read data
453 public void setConnection(Connection connection)
455 if (connection instanceof AbstractConnection)
457 AbstractConnection a = (AbstractConnection)connection;
458 if (a.getInputBufferSize()<_sslEngine.getSession().getApplicationBufferSize())
459 a.setInputBufferSize(_sslEngine.getSession().getApplicationBufferSize());
461 super.setConnection(connection);
464 public SslConnection getSslConnection()
466 return SslConnection.this;
470 public synchronized int fill(ByteBuffer buffer) throws IOException
473 LOG.debug("{} fill enter", SslConnection.this);
476 // Do we already have some decrypted data?
477 if (BufferUtil.hasContent(_decryptedInput))
478 return BufferUtil.append(buffer,_decryptedInput);
480 // We will need a network buffer
481 if (_encryptedInput == null)
482 _encryptedInput = _bufferPool.acquire(_sslEngine.getSession().getPacketBufferSize(), _encryptedDirectBuffers);
484 BufferUtil.compact(_encryptedInput);
486 // We also need an app buffer, but can use the passed buffer if it is big enough
488 if (BufferUtil.space(buffer) > _sslEngine.getSession().getApplicationBufferSize())
490 else if (_decryptedInput == null)
491 app_in = _decryptedInput = _bufferPool.acquire(_sslEngine.getSession().getApplicationBufferSize(), _decryptedDirectBuffers);
493 app_in = _decryptedInput;
495 // loop filling and unwrapping until we have something
498 // Let's try reading some encrypted data... even if we have some already.
499 int net_filled = getEndPoint().fill(_encryptedInput);
501 LOG.debug("{} filled {} encrypted bytes", SslConnection.this, net_filled);
503 decryption: while (true)
505 // Let's unwrap even if we have no net data because in that
506 // case we want to fall through to the handshake handling
507 int pos = BufferUtil.flipToFill(app_in);
508 SSLEngineResult unwrapResult = _sslEngine.unwrap(_encryptedInput, app_in);
509 BufferUtil.flipToFlush(app_in, pos);
511 LOG.debug("{} unwrap {}", SslConnection.this, unwrapResult);
513 HandshakeStatus handshakeStatus = _sslEngine.getHandshakeStatus();
514 HandshakeStatus unwrapHandshakeStatus = unwrapResult.getHandshakeStatus();
515 Status unwrapResultStatus = unwrapResult.getStatus();
517 _underFlown = unwrapResultStatus == Status.BUFFER_UNDERFLOW;
527 switch (unwrapResultStatus)
531 switch (handshakeStatus)
533 case NOT_HANDSHAKING:
535 // We were not handshaking, so just tell the app we are closed
540 _sslEngine.getDelegatedTask().run();
545 // We need to send some handshake data (probably the close handshake).
546 // We return -1 so that the application can drive the close by flushing
547 // or shutting down the output.
552 throw new IllegalStateException();
556 case BUFFER_UNDERFLOW:
559 if (unwrapHandshakeStatus == HandshakeStatus.FINISHED && !_handshaken)
563 LOG.debug("{} {} handshake completed", SslConnection.this,
564 _sslEngine.getUseClientMode() ? "client-side" : "resumed session server-side");
567 // Check whether renegotiation is allowed
568 if (_handshaken && handshakeStatus != HandshakeStatus.NOT_HANDSHAKING && !isRenegotiationAllowed())
571 LOG.debug("{} renegotiation denied", SslConnection.this);
576 // If bytes were produced, don't bother with the handshake status;
577 // pass the decrypted data to the application, which will perform
578 // another call to fill() or flush().
579 if (unwrapResult.bytesProduced() > 0)
581 if (app_in == buffer)
582 return unwrapResult.bytesProduced();
583 return BufferUtil.append(buffer,_decryptedInput);
586 switch (handshakeStatus)
588 case NOT_HANDSHAKING:
596 _sslEngine.getDelegatedTask().run();
601 // If we are called from flush()
602 // return to let it do the wrapping.
603 if (buffer == __FLUSH_CALLED_FILL)
606 _fillRequiresFlushToProgress = true;
607 flush(__FILL_CALLED_FLUSH);
608 if (BufferUtil.isEmpty(_encryptedOutput))
610 // The flush wrote all the encrypted bytes so continue to fill
611 _fillRequiresFlushToProgress = false;
616 // The flush did not complete, return from fill()
617 // and let the write completion mechanism to kick in.
629 throw new IllegalStateException();
635 throw new IllegalStateException();
643 getEndPoint().close();
648 // If we are handshaking, then wake up any waiting write as well as it may have been blocked on the read
649 if (_flushRequiresFillToProgress)
651 _flushRequiresFillToProgress = false;
652 getExecutor().execute(_runCompletWrite);
655 if (_encryptedInput != null && !_encryptedInput.hasRemaining())
657 _bufferPool.release(_encryptedInput);
658 _encryptedInput = null;
660 if (_decryptedInput != null && !_decryptedInput.hasRemaining())
662 _bufferPool.release(_decryptedInput);
663 _decryptedInput = null;
666 LOG.debug("{} fill exit", SslConnection.this);
670 private void closeInbound()
674 _sslEngine.closeInbound();
676 catch (SSLException x)
683 public synchronized boolean flush(ByteBuffer... appOuts) throws IOException
685 // The contract for flush does not require that all appOuts bytes are written
686 // or even that any appOut bytes are written! If the connection is write block
687 // or busy handshaking, then zero bytes may be taken from appOuts and this method
688 // will return 0 (even if some handshake bytes were flushed and filled).
689 // it is the applications responsibility to call flush again - either in a busy loop
690 // or better yet by using EndPoint#write to do the flushing.
693 LOG.debug("{} flush enter {}", SslConnection.this, Arrays.toString(appOuts));
697 if (_cannotAcceptMoreAppDataToFlush)
699 if (_sslEngine.isOutboundDone())
700 throw new EofException(new ClosedChannelException());
704 // We will need a network buffer
705 if (_encryptedOutput == null)
706 _encryptedOutput = _bufferPool.acquire(_sslEngine.getSession().getPacketBufferSize(), _encryptedDirectBuffers);
710 // We call sslEngine.wrap to try to take bytes from appOut buffers and encrypt them into the _netOut buffer
711 BufferUtil.compact(_encryptedOutput);
712 int pos = BufferUtil.flipToFill(_encryptedOutput);
713 SSLEngineResult wrapResult = _sslEngine.wrap(appOuts, _encryptedOutput);
715 LOG.debug("{} wrap {}", SslConnection.this, wrapResult);
716 BufferUtil.flipToFlush(_encryptedOutput, pos);
717 if (wrapResult.bytesConsumed()>0)
718 consumed+=wrapResult.bytesConsumed();
720 boolean allConsumed=true;
721 // clear empty buffers to prevent position creeping up the buffer
722 for (ByteBuffer b : appOuts)
724 if (BufferUtil.isEmpty(b))
730 Status wrapResultStatus = wrapResult.getStatus();
732 // and deal with the results returned from the sslEngineWrap
733 switch (wrapResultStatus)
736 // The SSL engine has close, but there may be close handshake that needs to be written
737 if (BufferUtil.hasContent(_encryptedOutput))
739 _cannotAcceptMoreAppDataToFlush = true;
740 getEndPoint().flush(_encryptedOutput);
741 getEndPoint().shutdownOutput();
742 // If we failed to flush the close handshake then we will just pretend that
743 // the write has progressed normally and let a subsequent call to flush
744 // (or WriteFlusher#onIncompleteFlushed) to finish writing the close handshake.
745 // The caller will find out about the close on a subsequent flush or fill.
746 if (BufferUtil.hasContent(_encryptedOutput))
749 // otherwise we have written, and the caller will close the underlying connection
752 getEndPoint().shutdownOutput();
756 case BUFFER_UNDERFLOW:
757 throw new IllegalStateException();
761 LOG.debug("{} {} {}", this, wrapResultStatus, BufferUtil.toDetailString(_encryptedOutput));
763 if (wrapResult.getHandshakeStatus() == HandshakeStatus.FINISHED && !_handshaken)
767 LOG.debug("{} {} handshake completed", SslConnection.this, "server-side");
770 HandshakeStatus handshakeStatus = _sslEngine.getHandshakeStatus();
772 // Check whether renegotiation is allowed
773 if (_handshaken && handshakeStatus != HandshakeStatus.NOT_HANDSHAKING && !isRenegotiationAllowed())
776 LOG.debug("{} renegotiation denied", SslConnection.this);
781 // if we have net bytes, let's try to flush them
782 if (BufferUtil.hasContent(_encryptedOutput))
783 getEndPoint().flush(_encryptedOutput);
785 // But we also might have more to do for the handshaking state.
786 switch (handshakeStatus)
788 case NOT_HANDSHAKING:
789 // Return with the number of bytes consumed (which may be 0)
790 return allConsumed && BufferUtil.isEmpty(_encryptedOutput);
793 // run the task and continue
794 _sslEngine.getDelegatedTask().run();
798 // Hey we just wrapped! Oh well who knows what the sslEngine is thinking, so continue and we will wrap again
802 // Ah we need to fill some data so we can write.
803 // So if we were not called from fill and the app is not reading anyway
804 if (appOuts[0]!=__FILL_CALLED_FLUSH && !getFillInterest().isInterested())
806 // Tell the onFillable method that there might be a write to complete
807 _flushRequiresFillToProgress = true;
808 fill(__FLUSH_CALLED_FILL);
809 // Check if after the fill() we need to wrap again
810 if (handshakeStatus == HandshakeStatus.NEED_WRAP)
813 return allConsumed && BufferUtil.isEmpty(_encryptedOutput);
816 throw new IllegalStateException();
823 getEndPoint().close();
829 LOG.debug("{} flush exit, consumed {}", SslConnection.this, consumed);
830 releaseEncryptedOutputBuffer();
834 private void releaseEncryptedOutputBuffer()
836 if (!Thread.holdsLock(DecryptedEndPoint.this))
837 throw new IllegalStateException();
838 if (_encryptedOutput != null && !_encryptedOutput.hasRemaining())
840 _bufferPool.release(_encryptedOutput);
841 _encryptedOutput = null;
846 public void shutdownOutput()
848 boolean ishut = isInputShutdown();
849 boolean oshut = isOutputShutdown();
851 LOG.debug("{} shutdownOutput: oshut={}, ishut={}", SslConnection.this, oshut, ishut);
854 // Aggressively close, since inbound close alert has already been processed
855 // and the TLS specification allows to close the connection directly, which
856 // is what most other implementations expect: a FIN rather than a TLS close
857 // reply. If a TLS close reply is sent, most implementations send a RST.
858 getEndPoint().close();
864 _sslEngine.closeOutbound();
865 flush(BufferUtil.EMPTY_BUFFER); // Send close handshake
866 SslConnection.this.fillInterested(); // seek reply FIN or RST or close handshake
871 getEndPoint().close();
877 public boolean isOutputShutdown()
879 return _sslEngine.isOutboundDone() || getEndPoint().isOutputShutdown();
886 // First send the TLS Close Alert, then the FIN
888 getEndPoint().close();
892 public boolean isOpen()
894 return getEndPoint().isOpen();
898 public Object getTransport()
900 return getEndPoint();
904 public boolean isInputShutdown()
906 return _sslEngine.isInboundDone();
910 public String toString()
912 return super.toString()+"->"+getEndPoint().toString();