I am trying to set up a client utilizing libmosquitto. Connecting to the broker works like a charm, with and without TLS. But whenever I try to send data over the TLS connection, I get the error:
Blockquote 1486712210: OpenSSL Error: error:14094438:SSL routines:ssl3_read_bytes:tlsv1 alert internal error 1486712210: OpenSSL Error: error:140940E5:SSL routines:ssl3_read_bytes:ssl handshake failure 1486712210: Socket error on client , disconnecting. Blockquote
I've tried the test client from the git repository of mosquitto, mosquitto/test/lib/c/08-ssl-connect-cert-auth-enc.c , same result.
My current server configuration:
listener 1883
listener 8883
cafile /etc/mosquitto/ca_certificates/ca.crt
certfile /etc/mosquitto/certs/werkstatt.logicway.net.crt
keyfile /etc/mosquitto/certs/werkstatt.logicway.net.key
require_certificate true
tls_version tlsv1
The source code:
#include <stdio.h>
#include <string.h>
#include <sstream>
#include <iostream>
#include <QtCore/QCoreApplication>
#include <QtDBus/QtDBus>
#include <mosquitto.h>
#include "logicgateway_data.h"
#include "logicgateway_client.h"
using namespace std;
const char *data_out;
struct mosquitto *mosq = NULL;
// BEschreibung kommt
int LGW_Client::receive_data(QString in_basket)
{
data_out = in_basket.QString::toLatin1();
cout << "Wert erhalten: " << data_out << endl;
mosquitto_publish(mosq,0,LGW_TOPIC,strlen(data_out),data_out,0,true);
return 0;
}
int main (int argc, char **argv)
{
// Verbindung mit der Qt-Dbus Session erstellen
QCoreApplication app(argc,argv);
if(!QDBusConnection::sessionBus().isConnected()){
fprintf(stderr,"Kann nicht mit D-Bus Session verbinden.\n"
"Um sie zu starten, geben Sie bitte ein:\n"
"\teval `dbus-launch --auto-syntax`\n");
return 1;
}
// Mosquitto initialisieren
mosquitto_lib_init();
mosq = mosquitto_new(CID,false,NULL);
if(!mosq)
{
printf("Nicht erstellt\n");
return 1;
}
else
{
printf("Mosquitto erfolgreich verbunden!\n");
}
// TLS Anbindung
if (LGW_PORT == 8883)
{
mosquitto_tls_opts_set(mosq,1,"tlsv1",NULL);
mosquitto_tls_set(mosq,"ca.crt",NULL,"client.crt","client.key", NULL);
}
// Mosquitto Verbindung zum Broker erstellen
if(mosquitto_connect(mosq,BROKER_ADRESS,LGW_PORT,60))
{
fprintf(stderr, "Fehler!\n");
return 1;
}
else
{
printf("Laeuft!\n");
}
// DBus Service registrieren um Daten zu empfangen
if(!QDBusConnection::sessionBus().registerService(SERVICE_NAME_CLIENT)) {
fprintf(stderr, "%s\n", qPrintable(QDBusConnection::sessionBus().lastError().message()));
exit(1);
}
printf ("LGW-Client: Gestartet...\n");
LGW_Client lgwclient;
QDBusConnection::sessionBus().registerObject("/",&lgwclient, QDBusConnection::ExportAllSlots);
app.exec();
// Fehlerbehandlung und aufräumen
//fprintf(stderr, "%s\n", qPrintable(QDBusConnection::sessionBus().lastError().message()));
mosquitto_loop_forever(mosq, -1, 1); //Bin mir nicht sicher, ob ich das hier wegen der While-Schleife brauche
mosquitto_destroy(mosq);
mosquitto_lib_cleanup();
return 1;
}
(Please be gentle regarding the code in general. I am a bloody beginner and fully aware, that there is much to improve. But at the moment i just want to get the TLS connection working)
Things that work:
- Running the code with port 1883 (without TLS)
- Running mosquitto_sub and mosquitto_pub with the ca.crt, client.crt and client.key I generated with generate-CA.sh and the information from the mosquitto documentation
I am pretty positive, it is just a tiny error and I am just too inexperienced to see it. Hope you can help.
Many thanks in advance!
Mathias