【问题标题】:Download SSL certificate from website and send it从网站下载 SSL 证书并发送
【发布时间】:2017-07-05 09:57:30
【问题描述】:

我们正在开发一个具有安全意识的应用程序,我们需要该应用程序向我们发送用于连接的证书。

是否可以下载证书并在 Android 上发送。我们需要这样做,以便我们可以检查证书是否已被泄露。或者最好从证书中检索详细信息,将其写入日志然后发送。我们有一个可以发送详细信息的 api

【问题讨论】:

    标签: android ssl certificate


    【解决方案1】:

    我看到你之前要求固定...

    您可以通过以下方式固定证书

    Certificate[] certs = conn.getServerCertificates();
    MessageDigest md = MessageDigest.getInstance("SHA-256");
    for (Certificate cert : certs) {
        X509Certificate x509Certificate = (X509Certificate) cert;
        byte[] key = x509Certificate.getPublicKey().getEncoded();
        md.update(key, 0, key.length);
        byte[] hashBytes = md.digest();
        StringBuilder hexHash = new StringBuilder();
        for (byte hashByte : hashBytes) {
            int k = 0xFF & hashByte;
            String tmp = (k < 16) ? "0" : "";
            tmp += Integer.toHexString(0xFF & hashByte);
            hexHash.append(tmp);
        }
        // You can even log cert.toString()
        Log.d(TAG, hexHash.toString()); get the hash from here
        // If you create `pins` as a Set with all the valid pins from above
        if (pins.contains(hexHash.toString())) {
            return true;
        }
    }
    

    您可以在连接conn 连接后运行它,然后如果引脚不匹配,您可以在发送数据之前中止。注意:上面的代码固定的是证书的公钥而不是证书。

    如果你只想要证书,你可以得到cert.toString()

    【讨论】:

    • 当我尝试运行 URL url = new URL(PATH); HttpsURLConnection connection = (HttpsURLConnection) url.openConnection();connection.connect(); 在我的 android 设备(mitmproxy 和 fiddler)上使用代理时,我得到一个 javax.net.ssl.SSLHandshakeException: java.security.cert.CertPathValidatorException: Trust anchor for certification path not found,但在不使用代理时却没有
    • 我猜您使用的是 M+ 设备,在这种情况下,您需要将您的代理 SSL 证书添加到网络安全配置中。
    • 我在 Android 6.0 和 7.0 上运行它。我不知道如何配置网络安全配置。它似乎适用于 android 6.0。
    猜你喜欢
    • 1970-01-01
    • 2015-11-15
    • 2020-08-06
    • 2018-04-10
    • 2011-02-24
    • 2013-07-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多