Mosquitto MQTT 客户端证书异常
Mosquitto MQTT client certificate exception
我正在开发一个应用程序,总而言之,它使用 MQTT 将传感器值发送到代理,以便稍后在仪表板 Web 应用程序中可视化该数据。
我有五个连接到代理的微控制器,我已经为每个微控制器的代理和客户端证书设置了一个服务器证书。
问题是,在 mosquitto.conf 文件中,我需要为想要连接的客户端使用客户端证书,所以如果我想从我的 Web 应用程序订阅主题,我需要一个客户端证书。我正在尝试找到实现此目的的正确方法,但在您无法控制的机器中拥有证书和密钥似乎是一个很大的安全风险。
如果有人知道调整 mosquitto 配置文件或建立某种例外(可能类似于 ACL)以仅要求某些客户端(在我的例子中是微控制器)的客户端证书并使用其他人(网络客户端)的用户名@密码。有没有可能做这样的事情?
任何帮助将不胜感激
编辑(关于@hardillb 的回答)
我的mosquitto.conf:
pid_file /var/run/mosquitto.pid
persistence true
persistence_location /var/lib/mosquitto/
log_dest file /var/log/mosquitto/mosquitto.log
include_dir /etc/mosquitto/conf.d
per_listener_settings true
listener 9873
protocol websockets
#http_dir /home/jamengual/Desktop/UIB/TFG/mqtt/webAPP
cafile /etc/mosquitto/ca_certificates/ca.crt
keyfile /etc/mosquitto/certs/server.key
certfile /etc/mosquitto/certs/server.crt
listener 8883
cafile /etc/mosquitto/ca_certificates/ca.crt
keyfile /etc/mosquitto/certs/server.key
certfile /etc/mosquitto/certs/server.crt
require_certificate true
"per_listener_settings true" 使服务器进入活动(退出)状态。
来自 mosquitto.conf 指南:
谈论身份验证机制:
Both certificate and PSK based encryption are configured on a
per-listener basis.
谈论 per_listener_settings 选项:
per_listener_settings [ true | false ]
If true, then authentication and access control settings will be controlled on a per-listener basis. The following options are affected:
password_file, acl_file, psk_file, allow_anonymous, allow_zero_length_clientid, auth_plugin, auth_opt_*, auto_id_prefix.
所以我知道 per_listener_settings 选项对于 require_certificate 部分可能不是必需的。但是,我仍然需要它来配置 websockets 的用户名和密码。
我的配置文件有问题吗?
Link to my question about how to store client certificates and keys in the client's machine
Mosquitto 允许您为每个代理拥有多个共享相同主题的侦听器 space。
Listeners 可以支持原生 MQTT、MQTT over Websockets(包括 Websockets over TLS)和 MQTT over TLS。
它还有 per_listener_settings
选项,允许您为不同的侦听器指定不同的身份验证选项。此选项是在 mosquitto 版本 1.5 中添加的。
因此,在这种情况下,您可以创建 MQTT over TLS 侦听器并使用客户端证书对这些用户(设备)进行身份验证,并创建一个 MQTT over Websocket 侦听器,它将使用 username/password 身份验证。
例如类似这样的东西(但可能使用身份验证插件而不是 acl/password 文件)
per_listener_settings true
listener 1884
cafile /path/to/ca
certfile /path/to/cert
keyfile /path/to/key
require_certificate true
acl_file /path/to/acl_file
listener 8883
protocol websockets
acl_file /path/to/acl_file
password_file /path/to/password
您还可以为 websocket 侦听器包含 ca_file
、cert_file
和 key_file
选项,以通过 TLS 启用 Websockets(但不要使用 require_certificate
因为 websockets 的浏览器端客户端证书处理不是很好的体验,因为他们不问使用哪个)。但通常我通常会使用 NGINX 之类的东西来代理 websocket 侦听器并进行 TLS 终止。
所有选项的详细信息可以在 mosquitto.conf 手册页中找到:https://mosquitto.org/man/mosquitto-conf-5.html
我正在开发一个应用程序,总而言之,它使用 MQTT 将传感器值发送到代理,以便稍后在仪表板 Web 应用程序中可视化该数据。 我有五个连接到代理的微控制器,我已经为每个微控制器的代理和客户端证书设置了一个服务器证书。
问题是,在 mosquitto.conf 文件中,我需要为想要连接的客户端使用客户端证书,所以如果我想从我的 Web 应用程序订阅主题,我需要一个客户端证书。我正在尝试找到实现此目的的正确方法,但在您无法控制的机器中拥有证书和密钥似乎是一个很大的安全风险。
如果有人知道调整 mosquitto 配置文件或建立某种例外(可能类似于 ACL)以仅要求某些客户端(在我的例子中是微控制器)的客户端证书并使用其他人(网络客户端)的用户名@密码。有没有可能做这样的事情?
任何帮助将不胜感激
编辑(关于@hardillb 的回答)
我的mosquitto.conf:
pid_file /var/run/mosquitto.pid
persistence true
persistence_location /var/lib/mosquitto/
log_dest file /var/log/mosquitto/mosquitto.log
include_dir /etc/mosquitto/conf.d
per_listener_settings true
listener 9873
protocol websockets
#http_dir /home/jamengual/Desktop/UIB/TFG/mqtt/webAPP
cafile /etc/mosquitto/ca_certificates/ca.crt
keyfile /etc/mosquitto/certs/server.key
certfile /etc/mosquitto/certs/server.crt
listener 8883
cafile /etc/mosquitto/ca_certificates/ca.crt
keyfile /etc/mosquitto/certs/server.key
certfile /etc/mosquitto/certs/server.crt
require_certificate true
"per_listener_settings true" 使服务器进入活动(退出)状态。 来自 mosquitto.conf 指南:
谈论身份验证机制:
Both certificate and PSK based encryption are configured on a per-listener basis.
谈论 per_listener_settings 选项:
per_listener_settings [ true | false ]
If true, then authentication and access control settings will be controlled on a per-listener basis. The following options are affected:
password_file, acl_file, psk_file, allow_anonymous, allow_zero_length_clientid, auth_plugin, auth_opt_*, auto_id_prefix.
所以我知道 per_listener_settings 选项对于 require_certificate 部分可能不是必需的。但是,我仍然需要它来配置 websockets 的用户名和密码。
我的配置文件有问题吗?
Link to my question about how to store client certificates and keys in the client's machine
Mosquitto 允许您为每个代理拥有多个共享相同主题的侦听器 space。
Listeners 可以支持原生 MQTT、MQTT over Websockets(包括 Websockets over TLS)和 MQTT over TLS。
它还有 per_listener_settings
选项,允许您为不同的侦听器指定不同的身份验证选项。此选项是在 mosquitto 版本 1.5 中添加的。
因此,在这种情况下,您可以创建 MQTT over TLS 侦听器并使用客户端证书对这些用户(设备)进行身份验证,并创建一个 MQTT over Websocket 侦听器,它将使用 username/password 身份验证。
例如类似这样的东西(但可能使用身份验证插件而不是 acl/password 文件)
per_listener_settings true
listener 1884
cafile /path/to/ca
certfile /path/to/cert
keyfile /path/to/key
require_certificate true
acl_file /path/to/acl_file
listener 8883
protocol websockets
acl_file /path/to/acl_file
password_file /path/to/password
您还可以为 websocket 侦听器包含 ca_file
、cert_file
和 key_file
选项,以通过 TLS 启用 Websockets(但不要使用 require_certificate
因为 websockets 的浏览器端客户端证书处理不是很好的体验,因为他们不问使用哪个)。但通常我通常会使用 NGINX 之类的东西来代理 websocket 侦听器并进行 TLS 终止。
所有选项的详细信息可以在 mosquitto.conf 手册页中找到:https://mosquitto.org/man/mosquitto-conf-5.html