|
1 | 1 | /* |
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. |
3 | 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
4 | 4 | * |
5 | 5 | * This code is free software; you can redistribute it and/or modify it |
|
42 | 42 | import java.security.spec.PSSParameterSpec; |
43 | 43 | import java.time.DateTimeException; |
44 | 44 | import java.time.Instant; |
45 | | -import java.time.ZonedDateTime; |
46 | 45 | import java.time.ZoneId; |
| 46 | +import java.time.ZonedDateTime; |
47 | 47 | import java.util.ArrayList; |
48 | 48 | import java.util.Arrays; |
| 49 | +import java.util.Collection; |
49 | 50 | import java.util.Date; |
50 | 51 | import java.util.HashMap; |
51 | 52 | import java.util.HashSet; |
52 | 53 | import java.util.List; |
53 | 54 | import java.util.Locale; |
54 | 55 | import java.util.Map; |
55 | 56 | import java.util.Set; |
56 | | -import java.util.Collection; |
57 | 57 | import java.util.StringTokenizer; |
58 | 58 | import java.util.concurrent.ConcurrentHashMap; |
59 | | -import java.util.regex.Pattern; |
60 | 59 | import java.util.regex.Matcher; |
| 60 | +import java.util.regex.Pattern; |
61 | 61 |
|
62 | 62 | /** |
63 | 63 | * Algorithm constraints for disabled algorithms property |
@@ -102,6 +102,7 @@ private static class JarHolder { |
102 | 102 | } |
103 | 103 |
|
104 | 104 | private final Set<String> disabledAlgorithms; |
| 105 | + private final List<Pattern> disabledPatterns; |
105 | 106 | private final Constraints algorithmConstraints; |
106 | 107 | private volatile SoftReference<Map<String, Boolean>> cacheRef = |
107 | 108 | new SoftReference<>(null); |
@@ -137,6 +138,13 @@ public DisabledAlgorithmConstraints(String propertyName, |
137 | 138 | super(decomposer); |
138 | 139 | disabledAlgorithms = getAlgorithms(propertyName); |
139 | 140 |
|
| 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 | + |
140 | 148 | // Check for alias |
141 | 149 | for (String s : disabledAlgorithms) { |
142 | 150 | Matcher matcher = INCLUDE_PATTERN.matcher(s); |
@@ -967,11 +975,48 @@ private boolean cachedCheckAlgorithm(String algorithm) { |
967 | 975 | if (result != null) { |
968 | 976 | return result; |
969 | 977 | } |
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); |
971 | 981 | cache.put(algorithm, result); |
972 | 982 | return result; |
973 | 983 | } |
974 | 984 |
|
| 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 | + |
975 | 1020 | /* |
976 | 1021 | * This constraint is used for the complete disabling of the algorithm. |
977 | 1022 | */ |
|
0 commit comments