37

我需要从Java网站上的SSL证书中提取到期日期,应该同时支持受信任和自签名证书,例如:1.trusted https://github.com 2.self-signed https://mms.nw .ru/

我已经将一些代码复制为:

import java.net.URL;
import java.security.SecureRandom;
import java.security.cert.Certificate;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;
import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.KeyManager;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLSession;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;

public class SSLTest {

    public static void main(String [] args) throws Exception {
        // configure the SSLContext with a TrustManager
        SSLContext ctx = SSLContext.getInstance("TLS");
        ctx.init(new KeyManager[0], new TrustManager[] {new DefaultTrustManager()}, new SecureRandom());
        SSLContext.setDefault(ctx);

        URL url = new URL("https://github.com");//https://mms.nw.ru
        HttpsURLConnection conn = (HttpsURLConnection) url.openConnection();
        conn.setHostnameVerifier(new HostnameVerifier() {
            @Override
            public boolean verify(String arg0, SSLSession arg1) {
                return true;
            }
        });
        System.out.println(conn.getResponseCode());
        Certificate[] certs = conn.getServerCertificates();
        for (Certificate cert :certs){
            System.out.println(cert.getType());
            System.out.println(cert);
        }

        conn.disconnect();
    }

    private static class DefaultTrustManager implements X509TrustManager {

        @Override
        public void checkClientTrusted(X509Certificate[] arg0, String arg1) throws CertificateException {}

        @Override
        public void checkServerTrusted(X509Certificate[] arg0, String arg1) throws CertificateException {}

        @Override
        public X509Certificate[] getAcceptedIssuers() {
            return null;
        }
    }
}

问题是:

  1. 如何从证书中解析到期日期,在我的代码中 toString() 确实输出了日期,但很难解析。

  2. 如何确定证书链,例如带有链 3 的 github 证书,我怎么知道从哪个证书获取到期日期?

4

3 回答 3

36

如何从证书中解析到期日期

将其转换为 anX509Certificate并调用getNotAfter().

如何确定证书链,例如带有链的github证书

你明白了。正如JavadocCertificate[]中所说,这就是数组。

我怎么知道从哪个证书获取到期日期?

阅读Javadoc。“对等方自己的证书首先是任何证书颁发机构”。

但是我不知道你为什么要这样做。Java 应该已经为您完成了这一切。

请丢弃不安全且不正确的 TrustManager 实现。处理自签名证书的正确方法是将它们导入客户端信任库。还请扔掉不安全的 HostnameVerifier,并使用默认的或安全的。如果您不希望它是安全的,为什么还要使用 HTTPS?

于 2012-10-19T03:45:58.977 回答
0

确保将 Begin/End 行附加到 PEM 格式,否则 java 无法理解它。

public class CertTest {

    public static final String cert = "-----BEGIN CERTIFICATE-----\n<CERT_DATA>\n-----END CERTIFICATE-----";

    public static void main(String[] args){
        try {
            X509Certificate myCert = (X509Certificate)CertificateFactory
                     .getInstance("X509")
                     .generateCertificate(
                                       // string encoded with default charset
                    new ByteArrayInputStream(cert.getBytes())
                     );
            System.out.println(myCert.getNotAfter());

        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }
}
于 2015-12-18T01:14:19.140 回答
0

以 Kotlin 的方式,它应该看起来像这样:

fun certInformation(aURL: String) {
    val destinationURL = URL(aURL)
    val conn = destinationURL.openConnection() as HttpsURLConnection
    conn.connect()
    val certs = conn.serverCertificates
    for (cert in certs) {
        println("Certificate is: $cert")
        if (cert is X509Certificate) {
            println(cert.issuerDN.name)
            println(cert.notAfter)
            println(cert.notBefore)
        }
    }
}
于 2018-11-11T04:52:39.063 回答