【问题标题】:OCSP certificate stapling in AndroidAndroid中的OCSP证书装订
【发布时间】:2016-07-07 23:39:22
【问题描述】:

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

到目前为止,在 iOS 中实现起来很容易,但对于 Android,我遇到的每一条信息都行不通。我一直在使用客户的 API 端点和 this website 来运行证书撤销测试,到目前为止,我还没有幸运地在我的 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();
            }

【问题讨论】:

    标签: android ocsp


    【解决方案1】:

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

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

    【讨论】:

      【解决方案2】:

      如果是 https://

      ,请尝试在构建中使用 Android 的 HttpURLConnectionHttpsURLConnection 而不是使用 OkHttp

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2012-03-25
        • 1970-01-01
        • 1970-01-01
        • 2023-02-04
        • 1970-01-01
        • 2019-02-12
        • 2014-06-09
        相关资源
        最近更新 更多