Android中的OCSP证书装订

时间:2016-03-21 12:46:56

标签: android ocsp

过去几天,我一直在试图在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();
            }

1 个答案:

答案 0 :(得分:0)

在https://

的情况下,不要使用OkHttp尝试在构建 HttpURLConnection HttpsURLConnection 中使用Android