|
27 | 27 |
|
28 | 28 | import sun.security.validator.Validator; |
29 | 29 |
|
| 30 | +import java.lang.ref.SoftReference; |
30 | 31 | import java.security.AlgorithmParameters; |
31 | 32 | import java.security.CryptoPrimitive; |
32 | 33 | import java.security.Key; |
|
45 | 46 | import java.time.ZoneId; |
46 | 47 | import java.util.ArrayList; |
47 | 48 | import java.util.Arrays; |
48 | | -import java.util.Date; |
49 | 49 | import java.util.HashMap; |
50 | 50 | import java.util.HashSet; |
51 | 51 | import java.util.List; |
|
54 | 54 | import java.util.Set; |
55 | 55 | import java.util.Collection; |
56 | 56 | import java.util.StringTokenizer; |
| 57 | +import java.util.concurrent.ConcurrentHashMap; |
57 | 58 | import java.util.regex.Pattern; |
58 | 59 | import java.util.regex.Matcher; |
59 | 60 |
|
@@ -101,6 +102,8 @@ private static class JarHolder { |
101 | 102 |
|
102 | 103 | private final Set<String> disabledAlgorithms; |
103 | 104 | private final Constraints algorithmConstraints; |
| 105 | + private volatile SoftReference<Map<String, Boolean>> cacheRef = |
| 106 | + new SoftReference<>(null); |
104 | 107 |
|
105 | 108 | public static DisabledAlgorithmConstraints certPathConstraints() { |
106 | 109 | return CertPathHolder.CONSTRAINTS; |
@@ -158,7 +161,7 @@ public final boolean permits(Set<CryptoPrimitive> primitives, |
158 | 161 | " or empty."); |
159 | 162 | } |
160 | 163 |
|
161 | | - if (!checkAlgorithm(disabledAlgorithms, algorithm, decomposer)) { |
| 164 | + if (!cachedCheckAlgorithm(algorithm)) { |
162 | 165 | return false; |
163 | 166 | } |
164 | 167 |
|
@@ -241,7 +244,7 @@ public final void permits(String algorithm, ConstraintsParameters cp, |
241 | 244 | // Check if named curves in the key are disabled. |
242 | 245 | for (Key key : cp.getKeys()) { |
243 | 246 | for (String curve : getNamedCurveFromKey(key)) { |
244 | | - if (!checkAlgorithm(disabledAlgorithms, curve, decomposer)) { |
| 247 | + if (!cachedCheckAlgorithm(curve)) { |
245 | 248 | throw new CertPathValidatorException( |
246 | 249 | "Algorithm constraints check failed on disabled " + |
247 | 250 | "algorithm: " + curve, |
@@ -947,6 +950,25 @@ private boolean permitsImpl(Key key) { |
947 | 950 | } |
948 | 951 | } |
949 | 952 |
|
| 953 | + private boolean cachedCheckAlgorithm(String algorithm) { |
| 954 | + Map<String, Boolean> cache; |
| 955 | + if ((cache = cacheRef.get()) == null) { |
| 956 | + synchronized (this) { |
| 957 | + if ((cache = cacheRef.get()) == null) { |
| 958 | + cache = new ConcurrentHashMap<>(); |
| 959 | + cacheRef = new SoftReference<>(cache); |
| 960 | + } |
| 961 | + } |
| 962 | + } |
| 963 | + Boolean result = cache.get(algorithm); |
| 964 | + if (result != null) { |
| 965 | + return result; |
| 966 | + } |
| 967 | + result = checkAlgorithm(disabledAlgorithms, algorithm, decomposer); |
| 968 | + cache.put(algorithm, result); |
| 969 | + return result; |
| 970 | + } |
| 971 | + |
950 | 972 | /* |
951 | 973 | * This constraint is used for the complete disabling of the algorithm. |
952 | 974 | */ |
|
0 commit comments