Fix IndexOutOfBoundsException in OpenSslEngine

Motivation:

- In unwrap(), it does not check if the current index of dsts has
  reached at its end offset, resulting in IndexOutOfBoundsException.
- SSLEngine does not update the position of the source buffer correctly
  when SSL.writeToSSL() returns a negative value.

Modifications:

Fix them all

Result:

Less bugs
This commit is contained in:
Trustin Lee 2014-05-18 02:41:41 +09:00
parent a17822cc45
commit 80e95220d8

View File

@ -196,10 +196,12 @@ public final class OpenSslEngine extends SSLEngine {
buf.put(src); buf.put(src);
final int netWrote = SSL.writeToBIO(networkBIO, addr, len); final int netWrote = SSL.writeToBIO(networkBIO, addr, len);
src.position(pos + netWrote);
if (netWrote >= 0) { if (netWrote >= 0) {
src.position(pos + netWrote);
lastPrimingReadResult = SSL.readFromSSL(ssl, addr, 0); // priming read lastPrimingReadResult = SSL.readFromSSL(ssl, addr, 0); // priming read
return netWrote; return netWrote;
} else {
src.position(pos);
} }
} finally { } finally {
bufPool.releaseBuffer(buf); bufPool.releaseBuffer(buf);
@ -405,7 +407,8 @@ public final class OpenSslEngine extends SSLEngine {
} }
int capacity = 0; int capacity = 0;
for (int i = offset; i < offset + length; ++i) { final int endOffset = offset + length;
for (int i = offset; i < endOffset; i ++) {
ByteBuffer dst = dsts[i]; ByteBuffer dst = dsts[i];
if (dst == null) { if (dst == null) {
throw new IllegalArgumentException(); throw new IllegalArgumentException();
@ -471,7 +474,7 @@ public final class OpenSslEngine extends SSLEngine {
// Write decrypted data to dsts buffers // Write decrypted data to dsts buffers
int bytesProduced = 0; int bytesProduced = 0;
int idx = offset; int idx = offset;
for (;;) { while (idx < endOffset) {
ByteBuffer dst = dsts[idx]; ByteBuffer dst = dsts[idx];
if (!dst.hasRemaining()) { if (!dst.hasRemaining()) {
idx ++; idx ++;