|
37 | 37 | public class HttpService { |
38 | 38 | // For debugging purposes |
39 | 39 | private static final boolean VERBOSE_REQUESTS = false; |
40 | | - public static boolean useTLS=false; |
41 | 40 | protected static SSLSecurityProtocol sslSecurityProtocol = null; |
| 41 | + |
| 42 | + /** |
| 43 | + * Boolean flag for validating certificates at either of the sides (client/server). |
| 44 | + * If true, then it will check and validate relevant certificates otherwise, in case of false, it will accept all certificates. |
| 45 | + * For PROD environment, TRUE is strongly recommended, whereas working in localhost OR development environment, FALSE is used. |
| 46 | + * Default Value: TRUE |
| 47 | + */ |
| 48 | + protected static boolean validateCertificates = true; |
| 49 | + |
42 | 50 | private static SSLSocketFactory sslSocketFactory = createSSLFactory(); |
43 | 51 | private static String HTTPS_SCHEME = "https"; |
44 | 52 | private static String HTTP_SCHEME = "http"; |
@@ -225,7 +233,7 @@ public static void setSslSecurityProtocol(SSLSecurityProtocol securityProtocol) |
225 | 233 | // Only update the SSL_SOCKET_FACTORY if changing protocols |
226 | 234 | if (sslSecurityProtocol != securityProtocol) { |
227 | 235 | sslSecurityProtocol = securityProtocol; |
228 | | - sslSocketFactory = new SplunkHttpsSocketFactory(createSSLFactory()); |
| 236 | + sslSocketFactory = createSSLFactory(); |
229 | 237 | } |
230 | 238 | } |
231 | 239 |
|
@@ -429,7 +437,6 @@ Socket open() throws IOException { |
429 | 437 | public ResponseMessage send(String path, RequestMessage request) { |
430 | 438 | // Construct a full URL to the resource |
431 | 439 | URL url = getUrl(path); |
432 | | - |
433 | 440 | // Create and initialize the connection object |
434 | 441 | HttpURLConnection cn; |
435 | 442 | try { |
@@ -550,100 +557,47 @@ public static SSLSocketFactory getSSLSocketFactory() { |
550 | 557 | return HttpService.sslSocketFactory; |
551 | 558 | } |
552 | 559 |
|
| 560 | + public static void setValidateCertificates(boolean validateCertificates) { |
| 561 | + HttpService.validateCertificates = validateCertificates; |
| 562 | + } |
| 563 | + |
553 | 564 | public static SSLSocketFactory createSSLFactory() { |
554 | | - TrustManager[] trustAll = new TrustManager[]{ |
555 | | - new X509TrustManager() { |
556 | | - public X509Certificate[] getAcceptedIssuers() { |
557 | | - return null; |
558 | | - } |
559 | | - |
560 | | - public void checkClientTrusted(X509Certificate[] certs, String authType) { |
561 | | - } |
562 | | - |
563 | | - public void checkServerTrusted(X509Certificate[] certs, String authType) { |
564 | | - } |
565 | | - } |
566 | | - }; |
| 565 | + |
567 | 566 | try { |
568 | | - String contextStr = ""; |
| 567 | + SSLContext context; |
569 | 568 | if (sslSecurityProtocol != null) { |
570 | | - contextStr = sslSecurityProtocol.toString().contains("SSL") ? "SSL" : "TLS"; |
571 | | - } else if (useTLS || System.getProperty("java.version").compareTo("1.8") >= 0) { |
572 | | - contextStr = "TLS"; |
| 569 | + String contextStr = sslSecurityProtocol.toString().contains("SSL") ? "SSL" : "TLS"; |
| 570 | + context = SSLContext.getInstance(contextStr); |
| 571 | + } else if (System.getProperty("java.version").compareTo("1.8") >= 0) { |
| 572 | + context = SSLContext.getInstance("TLS"); |
573 | 573 | } else { |
574 | | - contextStr = "SSL"; |
| 574 | + context = SSLContext.getDefault(); |
575 | 575 | } |
576 | | - SSLContext context = SSLContext.getInstance(contextStr); |
577 | | - |
578 | | - context.init(null, trustAll, new java.security.SecureRandom()); |
579 | | - return new SplunkHttpsSocketFactory(context.getSocketFactory()); |
580 | | - } catch (Exception e) { |
581 | | - throw new RuntimeException("Error setting up SSL socket factory: " + e, e); |
582 | | - } |
583 | | - } |
584 | | - |
585 | | - private static final class SplunkHttpsSocketFactory extends SSLSocketFactory { |
586 | | - private final SSLSocketFactory delegate; |
587 | 576 |
|
588 | | - public static String[] PROTOCOLS = {"SSLv3"}; |
589 | | - public static String[] PROTOCOLS_TLS = {"TLSv1.3", "TLSv1.2", "TLSv1.1", "TLSv1"}; |
590 | | - |
591 | | - private SplunkHttpsSocketFactory(SSLSocketFactory delegate) { |
592 | | - this.delegate = delegate; |
593 | | - } |
594 | | - |
595 | | - private Socket configure(Socket socket) { |
596 | | - if (socket instanceof SSLSocket) { |
597 | | - if (sslSecurityProtocol != null) { |
598 | | - String[] protocols = {sslSecurityProtocol.toString()}; |
599 | | - ((SSLSocket) socket).setEnabledProtocols(protocols); |
600 | | - } else if (useTLS || System.getProperty("java.version").compareTo("1.8") >= 0) { |
601 | | - ((SSLSocket) socket).setEnabledProtocols(PROTOCOLS_TLS); |
602 | | - } else { |
603 | | - ((SSLSocket) socket).setEnabledProtocols(PROTOCOLS); |
604 | | - } |
| 577 | + if (validateCertificates) { |
| 578 | + context.init(null, null, null); |
| 579 | + // For now this check is set as null. |
| 580 | + // TODO: Implementation logic for validating client certificate. |
| 581 | + } else { |
| 582 | + TrustManager[] trustAll = new TrustManager[]{ |
| 583 | + new X509TrustManager() { |
| 584 | + public X509Certificate[] getAcceptedIssuers() { |
| 585 | + return null; |
| 586 | + } |
| 587 | + |
| 588 | + public void checkClientTrusted(X509Certificate[] certs, String authType) { |
| 589 | + } |
| 590 | + |
| 591 | + public void checkServerTrusted(X509Certificate[] certs, String authType) { |
| 592 | + } |
| 593 | + } |
| 594 | + }; |
| 595 | + context.init(null, trustAll, null); |
605 | 596 | } |
606 | | - return socket; |
607 | | - } |
608 | | - |
609 | | - @Override |
610 | | - public String[] getDefaultCipherSuites() { |
611 | | - return delegate.getDefaultCipherSuites(); |
612 | | - } |
613 | | - |
614 | | - @Override |
615 | | - public String[] getSupportedCipherSuites() { |
616 | | - return delegate.getSupportedCipherSuites(); |
617 | | - } |
618 | | - |
619 | | - @Override |
620 | | - public Socket createSocket(Socket socket, String s, int i, boolean b) throws IOException { |
621 | | - return configure(delegate.createSocket(socket, s, i, b)); |
622 | | - } |
623 | | - |
624 | | - @Override |
625 | | - public Socket createSocket() throws IOException { |
626 | | - return configure(delegate.createSocket()); |
627 | | - } |
628 | 597 |
|
629 | | - @Override |
630 | | - public Socket createSocket(String s, int i) throws IOException, UnknownHostException { |
631 | | - return configure(delegate.createSocket(s, i)); |
632 | | - } |
633 | | - |
634 | | - @Override |
635 | | - public Socket createSocket(String s, int i, InetAddress inetAddress, int i1) throws IOException, UnknownHostException { |
636 | | - return configure(delegate.createSocket(s, i, inetAddress, i1)); |
637 | | - } |
638 | | - |
639 | | - @Override |
640 | | - public Socket createSocket(InetAddress inetAddress, int i) throws IOException { |
641 | | - return configure(delegate.createSocket(inetAddress, i)); |
642 | | - } |
643 | | - |
644 | | - @Override |
645 | | - public Socket createSocket(InetAddress inetAddress, int i, InetAddress inetAddress1, int i1) throws IOException { |
646 | | - return configure(delegate.createSocket(inetAddress, i, inetAddress1, i1)); |
| 598 | + return context.getSocketFactory(); |
| 599 | + } catch (Exception e) { |
| 600 | + throw new RuntimeException("Error setting up SSL socket factory: " + e, e); |
647 | 601 | } |
648 | 602 | } |
649 | 603 |
|
|
0 commit comments