ESP8266用(BearSSL_Validation.ino)のSSL接続用ファイル更新

ESP8266(ESP-WROOM-02)の内容。ESP-WROOM-32ではないので注意。

こちらのBearSSL_Validation.ino事前準備の手順。試している環境はWindows11。

前提

BearSSL_Validation.inoと同じディレクトリにあるcerts.hだが、サーバ側の証明書が更新されているとつながらなかったりする。(レポジトリ側の更新タイミング次第ではつながったりつながらなかったりだと思う)

certs.hを見ると下のように書いてあり

// this file is autogenerated - any modification will be overwritten
// unused symbols will not be linked in the final binary
// generated on 2023-03-20 23:02:42
// by ['../../../../tools/cert.py', '-s', 'www.example.com', '-n', 'SSL']

して更新するらしい。他のドメイン用に作る場合もおそらくcert.pyをたたけばよいと思う。

なお、cert.pyのファイル先頭コメント

#!/usr/bin/env python3

# Script to download/update certificates and public keys
# and generate compilable source files for c++/Arduino.
# released to public domain

Windowsのはまりポイント

WindowsでMS Storeからインストールするとツールは見れない場所にありそう。(MS Storeから入れたソフト類のディレクトリは見れない模様)インストーラー直ダウンロードしたIDE場合多分下みたいな配置。

C:\Users\hoge\Documents\ArduinoData\packages\esp8266\hardware\esp8266\3.1.2\libraries\ESP8266WiFi\examples\BearSSL_Validation

だと下にある

C:\Users\hoge\Documents\ArduinoData\packages\esp8266\hardware\esp8266\3.1.2\tools

MS Storeからインストールして見れない人は、レポジトリのソース一式ダウンロードしたら多分中に入っているcert.pyを使える。

環境構築

cert.pyはpython3なので必要ならインストール。

ライブラリインストール

ないと下みたいなメッセージ

python  '../../../../tools/cert.py' '-s' 'www.example.com' '-n' 'SSL'                            
Traceback (most recent call last):
  File "C:\Users\kitam\Documents\ArduinoData\packages\esp8266\hardware\esp8266\3.1.2\tools\cert.py", line 15, in <module>
    from cryptography import x509
ModuleNotFoundError: No module named 'cryptography'

もしかしたら

もいるかも。

import re
import ssl
import sys
import socket
import argparse
import datetime

あたりは元々使えると思うけど必要ならそいつらも

証明書生成実行

パスにユーザ名入っているので適宜書き換え。

出力ファイル名(test.h)も適宜書き換え

結果

PS C:\Users\kitam\Documents\ArduinoData\packages\esp8266\hardware\esp8266\3.1.2\libraries\ESP8266WiFi\examples\BearSSL_Validation> python  '../../../../tools/cert.py' '-s' 'www.example.com' '-n' 'SSL' > test.h
C:\Users\kitam\Documents\ArduinoData\packages\esp8266\hardware\esp8266\3.1.2\tools\cert.py:44: CryptographyDeprecationWarning: Properties that return a naïve datetime object have been deprecated. Please switch to not_valid_before_utc.
  print('// not valid before:', xcert.not_valid_before)
C:\Users\kitam\Documents\ArduinoData\packages\esp8266\hardware\esp8266\3.1.2\tools\cert.py:45: CryptographyDeprecationWarning: Properties that return a naïve datetime object have been deprecated. Please switch to not_valid_after_utc.
  print('// not valid after: ', xcert.not_valid_after)

なんか余計な警告出ているけどchatgpt先生によると以下らしいのでとりあえず無視

この警告は、Cryptography ライブラリが提供する一部の機能において、適切なタイムゾーン情報が欠如している(naïveな)datetimeオブジェクトを返すプロパティが非推奨であることを示しています。代わりに、not_valid_before_utcというプロパティを使用することが推奨されています。

ino修正

修正目的

ExampleのBearSSL_Validation.inoを任意フォルダ(今回はデスクトップ)に保存。

その後、cert.hの中身を先ほど出力したtest.hに変更

そのまま実行すると

C:\Users\kitam\Desktop\BearSSL_Validation\BearSSL_Validation.ino: In function 'void fetchCertAuthority()':
C:\Users\kitam\Desktop\BearSSL_Validation\BearSSL_Validation.ino:15:14: error: 'cert_DigiCert_TLS_RSA_SHA256_2020_CA1' was not declared in this scope; did you mean 'cert_DigiCert_Global_G2_TLS_RSA_SHA256_2020_CA1'?
   15 | #define CERT cert_DigiCert_TLS_RSA_SHA256_2020_CA1
      |              ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
C:\Users\kitam\Desktop\BearSSL_Validation\BearSSL_Validation.ino:15:14: note: in definition of macro 'CERT'
   15 | #define CERT cert_DigiCert_TLS_RSA_SHA256_2020_CA1
      |              ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

exit status 1

Compilation error: 'cert_DigiCert_TLS_RSA_SHA256_2020_CA1' was not declared in this scope; did you mean 'cert_DigiCert_Global_G2_TLS_RSA_SHA256_2020_CA1'?

出力されたtest.hにあるcert_DigiCert_Global_G2_TLS_RSA_SHA256_2020_CA1が元ヘッダーファイルにあったcert_DigiCert_TLS_RSA_SHA256_2020_CA1から変わっている。ちなみに、cert_DigiCert_Global_Root_G2とcert_DigiCert_Global_Root_CAも変わっている。

証明書のCNからcert.pyで自動命名しているのでしょうがない。

長期運用するならcert.pyをいじるか出てきたヘッダーの命名を書き換えて固定にした方が良いかも。深く考えないで取り急ぎinoの方を修正する。(15行目)

動作確認は元のページ

コメントを残す

メールアドレスが公開されることはありません。 が付いている欄は必須項目です