3

私は自己署名証明書を使ってセキュアなhttp接続をテストしようとしています...開発目的のためです。しかし、私は、ピア認証されていない例外を解決することができていない、もちろん私は、この例外に関する同様の記事を見ていると、次の一つは、現在私が使用している実装です:httpClientのSSLPeerUnverifiedException

public class SelfCertificatesSocketFactory extends SSLSocketFactory { 

SSLContext sslContext = SSLContext.getInstance("TLS"); 

public SelfCertificatesSocketFactory(KeyStore trustStore) throws NoSuchAlgorithmException,UnrecoverableKeyException,KeyStoreException,KeyManagementException { 
    super(trustStore); 

     TrustManager tm = new X509TrustManager() { 
      public void checkClientTrusted(X509Certificate[] chain, String authType) throws CertificateException { 
      } 

      public void checkServerTrusted(X509Certificate[] chain, String authType) throws CertificateException { 
      } 

      public X509Certificate[] getAcceptedIssuers() { 
       return null; 
      } 
     }; 




} 

@Override 
public Socket createSocket() throws IOException { 
    return sslContext.getSocketFactory().createSocket(); 
} 



@Override 
public Socket createSocket(Socket socket, String host, int port, 
     boolean autoClose) throws IOException, UnknownHostException { 
    return sslContext.getSocketFactory().createSocket(socket,host,port,autoClose); 
} 



} 

と使用方法:

private DefaultHttpClient createHttpsClient(){ 
    try { 
     KeyStore trustStore = KeyStore.getInstance(KeyStore.getDefaultType()); 
     trustStore.load(null, null); 

     SSLSocketFactory sf = new SelfCertificatesSocketFactory(trustStore); 
     //sf.setHostnameVerifier(SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER); 

     SchemeRegistry registry = new SchemeRegistry(); 
     registry.register(new Scheme("https", 443, sf)); 

     ClientConnectionManager ccm = new ThreadSafeClientConnManager(registry); 
     return new DefaultHttpClient(ccm); 
    } catch (Exception e) { 
     return new DefaultHttpClient(); 
    } 

} 

しかし、私はまだ例外が発生しています。私が間違っていることは何ですか? PD:Java Webアプリケーションを実装しています。これはAndroidクライアントではありません。 ありがとうございました。

+0

あなたは、JVMのトラストストアに証明書をインポートしたことがありますか? – kosa

+0

いいえ、それを行う方法がわかりません...私が持っているのは、keytoolによって生成された.keystoreファイルです。 – Pablo

+0

完全な質問をいただきありがとうございます。私はこのコードを見たことがありませんでした。 '' 'KeyStore trustStore = KeyStore.getInstance(KeyStore.getDefaultType()); trustStore.load(null、null); SSLSocketFactory sf = new SelfCertificatesSocketFactory(trustStore); '' ' – pulkitsinghal

答えて

2

あなたのコードで作成されたトラストマネージャインスタンスは、どこにも使われていないようで、KeyStoreインスタンスにはトラストマテリアルが含まれていないようです。

これをすべて実行する代わりに、機能を利用するだけでSSLSocketFactoryにする必要があります。次のコードを使用することで解決それを作った

TrustStrategy easyStrategy = new TrustStrategy() { 
    public boolean isTrusted(X509Certificate[] chain, String authType) 
      throws CertificateException { 
     // eh, why not? 
     return true; 
    } 
}; 
SSLSocketFactory sf = new SSLSocketFactory(easyStrategy); 
2

おかげで、:私の特定のケースで

HttpClient client = null; 
    TrustStrategy easyStrategy = new TrustStrategy() { 

     @Override 
     public boolean isTrusted(X509Certificate[] certificate, String authType) 
       throws CertificateException { 
      return true; 
     } 
    }; 
    try { 

     SSLSocketFactory sf = new SSLSocketFactory(easyStrategy,SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER); 
     SchemeRegistry registry = new SchemeRegistry(); 
     registry.register(new Scheme("https", 8443, sf)); 

     ClientConnectionManager ccm = new ThreadSafeClientConnManager(registry); 
     client = new DefaultHttpClient(ccm); 

    } catch (KeyManagementException e1) { 
     e1.printStackTrace(); 
    } catch (UnrecoverableKeyException e1) { 
     e1.printStackTrace(); 
    } catch (NoSuchAlgorithmException e1) { 
     e1.printStackTrace(); 
    } catch (KeyStoreException e1) { 
     e1.printStackTrace(); 
    } 
1

は、私のデバイスのシステム時刻を過去に設定しました。一見明白なことを指摘してthis page

おかげで... :)