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