简体   繁体   中英

tls connection failure in mosquitto

I've generated certificates and able to make a successful connection using below command

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

While I'm try to do the same with my code using same certificates, I'm facing following errors

CLIENT SIDE

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

SERVER SIDE

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.

Here is my code

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);
        }
}

UPDATE : Also tested for port 8884

您尚未调用mosquitto_lib_init()

Your code is connecting to port 1883 which is typically not the TLS port; depending on what you've configured in mosquitto.conf I think you'll want port 8883, assuming you have a TLS listener configured at 8883.

I also point out that you use /path/to and path/to which may or may not be a copy/paste typo.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM