在mosquitto连接失败

时间:2016-01-08 08:29:44

标签: ssl mqtt mosquitto libmosquitto

我已使用以下命令生成证书并能够成功建立连接

mosquitto_sub -t "hello/world" -v --cafile ../certs/ca.crt --cert ../certs/client.crt --key ../certs/client.key

虽然我尝试使用相同的证书对我的代码执行相同操作,但我面临以下错误

客户端

LIBMOSQUITTO 1004005
8: Unable to connect: A TLS error occurred.
Success

服务器端

1452241406: New connection from 127.0.0.1 on port 1883.
1452241406: OpenSSL Error: error:140780E5:SSL routines:SSL23_READ:ssl handshake failure
1452241406: Socket error on client <unknown>, disconnecting.

这是我的代码

int main(){
printf("LIBMOSQUITTO %d\n", LIBMOSQUITTO_VERSION_NUMBER);

        if ((m = mosquitto_new("rtr", 1, NULL)) == NULL) {
                fprintf(stderr, "Out of memory.\n");
                exit(1);
        }

        int rc = mosquitto_tls_set(m,
                        "path/to/ca.crt",          /* cafile */
                        NULL,                   /* capath */
                        "/path/to/client.crt",             /* certfile */
                        "/path/to/client.key",             /* keyfile */
                        NULL                    /* pw_callback() */
                        );

        if (rc != MOSQ_ERR_SUCCESS) {
                fprintf(stderr, "Cannot set TLS CA: %s (check path names)\n",
                                mosquitto_strerror(rc));
                exit(3);
        }
#if 0
        mosquitto_tls_opts_set(m,
                        SSL_VERIFY_PEER,
                        NULL,                   /* tls_version: "tlsv1.2", "tlsv1" */
                        NULL                    /* ciphers */
                        );
        mosquitto_tls_insecure_set(m, 1);
#endif
        if ((rc = mosquitto_connect(m, "localhost", 1883, 20)) != MOSQ_ERR_SUCCESS) {
                fprintf(stderr, "%d: Unable to connect: %s\n", rc,
                                mosquitto_strerror(rc));
                perror("");
                exit(2);
        }
}

更新:还测试了端口8884

2 个答案:

答案 0 :(得分:2)

您没有致电mosquitto_lib_init()

答案 1 :(得分:0)

您的代码连接到端口1883,该端口通常不是TLS端口;取决于您在mosquitto.conf中配置的内容,我认为您需要端口8883,假设您有一个配置为8883的TLS侦听器。

我还指出您使用的/path/topath/to可能是也可能不是复制/粘贴错误。