概要
CentOS6.9上での、Mosquitto-TLSの動作確認手順です。
Server側でBrokerを起動し、Client側からsubとpubしたい。その際、SSL化したい。
ざっくりそんなかんじです。
分かってしまうと簡単なのですが、証明書辺りでドツボにはまってしまいました。
あと、時刻同期も忘れていると気づくまでに時間がかかりますので、お気を付けください。
前提条件
1.ホスト名の設定を以下とします
Server側 centos69server とします。
Client側 centos69client とします。(こちらはなんでもよいです)
2./etc/hostsにホスト名を記入していること
Server側、Client側、両方の環境に定義してください。
ipアドレスは、環境に合わせて変更してください。
192.168.1.1 centos69server
3.時刻同期が行われていること
行われていない場合は、ntpのインストールと時刻同期を行います。
# sudo yum -y install ntp
# ntpdate ntp.nict.jp
4.ファイアウォールの無効化、SELinuxをOFF
今回動作確認を優先するため、あえてOFFにします。
終わったら、きちんと戻してください。
Server側の設定
1.mosquittoのインストール
[root@centos69server ~]# wget http://download.opensuse.org/repositories/home:/oojah:/mqtt/CentOS_CentOS-6/home:oojah:mqtt.repo -O "/etc/yum.repos.d/Mosquitto.repo"
[root@centos69server ~]# yum install mosquitto mosquitto-clients
2.認証局の証明書作成(自己証明書)
Common Name に、myca と入れます。
他は何も入れなくても大丈夫でした。
[root@centos69server ~]# cd /etc/mosquitto/certs
[root@centos69server certs]# openssl req -new -x509 -days 365 -extensions v3_ca -keyout ca.key -out ca.crt
Generating a 2048 bit RSA private key
.......................+++
..........................................................+++
writing new private key to 'ca.key'
Enter PEM pass phrase: (パスフレーズを入れます)
Verifying - Enter PEM pass phrase: (上記と同じパスフレーズを入れます)
-----
You are about to be asked to enter information that will be incorporated
into your certificate request.
What you are about to enter is what is called a Distinguished Name or a DN.
There are quite a few fields but you can leave some blank
For some fields there will be a default value,
If you enter '.', the field will be left blank.
-----
Country Name (2 letter code) [XX]:
State or Province Name (full name) []:
Locality Name (eg, city) [Default City]:
Organization Name (eg, company) [Default Company Ltd]:
Organizational Unit Name (eg, section) []:
Common Name (eg, your name or your server's hostname) []:myca
Email Address []:
3.サーバ証明書の作成(その1)
[root@centos69server certs]# openssl genrsa -out server.key 2048
Generating RSA private key, 2048 bit long modulus
................................................................................................................+++
............+++
e is 65537 (0x10001)
4.サーバ証明書の作成(その2)
Common Name に、centos69server と入れます。
他は何も入れなくても大丈夫でした。
[root@centos69server certs]# openssl req -out server.csr -key server.key -new
You are about to be asked to enter information that will be incorporated
into your certificate request.
What you are about to enter is what is called a Distinguished Name or a DN.
There are quite a few fields but you can leave some blank
For some fields there will be a default value,
If you enter '.', the field will be left blank.
-----
Country Name (2 letter code) [XX]:
State or Province Name (full name) []:
Locality Name (eg, city) [Default City]:
Organization Name (eg, company) [Default Company Ltd]:
Organizational Unit Name (eg, section) []:
Common Name (eg, your name or your server's hostname) []: centos69server
Email Address []:
Please enter the following 'extra' attributes
to be sent with your certificate request
A challenge password []:
An optional company name []:
5.サーバ証明書の作成(その3)
[root@centos69server certs]# openssl x509 -req -in server.csr -CA ca.crt -CAkey ca.key -CAcreateserial -out server.crt -days 365
Signature ok
subject=/C=XX/L=Default City/O=Default Company Ltd/CN=centos69server
Getting CA Private Key
Enter pass phrase for ca.key:
ここまでで、以下のファイルが作成されました。
/etc/mosquitto/certs |
---|
ca.crt |
ca.key |
ca.srl |
server.crt |
server.csr |
server.key |
6./etc/mosquitto/mosquitto.confの編集
listener 8883
cafile /etc/mosquitto/certs/ca.crt
certfile /etc/mosquitto/certs/server.crt
keyfile /etc/mosquitto/certs/server.key
7.mosquittoの実行
[user@centos69server mosquitto]$ cd /etc/mosquitto
[user@centos69server mosquitto]$ mosquitto -c mosquitto.conf
1493895821: mosquitto version 1.4.10 (build date 2016-12-05 08:25:40+0000) starting
1493895821: Config loaded from mosquitto.conf.
1493895821: Opening ipv4 listen socket on port 8883.
1493895821: Opening ipv6 listen socket on port 8883.
これで、Broker(Server側)の準備ができました。
次に、Pub/Sub側(Client側)の準備をします。
Client側の設定
[root@centos69client ~]# wget http://download.opensuse.org/repositories/home:/oojah:/mqtt/CentOS_CentOS-6/home:oojah:mqtt.repo -O "/etc/yum.repos.d/Mosquitto.repo"
[root@centos69client ~]# yum install mosquitto mosquitto-clients
1.証明書の複写
Server側(centos69server)から、以下のファイルをClient側の同一パスへ複写する。
/etc/mosquitto/certs |
---|
ca.crt |
2.動作確認(subでの接続)
[user@centos69client ~]$ mosquitto_sub -d -t xxx -h centos69server -p 8883 --cafile /etc/mosquitto/certs/ca.crt
Client mosqsub/29658-centos69c sending CONNECT
Client mosqsub/29658-centos69c received CONNACK
Client mosqsub/29658-centos69c sending SUBSCRIBE (Mid: 1, Topic: xxx, QoS: 0)
Client mosqsub/29658-centos69c received SUBACK
Subscribed (mid: 1): 0
3.動作確認(pubの実行)
[user@centos69client ~]$ mosquitto_pub -d -t xxx -m "hoge" -h centos69server -p 8883 --cafile /etc/mosquitto/certs/ca.crt
Client mosqpub/29687-centos69c sending CONNECT
Client mosqpub/29687-centos69c received CONNACK
Client mosqpub/29687-centos69c sending PUBLISH (d0, q0, r0, m1, 'xxx', ... (4 bytes))
Client mosqpub/29687-centos69c sending DISCONNECT
[user@centos69client ~]$
4.結果(sub側の結果)
[user@centos69client ~]$ mosquitto_sub -d -t xxx -h centos69server -p 8883 --cafile /etc/mosquitto/certs/ca.crt
Client mosqsub/29658-centos69c sending CONNECT
Client mosqsub/29658-centos69c received CONNACK
Client mosqsub/29658-centos69c sending SUBSCRIBE (Mid: 1, Topic: xxx, QoS: 0)
Client mosqsub/29658-centos69c received SUBACK
Subscribed (mid: 1): 0
Client mosqsub/29658-centos69c sending PINGREQ
Client mosqsub/29658-centos69c received PINGRESP
Client mosqsub/29658-centos69c received PUBLISH (d0, q0, r0, m0, 'xxx', ... (4 bytes))
hoge
hogeが確認できれば、OKです。
これでServer<-->Client間でのpub/subが確認できました。
謝辞
tthkさんアドバイスありがとうございました!