Skip to content

Commit d6cef99

Browse files
Jamil Nimehhaimaychao
authored andcommittedOct 20, 2020
8245417: Improve certificate chain handling
Co-authored-by: Hai-may Chao <hai-may.chao@oracle.com> Reviewed-by: mullan, jnimeh
1 parent 24f7f84 commit d6cef99

File tree

6 files changed

+66
-10
lines changed

6 files changed

+66
-10
lines changed
 

‎src/java.base/share/classes/sun/security/ssl/CertificateMessage.java

+18-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2015, 2019, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2015, 2020, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -137,6 +137,15 @@ static final class T12CertificateMessage extends HandshakeMessage {
137137
byte[] encodedCert = Record.getBytes24(m);
138138
listLen -= (3 + encodedCert.length);
139139
encodedCerts.add(encodedCert);
140+
if (encodedCerts.size() > SSLConfiguration.maxCertificateChainLength) {
141+
throw new SSLProtocolException(
142+
"The certificate chain length ("
143+
+ encodedCerts.size()
144+
+ ") exceeds the maximum allowed length ("
145+
+ SSLConfiguration.maxCertificateChainLength
146+
+ ")");
147+
}
148+
140149
}
141150
this.encodedCertChain = encodedCerts;
142151
} else {
@@ -859,6 +868,14 @@ static final class T13CertificateMessage extends HandshakeMessage {
859868
SSLExtensions extensions =
860869
new SSLExtensions(this, m, enabledExtensions);
861870
certList.add(new CertificateEntry(encodedCert, extensions));
871+
if (certList.size() > SSLConfiguration.maxCertificateChainLength) {
872+
throw new SSLProtocolException(
873+
"The certificate chain length ("
874+
+ certList.size()
875+
+ ") exceeds the maximum allowed length ("
876+
+ SSLConfiguration.maxCertificateChainLength
877+
+ ")");
878+
}
862879
}
863880

864881
this.certEntries = Collections.unmodifiableList(certList);

‎src/java.base/share/classes/sun/security/ssl/DTLSInputRecord.java

+17-6
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2015, 2019, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2015, 2020, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -37,6 +37,7 @@
3737
import java.util.TreeSet;
3838
import javax.crypto.BadPaddingException;
3939
import javax.net.ssl.SSLException;
40+
import javax.net.ssl.SSLProtocolException;
4041
import sun.security.ssl.SSLCipher.SSLReadCipher;
4142

4243
/**
@@ -91,7 +92,7 @@ void finishHandshake() {
9192
}
9293

9394
@Override
94-
Plaintext acquirePlaintext() {
95+
Plaintext acquirePlaintext() throws SSLProtocolException {
9596
if (reassembler != null) {
9697
return reassembler.acquirePlaintext();
9798
}
@@ -114,7 +115,7 @@ Plaintext[] decode(ByteBuffer[] srcs, int srcsOffset,
114115
}
115116
}
116117

117-
Plaintext[] decode(ByteBuffer packet) {
118+
Plaintext[] decode(ByteBuffer packet) throws SSLProtocolException {
118119
if (isClosed) {
119120
return null;
120121
}
@@ -346,7 +347,7 @@ private int bytesInCompletePacket(ByteBuffer packet) throws SSLException {
346347
private static HandshakeFragment parseHandshakeMessage(
347348
byte contentType, byte majorVersion, byte minorVersion,
348349
byte[] recordEnS, int recordEpoch, long recordSeq,
349-
ByteBuffer plaintextFragment) {
350+
ByteBuffer plaintextFragment) throws SSLProtocolException {
350351

351352
int remaining = plaintextFragment.remaining();
352353
if (remaining < handshakeHeaderSize) {
@@ -376,6 +377,16 @@ private static HandshakeFragment parseHandshakeMessage(
376377
((plaintextFragment.get() & 0xFF) << 16) |
377378
((plaintextFragment.get() & 0xFF) << 8) |
378379
(plaintextFragment.get() & 0xFF); // pos: 1-3
380+
381+
if (messageLength > SSLConfiguration.maxHandshakeMessageSize) {
382+
throw new SSLProtocolException(
383+
"The size of the handshake message ("
384+
+ messageLength
385+
+ ") exceeds the maximum allowed size ("
386+
+ SSLConfiguration.maxHandshakeMessageSize
387+
+ ")");
388+
}
389+
379390
int messageSeq =
380391
((plaintextFragment.get() & 0xFF) << 8) |
381392
(plaintextFragment.get() & 0xFF); // pos: 4/5
@@ -968,7 +979,7 @@ private boolean isEmpty() {
968979
(needToCheckFlight && !flightIsReady()));
969980
}
970981

971-
Plaintext acquirePlaintext() {
982+
Plaintext acquirePlaintext() throws SSLProtocolException {
972983
if (bufferedFragments.isEmpty()) {
973984
if (SSLLogger.isOn && SSLLogger.isOn("verbose")) {
974985
SSLLogger.fine("No received handshake messages");
@@ -1080,7 +1091,7 @@ private void resetHandshakeFlight(HandshakeFlight prev) {
10801091
needToCheckFlight = false;
10811092
}
10821093

1083-
private Plaintext acquireCachedMessage() {
1094+
private Plaintext acquireCachedMessage() throws SSLProtocolException {
10841095
RecordFragment rFrag = bufferedFragments.first();
10851096
if (readEpoch != rFrag.recordEpoch) {
10861097
if (readEpoch > rFrag.recordEpoch) {

‎src/java.base/share/classes/sun/security/ssl/SSLConfiguration.java

+10-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2018, 2019, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2018, 2020, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -43,6 +43,7 @@
4343
import javax.net.ssl.SSLEngine;
4444
import javax.net.ssl.SSLParameters;
4545
import javax.net.ssl.SSLSocket;
46+
import sun.security.action.GetIntegerAction;
4647
import sun.security.action.GetPropertyAction;
4748
import sun.security.ssl.SSLExtension.ClientExtensions;
4849
import sun.security.ssl.SSLExtension.ServerExtensions;
@@ -104,6 +105,14 @@ final class SSLConfiguration implements Cloneable {
104105
static final boolean acknowledgeCloseNotify = Utilities.getBooleanProperty(
105106
"jdk.tls.acknowledgeCloseNotify", false);
106107

108+
// Set the max size limit for Handshake Message to 2^15
109+
static final int maxHandshakeMessageSize = GetIntegerAction.privilegedGetProperty(
110+
"jdk.tls.maxHandshakeMessageSize", 32768);
111+
112+
// Set the max certificate chain length to 10
113+
static final int maxCertificateChainLength = GetIntegerAction.privilegedGetProperty(
114+
"jdk.tls.maxCertificateChainLength", 10);
115+
107116
// Is the extended_master_secret extension supported?
108117
static {
109118
boolean supportExtendedMasterSecret = Utilities.getBooleanProperty(

‎src/java.base/share/classes/sun/security/ssl/SSLEngineInputRecord.java

+10-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 1996, 2019, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 1996, 2020, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -297,6 +297,15 @@ private Plaintext[] decodeInputRecord(ByteBuffer packet)
297297
}
298298

299299
int handshakeBodyLen = Record.getInt24(handshakeFrag);
300+
if (handshakeBodyLen > SSLConfiguration.maxHandshakeMessageSize) {
301+
throw new SSLProtocolException(
302+
"The size of the handshake message ("
303+
+ handshakeBodyLen
304+
+ ") exceeds the maximum allowed size ("
305+
+ SSLConfiguration.maxHandshakeMessageSize
306+
+ ")");
307+
}
308+
300309
handshakeFrag.reset();
301310
int handshakeMessageLen =
302311
handshakeHeaderSize + handshakeBodyLen;

‎src/java.base/share/classes/sun/security/ssl/SSLSocketInputRecord.java

+10-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 1996, 2019, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 1996, 2020, Oracle and/or its affiliates. All rights reserved.
33
* Copyright (c) 2020, Azul Systems, Inc. All rights reserved.
44
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
55
*
@@ -310,6 +310,15 @@ private Plaintext[] decodeInputRecord() throws IOException, BadPaddingException
310310
}
311311

312312
int handshakeBodyLen = Record.getInt24(handshakeFrag);
313+
if (handshakeBodyLen > SSLConfiguration.maxHandshakeMessageSize) {
314+
throw new SSLProtocolException(
315+
"The size of the handshake message ("
316+
+ handshakeBodyLen
317+
+ ") exceeds the maximum allowed size ("
318+
+ SSLConfiguration.maxHandshakeMessageSize
319+
+ ")");
320+
}
321+
313322
handshakeFrag.reset();
314323
int handshakeMessageLen =
315324
handshakeHeaderSize + handshakeBodyLen;

‎test/jdk/java/net/httpclient/LargeHandshakeTest.java

+1
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@
8787
* @run main/othervm -Dtest.requiresHost=true
8888
* -Djdk.httpclient.HttpClient.log=headers
8989
* -Djdk.internal.httpclient.debug=true
90+
* -Djdk.tls.maxHandshakeMessageSize=131072
9091
* LargeHandshakeTest
9192
*
9293
*/

0 commit comments

Comments
 (0)
Please sign in to comment.