2013-08-08 13 views
26

あり、この問題(javax.net.ssl.SSLPeerUnverifiedException:いいえピア証明書)に関する記事の数十はあるが、私は私の作品は何も見つかっていません。安全に固定:javax.net.ssl.SSLPeerUnverifiedException:いいえピア証明書

多くの投稿(this、およびthisなど)は、すべての証明書を受け入れることでこれを「解決」しますが、もちろんこれはテスト以外の目的には適していません。

その他は非常にローカライズされたように見えると私のために動作しません。誰かが私に欠けている洞察を持っていることを本当に願っています。

だから、私の問題:私は、HTTPS経由で接続する、唯一のローカルネットワーク経由でアクセス可能なサーバ上でテストしています。私はブラウザを介して必要な電話をうまく動作します。証明書について不平を言うことはなく、証明書をチェックするとすべてがよさそうです。

StringBuilder builder = new StringBuilder(); 
builder.append(/* stuff goes here*/); 

httpGet.setEntity(new UrlEncodedFormEntity(nameValuePairs)); 
ResponseHandler<String> responseHandler = new BasicResponseHandler(); 

// Execute HTTP Post Request. Response body returned as a string 
HttpClient httpClient = MyActivity.getHttpClient(); 
HttpGet httpGet = new HttpGet(builder.toString()); 

String jsonResponse = httpClient.execute(httpGet, responseHandler); //Line causing the Exception 

MyActivity.getHttpClient()のための私のコード:

私は私のAndroidデバイス上でしようとすると、私はjavax.net.ssl.SSLPeerUnverifiedException: No peer certificate

を得るここにそれを呼び出すコードです

protected synchronized static HttpClient getHttpClient(){ 
    if (httpClient != null) 
     return httpClient; 

    HttpParams httpParameters = new BasicHttpParams(); 
    HttpConnectionParams.setConnectionTimeout(httpParameters, TIMEOUT_CONNECTION); 
    HttpConnectionParams.setSoTimeout(httpParameters, TIMEOUT_SOCKET); 
    HttpProtocolParams.setVersion(httpParameters, HttpVersion.HTTP_1_1); 

    //Thread safe in case various AsyncTasks try to access it concurrently 
    SchemeRegistry schemeRegistry = new SchemeRegistry(); 
    schemeRegistry.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80)); 
    schemeRegistry.register(new Scheme("https", SSLSocketFactory.getSocketFactory(), 443)); 
    ClientConnectionManager cm = new ThreadSafeClientConnManager(httpParameters, schemeRegistry); 

    httpClient = new DefaultHttpClient(cm, httpParameters); 

    CookieStore cookieStore = httpClient.getCookieStore(); 
    HttpContext localContext = new BasicHttpContext(); 
    localContext.setAttribute(ClientContext.COOKIE_STORE, cookieStore); 

    return httpClient; 
} 

すべてのヘルプははるかになります感謝。

編集

またちょうど言及して私は別のアプリで、他のSSLの問題があったが、前に私のためにそれを固定SchemeRegistry部分を追加しました。

編集2

は、これまでのところ、私は唯一のAndroid 3.1上でテストしてみたが、私は、これは関係なく、2.2以降のAndroid上で動作する必要があります。私はちょうど私のAndroidのタブ(アンドロイド3.1)上のブラウザでテストし、それが証明文句を言います。それは私のPCのブラウザではいいですが、Androidのブラウザや私のアプリではありません。 3

編集は、iOSのブラウザはまた、それについて文句が判明します。私はそれが(SSL certificate is not trusted - on mobile only

+0

この問題はhttps mの右のためですか? –

+0

はい私はhttps経由で接続しています。私は質問を更新します –

+0

これを試してみましょう:https://stackoverflow.com/a/38598593/2301721 – ashishdhiman2007

答えて

5

てみたコードの下に、ここで説明した証明書チェーンの問題だと思うし始めている: -

 BasicHttpResponse httpResponse = null; 

     HttpPost httppost = new HttpPost(URL); 

     HttpParams httpParameters = new BasicHttpParams(); 
     // Set the timeout in milliseconds until a connection is 
     // established. 
     int timeoutConnection = ConstantLib.CONNECTION_TIMEOUT; 
     HttpConnectionParams.setConnectionTimeout(httpParameters, 
       timeoutConnection); 
     // Set the default socket timeout (SO_TIMEOUT) 
     // in milliseconds which is the timeout for waiting for data. 
     int timeoutSocket = ConstantLib.CONNECTION_TIMEOUT; 
     HttpConnectionParams 
       .setSoTimeout(httpParameters, timeoutSocket); 

     DefaultHttpClient httpClient = new DefaultHttpClient(
       httpParameters); 
     List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(); 
     nameValuePairs.add(new BasicNameValuePair(ConstantLib.locale, 
       locale)); 
+1

HttpGetを使用していますが、それでも起こりますが、それはまだ起こります。 –

+0

しかし、それは私のためのwrksです –

+4

クール。私はhttpsで動作するアプリも持っていますが、今度はそうではありません。それが質問です –

4

の詳細については、これはSOこのスーパーポスト投稿してご覧ください。 2日後、突然私のデバイスはhttpsサイトや画像リンクを表示しませんでした。

いくつかの調査の後、それは私の時間設定は、デバイス上の日付までにはなかったことが判明します。

私は自分の時間設定を正しく変更して機能しました。

0

自己署名証明書+ ipアドレスを使用した場合、私はこの例外が発生しました。これらの行を追加するだけです。

HostnameVerifier allHostsValid = new HostnameVerifier() { 
      @Override 
      public boolean verify(String hostname, SSLSession session) { 
       return true; 
      } 
     }; 
HttpsURLConnection.setDefaultHostnameVerifier(allHostsValid); 

あなたのHttpURLConnectionは動作します。 そのトリックはCAに対する検証とは関係ありません!だから間違ったCAを指定してそのトリックを使用すると、別の例外が発生します。

String certStr = context.getString(R.string.caApi); 
X509Certificate ca = SecurityHelper.readCert(certStr); 

TrustManagerFactory tmf = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm()); 
KeyStore ks = KeyStore.getInstance(KeyStore.getDefaultType()); 
ks.load(null); 
ks.setCertificateEntry("caCert", ca); 

tmf.init(ks); 

SSLContext sslContext = SSLContext.getInstance("TLS"); 
sslContext.init(null, tmf.getTrustManagers(), null); 
HttpsURLConnection.setDefaultSSLSocketFactory(sslContext.getSocketFactory()); 

クールな機能のbuildTypesを使用することを忘れて、RESフォルダ内のデバッグ/リリースの異なるCAを置かないでください:だからホスト遺骨は念のために、私はここに、独自のCAを指定するためのコードを残して

信頼しました。

関連する問題