Mosquitto C client using Self Signed Certificate

Viewed 90

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 ?

0 Answers
Related