(mosquitto)mqtts通信をやってみる

投稿者: | 2016年11月13日

みなさんこんにちは。ヒロウミです。

mqttブローカであるmosquittoサーバを利用してみたのでメモっておきます。今回の目的はpub/subをSSLで暗号化し、クライアント証明書による本人認証まで行うことです。

環境

・ubuntu14.04

・mosquitto version 1.4.10

・mosquitto_sub version 1.4.10

・mosquitto_pub version 1.4.10

mosquittoのインストール

以下の手順でmosquittoをインストールしていきます

$ sudo add-apt-repository ppa:mosquitto-dev/mosquitto-ppa

$ sudo apt-get update

$ sudo apt-get install mosquitto-clients

$ sudo apt-get install mosquitto

 

mosquittoの設定

mosquittoの設定はこんな感じ。SSL通信を行うように設定します。また、クライアント証明書の設定も行います。

$ sudo cat /etc/mosquitto/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

# ssl config
listener 8883
cafile /home/hiro/ca/ca.crt
certfile /home/hiro/ca/server/server.crt
keyfile /home/hiro/ca/server/server.key
require_certificate true

サーバ証明書の発行

先ず初めにオレオレ認証局を作成し、サーバ証明書、クライアント証明書を作成します。

CA証明書の発行
$ mkdir ~/ca && cd $_
$ openssl req -new -x509 -days 3650 -extensions v3_ca -keyout ca.key -out ca.crt

サーバ証明書の発行と署名
$ mkdir ~/ca/server && cd $_
$ openssl genrsa -out server.key 2048
$ openssl req -out server.csr -key server.key -new
$ openssl x509 -req -in server.csr -CA ../ca.crt -CAkey ../ca.key -CAcreateserial -out server.crt -days 365

クライアント証明書の発行と署名
$ mkdir ~/ca/client && cd $_
$ openssl genrsa -out client.key 2048
$ openssl req -out client.csr -key client.key -new
$ openssl x509 -req -in client.csr -CA ../ca.crt -CAkey ../ca.key -CAcreateserial -out client.crt -days 365

pub/subテスト

Subscribe
$ mosquitto_sub -d -t testtopic  -h rproxy -p 8883 --cafile ~/ca/ca.crt --cert ~/ca/client/client.crt --key ~/ca/client/client.key

Publish
$ mosquitto_pub -d -h rproxy -p 8883 --cafile ~/ca/ca.crt --cert ~/ca/client/client.crt --key ~/ca/client/client.key -t testtopic -m 'hello mosquitto !!'

まとめ

今回はMQTTブローカとしてmosquittoを使用し暗号化されたpub/subを行ってみました。mosquittoの設定で「PostgreSQLと連携したACL」などまだまだ細かい設定があるようなので試していきたいと思います。

参考サイト:

Mosquitto(MQTT Broker)のインストール