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