I am getting following exception on NIO SSL handshake. During handshake process,
On client side,
a) NEED_WRAP
b) NEED_UNWRAP
c) NEED_TASK
You're not supposed to 'follow' a 'sequence' when using the SSLEngine
. You are supposed to react to the states and exceptions it provides:
NEED_WRAP
: do a wrap()
from the application send buffer to the network send bufferNEED_UNWRAP
: do an unwrap()
from the network receive buffer to the application receive bufferBUFFER_OVERFLOW
: do a flip()/write()/compact()
on the network send buffer, or a flip()/get()/compact()
on the application receive bufferBUFFER_UNDERFLOW
: do a read()
on the network receive buffer, or there is nothing in the application send buffer.EDIT What's this?
if(myNetData.limit() == 0)
{
myNetData.clear();
}
and this?
case NEED_WRAP:
myNetData.clear();
You can't just throw away engine data. Get rid of these altogether. You can't do anything to the network send or receive buffers except flip(), compact(), wrap(),
and unwrap().
Also it doesn't appear that you have separate net send and receive buffers. You need both. You need four altogether: net send, net receive, application send, and application receive. The network buffers need to be of the sizes advised by the SSLEngine.
Apart from that, you aren't really reacting exactly as I said above. For example, take the NEED_UNWRAP
path. You should:
unwrap()
from the net recv buffer to the application receive buffer.BUFFER_UNDERFLOW
, read()
into the net receive buffer and repeat (1).For NEED_WRAP
:
wrap()
from the application send buffer to the network send buffer.BUFFER_OVERFLOW
, write()
from the net send buffer and repeat 3.When you need to read application data:
flip()/get()/compact()
from the application receive buffer.BufferUnderflowException
, unwrap()
and repeat, bearing in mind that the unwrap()
may cause NEED_WRAP
or NEED_WRAP
or BUFFER_UNDERFLOW
or BUFFER_OVERFLOW.
When you need to write application data:
put()
into the application send buffer.BufferOverflowException
, flip()/wrap()/compact()
, bearing in mind that the wrap()
may cause NEED_WRAP
or NEED_WRAP
or BUFFER_UNDERFLOW
or BUFFER_OVERFLOW
.