SSLエラートラブルシューティング
問題
curl コマンドが外部サイトに接続できない
(というか、外部通信を行う Linux コマンド全般が SSL エラーで失敗する)
原因はおそらくプロキシサーバ
user@wslhost:~$ curl https://example.com -I
curl: (60) SSL certificate problem: self-signed certificate in certificate chain
More details here: https://curl.se/docs/sslcerts.html
curl failed to verify the legitimacy of the server and therefore could not
establish a secure connection to it. To learn more about this situation and
how to fix it, please visit the web page mentioned above.
user@wslhost:~$
解決
CA証明書を Windows から WSL に移設
-
Windows の検索に
certmgr.mscを入力して証明書マネージャーを開く -
証明書 - 現在のユーザ\信頼されたルート証明機関\証明書から
組織ネットワークで使用されている CA 証明書をエクスポートする- 形式は
Base 64 encoded X.509(拡張子.cer) - エクスポート先は任意のフォルダ
- 形式は
-
WSL 側で CA 証明書ディレクトリにコピーする
- 拡張子を
.crtにしないと$ sudo update-ca-certificatesで読み込まない
- 拡張子を
user@wslhost:~$ ls -la /usr/local/share/ca-certificates/
total 8
drwxr-xr-x 2 root root 4096 Aug 6 01:55 .
drwxr-xr-x 7 root root 4096 Aug 6 01:57 ..
user@wslhost:~$ sudo cp /mnt/c/path/to/org-proxy.cer /usr/local/share/ca-certificates/org-proxy.crt
user@wslhost:~$ sudo cp /mnt/c/path/to/org-root-ca.cer /usr/local/share/ca-certificates/org-root-ca.crt
user@wslhost:~$ ls -la /usr/local/share/ca-certificates/
total 16
drwxr-xr-x 2 root root 4096 Feb 2 19:53 .
drwxr-xr-x 7 root root 4096 Feb 2 19:48 ..
-r-xr-xr-x 1 root root 1546 Feb 2 19:52 org-root-ca.crt
-r-xr-xr-x 1 root root 1566 Feb 2 19:49 org-proxy.crt
CA証明書を反映する。
user@wslhost:~$ sudo update-ca-certificates
Updating certificates in /etc/ssl/certs...
2 added, 0 removed; done.
Running hooks in /etc/ca-certificates/update.d...
done.
動作確認。
user@wslhost:~$ curl https://example.com -I
HTTP/1.1 200 OK
どの CA証明書 を移設するか
-
一般的な Windows 環境では見かけない CA 証明書が対象になることが多い
-
組織ネットワーク配下でアクセス制限されているサイトの証明書を確認する
- ブラウザの警告画面から証明書情報を確認
- 同名の証明書を
certmgr.mscから探す
-
ネットワーク制御やセキュリティ関連の名称を手がかりにする
-
最終的にはトライアンドエラー
移設した CA証明書を Python にも読み込ませる
WSL 側で CA 証明書を登録しても、Python(requests)は自動では参照しないため
環境変数 REQUESTS_CA_BUNDLE を設定する。
#!/usr/bin/env python3
import sys
import requests
url = sys.argv[1]
r = requests.get(url, timeout=5)
print(r.status_code)
user@wslhost:~$ python3 check.py https://example.com
requests.exceptions.SSLError: HTTPSConnectionPool(...)
user@wslhost:~$ export REQUESTS_CA_BUNDLE=/etc/ssl/certs/ca-certificates.crt
user@wslhost:~$ python3 check.py https://example.com
200
export で設定した環境変数はセッション単位のため、
永続化したい場合は .bashrc や .env などに記載する。
移設した CA証明書を Git コマンドにも読み込ませる
Git でも SSL エラーが出る場合は、CA 証明書を指定する。
user@wslhost:~$ git config --global http.sslCAInfo /etc/ssl/certs/ca-certificates.crt
動作確認環境
user@wslhost:~$ ls -la /usr/local/share/ca-certificates/
total 16
drwxr-xr-x 2 root root 4096 Feb 2 19:53 .
drwxr-xr-x 7 root root 4096 Feb 2 19:48 ..
-r-xr-xr-x 1 root root 1546 Feb 2 19:52 org-root-ca.crt
-r-xr-xr-x 1 root root 1566 Feb 2 19:49 org-proxy.crt
user@wslhost:~$ uname -a
Linux wslhost x86_64 GNU/Linux