【问题标题】:SMACK 4.2.4 connection with ejabberd through TLS通过 TLS 与 ejabberd 的 SMACK 4.2.4 连接
【发布时间】:2018-07-03 21:13:23
【问题描述】:

我一直在使用 ejabberd 和 smack 4.2.4。在我从 LetsEncrypt 实现 TLS 证书之前,一切都很好。现在它给出了 SSL 握手错误。

如果我在连接配置中启用 TLS,同样的安全连接适用于 iOS 和其他客户端。

我进行了搜索,但可以找到任何解决此问题的方法。请帮忙连接安卓。

谢谢,

【问题讨论】:

    标签: android ejabberd smack


    【解决方案1】:

    我花了一些时间,终于找到了适合我的解决方案。

    configBuilder.setSecurityMode(ConnectionConfiguration.SecurityMode.required);
    SSLContext sslContext = getSSLContext(context);
    configBuilder.setCustomSSLContext(sslContext);
    
    
    public SSLContext getSSLContext(Context context ) throws IOException, CertificateException, NoSuchAlgorithmException, KeyStoreException, KeyManagementException {
            CertificateFactory cf = null;
    
            try {
                cf = CertificateFactory.getInstance("X.509");
            } catch (CertificateException e) {
                Log.e(TAG, e.getMessage());
            }
    
            InputStream in = context.getResources().openRawResource(R.raw.chain); // R.raw.chain is CA Root Certificate added in RAW resources folder
    
            InputStream caInput = new BufferedInputStream(in);
            Certificate ca = null;
            try {
                ca = cf.generateCertificate(caInput);
                Log.d(TAG, "ca=" + ((X509Certificate) ca).getSubjectDN());
            }
            catch (Exception e){
                Log.e(TAG, e.getMessage());
            }
            finally {
                caInput.close();
            }
    
            // Create a KeyStore containing our trusted CAs
            String keyStoreType = KeyStore.getDefaultType();
            KeyStore keyStore = KeyStore.getInstance(keyStoreType);
            keyStore.load(null, null);
            keyStore.setCertificateEntry("ca", ca);
    
            // Create a TrustManager that trusts the CAs in our KeyStore
            String tmfAlgorithm = TrustManagerFactory.getDefaultAlgorithm();
            TrustManagerFactory tmf = TrustManagerFactory.getInstance(tmfAlgorithm);
            tmf.init(keyStore);
    
            // Create an SSLContext that uses our TrustManager
            SSLContext sslContext = SSLContext.getInstance("TLS");
            sslContext.init(null, tmf.getTrustManagers(), null);
            return sslContext;
    
        }
    

    【讨论】:

    • 我们从哪里得到“链”文件?
    猜你喜欢
    • 2017-06-02
    • 2020-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-07-22
    • 2017-11-25
    • 2016-10-15
    • 2015-06-01
    • 1970-01-01
    相关资源
    最近更新 更多