在InstrumentedTest上将SSL与MockWebServer一起使用

时间:2018-10-07 13:56:06

标签: android ssl mockwebserver

我想使用MockWebServer创建Android InstrumentedTest并启用SSL。 我使用:Portecle创建了“ BKS”证书。

我使用以下代码来创建SSLSocketFactory:

    private SSLSocketFactory getSSLSocketFactory_KeyStore(Context context, String keyStoreType, int keystoreResId, String keyPassword)
        throws CertificateException, KeyStoreException, IOException, NoSuchAlgorithmException, KeyManagementException {

    InputStream caInput = context.getResources().openRawResource(keystoreResId);

    if (keyStoreType == null || keyStoreType.length() == 0) {
        keyStoreType = KeyStore.getDefaultType();
    }
    KeyStore keyStore = KeyStore.getInstance(keyStoreType);

    keyStore.load(caInput, keyPassword.toCharArray());

    String tmfAlgorithm = TrustManagerFactory.getDefaultAlgorithm();
    TrustManagerFactory tmf = TrustManagerFactory.getInstance(tmfAlgorithm);
    tmf.init(keyStore);

    TrustManager[] wrappedTrustManagers = getWrappedTrustManagers(tmf.getTrustManagers());

    SSLContext sslContext = SSLContext.getInstance("TLSv1.2");
    sslContext.init(null, wrappedTrustManagers, null);

    return new NoSSLv3SocketFactory(sslContext.getSocketFactory());
}

然后:

final MockWebServer server = new MockWebServer();
    server.useHttps(getSSLSocketFactory_KeyStore(appContext, "BKS", R.raw.mykeystore, "mypassword"), false);

我不断收到以下异常:

Caused by: javax.net.ssl.SSLProtocolException: SSL handshake aborted: 

ssl=0xdfca3778: Failure in SSL library, usually a protocol error
error:10000410:SSL routines:OPENSSL_internal:SSLV3_ALERT_HANDSHAKE_FAILURE (third_party/openssl/boringssl/src/ssl/tls_record.cc:586 0xc718d288:0x00000001)
error:1000009a:SSL routines:OPENSSL_internal:HANDSHAKE_FAILURE_ON_CLIENT_HELLO (third_party/openssl/boringssl/src/ssl/handshake.cc:542 0xc7800f0d:0x00000000)
at com.google.android.gms.org.conscrypt.NativeCrypto.SSL_do_handshake(Native Method)
at com.google.android.gms.org.conscrypt.NativeSsl.doHandshake(:com.google.android.gms@12862026@12.8.62 (040700-199405334):3)
at com.google.android.gms.org.conscrypt.ConscryptFileDescriptorSocket.startHandshake(:com.google.android.gms@12862026@12.8.62 (040700-199405334):15)

有什么想法吗?

0 个答案:

没有答案