|
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; |
|
53 | 54 | import java.util.Collection; |
54 | 55 | import java.util.StringTokenizer; |
55 | 56 | import java.util.TimeZone; |
| 57 | +import java.util.concurrent.ConcurrentHashMap; |
56 | 58 | import java.util.regex.Pattern; |
57 | 59 | import java.util.regex.Matcher; |
58 | 60 |
|
@@ -100,6 +102,8 @@ private static class JarHolder { |
100 | 102 |
|
101 | 103 | private final Set<String> disabledAlgorithms; |
102 | 104 | private final Constraints algorithmConstraints; |
| 105 | + private volatile SoftReference<Map<String, Boolean>> cacheRef = |
| 106 | + new SoftReference<>(null); |
103 | 107 |
|
104 | 108 | public static DisabledAlgorithmConstraints certPathConstraints() { |
105 | 109 | return CertPathHolder.CONSTRAINTS; |
@@ -157,7 +161,7 @@ public final boolean permits(Set<CryptoPrimitive> primitives, |
157 | 161 | " or empty."); |
158 | 162 | } |
159 | 163 |
|
160 | | - if (!checkAlgorithm(disabledAlgorithms, algorithm, decomposer)) { |
| 164 | + if (!cachedCheckAlgorithm(algorithm)) { |
161 | 165 | return false; |
162 | 166 | } |
163 | 167 |
|
@@ -240,7 +244,7 @@ public final void permits(String algorithm, ConstraintsParameters cp) |
240 | 244 | // Check if named curves in the key are disabled. |
241 | 245 | for (Key key : cp.getKeys()) { |
242 | 246 | for (String curve : getNamedCurveFromKey(key)) { |
243 | | - if (!checkAlgorithm(disabledAlgorithms, curve, decomposer)) { |
| 247 | + if (!cachedCheckAlgorithm(curve)) { |
244 | 248 | throw new CertPathValidatorException( |
245 | 249 | "Algorithm constraints check failed on disabled " + |
246 | 250 | "algorithm: " + curve, |
@@ -961,6 +965,25 @@ private boolean permitsImpl(Key key) { |
961 | 965 | } |
962 | 966 | } |
963 | 967 |
|
| 968 | + private boolean cachedCheckAlgorithm(String algorithm) { |
| 969 | + Map<String, Boolean> cache; |
| 970 | + if ((cache = cacheRef.get()) == null) { |
| 971 | + synchronized (this) { |
| 972 | + if ((cache = cacheRef.get()) == null) { |
| 973 | + cache = new ConcurrentHashMap<>(); |
| 974 | + cacheRef = new SoftReference<>(cache); |
| 975 | + } |
| 976 | + } |
| 977 | + } |
| 978 | + Boolean result = cache.get(algorithm); |
| 979 | + if (result != null) { |
| 980 | + return result; |
| 981 | + } |
| 982 | + result = checkAlgorithm(disabledAlgorithms, algorithm, decomposer); |
| 983 | + cache.put(algorithm, result); |
| 984 | + return result; |
| 985 | + } |
| 986 | + |
964 | 987 | /* |
965 | 988 | * This constraint is used for the complete disabling of the algorithm. |
966 | 989 | */ |
|
0 commit comments