Java SSL/TLS ignore expired cert? (java.security.cert.CertPathValidatorException: timestamp check failed)

后端 未结 3 504
礼貌的吻别
礼貌的吻别 2020-12-28 17:59

I am having an issue with a api that I communicate to via SSL. I am thinking the exception is coming due to the fact that the SSL cert has expired. The problem is that I do

3条回答
  •  灰色年华
    2020-12-28 18:43

    It is not safe to alter the default SSLContext since it affects the entire process. This lowers the security setting of every connection indiscriminately. It may also not be thread-safe although I am not sure.

    I recommend delegating such operations to a separate process per-request.

    String content = new HttpsNoVerify.fetch(URL.create(myURL));
    

    Listing of com/example/HttpsNoVerify.java:

    package com.example;
    
    import org.apache.commons.io.IOUtils;
    
    import javax.net.ssl.HttpsURLConnection;
    import javax.net.ssl.SSLContext;
    import javax.net.ssl.TrustManager;
    import javax.net.ssl.X509TrustManager;
    import java.net.URL;
    
    public class HttpsNoVerify {
        public static void main(String... args) throws Exception {
            URL url = new URL(args[0]);
    
            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());
    
            IOUtils.copy(url.openStream(), System.out);
        }
    
        public String fetch(URL url) throws Exception {
            return new SubProcess(HttpsNoVerify.class).run(url.toString());
        }
    }
    

    Listing of com/example/SubProcess.java:

    package com.example;
    
    import org.apache.commons.io.IOUtils;
    
    import java.util.Arrays;
    
    public class SubProcess {
        private final Class classToRun;
    
        public SubProcess(Class classToRun) {
            this.classToRun = classToRun;
        }
    
        public String run(String... args) throws Exception {
            ProcessBuilder processBuilder = new ProcessBuilder("java",
                    "-Djava.library.path=" + System.getProperty("java.library.path"),
                    "-classpath", System.getProperty("java.class.path"),
                    classToRun.getCanonicalName());
    
            for (String arg : args) processBuilder.command().add(arg);
    
            processBuilder.redirectErrorStream();
    
            Process process = processBuilder.start();
    
            String output = IOUtils.toString(process.getInputStream());
    
            process.waitFor();
    
            if (process.exitValue() != 0)
                throw new IllegalStateException(
                        String.format("Running %s with %s failed", classToRun, Arrays.toString(args)));
    
            return output;
        }
    }
    

提交回复
热议问题