2828package org .jruby .ext .openssl ;
2929
3030import java .io .IOException ;
31- import java .net .Socket ;
3231import java .nio .ByteBuffer ;
3332import java .nio .channels .Channel ;
3433import java .nio .channels .ClosedChannelException ;
@@ -145,14 +144,14 @@ private static CallSite callSite(final CallSite[] sites, final CallSiteIndex ind
145144 private SSLEngine engine ;
146145 private RubyIO io ;
147146
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
152151
153152 private boolean initialHandshake = false ;
154153
155- private SSLEngineResult .HandshakeStatus handshakeStatus ;
154+ private SSLEngineResult .HandshakeStatus handshakeStatus ; // != null after hand-shake starts
156155 private SSLEngineResult .Status status ;
157156
158157 int verifyResult = X509Utils .V_OK ;
@@ -212,13 +211,13 @@ private SSLEngine ossl_ssl_setup(final ThreadContext context, final boolean serv
212211 engine = sslContext .createSSLEngine (peerHost , peerPort );
213212
214213 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+
222221 this .engine = engine ;
223222 copySessionSetupIfSet (context );
224223
@@ -561,7 +560,6 @@ private IRubyObject doHandshake(final boolean blocking, final boolean exception)
561560 }
562561
563562 // otherwise, proceed as before
564-
565563 switch (handshakeStatus ) {
566564 case FINISHED :
567565 case NOT_HANDSHAKING :
@@ -582,16 +580,16 @@ private IRubyObject doHandshake(final boolean blocking, final boolean exception)
582580 }
583581 break ;
584582 case NEED_WRAP :
585- if ( netData .hasRemaining () ) {
583+ if ( netWriteData .hasRemaining () ) {
586584 while ( flushData (blocking ) ) { /* loop */ }
587585 }
588- assert !netData .hasRemaining ();
586+ assert !netWriteData .hasRemaining ();
589587 doWrap (blocking );
590588 flushData (blocking );
591589 assert status != SSLEngineResult .Status .BUFFER_UNDERFLOW ;
592590 if (status == SSLEngineResult .Status .BUFFER_OVERFLOW ) {
593- netData .compact ();
594- netData .flip ();
591+ netWriteData .compact ();
592+ netWriteData .flip ();
595593 if (handshakeStatus != SSLEngineResult .HandshakeStatus .NEED_UNWRAP || flushData (blocking )) {
596594 sel = waitSelect (SelectionKey .OP_WRITE , blocking , exception );
597595 if ( sel instanceof IRubyObject ) return (IRubyObject ) sel ; // :wait_writeable
@@ -605,9 +603,9 @@ private IRubyObject doHandshake(final boolean blocking, final boolean exception)
605603 }
606604
607605 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 ();
611609 handshakeStatus = result .getHandshakeStatus ();
612610 status = result .getStatus ();
613611 if (handshakeStatus == SSLEngineResult .HandshakeStatus .NEED_TASK
@@ -627,13 +625,13 @@ private void doTasks() {
627625
628626 private boolean flushData (boolean blocking ) throws IOException {
629627 try {
630- writeToChannel (netData , blocking );
628+ writeToChannel (netWriteData , blocking );
631629 }
632630 catch (IOException ioe ) {
633- netData .position (netData .limit ());
631+ netWriteData .position (netWriteData .limit ());
634632 throw ioe ;
635633 }
636- return netData .hasRemaining ();
634+ return netWriteData .hasRemaining ();
637635 }
638636
639637 private int writeToChannel (ByteBuffer buffer , boolean blocking ) throws IOException {
@@ -671,15 +669,15 @@ public int write(ByteBuffer src, boolean blocking) throws SSLException, IOExcept
671669 if ( ! blocking ) channel .configureBlocking (false );
672670
673671 try {
674- if ( netData .hasRemaining () ) {
672+ if ( netWriteData .hasRemaining () ) {
675673 flushData (blocking );
676674 }
677- netData .clear ();
678- final SSLEngineResult result = engine .wrap (src , netData );
675+ netWriteData .clear ();
676+ final SSLEngineResult result = engine .wrap (src , netWriteData );
679677 if ( result .getStatus () == SSLEngineResult .Status .CLOSED ) {
680678 throw getRuntime ().newIOError ("closed SSL engine" );
681679 }
682- netData .flip ();
680+ netWriteData .flip ();
683681 flushData (blocking );
684682 return result .bytesConsumed ();
685683 }
@@ -692,22 +690,22 @@ public int read(final ByteBuffer dst, final boolean blocking) throws IOException
692690 if ( initialHandshake ) return 0 ;
693691 if ( engine .isInboundDone () ) return -1 ;
694692
695- if ( ! peerAppData .hasRemaining () ) {
693+ if ( ! appReadData .hasRemaining () ) {
696694 int appBytesProduced = readAndUnwrap (blocking );
697695 if (appBytesProduced == -1 || appBytesProduced == 0 ) {
698696 return appBytesProduced ;
699697 }
700698 }
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 );
703701 dst .position (dst .arrayOffset () + limit );
704702 return limit ;
705703 }
706704
707705 private int readAndUnwrap (final boolean blocking ) throws IOException {
708- final int bytesRead = socketChannelImpl ().read (peerNetData );
706+ final int bytesRead = socketChannelImpl ().read (netReadData );
709707 if ( bytesRead == -1 ) {
710- if ( ! peerNetData .hasRemaining () ||
708+ if ( ! netReadData .hasRemaining () ||
711709 ( status == SSLEngineResult .Status .BUFFER_UNDERFLOW ) ) {
712710 closeInbound ();
713711 return -1 ;
@@ -716,12 +714,12 @@ private int readAndUnwrap(final boolean blocking) throws IOException {
716714 // be defered till the last engine.unwrap() call.
717715 // peerNetData could not be empty.
718716 }
719- peerAppData .clear ();
720- peerNetData .flip ();
717+ appReadData .clear ();
718+ netReadData .flip ();
721719
722720 SSLEngineResult result ;
723721 do {
724- result = engine .unwrap (peerNetData , peerAppData );
722+ result = engine .unwrap (netReadData , appReadData );
725723 }
726724 while ( result .getStatus () == SSLEngineResult .Status .OK &&
727725 result .getHandshakeStatus () == SSLEngineResult .HandshakeStatus .NEED_UNWRAP &&
@@ -730,15 +728,15 @@ private int readAndUnwrap(final boolean blocking) throws IOException {
730728 if ( result .getHandshakeStatus () == SSLEngineResult .HandshakeStatus .FINISHED ) {
731729 finishInitialHandshake ();
732730 }
733- if ( peerAppData .position () == 0 &&
731+ if ( appReadData .position () == 0 &&
734732 result .getStatus () == SSLEngineResult .Status .OK &&
735- peerNetData .hasRemaining () ) {
736- result = engine .unwrap (peerNetData , peerAppData );
733+ netReadData .hasRemaining () ) {
734+ result = engine .unwrap (netReadData , appReadData );
737735 }
738736 status = result .getStatus ();
739737 handshakeStatus = result .getHandshakeStatus ();
740738
741- if ( bytesRead == -1 && ! peerNetData .hasRemaining () ) {
739+ if ( bytesRead == -1 && ! netReadData .hasRemaining () ) {
742740 // now it's safe to call closeInbound().
743741 closeInbound ();
744742 }
@@ -747,15 +745,15 @@ private int readAndUnwrap(final boolean blocking) throws IOException {
747745 return -1 ;
748746 }
749747
750- peerNetData .compact ();
751- peerAppData .flip ();
748+ netReadData .compact ();
749+ appReadData .flip ();
752750 if ( ! initialHandshake && (
753751 handshakeStatus == SSLEngineResult .HandshakeStatus .NEED_TASK ||
754752 handshakeStatus == SSLEngineResult .HandshakeStatus .NEED_WRAP ||
755753 handshakeStatus == SSLEngineResult .HandshakeStatus .FINISHED ) ) {
756754 doHandshake (blocking );
757755 }
758- return peerAppData .remaining ();
756+ return appReadData .remaining ();
759757 }
760758
761759 private void closeInbound () {
@@ -776,9 +774,9 @@ private void doShutdown() throws IOException {
776774 debug (getRuntime (), "SSLSocket.doShutdown data in the data buffer - can't send close" );
777775 return ;
778776 }
779- netData .clear ();
777+ netWriteData .clear ();
780778 try {
781- engine .wrap (dummy , netData ); // send close (after sslEngine.closeOutbound)
779+ engine .wrap (dummy , netWriteData ); // send close (after sslEngine.closeOutbound)
782780 }
783781 catch (SSLException e ) {
784782 debug (getRuntime (), "SSLSocket.doShutdown" , e );
@@ -788,7 +786,7 @@ private void doShutdown() throws IOException {
788786 debugStackTrace (getRuntime (), "SSLSocket.doShutdown" , e );
789787 return ;
790788 }
791- netData .flip ();
789+ netWriteData .flip ();
792790 flushData (true );
793791 }
794792
@@ -814,7 +812,7 @@ private IRubyObject sysreadImpl(final ThreadContext context,
814812
815813 try {
816814 // 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 ) ) {
818816 final Object ex = waitSelect (SelectionKey .OP_READ , blocking , exception );
819817 if ( ex instanceof IRubyObject ) return (IRubyObject ) ex ; // :wait_readable
820818 }
@@ -992,7 +990,7 @@ private void close(boolean force) {
992990
993991 engine .closeOutbound ();
994992
995- if ( ! force && netData .hasRemaining () ) return ;
993+ if ( ! force && netWriteData .hasRemaining () ) return ;
996994
997995 try {
998996 doShutdown ();
0 commit comments