Allow Java to use untrusted certificates for SSL/HTTPS connections


Midnight Blue:

I've been working on a program to extract information from a dynamic web application, and it worked fine until I set up my tomcat server to use SSL with a self-signed (hence, untrusted) certificate. The stack trace of the error is:

javax.net.ssl.SSLHandshakeException: sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target
Error: sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target
        at com.sun.net.ssl.internal.ssl.Alerts.getSSLException(Alerts.java:150)
        at com.sun.net.ssl.internal.ssl.SSLSocketImpl.fatal(SSLSocketImpl.java:1584)
        at com.sun.net.ssl.internal.ssl.Handshaker.fatalSE(Handshaker.java:174)
        at com.sun.net.ssl.internal.ssl.Handshaker.fatalSE(Handshaker.java:168)
        at com.sun.net.ssl.internal.ssl.ClientHandshaker.serverCertificate(ClientHandshaker.java:848)
        at com.sun.net.ssl.internal.ssl.ClientHandshaker.processMessage(ClientHandshaker.java:106)
        at com.sun.net.ssl.internal.ssl.Handshaker.processLoop(Handshaker.java:495)
        at com.sun.net.ssl.internal.ssl.Handshaker.process_record(Handshaker.java:433)
        at com.sun.net.ssl.internal.ssl.SSLSocketImpl.readRecord(SSLSocketImpl.java:877)
        at com.sun.net.ssl.internal.ssl.SSLSocketImpl.performInitialHandshake(SSLSocketImpl.java:1089)
        at com.sun.net.ssl.internal.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.java:1116)
        at com.sun.net.ssl.internal.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.java:1100)
        at sun.net.www.protocol.https.HttpsClient.afterConnect(HttpsClient.java:402)
        at sun.net.www.protocol.https.AbstractDelegateHttpsURLConnection.connect(AbstractDelegateHttpsURLConnection.java:170)
        at sun.net.www.protocol.http.HttpURLConnection.getOutputStream(HttpURLConnection.java:857)
        at sun.net.www.protocol.https.HttpsURLConnectionImpl.getOutputStream(HttpsURLConnectionImpl.java:230)
        at com.certicom.gls.glscs.nongui.URLReader$PostURL.setupURL(URLReader.java:34)
        at com.certicom.gls.glscs.nongui.URLReader.getLogin(URLReader.java:227)
        at com.certicom.gls.glscs.nongui.URLReader.testLogin(URLReader.java:436)
        at com.certicom.gls.glscs.nongui.Controller.loginMenu(Controller.java:384)
        at com.certicom.gls.glscs.nongui.Controller.menu(Controller.java:324)
        at com.certicom.gls.glscs.nongui.Controller.<init>(Controller.java:49)
        at com.certicom.gls.glscs.nongui.Controller.main(Controller.java:61)

In a web browser, when the user accesses an HTTPS site with an untrusted certificate, the user is prompted with a warning and asked to set whether he is willing to proceed; I would like to implement something similar for my command line application.. .I admit I'm new to socket programming and networking. Any suggestions to fix this would be great!

Yizhai

Here is some relevant code:

// Create a trust manager that does not validate certificate chains
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) {
        }
    }
};

// Install the all-trusting trust manager
try {
    SSLContext sc = SSLContext.getInstance("SSL");
    sc.init(null, trustAllCerts, new java.security.SecureRandom());
    HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());
} catch (Exception e) {
}

// Now you can access an https URL without having the certificate in the truststore
try {
    URL url = new URL("https://hostname/index.html");
} catch (MalformedURLException e) {
}

This will disable SSL checking completely - just don't learn exception handling from code like this!

To do what you want, you will have to implement a check in TrustManager that prompts the user.

Related


Allow HttpClient to use untrusted SSL certificates

