Skip to content

Commit f2973ae

Browse files
committed
adding code for disable ssl verification
1 parent 4c4783f commit f2973ae

File tree

1 file changed

+54
-18
lines changed

1 file changed

+54
-18
lines changed

src/main/java/utilities/pgpBatchUpload/MutualAuthUploadUtility.java

Lines changed: 54 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,12 @@
4242
* </p>
4343
*/
4444
public class MutualAuthUploadUtility {
45-
45+
4646
private static final Logger logger = LogManager.getLogger(MutualAuthUploadUtility.class);
47-
47+
48+
// Global variable to control SSL verification
49+
private static boolean disableSslVerification = false;
50+
4851
/**
4952
* Handles file upload operation using JKS keystore and truststore for mutual authentication.
5053
*
@@ -193,6 +196,19 @@ public static ApiResponse<String> handleUploadOperationUsingPrivateKeyAndCerts(b
193196
throw new IOException("Failed to perform upload operation with private/cert keys ", e);
194197
}
195198
}
199+
200+
/**
201+
* Sets whether SSL verification should be disabled.
202+
* @param disable true to disable SSL verification, false to enable
203+
* By default, SSL verification is enabled.
204+
*/
205+
public static void setDisableSslVerification(boolean disable) {
206+
logger.warn("Setting disableSslVerification to: " + disable);
207+
if (disable) {
208+
logger.warn("SSL verification is DISABLED. This setting is NOT SAFE for production and should NOT be used in production environments!");
209+
}
210+
disableSslVerification = disable;
211+
}
196212

197213
/**
198214
* Creates an OkHttpClient configured for mutual TLS authentication.
@@ -209,22 +225,42 @@ public static ApiResponse<String> handleUploadOperationUsingPrivateKeyAndCerts(b
209225
private static OkHttpClient getOkHttpClientForMutualAuth(KeyStore clientKeyStore, KeyStore serverTrustStore,
210226
char[] keyPassword) throws NoSuchAlgorithmException, UnrecoverableKeyException, KeyStoreException,
211227
KeyManagementException
212-
{
213-
// Set up KeyManager and TrustManager
214-
KeyManagerFactory kmf = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm());
215-
kmf.init(clientKeyStore, keyPassword); // Use the provided password
216-
TrustManagerFactory tmf = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());
217-
tmf.init(serverTrustStore);
218-
219-
// Create SSL context
220-
SSLContext sslContext = SSLContext.getInstance("TLS");
221-
sslContext.init(kmf.getKeyManagers(), tmf.getTrustManagers(), new SecureRandom());
222-
223-
// Build OkHttpClient with mutual TLS
224-
return new OkHttpClient.Builder()
225-
.sslSocketFactory(sslContext.getSocketFactory(), (X509TrustManager) tmf.getTrustManagers()[0])
226-
.build();
227-
}
228+
{
229+
if (disableSslVerification) {
230+
logger.warn("SSL verification is DISABLED. This setting is NOT SAFE for production and should NOT be used in production environments!");
231+
// Trust all certificates
232+
X509TrustManager trustAllManager = new X509TrustManager() {
233+
public void checkClientTrusted(X509Certificate[] chain, String authType) {}
234+
public void checkServerTrusted(X509Certificate[] chain, String authType) {}
235+
public X509Certificate[] getAcceptedIssuers() { return new X509Certificate[0]; }
236+
};
237+
KeyManagerFactory kmf = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm());
238+
kmf.init(clientKeyStore, keyPassword);
239+
240+
SSLContext sslContext = SSLContext.getInstance("TLS");
241+
sslContext.init(kmf.getKeyManagers(), new javax.net.ssl.TrustManager[]{trustAllManager}, new SecureRandom());
242+
243+
return new OkHttpClient.Builder()
244+
.sslSocketFactory(sslContext.getSocketFactory(), trustAllManager)
245+
.hostnameVerifier((hostname, session) -> true)
246+
.build();
247+
} else {
248+
// Set up KeyManager and TrustManager
249+
KeyManagerFactory kmf = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm());
250+
kmf.init(clientKeyStore, keyPassword); // Use the provided password
251+
TrustManagerFactory tmf = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());
252+
tmf.init(serverTrustStore);
253+
254+
// Create SSL context
255+
SSLContext sslContext = SSLContext.getInstance("TLS");
256+
sslContext.init(kmf.getKeyManagers(), tmf.getTrustManagers(), new SecureRandom());
257+
258+
// Build OkHttpClient with mutual TLS
259+
return new OkHttpClient.Builder()
260+
.sslSocketFactory(sslContext.getSocketFactory(), (X509TrustManager) tmf.getTrustManagers()[0])
261+
.build();
262+
}
263+
}
228264

229265
/**
230266
* Uploads a file to the specified endpoint using the provided HTTP client.

0 commit comments

Comments
 (0)