Skip to content

Commit bf5e210

Browse files
committed
TlsChannel support Buffer idiosyncrasies
Support flip(), mark(), reset() and position(n) for ByteBuffers Cannot use compilerArgs --release=8 JAVA-3588
1 parent 7cc4be2 commit bf5e210

File tree

5 files changed

+27
-20
lines changed

5 files changed

+27
-20
lines changed

driver-core/src/main/com/mongodb/internal/connection/tlschannel/ServerTlsChannel.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
import javax.net.ssl.SSLSession;
3636
import javax.net.ssl.StandardConstants;
3737
import java.io.IOException;
38+
import java.nio.Buffer;
3839
import java.nio.ByteBuffer;
3940
import java.nio.channels.ByteChannel;
4041
import java.nio.channels.Channel;
@@ -399,7 +400,7 @@ private Optional<SNIServerName> getServerNameIndication() throws IOException, Eo
399400
}
400401
TlsChannelImpl.readFromChannel(underlying, inEncrypted.buffer); // IO block
401402
}
402-
inEncrypted.buffer.flip();
403+
((Buffer) inEncrypted.buffer).flip();
403404
Map<Integer, SNIServerName> serverNames = TlsExplorer.explore(inEncrypted.buffer);
404405
inEncrypted.buffer.compact();
405406
SNIServerName hostName = serverNames.get(StandardConstants.SNI_HOST_NAME);
@@ -421,7 +422,7 @@ private int readRecordHeaderSize() throws IOException, EofException {
421422
}
422423
TlsChannelImpl.readFromChannel(underlying, inEncrypted.buffer); // IO block
423424
}
424-
inEncrypted.buffer.flip();
425+
((Buffer) inEncrypted.buffer).flip();
425426
int recordHeaderSize = TlsExplorer.getRequiredSize(inEncrypted.buffer);
426427
inEncrypted.buffer.compact();
427428
return recordHeaderSize;

driver-core/src/main/com/mongodb/internal/connection/tlschannel/impl/BufferHolder.java

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import org.slf4j.Logger;
2424
import org.slf4j.LoggerFactory;
2525

26+
import java.nio.Buffer;
2627
import java.nio.ByteBuffer;
2728
import java.util.Optional;
2829

