1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.eclipse.jetty.io.ssl;
20
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;
26 import javax.net.ssl.SSLEngine;
27 import javax.net.ssl.SSLEngineResult;
28 import javax.net.ssl.SSLEngineResult.HandshakeStatus;
29 import javax.net.ssl.SSLEngineResult.Status;
30 import javax.net.ssl.SSLException;
31
32 import org.eclipse.jetty.io.AbstractConnection;
33 import org.eclipse.jetty.io.AbstractEndPoint;
34 import org.eclipse.jetty.io.ByteBufferPool;
35 import org.eclipse.jetty.io.Connection;
36 import org.eclipse.jetty.io.EndPoint;
37 import org.eclipse.jetty.io.EofException;
38 import org.eclipse.jetty.io.FillInterest;
39 import org.eclipse.jetty.io.RuntimeIOException;
40 import org.eclipse.jetty.io.SelectChannelEndPoint;
41 import org.eclipse.jetty.io.WriteFlusher;
42 import org.eclipse.jetty.util.BufferUtil;
43 import org.eclipse.jetty.util.Callback;
44 import org.eclipse.jetty.util.log.Log;
45 import org.eclipse.jetty.util.log.Logger;
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78 public class SslConnection extends AbstractConnection
79 {
80 private static final Logger LOG = Log.getLogger(SslConnection.class);
81 private static final boolean DEBUG = LOG.isDebugEnabled();
82 private static final ByteBuffer __FILL_CALLED_FLUSH= BufferUtil.allocate(0);
83 private static final ByteBuffer __FLUSH_CALLED_FILL= BufferUtil.allocate(0);
84 private final ByteBufferPool _bufferPool;
85 private final SSLEngine _sslEngine;
86 private final DecryptedEndPoint _decryptedEndPoint;
87 private ByteBuffer _decryptedInput;
88 private ByteBuffer _encryptedInput;
89 private ByteBuffer _encryptedOutput;
90 private final boolean _encryptedDirectBuffers = false;
91 private final boolean _decryptedDirectBuffers = false;
92 private final Runnable _runCompletWrite = new Runnable()
93 {
94 @Override
95 public void run()
96 {
97 _decryptedEndPoint.getWriteFlusher().completeWrite();
98 }
99 };
100 private boolean _renegotiationAllowed;
101
102 public SslConnection(ByteBufferPool byteBufferPool, Executor executor, EndPoint endPoint, SSLEngine sslEngine)
103 {
104
105
106 super(endPoint, executor, !EXECUTE_ONFILLABLE);
107 this._bufferPool = byteBufferPool;
108 this._sslEngine = sslEngine;
109 this._decryptedEndPoint = newDecryptedEndPoint();
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125 }
126
127 protected DecryptedEndPoint newDecryptedEndPoint()
128 {
129 return new DecryptedEndPoint();
130 }
131
132 public SSLEngine getSSLEngine()
133 {
134 return _sslEngine;
135 }
136
137 public DecryptedEndPoint getDecryptedEndPoint()
138 {
139 return _decryptedEndPoint;
140 }
141
142 public boolean isRenegotiationAllowed()
143 {
144 return _renegotiationAllowed;
145 }
146
147 public void setRenegotiationAllowed(boolean renegotiationAllowed)
148 {
149 this._renegotiationAllowed = renegotiationAllowed;
150 }
151
152 @Override
153 public void onOpen()
154 {
155 try
156 {
157
158 _sslEngine.beginHandshake();
159 super.onOpen();
160 getDecryptedEndPoint().getConnection().onOpen();
161 }
162 catch (SSLException x)
163 {
164 getEndPoint().close();
165 throw new RuntimeIOException(x);
166 }
167 }
168
169 @Override
170 public void onClose()
171 {
172 _decryptedEndPoint.getConnection().onClose();
173 super.onClose();
174 }
175
176 @Override
177 public void close()
178 {
179 getDecryptedEndPoint().getConnection().close();
180 }
181
182 @Override
183 public void onFillable()
184 {
185
186
187
188
189
190
191 if (DEBUG)
192 LOG.debug("onFillable enter {}", _decryptedEndPoint);
193
194
195 if (_decryptedEndPoint.isInputShutdown())
196 _decryptedEndPoint.close();
197
198
199
200 _decryptedEndPoint.getFillInterest().fillable();
201
202
203 synchronized(_decryptedEndPoint)
204 {
205 if (_decryptedEndPoint._flushRequiresFillToProgress)
206 {
207 _decryptedEndPoint._flushRequiresFillToProgress = false;
208 getExecutor().execute(_runCompletWrite);
209 }
210 }
211
212 if (DEBUG)
213 LOG.debug("onFillable exit {}", _decryptedEndPoint);
214 }
215
216 @Override
217 public void onFillInterestedFailed(Throwable cause)
218 {
219
220
221
222
223
224 _decryptedEndPoint.getFillInterest().onFail(cause);
225
226 boolean failFlusher = false;
227 synchronized(_decryptedEndPoint)
228 {
229 if (_decryptedEndPoint._flushRequiresFillToProgress)
230 {
231 _decryptedEndPoint._flushRequiresFillToProgress = false;
232 failFlusher = true;
233 }
234 }
235 if (failFlusher)
236 _decryptedEndPoint.getWriteFlusher().onFail(cause);
237 }
238
239 @Override
240 public String toString()
241 {
242 ByteBuffer b = _encryptedInput;
243 int ei=b==null?-1:b.remaining();
244 b = _encryptedOutput;
245 int eo=b==null?-1:b.remaining();
246 b = _decryptedInput;
247 int di=b==null?-1:b.remaining();
248
249 return String.format("SslConnection@%x{%s,eio=%d/%d,di=%d} -> %s",
250 hashCode(),
251 _sslEngine.getHandshakeStatus(),
252 ei,eo,di,
253 _decryptedEndPoint.getConnection());
254 }
255
256 public class DecryptedEndPoint extends AbstractEndPoint
257 {
258 private boolean _fillRequiresFlushToProgress;
259 private boolean _flushRequiresFillToProgress;
260 private boolean _cannotAcceptMoreAppDataToFlush;
261 private boolean _handshaken;
262 private boolean _underFlown;
263
264 private final Callback _writeCallback = new Callback()
265 {
266 @Override
267 public void succeeded()
268 {
269
270
271
272 boolean fillable = false;
273 synchronized (DecryptedEndPoint.this)
274 {
275 if (DEBUG)
276 LOG.debug("write.complete {}", SslConnection.this.getEndPoint());
277
278 releaseEncryptedOutputBuffer();
279
280 _cannotAcceptMoreAppDataToFlush = false;
281
282 if (_fillRequiresFlushToProgress)
283 {
284 _fillRequiresFlushToProgress = false;
285 fillable = true;
286 }
287 }
288 if (fillable)
289 getFillInterest().fillable();
290 getExecutor().execute(_runCompletWrite);
291 }
292
293 @Override
294 public void failed(final Throwable x)
295 {
296
297
298
299 boolean fail_filler = false;
300 synchronized (DecryptedEndPoint.this)
301 {
302 if (DEBUG)
303 LOG.debug("{} write.failed", SslConnection.this, x);
304 BufferUtil.clear(_encryptedOutput);
305 releaseEncryptedOutputBuffer();
306
307 _cannotAcceptMoreAppDataToFlush = false;
308
309 if (_fillRequiresFlushToProgress)
310 {
311 _fillRequiresFlushToProgress = false;
312 fail_filler = true;
313 }
314 }
315
316 final boolean filler_failed=fail_filler;
317
318 getExecutor().execute(new Runnable()
319 {
320 @Override
321 public void run()
322 {
323
324 if (filler_failed)
325 getFillInterest().onFail(x);
326 getWriteFlusher().onFail(x);
327 }
328 });
329 }
330 };
331
332 public DecryptedEndPoint()
333 {
334 super(null,getEndPoint().getLocalAddress(), getEndPoint().getRemoteAddress());
335 setIdleTimeout(getEndPoint().getIdleTimeout());
336 }
337
338 @Override
339 protected FillInterest getFillInterest()
340 {
341 return super.getFillInterest();
342 }
343
344 @Override
345 public void setIdleTimeout(long idleTimeout)
346 {
347 super.setIdleTimeout(idleTimeout);
348 getEndPoint().setIdleTimeout(idleTimeout);
349 }
350
351 @Override
352 protected WriteFlusher getWriteFlusher()
353 {
354 return super.getWriteFlusher();
355 }
356
357 @Override
358 protected void onIncompleteFlush()
359 {
360
361
362
363
364 boolean flush = false;
365 synchronized (DecryptedEndPoint.this)
366 {
367 if (DEBUG)
368 LOG.debug("onIncompleteFlush {}", getEndPoint());
369
370 if (BufferUtil.hasContent(_encryptedOutput))
371 {
372
373 _cannotAcceptMoreAppDataToFlush = true;
374 getEndPoint().write(_writeCallback, _encryptedOutput);
375 }
376
377 else if (_sslEngine.getHandshakeStatus() == HandshakeStatus.NEED_UNWRAP)
378 {
379
380 _flushRequiresFillToProgress = true;
381 SslConnection.this.fillInterested();
382 }
383 else
384 {
385 flush = true;
386 }
387 }
388 if (flush)
389 {
390
391 if (isOutputShutdown())
392 {
393
394 getWriteFlusher().onClose();
395 }
396
397 else
398 {
399
400 getWriteFlusher().completeWrite();
401 }
402 }
403 }
404
405 @Override
406 protected boolean needsFill() throws IOException
407 {
408
409
410
411
412
413 synchronized (DecryptedEndPoint.this)
414 {
415
416 if (BufferUtil.hasContent(_decryptedInput))
417 return true;
418
419
420 if (BufferUtil.isEmpty(_encryptedInput) || _underFlown)
421 {
422
423
424
425 if (_fillRequiresFlushToProgress)
426 {
427
428
429
430 if (BufferUtil.hasContent(_encryptedOutput))
431 {
432
433 _cannotAcceptMoreAppDataToFlush = true;
434 getEndPoint().write(_writeCallback, _encryptedOutput);
435 }
436 else
437 {
438
439
440 _fillRequiresFlushToProgress = false;
441 return true;
442 }
443 }
444 else
445 {
446
447
448 SslConnection.this.fillInterested();
449 }
450
451 return false;
452 }
453 else
454 {
455
456 return true;
457 }
458 }
459 }
460
461 @Override
462 public void setConnection(Connection connection)
463 {
464 if (connection instanceof AbstractConnection)
465 {
466 AbstractConnection a = (AbstractConnection)connection;
467 if (a.getInputBufferSize()<_sslEngine.getSession().getApplicationBufferSize())
468 a.setInputBufferSize(_sslEngine.getSession().getApplicationBufferSize());
469 }
470 super.setConnection(connection);
471 }
472
473 public SslConnection getSslConnection()
474 {
475 return SslConnection.this;
476 }
477
478 @Override
479 public synchronized int fill(ByteBuffer buffer) throws IOException
480 {
481 if (DEBUG)
482 LOG.debug("{} fill enter", SslConnection.this);
483 try
484 {
485
486 if (BufferUtil.hasContent(_decryptedInput))
487 return BufferUtil.flipPutFlip(_decryptedInput, buffer);
488
489
490 if (_encryptedInput == null)
491 _encryptedInput = _bufferPool.acquire(_sslEngine.getSession().getPacketBufferSize(), _encryptedDirectBuffers);
492 else
493 BufferUtil.compact(_encryptedInput);
494
495
496 ByteBuffer app_in;
497 if (BufferUtil.space(buffer) > _sslEngine.getSession().getApplicationBufferSize())
498 app_in = buffer;
499 else if (_decryptedInput == null)
500 app_in = _decryptedInput = _bufferPool.acquire(_sslEngine.getSession().getApplicationBufferSize(), _decryptedDirectBuffers);
501 else
502 app_in = _decryptedInput;
503
504
505 while (true)
506 {
507
508 int net_filled = getEndPoint().fill(_encryptedInput);
509 if (DEBUG)
510 LOG.debug("{} filled {} encrypted bytes", SslConnection.this, net_filled);
511
512 decryption: while (true)
513 {
514
515
516 int pos = BufferUtil.flipToFill(app_in);
517 SSLEngineResult unwrapResult = _sslEngine.unwrap(_encryptedInput, app_in);
518 BufferUtil.flipToFlush(app_in, pos);
519 if (DEBUG)
520 LOG.debug("{} unwrap {}", SslConnection.this, unwrapResult);
521
522 HandshakeStatus handshakeStatus = _sslEngine.getHandshakeStatus();
523 HandshakeStatus unwrapHandshakeStatus = unwrapResult.getHandshakeStatus();
524 Status unwrapResultStatus = unwrapResult.getStatus();
525
526 _underFlown = unwrapResultStatus == Status.BUFFER_UNDERFLOW;
527
528 if (_underFlown)
529 {
530 if (net_filled < 0)
531 closeInbound();
532 if (net_filled <= 0)
533 return net_filled;
534 }
535
536 switch (unwrapResultStatus)
537 {
538 case CLOSED:
539 {
540 switch (handshakeStatus)
541 {
542 case NOT_HANDSHAKING:
543 {
544
545 return -1;
546 }
547 case NEED_TASK:
548 {
549 _sslEngine.getDelegatedTask().run();
550 continue;
551 }
552 case NEED_WRAP:
553 {
554
555
556
557 return -1;
558 }
559 default:
560 {
561 throw new IllegalStateException();
562 }
563 }
564 }
565 case BUFFER_UNDERFLOW:
566 case OK:
567 {
568 if (unwrapHandshakeStatus == HandshakeStatus.FINISHED && !_handshaken)
569 {
570 _handshaken = true;
571 if (DEBUG)
572 LOG.debug("{} {} handshake completed", SslConnection.this,
573 _sslEngine.getUseClientMode() ? "client-side" : "resumed session server-side");
574 }
575
576
577 if (_handshaken && handshakeStatus != HandshakeStatus.NOT_HANDSHAKING && !isRenegotiationAllowed())
578 {
579 if (DEBUG)
580 LOG.debug("{} renegotiation denied", SslConnection.this);
581 closeInbound();
582 return -1;
583 }
584
585
586
587
588 if (unwrapResult.bytesProduced() > 0)
589 {
590 if (app_in == buffer)
591 return unwrapResult.bytesProduced();
592 return BufferUtil.flipPutFlip(_decryptedInput, buffer);
593 }
594
595 switch (handshakeStatus)
596 {
597 case NOT_HANDSHAKING:
598 {
599 if (_underFlown)
600 break decryption;
601 continue;
602 }
603 case NEED_TASK:
604 {
605 _sslEngine.getDelegatedTask().run();
606 continue;
607 }
608 case NEED_WRAP:
609 {
610
611
612 if (buffer == __FLUSH_CALLED_FILL)
613 return 0;
614
615 _fillRequiresFlushToProgress = true;
616 flush(__FILL_CALLED_FLUSH);
617 if (BufferUtil.isEmpty(_encryptedOutput))
618 {
619
620 _fillRequiresFlushToProgress = false;
621 continue;
622 }
623 else
624 {
625
626
627 return 0;
628 }
629 }
630 case NEED_UNWRAP:
631 {
632 if (_underFlown)
633 break decryption;
634 continue;
635 }
636 default:
637 {
638 throw new IllegalStateException();
639 }
640 }
641 }
642 default:
643 {
644 throw new IllegalStateException();
645 }
646 }
647 }
648 }
649 }
650 catch (SSLException e)
651 {
652 getEndPoint().close();
653 throw new EofException(e);
654 }
655 catch (Exception e)
656 {
657 getEndPoint().close();
658 throw e;
659 }
660 finally
661 {
662
663 if (_flushRequiresFillToProgress)
664 {
665 _flushRequiresFillToProgress = false;
666 getExecutor().execute(_runCompletWrite);
667 }
668
669 if (_encryptedInput != null && !_encryptedInput.hasRemaining())
670 {
671 _bufferPool.release(_encryptedInput);
672 _encryptedInput = null;
673 }
674 if (_decryptedInput != null && !_decryptedInput.hasRemaining())
675 {
676 _bufferPool.release(_decryptedInput);
677 _decryptedInput = null;
678 }
679 if (DEBUG)
680 LOG.debug("{} fill exit", SslConnection.this);
681 }
682 }
683
684 private void closeInbound()
685 {
686 try
687 {
688 _sslEngine.closeInbound();
689 }
690 catch (SSLException x)
691 {
692 LOG.ignore(x);
693 }
694 }
695
696 @Override
697 public synchronized boolean flush(ByteBuffer... appOuts) throws IOException
698 {
699
700
701
702
703
704
705
706 if (DEBUG)
707 LOG.debug("{} flush enter {}", SslConnection.this, Arrays.toString(appOuts));
708 int consumed=0;
709 try
710 {
711 if (_cannotAcceptMoreAppDataToFlush)
712 {
713 if (_sslEngine.isOutboundDone())
714 throw new EofException(new ClosedChannelException());
715 return false;
716 }
717
718
719 if (_encryptedOutput == null)
720 _encryptedOutput = _bufferPool.acquire(_sslEngine.getSession().getPacketBufferSize(), _encryptedDirectBuffers);
721
722 while (true)
723 {
724
725 BufferUtil.compact(_encryptedOutput);
726 int pos = BufferUtil.flipToFill(_encryptedOutput);
727 SSLEngineResult wrapResult = _sslEngine.wrap(appOuts, _encryptedOutput);
728 if (DEBUG)
729 LOG.debug("{} wrap {}", SslConnection.this, wrapResult);
730 BufferUtil.flipToFlush(_encryptedOutput, pos);
731 if (wrapResult.bytesConsumed()>0)
732 consumed+=wrapResult.bytesConsumed();
733
734 boolean allConsumed=true;
735
736 for (ByteBuffer b : appOuts)
737 {
738 if (BufferUtil.isEmpty(b))
739 BufferUtil.clear(b);
740 else
741 allConsumed=false;
742 }
743
744 Status wrapResultStatus = wrapResult.getStatus();
745
746
747 switch (wrapResultStatus)
748 {
749 case CLOSED:
750
751 if (BufferUtil.hasContent(_encryptedOutput))
752 {
753 _cannotAcceptMoreAppDataToFlush = true;
754 getEndPoint().flush(_encryptedOutput);
755 getEndPoint().shutdownOutput();
756
757
758
759
760 if (BufferUtil.hasContent(_encryptedOutput))
761 return false;
762 }
763
764 else
765 {
766 getEndPoint().shutdownOutput();
767 }
768 return allConsumed;
769
770 case BUFFER_UNDERFLOW:
771 throw new IllegalStateException();
772
773 default:
774 if (DEBUG)
775 LOG.debug("{} {} {}", this, wrapResultStatus, BufferUtil.toDetailString(_encryptedOutput));
776
777 if (wrapResult.getHandshakeStatus() == HandshakeStatus.FINISHED && !_handshaken)
778 {
779 _handshaken = true;
780 if (DEBUG)
781 LOG.debug("{} {} handshake completed", SslConnection.this, "server-side");
782 }
783
784 HandshakeStatus handshakeStatus = _sslEngine.getHandshakeStatus();
785
786
787 if (_handshaken && handshakeStatus != HandshakeStatus.NOT_HANDSHAKING && !isRenegotiationAllowed())
788 {
789 if (DEBUG)
790 LOG.debug("{} renegotiation denied", SslConnection.this);
791 shutdownOutput();
792 return allConsumed;
793 }
794
795
796 if (BufferUtil.hasContent(_encryptedOutput))
797 getEndPoint().flush(_encryptedOutput);
798
799
800 switch (handshakeStatus)
801 {
802 case NOT_HANDSHAKING:
803
804 return allConsumed && BufferUtil.isEmpty(_encryptedOutput);
805
806 case NEED_TASK:
807
808 _sslEngine.getDelegatedTask().run();
809 continue;
810
811 case NEED_WRAP:
812
813 continue;
814
815 case NEED_UNWRAP:
816
817
818 if (appOuts[0]!=__FILL_CALLED_FLUSH && !getFillInterest().isInterested())
819 {
820
821 _flushRequiresFillToProgress = true;
822 fill(__FLUSH_CALLED_FILL);
823
824 if (handshakeStatus == HandshakeStatus.NEED_WRAP)
825 continue;
826 }
827 return allConsumed && BufferUtil.isEmpty(_encryptedOutput);
828
829 case FINISHED:
830 throw new IllegalStateException();
831 }
832 }
833 }
834 }
835 catch (Exception e)
836 {
837 getEndPoint().close();
838 throw e;
839 }
840 finally
841 {
842 if (DEBUG)
843 LOG.debug("{} flush exit, consumed {}", SslConnection.this, consumed);
844 releaseEncryptedOutputBuffer();
845 }
846 }
847
848 private void releaseEncryptedOutputBuffer()
849 {
850 if (!Thread.holdsLock(DecryptedEndPoint.this))
851 throw new IllegalStateException();
852 if (_encryptedOutput != null && !_encryptedOutput.hasRemaining())
853 {
854 _bufferPool.release(_encryptedOutput);
855 _encryptedOutput = null;
856 }
857 }
858
859 @Override
860 public void shutdownOutput()
861 {
862 boolean ishut = isInputShutdown();
863 boolean oshut = isOutputShutdown();
864 if (DEBUG)
865 LOG.debug("{} shutdownOutput: oshut={}, ishut={}", SslConnection.this, oshut, ishut);
866 if (ishut)
867 {
868
869
870
871
872 getEndPoint().close();
873 }
874 else if (!oshut)
875 {
876 try
877 {
878 _sslEngine.closeOutbound();
879 flush(BufferUtil.EMPTY_BUFFER);
880 SslConnection.this.fillInterested();
881 }
882 catch (Exception e)
883 {
884 LOG.ignore(e);
885 getEndPoint().close();
886 }
887 }
888 }
889
890 @Override
891 public boolean isOutputShutdown()
892 {
893 return _sslEngine.isOutboundDone() || getEndPoint().isOutputShutdown();
894 }
895
896 @Override
897 public void close()
898 {
899
900 shutdownOutput();
901 getEndPoint().close();
902 }
903
904 @Override
905 public boolean isOpen()
906 {
907 return getEndPoint().isOpen();
908 }
909
910 @Override
911 public Object getTransport()
912 {
913 return getEndPoint();
914 }
915
916 @Override
917 public boolean isInputShutdown()
918 {
919 return _sslEngine.isInboundDone();
920 }
921
922 @Override
923 public String toString()
924 {
925 return super.toString()+"->"+getEndPoint().toString();
926 }
927 }
928 }