Skip to content

Commit 0064cf9

Browse files
author
Hai-May Chao
committed
8311596: Add separate system properties for TLS server and client for maximum chain length
Reviewed-by: jnimeh, weijun, mullan
1 parent 3a7525d commit 0064cf9

File tree

2 files changed

+68
-9
lines changed

2 files changed

+68
-9
lines changed

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

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2015, 2022, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2015, 2023, 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
@@ -130,12 +130,16 @@ static final class T12CertificateMessage extends HandshakeMessage {
130130
byte[] encodedCert = Record.getBytes24(m);
131131
listLen -= (3 + encodedCert.length);
132132
encodedCerts.add(encodedCert);
133-
if (encodedCerts.size() > SSLConfiguration.maxCertificateChainLength) {
133+
int maxAllowedChainLength = handshakeContext.sslConfig.isClientMode ?
134+
SSLConfiguration.maxInboundServerCertChainLen :
135+
SSLConfiguration.maxInboundClientCertChainLen;
136+
137+
if (encodedCerts.size() > maxAllowedChainLength) {
134138
throw new SSLProtocolException(
135139
"The certificate chain length ("
136140
+ encodedCerts.size()
137141
+ ") exceeds the maximum allowed length ("
138-
+ SSLConfiguration.maxCertificateChainLength
142+
+ maxAllowedChainLength
139143
+ ")");
140144
}
141145

@@ -861,12 +865,16 @@ static final class T13CertificateMessage extends HandshakeMessage {
861865
SSLExtensions extensions =
862866
new SSLExtensions(this, m, enabledExtensions);
863867
certList.add(new CertificateEntry(encodedCert, extensions));
864-
if (certList.size() > SSLConfiguration.maxCertificateChainLength) {
868+
int maxAllowedChainLength = handshakeContext.sslConfig.isClientMode ?
869+
SSLConfiguration.maxInboundServerCertChainLen :
870+
SSLConfiguration.maxInboundClientCertChainLen;
871+
872+
if (certList.size() > maxAllowedChainLength) {
865873
throw new SSLProtocolException(
866874
"The certificate chain length ("
867875
+ certList.size()
868876
+ ") exceeds the maximum allowed length ("
869-
+ SSLConfiguration.maxCertificateChainLength
877+
+ maxAllowedChainLength
870878
+ ")");
871879
}
872880
}

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

Lines changed: 55 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2018, 2022, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2018, 2023, 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
@@ -108,9 +108,11 @@ final class SSLConfiguration implements Cloneable {
108108
static final int maxHandshakeMessageSize = GetIntegerAction.privilegedGetProperty(
109109
"jdk.tls.maxHandshakeMessageSize", 32768);
110110

111-
// Set the max certificate chain length to 10
112-
static final int maxCertificateChainLength = GetIntegerAction.privilegedGetProperty(
113-
"jdk.tls.maxCertificateChainLength", 10);
111+
// Limit the certificate chain length accepted from clients
112+
static final int maxInboundClientCertChainLen;
113+
114+
// Limit the certificate chain length accepted from servers
115+
static final int maxInboundServerCertChainLen;
114116

115117
// To switch off the supported_groups extension for DHE cipher suite.
116118
static final boolean enableFFDHE =
@@ -133,6 +135,55 @@ final class SSLConfiguration implements Cloneable {
133135
useExtendedMasterSecret = supportExtendedMasterSecret;
134136
}
135137

138+
static {
139+
boolean globalPropSet = false;
140+
141+
// jdk.tls.maxCertificateChainLength property has no default
142+
Integer maxCertificateChainLength = GetIntegerAction.privilegedGetProperty(
143+
"jdk.tls.maxCertificateChainLength");
144+
if (maxCertificateChainLength != null && maxCertificateChainLength >= 0) {
145+
globalPropSet = true;
146+
}
147+
148+
/*
149+
* If either jdk.tls.server.maxInboundCertificateChainLength or
150+
* jdk.tls.client.maxInboundCertificateChainLength is set, it will
151+
* override jdk.tls.maxCertificateChainLength, regardless of whether
152+
* jdk.tls.maxCertificateChainLength is set or not.
153+
* If neither jdk.tls.server.maxInboundCertificateChainLength nor
154+
* jdk.tls.client.maxInboundCertificateChainLength is set, the behavior
155+
* depends on the setting of jdk.tls.maxCertificateChainLength. If
156+
* jdk.tls.maxCertificateChainLength is set, it falls back to that
157+
* value; otherwise, it defaults to 8 for
158+
* jdk.tls.server.maxInboundCertificateChainLength
159+
* and 10 for jdk.tls.client.maxInboundCertificateChainLength.
160+
* Users can independently set either
161+
* jdk.tls.server.maxInboundCertificateChainLength or
162+
* jdk.tls.client.maxInboundCertificateChainLength.
163+
*/
164+
Integer inboundClientLen = GetIntegerAction.privilegedGetProperty(
165+
"jdk.tls.server.maxInboundCertificateChainLength");
166+
167+
// Default for jdk.tls.server.maxInboundCertificateChainLength is 8
168+
if (inboundClientLen == null || inboundClientLen < 0) {
169+
maxInboundClientCertChainLen = globalPropSet ?
170+
maxCertificateChainLength : 8;
171+
} else {
172+
maxInboundClientCertChainLen = inboundClientLen;
173+
}
174+
175+
Integer inboundServerLen = GetIntegerAction.privilegedGetProperty(
176+
"jdk.tls.client.maxInboundCertificateChainLength");
177+
178+
// Default for jdk.tls.client.maxInboundCertificateChainLength is 10
179+
if (inboundServerLen == null || inboundServerLen < 0) {
180+
maxInboundServerCertChainLen = globalPropSet ?
181+
maxCertificateChainLength : 10;
182+
} else {
183+
maxInboundServerCertChainLen = inboundServerLen;
184+
}
185+
}
186+
136187
SSLConfiguration(SSLContextImpl sslContext, boolean isClientMode) {
137188

138189
// Configurations with SSLParameters, default values.

0 commit comments

Comments
 (0)