Commit 41eb5b65 authored by Prameet Shah's avatar Prameet Shah Committed by Kenny Root
Browse files

OpenSSLEngineImpl: fix unwrap behavior with array

The decrypted bytes should written sequentially into each buffer of
the destination array until it's full before moving to the next
buffer.

Change-Id: I2454249c167deafde6c12134d3c8cd658cd7c21b
parent 3b7268cd
......@@ -445,18 +445,24 @@ public class OpenSSLEngineImpl extends SSLEngine implements NativeCrypto.SSLHand
try {
int positionBeforeRead = srcDuplicate.position();
int produced = 0;
for (int i = 0; i < length; i++) {
ByteBuffer dst = dsts[offset + i];
boolean shouldStop = false;
while (!shouldStop) {
ByteBuffer dst = getNextAvailableByteBuffer(dsts, offset, length);
if (dst == null) {
shouldStop = true;
continue;
}
ByteBuffer arrayDst = dst;
if (dst.isDirect() || dst.arrayOffset() != 0 || dst.position() != 0
|| dst.limit() != dst.capacity()) {
|| dst.limit() != dst.capacity()) {
arrayDst = ByteBuffer.allocate(dst.remaining());
}
int internalProduced = NativeCrypto.SSL_read_BIO(sslNativePointer, arrayDst.array(),
source.getContext(),
localToRemoteSink.getContext(), this);
source.getContext(), localToRemoteSink.getContext(), this);
if (internalProduced <= 0) {
shouldStop = true;
continue;
}
arrayDst.position(arrayDst.position() + internalProduced);
......@@ -477,6 +483,16 @@ public class OpenSSLEngineImpl extends SSLEngine implements NativeCrypto.SSLHand
}
}
/** Returns the next non-empty ByteBuffer. */
private ByteBuffer getNextAvailableByteBuffer(ByteBuffer[] buffers, int offset, int length) {
for (int i = offset; i < length; ++i) {
if (buffers[i].remaining() > 0) {
return buffers[i];
}
}
return null;
}
@Override
public SSLEngineResult wrap(ByteBuffer[] srcs, int offset, int length, ByteBuffer dst)
throws SSLException {
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment