Skip to content

Commit ef99efe

Browse files
Merge pull request #175 from splunk/DVPL-9696
Enable certificate validation by default, allow all certificates for local environment.
2 parents c961d13 + 69d854c commit ef99efe

File tree

2 files changed

+50
-89
lines changed

2 files changed

+50
-89
lines changed

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

Lines changed: 43 additions & 89 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,16 @@
3737
public class HttpService {
3838
// For debugging purposes
3939
private static final boolean VERBOSE_REQUESTS = false;
40-
public static boolean useTLS=false;
4140
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+
4250
private static SSLSocketFactory sslSocketFactory = createSSLFactory();
4351
private static String HTTPS_SCHEME = "https";
4452
private static String HTTP_SCHEME = "http";
@@ -225,7 +233,7 @@ public static void setSslSecurityProtocol(SSLSecurityProtocol securityProtocol)
225233
// Only update the SSL_SOCKET_FACTORY if changing protocols
226234
if (sslSecurityProtocol != securityProtocol) {
227235
sslSecurityProtocol = securityProtocol;
228-
sslSocketFactory = new SplunkHttpsSocketFactory(createSSLFactory());
236+
sslSocketFactory = createSSLFactory();
229237
}
230238
}
231239

@@ -429,7 +437,6 @@ Socket open() throws IOException {
429437
public ResponseMessage send(String path, RequestMessage request) {
430438
// Construct a full URL to the resource
431439
URL url = getUrl(path);
432-
433440
// Create and initialize the connection object
434441
HttpURLConnection cn;
435442
try {
@@ -550,100 +557,47 @@ public static SSLSocketFactory getSSLSocketFactory() {
550557
return HttpService.sslSocketFactory;
551558
}
552559

560+
public static void setValidateCertificates(boolean validateCertificates) {
561+
HttpService.validateCertificates = validateCertificates;
562+
}
563+
553564
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+
567566
try {
568-
String contextStr = "";
567+
SSLContext context;
569568
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");
573573
} else {
574-
contextStr = "SSL";
574+
context = SSLContext.getDefault();
575575
}
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;
587576

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);
605596
}
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-
}
628597

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);
647601
}
648602
}
649603

splunk/src/test/java/com/splunk/SDKTestCase.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import org.junit.After;
2020
import org.junit.Assert;
2121
import org.junit.Before;
22+
import org.junit.BeforeClass;
2223

2324
import java.io.*;
2425
import java.net.InetSocketAddress;
@@ -90,6 +91,12 @@ public static Integer getJavaVersion() {
9091
return Integer.parseInt(version);
9192
}
9293

94+
@BeforeClass
95+
public static void preClassLoadActions() {
96+
// Bypass the certification validation here.
97+
HttpService.setValidateCertificates(false);
98+
}
99+
93100
@Before
94101
public void setUp() throws Exception {
95102
// If using Charles Proxy for debugging, uncomment these lines.

0 commit comments

Comments
 (0)