Skip to content

Commit 9d29c77

Browse files
committed
8217633: Configurable extensions with system properties
Backport-of: 71bfe96
1 parent 1bed6cd commit 9d29c77

File tree

2 files changed

+144
-24
lines changed

2 files changed

+144
-24
lines changed

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

Lines changed: 69 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,9 @@
2828
import java.io.IOException;
2929
import java.nio.ByteBuffer;
3030
import java.text.MessageFormat;
31-
import java.util.Collection;
32-
import java.util.Collections;
33-
import java.util.LinkedList;
34-
import java.util.Locale;
31+
import java.util.*;
32+
33+
import sun.security.action.GetPropertyAction;
3534
import sun.security.ssl.SSLHandshake.HandshakeMessage;
3635
import sun.security.util.HexDumpEncoder;
3736

@@ -628,8 +627,8 @@ void absentOnTrade(ConnectionContext context,
628627
}
629628

630629
public boolean isAvailable(ProtocolVersion protocolVersion) {
631-
for (int i = 0; i < supportedProtocols.length; i++) {
632-
if (supportedProtocols[i] == protocolVersion) {
630+
for (ProtocolVersion supportedProtocol : supportedProtocols) {
631+
if (supportedProtocol == protocolVersion) {
633632
return true;
634633
}
635634
}
@@ -693,18 +692,23 @@ static final class ClientExtensions {
693692
static final Collection<SSLExtension> defaults;
694693

695694
static {
695+
Collection<String> clientDisabledExtensions =
696+
getDisabledExtensions("jdk.tls.client.disableExtensions");
696697
Collection<SSLExtension> extensions = new LinkedList<>();
697698
for (SSLExtension extension : SSLExtension.values()) {
698-
if (extension.handshakeType != SSLHandshake.NOT_APPLICABLE) {
699+
if (extension.handshakeType != SSLHandshake.NOT_APPLICABLE &&
700+
!clientDisabledExtensions.contains(extension.name)) {
699701
extensions.add(extension);
700702
}
701703
}
702704

703-
// Switch off SNI extention?
704-
boolean enableExtension =
705-
Utilities.getBooleanProperty("jsse.enableSNIExtension", true);
706-
if (!enableExtension) {
707-
extensions.remove(CH_SERVER_NAME);
705+
// Switch off SNI extension?
706+
if (extensions.contains(CH_SERVER_NAME)) {
707+
boolean enableExtension = Utilities.getBooleanProperty(
708+
"jsse.enableSNIExtension", true);
709+
if (!enableExtension) {
710+
extensions.remove(CH_SERVER_NAME);
711+
}
708712
}
709713

710714
// To switch off the max_fragment_length extension.
@@ -715,13 +719,15 @@ static final class ClientExtensions {
715719
// the two properties set to true, the extension is switch on.
716720
// We may remove the "jsse.enableMFLExtension" property in the
717721
// future. Please don't continue to use the misspelling property.
718-
enableExtension =
719-
Utilities.getBooleanProperty(
720-
"jsse.enableMFLNExtension", false) ||
721-
Utilities.getBooleanProperty(
722-
"jsse.enableMFLExtension", false);
723-
if (!enableExtension) {
724-
extensions.remove(CH_MAX_FRAGMENT_LENGTH);
722+
if (extensions.contains(CH_MAX_FRAGMENT_LENGTH)) {
723+
boolean enableExtension =
724+
Utilities.getBooleanProperty(
725+
"jsse.enableMFLNExtension", false) ||
726+
Utilities.getBooleanProperty(
727+
"jsse.enableMFLExtension", false);
728+
if (!enableExtension) {
729+
extensions.remove(CH_MAX_FRAGMENT_LENGTH);
730+
}
725731
}
726732

727733
// To switch on certificate_authorities extension in ClientHello.
@@ -762,10 +768,12 @@ static final class ClientExtensions {
762768
// lot in practice. When there is a need to use this extension
763769
// in ClientHello handshake message, please take care of the
764770
// potential compatibility and interoperability issues above.
765-
enableExtension = Utilities.getBooleanProperty(
766-
"jdk.tls.client.enableCAExtension", false);
767-
if (!enableExtension) {
768-
extensions.remove(CH_CERTIFICATE_AUTHORITIES);
771+
if (extensions.contains(CH_CERTIFICATE_AUTHORITIES)) {
772+
boolean enableExtension = Utilities.getBooleanProperty(
773+
"jdk.tls.client.enableCAExtension", false);
774+
if (!enableExtension) {
775+
extensions.remove(CH_CERTIFICATE_AUTHORITIES);
776+
}
769777
}
770778

771779
defaults = Collections.unmodifiableCollection(extensions);
@@ -777,14 +785,51 @@ static final class ServerExtensions {
777785
static final Collection<SSLExtension> defaults;
778786

779787
static {
788+
Collection<String> serverDisabledExtensions =
789+
getDisabledExtensions("jdk.tls.server.disableExtensions");
780790
Collection<SSLExtension> extensions = new LinkedList<>();
781791
for (SSLExtension extension : SSLExtension.values()) {
782-
if (extension.handshakeType != SSLHandshake.NOT_APPLICABLE) {
792+
if (extension.handshakeType != SSLHandshake.NOT_APPLICABLE &&
793+
!serverDisabledExtensions.contains(extension.name)) {
783794
extensions.add(extension);
784795
}
785796
}
786797

787798
defaults = Collections.unmodifiableCollection(extensions);
788799
}
789800
}
801+
802+
// Get disabled extensions, which could be customized with System Properties.
803+
private static Collection<String> getDisabledExtensions(
804+
String propertyName) {
805+
String property = GetPropertyAction.privilegedGetProperty(propertyName);
806+
if (SSLLogger.isOn && SSLLogger.isOn("ssl,sslctx")) {
807+
SSLLogger.fine(
808+
"System property " + propertyName + " is set to '" +
809+
property + "'");
810+
}
811+
if (property != null && !property.isEmpty()) {
812+
// remove double quote marks from beginning/end of the property
813+
if (property.length() > 1 && property.charAt(0) == '"' &&
814+
property.charAt(property.length() - 1) == '"') {
815+
property = property.substring(1, property.length() - 1);
816+
}
817+
}
818+
819+
if (property != null && !property.isEmpty()) {
820+
String[] extensionNames = property.split(",");
821+
Collection<String> extensions =
822+
new ArrayList<>(extensionNames.length);
823+
for (String extension : extensionNames) {
824+
extension = extension.trim();
825+
if (!extension.isEmpty()) {
826+
extensions.add(extension);
827+
}
828+
}
829+
830+
return extensions;
831+
}
832+
833+
return Collections.emptyList();
834+
}
790835
}
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
/*
2+
* Copyright (c) 2021, Oracle and/or its affiliates. All rights reserved.
3+
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4+
*
5+
* This code is free software; you can redistribute it and/or modify it
6+
* under the terms of the GNU General Public License version 2 only, as
7+
* published by the Free Software Foundation.
8+
*
9+
* This code is distributed in the hope that it will be useful, but WITHOUT
10+
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11+
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12+
* version 2 for more details (a copy is included in the LICENSE file that
13+
* accompanied this code).
14+
*
15+
* You should have received a copy of the GNU General Public License version
16+
* 2 along with this work; if not, write to the Free Software Foundation,
17+
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18+
*
19+
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20+
* or visit www.oracle.com if you need additional information or have any
21+
* questions.
22+
*/
23+
24+
/*
25+
* @test
26+
* @bug 8217633
27+
* @library /javax/net/ssl/templates
28+
* @summary Configurable extensions with system properties
29+
* @run main/othervm DisableExtensions supported_versions TLSv1.3 fail
30+
* @run main/othervm DisableExtensions supported_versions TLSv1.2
31+
*/
32+
33+
import javax.net.ssl.SSLSocket;
34+
import javax.net.ssl.SSLException;
35+
36+
public class DisableExtensions extends SSLSocketTemplate {
37+
38+
private final String[] protocols;
39+
40+
public DisableExtensions(String[] protocols) {
41+
this.protocols = protocols;
42+
}
43+
44+
@Override
45+
protected void configureClientSocket(SSLSocket socket) {
46+
socket.setEnabledProtocols(protocols);
47+
}
48+
49+
// Run the test case.
50+
//
51+
// Check that the extension could be disabled, and the impact may be
52+
// different for different protocols.
53+
public static void main(String[] args) throws Exception {
54+
System.setProperty("jdk.tls.client.disableExtensions", args[0]);
55+
56+
boolean shouldSuccess = (args.length != 3);
57+
58+
try {
59+
(new DisableExtensions(new String[] {args[1]})).run();
60+
} catch (SSLException | IllegalStateException ssle) {
61+
if (shouldSuccess) {
62+
throw new RuntimeException(
63+
"The extension " + args[0] + " is disabled");
64+
}
65+
66+
return;
67+
}
68+
69+
if (!shouldSuccess) {
70+
throw new RuntimeException(
71+
"The extension " + args[0] +
72+
" should be disabled and the connection should fail");
73+
}
74+
}
75+
}

0 commit comments

Comments
 (0)