Skip to content

Commit 00e9c96

Browse files
committed
8285398: Cache the results of constraint checks
Reviewed-by: coffeys, xuelei
1 parent 4bf2c18 commit 00e9c96

File tree

1 file changed

+25
-3
lines changed

1 file changed

+25
-3
lines changed

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

Lines changed: 25 additions & 3 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;
@@ -45,7 +46,6 @@
4546
import java.time.ZoneId;
4647
import java.util.ArrayList;
4748
import java.util.Arrays;
48-
import java.util.Date;
4949
import java.util.HashMap;
5050
import java.util.HashSet;
5151
import java.util.List;
@@ -54,6 +54,7 @@
5454
import java.util.Set;
5555
import java.util.Collection;
5656
import java.util.StringTokenizer;
57+
import java.util.concurrent.ConcurrentHashMap;
5758
import java.util.regex.Pattern;
5859
import java.util.regex.Matcher;
5960

@@ -101,6 +102,8 @@ private static class JarHolder {
101102

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

105108
public static DisabledAlgorithmConstraints certPathConstraints() {
106109
return CertPathHolder.CONSTRAINTS;
@@ -158,7 +161,7 @@ public final boolean permits(Set<CryptoPrimitive> primitives,
158161
" or empty.");
159162
}
160163

161-
if (!checkAlgorithm(disabledAlgorithms, algorithm, decomposer)) {
164+
if (!cachedCheckAlgorithm(algorithm)) {
162165
return false;
163166
}
164167

@@ -241,7 +244,7 @@ public final void permits(String algorithm, ConstraintsParameters cp,
241244
// Check if named curves in the key are disabled.
242245
for (Key key : cp.getKeys()) {
243246
for (String curve : getNamedCurveFromKey(key)) {
244-
if (!checkAlgorithm(disabledAlgorithms, curve, decomposer)) {
247+
if (!cachedCheckAlgorithm(curve)) {
245248
throw new CertPathValidatorException(
246249
"Algorithm constraints check failed on disabled " +
247250
"algorithm: " + curve,
@@ -947,6 +950,25 @@ private boolean permitsImpl(Key key) {
947950
}
948951
}
949952

953+
private boolean cachedCheckAlgorithm(String algorithm) {
954+
Map<String, Boolean> cache;
955+
if ((cache = cacheRef.get()) == null) {
956+
synchronized (this) {
957+
if ((cache = cacheRef.get()) == null) {
958+
cache = new ConcurrentHashMap<>();
959+
cacheRef = new SoftReference<>(cache);
960+
}
961+
}
962+
}
963+
Boolean result = cache.get(algorithm);
964+
if (result != null) {
965+
return result;
966+
}
967+
result = checkAlgorithm(disabledAlgorithms, algorithm, decomposer);
968+
cache.put(algorithm, result);
969+
return result;
970+
}
971+
950972
/*
951973
* This constraint is used for the complete disabling of the algorithm.
952974
*/

0 commit comments

Comments
 (0)