19

在过去的几天里,我一直在努力尝试在 Android 中实现 OCSP 验证。

到目前为止,iOS 中的实现很容易,但对于 Android,我遇到的每一条信息都不起作用。我一直在使用我客户的 API 端点和 这个网站来运行证书吊销测试,到目前为止,我还没有幸运地在我的 Android 应用程序中检测到吊销的证书。我正在使用 OKHTTPClient。这是我验证证书吊销的方法

public void checkServerTrusted(X509Certificate[] chain, String authType)
            throws CertificateException {

        assert (chain != null);
        if (chain == null) {
            throw new IllegalArgumentException(
                    "checkServerTrusted: X509Certificate array is null");
        }

        assert (chain.length > 0);
        if (!(chain.length > 0)) {
            throw new IllegalArgumentException(
                    "checkServerTrusted: X509Certificate is empty");
        }

        if (VERIFY_AUTHTYPE) {
            assert (null != authType && authType.equalsIgnoreCase(AUTH_TYPE));
            if (!(null != authType && authType.equalsIgnoreCase(AUTH_TYPE))) {
                throw new CertificateException(
                        "checkServerTrusted: AuthType is not " + AUTH_TYPE);
            }
        }

        if(chain[0]!=null){
            try {
                X509Certificate issuerCert = chain[1];
                X509Certificate c1 = chain[0];
                TrustAnchor anchor = new TrustAnchor(issuerCert, null);
                Set anchors = Collections.singleton(anchor);
                CertificateFactory cf = CertificateFactory.getInstance("X.509");
                List list = Arrays.asList(new Certificate[]{c1});
                CertPath path = cf.generateCertPath(list);
                PKIXParameters params = new PKIXParameters(anchors);
                // Activate certificate revocation checking
                params.setRevocationEnabled(false);
                // Activate OCSP
                Security.setProperty("ocsp.enable", "true");

                // Ensure that the ocsp.responderURL property is not set.
                if (Security.getProperty("ocsp.responderURL") != null) {
                    throw new
                            Exception("The ocsp.responderURL property must not be set");
                }
                CertPathValidator validator = CertPathValidator.getInstance("PKIX");
                PKIXCertPathValidatorResult result = (PKIXCertPathValidatorResult) validator
                        .validate(path, params);

                System.out.println("VALID");
            } catch (Exception e) {
                System.out.println("EXCEPTION " + e.getMessage());
                e.printStackTrace();
            }
4

2 回答 2

0

我有一段与您类似的代码,只是略有不同,目前正确阻止了与已撤销证书的连接,请参见此处:https ://gist.github.com/HylkeB/f9f3c26398c49985e54545df1b76b062

它不是装订的,我无法让它工作(我还没有找到如何将status_request扩展添加到 ClientHello TLS 握手)。现在这已经足够好了。

于 2022-01-06T09:21:24.930 回答
-1

如果是 https://,请尝试在构建HttpURLConnectionHttpsURLConnection中使用 Android,而不是使用 OkHttp

于 2016-10-12T05:51:32.833 回答