|
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; |
@@ -152,7 +156,7 @@ public DisabledAlgorithmConstraints(String propertyName, |
152 | 156 | @Override |
153 | 157 | public final boolean permits(Set<CryptoPrimitive> primitives, |
154 | 158 | String algorithm, AlgorithmParameters parameters) { |
155 | | - if (!checkAlgorithm(disabledAlgorithms, algorithm, decomposer)) { |
| 159 | + if (!cachedCheckAlgorithm(algorithm)) { |
156 | 160 | return false; |
157 | 161 | } |
158 | 162 |
|
@@ -235,7 +239,7 @@ public final void permits(String algorithm, ConstraintsParameters cp) |
235 | 239 | // Check if named curves in the key are disabled. |
236 | 240 | for (Key key : cp.getKeys()) { |
237 | 241 | for (String curve : getNamedCurveFromKey(key)) { |
238 | | - if (!checkAlgorithm(disabledAlgorithms, curve, decomposer)) { |
| 242 | + if (!cachedCheckAlgorithm(curve)) { |
239 | 243 | throw new CertPathValidatorException( |
240 | 244 | "Algorithm constraints check failed on disabled " + |
241 | 245 | "algorithm: " + curve, |
@@ -952,6 +956,25 @@ private boolean permitsImpl(Key key) { |
952 | 956 | } |
953 | 957 | } |
954 | 958 |
|
| 959 | + private boolean cachedCheckAlgorithm(String algorithm) { |
| 960 | + Map<String, Boolean> cache; |
| 961 | + if ((cache = cacheRef.get()) == null) { |
| 962 | + synchronized (this) { |
| 963 | + if ((cache = cacheRef.get()) == null) { |
| 964 | + cache = new ConcurrentHashMap<>(); |
| 965 | + cacheRef = new SoftReference<>(cache); |
| 966 | + } |
| 967 | + } |
| 968 | + } |
| 969 | + Boolean result = cache.get(algorithm); |
| 970 | + if (result != null) { |
| 971 | + return result; |
| 972 | + } |
| 973 | + result = checkAlgorithm(disabledAlgorithms, algorithm, decomposer); |
| 974 | + cache.put(algorithm, result); |
| 975 | + return result; |
| 976 | + } |
| 977 | + |
955 | 978 | /* |
956 | 979 | * This constraint is used for the complete disabling of the algorithm. |
957 | 980 | */ |
|
0 commit comments