Skip to content

Commit c90978b

Browse files
committed
8341964: Add mechanism to disable different parts of TLS cipher suite
Reviewed-by: mbaesken Backport-of: 697f27c5d53dbe275685b87c8ed1bcfe4da6e4d0
1 parent f78f1bf commit c90978b

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);
@@ -976,11 +984,48 @@ private boolean cachedCheckAlgorithm(String algorithm) {
976984
if (result != null) {
977985
return result;
978986
}
979-
result = checkAlgorithm(disabledAlgorithms, algorithm, decomposer);
987+
// We won't check patterns if algorithm check fails.
988+
result = checkAlgorithm(disabledAlgorithms, algorithm, decomposer)
989+
&& checkDisabledPatterns(algorithm);
980990
cache.put(algorithm, result);
981991
return result;
982992
}
983993

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

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

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -725,7 +725,11 @@ jdk.jar.disabledAlgorithms=MD2, MD5, RSA keySize < 1024, \
725725
# This is in addition to the jdk.certpath.disabledAlgorithms property above.
726726
#
727727
# See the specification of "jdk.certpath.disabledAlgorithms" for the
728-
# syntax of the disabled algorithm string.
728+
# syntax of the disabled algorithm string. Additionally, TLS cipher suites
729+
# can be disabled with this property using one or more "*" wildcard characters.
730+
# For example, "TLS_RSA_*" disables all cipher suites that start with
731+
# "TLS_RSA_". Only cipher suites starting with "TLS_" are allowed to have
732+
# wildcard characters.
729733
#
730734
# Note: The algorithm restrictions do not apply to trust anchors or
731735
# self-signed certificates.
@@ -735,7 +739,7 @@ jdk.jar.disabledAlgorithms=MD2, MD5, RSA keySize < 1024, \
735739
#
736740
# Example:
737741
# jdk.tls.disabledAlgorithms=MD5, SSLv3, DSA, RSA keySize < 2048, \
738-
# rsa_pkcs1_sha1, secp224r1
742+
# rsa_pkcs1_sha1, secp224r1, TLS_RSA_*
739743
jdk.tls.disabledAlgorithms=SSLv3, TLSv1, TLSv1.1, DTLSv1.0, RC4, DES, \
740744
MD5withRSA, DH keySize < 1024, EC keySize < 224, 3DES_EDE_CBC, anon, NULL, \
741745
ECDH

0 commit comments

Comments
 (0)