India English
Kenya English
United Kingdom English
South Africa English
Nigeria English
United States English
United States Español
Indonesia English
Bangladesh English
Egypt العربية
Tanzania English
Ethiopia English
Uganda English
Congo - Kinshasa English
Ghana English
Côte d’Ivoire English
Zambia English
Cameroon English
Rwanda English
Germany Deutsch
France Français
Spain Català
Spain Español
Italy Italiano
Russia Русский
Japan English
Brazil Português
Brazil Português
Mexico Español
Philippines English
Pakistan English
Turkey Türkçe
Vietnam English
Thailand English
South Korea English
Australia English
China 中文
Somalia English
Canada English
Canada Français
Netherlands Nederlands

How to Disable javax.net.ssl.SSLHandshakeException (The Right Way and the Risky Way)

You’re hitting a javax.net.ssl.SSLHandshakeException.

It’s like your code walked up to a door with a bouncer (the server), tried to shake its hand (initiate an SSL handshake), and got denied entry.

This usually means something’s off with the SSL certificate: maybe it’s expired, maybe it’s not signed by a trusted authority, or maybe your system just doesn’t trust it.

Now, you could just try to disable the whole security check. It’s like telling the bouncer to take the night off. But let me tell you, that’s a terrible idea in 99% of cases. You’re leaving your system wide open to attacks. It’s like leaving your front door unlocked in a bad neighborhood.

So, before we even talk about disabling anything, let’s look at the right way to fix this.

The Right Way: Fix the Underlying Issue

1. Identify the Root Cause:

Check the Certificate: Is it expired? Is it self-signed? Is it issued by a trusted Certificate Authority (CA)?

Check Your Truststore: Does your Java environment trust the CA that issued the certificate? You might need to import the certificate into your truststore.

Check the Hostname: Does the hostname in the URL match the hostname in the certificate?

Check the Protocol/Cipher Suite: Are you using an outdated or unsupported protocol (like SSLv3) or cipher suite?

2. Solve the Problem:

Update the Certificate: If it’s expired, get a new one.

Import the Certificate: If it’s not trusted, import it into your truststore.

Correct the Hostname: Make sure the hostname matches.

Update Your Java Environment: If you’re using an old version of Java, update it to support newer protocols and cipher suites.

Tools to Help:

  • Keytool: For managing your truststore.
  • OpenSSL: For inspecting certificates.

Example:

Let’s say your certificate is self-signed. You’d use keytool to import it into your truststore:

Bash

keytool -import -alias mycert -keystore mytruststore.jks -file mycert.cer

The Risky Way: Disabling SSL Verification (Use with Extreme Caution)

I’m only showing you this because sometimes, in very specific situations (like testing in a controlled environment), you might need to temporarily disable SSL verification.

But I cannot stress enough how dangerous this is in a production environment.

1. Create a Custom Trust Manager:

You can create a custom X509TrustManager that blindly trusts all certificates. This is like having a bouncer who lets everyone in, no questions asked.

2. Initialize an SSL Context:

Create an SSLContext using your custom trust manager.

3. Use the SSL Context:

Configure your code to use this insecure SSL context.

Example:

Java

TrustManager[] trustAllCerts = new TrustManager[] {
  new X509TrustManager() {
    public java.security.cert.X509Certificate[] getAcceptedIssuers() {
      return null;
    }
    public void checkClientTrusted(
        java.security.cert.X509Certificate[] certs, String authType) {
    }
    public void checkServerTrusted(
        java.security.cert.X509Certificate[] certs, String authType) {
    }
  }
};

SSLContext sc = SSLContext.getInstance("SSL");
sc.init(null, trustAllCerts, new java.security.SecureRandom());
HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());

Warning: I can’t say this enough: do not use this in production! You’re opening yourself up to man-in-the-middle attacks and other security vulnerabilities.

The Bottom Line:

Disabling SSL verification is like playing with fire.

You might get away with it for a while, but eventually, you’re going to get burned.

Fix the underlying issue, and keep your system secure.

Read also:

error

Enjoy this blog? Please spread the word :)