TlsChannel fix: Handle zeroing in the BufferHolder
As we use a PowerOfTwoBufferPool the buffer can be greater
than maxTlsPacketSize.
@@ -129,9 +129,7 @@ public class BufferHolder {
* <p>Typically used for security reasons, with buffers that contains now-unused plaintext.
*/
public void zeroRemaining() {
- ((Buffer) buffer).mark();
- buffer.put(zeros, 0, buffer.remaining());
- ((Buffer) buffer).reset();
+ zero(buffer.position());
}
/**
@@ -140,9 +138,20 @@ public class BufferHolder {
* <p>Typically used for security reasons, with buffers that contains now-unused plaintext.
*/
public void zero() {
+ zero(0);
+ }
+
+ private void zero(final int position) {
((Buffer) buffer).mark();
- ((Buffer) buffer).position(0);
- buffer.put(zeros, 0, buffer.remaining());
+ ((Buffer) buffer).position(position);
+ int size = buffer.remaining();
+ int length = Math.min(size, zeros.length);
+ int offset = 0;
+ while (length > 0) {
+ buffer.put(zeros, 0, length);
+ offset = offset + length;
+ length = Math.min(size - offset, zeros.length);
+ }
((Buffer) buffer).reset();
}
