2012-04-05 9 views
1

sslをサポートするApache tomcat 5をセットアップしました。自己署名証明書を作成し、クライアント証明書をサーバーのtrusstoreにインポートし、p12ファイルをブラウザーにインポートし、https上のページにアクセスすることが可能です。どのようにjavaを使用して同じを達成するには?続き自己署名入りのサーバーとクライアント証明書でhttps getメソッドを呼び出す方法

は私がしようとするが、任意の成功なしていたコードです...

//reference : http://vafer.org/blog/20061010073725/ http://www.mkyong.com/java/java-//https-client-httpsurlconnection-example/ 

import java.io.BufferedReader; 
import java.io.File; 
import java.io.FileInputStream; 
import java.io.IOException; 
import java.io.InputStream; 
import java.io.InputStreamReader; 
import java.net.URL; 
import java.security.KeyManagementException; 
import java.security.KeyStore; 
import java.security.KeyStoreException; 
import java.security.NoSuchAlgorithmException; 
import java.security.SecureRandom; 
import java.security.UnrecoverableKeyException; 
import java.security.cert.CertificateException; 

import javax.net.ssl.HttpsURLConnection; 
import javax.net.ssl.KeyManagerFactory; 
import javax.net.ssl.SSLContext; 
import javax.net.ssl.SSLPeerUnverifiedException; 
import java.security.cert.Certificate; 






public class HttpClientTutorial { 

    @SuppressWarnings("unused") 
    private static javax.net.ssl.SSLSocketFactory getFactory(File pKeyFile, String pKeyPassword) throws NoSuchAlgorithmException, KeyStoreException, CertificateException, IOException, UnrecoverableKeyException, KeyManagementException 
    { 
      KeyManagerFactory keyManagerFactory = KeyManagerFactory.getInstance("SunX509"); 
      KeyStore keyStore = KeyStore.getInstance("PKCS12"); 

      InputStream keyInput = new FileInputStream(pKeyFile); 
      keyStore.load(keyInput, pKeyPassword.toCharArray()); 
      keyInput.close(); 

      keyManagerFactory.init(keyStore, pKeyPassword.toCharArray()); 

      SSLContext context = SSLContext.getInstance("TLS"); 
      context.init(keyManagerFactory.getKeyManagers(), null, new SecureRandom()); 

      return context.getSocketFactory(); 
    } 

     private static void print_https_cert(HttpsURLConnection con){ 

      if(con!=null){ 

       try { 

      System.out.println("Response Code : " + con.getResponseCode()); 
      System.out.println("Cipher Suite : " + con.getCipherSuite()); 
      System.out.println("\n"); 

      Certificate[] certs = con.getServerCertificates(); 
      for(Certificate cert : certs){ 
       System.out.println("Cert Type : " + cert.getType()); 
       System.out.println("Cert Hash Code : " + cert.hashCode()); 
       System.out.println("Cert Public Key Algorithm : " + cert.getPublicKey().getAlgorithm()); 
       System.out.println("Cert Public Key Format : " + cert.getPublicKey().getFormat()); 
       System.out.println("\n"); 
      } 

      } catch (SSLPeerUnverifiedException e) { 
       e.printStackTrace(); 
      } catch (IOException e){ 
       e.printStackTrace(); 
      } 

      } 

      } 

      private static void print_content(HttpsURLConnection con){ 
      if(con!=null){ 

      try { 

       System.out.println("****** Content of the URL ********");    
       BufferedReader br = 
       new BufferedReader(
        new InputStreamReader(con.getInputStream())); 

       String input; 

       while ((input = br.readLine()) != null){ 
        System.out.println(input); 
       } 
       br.close(); 

      } catch (IOException e) { 
       e.printStackTrace(); 
      } 

       } 

      } 


    public static void main(String[] args) throws IOException, UnrecoverableKeyException, KeyManagementException, NoSuchAlgorithmException, KeyStoreException, CertificateException { 
     URL url = new URL("https://localhost:8443/SpringSec2"); 
     HttpsURLConnection con = (HttpsURLConnection) url.openConnection(); 
     con.setSSLSocketFactory(getFactory(new File("src/Client.p12"), "client")); 

      //dumpl all cert info 
     print_https_cert(con); 

     //dump all the content 
     print_content(con); 


    } 



} 


*************************************************************************************** 

例外:

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 com.sun.net.ssl.internal.ssl.Alerts.getSSLException(Unknown Source) 
at com.sun.net.ssl.internal.ssl.SSLSocketImpl.fatal(Unknown Source) 
at com.sun.net.ssl.internal.ssl.Handshaker.fatalSE(Unknown Source) 
at com.sun.net.ssl.internal.ssl.Handshaker.fatalSE(Unknown Source) 
at com.sun.net.ssl.internal.ssl.ClientHandshaker.serverCertificate(Unknown Source) 
at com.sun.net.ssl.internal.ssl.ClientHandshaker.processMessage(Unknown Source) 
at com.sun.net.ssl.internal.ssl.Handshaker.processLoop(Unknown Source) 
at com.sun.net.ssl.internal.ssl.Handshaker.process_record(Unknown Source) 
at com.sun.net.ssl.internal.ssl.SSLSocketImpl.readRecord(Unknown Source) 
at com.sun.net.ssl.internal.ssl.SSLSocketImpl.performInitialHandshake(Unknown Source) 
at com.sun.net.ssl.internal.ssl.SSLSocketImpl.startHandshake(Unknown Source) 
at com.sun.net.ssl.internal.ssl.SSLSocketImpl.startHandshake(Unknown Source) 
at sun.net.www.protocol.https.HttpsClient.afterConnect(Unknown Source) 
at sun.net.www.protocol.https.AbstractDelegateHttpsURLConnection.connect(Unknown Source) 
at sun.net.www.protocol.http.HttpURLConnection.getInputStream(Unknown Source) 
at java.net.HttpURLConnection.getResponseCode(Unknown Source) 
at sun.net.www.protocol.https.HttpsURLConnectionImpl.getResponseCode(Unknown Source) 
+0

取得しているエラーは何ですか? – momo

+0

上記の例外が追加されましたが、私はクライアント側でサーバー証明書の世話をしていないと感じていますが、それを達成する方法がわかりません.... – Nohsib

答えて

1

あなたは、自己署名証明書をやっているので、あなたはあなた自身を提供する必要がありますTrustManager。あなたのコード内の行

SSLContext context = SSLContext.getInstance("TLS"); context.init(keyManagerFactory.getKeyManagers(), null, new SecureRandom());

context.initの2番目のパラメータは、あなたが信頼できるサーバーを管理するためのTrustManagerです。基本的には独自の内線番号X509TrustManagerを作成する必要があります。そのコードの例はhttp://www.howardism.org/Technical/Java/SelfSignedCerts.htmlにあります。 NaiveTrustManagerを検索すると、checkServerTrusted()が実装されていないことがわかります。これはすべてを信頼することを意味しています。最初にそれを試して、それが動作するかどうかを確認してくださいその後、より強力なチェックを実装することを検討することをお勧めします。

関連する問題