Jamie I'm struggling to get a Windows 8 application to communicate with my test Web API over SSL. It seems that HttpClient/HttpClientHandler don't, and optionally untrusted certificates (like WebRequest) enable you to do so (albeit in a hacky way ServerCertifi

Allow HttpClient to use untrusted SSL certificates

Jamie I'm struggling to get a Windows 8 application to communicate with my test Web API over SSL. It seems that HttpClient/HttpClientHandler don't, and optionally untrusted certificates (like WebRequest) enable you to do so (albeit in a hacky way ServerCertifi

Allow HttpClient to use untrusted SSL certificates

Jamie I'm struggling to get a Windows 8 application to communicate with my test Web API over SSL. It seems that HttpClient/HttpClientHandler don't, and optionally untrusted certificates (like WebRequest) enable you to do so (albeit in a hacky way ServerCertifi

Publishing from Visual Studio 2015 - Allow Untrusted Certificates

Koryagin I am publishing my ASP.NET 5 MVC6 project from Visual Studio 2015. I have imported the publish profile from the server. The connection is successfully verified, but when publishing the project I get the following error: ERROR_CERTIFICATE_VALIDATION_FA

Publishing from Visual Studio 2015 - Allow Untrusted Certificates

koryakinp: I am publishing my ASP.NET 5 MVC6 project from Visual Studio 2015. I have imported the publish profile from the server. The connection is successfully verified, but when publishing the project I get the following error: ERROR_CERTIFICATE_VALIDATION_

Publishing from Visual Studio 2015 - Allow Untrusted Certificates

koryakinp: I am publishing my ASP.NET 5 MVC6 project from Visual Studio 2015. I have imported the publish profile from the server. The connection is successfully verified, but when publishing the project I get the following error: ERROR_CERTIFICATE_VALIDATION_

Flurl and untrusted certificates

digitag Currently I'm working on Flurl and trying to contact the API via https (I'm in the lab). So the certificate is invalid and Flurl can't continue to work :/ Here is my error message: Unhandled Exception: System.AggregateException: One or more errors occu

Handling untrusted certificates

cheese We have web services that can use certificates. When we open this link in a web browser, we see the following image: There is a problem with the website security certificate. And only when we click continue, it will display the information (response) we

Flurl and untrusted certificates

digitag Currently I'm working on Flurl and trying to contact the API via https (I'm in the lab). So the certificate is invalid and Flurl can't continue to work :/ Here is my error message: Unhandled Exception: System.AggregateException: One or more errors occu

Handling untrusted certificates

cheese We have web services that can use certificates. When we open this link in a web browser, we see the following image: There is a problem with the website security certificate. And only when we click continue, it will display the information (response) we

Allow use of unverified SSL certificates in UIWebview

Christine Schlenske I embed a website in a UIWebView. During development I point it to localhost. The problem is that whenever it hits the "https://" URL, it doesn't load. When I load the url into the mobile browser, I get the following popup: Is there a way t

Allow use of unverified SSL certificates in UIWebview

Christine Schlenske I embed a website in a UIWebView. During development I point it to localhost. The problem is that whenever it hits the "https://" URL, it doesn't load. When I load the url into the mobile browser, I get the following popup: Is there a way t

Allow use of unverified SSL certificates in UIWebview

Christine Schlenske I embed a website in a UIWebView. During development I point it to localhost. The problem is that whenever it hits the "https://" URL, it doesn't load. When I load the url into the mobile browser, I get the following popup: Is there a way t

Why RDS doesn't use Amazon root certificates for SSL connections

pony According to RDS' documentation, a specific public certificate ("rds-ca-2019-root.pem") needs to be installed in order to use RDS with SSL. However, Amazon has a public certificate "Amazon Root CA" which is installed on most operating systems. Is there an

Let's Encrypt Untrusted Certificates on Firefox

Andre I just added the certificate in IIS 8 (Windows Server 2012) using letsencrypt-win-simple.V1.9.1. There is no problem in Google Chrome, but the connection in Firefox is not trusted. I followed this tutorial: https://weblog.west-wind.com/posts/2016/feb/22/

IE9: Permanently accept untrusted certificates

bob Internet Explorer 9 always shows me the following error message when accessing a website via HTTPS with an untrusted certificate: Is there a way to import the certificate permanently so I don't have to click on this message every time? I don't have access

Let's Encrypt Untrusted Certificates on Firefox

Andre I just added the certificate in IIS 8 (Windows Server 2012) using letsencrypt-win-simple.V1.9.1. There is no problem in Google Chrome, but the connection in Firefox is not trusted. I followed this tutorial: https://weblog.west-wind.com/posts/2016/feb/22/

Let's Encrypt Untrusted Certificates on Firefox

Andre I just added the certificate in IIS 8 (Windows Server 2012) using letsencrypt-win-simple.V1.9.1. There is no problem in Google Chrome, but the connection in Firefox is not trusted. I followed this tutorial: https://weblog.west-wind.com/posts/2016/feb/22/

How to view and clear untrusted certificates in browser?

username I am using Firefox. For untrusted certificates, you can choose to accept. I would like to know where to look for these certificates and then how to clear them. I tried clearing the cache, but when I reload the same page afterwards (over HTTPS), it doe

Can I make davfs ignore untrusted certificates?

the best My server only serves WebDAV over HTTPS so that other computers can access the DAV. Assuming a certificate has been issued www.myserver.com, WebDAV is located https://www.myserver.com/webdav. For various reasons, I also want the directory to be mounte

Troubleshoot untrusted self-signed certificates

CJ Dennis I've created a self-signed certificate for use on my Windows 10 development machine, and I've added it to the Trusted Root Certification Authorities/Certificates suggested by " How to Install an Apache Self-Signed Certificate on YouTube's Windows 10

Let's Encrypt Untrusted Certificates on Firefox

Andre I just added the certificate in IIS 8 (Windows Server 2012) using letsencrypt-win-simple.V1.9.1. There is no problem in Google Chrome, but the connection in Firefox is not trusted. I followed this tutorial: https://weblog.west-wind.com/posts/2016/feb/22/

Can I make davfs ignore untrusted certificates?

the best My server only serves WebDAV over HTTPS so that other computers can access the DAV. Assuming a certificate has been issued www.myserver.com, WebDAV is located https://www.myserver.com/webdav. For various reasons, I also want the directory to be mounte