private ByteBuffer sslDataUnwrap ( ByteBuffer inBBuff , ByteBuffer outBBuff ) throws GSSException { try { int iter = 0 ; if ( sslEngine . getHandshakeStatus ( ) != SSLEngineResult . HandshakeStatus . NOT_HANDSHAKING ) { throw new Exception ( "SSLEngine handshaking needed! " + "HandshakeStatus: " + sslEngine . getHandshakeStatus ( ) . toString ( ) ) ; } do { logger . debug ( "PROCESSING DATA (UNWRAP) " + ++ iter + ": " + inBBuff . remaining ( ) ) ; SSLEngineResult result = sslEngine . unwrap ( inBBuff , outBBuff ) ; if ( result . getHandshakeStatus ( ) == SSLEngineResult . HandshakeStatus . NEED_TASK ) { runDelegatedTasks ( sslEngine ) ; continue ; } if ( result . getStatus ( ) == SSLEngineResult . Status . BUFFER_OVERFLOW ) { // increase it to the size needed. int appSize = sslEngine . getSession ( ) . getApplicationBufferSize ( ) ; ByteBuffer b = ByteBuffer . allocate ( appSize + outBBuff . position ( ) ) ; outBBuff . flip ( ) ; b . put ( outBBuff ) ; outBBuff = b ; continue ; } else if ( result . getStatus ( ) == SSLEngineResult . Status . BUFFER_UNDERFLOW ) { // More data needed from peer break ; } else if ( result . getStatus ( ) == SSLEngineResult . Status . CLOSED ) { throw new ClosedGSSException ( ) ; } if ( result . getStatus ( ) != SSLEngineResult . Status . OK ) { throw new GlobusGSSException ( GSSException . FAILURE , GlobusGSSException . TOKEN_FAIL , result . getStatus ( ) . toString ( ) ) ; } } while ( inBBuff . hasRemaining ( ) ) ; return outBBuff ; } catch ( IllegalArgumentException e ) { throw new GlobusGSSException ( GSSException . DEFECTIVE_TOKEN , e ) ; } catch ( SSLException e ) { if ( e . toString ( ) . endsWith ( "bad record MAC" ) ) throw new GlobusGSSException ( GSSException . BAD_MIC , e ) ; else if ( e . toString ( ) . endsWith ( "ciphertext sanity check failed" ) ) throw new GlobusGSSException ( GSSException . DEFECTIVE_TOKEN , e ) ; else throw new GlobusGSSException ( GSSException . FAILURE , e ) ; } catch ( GSSException e ) { throw e ; } catch ( Exception e ) { throw new GlobusGSSException ( GSSException . FAILURE , e ) ; } }
Not all in inBBuff might be consumed by this method!!!