gpt4 book ai didi

Android SSL 证书固定

转载 作者:行者123 更新时间:2023-11-29 01:33:40 24 4
gpt4 key购买 nike

我知道有很多关于在 Android 中固定证书的问题,但我找不到我要找的东西......

我将 SSLSocketFactory 子类化并覆盖 checkServerTrusted() 方法。在此方法中,我执行以下操作:

CertificateFactory cf = CertificateFactory.getInstance("X.509");
X509Certificate ca = (X509Certificate) cf.generateCertificate(new ByteArrayInputStream(PUB_KEY.getBytes("UTF-8")));
for (X509Certificate cert : chain) {
// Verifing by public key
cert.verify(ca.getPublicKey());
}

链中的一个项目验证而另一个不验证(抛出一个Exception)。我想我无法理解证书链是如何工作的。

同一个公共(public)证书是否应该验证链中的所有证书?

最佳答案

我发现在 Android 上实现证书固定的最简单方法是使用 OkHttp图书馆。

这是一个 excerpt from the documentation :

By default, OkHttp trusts the certificate authorities of the host platform. This strategy maximizes connectivity, but it is subject to certificate authority attacks such as the 2011 DigiNotar attack. It also assumes your HTTPS servers’ certificates are signed by a certificate authority.

Use CertificatePinner to constrain which certificate authorities are trusted. Certificate pinning increases security, but limits your server team’s abilities to update their TLS certificates. Do not use certificate pinning without the blessing of your server’s TLS administrator!

  public CertificatePinning() {
client = new OkHttpClient();
client.setCertificatePinner(
new CertificatePinner.Builder()
.add("publicobject.com", "sha1/DmxUShsZuNiqPQsX2Oi9uv2sCnw=")
.add("publicobject.com", "sha1/SXxoaOSEzPC6BgGmxAt/EAcsajw=")
.add("publicobject.com", "sha1/blhOM3W9V/bVQhsWAcLYwPU6n24=")
.add("publicobject.com", "sha1/T5x9IXmcrQ7YuQxXnxoCmeeQ84c=")
.build());
}

public void run() throws Exception {
Request request = new Request.Builder()
.url("https://publicobject.com/robots.txt")
.build();

Response response = client.newCall(request).execute();
if (!response.isSuccessful()) throw new IOException("Unexpected code " + response);

for (Certificate certificate : response.handshake().peerCertificates()) {
System.out.println(CertificatePinner.pin(certificate));
}
}

如果您需要支持自签名证书,请回答 Does OkHttp support accepting self-signed SSL certs?会指导你。

关于Android SSL 证书固定,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29849495/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com