Skip to content

Commit

Permalink
Merge upstream-dev
Browse files Browse the repository at this point in the history
  • Loading branch information
corretto-github-robot committed Oct 18, 2023
2 parents 2534042 + c851317 commit e979d57
Showing 1 changed file with 28 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@

import sun.security.validator.Validator;

import java.lang.ref.SoftReference;
import java.io.ByteArrayOutputStream;
import java.io.PrintStream;
import java.security.AlgorithmParameters;
Expand Down Expand Up @@ -55,6 +56,7 @@
import java.util.Collection;
import java.util.Collections;
import java.util.StringTokenizer;
import java.util.concurrent.ConcurrentHashMap;
import java.util.regex.Pattern;
import java.util.regex.Matcher;

Expand Down Expand Up @@ -99,6 +101,8 @@ private static class JarHolder {

private final List<String> disabledAlgorithms;
private final Constraints algorithmConstraints;
private volatile SoftReference<Map<String, Boolean>> cacheRef =
new SoftReference<>(null);

public static DisabledAlgorithmConstraints certPathConstraints() {
return CertPathHolder.CONSTRAINTS;
Expand Down Expand Up @@ -158,7 +162,10 @@ public DisabledAlgorithmConstraints(String propertyName,
@Override
public final boolean permits(Set<CryptoPrimitive> primitives,
String algorithm, AlgorithmParameters parameters) {
if (!checkAlgorithm(disabledAlgorithms, algorithm, decomposer)) {
if (algorithm == null || algorithm.isEmpty()) {
throw new IllegalArgumentException("No algorithm name specified");
}
if (!cachedCheckAlgorithm(algorithm)) {
return false;
}

Expand Down Expand Up @@ -242,7 +249,7 @@ public final void permits(String algorithm, ConstraintsParameters cp,
// Check if named curves in the key are disabled.
for (Key key : cp.getKeys()) {
for (String curve : getNamedCurveFromKey(key)) {
if (!checkAlgorithm(disabledAlgorithms, curve, decomposer)) {
if (!cachedCheckAlgorithm(curve)) {
throw new CertPathValidatorException(
"Algorithm constraints check failed on disabled " +
"algorithm: " + curve,
Expand Down Expand Up @@ -950,6 +957,25 @@ private boolean permitsImpl(Key key) {
}
}

private boolean cachedCheckAlgorithm(String algorithm) {
Map<String, Boolean> cache;
if ((cache = cacheRef.get()) == null) {
synchronized (this) {
if ((cache = cacheRef.get()) == null) {
cache = new ConcurrentHashMap<>();
cacheRef = new SoftReference<>(cache);
}
}
}
Boolean result = cache.get(algorithm);
if (result != null) {
return result;
}
result = checkAlgorithm(disabledAlgorithms, algorithm, decomposer);
cache.put(algorithm, result);
return result;
}

/*
* This constraint is used for the complete disabling of the algorithm.
*/
Expand Down

0 comments on commit e979d57

Please sign in to comment.