25
25
26
26
package sun .security .ssl ;
27
27
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
+
34
30
import static sun .security .ssl .CipherSuite .HashAlg .*;
35
31
import static sun .security .ssl .CipherSuite .KeyExchange .*;
36
32
import static sun .security .ssl .CipherSuite .MacAlg .*;
@@ -861,6 +857,39 @@ enum CipherSuite {
861
857
862
858
final boolean exportable ;
863
859
860
+ private static final Map <Integer , CipherSuite > cipherSuiteIds ;
861
+ private static final Map <String , CipherSuite > cipherSuiteNames ;
862
+ private static final List <CipherSuite > allowedCipherSuites ;
863
+ private static final List <CipherSuite > defaultCipherSuites ;
864
+
865
+ static {
866
+ Map <Integer , CipherSuite > ids = new HashMap <>();
867
+ Map <String , CipherSuite > names = new HashMap <>();
868
+ List <CipherSuite > allowedCS = new ArrayList <>();
869
+ List <CipherSuite > defaultCS = new ArrayList <>();
870
+
871
+ for (CipherSuite cs : CipherSuite .values ()) {
872
+ ids .put (cs .id , cs );
873
+ names .put (cs .name , cs );
874
+ for (String alias : cs .aliases ) {
875
+ names .put (alias , cs );
876
+ }
877
+
878
+ if (!cs .supportedProtocols .isEmpty ()) {
879
+ allowedCS .add (cs );
880
+ }
881
+
882
+ if (cs .isDefaultEnabled ) {
883
+ defaultCS .add (cs );
884
+ }
885
+ }
886
+
887
+ cipherSuiteIds = Map .copyOf (ids );
888
+ cipherSuiteNames = Map .copyOf (names );
889
+ allowedCipherSuites = List .copyOf (allowedCS );
890
+ defaultCipherSuites = List .copyOf (defaultCS );
891
+ }
892
+
864
893
// known but unsupported cipher suite
865
894
private CipherSuite (String name , int id ) {
866
895
this (id , false , name , "" ,
@@ -898,62 +927,29 @@ private CipherSuite(int id, boolean isDefaultEnabled,
898
927
}
899
928
900
929
static CipherSuite nameOf (String ciperSuiteName ) {
901
- for (CipherSuite cs : CipherSuite .values ()) {
902
- if (cs .name .equals (ciperSuiteName ) ||
903
- cs .aliases .contains (ciperSuiteName )) {
904
- return cs ;
905
- }
906
- }
907
-
908
- return null ;
930
+ return cipherSuiteNames .get (ciperSuiteName );
909
931
}
910
932
911
933
static CipherSuite valueOf (int id ) {
912
- for (CipherSuite cs : CipherSuite .values ()) {
913
- if (cs .id == id ) {
914
- return cs ;
915
- }
916
- }
917
-
918
- return null ;
934
+ return cipherSuiteIds .get (id );
919
935
}
920
936
921
937
static String nameOf (int id ) {
922
- for ( CipherSuite cs : CipherSuite . values ()) {
923
- if ( cs . id == id ) {
924
- return cs . name ;
925
- }
938
+ CipherSuite cs = cipherSuiteIds . get ( id );
939
+
940
+ if ( cs != null ) {
941
+ return cs . name ;
926
942
}
927
943
928
944
return "UNKNOWN-CIPHER-SUITE(" + Utilities .byte16HexString (id ) + ")" ;
929
945
}
930
946
931
947
static Collection <CipherSuite > allowedCipherSuites () {
932
- Collection <CipherSuite > cipherSuites = new LinkedList <>();
933
- for (CipherSuite cs : CipherSuite .values ()) {
934
- if (!cs .supportedProtocols .isEmpty ()) {
935
- cipherSuites .add (cs );
936
- } else {
937
- // values() is ordered, remaining cipher suites are
938
- // not supported.
939
- break ;
940
- }
941
- }
942
- return cipherSuites ;
948
+ return allowedCipherSuites ;
943
949
}
944
950
945
951
static Collection <CipherSuite > defaultCipherSuites () {
946
- Collection <CipherSuite > cipherSuites = new LinkedList <>();
947
- for (CipherSuite cs : CipherSuite .values ()) {
948
- if (cs .isDefaultEnabled ) {
949
- cipherSuites .add (cs );
950
- } else {
951
- // values() is ordered, remaining cipher suites are
952
- // not enabled.
953
- break ;
954
- }
955
- }
956
- return cipherSuites ;
952
+ return defaultCipherSuites ;
957
953
}
958
954
959
955
/**
@@ -976,19 +972,11 @@ static List<CipherSuite> validValuesOf(String[] names) {
976
972
}
977
973
978
974
boolean found = false ;
979
- for (CipherSuite cs : CipherSuite .values ()) {
980
- if (!cs .supportedProtocols .isEmpty ()) {
981
- if (cs .name .equals (name ) ||
982
- cs .aliases .contains (name )) {
983
- cipherSuites .add (cs );
984
- found = true ;
985
- break ;
986
- }
987
- } else {
988
- // values() is ordered, remaining cipher suites are
989
- // not supported.
990
- break ;
991
- }
975
+ CipherSuite cs ;
976
+ if ((cs = cipherSuiteNames .get (name )) != null
977
+ && !cs .supportedProtocols .isEmpty ()) {
978
+ cipherSuites .add (cs );
979
+ found = true ;
992
980
}
993
981
if (!found ) {
994
982
throw new IllegalArgumentException (
0 commit comments