gpt4 book ai didi

Android Smack SSL/TLS 连接到具有 CA 证书的 XMPP Ejabberd 服务器

转载 作者:太空狗 更新时间:2023-10-29 16:31:19 26 4
gpt4 key购买 nike

我正在开发我的第一个 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 对于 keystore 创建和 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>">>

我无法理解这个收到的“<<22,3,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 步,我能够创建一个 keystore 文件并对其进行验证,但我使用了错误的证书文件,正确的是外部 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'/>

关于Android Smack SSL/TLS 连接到具有 CA 证书的 XMPP Ejabberd 服务器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37815995/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com