2016-09-14 13 views
0

私は、Smack 4.1.8ライブラリを使ってOpenfire 3.10.2サーバに接続しています。サーバーには自己署名証明書があります。 JVMが使用するキーストアにサーバーの証明書を追加すると、問題なく接続できます。しかし、私はむしろ私のアプリケーションでちょっとしたことを使うためにキーストアを持っています。smack setKeystorePathが機能しませんか?

私が使用しているコードは以下の通りです。私はsetKeystorePathがクラスパスパスで、ファイルシステムパスではないと思ういくつかの例を見てきました。私はそれを両方の方法で試しました。 keytoolコマンドを使用してタイプを使用してキーストアのタイプを確認しました。

誰かがこの実例を持っていますか?ここで

XMPPTCPConnectionConfiguration.Builder configBuilder = XMPPTCPConnectionConfiguration.builder(); 
    configBuilder.setUsernameAndPassword("bugs", "xxxxxx"); 
    final String l_jabber_hostname = "xxxxxxx"; 
    configBuilder.setServiceName(l_jabber_hostname); 
    configBuilder.setHost(l_jabber_hostname); 
    configBuilder.setPort(5222); 
    configBuilder.setSecurityMode(ConnectionConfiguration.SecurityMode.required); 
    configBuilder.setKeystorePath("/tmp/cacerts"); 
    configBuilder.setKeystoreType("JKS"); 
    configBuilder.setHostnameVerifier(new HostnameVerifier() { 
     @Override 
     public boolean verify(String a_hostname, SSLSession session) { 
     boolean l_approved = l_jabber_hostname.equalsIgnoreCase(a_hostname); 
     return l_approved; 
     } 
    }); 

    Roster.setRosterLoadedAtLoginDefault(false); 

    XMPPTCPConnectionConfiguration l_config = configBuilder.build(); 
    AbstractXMPPConnection connection = new XMPPTCPConnection(l_config); 

    connection.connect(); 

は誤りです:

<org.jivesoftware.smack.SmackException: 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>org.jivesoftware.smack.SmackException: 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 org.jivesoftware.smack.tcp.XMPPTCPConnection$PacketReader.parsePackets(XMPPTCPConnection.java:1029) 
     at org.jivesoftware.smack.tcp.XMPPTCPConnection$PacketReader.access$300(XMPPTCPConnection.java:956) 
     at org.jivesoftware.smack.tcp.XMPPTCPConnection$PacketReader$1.run(XMPPTCPConnection.java:971) 
     at java.lang.Thread.run(Thread.java:745) 
    Caused by: 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 org.jivesoftware.smack.tcp.XMPPTCPConnection.proceedTLSReceived(XMPPTCPConnection.java:769) 
     at org.jivesoftware.smack.tcp.XMPPTCPConnection.access$1000(XMPPTCPConnection.java:140) 
     at org.jivesoftware.smack.tcp.XMPPTCPConnection$PacketReader.parsePackets(XMPPTCPConnection.java:1022) 
     ... 3 more 
    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) 
     ... 13 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) 
     ... 19 more 

答えて

0

は同じ問題を抱えていました。解決策はSSLContextを作成することでした:

public void connect() throws SmackException, IOException, XMPPException, UnrecoverableKeyException, KeyManagementException, KeyStoreException, NoSuchAlgorithmException, CertificateException 
    { 

     XMPPTCPConnectionConfiguration config = XMPPTCPConnectionConfiguration.builder() 
      .setUsernameAndPassword(username, password) 
      .setServiceName(xmppdomain) 
      .setHost(host) 
      .setPort(port) 
      .setCustomSSLContext(getSSLContext()) 
      .build(); 

     xmppConnection = new XMPPTCPConnection(config); 
     xmppConnection.connect(); 

    } 

    private SSLContext getSSLContext() throws KeyStoreException, NoSuchAlgorithmException, CertificateException, IOException, UnrecoverableKeyException, KeyManagementException { 

     char[] JKS_PASSWORD = "password".toCharArray(); 
     char[] KEY_PASSWORD = "password".toCharArray(); 

     KeyStore keyStore = KeyStore.getInstance("JKS"); 
     InputStream is = new FileInputStream("<PATH_TO_JKS>"); 
     keyStore.load(is, JKS_PASSWORD); 
     KeyManagerFactory kmf = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm()); 
     kmf.init(keyStore, KEY_PASSWORD); 
     TrustManagerFactory tmf = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm()); 
     tmf.init(keyStore); 

     SSLContext sc = SSLContext.getInstance("TLS"); 
     sc.init(kmf.getKeyManagers(), tmf.getTrustManagers(), new java.security.SecureRandom()); 

     return sc; 


    } 
関連する問題