Skip to content

Commit 83c2b58

Browse files
committed
Add an option to disable SSL verification
1 parent f04fc03 commit 83c2b58

File tree

1 file changed

+34
-0
lines changed

1 file changed

+34
-0
lines changed

src/main/java/io/cryptolens/internal/HelperMethods.java

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,12 @@
77
import io.cryptolens.models.ErrorType;
88
import io.cryptolens.models.RequestModel;
99

10+
import javax.net.ssl.*;
1011
import java.lang.reflect.Field;
1112
import java.lang.reflect.Type;
13+
import java.security.SecureRandom;
14+
import java.security.cert.CertificateException;
15+
import java.security.cert.X509Certificate;
1216
import java.time.Instant;
1317
import java.time.LocalDateTime;
1418
import java.time.ZoneId;
@@ -17,6 +21,13 @@
1721

1822
public class HelperMethods {
1923

24+
/**
25+
* This field can be used to bypass SSL verification when calling app.cryptolens.io. Set this to 'false'
26+
* before calling any of the API methods. Once an API method is called, it will no longer be possible
27+
* to re-enable SSL verification by setting this variable to false.
28+
*/
29+
public static boolean SSLEnabled = true;
30+
2031
public static <T extends BasicResult> T SendRequestToWebAPI(String method, RequestModel model, Map<String,String> extraParams, Class<T> clazz) {
2132
return SendRequestToWebAPI(method, model, extraParams, clazz, null);
2233
}
@@ -55,6 +66,29 @@ public static <T extends BasicResult> T SendRequestToWebAPI(String method, Reque
5566
RequestHandler requestHandler = new HttpsURLConnectionRequestHandler();
5667

5768
try {
69+
if(!HelperMethods.SSLEnabled) {
70+
HttpsURLConnection.setDefaultHostnameVerifier(new HostnameVerifier() {
71+
public boolean verify(String hostname, SSLSession session) {
72+
return true;
73+
}
74+
});
75+
SSLContext context = SSLContext.getInstance("TLS");
76+
context.init(null, new X509TrustManager[]{new X509TrustManager() {
77+
public void checkClientTrusted(X509Certificate[] chain,
78+
String authType) throws CertificateException {
79+
}
80+
81+
public void checkServerTrusted(X509Certificate[] chain,
82+
String authType) throws CertificateException {
83+
}
84+
85+
public X509Certificate[] getAcceptedIssuers() {
86+
return new X509Certificate[0];
87+
}
88+
}}, new SecureRandom());
89+
HttpsURLConnection.setDefaultSSLSocketFactory(
90+
context.getSocketFactory());
91+
}
5892

5993
String response = requestHandler.makePostRequest(licenseServerUrl + "/api/" + method, params);
6094

0 commit comments

Comments
 (0)