ByteBuffer snetBuffer = ByteBuffer.allocate(serverEngine.getSession().getPacketBufferSize());
clientEngine.beginHandshake();
serverEngine.beginHandshake();
SSLEngineResult result = null;
SSLEngineResult.HandshakeStatus srv = serverEngine.getHandshakeStatus();
SSLEngineResult.HandshakeStatus cli = clientEngine.getHandshakeStatus();
while (srv != HandshakeStatus.NOT_HANDSHAKING
&& cli != HandshakeStatus.NOT_HANDSHAKING)
{
if (cli == HandshakeStatus.NEED_WRAP)
{
if (srv != HandshakeStatus.NEED_UNWRAP)
{
throw new SSLException("invalid server handshake state: " + srv);
}
result = clientEngine.wrap(empty, cnetBuffer);
if (result.getStatus() != Status.OK)
throw new SSLException("unexpected status after wrap: "
+ result.getStatus());
cli = result.getHandshakeStatus();
cnetBuffer.flip();
result = serverEngine.unwrap(cnetBuffer, empty);
cnetBuffer.compact();
if (result.getStatus() != Status.OK)
throw new SSLException("unexpected status after unwrap: "
+ result.getStatus());
srv = result.getHandshakeStatus();
if (cli == HandshakeStatus.NEED_TASK)
{
Runnable task = null;
while ((task = clientEngine.getDelegatedTask()) != null)
task.run();
cli = clientEngine.getHandshakeStatus();
}
if (srv == HandshakeStatus.NEED_TASK)
{
Runnable task = null;
while ((task = serverEngine.getDelegatedTask()) != null)
task.run();
srv = serverEngine.getHandshakeStatus();
}
}
else if (cli == HandshakeStatus.NEED_UNWRAP)
{
if (srv != HandshakeStatus.NEED_WRAP)
{
throw new SSLException("invalid server handshake state: " + srv);
}
result = serverEngine.wrap(empty, snetBuffer);
if (result.getStatus() != Status.OK)
throw new SSLException("unexpected status after wrap: "
+ result.getStatus());
srv = result.getHandshakeStatus();
snetBuffer.flip();
result = clientEngine.unwrap(snetBuffer, empty);
snetBuffer.compact();
if (result.getStatus() != Status.OK)
throw new SSLException("unexpected status after unwrap: "
+ result.getStatus());
cli = result.getHandshakeStatus();
if (cli == HandshakeStatus.NEED_TASK)
{
Runnable task = null;
while ((task = clientEngine.getDelegatedTask()) != null)
task.run();
cli = clientEngine.getHandshakeStatus();
}
if (srv == HandshakeStatus.NEED_TASK)
{
Runnable task = null;
while ((task = serverEngine.getDelegatedTask()) != null)
task.run();
srv = serverEngine.getHandshakeStatus();
}
}
else if (cli == HandshakeStatus.NEED_TASK)
{
throw new SSLException("invalid initial state: " + cli);
}
else if (cli == HandshakeStatus.FINISHED)
{
if (srv != HandshakeStatus.FINISHED)
throw new SSLException("invalid final server state: " + srv);
break;
}
}
ByteBuffer appBuffer = ByteBuffer.allocate(serverEngine.getSession().getApplicationBufferSize());
Charset cs = Charset.forName("US-ASCII");
CharsetEncoder enc = cs.newEncoder();
enc.encode(CharBuffer.wrap(TEST_MESSAGE), appBuffer, true);
appBuffer.flip();
result = clientEngine.wrap(appBuffer, cnetBuffer);
if (result.getStatus() != Status.OK)
throw new SSLException("unexpected status: " + result.getStatus());
cnetBuffer.flip();
appBuffer.clear();
result = serverEngine.unwrap(cnetBuffer, appBuffer);
if (result.getStatus() != Status.OK)
throw new SSLException("unexpected status: " + result.getStatus());
appBuffer.flip();
String msg = cs.decode(appBuffer).toString();
if (!msg.equals(TEST_MESSAGE))
throw new SSLException("message decode failed");
appBuffer.clear();
enc.encode(CharBuffer.wrap(msg), appBuffer, true);
appBuffer.flip();
result = serverEngine.wrap(appBuffer, snetBuffer);
if (result.getStatus() != Status.OK)
throw new SSLException("unexpected status: " + result.getStatus());
snetBuffer.flip();
appBuffer.clear();
result = clientEngine.unwrap(snetBuffer, appBuffer);
if (result.getStatus() != Status.OK)
throw new SSLException("unexpected status: " + result.getStatus());
appBuffer.flip();
msg = cs.decode(appBuffer).toString();
if (!msg.equals(TEST_MESSAGE))
throw new SSLException("message decode (2) failed");
}