|
36 | 36 | public class HttpService { |
37 | 37 | // For debugging purposes |
38 | 38 | private static final boolean VERBOSE_REQUESTS = false; |
39 | | - protected static SSLSecurityProtocol sslSecurityProtocol = SSLSecurityProtocol.SSLv3; |
| 39 | + public static boolean useTLS=false; |
| 40 | + protected static SSLSecurityProtocol sslSecurityProtocol = null; |
40 | 41 | private static SSLSocketFactory sslSocketFactory = createSSLFactory(); |
41 | 42 | private static String HTTPS_SCHEME = "https"; |
42 | 43 | private static String HTTP_SCHEME = "http"; |
| 44 | + private static String HOSTNAME = "localhost"; |
43 | 45 |
|
44 | 46 | private static final HostnameVerifier HOSTNAME_VERIFIER = new HostnameVerifier() { |
45 | 47 | public boolean verify(String s, SSLSession sslSession) { |
46 | | - return true; |
| 48 | + if (s.equals(HOSTNAME)) { |
| 49 | + return true; |
| 50 | + } else { |
| 51 | + HostnameVerifier hv = HttpsURLConnection.getDefaultHostnameVerifier(); |
| 52 | + return hv.verify(s, sslSession); |
| 53 | + } |
47 | 54 | } |
48 | 55 | }; |
49 | 56 |
|
@@ -204,7 +211,7 @@ public static void setSslSecurityProtocol(SSLSecurityProtocol securityProtocol) |
204 | 211 | // Only update the SSL_SOCKET_FACTORY if changing protocols |
205 | 212 | if (sslSecurityProtocol != securityProtocol) { |
206 | 213 | sslSecurityProtocol = securityProtocol; |
207 | | - sslSocketFactory = new SplunkHttpsSocketFactory(createSSLFactory(), securityProtocol); |
| 214 | + sslSocketFactory = new SplunkHttpsSocketFactory(createSSLFactory()); |
208 | 215 | } |
209 | 216 | } |
210 | 217 |
|
@@ -528,41 +535,43 @@ public void checkServerTrusted(X509Certificate[] certs, String authType) { |
528 | 535 | } |
529 | 536 | }; |
530 | 537 | try { |
531 | | - SSLContext context; |
532 | | - switch (HttpService.sslSecurityProtocol) { |
533 | | - case TLSv1_2: |
534 | | - case TLSv1_1: |
535 | | - case TLSv1: |
536 | | - context = SSLContext.getInstance("TLS"); |
537 | | - break; |
538 | | - default: |
539 | | - context = SSLContext.getInstance("SSL"); |
| 538 | + String contextStr = ""; |
| 539 | + if (sslSecurityProtocol != null) { |
| 540 | + contextStr = sslSecurityProtocol.toString().contains("SSL") ? "SSL" : "TLS"; |
| 541 | + } else if (useTLS || System.getProperty("java.version").compareTo("1.8") >= 0) { |
| 542 | + contextStr = "TLS"; |
| 543 | + } else { |
| 544 | + contextStr = "SSL"; |
540 | 545 | } |
| 546 | + SSLContext context = SSLContext.getInstance(contextStr); |
541 | 547 |
|
542 | 548 | context.init(null, trustAll, new java.security.SecureRandom()); |
543 | | - return new SplunkHttpsSocketFactory(context.getSocketFactory(), HttpService.sslSecurityProtocol); |
| 549 | + return new SplunkHttpsSocketFactory(context.getSocketFactory()); |
544 | 550 | } catch (Exception e) { |
545 | 551 | throw new RuntimeException("Error setting up SSL socket factory: " + e, e); |
546 | 552 | } |
547 | 553 | } |
548 | 554 |
|
549 | 555 | private static final class SplunkHttpsSocketFactory extends SSLSocketFactory { |
550 | 556 | private final SSLSocketFactory delegate; |
551 | | - private SSLSecurityProtocol sslSecurityProtocol; |
552 | 557 |
|
553 | | - private SplunkHttpsSocketFactory(SSLSocketFactory delegate) { |
554 | | - this.delegate = delegate; |
555 | | - this.sslSecurityProtocol = HttpService.sslSecurityProtocol; |
556 | | - } |
| 558 | + public static String[] PROTOCOLS = {"SSLv3"}; |
| 559 | + public static String[] PROTOCOLS_TLS = {"TLSv1.3", "TLSv1.2", "TLSv1.1", "TLSv1"}; |
557 | 560 |
|
558 | | - private SplunkHttpsSocketFactory(SSLSocketFactory delegate, SSLSecurityProtocol securityProtocol) { |
| 561 | + private SplunkHttpsSocketFactory(SSLSocketFactory delegate) { |
559 | 562 | this.delegate = delegate; |
560 | | - this.sslSecurityProtocol = securityProtocol; |
561 | 563 | } |
562 | 564 |
|
563 | 565 | private Socket configure(Socket socket) { |
564 | 566 | if (socket instanceof SSLSocket) { |
565 | | - ((SSLSocket) socket).setEnabledProtocols(new String[]{sslSecurityProtocol.toString()}); |
| 567 | + if (sslSecurityProtocol != null) { |
| 568 | + String[] protocols = {sslSecurityProtocol.toString()}; |
| 569 | + ((SSLSocket) socket).setEnabledProtocols(protocols); |
| 570 | + } else if (useTLS || System.getProperty("java.version").compareTo("1.8") >= 0) { |
| 571 | + ((SSLSocket) socket).setEnabledProtocols(PROTOCOLS_TLS); |
| 572 | + } else { |
| 573 | + ((SSLSocket) socket).setEnabledProtocols(PROTOCOLS); |
| 574 | + } |
566 | 575 | } |
567 | 576 | return socket; |
568 | 577 | } |
|
0 commit comments