Skip to content

Keep reading from network on TLS buffer underflow #702

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ public class SslEngineFrameBuilder extends FrameBuilder {

private final ByteBuffer cipherBuffer;

private boolean needToReadMore = false;

public SslEngineFrameBuilder(SSLEngine sslEngine, ByteBuffer plainIn, ByteBuffer cipherIn, ReadableByteChannel channel) {
super(channel, plainIn);
this.sslEngine = sslEngine;
Expand All @@ -40,11 +42,21 @@ public SslEngineFrameBuilder(SSLEngine sslEngine, ByteBuffer plainIn, ByteBuffer

@Override
protected boolean somethingToRead() throws IOException {
if (applicationBuffer.hasRemaining()) {
if (applicationBuffer.hasRemaining() && !needToReadMore) {
return true;
} else {
applicationBuffer.clear();

if (needToReadMore) {
int read = NioHelper.read(channel, cipherBuffer);
if (read == 0) {
return false;
} else {
this.needToReadMore = false;
this.cipherBuffer.flip();
}
}

while (true) {
SSLEngineResult result = sslEngine.unwrap(cipherBuffer, applicationBuffer);
switch (result.getStatus()) {
Expand All @@ -61,6 +73,7 @@ protected boolean somethingToRead() throws IOException {
cipherBuffer.compact();
int read = NioHelper.read(channel, cipherBuffer);
if (read == 0) {
this.needToReadMore = true;
return false;
}
cipherBuffer.flip();
Expand Down