private SSLEngineResult encryptMessage ( ) throws IOException { if ( TraceComponent . isAnyTracingEnabled ( ) && tc . isEntryEnabled ( ) ) { Tr . entry ( tc , "encryptMessage" ) ; } // Get the application buffer used as input to the encryption algorithm. // Extract the app buffers containing data to be written. final WsByteBuffer [ ] appBuffers = getBuffers ( ) ; SSLEngineResult result ; while ( true ) { // Protect JSSE from potential SSL packet sizes that are too big. int [ ] appLimitInfo = SSLUtils . adjustBuffersForJSSE ( appBuffers , getConnLink ( ) . getAppBufferSize ( ) ) ; if ( TraceComponent . isAnyTracingEnabled ( ) && tc . isEventEnabled ( ) ) { Tr . event ( tc , "before wrap: appBuffers: " + SSLUtils . getBufferTraceInfo ( appBuffers ) + "\r\n\tencAppBuf: " + SSLUtils . getBufferTraceInfo ( encryptedAppBuffer ) ) ; } // Call the SSL engine to encrypt the request. result = getConnLink ( ) . getSSLEngine ( ) . wrap ( SSLUtils . getWrappedByteBuffers ( appBuffers ) , encryptedAppBuffer . getWrappedByteBuffer ( ) ) ; // Check the result of the call to wrap. Status status = result . getStatus ( ) ; if ( TraceComponent . isAnyTracingEnabled ( ) && tc . isEventEnabled ( ) ) { Tr . event ( tc , "after wrap: appBuffers: " + SSLUtils . getBufferTraceInfo ( appBuffers ) + "\r\n\tencAppBuf: " + SSLUtils . getBufferTraceInfo ( encryptedAppBuffer ) + "\r\n\tstatus=" + status + " consumed=" + result . bytesConsumed ( ) + " produced=" + result . bytesProduced ( ) ) ; } // If a limit modification was saved, restore it. if ( appLimitInfo != null ) { SSLUtils . resetBuffersAfterJSSE ( appBuffers , appLimitInfo ) ; } if ( status == Status . OK ) { break ; } else if ( status == Status . BUFFER_OVERFLOW ) { // The output buffers provided to the SSL engine were not big enough. A bigger buffer // must be supplied. If we can build a bigger buffer and call again, build it. increaseEncryptedBuffer ( ) ; continue ; } else { throw new IOException ( "Unable to encrypt data, status=" + status ) ; } } // end of while if ( TraceComponent . isAnyTracingEnabled ( ) && tc . isEntryEnabled ( ) ) { Tr . exit ( tc , "encryptMessage: " + result . getStatus ( ) ) ; } return result ; }
Handle common activity of write and writeAsynch involving the encryption of the current buffers . The caller will have the responsibility of writing them to the device side channel .