Skip to content

Commit 37f8029

Browse files
TheMangovnikjerboaa
authored andcommitted
8341964: Add mechanism to disable different parts of TLS cipher suite
Backport-of: c90978b8ec00bc19f467e6002528496283186579
1 parent 2df5d20 commit 37f8029

File tree

6 files changed

+517
-265
lines changed

6 files changed

+517
-265
lines changed

src/java.base/share/classes/sun/security/util/DisabledAlgorithmConstraints.java

Lines changed: 50 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2010, 2022, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2010, 2024, 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
@@ -42,22 +42,22 @@
4242
import java.security.spec.PSSParameterSpec;
4343
import java.time.DateTimeException;
4444
import java.time.Instant;
45-
import java.time.ZonedDateTime;
4645
import java.time.ZoneId;
46+
import java.time.ZonedDateTime;
4747
import java.util.ArrayList;
4848
import java.util.Arrays;
49+
import java.util.Collection;
4950
import java.util.Date;
5051
import java.util.HashMap;
5152
import java.util.HashSet;
5253
import java.util.List;
5354
import java.util.Locale;
5455
import java.util.Map;
5556
import java.util.Set;
56-
import java.util.Collection;
5757
import java.util.StringTokenizer;
5858
import java.util.concurrent.ConcurrentHashMap;
59-
import java.util.regex.Pattern;
6059
import java.util.regex.Matcher;
60+
import java.util.regex.Pattern;
6161

6262
/**
6363
* Algorithm constraints for disabled algorithms property
@@ -102,6 +102,7 @@ private static class JarHolder {
102102
}
103103

104104
private final Set<String> disabledAlgorithms;
105+
private final List<Pattern> disabledPatterns;
105106
private final Constraints algorithmConstraints;
106107
private volatile SoftReference<Map<String, Boolean>> cacheRef =
107108
new SoftReference<>(null);
@@ -137,6 +138,13 @@ public DisabledAlgorithmConstraints(String propertyName,
137138
super(decomposer);
138139
disabledAlgorithms = getAlgorithms(propertyName);
139140

141+
// Support patterns only for jdk.tls.disabledAlgorithms
142+
if (PROPERTY_TLS_DISABLED_ALGS.equals(propertyName)) {
143+
disabledPatterns = getDisabledPatterns();
144+
} else {
145+
disabledPatterns = null;
146+
}
147+
140148
// Check for alias
141149
for (String s : disabledAlgorithms) {
142150
Matcher matcher = INCLUDE_PATTERN.matcher(s);
@@ -967,11 +975,48 @@ private boolean cachedCheckAlgorithm(String algorithm) {
967975
if (result != null) {
968976
return result;
969977
}
970-
result = checkAlgorithm(disabledAlgorithms, algorithm, decomposer);
978+
// We won't check patterns if algorithm check fails.
979+
result = checkAlgorithm(disabledAlgorithms, algorithm, decomposer)
980+
&& checkDisabledPatterns(algorithm);
971981
cache.put(algorithm, result);
972982
return result;
973983
}
974984

985+
private boolean checkDisabledPatterns(final String algorithm) {
986+
return disabledPatterns == null || disabledPatterns.stream().noneMatch(
987+
p -> p.matcher(algorithm).matches());
988+
}
989+
990+
private List<Pattern> getDisabledPatterns() {
991+
List<Pattern> ret = null;
992+
List<String> patternStrings = new ArrayList<>(4);
993+
994+
for (String p : disabledAlgorithms) {
995+
if (p.contains("*")) {
996+
if (!p.startsWith("TLS_")) {
997+
throw new IllegalArgumentException(
998+
"Wildcard pattern must start with \"TLS_\"");
999+
}
1000+
patternStrings.add(p);
1001+
}
1002+
}
1003+
1004+
if (!patternStrings.isEmpty()) {
1005+
ret = new ArrayList<>(patternStrings.size());
1006+
1007+
for (String p : patternStrings) {
1008+
// Exclude patterns from algorithm code flow.
1009+
disabledAlgorithms.remove(p);
1010+
1011+
// Ignore all regex characters but asterisk.
1012+
ret.add(Pattern.compile(
1013+
"^\\Q" + p.replace("*", "\\E.*\\Q") + "\\E$"));
1014+
}
1015+
}
1016+
1017+
return ret;
1018+
}
1019+
9751020
/*
9761021
* This constraint is used for the complete disabling of the algorithm.
9771022
*/

src/java.base/share/conf/security/java.security

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -745,7 +745,11 @@ jdk.jar.disabledAlgorithms=MD2, MD5, RSA keySize < 1024, \
745745
# This is in addition to the jdk.certpath.disabledAlgorithms property above.
746746
#
747747
# See the specification of "jdk.certpath.disabledAlgorithms" for the
748-
# syntax of the disabled algorithm string.
748+
# syntax of the disabled algorithm string. Additionally, TLS cipher suites
749+
# can be disabled with this property using one or more "*" wildcard characters.
750+
# For example, "TLS_RSA_*" disables all cipher suites that start with
751+
# "TLS_RSA_". Only cipher suites starting with "TLS_" are allowed to have
752+
# wildcard characters.
749753
#
750754
# Note: The algorithm restrictions do not apply to trust anchors or
751755
# self-signed certificates.
@@ -755,7 +759,7 @@ jdk.jar.disabledAlgorithms=MD2, MD5, RSA keySize < 1024, \
755759
#
756760
# Example:
757761
# jdk.tls.disabledAlgorithms=MD5, SSLv3, DSA, RSA keySize < 2048, \
758-
# rsa_pkcs1_sha1, secp224r1
762+
# rsa_pkcs1_sha1, secp224r1, TLS_RSA_*
759763
jdk.tls.disabledAlgorithms=SSLv3, TLSv1, TLSv1.1, DTLSv1.0, RC4, DES, \
760764
MD5withRSA, DH keySize < 1024, EC keySize < 224, 3DES_EDE_CBC, anon, NULL, \
761765
ECDH, \

0 commit comments

Comments
 (0)