2017-03-27 16 views
0

私はアンドロイドテストでSSLに問題があります。テストはAndroidテストのSSLHandshakeException

方法 requestSettingsは単純 HttpUrlConnectionを使用しています
@Test 
public void requestSettings() throws Exception { 
    TestSubscriberBase<Settings> subscriberBase = TestSubscribers.mustHaveResult(); 
    CommonApi.requestSettings(true).subscribe(subscriberBase); 
    System.out.println(subscriberBase.result.text); 
} 

のように見えます。また、アンドロイドアプリにはが完璧に機能します。私が試した何

javax.net.ssl.SSLHandshakeException: sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target 
    at sun.security.ssl.Alerts.getSSLException(Alerts.java:192) 
    at sun.security.ssl.SSLSocketImpl.fatal(SSLSocketImpl.java:1949) 
    at sun.security.ssl.Handshaker.fatalSE(Handshaker.java:302) 
    at sun.security.ssl.Handshaker.fatalSE(Handshaker.java:296) 
    at sun.security.ssl.ClientHandshaker.serverCertificate(ClientHandshaker.java:1509) 
    at sun.security.ssl.ClientHandshaker.processMessage(ClientHandshaker.java:216) 
    at sun.security.ssl.Handshaker.processLoop(Handshaker.java:979) 
    at sun.security.ssl.Handshaker.process_record(Handshaker.java:914) 
    at sun.security.ssl.SSLSocketImpl.readRecord(SSLSocketImpl.java:1062) 
    at sun.security.ssl.SSLSocketImpl.performInitialHandshake(SSLSocketImpl.java:1375) 
    at sun.security.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.java:1403) 
    at sun.security.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.java:1387) 
    at sun.net.www.protocol.https.HttpsClient.afterConnect(HttpsClient.java:559) 
    at sun.net.www.protocol.https.AbstractDelegateHttpsURLConnection.connect(AbstractDelegateHttpsURLConnection.java:185) 
    at sun.net.www.protocol.https.HttpsURLConnectionImpl.connect(HttpsURLConnectionImpl.java:153) 
    at ru.reksoft.platform.api.ApiGate.sendCommonRequest(ApiGate.java:170) 
    at ru.reksoft.platform.api.ApiGate.sendRequest(ApiGate.java:121) 
    at ru.reksoft.platform.test.TestApiGate.sendRequest(TestApiGate.java:91) 
    at ru.reksoft.okey.api.ApiBase$1.call(ApiBase.java:30) 
    at ru.reksoft.okey.api.ApiBase$1.call(ApiBase.java:24) 
    at rx.Observable.unsafeSubscribe(Observable.java:10144) 
    at rx.internal.operators.OperatorSubscribeOn$1.call(OperatorSubscribeOn.java:94) 
    at rx.internal.schedulers.ImmediateScheduler$InnerImmediateScheduler.schedule(ImmediateScheduler.java:58) 
    at rx.internal.operators.OperatorSubscribeOn.call(OperatorSubscribeOn.java:45) 
    at rx.internal.operators.OperatorSubscribeOn.call(OperatorSubscribeOn.java:30) 
    at rx.Observable.unsafeSubscribe(Observable.java:10144) 
    at rx.internal.operators.OnSubscribeRedo$2.call(OnSubscribeRedo.java:273) 
    at rx.internal.schedulers.TrampolineScheduler$InnerCurrentThreadScheduler.enqueue(TrampolineScheduler.java:73) 
    at rx.internal.schedulers.TrampolineScheduler$InnerCurrentThreadScheduler.schedule(TrampolineScheduler.java:52) 
    at rx.internal.operators.OnSubscribeRedo$5.request(OnSubscribeRedo.java:361) 
    at rx.internal.producers.ProducerArbiter.setProducer(ProducerArbiter.java:126) 
    at rx.internal.operators.OnSubscribeRedo$2$1.setProducer(OnSubscribeRedo.java:267) 
    at rx.Subscriber.setProducer(Subscriber.java:205) 
    at rx.Subscriber.setProducer(Subscriber.java:205) 
    at rx.internal.operators.OnSubscribeRedo.call(OnSubscribeRedo.java:353) 
    at rx.internal.operators.OnSubscribeRedo.call(OnSubscribeRedo.java:47) 
    at rx.Observable.unsafeSubscribe(Observable.java:10144) 
    at rx.internal.operators.OnSubscribeDoOnEach.call(OnSubscribeDoOnEach.java:41) 
    at rx.internal.operators.OnSubscribeDoOnEach.call(OnSubscribeDoOnEach.java:30) 
    at rx.Observable.unsafeSubscribe(Observable.java:10144) 
    at rx.internal.operators.OnSubscribeDoOnEach.call(OnSubscribeDoOnEach.java:41) 
    at rx.internal.operators.OnSubscribeDoOnEach.call(OnSubscribeDoOnEach.java:30) 
    at rx.Observable.unsafeSubscribe(Observable.java:10144) 
    at rx.internal.operators.OnSubscribeRedo$2.call(OnSubscribeRedo.java:273) 
    at rx.internal.schedulers.TrampolineScheduler$InnerCurrentThreadScheduler.enqueue(TrampolineScheduler.java:73) 
    at rx.internal.schedulers.TrampolineScheduler$InnerCurrentThreadScheduler.schedule(TrampolineScheduler.java:52) 
    at rx.internal.operators.OnSubscribeRedo$5.request(OnSubscribeRedo.java:361) 
    at rx.internal.producers.ProducerArbiter.setProducer(ProducerArbiter.java:126) 
    at rx.internal.operators.OnSubscribeRedo$2$1.setProducer(OnSubscribeRedo.java:267) 
    at rx.internal.operators.OnSubscribeRedo.call(OnSubscribeRedo.java:353) 
    at rx.internal.operators.OnSubscribeRedo.call(OnSubscribeRedo.java:47) 
    at rx.Observable.unsafeSubscribe(Observable.java:10144) 
    at rx.internal.operators.OnSubscribeRedo$2.call(OnSubscribeRedo.java:273) 
    at rx.internal.schedulers.TrampolineScheduler$InnerCurrentThreadScheduler.enqueue(TrampolineScheduler.java:73) 
    at rx.internal.schedulers.TrampolineScheduler$InnerCurrentThreadScheduler.schedule(TrampolineScheduler.java:52) 
    at rx.internal.operators.OnSubscribeRedo$5.request(OnSubscribeRedo.java:361) 
    at rx.Subscriber.setProducer(Subscriber.java:209) 
    at rx.Subscriber.setProducer(Subscriber.java:205) 
    at rx.Subscriber.setProducer(Subscriber.java:205) 
    at rx.Subscriber.setProducer(Subscriber.java:205) 
    at rx.Subscriber.setProducer(Subscriber.java:205) 
    at rx.Subscriber.setProducer(Subscriber.java:205) 
    at rx.internal.operators.OnSubscribeRedo.call(OnSubscribeRedo.java:353) 
    at rx.internal.operators.OnSubscribeRedo.call(OnSubscribeRedo.java:47) 
    at rx.Observable.unsafeSubscribe(Observable.java:10144) 
    at rx.internal.operators.OnSubscribeDoOnEach.call(OnSubscribeDoOnEach.java:41) 
    at rx.internal.operators.OnSubscribeDoOnEach.call(OnSubscribeDoOnEach.java:30) 
    at rx.internal.operators.OnSubscribeLift.call(OnSubscribeLift.java:48) 
    at rx.internal.operators.OnSubscribeLift.call(OnSubscribeLift.java:30) 
    at rx.Observable.unsafeSubscribe(Observable.java:10144) 
    at rx.internal.operators.OnSubscribeDoOnEach.call(OnSubscribeDoOnEach.java:41) 
    at rx.internal.operators.OnSubscribeDoOnEach.call(OnSubscribeDoOnEach.java:30) 
    at rx.Observable.unsafeSubscribe(Observable.java:10144) 
    at rx.internal.operators.OnSubscribeDoOnEach.call(OnSubscribeDoOnEach.java:41) 
    at rx.internal.operators.OnSubscribeDoOnEach.call(OnSubscribeDoOnEach.java:30) 
    at rx.Observable.unsafeSubscribe(Observable.java:10144) 
    at rx.internal.operators.OnSubscribeDoOnEach.call(OnSubscribeDoOnEach.java:41) 
    at rx.internal.operators.OnSubscribeDoOnEach.call(OnSubscribeDoOnEach.java:30) 
    at rx.Observable.subscribe(Observable.java:10240) 
    at rx.Observable.subscribe(Observable.java:10207) 
    at ru.reksoft.okey.tests.CommonApiTest.requestSettings(CommonApiTest.java:52) 
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) 
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) 
    at java.lang.reflect.Method.invoke(Method.java:498) 
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50) 
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12) 
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47) 
    at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17) 
    at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26) 
    at org.junit.rules.TestWatcher$1.evaluate(TestWatcher.java:55) 
    at org.junit.rules.RunRules.evaluate(RunRules.java:20) 
    at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325) 
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78) 
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57) 
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290) 
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71) 
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288) 
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58) 
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268) 
    at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26) 
    at org.junit.runners.ParentRunner.run(ParentRunner.java:363) 
    at org.junit.runner.JUnitCore.run(JUnitCore.java:137) 
    at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:119) 
    at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:42) 
    at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:234) 
    at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:74) 
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) 
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) 
    at java.lang.reflect.Method.invoke(Method.java:498) 
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:144) 
Caused by: sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target 
    at sun.security.validator.PKIXValidator.doBuild(PKIXValidator.java:387) 
    at sun.security.validator.PKIXValidator.engineValidate(PKIXValidator.java:292) 
    at sun.security.validator.Validator.validate(Validator.java:260) 
    at sun.security.ssl.X509TrustManagerImpl.validate(X509TrustManagerImpl.java:324) 
    at sun.security.ssl.X509TrustManagerImpl.checkTrusted(X509TrustManagerImpl.java:229) 
    at sun.security.ssl.X509TrustManagerImpl.checkServerTrusted(X509TrustManagerImpl.java:124) 
    at sun.security.ssl.ClientHandshaker.serverCertificate(ClientHandshaker.java:1491) 
    ... 107 more 
