<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    kooyee ‘s blog

    開源軟件, 眾人努力的結(jié)晶, 全人類的共同財(cái)富
    posts - 103, comments - 55, trackbacks - 0, articles - 66
       :: 首頁(yè) :: 新隨筆 :: 聯(lián)系 :: 聚合  :: 管理
    概念 JAVA使用keystore文件來存儲(chǔ)所有KEY,keystore文件可以存放多個(gè)KEY,訪問它需要密碼。下面我介紹下如何將用OpenSSL做自簽名的證書一文中介紹的OpenSSL產(chǎn)生的KEY與JAVA的KEY轉(zhuǎn)換后使用,從而達(dá)到JAVA與OpenSSL通信的目的。
    文章來源:http://blog.csdn.net/ky5566/archive/2007/05/10/1603531.aspx



    cnf -infiles clientapp.crs
     

    轉(zhuǎn)換PEM到DER格式

    openssl x509 -in clientapp.pem -out clientapp.der -outform DER


    導(dǎo)入CA證書,即P1

    keytool -keystore mycerts -alias systemca -import -file cacert.pem


    導(dǎo)入用戶證書,即被V1加密過的P2

    keytool -keystore mycerts -alias clientapp -import -file clientapp.der

    注意:這里一定要先導(dǎo)入CA證書再導(dǎo)入用戶證書,否則會(huì)報(bào)錯(cuò)。

    現(xiàn)在我們就生成了JAVA服務(wù)器使用的所有KEY了,在程序中將mycerts這個(gè)keystore導(dǎo)入就可以了。
    如果客戶端是使用OpenSSL的程序,那么用CA證書cacert.pem就能正常通信了,如果也是JAVA程序,那么我們需要將CA證書也轉(zhuǎn)換成keystore:

    keytool -import -keystore clikeystore -import -trustcacerts -file cacert.pem

    生成的clikeystore供JAVA客戶端使用,就能通信。


    再附上SVR和CLI的JAVA程序,我已經(jīng)用上面的KEY都測(cè)試通過:
    SVR端:
    import  java.io. * ;
    import  java.net. * ;
    import  com.sun.net.ssl.KeyManagerFactory;
    import  com.sun.net.ssl.KeyManager;
    import  com.sun.net.ssl.TrustManagerFactory;
    import  com.sun.net.ssl.TrustManager;
    import  com.sun.net.ssl.SSLContext;
    import  javax.net.ServerSocketFactory;
    import  java.security.KeyStore;

    public   class  svr  implements  Runnable {
      
      
    public   static   final   int  PORT  =   5555 ;
      
    public   static   final  String HOST  =   " localhost " ;
      
    public   static   final  String QUESTION  =   " Knock, knock. " ;
      
    public   static   final  String ANSWER  =   " Who's there? " ;

      
    //  The new constants that are used during setup.
       public   static   final  String KEYSTORE_FILE  =   " mycerts " ; // "server_keystore";
       public   static   final  String ALGORITHM  =   " sunx509 " ;
      
    public   static   final  String PASSWORD  =   " churchillobjects " ;
      
      
    public   static   void  main(String[] args) {
        
    new  Thread( new  svr()).start();
      }

      
      
    public   void  run() {
        ServerSocket ss 
    =   null ;
        
    try   {

          
    //  Local references used for clarity. Their presence
          
    //  here is part of the reason we need to import
          
    //  so many classes.
          KeyManagerFactory kmf;
          KeyManager[] km;
          KeyStore ks;
          TrustManagerFactory tmf;
          TrustManager[] tm;
          SSLContext sslc;
          
          
    //  Create a keystore that will read the JKS (Java KeyStore)
          
    //  file format which was created by the keytool utility.
          ks  =  KeyStore.getInstance( " JKS " );
          
          
    //  Load the keystore object with the binary keystore file and
          
    //  a byte array representing its password.
          ks.load( new  FileInputStream(KEYSTORE_FILE), PASSWORD.toCharArray());
          
          
    //  Gives us a factory for key managers that will let
          
    //  us handle the asymetric keys we created earlier.
          kmf  =  KeyManagerFactory.getInstance(ALGORITHM);

          
    //  Initialize the key manager factory with the keystore object,
          
    //  again using the same password for security since it is going to
          
    //  access the private key.
          kmf.init(ks, PASSWORD.toCharArray());
          
          
    //  Now we can get the key managers from the factory, since it knows
          
    //  what type we are using now.
          km  =  kmf.getKeyManagers();
          
          
    //  Next, create a trust manager factory using the same algorithm.
          
    //  This is to avoid using the certificates in cacerts that
          
    //  represent an authentication security risk.
          tmf  =  TrustManagerFactory.getInstance(ALGORITHM);
          
          
    //  then initialize it with the keystore object. This time we don't
          
    //  need the keystore password. This is because trusted certificates
          
    //  are not a sensitive element in the keystore, unlike the
          
    //  private keys.
          tmf.init(ks);
          
          
    //  Once that's initialized, get the trust managers from the factory.
          tm  =  tmf.getTrustManagers();
          
          
    //  Almost done, we need a context object that will get our
          
    //  server socket factory. We specify TLS to indicate that we will
          
    //  need a server socket factory that supports SSL.
          sslc  =  SSLContext.getInstance( " TLS " );
          
          
    //  Initialize the context object with the key managers and trust
          
    //  managers we got earlier. The third parameter is an optional
          
    //  SecureRandom object. By passing in null, we are letting the
          
    //  context object create its own.
          sslc.init(km, tm,  null );
          
          
    //  Finally, we get the ordinary-looking server socket factory
          
    //  from the context object.
          ServerSocketFactory ssf  =  sslc.getServerSocketFactory();
          
          
    //  From the factory, we simply ask for an ordinary-looking
          
    //  server socket on the port we wish.
          ss  =  ssf.createServerSocket(PORT);

          listen(ss);
        }

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

        
    finally {
          
    if (ss != null ) {
            
    try {
              ss.close();
            }

            
    catch (IOException e) {
              
    //  oh, well
            }

          }

          System.exit(
    0 );
        }

      }

      
      
    static   void  listen(ServerSocket ss)  throws  Exception {
        System.out.println(
    " Ready for connections. " );
        
    while ( true ) {
          Socket s 
    =  ss.accept();
          BufferedWriter bw 
    =   new  BufferedWriter(
            
    new  OutputStreamWriter(s.getOutputStream()));
          BufferedReader br 
    =   new  BufferedReader(
            
    new  InputStreamReader(s.getInputStream()));
          String q 
    =  br.readLine();
          
    if ( ! QUESTION.equals(q)) {
            
    throw   new  RuntimeException( " Wrong question: \ ""  + q +  " \ "" );
          }

          System.out.println(
    " Question: \ ""  + q +  " \ "" );
          bw.write(ANSWER
    + " \n " );
          bw.flush();
          s.close();
        }

      }

    }


    CLI端程序:

    import  java.io. * ;
    import  java.net. * ;
    import  com.sun.net.ssl.KeyManagerFactory;
    import  com.sun.net.ssl.TrustManagerFactory;
    import  com.sun.net.ssl.SSLContext;
    import  java.security.KeyStore;
    import  javax.net.SocketFactory;

    public   class  cli  implements  Runnable {
      
      
    public   static   final   int  PORT  =   5555 ;
      
    public   static   final  String HOST  =   " localhost " ;
      
    public   static   final  String KEYSTORE_FILE  =   " clikeystore " ; // "client_keystore";
       public   static   final  String ALGORITHM  =   " sunx509 " ;
      
    public   static   final  String PASSWORD  =   " churchillobjects " ;
      
    public   static   final  String QUESTION  =   " Knock, knock. " ;
      
    public   static   final  String ANSWER  =   " Who's there? " ;
      
      
    public   static   void  main(String[] args) {
        
    new  Thread( new  cli()).start();
      }

      
      
    public   void  run() {
        Socket socket 
    =   null ;
        
    try {
          KeyManagerFactory kmf;
          KeyStore ks;
          TrustManagerFactory tmf;
          SSLContext sslc;

          kmf 
    =  KeyManagerFactory.getInstance(ALGORITHM);
          ks 
    =  KeyStore.getInstance(  " JKS "  );
          ks.load(
    new  FileInputStream(KEYSTORE_FILE), PASSWORD.toCharArray());
          kmf.init(ks, PASSWORD.toCharArray());
          tmf 
    =  TrustManagerFactory.getInstance(ALGORITHM);
          tmf.init(ks);
          sslc 
    =  SSLContext.getInstance( " TLS " );
          sslc.init(kmf.getKeyManagers(), tmf.getTrustManagers(), 
    null );

          
    //  The process is different from here on the client. Instead of
          
    //  getting a ServerSocketFactory, we ask for a SocketFactory from
          
    //  the SSL context.
          SocketFactory sf  =  sslc.getSocketFactory();

          
    //  Then we get the socket from the factory and treat it
          
    //  as if it were a standard (plain) socket.
          socket  =  sf.createSocket(HOST, PORT);
        
          doQuery(socket);
        }

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

        
    finally {
          
    if (socket != null ) {
            
    try {
              socket.close();
            }

            
    catch (IOException e) {
              
    //  oh, well
            }

          }

          System.exit(
    0 );
        }

      }


      
    private   void  doQuery(Socket s)  throws  Exception {
        BufferedWriter bw 
    =   new  BufferedWriter( new  OutputStreamWriter(s.getOutputStream()));
        BufferedReader br 
    =   new  BufferedReader( new  InputStreamReader(s.getInputStream()));
        bw.write(QUESTION
    + " \n " );
        bw.flush();
        String response 
    =  br.readLine();
        
    if ( ! ANSWER.equals(response)) {
          
    throw   new  RuntimeException( " Wrong answer: \ ""  + response +  " \ "" );
        }

        System.out.println(
    " Got the right answer: \ ""  + response +  " \ "" );
      }

    }


    評(píng)論

    # re: [導(dǎo)入]用OpenSSL與JAVA(JSSE)通信   回復(fù)  更多評(píng)論   

    2008-05-27 16:59 by kooyee
    如果客戶端不使用keystore的話
    可以用:

    System.setProperty("javax.net.ssl.trustStore","trustedcerts");

    SSLSocketFactory factory = (SSLSocketFactory) SSLSocketFactory
    .getDefault();
    SSLSocket client = (SSLSocket) factory.createSocket("127.0.0.1",8080);

    這樣就可以無(wú)條件的trust服務(wù)端的keystore
    (注 這里的trustedcerts是服務(wù)端keystore文件名)

    如果不用System.setProperty的話,也可以在啟動(dòng)客戶端時(shí)加入?yún)?shù)(與setProperty效果一樣)
    java -Djavax.net.ssl.trustStore=truststore clientApp

    只有注冊(cè)用戶登錄后才能發(fā)表評(píng)論。


    網(wǎng)站導(dǎo)航:
     
    主站蜘蛛池模板: 全黄性性激高免费视频| 亚洲综合日韩久久成人AV| 久久久亚洲欧洲日产国码农村| 无码毛片一区二区三区视频免费播放 | 日本系列1页亚洲系列| 天天天欲色欲色WWW免费| 久久亚洲最大成人网4438| 国产成人免费爽爽爽视频| 亚洲毛片基地4455ww| 免费观看美女裸体网站| 亚洲天堂在线视频| 国产97视频人人做人人爱免费| 国产成在线观看免费视频| 77777午夜亚洲| 成人免费视频观看无遮挡| 亚洲精品国产suv一区88| 四虎影视在线永久免费观看| 国产综合激情在线亚洲第一页| 亚洲AV无码一区二区三区在线观看 | 免费国产黄网站在线观看| 亚洲中文字幕久久精品无码2021| 久久久久久国产a免费观看不卡| 黄页免费的网站勿入免费直接进入| 久久久久se色偷偷亚洲精品av | 国产乱子伦精品免费女| 色吊丝性永久免费看码| 国产偷v国产偷v亚洲高清| 67194成手机免费观看| 亚洲乱色伦图片区小说| 亚洲国产成人精品久久久国产成人一区二区三区综 | 亚洲人成电影网站色www| 亚洲国产精品碰碰| 无码日韩精品一区二区免费暖暖 | 中文字幕免费人成乱码中国| 亚洲视频精品在线| 免费成人在线电影| 亚洲人成未满十八禁网站| 亚洲一级片免费看| 黄页网站在线观看免费高清| 污污污视频在线免费观看| 亚洲电影唐人社一区二区|