Mosquitto の SSL 対応を試してみる
以前に、「Mosquitto を VPN で試しました 」では、Mosquitto を OpenVPN 経由で試してみました。
今回は、Mosquitto を SSL 対応にします。
まず、mosquitto-tls は、CA、サーバー、クライアントの各キー・証明書を作成する手順です。
CA を作成します。プライベート認証局や独自認証局、自己認証局と呼ばれる、主に個人で使用する認証局です。
[shell]
mkdir /etc/mosquitto/ca
cd /etc/mosquitto/ca
openssl req -new -x509 -days 365 -extensions v3_ca -keyout ca.key -out ca.crt
[/shell]
サーバー用のキーと証明書を作成します。CSR の設定例は google 検索すると豊富に見つかります。「お名前.com VPS (KVM) にて WordPress をはじめるまで(6)」も参考にどうぞ。Common Name が特に重要です。接続するサーバーを指定します。
[shell]
mkdir /etc/mosquitto/tls
cd /etc/mosquitto/tls
openssl genrsa -des3 -out server.key 2048
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
[/shell]
クライアント用のキーと証明書を作成します。
[shell]
openssl genrsa -des3 -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
[/shell]
/etc/mosquitto/mosquitto.conf に記述を追加します。
[shell]
port 8883
cafile /etc/mosquitto/ca/ca.crt
capath /etc/mosquitto/ca/
certfile /etc/mosquitto/tls/server.crt
keyfile /etc/mosquitto/tls/server.key
require_certificate true
allow_anonymous false
password_file /etc/mosquitto/passwd
[/shell]
パスワードファイルは次のコマンドで作成します。
[shell]
mosquitto_passwd -c /etc/mosquitto/passwd username
[/shell]
Mosquitto を再起動します。
[shell]
service mosquitto restart
[/shell]
「Mosquitto へ SSL で接続してみる」では、Python の Mosquitto クライアント プログラムで接続を試します。
また、「Mosquitto の Bridge を SSL で試します」では、ブリッジ接続を SSL で試します。
証明書の有効期限切れ通知
次のようなメッセージが出力されました。これは、証明書の有効期限切れです。証明書を再作成することで解消します。
1443001305: OpenSSL Error: error:14094415:SSL routines:SSL3_READ_BYTES:sslv3 alert certificate expired