paho.mqtt.c使用openssl加密通讯
生活随笔
收集整理的這篇文章主要介紹了
paho.mqtt.c使用openssl加密通讯
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
證書的生成和配置請參考https://blog.csdn.net/sy84436446/article/details/128576433
- 下載和編譯
- 測試paho.mqtt.c
2.1訂閱
修改src/samples/MQTTClient_subscribe.c
#define ADDRESS "ip" #define TOPIC "test2"運行訂閱
cd build/output/samples ./MQTTClient_subscribe使用mosquitto發布消息
mosquitto_pub -h "ip" -t "test2" -m "my name is xxx"2.2發布
修改src/samples/MQTTClient_publish.c
#define ADDRESS "ip" #define TOPIC "test1"使用mosquitto訂閱消息
mosquitto_sub -h "ip" -t "test1"運行發布
cd build/output/samples ./MQTTClient_publish- 使用openssl
3.1 啟用ssl加密不做認證
# git diff src/samples/MQTTClient_publish.c diff --git a/src/samples/MQTTClient_publish.c b/src/samples/MQTTClient_publish.c index 399fd33..daa3522 100644 --- a/src/samples/MQTTClient_publish.c +++ b/src/samples/MQTTClient_publish.c @@ -19,9 +19,9 @@#include <string.h>#include "MQTTClient.h"-#define ADDRESS "tcp://mqtt.eclipseprojects.io:1883" +#define ADDRESS "ssl://192.168.186.131:1883"#define CLIENTID "ExampleClientPub" -#define TOPIC "MQTT Examples" +#define TOPIC "test1"#define PAYLOAD "Hello World!"#define QOS 1#define TIMEOUT 10000L @@ -41,6 +41,11 @@ int main(int argc, char* argv[])exit(EXIT_FAILURE);}+ MQTTClient_SSLOptions ssl_opts=MQTTClient_SSLOptions_initializer; + ssl_opts.enableServerCertAuth=0; + ssl_opts.sslVersion=MQTT_SSL_VERSION_DEFAULT; + conn_opts.ssl=&ssl_opts; +conn_opts.keepAliveInterval = 20;conn_opts.cleansession = 1;if ((rc = MQTTClient_connect(client, &conn_opts)) != MQTTCLIENT_SUCCESS)# 訂閱 mosquitto_sub -h 192.168.186.131 -t test --cafile ./ca.crt # 發布 ./build/output/samples/MQTTClient_publish3.2單向認證
注意在mosquitto配置文件中配置證書路徑
# git diff src/samples/MQTTClient_publish.c diff --git a/src/samples/MQTTClient_publish.c b/src/samples/MQTTClient_publish.c index 399fd33..1155f24 100644 --- a/src/samples/MQTTClient_publish.c +++ b/src/samples/MQTTClient_publish.c @@ -19,9 +19,9 @@#include <string.h>#include "MQTTClient.h"-#define ADDRESS "tcp://mqtt.eclipseprojects.io:1883" +#define ADDRESS "ssl://192.168.186.131:1883"#define CLIENTID "ExampleClientPub" -#define TOPIC "MQTT Examples" +#define TOPIC "test"#define PAYLOAD "Hello World!"#define QOS 1#define TIMEOUT 10000L @@ -41,6 +41,11 @@ int main(int argc, char* argv[])exit(EXIT_FAILURE);}+ MQTTClient_SSLOptions ssl_opts=MQTTClient_SSLOptions_initializer; + ssl_opts.trustStore = "/home/sy/key/ca.crt"; + ssl_opts.sslVersion=MQTT_SSL_VERSION_DEFAULT; + conn_opts.ssl=&ssl_opts; +conn_opts.keepAliveInterval = 20;conn_opts.cleansession = 1;if ((rc = MQTTClient_connect(client, &conn_opts)) != MQTTCLIENT_SUCCESS)# 訂閱 mosquitto_sub -h 192.168.186.131 -t test --cafile ./ca.crt # 發布 ./build/output/samples/MQTTClient_publish3.3雙向認證
注意在mosquitto配置文件中開啟require_certificate true。
# git diff src/samples/MQTTClient_publish.c diff --git a/src/samples/MQTTClient_publish.c b/src/samples/MQTTClient_publish.c index 399fd33..7a0fe3f 100644 --- a/src/samples/MQTTClient_publish.c +++ b/src/samples/MQTTClient_publish.c @@ -19,9 +19,9 @@#include <string.h>#include "MQTTClient.h"-#define ADDRESS "tcp://mqtt.eclipseprojects.io:1883" +#define ADDRESS "ssl://192.168.186.131:1883"#define CLIENTID "ExampleClientPub" -#define TOPIC "MQTT Examples" +#define TOPIC "test"#define PAYLOAD "Hello World!"#define QOS 1#define TIMEOUT 10000L @@ -41,6 +41,13 @@ int main(int argc, char* argv[])exit(EXIT_FAILURE);}+ MQTTClient_SSLOptions ssl_opts=MQTTClient_SSLOptions_initializer; + ssl_opts.trustStore = "/home/sy/key/ca.crt"; + ssl_opts.privateKey = "/home/sy/key/client.key"; + ssl_opts.keyStore = "/home/sy/key/client.crt"; + ssl_opts.sslVersion=MQTT_SSL_VERSION_DEFAULT; + conn_opts.ssl=&ssl_opts; +conn_opts.keepAliveInterval = 20;conn_opts.cleansession = 1;if ((rc = MQTTClient_connect(client, &conn_opts)) != MQTTCLIENT_SUCCESS)# 訂閱 sudo mosquitto_sub -h 192.168.186.131 -t test --cafile ./ca.crt --cert ./client.crt --key ./client.key # 發布 ./build/output/samples/MQTTClient_publish總結
以上是生活随笔為你收集整理的paho.mqtt.c使用openssl加密通讯的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: appcan java_appcan下载
- 下一篇: 【存储】GPFS简介及搭建