@@ -111,7 +112,7 @@ public void enlarge() {
111112

112113
private void resizeImpl(int newCapacity) {
113114
ByteBuffer newBuffer = allocator.allocate(newCapacity);
114-
buffer.flip();
115+
((Buffer) buffer).flip();
115116
newBuffer.put(buffer);
116117
if (plainData) {
117118
zero();
@@ -128,9 +129,9 @@ private void resizeImpl(int newCapacity) {
128129
* <p>Typically used for security reasons, with buffers that contains now-unused plaintext.
129130
*/
130131
public void zeroRemaining() {
131-
buffer.mark();
132+
((Buffer) buffer).mark();
132133
buffer.put(zeros, 0, buffer.remaining());
133-
buffer.reset();
134+
((Buffer) buffer).reset();
134135
}
135136

136137
/**
@@ -139,10 +140,10 @@ public void zeroRemaining() {
139140
* <p>Typically used for security reasons, with buffers that contains now-unused plaintext.
140141
*/
141142
public void zero() {
142-
buffer.mark();
143-
buffer.position(0);
143+
((Buffer) buffer).mark();
144+
((Buffer) buffer).position(0);
144145
buffer.put(zeros, 0, buffer.remaining());
145-
buffer.reset();
146+
((Buffer) buffer).reset();
146147
}
147148

148149
public boolean nullOrEmpty() {

driver-core/src/main/com/mongodb/internal/connection/tlschannel/impl/ByteBufferUtil.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919

2020
package com.mongodb.internal.connection.tlschannel.impl;
2121

22+
import java.nio.Buffer;
2223
import java.nio.ByteBuffer;
2324

2425
public class ByteBufferUtil {
@@ -43,8 +44,8 @@ public static void copy(ByteBuffer src, ByteBuffer dst, int length) {
4344
return;
4445
}
4546
ByteBuffer tmp = src.duplicate();
46-
tmp.limit(src.position() + length);
47+
((Buffer) tmp).limit(src.position() + length);
4748
dst.put(tmp);
48-
src.position(src.position() + length);
49+
((Buffer) src).position(src.position() + length);
4950
}
5051
}

driver-core/src/main/com/mongodb/internal/connection/tlschannel/impl/TlsChannelImpl.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
import javax.net.ssl.SSLException;
3737
import javax.net.ssl.SSLSession;
3838
import java.io.IOException;
39+
import java.nio.Buffer;
3940
import java.nio.ByteBuffer;
4041
import java.nio.channels.ByteChannel;
4142
import java.nio.channels.ClosedChannelException;
@@ -210,7 +211,7 @@ public long read(ByteBufferSet dest) throws IOException, NeedsTaskException {
210211
throw new ClosedChannelException();
211212
}
212213
HandshakeStatus handshakeStatus = engine.getHandshakeStatus();
213-
int bytesToReturn = inPlain.nullOrEmpty() ? 0 : inPlain.buffer.position();
214+
int bytesToReturn = inPlain.nullOrEmpty() ? 0 :inPlain.buffer.position();
214215
while (true) {
215216
if (bytesToReturn > 0) {
216217
if (inPlain.nullOrEmpty()) {
@@ -263,7 +264,7 @@ private void handleTask() throws NeedsTaskException {
263264
}
264265

265266
private int transferPendingPlain(ByteBufferSet dstBuffers) {
266-
inPlain.buffer.flip(); // will read
267+
((Buffer) inPlain.buffer).flip(); // will read
267268
int bytes = dstBuffers.putRemaining(inPlain.buffer);
268269
inPlain.buffer.compact(); // will write
269270
boolean disposed = inPlain.release();
@@ -314,7 +315,7 @@ private UnwrapResult unwrapLoop(Optional<ByteBufferSet> dest, HandshakeStatus or
314315
}
315316

316317
private SSLEngineResult callEngineUnwrap(ByteBufferSet dest) throws SSLException {
317-
inEncrypted.buffer.flip();
318+
((Buffer) inEncrypted.buffer).flip();
318319
try {
319320
SSLEngineResult result =
320321
engine.unwrap(inEncrypted.buffer, dest.array, dest.offset, dest.length);
@@ -449,7 +450,7 @@ private void writeToChannel() throws IOException {
449450
if (outEncrypted.buffer.position() == 0) {
450451
return;
451452
}
452-
outEncrypted.buffer.flip();
453+
((Buffer) outEncrypted.buffer).flip();
453454
try {
454455
try {
455456
writeToChannel(writeChannel, outEncrypted.buffer);

driver-core/src/main/com/mongodb/internal/connection/tlschannel/impl/TlsExplorer.java

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import javax.net.ssl.SNIServerName;
2424
import javax.net.ssl.SSLProtocolException;
2525
import javax.net.ssl.StandardConstants;
26+
import java.nio.Buffer;
2627
import java.nio.BufferUnderflowException;
2728
import java.nio.ByteBuffer;
2829
import java.util.HashMap;
@@ -51,7 +52,7 @@ private TlsExplorer() {}
5152
*/
5253
public static int getRequiredSize(ByteBuffer source) {
5354
if (source.remaining() < RECORD_HEADER_SIZE) throw new BufferUnderflowException();
54-
source.mark();
55+
((Buffer) source).mark();
5556
try {
5657
byte firstByte = source.get();
5758
source.get(); // second byte discarded
@@ -63,13 +64,13 @@ public static int getRequiredSize(ByteBuffer source) {
6364
return (((source.get() & 0xFF) << 8) | (source.get() & 0xFF)) + 5;
6465
}
6566
} finally {
66-
source.reset();
67+
((Buffer) source).reset();
6768
}
6869
}
6970

7071
public static Map<Integer, SNIServerName> explore(ByteBuffer source) throws SSLProtocolException {
7172
if (source.remaining() < RECORD_HEADER_SIZE) throw new BufferUnderflowException();
72-
source.mark();
73+
((Buffer) source).mark();
7374
try {
7475
byte firstByte = source.get();
7576
ignore(source, 1); // ignore second byte
@@ -84,7 +85,7 @@ public static Map<Integer, SNIServerName> explore(ByteBuffer source) throws SSLP
8485
throw new SSLProtocolException("Not handshake record");
8586
}
8687
} finally {
87-
source.reset();
88+
((Buffer) source).reset();
8889
}
8990
}
9091

@@ -134,7 +135,7 @@ private static Map<Integer, SNIServerName> exploreHandshake(ByteBuffer input, in
134135
// records, but in practice this does not occur.
135136
if (handshakeLength > recordLength - 4) // 4: handshake header size
136137
throw new SSLProtocolException("Handshake message spans multiple records");
137-
input.limit(handshakeLength + input.position());
138+
((Buffer) input).limit(handshakeLength + input.position());
138139
return exploreClientHello(input);
139140
}
140141

@@ -257,7 +258,9 @@ private static void ignoreByteVector16(ByteBuffer input) {
257258
}
258259

259260
private static void ignore(ByteBuffer input, int length) {
260-
if (length != 0) input.position(input.position() + length);
261+
if (length != 0) {
262+
((Buffer) input).position(input.position() + length);
263+
}
261264
}
262265

263266
// For some reason, SNIServerName is abstract

0 commit comments

Comments
 (0)