28
28
package org .jruby .ext .openssl ;
29
29
30
30
import java .io .IOException ;
31
- import java .net .Socket ;
32
31
import java .nio .ByteBuffer ;
33
32
import java .nio .channels .Channel ;
34
33
import java .nio .channels .ClosedChannelException ;
@@ -145,14 +144,14 @@ private static CallSite callSite(final CallSite[] sites, final CallSiteIndex ind
145
144
private SSLEngine engine ;
146
145
private RubyIO io ;
147
146
148
- private ByteBuffer peerAppData ;
149
- private ByteBuffer peerNetData ;
150
- private ByteBuffer netData ;
151
- private ByteBuffer dummy ;
147
+ private ByteBuffer appReadData ;
148
+ private ByteBuffer netReadData ;
149
+ private ByteBuffer netWriteData ;
150
+ private final ByteBuffer dummy = ByteBuffer . allocate ( 0 ); // could be static
152
151
153
152
private boolean initialHandshake = false ;
154
153
155
- private SSLEngineResult .HandshakeStatus handshakeStatus ;
154
+ private SSLEngineResult .HandshakeStatus handshakeStatus ; // != null after hand-shake starts
156
155
private SSLEngineResult .Status status ;
157
156
158
157
int verifyResult = X509Utils .V_OK ;
@@ -212,13 +211,13 @@ private SSLEngine ossl_ssl_setup(final ThreadContext context, final boolean serv
212
211
engine = sslContext .createSSLEngine (peerHost , peerPort );
213
212
214
213
final javax .net .ssl .SSLSession session = engine .getSession ();
215
- peerNetData = ByteBuffer .allocate (session .getPacketBufferSize ());
216
- peerAppData = ByteBuffer .allocate (session .getApplicationBufferSize ());
217
- netData = ByteBuffer .allocate (session .getPacketBufferSize ());
218
- peerNetData .limit (0 );
219
- peerAppData .limit (0 );
220
- netData .limit (0 );
221
- dummy = ByteBuffer . allocate ( 0 );
214
+ netReadData = ByteBuffer .allocate (session .getPacketBufferSize ());
215
+ appReadData = ByteBuffer .allocate (session .getApplicationBufferSize ());
216
+ netWriteData = ByteBuffer .allocate (session .getPacketBufferSize ());
217
+ netReadData .limit (0 );
218
+ appReadData .limit (0 );
219
+ netWriteData .limit (0 );
220
+
222
221
this .engine = engine ;
223
222
copySessionSetupIfSet (context );
224
223
@@ -561,7 +560,6 @@ private IRubyObject doHandshake(final boolean blocking, final boolean exception)
561
560
}
562
561
563
562
// otherwise, proceed as before
564
-
565
563
switch (handshakeStatus ) {
566
564
case FINISHED :
567
565
case NOT_HANDSHAKING :
@@ -582,16 +580,16 @@ private IRubyObject doHandshake(final boolean blocking, final boolean exception)
582
580
}
583
581
break ;
584
582
case NEED_WRAP :
585
- if ( netData .hasRemaining () ) {
583
+ if ( netWriteData .hasRemaining () ) {
586
584
while ( flushData (blocking ) ) { /* loop */ }
587
585
}
588
- assert !netData .hasRemaining ();
586
+ assert !netWriteData .hasRemaining ();
589
587
doWrap (blocking );
590
588
flushData (blocking );
591
589
assert status != SSLEngineResult .Status .BUFFER_UNDERFLOW ;
592
590
if (status == SSLEngineResult .Status .BUFFER_OVERFLOW ) {
593
- netData .compact ();
594
- netData .flip ();
591
+ netWriteData .compact ();
592
+ netWriteData .flip ();
595
593
if (handshakeStatus != SSLEngineResult .HandshakeStatus .NEED_UNWRAP || flushData (blocking )) {
596
594
sel = waitSelect (SelectionKey .OP_WRITE , blocking , exception );
597
595
if ( sel instanceof IRubyObject ) return (IRubyObject ) sel ; // :wait_writeable
@@ -605,9 +603,9 @@ private IRubyObject doHandshake(final boolean blocking, final boolean exception)
605
603
}
606
604
607
605
private void doWrap (boolean blocking ) throws IOException {
608
- netData .clear ();
609
- SSLEngineResult result = engine .wrap (dummy , netData );
610
- netData .flip ();
606
+ netWriteData .clear ();
607
+ SSLEngineResult result = engine .wrap (dummy , netWriteData );
608
+ netWriteData .flip ();
611
609
handshakeStatus = result .getHandshakeStatus ();
612
610
status = result .getStatus ();
613
611
if (handshakeStatus == SSLEngineResult .HandshakeStatus .NEED_TASK
@@ -627,13 +625,13 @@ private void doTasks() {
627
625
628
626
private boolean flushData (boolean blocking ) throws IOException {
629
627
try {
630
- writeToChannel (netData , blocking );
628
+ writeToChannel (netWriteData , blocking );
631
629
}
632
630
catch (IOException ioe ) {
633
- netData .position (netData .limit ());
631
+ netWriteData .position (netWriteData .limit ());
634
632
throw ioe ;
635
633
}
636
- return netData .hasRemaining ();
634
+ return netWriteData .hasRemaining ();
637
635
}
638
636
639
637
private int writeToChannel (ByteBuffer buffer , boolean blocking ) throws IOException {
@@ -671,15 +669,15 @@ public int write(ByteBuffer src, boolean blocking) throws SSLException, IOExcept
671
669
if ( ! blocking ) channel .configureBlocking (false );
672
670
673
671
try {
674
- if ( netData .hasRemaining () ) {
672
+ if ( netWriteData .hasRemaining () ) {
675
673
flushData (blocking );
676
674
}
677
- netData .clear ();
678
- final SSLEngineResult result = engine .wrap (src , netData );
675
+ netWriteData .clear ();
676
+ final SSLEngineResult result = engine .wrap (src , netWriteData );
679
677
if ( result .getStatus () == SSLEngineResult .Status .CLOSED ) {
680
678
throw getRuntime ().newIOError ("closed SSL engine" );
681
679
}
682
- netData .flip ();
680
+ netWriteData .flip ();
683
681
flushData (blocking );
684
682
return result .bytesConsumed ();
685
683
}
@@ -692,22 +690,22 @@ public int read(final ByteBuffer dst, final boolean blocking) throws IOException
692
690
if ( initialHandshake ) return 0 ;
693
691
if ( engine .isInboundDone () ) return -1 ;
694
692
695
- if ( ! peerAppData .hasRemaining () ) {
693
+ if ( ! appReadData .hasRemaining () ) {
696
694
int appBytesProduced = readAndUnwrap (blocking );
697
695
if (appBytesProduced == -1 || appBytesProduced == 0 ) {
698
696
return appBytesProduced ;
699
697
}
700
698
}
701
- int limit = Math .min (peerAppData .remaining (), dst .remaining ());
702
- peerAppData .get (dst .array (), dst .arrayOffset (), limit );
699
+ int limit = Math .min (appReadData .remaining (), dst .remaining ());
700
+ appReadData .get (dst .array (), dst .arrayOffset (), limit );
703
701
dst .position (dst .arrayOffset () + limit );
704
702
return limit ;
705
703
}
706
704
707
705
private int readAndUnwrap (final boolean blocking ) throws IOException {
708
- final int bytesRead = socketChannelImpl ().read (peerNetData );
706
+ final int bytesRead = socketChannelImpl ().read (netReadData );
709
707
if ( bytesRead == -1 ) {
710
- if ( ! peerNetData .hasRemaining () ||
708
+ if ( ! netReadData .hasRemaining () ||
711
709
( status == SSLEngineResult .Status .BUFFER_UNDERFLOW ) ) {
712
710
closeInbound ();
713
711
return -1 ;
@@ -716,12 +714,12 @@ private int readAndUnwrap(final boolean blocking) throws IOException {
716
714
// be defered till the last engine.unwrap() call.
717
715
// peerNetData could not be empty.
718
716
}
719
- peerAppData .clear ();
720
- peerNetData .flip ();
717
+ appReadData .clear ();
718
+ netReadData .flip ();
721
719
722
720
SSLEngineResult result ;
723
721
do {
724
- result = engine .unwrap (peerNetData , peerAppData );
722
+ result = engine .unwrap (netReadData , appReadData );
725
723
}
726
724
while ( result .getStatus () == SSLEngineResult .Status .OK &&
727
725
result .getHandshakeStatus () == SSLEngineResult .HandshakeStatus .NEED_UNWRAP &&
@@ -730,15 +728,15 @@ private int readAndUnwrap(final boolean blocking) throws IOException {
730
728
if ( result .getHandshakeStatus () == SSLEngineResult .HandshakeStatus .FINISHED ) {
731
729
finishInitialHandshake ();
732
730
}
733
- if ( peerAppData .position () == 0 &&
731
+ if ( appReadData .position () == 0 &&
734
732
result .getStatus () == SSLEngineResult .Status .OK &&
735
- peerNetData .hasRemaining () ) {
736
- result = engine .unwrap (peerNetData , peerAppData );
733
+ netReadData .hasRemaining () ) {
734
+ result = engine .unwrap (netReadData , appReadData );
737
735
}
738
736
status = result .getStatus ();
739
737
handshakeStatus = result .getHandshakeStatus ();
740
738
741
- if ( bytesRead == -1 && ! peerNetData .hasRemaining () ) {
739
+ if ( bytesRead == -1 && ! netReadData .hasRemaining () ) {
742
740
// now it's safe to call closeInbound().
743
741
closeInbound ();
744
742
}
@@ -747,15 +745,15 @@ private int readAndUnwrap(final boolean blocking) throws IOException {
747
745
return -1 ;
748
746
}
749
747
750
- peerNetData .compact ();
751
- peerAppData .flip ();
748
+ netReadData .compact ();
749
+ appReadData .flip ();
752
750
if ( ! initialHandshake && (
753
751
handshakeStatus == SSLEngineResult .HandshakeStatus .NEED_TASK ||
754
752
handshakeStatus == SSLEngineResult .HandshakeStatus .NEED_WRAP ||
755
753
handshakeStatus == SSLEngineResult .HandshakeStatus .FINISHED ) ) {
756
754
doHandshake (blocking );
757
755
}
758
- return peerAppData .remaining ();
756
+ return appReadData .remaining ();
759
757
}
760
758
761
759
private void closeInbound () {
@@ -776,9 +774,9 @@ private void doShutdown() throws IOException {
776
774
debug (getRuntime (), "SSLSocket.doShutdown data in the data buffer - can't send close" );
777
775
return ;
778
776
}
779
- netData .clear ();
777
+ netWriteData .clear ();
780
778
try {
781
- engine .wrap (dummy , netData ); // send close (after sslEngine.closeOutbound)
779
+ engine .wrap (dummy , netWriteData ); // send close (after sslEngine.closeOutbound)
782
780
}
783
781
catch (SSLException e ) {
784
782
debug (getRuntime (), "SSLSocket.doShutdown" , e );
@@ -788,7 +786,7 @@ private void doShutdown() throws IOException {
788
786
debugStackTrace (getRuntime (), "SSLSocket.doShutdown" , e );
789
787
return ;
790
788
}
791
- netData .flip ();
789
+ netWriteData .flip ();
792
790
flushData (true );
793
791
}
794
792
@@ -814,7 +812,7 @@ private IRubyObject sysreadImpl(final ThreadContext context,
814
812
815
813
try {
816
814
// So we need to make sure to only block when there is no data left to process
817
- if ( engine == null || ! ( peerAppData .hasRemaining () || peerNetData .position () > 0 ) ) {
815
+ if ( engine == null || ! ( appReadData .hasRemaining () || netReadData .position () > 0 ) ) {
818
816
final Object ex = waitSelect (SelectionKey .OP_READ , blocking , exception );
819
817
if ( ex instanceof IRubyObject ) return (IRubyObject ) ex ; // :wait_readable
820
818
}
@@ -992,7 +990,7 @@ private void close(boolean force) {
992
990
993
991
engine .closeOutbound ();
994
992
995
- if ( ! force && netData .hasRemaining () ) return ;
993
+ if ( ! force && netWriteData .hasRemaining () ) return ;
996
994
997
995
try {
998
996
doShutdown ();
0 commit comments