【问题标题】:Android validate TSL server certificateAndroid 验证 TLS 服务器证书
【发布时间】:2021-11-11 11:27:07
【问题描述】:

我有以下任务何时完成,但网络上似乎没有一个很好的方法来展示如何优雅地完成它。 (解决方案使用旧的 HTTPClient 或解决方案根本不是很相关)有人可以提供任何建议或解决方案吗?

给定一个 url,我的应用需要获取关联的 TLS 服务器证书并执行以下任务。

  1. 检索证书中的主题字段
  2. 检查 TLS 服务器证书是否过期
  3. 通过查询Issuing检查TLS服务器证书是否有效 CA 的 OCSP 服务。

HttpURLConnection 和 URLConnection 似乎与我要完成的任务有关,但不确定如何检索服务器证书。

我知道以下有关 Android 中的服务器证书的网站

http://www.normalesup.org/~george/articles/manual_https_cert_check_on_android.html

Receive & Validate certificate from server HTTPS - android

【问题讨论】:

  • 除非你在做一些非常不寻常的事情,否则你应该不需要任何这些,HTTPSUrlConnection 的代码已经完成了所有这些。

标签: android android-studio ssl tls1.2


【解决方案1】:

事实证明,您可以使用 getServerCertificates() 来检索在您启动 HttpsURLConnection 后获取的证书链。 TLS 是由 HttpsURLConnection 实现的,因此您可以确认证书是真实的,并且您与服务器的通信是保密的,数据完整性得以保留。

public void verifyCertificate(View view) {
        new Thread(new Runnable() {
            @Override
            public void run() {
                Log.d("DEBUG", "Hello there");
                try {
                    aFunctionWithCoolName("https://urlOfTheSiteYouWannaCheck.com");
                    Log.d("DEBUG", "Executed aFancyFunctionWithCoolName without any exceptions");
                } catch (IOException e) {
                    e.printStackTrace();
                    Log.d("DEBUG", "IOException");
                } catch (NoSuchAlgorithmException e) {
                    e.printStackTrace();
                    Log.d("DEBUG", "NoSuchAlgorithmException");
                } catch (CertificateEncodingException e) {
                    e.printStackTrace();
                    Log.d("DEBUG", "CertificateEncodingException");
                } catch (CertificateParsingException e) {
                    e.printStackTrace();
                    Log.d("DEBUG", "CertificateParsingException");
                } catch (Exception e) {
                    Log.wtf("DEBUG", "Too sad, I don't know what is happening :(");
                }
            }
        }).start();
    }

    private static void aFunctionWithCoolName(String httpsURL) throws IOException, NoSuchAlgorithmException, CertificateEncodingException, CertificateParsingException {

        final HttpsURLConnection con = (HttpsURLConnection) (new URL(httpsURL)).openConnection();

        con.setRequestMethod("GET");
        con.setConnectTimeout(5000);

        con.connect();

        // https://developer.android.com/reference/java/security/cert/X509Certificate
        // https://developer.android.com/reference/java/security/cert/Certificate
        // https://developer.android.com/reference/javax/net/ssl/HttpsURLConnection#getServerCertificates()

        final Certificate[] certs = con.getServerCertificates();
        final Certificate subjectCert = certs[0];
        final Certificate rootCert = certs[certs.length-1];
        if (subjectCert instanceof X509Certificate && rootCert instanceof X509Certificate) {
            X509Certificate sc = (X509Certificate) subjectCert;
            X509Certificate rc = (X509Certificate) rootCert;
            printX509CertificateDetail(sc);

        }
    }

    public static void printX509CertificateDetail(X509Certificate cert) {
        Log.d("DEBUG", "===========================================");
        Log.d("DEBUG - Subject DN", cert.getSubjectX500Principal().toString());
        Log.d("DEBUG - Subject CN", getSubjectCommonName(cert));
        Log.d("DEBUG - URL DN", url.getHost());
        Log.d("DEBUG - Issuer DN", cert.getIssuerDN().toString());
        Log.d("DEBUG - Not After", cert.getNotAfter().toString());
        Log.d("DEBUG - Not Before", cert.getNotBefore().toString());
    }

【讨论】:

    猜你喜欢
    • 2013-05-05
    • 1970-01-01
    • 2016-10-20
    • 2015-05-13
    • 2011-07-07
    • 1970-01-01
    • 1970-01-01
    • 2012-11-14
    • 2018-10-20
    相关资源
    最近更新 更多