Skip to content

Commit

Permalink
Add an option to disable SSL verification
Browse files Browse the repository at this point in the history
  • Loading branch information
artemlos committed Nov 8, 2021
1 parent f04fc03 commit 83c2b58
Showing 1 changed file with 34 additions and 0 deletions.
34 changes: 34 additions & 0 deletions src/main/java/io/cryptolens/internal/HelperMethods.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,12 @@
import io.cryptolens.models.ErrorType;
import io.cryptolens.models.RequestModel;

import javax.net.ssl.*;
import java.lang.reflect.Field;
import java.lang.reflect.Type;
import java.security.SecureRandom;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
Expand All @@ -17,6 +21,13 @@

public class HelperMethods {

/**
* This field can be used to bypass SSL verification when calling app.cryptolens.io. Set this to 'false'
* before calling any of the API methods. Once an API method is called, it will no longer be possible
* to re-enable SSL verification by setting this variable to false.
*/
public static boolean SSLEnabled = true;

public static <T extends BasicResult> T SendRequestToWebAPI(String method, RequestModel model, Map<String,String> extraParams, Class<T> clazz) {
return SendRequestToWebAPI(method, model, extraParams, clazz, null);
}
Expand Down Expand Up @@ -55,6 +66,29 @@ public static <T extends BasicResult> T SendRequestToWebAPI(String method, Reque
RequestHandler requestHandler = new HttpsURLConnectionRequestHandler();

try {
if(!HelperMethods.SSLEnabled) {
HttpsURLConnection.setDefaultHostnameVerifier(new HostnameVerifier() {
public boolean verify(String hostname, SSLSession session) {
return true;
}
});
SSLContext context = SSLContext.getInstance("TLS");
context.init(null, new X509TrustManager[]{new X509TrustManager() {
public void checkClientTrusted(X509Certificate[] chain,
String authType) throws CertificateException {
}

public void checkServerTrusted(X509Certificate[] chain,
String authType) throws CertificateException {
}

public X509Certificate[] getAcceptedIssuers() {
return new X509Certificate[0];
}
}}, new SecureRandom());
HttpsURLConnection.setDefaultSSLSocketFactory(
context.getSocketFactory());
}

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

Expand Down

0 comments on commit 83c2b58

Please sign in to comment.