Skip to content

Commit 3344c17

Browse files
committed
Add support for re-keying with TLS 1.3
1 parent 2afae30 commit 3344c17

File tree

4 files changed

+48
-2
lines changed

4 files changed

+48
-2
lines changed

java/org/apache/tomcat/util/net/LocalStrings.properties

+2
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ channel.nio.ssl.expandNetInBuffer=Expanding network input buffer to [{0}] bytes
2626
channel.nio.ssl.expandNetOutBuffer=Expanding network output buffer to [{0}] bytes
2727
channel.nio.ssl.foundHttp=Found an plain text HTTP request on what should be an encrypted TLS connection
2828
channel.nio.ssl.handshakeError=Handshake error
29+
channel.nio.ssl.handshakeWrapPending=There is already handshake data waiting to be wrapped
30+
channel.nio.ssl.handshakeWrapQueueTooLong=The queue of handshake data to be wrapped has grown too long
2931
channel.nio.ssl.incompleteHandshake=Handshake incomplete, you must complete handshake before reading data.
3032
channel.nio.ssl.invalidCloseState=Invalid close state, will not send network data.
3133
channel.nio.ssl.invalidStatus=Unexpected status [{0}].

java/org/apache/tomcat/util/net/SecureNio2Channel.java

+24-2
Original file line numberDiff line numberDiff line change
@@ -53,10 +53,12 @@ public class SecureNio2Channel extends Nio2Channel {
5353
private static final Log log = LogFactory.getLog(SecureNio2Channel.class);
5454
private static final StringManager sm = StringManager.getManager(SecureNio2Channel.class);
5555

56-
// Value determined by observation of what the SSL Engine requested in
57-
// various scenarios
56+
// Value determined by observation of what the SSL Engine requested in various scenarios
5857
private static final int DEFAULT_NET_BUFFER_SIZE = 16921;
5958

59+
// Much longer than it should ever need to be but short enough to trigger connection closure if something goes wrong
60+
private static final int HANDSHAKE_WRAP_QUEUE_LENGTH_LIMIT = 100;
61+
6062
protected final Nio2Endpoint endpoint;
6163

6264
protected ByteBuffer netInBuffer;
@@ -67,6 +69,7 @@ public class SecureNio2Channel extends Nio2Channel {
6769
protected volatile boolean sniComplete = false;
6870

6971
private volatile boolean handshakeComplete = false;
72+
private volatile int handshakeWrapQueueLength = 0;
7073
private volatile HandshakeStatus handshakeStatus; //gets set by handshake
7174

7275
protected boolean closed;
@@ -762,6 +765,11 @@ private Integer unwrap(int nRead, long timeout, TimeUnit unit) throws ExecutionE
762765
//perform any tasks if needed
763766
if (unwrap.getHandshakeStatus() == HandshakeStatus.NEED_TASK) {
764767
tasks();
768+
} else if (unwrap.getHandshakeStatus() == HandshakeStatus.NEED_WRAP) {
769+
if (++handshakeWrapQueueLength > HANDSHAKE_WRAP_QUEUE_LENGTH_LIMIT) {
770+
throw new ExecutionException(
771+
new IOException(sm.getString("channel.nio.ssl.handshakeWrapQueueTooLong")));
772+
}
765773
}
766774
//if we need more network data, then bail out for now.
767775
if (unwrap.getStatus() == Status.BUFFER_UNDERFLOW) {
@@ -892,6 +900,8 @@ protected void wrap() {
892900
if (!netOutBuffer.hasRemaining()) {
893901
netOutBuffer.clear();
894902
SSLEngineResult result = sslEngine.wrap(src, netOutBuffer);
903+
// Call to wrap() will have included any required handshake data
904+
handshakeWrapQueueLength = 0;
895905
written = result.bytesConsumed();
896906
netOutBuffer.flip();
897907
if (result.getStatus() == Status.OK) {
@@ -957,6 +967,11 @@ public void completed(Integer nBytes, A attach) {
957967
//perform any tasks if needed
958968
if (unwrap.getHandshakeStatus() == HandshakeStatus.NEED_TASK) {
959969
tasks();
970+
} else if (unwrap.getHandshakeStatus() == HandshakeStatus.NEED_WRAP) {
971+
if (++handshakeWrapQueueLength > HANDSHAKE_WRAP_QUEUE_LENGTH_LIMIT) {
972+
throw new ExecutionException(new IOException(
973+
sm.getString("channel.nio.ssl.handshakeWrapQueueTooLong")));
974+
}
960975
}
961976
//if we need more network data, then bail out for now.
962977
if (unwrap.getStatus() == Status.BUFFER_UNDERFLOW) {
@@ -1070,6 +1085,11 @@ public void completed(Integer nBytes, A attach) {
10701085
//perform any tasks if needed
10711086
if (unwrap.getHandshakeStatus() == HandshakeStatus.NEED_TASK) {
10721087
tasks();
1088+
} else if (unwrap.getHandshakeStatus() == HandshakeStatus.NEED_WRAP) {
1089+
if (++handshakeWrapQueueLength > HANDSHAKE_WRAP_QUEUE_LENGTH_LIMIT) {
1090+
throw new ExecutionException(new IOException(
1091+
sm.getString("channel.nio.ssl.handshakeWrapQueueTooLong")));
1092+
}
10731093
}
10741094
//if we need more network data, then bail out for now.
10751095
if (unwrap.getStatus() == Status.BUFFER_UNDERFLOW) {
@@ -1179,6 +1199,8 @@ public <A> void write(final ByteBuffer src, final long timeout, final TimeUnit u
11791199
netOutBuffer.clear();
11801200
// Wrap the source data into the internal buffer
11811201
SSLEngineResult result = sslEngine.wrap(src, netOutBuffer);
1202+
// Call to wrap() will have included any required handshake data
1203+
handshakeWrapQueueLength = 0;
11821204
final int written = result.bytesConsumed();
11831205
netOutBuffer.flip();
11841206
if (result.getStatus() == Status.OK) {

java/org/apache/tomcat/util/net/SecureNioChannel.java

+19
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ public class SecureNioChannel extends NioChannel {
6565
protected boolean sniComplete = false;
6666

6767
protected boolean handshakeComplete = false;
68+
protected boolean needHandshakeWrap = false;
6869
protected HandshakeStatus handshakeStatus; //gets set by handshake
6970

7071
protected boolean closed = false;
@@ -622,6 +623,14 @@ public int read(ByteBuffer dst) throws IOException {
622623
//perform any tasks if needed
623624
if (unwrap.getHandshakeStatus() == HandshakeStatus.NEED_TASK) {
624625
tasks();
626+
} else if (unwrap.getHandshakeStatus() == HandshakeStatus.NEED_WRAP) {
627+
if (getOutboundRemaining() == 0) {
628+
handshakeWrap(true);
629+
} else if (needHandshakeWrap) {
630+
throw new IOException(sm.getString("channel.nio.ssl.handshakeWrapPending"));
631+
} else {
632+
needHandshakeWrap = true;
633+
}
625634
}
626635
//if we need more network data, then bail out for now.
627636
if (unwrap.getStatus() == Status.BUFFER_UNDERFLOW) {
@@ -711,6 +720,14 @@ public long read(ByteBuffer[] dsts, int offset, int length)
711720
//perform any tasks if needed
712721
if (unwrap.getHandshakeStatus() == HandshakeStatus.NEED_TASK) {
713722
tasks();
723+
} else if (unwrap.getHandshakeStatus() == HandshakeStatus.NEED_WRAP) {
724+
if (getOutboundRemaining() == 0) {
725+
handshakeWrap(true);
726+
} else if (needHandshakeWrap) {
727+
throw new IOException(sm.getString("channel.nio.ssl.handshakeWrapPending"));
728+
} else {
729+
needHandshakeWrap = true;
730+
}
714731
}
715732
//if we need more network data, then bail out for now.
716733
if (unwrap.getStatus() == Status.BUFFER_UNDERFLOW) {
@@ -809,6 +826,8 @@ public int write(ByteBuffer src) throws IOException {
809826
netOutBuffer.clear();
810827

811828
SSLEngineResult result = sslEngine.wrap(src, netOutBuffer);
829+
// Call to wrap() will have included any required handshake data
830+
needHandshakeWrap = false;
812831
// The number of bytes written
813832
int written = result.bytesConsumed();
814833
netOutBuffer.flip();

webapps/docs/changelog.xml

+3
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,9 @@
165165
Make counting of active HTTP/2 streams per connection more robust.
166166
(markt)
167167
</fix>
168+
<add>
169+
Add support for TLS 1.3 client initiated re-keying. (markt)
170+
</add>
168171
</changelog>
169172
</subsection>
170173
<subsection name="Jasper">

0 commit comments

Comments
 (0)