使用自签名证书的蚊子C客户

发布于 2025-01-30 14:13:06 字数 2467 浏览 0 评论 0原文

我已经为Mosquitto MQTT创建了一个自签名的证书,但是我无法使用我的小C代码应用程序进行连接。

它使用CMD行应用程序工作:

mosquitto_sub  -h myhostname -t "/MyTopic"  -p 8883 --cafile /etc/mosquitto/certs/my_cert.crt

它还使用Paho Lib从Python脚本起作用。

但是,如果我尝试以下C代码,我会在蚊子日志中获取此错误:

1652976798: OpenSSL Error[0]: error:14094412:SSL routines:ssl3_read_bytes:sslv3 alert bad certificate
1652976798: Socket error on client <unknown>, disconnecting.
1652976799: New connection from xxxxxxxx on port 8883.
1652976799: OpenSSL Error[0]: error:1409441A:SSL routines:ssl3_read_bytes:tlsv1 alert decode error
1652976799: Socket error on client <unknown>, disconnecting.
1652976799: New connection from xxxxxxxx on port 8883.
1652976799: OpenSSL Error[0]: error:140943F2:SSL routines:ssl3_read_bytes:sslv3 alert unexpected message
1652976799: Socket error on client <unknown>, disconnecting.
1652976800: New connection from xxxxxxxx on port 8883.
1652976800: Socket error on client <unknown>, disconnecting.
1652976800: New connection from xxxxxxxx on port 8883.
1652976801: Socket error on client <unknown>, disconnecting.
1652976801: New connection from xxxxxxxx on port 8883.
1652976801: OpenSSL Error[0]: error:140943F2:SSL routines:ssl3_read_bytes:sslv3 alert unexpected message
#include <stdio.h>
#include <mosquitto.h>


void on_connect1(struct mosquitto *mosq, void *obj, int result)
{
    int rc = MOSQ_ERR_SUCCESS;

    if(!result){
        mosquitto_subscribe(mosq, NULL, "/MyTopic", 0);
    }else{
        fprintf(stderr, "%s\n", mosquitto_connack_string(result));
    }
}

void on_message1(struct mosquitto *mosq, void *obj, const struct mosquitto_message *message)
{
    printf("I'm connected\n");

}

int main(int argc, char *argv[])
{
    struct mosquitto *mosq1, *mosq2;

    mosquitto_lib_init();

    mosq1 = mosquitto_new("My C Client", true, NULL);

    mosquitto_connect_callback_set(mosq1, on_connect1);
    mosquitto_message_callback_set(mosq1, on_message1);
    mosquitto_tls_set(mosq1, "/etc/mosquitto/certs/my_cert.crt", NULL, NULL, NULL, NULL);
    mosquitto_tls_opts_set(mosq1, 0, "tlsv1.2", NULL);
    mosquitto_connect(mosq1, "myhostname", 8883, 60);

    mosquitto_loop_start(mosq1);
    mosquitto_loop_forever(mosq1, -1, 1);

    mosquitto_destroy(mosq1);

    mosquitto_lib_cleanup();

    return 0;
}

尝试将Mosquitto_tls_opts_set设置为TLSV1 1.1等。

我 有提示吗?

I have created a self-signed certificate for mosquitto mqtt, but i cannot connect using my small C code app.

It works using the cmd line app:

mosquitto_sub  -h myhostname -t "/MyTopic"  -p 8883 --cafile /etc/mosquitto/certs/my_cert.crt

It also works from a python script using Paho lib.

But if i try with the below C code, i get this errors in the mosquitto logs:

1652976798: OpenSSL Error[0]: error:14094412:SSL routines:ssl3_read_bytes:sslv3 alert bad certificate
1652976798: Socket error on client <unknown>, disconnecting.
1652976799: New connection from xxxxxxxx on port 8883.
1652976799: OpenSSL Error[0]: error:1409441A:SSL routines:ssl3_read_bytes:tlsv1 alert decode error
1652976799: Socket error on client <unknown>, disconnecting.
1652976799: New connection from xxxxxxxx on port 8883.
1652976799: OpenSSL Error[0]: error:140943F2:SSL routines:ssl3_read_bytes:sslv3 alert unexpected message
1652976799: Socket error on client <unknown>, disconnecting.
1652976800: New connection from xxxxxxxx on port 8883.
1652976800: Socket error on client <unknown>, disconnecting.
1652976800: New connection from xxxxxxxx on port 8883.
1652976801: Socket error on client <unknown>, disconnecting.
1652976801: New connection from xxxxxxxx on port 8883.
1652976801: OpenSSL Error[0]: error:140943F2:SSL routines:ssl3_read_bytes:sslv3 alert unexpected message
#include <stdio.h>
#include <mosquitto.h>


void on_connect1(struct mosquitto *mosq, void *obj, int result)
{
    int rc = MOSQ_ERR_SUCCESS;

    if(!result){
        mosquitto_subscribe(mosq, NULL, "/MyTopic", 0);
    }else{
        fprintf(stderr, "%s\n", mosquitto_connack_string(result));
    }
}

void on_message1(struct mosquitto *mosq, void *obj, const struct mosquitto_message *message)
{
    printf("I'm connected\n");

}

int main(int argc, char *argv[])
{
    struct mosquitto *mosq1, *mosq2;

    mosquitto_lib_init();

    mosq1 = mosquitto_new("My C Client", true, NULL);

    mosquitto_connect_callback_set(mosq1, on_connect1);
    mosquitto_message_callback_set(mosq1, on_message1);
    mosquitto_tls_set(mosq1, "/etc/mosquitto/certs/my_cert.crt", NULL, NULL, NULL, NULL);
    mosquitto_tls_opts_set(mosq1, 0, "tlsv1.2", NULL);
    mosquitto_connect(mosq1, "myhostname", 8883, 60);

    mosquitto_loop_start(mosq1);
    mosquitto_loop_forever(mosq1, -1, 1);

    mosquitto_destroy(mosq1);

    mosquitto_lib_cleanup();

    return 0;
}

I've tried setting mosquitto_tls_opts_set to tlsv1 1.1 etc.

I'm using the hostname that i've used when i've generated the certs,
Any hints ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文