4242
4343import javax .net .ssl .SSLEngine ;
4444import javax .net .ssl .SSLEngineResult ;
45+ import javax .net .ssl .SSLEngineResult .HandshakeStatus ;
4546import javax .net .ssl .SSLEngineResult .Status ;
4647import javax .net .ssl .SSLException ;
47-
4848import java .io .IOException ;
4949import java .net .SocketAddress ;
5050import java .nio .ByteBuffer ;
@@ -391,7 +391,7 @@ public void read(ChannelHandlerContext ctx) {
391391
392392 @ Override
393393 public void write (final ChannelHandlerContext ctx , Object msg , ChannelPromise promise ) throws Exception {
394- pendingUnencryptedWrites .add (PendingWrite .newInstance (( ByteBuf ) msg , promise ));
394+ pendingUnencryptedWrites .add (PendingWrite .newInstance (msg , promise ));
395395 }
396396
397397 @ Override
@@ -713,7 +713,7 @@ private static int getEncryptedPacketLength(ByteBuf buffer) {
713713 int majorVersion = buffer .getUnsignedByte (first + 1 );
714714 if (majorVersion == 3 ) {
715715 // SSLv3 or TLS
716- packetLength = ( buffer .getUnsignedShort (first + 3 ) ) + 5 ;
716+ packetLength = buffer .getUnsignedShort (first + 3 ) + 5 ;
717717 if (packetLength <= 5 ) {
718718 // Neither SSLv3 or TLSv1 (i.e. SSLv2 or bad data)
719719 tls = false ;
@@ -814,25 +814,27 @@ private void unwrap(ChannelHandlerContext ctx) throws SSLException {
814814
815815 private void unwrap (ChannelHandlerContext ctx , ByteBuffer packet , List <Object > out ) throws SSLException {
816816 boolean wrapLater = false ;
817- int bytesProduced = 0 ;
817+ int totalProduced = 0 ;
818818 try {
819- loop :
820819 for (;;) {
821820 if (decodeOut == null ) {
822821 decodeOut = ctx .alloc ().buffer ();
823822 }
824- SSLEngineResult result = unwrap (engine , packet , decodeOut );
825- bytesProduced += result .bytesProduced ();
826- switch (result .getStatus ()) {
827- case CLOSED :
828- // notify about the CLOSED state of the SSLEngine. See #137
829- sslCloseFuture .trySuccess (ctx .channel ());
830- break ;
831- case BUFFER_UNDERFLOW :
832- break loop ;
823+
824+ final SSLEngineResult result = unwrap (engine , packet , decodeOut );
825+ final Status status = result .getStatus ();
826+ final HandshakeStatus handshakeStatus = result .getHandshakeStatus ();
827+ final int produced = result .bytesProduced ();
828+ final int consumed = result .bytesConsumed ();
829+
830+ totalProduced += produced ;
831+ if (status == Status .CLOSED ) {
832+ // notify about the CLOSED state of the SSLEngine. See #137
833+ sslCloseFuture .trySuccess (ctx .channel ());
834+ break ;
833835 }
834836
835- switch (result . getHandshakeStatus () ) {
837+ switch (handshakeStatus ) {
836838 case NEED_UNWRAP :
837839 break ;
838840 case NEED_WRAP :
@@ -848,11 +850,10 @@ private void unwrap(ChannelHandlerContext ctx, ByteBuffer packet, List<Object> o
848850 case NOT_HANDSHAKING :
849851 break ;
850852 default :
851- throw new IllegalStateException (
852- "Unknown handshake status: " + result .getHandshakeStatus ());
853+ throw new IllegalStateException ("Unknown handshake status: " + handshakeStatus );
853854 }
854855
855- if (result . bytesConsumed () == 0 && result . bytesProduced () == 0 ) {
856+ if (status == Status . BUFFER_UNDERFLOW || consumed == 0 && produced == 0 ) {
856857 break ;
857858 }
858859 }
@@ -864,7 +865,7 @@ private void unwrap(ChannelHandlerContext ctx, ByteBuffer packet, List<Object> o
864865 setHandshakeFailure (e );
865866 throw e ;
866867 } finally {
867- if (bytesProduced > 0 ) {
868+ if (totalProduced > 0 ) {
868869 ByteBuf decodeOut = this .decodeOut ;
869870 this .decodeOut = null ;
870871 out .add (decodeOut );
0 commit comments