BufferOverflow possible on an unwrap

If my outgoiing buffer is the size the SSLSession tells me to make it and the incoming buffer is the size it tells me to make it, is it possible to get a BufferOverflow? Shouldn't the sizes be enough that this will never happen as long as the outgoing buffer is cleared. According to my logs, it looks like I have an empy out buffer, and I have a loop which decrypted some of the data in the incoming buffer(not all), and then it tries to decrypt more but can't due to BufferOverflow which makes no sense to me as the Buffer has plenty of room left. Here is the logs. Notice I log the buffers before in the loop and after unwrap is called.

INFO: [echoServer][session 0]

incoming=java.nio.HeapByteBuffer[pos=0 lim=4995 cap=16665]

out=java.nio.HeapByteBuffer[pos=0 lim=16384 cap=16384]

Aug 2, 2005 6:52:12 AM biz.xsoftware.impl.nio.cm.basic.Helper processKeys

WARNING: [echoServer] unwrap failed

java.lang.RuntimeException: Bug, stuck in loop,

bufIn=java.nio.HeapByteBuffer[pos=37 lim=4995 cap=16665]

bufOut=java.nio.HeapByteBuffer[pos=5 lim=16384 cap=16384] hsStatus=NOT_HANDSHAKING status=BUFFER_OVERFLOW

at biz.xsoftware.impl.nio.secure.engine.AsynchSSLEngineImpl.feedEncryptedPacketImpl(AsynchSSLEngineImpl.java:193)

at biz.xsoftware.impl.nio.secure.engine.AsynchSSLEngineImpl.feedEncryptedPacket(AsynchSSLEngineImpl.java:144)

at biz.xsoftware.impl.nio.secure.engine.AsynchSSLEngineSynchronized.feedEncryptedPacket(AsynchSSLEngineSynchronized.java:32)

at biz.xsoftware.impl.nio.cm.secure.ReaderProxy.incomingData(ReaderProxy.java:33)

at biz.xsoftware.impl.nio.cm.basic.Helper.read(Helper.java:146)

at biz.xsoftware.impl.nio.cm.basic.Helper.processKey(Helper.java:77)

at biz.xsoftware.impl.nio.cm.basic.Helper.processKeys(Helper.java:43)

at biz.xsoftware.impl.nio.cm.basic.SelectorManager2.runLoop(SelectorManager2.java:259)

at biz.xsoftware.impl.nio.cm.basic.SelectorManager2$PollingThread.run(SelectorManager2.java:222)

[2037 byte] By [deanhiller2000a] at [2007-10-1 19:57:53]
# 1

It looks like you end up reading two packets at once into your input buffer and then try to convert them one after the other into the same un emptied buffer. Now, if you read into a buffer of the given packet size and unwrap into one of the app size, I doubt (and hope) you will ever have more than will fit in your output buffer from 1 complete read. However, the SSLEngine returns BUFFER_OVERFLOW whenever it doesn't have the entire amount free, whether or not it needs to put application data in it. This may be because it may use the rest of the buffer for its own purposes. IE:

1. Read 100 bytes into packet buffer.

2. Call SSLEngine.unwrap(packet,app)

3. Unwrap 50 bytes from the packet buffer, producing 20 output bytes.

4. Call SSLEngine.unwrap(packet,app)

5. Get a buffer overflow because unwrap needs 20 more free bytes in app.

Therefore what I did was use a separate buffer that I copied all the data to after each unwrap

Talchasa at 2007-7-11 16:27:13 > top of Java-index,Security,Java Secure Socket Extension (JSSE)...