Skip to content

Commit 4b25717

Browse files
committed
8285398: Cache the results of constraint checks
Reviewed-by: mdoerr Backport-of: 00e9c96d51bec53d4ae8a07c9c98af2c62f3d290
1 parent e4d1d62 commit 4b25717

File tree

1 file changed

+25
-2
lines changed

1 file changed

+25
-2
lines changed

src/java.base/share/classes/sun/security/util/DisabledAlgorithmConstraints.java

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727

2828
import sun.security.validator.Validator;
2929

30+
import java.lang.ref.SoftReference;
3031
import java.security.AlgorithmParameters;
3132
import java.security.CryptoPrimitive;
3233
import java.security.Key;
@@ -53,6 +54,7 @@
5354
import java.util.Collection;
5455
import java.util.StringTokenizer;
5556
import java.util.TimeZone;
57+
import java.util.concurrent.ConcurrentHashMap;
5658
import java.util.regex.Pattern;
5759
import java.util.regex.Matcher;
5860

@@ -100,6 +102,8 @@ private static class JarHolder {
100102

101103
private final Set<String> disabledAlgorithms;
102104
private final Constraints algorithmConstraints;
105+
private volatile SoftReference<Map<String, Boolean>> cacheRef =
106+
new SoftReference<>(null);
103107

104108
public static DisabledAlgorithmConstraints certPathConstraints() {
105109
return CertPathHolder.CONSTRAINTS;
@@ -157,7 +161,7 @@ public final boolean permits(Set<CryptoPrimitive> primitives,
157161
" or empty.");
158162
}
159163

160-
if (!checkAlgorithm(disabledAlgorithms, algorithm, decomposer)) {
164+
if (!cachedCheckAlgorithm(algorithm)) {
161165
return false;
162166
}
163167

@@ -240,7 +244,7 @@ public final void permits(String algorithm, ConstraintsParameters cp)
240244
// Check if named curves in the key are disabled.
241245
for (Key key : cp.getKeys()) {
242246
for (String curve : getNamedCurveFromKey(key)) {
243-
if (!checkAlgorithm(disabledAlgorithms, curve, decomposer)) {
247+
if (!cachedCheckAlgorithm(curve)) {
244248
throw new CertPathValidatorException(
245249
"Algorithm constraints check failed on disabled " +
246250
"algorithm: " + curve,
@@ -961,6 +965,25 @@ private boolean permitsImpl(Key key) {
961965
}
962966
}
963967

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+
964987
/*
965988
* This constraint is used for the complete disabling of the algorithm.
966989
*/

0 commit comments

Comments
 (0)