Skip to content

Commit f8bf084

Browse files
committed
8341964: Add mechanism to disable different parts of TLS cipher suite
Backport-of: 697f27c5d53dbe275685b87c8ed1bcfe4da6e4d0
1 parent 6efa88d commit f8bf084

File tree

6 files changed

+521
-269
lines changed

6 files changed

+521
-269
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,21 +42,21 @@
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.HashMap;
5051
import java.util.HashSet;
5152
import java.util.List;
5253
import java.util.Locale;
5354
import java.util.Map;
5455
import java.util.Set;
55-
import java.util.Collection;
5656
import java.util.StringTokenizer;
5757
import java.util.concurrent.ConcurrentHashMap;
58-
import java.util.regex.Pattern;
5958
import java.util.regex.Matcher;
59+
import java.util.regex.Pattern;
6060

6161
/**
6262
* Algorithm constraints for disabled algorithms property
@@ -101,6 +101,7 @@ private static class JarHolder {
101101
}
102102

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

140+
// Support patterns only for jdk.tls.disabledAlgorithms
141+
if (PROPERTY_TLS_DISABLED_ALGS.equals(propertyName)) {
142+
disabledPatterns = getDisabledPatterns();
143+
} else {
144+
disabledPatterns = null;
145+
}
146+
139147
// Check for alias
140148
for (String s : disabledAlgorithms) {
141149
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
@@ -748,7 +748,11 @@ http.auth.digest.disabledAlgorithms = MD5, SHA-1
748748
# This is in addition to the jdk.certpath.disabledAlgorithms property above.
749749
#
750750
# See the specification of "jdk.certpath.disabledAlgorithms" for the
751-
# syntax of the disabled algorithm string.
751+
# syntax of the disabled algorithm string. Additionally, TLS cipher suites
752+
# can be disabled with this property using one or more "*" wildcard characters.
753+
# For example, "TLS_RSA_*" disables all cipher suites that start with
754+
# "TLS_RSA_". Only cipher suites starting with "TLS_" are allowed to have
755+
# wildcard characters.
752756
#
753757
# Note: The algorithm restrictions do not apply to trust anchors or
754758
# self-signed certificates.
@@ -758,7 +762,7 @@ http.auth.digest.disabledAlgorithms = MD5, SHA-1
758762
#
759763
# Example:
760764
# jdk.tls.disabledAlgorithms=MD5, SSLv3, DSA, RSA keySize < 2048, \
761-
# rsa_pkcs1_sha1, secp224r1
765+
# rsa_pkcs1_sha1, secp224r1, TLS_RSA_*
762766
jdk.tls.disabledAlgorithms=SSLv3, TLSv1, TLSv1.1, DTLSv1.0, RC4, DES, \
763767
MD5withRSA, DH keySize < 1024, EC keySize < 224, 3DES_EDE_CBC, anon, NULL, \
764768
ECDH

0 commit comments

Comments
 (0)