android - 如何在 Android 中实现叶/中间证书固定?

标签 android ssl-certificate x509certificate

我已经在我的项目中实现了叶证书,它运行良好。请检查下面的代码,现在的问题是叶证书将在我的服务器中一年后过期,所以我想验证叶证书以便在它过期/无效时我可以使用中间证书?

有实现中间证书的例子吗?

请帮帮我!

代码:-

SSLContext sslContext = null;
        try {
            CertificateFactory cf = CertificateFactory.getInstance("X.509");
            InputStream caInput = context.getResources().openRawResource(certRawRef);
            Certificate ca;
            try {
                ca = cf.generateCertificate(caInput);
            } finally {
                caInput.close();
            }
            // Create a KeyStore containing our trusted CAs
            String keyStoreType = KeyStore.getDefaultType();
            KeyStore keyStore = KeyStore.getInstance(keyStoreType);
            keyStore.load(null, null);
            keyStore.setCertificateEntry("ca", ca);
            // Create a TrustManager that trusts the CAs in our KeyStore
            String tmfAlgorithm = TrustManagerFactory.getDefaultAlgorithm();
            TrustManagerFactory tmf = TrustManagerFactory.getInstance(tmfAlgorithm);
            tmf.init(keyStore);
            // Create an SSLContext that uses our TrustManager

            sslContext = SSLContext.getInstance("TLSv1.2");
            sslContext.init(null, tmf.getTrustManagers(), null);
            return sslContext;
        } catch (Exception e) {
            Log.e("EXCEPTION",e.toString());
            //Print here right certificate failure issue
        }

最佳答案

终于找到答案了:-

try {
            CertificateFactory cf = CertificateFactory.getInstance("X.509");
            InputStream caInputLeaf = context.getResources().openRawResource(leafCert);
            InputStream caInputInter = context.getResources().openRawResource(interCert);
            try {
                if (cf != null) {
                    ca = cf.generateCertificate(caInputLeaf);

                    URL url = new URL(URL);
                    HttpsURLConnection conn = (HttpsURLConnection) url.openConnection();
                    conn.setRequestMethod("GET");
                    conn.connect();

                    chain = conn.getServerCertificates();
                    if(chain!=null && chain[0].equals(ca)) {           //Return Leaf certificate
                        return ca;
                    }
                    else{                                   //Return Intermediate certificate
                        ca = cf.generateCertificate(caInputInter);
                        return ca;
                    }
                }
            } catch (Exception cee) {
                ca = cf.generateCertificate(caInputInter);
                return ca;
            }
        } catch (Exception e) {
            Log.e("EXCEPTION", e.toString());
        }

关于android - 如何在 Android 中实现叶/中间证书固定?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43602877/

相关文章:

java - Android:单击提交按钮后将 'waiting' 字符串数据发送到数据库

Android 4.2及以上读取APN设置

Android QuickBlox getFile 响应问题

Android 抱歉,无法播放此视频

php - iOS 没有打开我的 https ://website SSL Certificates

python - 使用 pandas read_csv 和证书

ios - 如何在具有授权证书的 React Native Webview 上加载网站

java - 使用 Java 加密私钥

c# - X509Certificate.CreateFromCertFile - 指定的网络密码不正确

Apache X509 证书授权模式设置 HTTP header