Skip to content

Commit aed31d9

Browse files
author
Paul Hohensee
committed
8270317: Large Allocation in CipherSuite
Reviewed-by: simonis, mbalao Backport-of: e627caec84c169c99c04e0d355c29b806a0266ed
1 parent 713c020 commit aed31d9

File tree

1 file changed

+48
-60
lines changed

1 file changed

+48
-60
lines changed

jdk/src/share/classes/sun/security/ssl/CipherSuite.java

+48-60
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,8 @@
2525

2626
package sun.security.ssl;
2727

28-
import java.util.ArrayList;
29-
import java.util.Arrays;
30-
import java.util.Collection;
31-
import java.util.Collections;
32-
import java.util.LinkedList;
33-
import java.util.List;
28+
import java.util.*;
29+
3430
import static sun.security.ssl.CipherSuite.HashAlg.*;
3531
import static sun.security.ssl.CipherSuite.KeyExchange.*;
3632
import static sun.security.ssl.CipherSuite.MacAlg.*;
@@ -874,6 +870,39 @@ enum CipherSuite {
874870

875871
final boolean exportable;
876872

873+
private static final Map<Integer, CipherSuite> cipherSuiteIds;
874+
private static final Map<String, CipherSuite> cipherSuiteNames;
875+
private static final List<CipherSuite> allowedCipherSuites;
876+
private static final List<CipherSuite> defaultCipherSuites;
877+
878+
static {
879+
Map<Integer, CipherSuite> ids = new HashMap<>();
880+
Map<String, CipherSuite> names = new HashMap<>();
881+
List<CipherSuite> allowedCS = new ArrayList<>();
882+
List<CipherSuite> defaultCS = new ArrayList<>();
883+
884+
for(CipherSuite cs : CipherSuite.values()) {
885+
ids.put(cs.id, cs);
886+
names.put(cs.name, cs);
887+
for (String alias : cs.aliases) {
888+
names.put(alias, cs);
889+
}
890+
891+
if (!cs.supportedProtocols.isEmpty()) {
892+
allowedCS.add(cs);
893+
}
894+
895+
if (cs.isDefaultEnabled) {
896+
defaultCS.add(cs);
897+
}
898+
}
899+
900+
cipherSuiteIds = Collections.unmodifiableMap(ids);
901+
cipherSuiteNames = Collections.unmodifiableMap(names);
902+
allowedCipherSuites = Collections.unmodifiableList(allowedCS);
903+
defaultCipherSuites = Collections.unmodifiableList(defaultCS);
904+
}
905+
877906
// known but unsupported cipher suite
878907
private CipherSuite(String name, int id) {
879908
this(id, false, name, "",
@@ -911,62 +940,29 @@ private CipherSuite(int id, boolean isDefaultEnabled,
911940
}
912941

913942
static CipherSuite nameOf(String ciperSuiteName) {
914-
for (CipherSuite cs : CipherSuite.values()) {
915-
if (cs.name.equals(ciperSuiteName) ||
916-
cs.aliases.contains(ciperSuiteName)) {
917-
return cs;
918-
}
919-
}
920-
921-
return null;
943+
return cipherSuiteNames.get(ciperSuiteName);
922944
}
923945

924946
static CipherSuite valueOf(int id) {
925-
for (CipherSuite cs : CipherSuite.values()) {
926-
if (cs.id == id) {
927-
return cs;
928-
}
929-
}
930-
931-
return null;
947+
return cipherSuiteIds.get(id);
932948
}
933949

934950
static String nameOf(int id) {
935-
for (CipherSuite cs : CipherSuite.values()) {
936-
if (cs.id == id) {
937-
return cs.name;
938-
}
951+
CipherSuite cs = cipherSuiteIds.get(id);
952+
953+
if (cs != null) {
954+
return cs.name;
939955
}
940956

941957
return "UNKNOWN-CIPHER-SUITE(" + Utilities.byte16HexString(id) + ")";
942958
}
943959

944960
static Collection<CipherSuite> allowedCipherSuites() {
945-
Collection<CipherSuite> cipherSuites = new LinkedList<>();
946-
for (CipherSuite cs : CipherSuite.values()) {
947-
if (!cs.supportedProtocols.isEmpty()) {
948-
cipherSuites.add(cs);
949-
} else {
950-
// values() is ordered, remaining cipher suites are
951-
// not supported.
952-
break;
953-
}
954-
}
955-
return cipherSuites;
961+
return allowedCipherSuites;
956962
}
957963

958964
static Collection<CipherSuite> defaultCipherSuites() {
959-
Collection<CipherSuite> cipherSuites = new LinkedList<>();
960-
for (CipherSuite cs : CipherSuite.values()) {
961-
if (cs.isDefaultEnabled) {
962-
cipherSuites.add(cs);
963-
} else {
964-
// values() is ordered, remaining cipher suites are
965-
// not enabled.
966-
break;
967-
}
968-
}
969-
return cipherSuites;
965+
return defaultCipherSuites;
970966
}
971967

972968
/**
@@ -989,19 +985,11 @@ static List<CipherSuite> validValuesOf(String[] names) {
989985
}
990986

991987
boolean found = false;
992-
for (CipherSuite cs : CipherSuite.values()) {
993-
if (!cs.supportedProtocols.isEmpty()) {
994-
if (cs.name.equals(name) ||
995-
cs.aliases.contains(name)) {
996-
cipherSuites.add(cs);
997-
found = true;
998-
break;
999-
}
1000-
} else {
1001-
// values() is ordered, remaining cipher suites are
1002-
// not supported.
1003-
break;
1004-
}
988+
CipherSuite cs;
989+
if ((cs = cipherSuiteNames.get(name)) != null
990+
&& !cs.supportedProtocols.isEmpty()) {
991+
cipherSuites.add(cs);
992+
found = true;
1005993
}
1006994
if (!found) {
1007995
throw new IllegalArgumentException(

0 commit comments

Comments
 (0)