3636public class HttpService {
3737 // For debugging purposes
3838 private static final boolean VERBOSE_REQUESTS = false ;
39- public static boolean useTLS = false ;
4039 protected static SSLSecurityProtocol sslSecurityProtocol = null ;
4140
4241 /**
@@ -45,7 +44,7 @@ public class HttpService {
4544 * For PROD environment, TRUE is strongly recommended, whereas working in localhost OR development environment, FALSE is used.
4645 * Default Value: TRUE
4746 */
48- public static boolean validateCertificates = true ;
47+ protected static boolean validateCertificates = true ;
4948
5049 private static SSLSocketFactory sslSocketFactory = createSSLFactory ();
5150 private static String HTTPS_SCHEME = "https" ;
@@ -220,7 +219,7 @@ public static void setSslSecurityProtocol(SSLSecurityProtocol securityProtocol)
220219 // Only update the SSL_SOCKET_FACTORY if changing protocols
221220 if (sslSecurityProtocol != securityProtocol ) {
222221 sslSecurityProtocol = securityProtocol ;
223- sslSocketFactory = new SplunkHttpsSocketFactory ( createSSLFactory () );
222+ sslSocketFactory = createSSLFactory ();
224223 }
225224 }
226225
@@ -423,9 +422,7 @@ public ResponseMessage send(String path, RequestMessage request) {
423422 throw new RuntimeException (e .getMessage (), e );
424423 }
425424 if (cn instanceof HttpsURLConnection ) {
426- if (!validateCertificates ) {
427- ((HttpsURLConnection ) cn ).setSSLSocketFactory (sslSocketFactory );
428- }
425+ ((HttpsURLConnection ) cn ).setSSLSocketFactory (sslSocketFactory );
429426 ((HttpsURLConnection ) cn ).setHostnameVerifier (HOSTNAME_VERIFIER );
430427 }
431428 cn .setUseCaches (false );
@@ -537,102 +534,42 @@ public static void setValidateCertificates(boolean validateCertificates) {
537534 public static SSLSocketFactory createSSLFactory () {
538535
539536 try {
540- String contextStr = "" ;
537+ SSLContext context ;
541538 if (sslSecurityProtocol != null ) {
542- contextStr = sslSecurityProtocol .toString ().contains ("SSL" ) ? "SSL" : "TLS" ;
543- } else if (useTLS || System .getProperty ("java.version" ).compareTo ("1.8" ) >= 0 ) {
544- contextStr = "TLS" ;
539+ String contextStr = sslSecurityProtocol .toString ().contains ("SSL" ) ? "SSL" : "TLS" ;
540+ context = SSLContext .getInstance (contextStr );
541+ } else if (System .getProperty ("java.version" ).compareTo ("1.8" ) >= 0 ) {
542+ context = SSLContext .getInstance ("TLS" );
545543 } else {
546- contextStr = "SSL" ;
544+ context = SSLContext . getDefault () ;
547545 }
548- SSLContext context = SSLContext .getInstance (contextStr );
549546
550- TrustManager [] trustAll = new TrustManager []{
551- new X509TrustManager () {
552- public X509Certificate [] getAcceptedIssuers () {
553- return null ;
554- }
547+ if (validateCertificates ) {
548+ context .init (null , null , null );
549+ // For now this check is set as null.
550+ // TODO: Implementation logic for validating client certificate.
551+ } else {
552+ TrustManager [] trustAll = new TrustManager []{
553+ new X509TrustManager () {
554+ public X509Certificate [] getAcceptedIssuers () {
555+ return null ;
556+ }
555557
556- public void checkClientTrusted (X509Certificate [] certs , String authType ) {
557- }
558+ public void checkClientTrusted (X509Certificate [] certs , String authType ) {
559+ }
558560
559- public void checkServerTrusted (X509Certificate [] certs , String authType ) {
561+ public void checkServerTrusted (X509Certificate [] certs , String authType ) {
562+ }
560563 }
561- }
562- };
563- context .init (null , trustAll , new java .security .SecureRandom ());
564-
564+ };
565+ context .init (null , trustAll , null );
566+ }
565567
566- return new SplunkHttpsSocketFactory ( context .getSocketFactory () );
568+ return context .getSocketFactory ();
567569 } catch (Exception e ) {
568570 throw new RuntimeException ("Error setting up SSL socket factory: " + e , e );
569571 }
570572 }
571573
572- private static final class SplunkHttpsSocketFactory extends SSLSocketFactory {
573- private final SSLSocketFactory delegate ;
574-
575- public static String [] PROTOCOLS = {"SSLv3" };
576- public static String [] PROTOCOLS_TLS = {"TLSv1.3" , "TLSv1.2" , "TLSv1.1" , "TLSv1" };
577-
578- private SplunkHttpsSocketFactory (SSLSocketFactory delegate ) {
579- this .delegate = delegate ;
580- }
581-
582- private Socket configure (Socket socket ) {
583- if (socket instanceof SSLSocket ) {
584- if (sslSecurityProtocol != null ) {
585- String [] protocols = {sslSecurityProtocol .toString ()};
586- ((SSLSocket ) socket ).setEnabledProtocols (protocols );
587- } else if (useTLS || System .getProperty ("java.version" ).compareTo ("1.8" ) >= 0 ) {
588- ((SSLSocket ) socket ).setEnabledProtocols (PROTOCOLS_TLS );
589- } else {
590- ((SSLSocket ) socket ).setEnabledProtocols (PROTOCOLS );
591- }
592- }
593- return socket ;
594- }
595-
596- @ Override
597- public String [] getDefaultCipherSuites () {
598- return delegate .getDefaultCipherSuites ();
599- }
600-
601- @ Override
602- public String [] getSupportedCipherSuites () {
603- return delegate .getSupportedCipherSuites ();
604- }
605-
606- @ Override
607- public Socket createSocket (Socket socket , String s , int i , boolean b ) throws IOException {
608- return configure (delegate .createSocket (socket , s , i , b ));
609- }
610-
611- @ Override
612- public Socket createSocket () throws IOException {
613- return configure (delegate .createSocket ());
614- }
615-
616- @ Override
617- public Socket createSocket (String s , int i ) throws IOException , UnknownHostException {
618- return configure (delegate .createSocket (s , i ));
619- }
620-
621- @ Override
622- public Socket createSocket (String s , int i , InetAddress inetAddress , int i1 ) throws IOException , UnknownHostException {
623- return configure (delegate .createSocket (s , i , inetAddress , i1 ));
624- }
625-
626- @ Override
627- public Socket createSocket (InetAddress inetAddress , int i ) throws IOException {
628- return configure (delegate .createSocket (inetAddress , i ));
629- }
630-
631- @ Override
632- public Socket createSocket (InetAddress inetAddress , int i , InetAddress inetAddress1 , int i1 ) throws IOException {
633- return configure (delegate .createSocket (inetAddress , i , inetAddress1 , i1 ));
634- }
635- }
636-
637574}
638575
0 commit comments