|
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);
|
@@ -976,11 +984,48 @@ private boolean cachedCheckAlgorithm(String algorithm) {
|
976 | 984 | if (result != null) {
|
977 | 985 | return result;
|
978 | 986 | }
|
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); |
980 | 990 | cache.put(algorithm, result);
|
981 | 991 | return result;
|
982 | 992 | }
|
983 | 993 |
|
| 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 | + |
984 | 1029 | /*
|
985 | 1030 | * This constraint is used for the complete disabling of the algorithm.
|
986 | 1031 | */
|
|
0 commit comments