Skip to content

Commit bbf2e9a

Browse files
author
Alexey Bakhtin
committed
8285398: Cache the results of constraint checks
Reviewed-by: phh Backport-of: 00e9c96d51bec53d4ae8a07c9c98af2c62f3d290
1 parent 0bc5d68 commit bbf2e9a

File tree

1 file changed

+25
-2
lines changed

1 file changed

+25
-2
lines changed

jdk/src/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.io.ByteArrayOutputStream;
3132
import java.io.PrintStream;
3233
import java.security.AlgorithmParameters;
@@ -55,6 +56,7 @@
5556
import java.util.Collection;
5657
import java.util.Collections;
5758
import java.util.StringTokenizer;
59+
import java.util.concurrent.ConcurrentHashMap;
5860
import java.util.regex.Pattern;
5961
import java.util.regex.Matcher;
6062

@@ -99,6 +101,8 @@ private static class JarHolder {
99101

100102
private final List<String> disabledAlgorithms;
101103
private final Constraints algorithmConstraints;
104+
private volatile SoftReference<Map<String, Boolean>> cacheRef =
105+
new SoftReference<>(null);
102106

103107
public static DisabledAlgorithmConstraints certPathConstraints() {
104108
return CertPathHolder.CONSTRAINTS;
@@ -158,7 +162,7 @@ public DisabledAlgorithmConstraints(String propertyName,
158162
@Override
159163
public final boolean permits(Set<CryptoPrimitive> primitives,
160164
String algorithm, AlgorithmParameters parameters) {
161-
if (!checkAlgorithm(disabledAlgorithms, algorithm, decomposer)) {
165+
if (!cachedCheckAlgorithm(algorithm)) {
162166
return false;
163167
}
164168

@@ -242,7 +246,7 @@ public final void permits(String algorithm, ConstraintsParameters cp,
242246
// Check if named curves in the key are disabled.
243247
for (Key key : cp.getKeys()) {
244248
for (String curve : getNamedCurveFromKey(key)) {
245-
if (!checkAlgorithm(disabledAlgorithms, curve, decomposer)) {
249+
if (!cachedCheckAlgorithm(curve)) {
246250
throw new CertPathValidatorException(
247251
"Algorithm constraints check failed on disabled " +
248252
"algorithm: " + curve,
@@ -950,6 +954,25 @@ private boolean permitsImpl(Key key) {
950954
}
951955
}
952956

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

0 commit comments

Comments
 (0)