3636import java .util .ArrayList ;
3737import java .util .Arrays ;
3838import java .util .List ;
39- import java .util .Objects ;
4039import java .util .concurrent .atomic .AtomicReference ;
4140import java .util .function .Supplier ;
42- import java .util .stream .Collectors ;
4341import javax .net .ssl .CertPathTrustManagerParameters ;
4442import javax .net .ssl .KeyManager ;
4543import javax .net .ssl .KeyManagerFactory ;
4644import javax .net .ssl .SSLContext ;
4745import javax .net .ssl .SSLServerSocket ;
48- import javax .net .ssl .SSLServerSocketFactory ;
4946import javax .net .ssl .SSLSocket ;
5047import javax .net .ssl .TrustManager ;
5148import javax .net .ssl .TrustManagerFactory ;
6259
6360/**
6461 * Utility code for X509 handling
65- *
66- * Default cipher suites:
67- *
68- * Performance testing done by Facebook engineers shows that on Intel x86_64 machines, Java9 performs better with
69- * GCM and Java8 performs better with CBC, so these seem like reasonable defaults.
7062 */
7163public abstract class X509Util implements Closeable , AutoCloseable {
7264
@@ -102,6 +94,8 @@ private static String defaultTlsProtocol() {
10294 List <String > supported = new ArrayList <>();
10395 try {
10496 supported = Arrays .asList (SSLContext .getDefault ().getSupportedSSLParameters ().getProtocols ());
97+ // We cannot use the default protocols directly, because the SSLContext factory methods
98+ // only accept a single protocol
10599 if (supported .contains (TLS_1_3 )) {
106100 defaultProtocol = TLS_1_3 ;
107101 }
@@ -112,36 +106,6 @@ private static String defaultTlsProtocol() {
112106 return defaultProtocol ;
113107 }
114108
115- // ChaCha20 was introduced in OpenJDK 11.0.15 and it is not supported by JDK8.
116- private static String [] getTLSv13Ciphers () {
117- return new String []{"TLS_AES_256_GCM_SHA384" , "TLS_AES_128_GCM_SHA256" , "TLS_CHACHA20_POLY1305_SHA256" };
118- }
119-
120- private static String [] getGCMCiphers () {
121- return new String []{"TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256" , "TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256" , "TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384" , "TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384" };
122- }
123-
124- private static String [] getCBCCiphers () {
125- return new String []{"TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA256" , "TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256" , "TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA" , "TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA" , "TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA384" , "TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384" , "TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA" , "TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA" };
126- }
127-
128- /**
129- * Returns a filtered set of ciphers, where ciphers not supported by the JDK are removed.
130- */
131- private static String [] getSupportedCiphers (String []... cipherLists ) {
132- List <String > supported = Arrays .asList (
133- ((SSLServerSocketFactory ) SSLServerSocketFactory .getDefault ()).getSupportedCipherSuites ());
134-
135- return Arrays .stream (cipherLists ).flatMap (Arrays ::stream ).filter (supported ::contains ).collect (Collectors .toList ()).toArray (new String [0 ]);
136- }
137-
138- // On Java 8, prefer CBC ciphers since AES-NI support is lacking and GCM is slower than CBC.
139- private static final String [] DEFAULT_CIPHERS_JAVA8 = getSupportedCiphers (getCBCCiphers (), getGCMCiphers (), getTLSv13Ciphers ());
140- // On Java 9 and later, prefer GCM ciphers due to improved AES-NI support.
141- // Note that this performance assumption might not hold true for architectures other than x86_64.
142- // TLSv1.3 ciphers can be added at the end of the list without impacting the priority of TLSv1.3 vs TLSv1.2.
143- private static final String [] DEFAULT_CIPHERS_JAVA9 = getSupportedCiphers (getGCMCiphers (), getCBCCiphers (), getTLSv13Ciphers ());
144-
145109 public static final int DEFAULT_HANDSHAKE_DETECTION_TIMEOUT_MILLIS = 5000 ;
146110
147111 /**
@@ -636,26 +600,6 @@ public SSLServerSocket createSSLServerSocket(int port) throws X509Exception, IOE
636600 return getDefaultSSLContextAndOptions ().createSSLServerSocket (port );
637601 }
638602
639- static String [] getDefaultCipherSuites () {
640- return getDefaultCipherSuitesForJavaVersion (System .getProperty ("java.specification.version" ));
641- }
642-
643- static String [] getDefaultCipherSuitesForJavaVersion (String javaVersion ) {
644- Objects .requireNonNull (javaVersion );
645- if (javaVersion .matches ("\\ d+" )) {
646- // Must be Java 9 or later
647- LOG .debug ("Using Java9+ optimized cipher suites for Java version {}" , javaVersion );
648- return DEFAULT_CIPHERS_JAVA9 ;
649- } else if (javaVersion .startsWith ("1." )) {
650- // Must be Java 1.8 or earlier
651- LOG .debug ("Using Java8 optimized cipher suites for Java version {}" , javaVersion );
652- return DEFAULT_CIPHERS_JAVA8 ;
653- } else {
654- LOG .debug ("Could not parse java version {}, using Java8 optimized cipher suites" , javaVersion );
655- return DEFAULT_CIPHERS_JAVA8 ;
656- }
657- }
658-
659603 private FileChangeWatcher newFileChangeWatcher (String fileLocation ) throws IOException {
660604 if (fileLocation == null || fileLocation .isEmpty ()) {
661605 return null ;
0 commit comments