java - ignore expired ssl certificate

后端 未结 3 2161
自闭症患者
自闭症患者 2020-11-27 07:20
URL myUrl = new URL(\"https://www.....\");

SSL Certificate of website is expired. How to avoid it and make URL() work ?

相关标签:
3条回答
  • 2020-11-27 07:50

    You have to create a custom X509 validator that will ignore expired certificates. In fact, no check will be performed.

    Code taken from here: http://exampledepot.com/egs/javax.net.ssl/TrustAll.html

    // 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
    // It should work with expired certificate as well
    try {
        URL myUrl = new URL("https://www.....");
    } catch (MalformedURLException e) {
    }
    
    0 讨论(0)
  • 2020-11-27 08:00

    You should build a TrustManager that wraps the default trust manager, catches the CertificiateExpiredException and ignores it.

    Note: as detailed in this answer, whether or not this is secure is very much implementation dependent. In particular, it relies on the date validation being done last, after everything else has been checked properly.

    Something along these lines should work:

    TrustManagerFactory tmf = TrustManagerFactory.getInstance(
        TrustManagerFactory.getDefaultAlgorithm());
    // Initialise the TMF as you normally would, for example:
    tmf.init((KeyStore)null); 
    
    TrustManager[] trustManagers = tmf.getTrustManagers();
    final X509TrustManager origTrustmanager = (X509TrustManager)trustManagers[0];
    
    TrustManager[] wrappedTrustManagers = new TrustManager[]{
       new X509TrustManager() {
           public java.security.cert.X509Certificate[] getAcceptedIssuers() {
              return origTrustmanager.getAcceptedIssuers();
           }
    
           public void checkClientTrusted(X509Certificate[] certs, String authType) {
               origTrustmanager.checkClientTrusted(certs, authType);
           }
    
           public void checkServerTrusted(X509Certificate[] certs, String authType) {
               try {
                   origTrustmanager.checkServerTrusted(certs, authType);
               } catch (CertificateExpiredException e) {}
           }
       }
    };
    
    SSLContext sc = SSLContext.getInstance("TLS");
    sc.init(null, wrappedTrustManagers, null);
    HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());
    

    The trust managers throw CertificateExceptions (see subclasses for details) when something is wrong with a certificate. Be specific in what you want to catch/ignore. Everything you really want validated has to be checked before what you catch is potentially thrown, or you'll have to validate it manually too. Anything more relaxed than this (in particular, not doing anything and therefore not throwing any exception) will ignore the certificate verification and validation altogether, which is about the same as using anonymous cipher suites or ignoring authentication. This would defeat the security purpose of using SSL/TLS (as opposed to being only a bit more flexible on the expiry date).

    0 讨论(0)
  • 2020-11-27 08:11

    I wrote a custom TrustManager to solve this problem, you can see it at https://gist.github.com/divergentdave/9a68d820e3610513bd4fcdc4ae5f91a1. This TrustManager wraps the offending X509Certificate in another class to disable the expiration check while leaving all other validation in place. (i.e. matches the hostname, chains to a trusted CA, signature valid, etc.)

    0 讨论(0)
提交回复
热议问题