Android Smack SSL/TLS 使用 CA 证书连接到 XMPP Ejabberd 服务器

Android Smack SSL/TLS connection to XMPP Ejabberd server with CA Certificate

我正在开发我的第一个 XMPP Android 应用程序,我在 XMPP 方面没有太多实践,但实际上我能够成功地将我的 Smack 客户端连接到我的 Ejabberd 服务器,问题出现在我尝试使用 TLS(使用 CA 证书)做同样的事情。

这里是ejabberd.yml关于TLS的配置:

hosts:
  - "localhost"
  - "mydomain.com"

listen:
  - 
    port: 5222
    module: ejabberd_c2s
    ##
    ## If TLS is compiled in and you installed a SSL
    ## certificate, specify the full path to the
    ## file and uncomment these lines:
    ##
    certfile: "/home/matt/ssl-cert/stunnel.pem"
    starttls: true

pem 文件必须有效,因为我将它用于 SSL WebSocket 连接没有问题。

这里是我的 XMPP Java class 中用来初始化 TLS 连接的方法:

private void initialiseConnection() {

    XMPPTCPConnectionConfiguration.Builder config = XMPPTCPConnectionConfiguration
            .builder();
    config.setSecurityMode(ConnectionConfiguration.SecurityMode.ifpossible);
    config.setServiceName(serverAddress); //mydomain.com
    config.setHost(serverAddress);
    config.setPort(5222);
    config.setDebuggerEnabled(true);

    SSLContext sslContext = null;

    try {
        sslContext = createSSLContext(context);
    } catch (KeyStoreException e) {
        e.printStackTrace();
    } catch (NoSuchAlgorithmException e) {
        e.printStackTrace();
    } catch (KeyManagementException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } catch (CertificateException e) {
        e.printStackTrace();
    }

    config.setCustomSSLContext(sslContext);
    config.setSocketFactory(sslContext.getSocketFactory());

    XMPPTCPConnection.setUseStreamManagementResumptiodDefault(true);
    XMPPTCPConnection.setUseStreamManagementDefault(true);

    connection = new XMPPTCPConnection(config.build());
    XMPPConnectionListener connectionListener = new XMPPConnectionListener();
    connection.addConnectionListener(connectionListener);
}

private SSLContext createSSLContext(Context context) throws KeyStoreException,
        NoSuchAlgorithmException, KeyManagementException, IOException, CertificateException {

    KeyStore trustStore;
    InputStream in = null;
    trustStore = KeyStore.getInstance("BKS");

    in = context.getResources().openRawResource(R.raw.my_keystore);

    trustStore.load(in, "MyPassword123".toCharArray());

    TrustManagerFactory trustManagerFactory = TrustManagerFactory
            .getInstance(KeyManagerFactory.getDefaultAlgorithm());
    trustManagerFactory.init(trustStore);
    SSLContext sslContext = SSLContext.getInstance("TLS");
    sslContext.init(null, trustManagerFactory.getTrustManagers(), new SecureRandom());
    return sslContext;
}

请注意,没有 SSL/TLS 部分(并且没有 Ejabberd 配置中的 SSL/TLS 部分)一切正常。

p.s 对于密钥库的创建和 SSL 方法的集成,我遵循了 page.

中的 lqbal 教程

现在,Android 监控日志 (Android Studio) 只给我一行有关连接问题的信息。

E/(onCreate): IOException: Handshake failed

仅此而已,但在 Ejabberd 服务器日志上我有以下行:

2016-06-14 15:57:26.461 [info] <0.14993.0>@ejabberd_listener:accept:333 (#Port<0.73878>) Accepted connection xx.xx.xx.xx:xxxxx -> xx.xxx.xx.xx:5222
2016-06-14 15:57:26.466 [debug] <0.15099.0>@ejabberd_receiver:process_data:284 Received XML on stream = <<22,3,1,0,133,1,0,0,129,3,3,159,29,211,249,221,88,135,177,183,150,98,234,76,6,91,52,30,26,186,202,176,199,127,245,56,211,198,43,66,35,237,140,0,0,40,192,43,192,44,192,47,192,48,0,158,0,159,192,9,192,10,192,19,192,20,0,51,0,57,192,7,192,17,0,156,0,157,0,47,0,53,0,5,0,255,1,0,0,48,0,23,0,0,0,13,0,22,0,20,6,1,6,3,5,1,5,3,4,1,4,3,3,1,3,3,2,1,2,3,0,11,0,2,1,0,0,10,0,8,0,6,0,23,0,24,0,25>>
2016-06-14 15:57:26.466 [debug] <0.15100.0>@ejabberd_c2s:send_text:1832 Send XML on stream = <<"<?xml version='1.0'?><stream:stream xmlns='jabber:client' xmlns:stream='http://etherx.jabber.org/streams' id='17298480576042278904' from='mydomain.com' version='1.0'>">>
2016-06-14 15:57:26.466 [debug] <0.15100.0>@ejabberd_c2s:send_text:1832 Send XML on stream = <<"<stream:error><xml-not-well-formed xmlns='urn:ietf:params:xml:ns:xmpp-streams'></xml-not-well-formed></stream:error>">>
2016-06-14 15:57:26.466 [debug] <0.15100.0>@ejabberd_c2s:send_text:1832 Send XML on stream = <<"</stream:stream>">>

我无法理解收到的“<<22,3,1...”标签 (???)

怎么了?

<<22,3,1...数据包是一个TLS握手数据包。它实际上不是 XML;它以十进制形式打印为单个字节。 22字节表示"handshake",3和1分别是主版本号和次版本号。版本“3.1”实际上代表 TLS 1.0。有关详细信息,请参阅 the description on Wikipedia

似乎发生的是您的 Java 代码在连接后立即启动 TLS 握手,但 ejabberd 期望它首先协商 STARTTLS。 section 5 of RFC 6120 中对此进行了描述。基本上,服务器发送一个功能列表,包括 STARTTLS:

<stream:features>
   <starttls xmlns='urn:ietf:params:xml:ns:xmpp-tls'>
     <required/>
   </starttls>
</stream:features>

客户端请求 STARTTLS:

<starttls xmlns='urn:ietf:params:xml:ns:xmpp-tls'/>

然后服务器告诉客户端继续:

<proceed xmlns='urn:ietf:params:xml:ns:xmpp-tls'/>

然后然后客户端可以开始TLS握手。


一定有办法让 Smack 进行上面的 STARTTLS 协商,但我不知道怎么做。但是,您可以通过更改配置让 ejabberd 接受这种 TLS 握手方式:

port: 5223
starttls: false

传统上,XMPP 服务器接受在端口 5222 上执行 STARTTLS 的 "normal" 连接,在端口 5223 上接受 "instant-TLS" 连接,因此我建议遵循该约定以减少混淆。

不设置套接字工厂。

好的,经过一些研究并感谢之前的回答,我终于能够将我的 Smack 客户端连接到我的 Ejabberd 服务器。下面是所有编辑。

这是 XMPP class 最终代码,我删除了 config.setSocketFactory 行并将连接端口更改为 5223。

private void initialiseConnection() {

    XMPPTCPConnectionConfiguration.Builder config = XMPPTCPConnectionConfiguration
            .builder();
    config.setSecurityMode(ConnectionConfiguration.SecurityMode.ifpossible);
    config.setServiceName(serverAddress);
    config.setHost(serverAddress);
    config.setPort(5223);
    config.setDebuggerEnabled(true);

    SSLContext sslContext = null;

    try {
        sslContext = createSSLContext(context);
    } catch (KeyStoreException e) {
        e.printStackTrace();
    } catch (NoSuchAlgorithmException e) {
        e.printStackTrace();
    } catch (KeyManagementException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } catch (CertificateException e) {
        e.printStackTrace();
    }

    config.setCustomSSLContext(sslContext);

    XMPPTCPConnection.setUseStreamManagementResumptiodDefault(true);
    XMPPTCPConnection.setUseStreamManagementDefault(true);

    connection = new XMPPTCPConnection(config.build());
    XMPPConnectionListener connectionListener = new XMPPConnectionListener();
    connection.addConnectionListener(connectionListener);
}

private SSLContext createSSLContext(Context context) throws KeyStoreException,
        NoSuchAlgorithmException, KeyManagementException, IOException, CertificateException {

    KeyStore trustStore;
    InputStream in = null;
    trustStore = KeyStore.getInstance("BKS");

    in = context.getResources().openRawResource(R.raw.my_keystore);

    trustStore.load(in, "MyPassword123".toCharArray());

    TrustManagerFactory trustManagerFactory = TrustManagerFactory
            .getInstance(KeyManagerFactory.getDefaultAlgorithm());
    trustManagerFactory.init(trustStore);
    SSLContext sslContext = SSLContext.getInstance("TLS");
    sslContext.init(null, trustManagerFactory.getTrustManagers(), new SecureRandom());
    return sslContext;
}

这些是 ejabberd.yml 文件中的新端口设置

port: 5223
module: ejabberd_c2s
certfile: "/etc/ejabberd/ejabberd.pem"
starttls: true

这里的证书,我在客户端和服务器上都错了,对于客户端部分,我已经按照 this page 上的 lqbal 教程,通过第 2 步和第 3 步我能够创建一个密钥库文件并验证它,但我使用了错误的证书文件,正确的是外部 CA 根证书("AddTrustExternalCARoot.crt" COMODO 在我的例子中)。

对于服务器,我使用了一个内部证书位置错误的 pem 链,ejabberd.pem 的正确方法如下(您可以找到所有详细信息 here) : 1. 私钥 (.key) 2. 证书 (domain.crt) 3. 链 (.ca-bundle)。最后,我将 ejabberd.pem 移到了 /etc/ejabberd 文件夹中。

现在 TLS 连接正常:

SMACK: SENT (0): <starttls xmlns='urn:ietf:params:xml:ns:xmpp-tls'></starttls>
SMACK: RECV (0): <proceed xmlns='urn:ietf:params:xml:ns:xmpp-tls'/>