记录linux环境下搭建本地MQTT服务器实现mqtt的ssl加密通讯

embedded/2024/9/29 10:36:08/

1、ubuntu安装mosquitto

sudo apt-get update//安装服务端
sudo apt-get install mosquitto//安装客户端
sudo apt-get install mosquitto-clients

 2、安装openssl

3、mqtts/tls加密传输

mosquitto原生支持了TLS加密,TLS(传输层安全)是SSL(安全套接层)的新名称,mqtts/tls支持3种加密模式:

  • CA Signed server certificate:由CA机构签名的服务器认证(启用ssl加密不做认证)
  • Self-signed server certificate:自签名的服务器认证自签名服务器认证需要填写PEM格式的自签名CA证书 (单向认证)
  • Self-signed server & client certificate:自签名的服务器&客户端双向认证。自签名的双向认证需要填写PEM格式的自签名CA证书(CA Certificate),客户端证书(Client Certificate)和客户端密钥(Client Key)。 如果客户端密钥文件是加密的,需要填写客户端密钥密码(Client Key Passphrase)。(双向认证)

证书生成

使用github中的一个脚本,可以让hostname为localhost、本机ip或127.0.0.1,脚本从OweTracks项目下载并运行generate-CA.sh脚本。该脚本创建CA文件,生成服务器证书,并使用CA来签名证书。

//创建证书目录
mkdir certs//下载脚本到certs
cd certs
wget https://raw.githubusercontent.com/owntracks/tools/master/TLS/generate-CA.sh//执行脚本
sh ./generate-CA.sh

可生成以下内容:

[yangzheng@localhost certs]$ ls
ca.crt  ca.key  ca.srl generate-CA.sh  localhost.localdomain.crt  localhost.localdomain.csr  localhost.localdomain.key

将上述生成的文件拷贝到/etc/mosquitto/certs目录下(也可以不拷贝,再配置文件指定绝对路径)

//给下面3个文件换个名字
[yangzheng@localhost certs]$ mv localhost.localdomain.crt server.crt
[yangzheng@localhost certs]$ mv localhost.localdomain.csr server.csr
[yangzheng@localhost certs]$ mv localhost.localdomain.key server.key//拷贝文件到/etc/mosquitto/certs
[yangzheng@localhost certs]$ sudo cp -rf  ca.crt  ca.key  ca.srl client.crt  client.csr  client.key   server.crt  server.csr  server.key /etc/mosquitto/certs/

修改mosiquitto.conf,添加证书的路径,加密端口8883(配置端口监听端口默认是1883)

[yangzheng@localhost app]$ cat /etc/mosquitto/mosquitto.conf
# Place your local configuration in /etc/mosquitto/conf.d/
#
# A full description of the configuration file is at
# /usr/share/doc/mosquitto/examples/mosquitto.conf.examplepid_file /var/run/mosquitto.pidpersistence true
persistence_location /var/lib/mosquitto/log_dest file /var/log/mosquitto/mosquitto.log#add
listener 8883
cafile /etc/mosquitto/certs/ca.crt
certfile /etc/mosquitto/certs/host.crt
keyfile /etc/mosquitto/certs/host.key# one-way or two-way authentication
#require_certificate true
#end addinclude_dir /etc/mosquitto/conf.d

启动mqtt服务

[yangzheng@localhost certs]$ sudo mosquitto -d -c /etc/mosquitto/mosquitto.conf^C
[yangzheng@localhost certs]$ ps -aux |grep mosquitto
mosquit+  4860  0.1  0.1  45356  5684 ?        Ss   17:19   0:05 mosquitto -d -c /etc/mosquitto/mosquitto.conf
yangzhe+  4958  0.0  0.0  16156  1036 pts/10   S+   18:13   0:00 grep mosquitto

4 测试

ubuntu 命令行测试

a 单向认证

单向认证时客户端不需要生成客户端证书、钥匙和请求,仅需要将CA证书ca.crt,ca.key,ca.srl,拷贝到客户端系统中, 测试结果:

b 双向认证

双向认证则需根据CA证书生成客户端证书,生成客户端证书、钥匙和请求的方法是在CA证书文件夹下执行OpenSSL,命令如下:

[yangzheng@localhost certs]$ openssl genrsa -out client.key 2048
[yangzheng@localhost certs]$ openssl req -new -out client.csr -key client.key -subj "/CN=client/O=example.com"
[yangzheng@localhost certs]$ openssl x509 -req -in client.csr -CA ca.crt -CAkey ca.key -CAserial ./ca.srl -out client.crt -days 3650 -addtrust clientAuth

测试结果:

源码测试,关键配置:

a 只加密不认证

    MQTTClient_SSLOptions ssl_opts   = MQTTClient_SSLOptions_initializer;ssl_opts.enableServerCertAuth    = DISABLE;conn_opts.ssl                    = &ssl_opts;

b 单向认证

    MQTTClient_SSLOptions ssl_opts  = MQTTClient_SSLOptions_initializer;ssl_opts.enableServerCertAuth   = ENABLE;ssl_opts.trustStore             = "/mnt/tmp/ca.crt";conn_opts.ssl                   = &ssl_opts;

c 双向认证

    MQTTClient_SSLOptions ssl_opts  = MQTTClient_SSLOptions_initializer;ssl_opts.enableServerCertAuth   = ENABLE;ssl_opts.trustStore             = "/mnt/tmp/ca.crt";ssl_opts.privateKey             = "/mnt/tmp/client.key";ssl_opts.keyStore               = "/mnt/tmp/client.crt";conn_opts.ssl                   = &ssl_opts;

备注:

1、c 客户端源码下载git clone https://github.com/eclipse/paho.mqtt.c.git

2、使用mqtts/tls加密通信时,server地址需加"ssl://", 例如"ssl://127.0.0.1:8883";

3、编译mqtt源码时,在makefile增加OPENSSL宏定义,如CFLAGS += -DOPENSSL

4、需要移植openssl库,mqtt源码编译需要链接到openssl中的include和lib

测试结果:


http://www.ppmy.cn/embedded/119308.html

相关文章

基于python+spark的外卖餐饮数据分析系统设计与实现(含论文)-Spark毕业设计选题推荐

博主介绍: 大家好,本人精通Java、Python、C#、C、C编程语言,同时也熟练掌握微信小程序、Php和Android等技术,能够为大家提供全方位的技术支持和交流。 我有丰富的成品Java、Python、C#毕设项目经验,能够为学生提供各类…

Word样式的同步与重置

有时候我们需要修改Word中的样式,实现排版的个性化。 如何同步样式到其他电脑上? Word中的样式是由Normal.dotm文件控制的,对样式所有的设置和修改,都会保存到这个问题件中,所以我们只需要在设置好样式以后&#xff…

JSP(Java Server Pages)基础使用二

简单练习在jsp页面上输出出乘法口诀表 既然大家都是来看这种代码的人了&#xff0c;那么这种输出乘法口诀表的这种简单算法肯定是难不住大家了&#xff0c;所以这次主要是来说jsp的使用格式问题。 <%--Created by IntelliJ IDEA.User: ***Date: 2024/7/18Time: 11:26To ch…

Kafka技术详解[1]:简介与基础概念

目录 1. Kafka入门 1.1 概述 1.1.1 初识Kafka 1.1.2 消息队列 1.1.3 生产者-消费者模式 1.1.4 消息中间件对比 1.1.5 ZooKeeper 1. Kafka入门 1.1 概述 1.1.1 初识Kafka Kafka是由Scala和Java语言开发的高吞吐量分布式消息发布和订阅系统&#xff0c;也是大数据技术领…

微软 Win11 24H2 RP 26100.1876 预览版发布!附详细更新日志

系统之家于9月24日发出最新报道&#xff0c;微软为Release Preview频道的Windows Insider项目成员&#xff0c;发布了适用Windows11 24H2版本更新的 KB5043178&#xff0c;更新后&#xff0c;系统版本号将升至26100.1876。此更新为用户带来了不同的新功能&#xff0c;例如打开开…

搜索:如何用 A*搜索算法实现游戏中的寻路功能?

搜索:如何用 A*搜索算法实现游戏中的寻路功能? 在游戏开发中,寻路功能是一个非常重要的部分。它可以让游戏中的角色自动找到从一个位置到另一个位置的最佳路径。A搜索算法是一种常用的寻路算法,它可以在复杂的地图环境中快速找到最短路径。本文将详细介绍如何用 A搜索算法…

如何在数据分析中处理异常?

在数据分析中&#xff0c;处理异常值是确保数据质量的关键步骤。以下是一些常见的方法&#xff1a; 1. 检测异常值 可视化方法 箱线图&#xff1a;通过matplotlib或seaborn绘制箱线图&#xff0c;识别数据中的异常值。 import seaborn as sns import matplotlib.pyplot as …

【吊打面试官系列-MySQL面试题】优化MySQL数据库的方法?

大家好&#xff0c;我是锋哥。今天分享关于【优化MySQL数据库的方法?】面试题&#xff0c;希望对大家有帮助&#xff1b; 优化MySQL数据库的方法? 1、选取最适用的字段属性&#xff0c;尽可能减少定义字段宽度&#xff0c;尽量把字段设置 NOTNULL&#xff0c; 例如’省份’、…