Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ssl/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 使用SSL固定的呼叫错误_Android_Ssl_Openssl_Httpclient_Bouncycastle - Fatal编程技术网

Android 使用SSL固定的呼叫错误

Android 使用SSL固定的呼叫错误,android,ssl,openssl,httpclient,bouncycastle,Android,Ssl,Openssl,Httpclient,Bouncycastle,我尝试按照以下方法实现SSL固定: 该策略是将受信任的证书放入密钥库中,并扩展所使用的,以便只接受密钥库中包含的证书 除了三星Galaxy S2(欧洲版本2.3.6)外,它在多个测试设备(Nexus、三星S1、Wildfire等)上都能正常工作。偶尔(可能3-4次尝试)我会收到以下错误消息之一: 错误:0407006A:rsa例程:rsa\u padding\u check\u PKCS1\u type\u 1:block 类型不是01(SHA-1) 错误:04067084:rsa例程:rs

我尝试按照以下方法实现SSL固定:

该策略是将受信任的证书放入密钥库中,并扩展所使用的,以便只接受密钥库中包含的证书

除了三星Galaxy S2(欧洲版本2.3.6)外,它在多个测试设备(Nexus、三星S1、Wildfire等)上都能正常工作。偶尔(可能3-4次尝试)我会收到以下错误消息之一:

  • 错误:0407006A:rsa例程:rsa\u padding\u check\u PKCS1\u type\u 1:block 类型不是01(SHA-1)
  • 错误:04067084:rsa例程:rsa_EAY_PUBLIC_DECRYPT:数据对于模数(SHA-1)太大
只有在BKS中有多个具有相同CNAME条目的密钥时,才会出现此问题。当我只在密钥库中放入一个密钥时,每次都可以正常工作。但是,如果我想更改后端的证书,为了有一个平稳的过渡期,我需要应用程序能够接受同一主机的两个密钥

任何关于错误消息的原因以及如何规避错误消息的想法都将受到高度赞赏

谢谢大家!

该策略是将可信证书放入BKS密钥库中 并扩展HTTPClient使用的SSLSocketFactory,以便 接受密钥库中包含的证书

对于固定,您只需要扩展
X509TrustManager
并覆盖
checkServerTrusted

OWASP提供了一个示例(参见Android示例)。所有示例都从站点的证书获取随机字节并锁定,以确保没有PKI有趣的业务:

public final class PubKeyManager implements X509TrustManager
{
  private static String PUB_KEY = "30820122300d06092a864886f70d0101" +
    "0105000382010f003082010a0282010100b35ea8adaf4cb6db86068a836f3c85" +
    "5a545b1f0cc8afb19e38213bac4d55c3f2f19df6dee82ead67f70a990131b6bc" +
    "ac1a9116acc883862f00593199df19ce027c8eaaae8e3121f7f329219464e657" +
    "2cbf66e8e229eac2992dd795c4f23df0fe72b6ceef457eba0b9029619e0395b8" +
    "609851849dd6214589a2ceba4f7a7dcceb7ab2a6b60c27c69317bd7ab2135f50" +
    "c6317e5dbfb9d1e55936e4109b7b911450c746fe0d5d07165b6b23ada7700b00" +
    "33238c858ad179a82459c4718019c111b4ef7be53e5972e06ca68a112406da38" +
    "cf60d2f4fda4d1cd52f1da9fd6104d91a34455cd7b328b02525320a35253147b" +
    "e0b7a5bc860966dc84f10d723ce7eed5430203010001";

  public void checkServerTrusted(X509Certificate[] chain, String authType) throws CertificateException
  {
    if (chain == null) {
      throw new IllegalArgumentException("checkServerTrusted: X509Certificate array is null");
    }

    if (!(chain.length > 0)) {
      throw new IllegalArgumentException("checkServerTrusted: X509Certificate is empty");
    }

    if (!(null != authType && authType.equalsIgnoreCase("RSA"))) {
      throw new CertificateException("checkServerTrusted: AuthType is not RSA");
    }

    // Perform customary SSL/TLS checks
    try {
      TrustManagerFactory tmf = TrustManagerFactory.getInstance("X509");
      tmf.init((KeyStore) null);

      for (TrustManager trustManager : tmf.getTrustManagers()) {
        ((X509TrustManager) trustManager).checkServerTrusted(chain, authType);
      }
    } catch (Exception e) {
      throw new CertificateException(e);
    }

    // Hack ahead: BigInteger and toString(). We know a DER encoded Public Key begins
    // with 0x30 (ASN.1 SEQUENCE and CONSTRUCTED), so there is no leading 0x00 to drop.
    RSAPublicKey pubkey = (RSAPublicKey) chain[0].getPublicKey();
    String encoded = new BigInteger(1 /* positive */, pubkey.getEncoded()).toString(16);

    // Pin it!
    final boolean expected = PUB_KEY.equalsIgnoreCase(encoded);
    if (!expected) {
      throw new CertificateException("checkServerTrusted: Expected public key: "
                + PUB_KEY + ", got public key:" + encoded);
      }
    }
  }
}

谢谢,我很久以前也用过这种方法。BKS存储的东西不能正常工作,并且会产生不必要的开销。。。