Skip to content

Commit 456735f

Browse files
eastigPaul Hohensee
authored andcommitted
8268427: Improve AlgorithmConstraints:checkAlgorithm performance
Backport-of: 3b83bc1bc331d268987f56ea4f23124a7f6ee38b
1 parent 02ea80a commit 456735f

File tree

4 files changed

+95
-40
lines changed

4 files changed

+95
-40
lines changed

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

Lines changed: 16 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
import java.util.ArrayList;
3333
import java.util.Arrays;
3434
import java.util.Collections;
35+
import java.util.TreeSet;
3536
import java.util.List;
3637
import java.util.Set;
3738

@@ -48,7 +49,7 @@ protected AbstractAlgorithmConstraints(AlgorithmDecomposer decomposer) {
4849
}
4950

5051
// Get algorithm constraints from the specified security property.
51-
static List<String> getAlgorithms(String propertyName) {
52+
static Set<String> getAlgorithms(String propertyName) {
5253
@SuppressWarnings("removal")
5354
String property = AccessController.doPrivileged(
5455
new PrivilegedAction<String>() {
@@ -73,38 +74,30 @@ public String run() {
7374

7475
// map the disabled algorithms
7576
if (algorithmsInProperty == null) {
76-
return Collections.emptyList();
77+
return Collections.emptySet();
7778
}
78-
return new ArrayList<>(Arrays.asList(algorithmsInProperty));
79+
Set<String> algorithmsInPropertySet = new TreeSet<>(String.CASE_INSENSITIVE_ORDER);
80+
algorithmsInPropertySet.addAll(Arrays.asList(algorithmsInProperty));
81+
return algorithmsInPropertySet;
7982
}
8083

81-
static boolean checkAlgorithm(List<String> algorithms, String algorithm,
84+
static boolean checkAlgorithm(Set<String> algorithms, String algorithm,
8285
AlgorithmDecomposer decomposer) {
8386
if (algorithm == null || algorithm.isEmpty()) {
8487
throw new IllegalArgumentException("No algorithm name specified");
8588
}
8689

87-
Set<String> elements = null;
88-
for (String item : algorithms) {
89-
if (item == null || item.isEmpty()) {
90-
continue;
91-
}
92-
93-
// check the full name
94-
if (item.equalsIgnoreCase(algorithm)) {
95-
return false;
96-
}
90+
if (algorithms.contains(algorithm)) {
91+
return false;
92+
}
9793

98-
// decompose the algorithm into sub-elements
99-
if (elements == null) {
100-
elements = decomposer.decompose(algorithm);
101-
}
94+
// decompose the algorithm into sub-elements
95+
Set<String> elements = decomposer.decompose(algorithm);
10296

103-
// check the items of the algorithm
104-
for (String element : elements) {
105-
if (item.equalsIgnoreCase(element)) {
106-
return false;
107-
}
97+
// check the element of the elements
98+
for (String element : elements) {
99+
if (algorithms.contains(element)) {
100+
return false;
108101
}
109102
}
110103

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

Lines changed: 12 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,9 @@ public class DisabledAlgorithmConstraints extends AbstractAlgorithmConstraints {
8585
private static final String PROPERTY_DISABLED_EC_CURVES =
8686
"jdk.disabled.namedCurves";
8787

88+
private static final Pattern INCLUDE_PATTERN = Pattern.compile("include " +
89+
PROPERTY_DISABLED_EC_CURVES, Pattern.CASE_INSENSITIVE);
90+
8891
private static class CertPathHolder {
8992
static final DisabledAlgorithmConstraints CONSTRAINTS =
9093
new DisabledAlgorithmConstraints(PROPERTY_CERTPATH_DISABLED_ALGS);
@@ -95,7 +98,7 @@ private static class JarHolder {
9598
new DisabledAlgorithmConstraints(PROPERTY_JAR_DISABLED_ALGS);
9699
}
97100

98-
private final List<String> disabledAlgorithms;
101+
private final Set<String> disabledAlgorithms;
99102
private final Constraints algorithmConstraints;
100103

101104
public static DisabledAlgorithmConstraints certPathConstraints() {
@@ -130,21 +133,14 @@ public DisabledAlgorithmConstraints(String propertyName,
130133
disabledAlgorithms = getAlgorithms(propertyName);
131134

132135
// Check for alias
133-
int ecindex = -1, i = 0;
134136
for (String s : disabledAlgorithms) {
135-
if (s.regionMatches(true, 0,"include ", 0, 8)) {
136-
if (s.regionMatches(true, 8, PROPERTY_DISABLED_EC_CURVES, 0,
137-
PROPERTY_DISABLED_EC_CURVES.length())) {
138-
ecindex = i;
139-
break;
140-
}
137+
Matcher matcher = INCLUDE_PATTERN.matcher(s);
138+
if (matcher.matches()) {
139+
disabledAlgorithms.remove(matcher.group());
140+
disabledAlgorithms.addAll(
141+
getAlgorithms(PROPERTY_DISABLED_EC_CURVES));
142+
break;
141143
}
142-
i++;
143-
}
144-
if (ecindex > -1) {
145-
disabledAlgorithms.remove(ecindex);
146-
disabledAlgorithms.addAll(ecindex,
147-
getAlgorithms(PROPERTY_DISABLED_EC_CURVES));
148144
}
149145
algorithmConstraints = new Constraints(propertyName, disabledAlgorithms);
150146
}
@@ -332,8 +328,8 @@ private static class Holder {
332328
"denyAfter\\s+(\\d{4})-(\\d{2})-(\\d{2})");
333329
}
334330

335-
public Constraints(String propertyName, List<String> constraintArray) {
336-
for (String constraintEntry : constraintArray) {
331+
public Constraints(String propertyName, Set<String> constraintSet) {
332+
for (String constraintEntry : constraintSet) {
337333
if (constraintEntry == null || constraintEntry.isEmpty()) {
338334
continue;
339335
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ public class LegacyAlgorithmConstraints extends AbstractAlgorithmConstraints {
4040
public static final String PROPERTY_TLS_LEGACY_ALGS =
4141
"jdk.tls.legacyAlgorithms";
4242

43-
private final List<String> legacyAlgorithms;
43+
private final Set<String> legacyAlgorithms;
4444

4545
public LegacyAlgorithmConstraints(String propertyName,
4646
AlgorithmDecomposer decomposer) {
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
/*
2+
* Copyright (c) 2021, Huawei Technologies Co., Ltd. All rights reserved.
3+
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4+
*
5+
* This code is free software; you can redistribute it and/or modify it
6+
* under the terms of the GNU General Public License version 2 only, as
7+
* published by the Free Software Foundation.
8+
*
9+
* This code is distributed in the hope that it will be useful, but WITHOUT
10+
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11+
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12+
* version 2 for more details (a copy is included in the LICENSE file that
13+
* accompanied this code).
14+
*
15+
* You should have received a copy of the GNU General Public License version
16+
* 2 along with this work; if not, write to the Free Software Foundation,
17+
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18+
*
19+
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20+
* or visit www.oracle.com if you need additional information or have any
21+
* questions.
22+
*/
23+
package org.openjdk.bench.java.security;
24+
25+
import org.openjdk.jmh.annotations.Benchmark;
26+
import org.openjdk.jmh.annotations.BenchmarkMode;
27+
import org.openjdk.jmh.annotations.Fork;
28+
import org.openjdk.jmh.annotations.Mode;
29+
import org.openjdk.jmh.annotations.OutputTimeUnit;
30+
import org.openjdk.jmh.annotations.Param;
31+
import org.openjdk.jmh.annotations.Scope;
32+
import org.openjdk.jmh.annotations.Setup;
33+
import org.openjdk.jmh.annotations.State;
34+
import sun.security.util.DisabledAlgorithmConstraints;
35+
36+
import java.security.AlgorithmConstraints;
37+
import java.security.CryptoPrimitive;
38+
import java.util.concurrent.TimeUnit;
39+
import java.util.EnumSet;
40+
import java.util.Set;
41+
42+
import static sun.security.util.DisabledAlgorithmConstraints.PROPERTY_TLS_DISABLED_ALGS;
43+
44+
@BenchmarkMode(Mode.AverageTime)
45+
@OutputTimeUnit(TimeUnit.NANOSECONDS)
46+
@Fork(jvmArgsAppend = {"--add-exports", "java.base/sun.security.util=ALL-UNNAMED"})
47+
@State(Scope.Thread)
48+
public class AlgorithmConstraintsPermits {
49+
50+
AlgorithmConstraints tlsDisabledAlgConstraints;
51+
Set<CryptoPrimitive> primitives = EnumSet.of(CryptoPrimitive.KEY_AGREEMENT);
52+
53+
@Param({"SSLv3", "DES", "NULL", "TLS1.3"})
54+
String algorithm;
55+
56+
@Setup
57+
public void setup() {
58+
tlsDisabledAlgConstraints = new DisabledAlgorithmConstraints(PROPERTY_TLS_DISABLED_ALGS);
59+
}
60+
61+
@Benchmark
62+
public boolean permits() {
63+
return tlsDisabledAlgConstraints.permits(primitives, algorithm, null);
64+
}
65+
}
66+

0 commit comments

Comments
 (0)