投稿

ラベル(MQTT)が付いた投稿を表示しています

MQTTでIoT通信(5)MosquittoでPSK認証

 MQTTでIoT通信するのに、平文というわけには以下なので、取りあえずの暗号化、本番だと、公開鍵暗号を使った相互認証がいいのであろうが、照明をの作成とか結構面倒くさいので取りあえず、試験的に使うので、共通鍵暗号を使ったPSKで構築することにした。 PSKなので、共有鍵がもれるとアウトなので注意が必要です。 まずはサーバー側のMosquittoの設定 まずPSKの設定を記述する /etc/mosquitto/pskfile の作成 openssl rand -hex 32 でランダムな16進数の32バイトの文字列を生成する 。例えば以下のような a1b2c3d4e5f67890abcdef1234567890 この文字列とクライアントを識別する識別子を /etc/mosquitto/pskfile に格納する。 デバイスごとにパス輪変えてもいいし、取りあえず一緒でもいい。 esp01:a1b2c3d4e5f67890abcdef1234567890 esp02:a1b2c3d4e5f67890abcdef1234567890  /etc/mosquitto/pskfile の所有者と、アクセス権を変更する。 sudo chown mosquitto:mosquitto /etc/mosquitto/pskfile sudo chmod 0600 /etc/mosquitto/pskfile /etc/mosquitto/mosquitto.conf の編集 pid_file /run/mosquitto/mosquitto.pid persistence true persistence_location /var/lib/mosquitto/ log_dest file /var/log/mosquitto/mosquitto.log include_dir /etc/mosquitto/conf.d  だっけ設定されているのだが、以下を追加 # 暗号化通信用のポートを指定 listener 8883 # PSK通信用のヒント文字列(クライアントに通知する任意の文字列) psk_hint my_mqtt_broker_hint # 作成したPSKファイルのパスを指定 psk_file /etc/mosquitto/pskfile # (...