Skip to content

Commit 0f8727e

Browse files
authored
Merge pull request #158 from splunk/DVPL-9265
HttpService Updated for TLS and hostname implementation
2 parents f4c99f8 + a91d966 commit 0f8727e

File tree

1 file changed

+30
-21
lines changed

1 file changed

+30
-21
lines changed

splunk/src/main/java/com/splunk/HttpService.java

Lines changed: 30 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -36,14 +36,21 @@
3636
public class HttpService {
3737
// For debugging purposes
3838
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;
4041
private static SSLSocketFactory sslSocketFactory = createSSLFactory();
4142
private static String HTTPS_SCHEME = "https";
4243
private static String HTTP_SCHEME = "http";
44+
private static String HOSTNAME = "localhost";
4345

4446
private static final HostnameVerifier HOSTNAME_VERIFIER = new HostnameVerifier() {
4547
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+
}
4754
}
4855
};
4956

@@ -204,7 +211,7 @@ public static void setSslSecurityProtocol(SSLSecurityProtocol securityProtocol)
204211
// Only update the SSL_SOCKET_FACTORY if changing protocols
205212
if (sslSecurityProtocol != securityProtocol) {
206213
sslSecurityProtocol = securityProtocol;
207-
sslSocketFactory = new SplunkHttpsSocketFactory(createSSLFactory(), securityProtocol);
214+
sslSocketFactory = new SplunkHttpsSocketFactory(createSSLFactory());
208215
}
209216
}
210217

@@ -528,41 +535,43 @@ public void checkServerTrusted(X509Certificate[] certs, String authType) {
528535
}
529536
};
530537
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";
540545
}
546+
SSLContext context = SSLContext.getInstance(contextStr);
541547

542548
context.init(null, trustAll, new java.security.SecureRandom());
543-
return new SplunkHttpsSocketFactory(context.getSocketFactory(), HttpService.sslSecurityProtocol);
549+
return new SplunkHttpsSocketFactory(context.getSocketFactory());
544550
} catch (Exception e) {
545551
throw new RuntimeException("Error setting up SSL socket factory: " + e, e);
546552
}
547553
}
548554

549555
private static final class SplunkHttpsSocketFactory extends SSLSocketFactory {
550556
private final SSLSocketFactory delegate;
551-
private SSLSecurityProtocol sslSecurityProtocol;
552557

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"};
557560

558-
private SplunkHttpsSocketFactory(SSLSocketFactory delegate, SSLSecurityProtocol securityProtocol) {
561+
private SplunkHttpsSocketFactory(SSLSocketFactory delegate) {
559562
this.delegate = delegate;
560-
this.sslSecurityProtocol = securityProtocol;
561563
}
562564

563565
private Socket configure(Socket socket) {
564566
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+
}
566575
}
567576
return socket;
568577
}

0 commit comments

Comments
 (0)