Skip to content

Commit b4f0859

Browse files
committed
8285398: Cache the results of constraint checks
Backport-of: 4b25717
1 parent eb8789b commit b4f0859

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;
@@ -152,7 +156,7 @@ public DisabledAlgorithmConstraints(String propertyName,
152156
@Override
153157
public final boolean permits(Set<CryptoPrimitive> primitives,
154158
String algorithm, AlgorithmParameters parameters) {
155-
if (!checkAlgorithm(disabledAlgorithms, algorithm, decomposer)) {
159+
if (!cachedCheckAlgorithm(algorithm)) {
156160
return false;
157161
}
158162

@@ -235,7 +239,7 @@ public final void permits(String algorithm, ConstraintsParameters cp)
235239
// Check if named curves in the key are disabled.
236240
for (Key key : cp.getKeys()) {
237241
for (String curve : getNamedCurveFromKey(key)) {
238-
if (!checkAlgorithm(disabledAlgorithms, curve, decomposer)) {
242+
if (!cachedCheckAlgorithm(curve)) {
239243
throw new CertPathValidatorException(
240244
"Algorithm constraints check failed on disabled " +
241245
"algorithm: " + curve,
@@ -952,6 +956,25 @@ private boolean permitsImpl(Key key) {
952956
}
953957
}
954958

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+
955978
/*
956979
* This constraint is used for the complete disabling of the algorithm.
957980
*/

0 commit comments

Comments
 (0)