Caused by: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target 
    at sun.security.provider.certpath.SunCertPathBuilder.build(SunCertPathBuilder.java:141) 
    at sun.security.provider.certpath.SunCertPathBuilder.engineBuild(SunCertPathBuilder.java:126) 
    at java.security.cert.CertPathBuilder.build(CertPathBuilder.java:280) 
    at sun.security.validator.PKIXValidator.doBuild(PKIXValidator.java:382) 
    ... 113 more 

すべての証明書

TrustManager[] trustAllCerts = new TrustManager[] { 
    new X509TrustManager() { 
     public X509Certificate[] getAcceptedIssuers() { 
      return new X509Certificate[0]; 
     } 
     public void checkClientTrusted(X509Certificate[] certs, String authType) {} 
     public void checkServerTrusted(X509Certificate[] certs, String authType) {} 
    }}; 

HostnameVerifier hv = new HostnameVerifier() { 
    public boolean verify(String hostname, SSLSession session) { return true; } 
}; 

try { 
    SSLContext sc = SSLContext.getInstance("SSL"); 
    sc.init(null, trustAllCerts, new SecureRandom()); 
    HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory()); 
    HttpsURLConnection.setDefaultHostnameVerifier(hv); 
} catch (Exception e) { 
} 
を信頼

  1. 私は、HTTPSをテストするには、このエラーを取得してAndroidのメーカーI`mからの要求を試してみました

  2. システムプロパティDcom.sun.neの設定t.ssl.checkRevocation = falseをSystem.getProperties().setProperty("Dcom.sun.net.ssl.checkRevocation","false");

だから私はSSLHandshakeExceptionを避けるために何ができますか?

P.S.多くのテストURLがあるので証明書を追加したくないので、 URLの証明書を追加するのに多くの時間がかかりません。

答えて

1

問題が見つかりました。

HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory()); 
HttpsURLConnection.setDefaultHostnameVerifier(hv); 

これらの2つの方法は、Androidテスト環境でを動作しますが、アンドロイドのアプリケーションでは動作しませんように見えます。

私は実際にHttpsUrlConnectionインスタンスを渡し、SSLSocketFactoryHostNameVerifierと設定しました。

public void disableSSLinitDisablingSSL(HttpsURLConnection httpsURLConnection { 
    TrustManager[] trustAllCerts = new TrustManager[] { 
     new X509TrustManager() { 
      public X509Certificate[] getAcceptedIssuers() { 
       return new X509Certificate[0]; 
      } 
      public void checkClientTrusted(X509Certificate[] certs, String authType) {} 
      public void checkServerTrusted(X509Certificate[] certs, String authType) {} 
     }}; 

    HostnameVerifier hv = new HostnameVerifier() { 
    public boolean verify(String hostname, SSLSession session) { return true; } 
    }; 

    SSLContext sc = SSLContext.getInstance("SSL"); 
    sc.init(null, trustAllCerts, new SecureRandom()); 
    httpsURLConnection.setSSLSocketFactory(sc.getSocketFactory()); 
    httpsURLConnection.setHostnameVerifier(hv); 
} 
関連する問題