我正在开发我的第一个XMPP Android应用程序,我在XMPP中没有很多练习,但实际上我能够成功地将我的Smack客户端连接到我的Ejabberd服务器,当我尝试做同样的事情时出现问题使用TLS(带CA证书)。
这里有关于TLS的 ejabberd.yml 配置:
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类中用于初始化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 Monitor日志(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>">>
我无法理解这收到了“&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt; /&#22,3,...”标签(???)
怎么了?
答案 0 :(得分:2)
<<22,3,1...
数据包是TLS握手数据包。它实际上不是XML;它以十进制形式打印为单个字节。 22字节表示“握手”,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的“正常”连接,以及在端口5223上执行“instant-TLS”连接,因此我建议遵循该约定以减少混淆。
答案 1 :(得分:2)
不要设置套接字工厂。
答案 2 :(得分:1)
好的,最后经过一些研究并感谢之前的答案,我能够将我的Smack客户端连接到我的Ejabberd服务器。以下是所有编辑内容。
这是XMPP类的最终代码,我删除了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根证书(&#34; AddTrustExternalCARoot.crt&#34;在我的情况下是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'/>