使用自签名证书的MockWebServer握手失败

时间:2018-08-16 21:51:14

标签: android ssl self-signed okhttp mockwebserver

我正在寻找使用Android应用程序使用带有ssl的模拟网络服务器编写OkHttpClient的集成测试。我能够使用HeldCertificate和HandshakeCertificates实现此目的,但是我对使用自签名证书感兴趣。尝试使用自签名证书时,握手失败。下面是重现此内容的详细信息。

a。使用此命令创建的自签名证书

openssl req -x509 -out localhost.crt -keyout localhost.key -newkey rsa:2048 -nodes -sha256 -subj '/CN=localhost' -extensions EXT -config <( \ printf "[dn]\nCN=localhost\n[req]\ndistinguished_name = dn\n[EXT]\nsubjectAltName=DNS:localhost\nkeyUsage=digitalSignature\nextendedKeyUsage=serverAuth")

b。转换为der for android。 openssl x509 -in localhost.crt -outform der -out localhost.der.crt

c。使用此处提供的代码https://gist.github.com/erickok/7692592来获取SSLSocketFactory,以将其传递给mockWebserver.useHttps()和OkHttpClientBuilder..sslSocketFactory()。

尝试发送和接收非常简单的请求时遇到握手失败。

I/MockWebServer(16935): MockWebServer[38515] connection from null failed: javax.net.ssl.SSLHandshakeException: Handshake failed I/System.out(16935): TEMP: 7. Exception thrown javax.net.ssl.SSLProtocolException: SSL handshake terminated: ssl=0xeef2cd80: Failure in SSL library, usually a protocol error I/System.out(16935): error:100c5410:SSL routines:ssl3_read_bytes:SSLV3_ALERT_HANDSHAKE_FAILURE (external/boringssl/src/ssl/s3_pkt.c:972 0xee9c4b80:0x00000001) I/System.out(16935): error:100c009f:SSL routines:ssl3_get_server_hello:HANDSHAKE_FAILURE_ON_CLIENT_HELLO (external/boringssl/src/ssl/s3_clnt.c:771 0xea1a6827:0x00000000) Handshake failed W/System.err(16935): javax.net.ssl.SSLHandshakeException: Handshake failed W/System.err(16935): at com.android.org.conscrypt.OpenSSLSocketImpl.startHandshake(OpenSSLSocketImpl.java:441) W/System.err(16935): at okhttp3.internal.connection.RealConnection.connectTls(RealConnection.java:337) W/System.err(16935): at okhttp3.internal.connection.RealConnection.establishProtocol(RealConnection.java:297) W/System.err(16935): at okhttp3.internal.connection.RealConnection.connect(RealConnection.java:178) W/System.err(16935): at okhttp3.internal.connection.StreamAllocation.findConnection(StreamAllocation.java:272) W/System.err(16935): at okhttp3.internal.connection.StreamAllocation.findHealthyConnection(StreamAllocation.java:135) W/System.err(16935): at okhttp3.internal.connection.StreamAllocation.newStream(StreamAllocation.java:114) W/System.err(16935): at okhttp3.internal.connection.ConnectInterceptor.intercept(ConnectInterceptor.java:42) W/System.err(16935): at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:147) W/System.err(16935): at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:121) W/System.err(16935): at okhttp3.internal.cache.CacheInterceptor.intercept(CacheInterceptor.java:93) W/System.err(16935): at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:147) W/System.err(16935): at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:121) W/System.err(16935): at okhttp3.internal.http.BridgeInterceptor.intercept(BridgeInterceptor.java:93) W/System.err(16935): at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:147) W/System.err(16935): at okhttp3.internal.http.RetryAndFollowUpInterceptor.intercept(RetryAndFollowUpInterceptor.java:126) W/System.err(16935): at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:147) W/System.err(16935): at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:121) W/System.err(16935): at okhttp3.RealCall.getResponseWithInterceptorChain(RealCall.java:200) W/System.err(16935): at okhttp3.RealCall.execute(RealCall.java:77) W/System.err(16935): at com.xyz.okhttp3_abc.sample.test.SampleUiTest.setup(SampleUiTest.java:180) W/System.err(16935): at java.lang.reflect.Method.invoke(Native Method) W/System.err(16935): at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50) W/System.err(16935): at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12) W/System.err(16935): at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47) W/System.err(16935): at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:24) W/System.err(16935): at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325) W/System.err(16935): at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78) W/System.err(16935): at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57) W/System.err(16935): at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290) W/System.err(16935): at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71) W/System.err(16935): at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288) W/System.err(16935): at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58) W/System.err(16935): at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268) W/System.err(16935): at org.junit.runners.ParentRunner.run(ParentRunner.java:363) W/System.err(16935): at org.junit.runners.Suite.runChild(Suite.java:128) W/System.err(16935): at org.junit.runners.Suite.runChild(Suite.java:27) W/System.err(16935): at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290) W/System.err(16935): at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71) W/System.err(16935): at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288) W/System.err(16935): at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58) W/System.err(16935): at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268) W/System.err(16935): at org.junit.runners.ParentRunner.run(ParentRunner.java:363) W/System.err(16935): at org.junit.runners.Suite.runChild(Suite.java:128) W/System.err(16935): at org.junit.runners.Suite.runChild(Suite.java:27) W/System.err(16935): at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290) W/System.err(16935): at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71) W/System.err(16935): at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288) W/System.err(16935): at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58) W/System.err(16935): at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268) W/System.err(16935): at org.junit.runners.ParentRunner.run(ParentRunner.java:363) W/System.err(16935): at org.junit.runner.JUnitCore.run(JUnitCore.java:137) W/System.err(16935): at org.junit.runner.JUnitCore.run(JUnitCore.java:115) W/System.err(16935): at android.support.test.internal.runner.TestExecutor.execute(TestExecutor.java:56) W/System.err(16935): at android.support.test.runner.AndroidJUnitRunner.onStart(AndroidJUnitRunner.java:384) W/System.err(16935): at android.app.Instrumentation$InstrumentationThread.run(Instrumentation.java:1959) W/System.err(16935): Caused by: javax.net.ssl.SSLProtocolException: SSL handshake terminated: ssl=0xeef2cd80: Failure in SSL library, usually a protocol error W/System.err(16935): error:100c5410:SSL routines:ssl3_read_bytes:SSLV3_ALERT_HANDSHAKE_FAILURE (external/boringssl/src/ssl/s3_pkt.c:972 0xee9c4b80:0x00000001) W/System.err(16935): error:100c009f:SSL routines:ssl3_get_server_hello:HANDSHAKE_FAILURE_ON_CLIENT_HELLO (external/boringssl/src/ssl/s3_clnt.c:771 0xea1a6827:0x00000000) W/System.err(16935): at com.android.org.conscrypt.NativeCrypto.SSL_do_handshake(Native Method) W/System.err(16935): at com.android.org.conscrypt.OpenSSLSocketImpl.startHandshake(OpenSSLSocketImpl.java:353) W/System.err(16935): ... 55 more I/TestRunner(16935): failed: simpleTest[0](com.xyz.okhttp3_abc.sample.test.SampleUiTest) I/TestRunner(16935): failed: simpleTest[0](com.xyz.okhttp3_abc.sample.test.SampleUiTest) I/TestRunner(16935): ----- begin exception ----- I/TestRunner(16935): javax.net.ssl.SSLHandshakeException: Handshake failed I/TestRunner(16935): at com.android.org.conscrypt.OpenSSLSocketImpl.startHandshake(OpenSSLSocketImpl.java:441) I/TestRunner(16935): at okhttp3.internal.connection.RealConnection.connectTls(RealConnection.java:337) I/TestRunner(16935): at okhttp3.internal.connection.RealConnection.establishProtocol(RealConnection.java:297) I/TestRunner(16935): at okhttp3.internal.connection.RealConnection.connect(RealConnection.java:178) I/TestRunner(16935): at okhttp3.internal.connection.StreamAllocation.findConnection(StreamAllocation.java:272) I/TestRunner(16935): at okhttp3.internal.connection.StreamAllocation.findHealthyConnection(StreamAllocation.java:135) I/TestRunner(16935): at okhttp3.internal.connection.StreamAllocation.newStream(StreamAllocation.java:114) I/TestRunner(16935): at okhttp3.internal.connection.ConnectInterceptor.intercept(ConnectInterceptor.java:42) I/TestRunner(16935): at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:147) I/TestRunner(16935): at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:121) I/TestRunner(16935): at okhttp3.internal.cache.CacheInterceptor.intercept(CacheInterceptor.java:93) I/TestRunner(16935): at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:147) I/TestRunner(16935): at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:121) I/TestRunner(16935): at okhttp3.internal.http.BridgeInterceptor.intercept(BridgeInterceptor.java:93) I/TestRunner(16935): at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:147) I/TestRunner(16935): at okhttp3.internal.http.RetryAndFollowUpInterceptor.intercept(RetryAndFollowUpInterceptor.java:126) I/TestRunner(16935): at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:147) I/TestRunner(16935): at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:121) I/TestRunner(16935): at okhttp3.RealCall.getResponseWithInterceptorChain(RealCall.java:200) I/TestRunner(16935): at okhttp3.RealCall.execute(RealCall.java:77) I/TestRunner(16935): at com.xyz.okhttp3_abc.sample.test.SampleUiTest.setup(SampleUiTest.java:180) I/TestRunner(16935): at java.lang.reflect.Method.invoke(Native Method) I/TestRunner(16935): at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50) I/TestRunner(16935): at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12) I/TestRunner(16935): at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47) I/TestRunner(16935): at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:24) I/TestRunner(16935): at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325) I/TestRunner(16935): at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78) I/TestRunner(16935): at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57) I/TestRunner(16935): at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290) I/TestRunner(16935): at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71) I/TestRunner(16935): at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288) I/TestRunner(16935): at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58) I/TestRunner(16935): at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268) I/TestRunner(16935): at org.junit.runners.ParentRunner.run(ParentRunner.java:363) I/TestRunner(16935): at org.junit.runners.Suite.runChild(Suite.java:128) I/TestRunner(16935): at org.junit.runners.Suite.runChild(Suite.java:27) I/TestRunner(16935): at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290) I/TestRunner(16935): at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71) I/TestRunner(16935): at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288) I/TestRunner(16935): at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58) I/TestRunner(16935): at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268) I/TestRunner(16935): at org.junit.runners.ParentRunner.run(ParentRunner.java:363) I/TestRunner(16935): at org.junit.runners.Suite.runChild(Suite.java:128) I/TestRunner(16935): at org.junit.runners.Suite.runChild(Suite.java:27) I/TestRunner(16935): at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290) I/TestRunner(16935): at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71) I/TestRunner(16935): at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288) I/TestRunner(16935): at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58) I/TestRunner(16935): at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268) I/TestRunner(16935): at org.junit.runners.ParentRunner.run(ParentRunner.java:363) I/TestRunner(16935): at org.junit.runner.JUnitCore.run(JUnitCore.java:137) I/TestRunner(16935): at org.junit.runner.JUnitCore.run(JUnitCore.java:115) I/TestRunner(16935): at android.support.test.internal.runner.TestExecutor.execute(TestExecutor I/TestRunner(16935): ----- end exception ----- I/TestRunner(16935): finished: simpleTest[0](com.xyz.okhttp3_abc.sample.test.SampleUiTest) I/MonitoringInstr(16935): Activities that are still in CREATED to STOPPED: 0

0 个答案:

没有答案