final SSLEngine sslEngine = sslCtx.getSslEngine();
final Connection connection = ctx.getConnection();
final boolean isLoggingFinest = LOGGER.isLoggable(Level.FINEST);
Buffer tmpInputToDispose = null;
Buffer tmpNetBuffer = null;
Buffer tmpAppBuffer = tmpAppBuffer0;
try {
HandshakeStatus handshakeStatus = sslEngine.getHandshakeStatus();
_exitWhile:
while (true) {
if (isLoggingFinest) {
LOGGER.log(Level.FINEST, "Loop Engine: {0} handshakeStatus={1}",
new Object[]{sslEngine, sslEngine.getHandshakeStatus()});
}
switch (handshakeStatus) {
case NEED_UNWRAP: {
if (isLoggingFinest) {
LOGGER.log(Level.FINEST, "NEED_UNWRAP Engine: {0}", sslEngine);
}
if (inputBuffer == null || !inputBuffer.hasRemaining()) {
break _exitWhile;
}
final int expectedLength = getSSLPacketSize(inputBuffer);
if (expectedLength == -1
|| inputBuffer.remaining() < expectedLength) {
break _exitWhile;
}
if (tmpAppBuffer == null) {
tmpAppBuffer = allocateOutputBuffer(sslCtx.getAppBufferSize());
}
final SSLEngineResult sslEngineResult =
handshakeUnwrap(sslCtx, inputBuffer, tmpAppBuffer);
if (!inputBuffer.hasRemaining()) {
tmpInputToDispose = inputBuffer;
inputBuffer = null;
}
final SSLEngineResult.Status status = sslEngineResult.getStatus();
if (status == Status.BUFFER_UNDERFLOW ||
status == Status.BUFFER_OVERFLOW) {
throw new SSLException("SSL unwrap error: " + status);
}
handshakeStatus = sslEngine.getHandshakeStatus();
break;
}
case NEED_WRAP: {
if (isLoggingFinest) {
LOGGER.log(Level.FINEST, "NEED_WRAP Engine: {0}", sslEngine);
}
tmpNetBuffer = handshakeWrap(
connection, sslCtx, tmpNetBuffer);
handshakeStatus = sslEngine.getHandshakeStatus();
break;
}
case NEED_TASK: {
if (isLoggingFinest) {
LOGGER.log(Level.FINEST, "NEED_TASK Engine: {0}", sslEngine);
}
executeDelegatedTask(sslEngine);
handshakeStatus = sslEngine.getHandshakeStatus();
break;
}
case FINISHED:
case NOT_HANDSHAKING: {
break _exitWhile;
}
}
if (handshakeStatus == HandshakeStatus.FINISHED) {
break _exitWhile;
}
}
} catch (IOException ioe) {
notifyHandshakeFailed(connection, ioe);
throw ioe;
} finally {
if (tmpAppBuffer0 == null && tmpAppBuffer != null) {
tmpAppBuffer.dispose();
}
if (tmpInputToDispose != null) {
tmpInputToDispose.tryDispose();
inputBuffer = null;