{"id":1634,"date":"2023-03-25T21:34:09","date_gmt":"2023-03-25T13:34:09","guid":{"rendered":"https:\/\/www.appblog.cn\/?p=1634"},"modified":"2023-04-23T22:01:04","modified_gmt":"2023-04-23T14:01:04","slug":"android-anti-packet-capture-certificate-strong-verification","status":"publish","type":"post","link":"https:\/\/www.appblog.cn\/index.php\/2023\/03\/25\/android-anti-packet-capture-certificate-strong-verification\/","title":{"rendered":"Android\u9632\u6293\u5305(\u8bc1\u4e66\u5f3a\u6821\u9a8c)"},"content":{"rendered":"<h2>Android HTTPS\u6293\u5305<\/h2>\n<p>\u5e38\u89c4https\u6293\u5305\u65b9\u6848\u4fbf\u662f\u901a\u8fc7\u5185\u7f6e\u4ee3\u7406\u8bc1\u4e66\u6765\u8fdb\u884c\u6293\u5305\u7684\u3002<\/p>\n<ul>\n<li>\u5176\u4e2d\u57287.0\u4ee5\u4e0b\uff0c\u5982\u679c\u5e94\u7528\u4e0d\u505a\u8bc1\u4e66\u5f3a\u6821\u9a8c\uff0c\u76f4\u63a5\u901a\u8fc7https\u8fdb\u884c\u8bf7\u6c42\u662f\u53ef\u4ee5\u8fdb\u884c\u6293\u5305\u7684\u3002<\/li>\n<li>\u800c\u5230\u4e867.0\u53ca\u4ee5\u4e0a\uff0c\u7531\u4e8e\u66f4\u65b0\u4e86\u5b89\u5168\u673a\u5236\uff0c\u5bfc\u81f4\u5e94\u7528\u9ed8\u8ba4\u4e0d\u4fe1\u4efb\u7528\u6237\u624b\u52a8\u5b89\u88c5\u7684\u8bc1\u4e66\uff0c\u6240\u4ee5\u5373\u4f7f\u662f\u6ca1\u6709\u4e3b\u52a8\u505a\u8bc1\u4e66\u5f3a\u6821\u9a8c\u4e5f\u662f\u6293\u4e0d\u4e86\u5305\u7684\u3002<\/li>\n<\/ul>\n<p><!-- more --><\/p>\n<h2>\u8bc1\u4e66\u5f3a\u6821\u9a8c<\/h2>\n<p>\u6d4f\u89c8\u5668\u5bfc\u51fa\u8bc1\u4e66\uff1a\u9009\u62e9<code>Base64 \u7f16\u7801X.509(.CER)(S)<\/code><\/p>\n<p>CustomTrust.java<\/p>\n<pre><code class=\"language-java\">import java.io.IOException;\nimport java.io.InputStream;\nimport java.security.GeneralSecurityException;\nimport java.security.KeyStore;\nimport java.security.cert.Certificate;\nimport java.security.cert.CertificateFactory;\nimport java.util.Arrays;\nimport java.util.Collection;\n\nimport javax.net.ssl.KeyManagerFactory;\nimport javax.net.ssl.TrustManager;\nimport javax.net.ssl.TrustManagerFactory;\nimport javax.net.ssl.X509TrustManager;\n\nimport okhttp3.CertificatePinner;\nimport okio.Buffer;\n\npublic class CustomTrust {\n\n    public static InputStream trustedCertificatesInputStream() {\n        String comodoRsaCertificationAuthority = &quot;-----BEGIN CERTIFICATE-----\\n&quot; +\n                &quot;MIIJrzCCCJegAwIBAgIMLO4ZPBiCeOo+Q3VzMA0GCSqGSIb3DQEBCwUAMGYxCzAJ\\n&quot; +\n                &quot;BgNVBAYTAkJFMRkwFwYDVQQKExBHbG9iYWxTaWduIG52LXNhMTwwOgYDVQQDEzNH\\n&quot; +\n                ...\n                &quot;nRBHS8ITYjTG0Dw5CTklj\/6i9PP735snPfzQKOht3N0X0x8=\\n&quot; +\n                &quot;-----END CERTIFICATE-----\\n&quot;;\n        String entrustRootCertificateAuthority = &quot;&quot;;\n        return new Buffer()\n                .writeUtf8(comodoRsaCertificationAuthority)\n                .writeUtf8(entrustRootCertificateAuthority)\n                .inputStream();\n    }\n\n    public static X509TrustManager trustManagerForCertificates(InputStream in) throws GeneralSecurityException {\n        CertificateFactory certificateFactory = CertificateFactory.getInstance(&quot;X.509&quot;);\n        Collection&lt;? extends Certificate&gt; certificates = certificateFactory.generateCertificates(in);\n        if (certificates.isEmpty()) {\n            throw new IllegalArgumentException(&quot;expected non-empty set of trusted certificates&quot;);\n        }\n\n        \/\/ Put the certificates a key store.\n        char[] password = &quot;password&quot;.toCharArray(); \/\/ Any password will work.\n        KeyStore keyStore = newEmptyKeyStore(password);\n        int index = 0;\n        for (Certificate certificate : certificates) {\n            String certificateAlias = Integer.toString(index++);\n            keyStore.setCertificateEntry(certificateAlias, certificate);\n        }\n\n        \/\/ Use it to build an X509 trust manager.\n        KeyManagerFactory keyManagerFactory = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm());\n        keyManagerFactory.init(keyStore, password);\n        TrustManagerFactory trustManagerFactory = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());\n        trustManagerFactory.init(keyStore);\n        TrustManager[] trustManagers = trustManagerFactory.getTrustManagers();\n        if (trustManagers.length != 1 || !(trustManagers[0] instanceof X509TrustManager)) {\n            throw new IllegalStateException(&quot;Unexpected default trust managers:&quot; + Arrays.toString(trustManagers));\n        }\n        return (X509TrustManager) trustManagers[0];\n    }\n\n    private static KeyStore newEmptyKeyStore(char[] password) throws GeneralSecurityException {\n        try {\n            KeyStore keyStore = KeyStore.getInstance(KeyStore.getDefaultType());\n            InputStream in = null; \/\/ By convention, &#039;null&#039; creates an empty key store.\n            keyStore.load(in, password);\n            return keyStore;\n        } catch (IOException e) {\n            throw new AssertionError(e);\n        }\n    }\n}<\/code><\/pre>\n<p>OKHttp3\u4e2d\u5f15\u5165\u8bc1\u4e66\u5f3a\u6821\u9a8c\u7684\u6838\u5fc3\u4ee3\u7801<\/p>\n<pre><code class=\"language-java\">X509TrustManager trustManager;\nSSLSocketFactory sslSocketFactory;\ntry {\n    trustManager = CustomTrust.trustManagerForCertificates(CustomTrust.trustedCertificatesInputStream());\n    SSLContext sslContext = SSLContext.getInstance(&quot;TLS&quot;);\n    sslContext.init(null, new TrustManager[]{trustManager}, null);\n    sslSocketFactory = sslContext.getSocketFactory();\n} catch (GeneralSecurityException e) {\n}\n\nmClient = new OkHttpClient().newBuilder()\n        .retryOnConnectionFailure(true)\n        .writeTimeout(TIMEOUT, TimeUnit.SECONDS)\n        .connectTimeout(TIMEOUT, TimeUnit.SECONDS)\n        .readTimeout(TIMEOUT, TimeUnit.SECONDS)\n        .sslSocketFactory(sslSocketFactory, trustManager)\n        .build();<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Android HTTPS\u6293\u5305 \u5e38\u89c4https\u6293\u5305\u65b9\u6848\u4fbf\u662f\u901a\u8fc7\u5185\u7f6e\u4ee3\u7406\u8bc1\u4e66\u6765\u8fdb\u884c\u6293\u5305\u7684\u3002 \u5176\u4e2d\u57287.0\u4ee5\u4e0b\uff0c\u5982 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[311],"tags":[407],"class_list":["post-1634","post","type-post","status-publish","format-standard","hentry","category-android-advance","tag-407"],"_links":{"self":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/1634","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/comments?post=1634"}],"version-history":[{"count":0,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/1634\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/media?parent=1634"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/categories?post=1634"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/tags?post=1634"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}