AWS / PHP / Python ちょいメモ

amazon web service , PHP, Python を使ったときのメモ。日本語でググってもわからなかった事を中心に。

Python が 参照する証明書ストアを掘ってみた (CentOS 版)

以前まとめた Ubuntu版 の続きになります。

Redhat 6 (CentOS) 系では、証明書ストア管理には、 ca-certificates の利用がオプション状態となっているので、enableにしてから使おうね。というような内容になります。Redhat 7以降は、すでに対処後の状態になってるはずです。

※文中、主に CentOSの対処方法を見ながら設定したので、RedhatCentOSと書き換えて記載させてもらいます

最初に

まずは、OS自身がもってるルート証明書の更新。ここから。

openssl , ルート証明書の更新

$ sudo yum update openssl
$ sudo yum update ca-certificates

うまくいかなかったら

各種更新でも対応ができなかった場合、対象サイトの証明書のルート証明書などを、手動でインストール必要があります。

Redhat6の場合、後述のような証明書管理システムの更新を行って対処しました。




対処

CentOS7 の場合

update-ca-trust を使って、ルート証明書などを取り込める旨の記載があった。

じゃあ、それでとやってみたものの、python, curl で対処サイトが見れない状態は継続。

CentOS6なら

動作上、次を直接編集するなどすればOKそう。

/etc/pki/tls/certs/ca-bundle.crt

でも、できるなら前述のコマンドを使いたい。

CentOS6の場合 : 確認

update-ca-trust の man をみてみると、この手順を行うには、どうやらいくつか設定が必要らしい。

$ man update-ca-trust

...

FILES
       /etc/pki/tls/certs/ca-bundle.crt
           Legacy filename, file contains a list of CA certificates trusted for TLS server authentication usage, in the simple BEGIN/END CERTIFICATE file format, without distrust information. If compatible CA trust
           replacements are disabled, this is a static file and will remain unchanged. Only if compatible CA trust replacements are enabled, this file is a symbolic link that refers to the consolidated output created by
           the update-ca-trust command.

       /etc/pki/tls/certs/ca-bundle.trust.crt
           Legacy filename, file contains a list of CA certificates in the extended BEGIN/END TRUSTED CERTIFICATE file format, which includes trust (and/or distrust) flags specific to certificate usage. If compatible CA
           trust replacements are disabled, this is a static file and will remain unchanged. Only if compatible CA trust replacements are enabled, this file is a symbolic link that refers to the consolidated output
           created by the update-ca-trust command.

       /etc/pki/java/cacerts
           Legacy filename, file contains a list of CA certificates trusted for TLS server authentication usage, in the Java keystore file format, without distrust information. If compatible CA trust replacements are
           disabled, this is a static file and will remain unchanged. Only if compatible CA trust replacements are enabled, this file is a symbolic link that refers to the consolidated output created by the
           update-ca-trust command.

       /usr/share/pki/ca-trust-source
           Contains multiple, low priority source configuration files as explained in section SOURCE CONFIGURATION. Please pay attention to the specific meanings of the respective subdirectories.

       /etc/pki/ca-trust/source
           Contains multiple, high priority source configuration files as explained in section SOURCE CONFIGURATION. Please pay attention to the specific meanings of the respective subdirectories.

       /etc/pki/ca-trust/extracted
           Contains consolidated and automatically generated configuration files for consumption by applications, which are created using the update-ca-trust extract command. Don’t edit files in this directory, because
           they will be overwritten. See section EXTRACTED CONFIGURATION for additional details.
...


対象ファイルを見てみると、tls/certs 配下は、作業前とファイルは変化してない。Legacy なのでさらわないってことかな。

$ ll /etc/pki/ca-trust/extracted/*
...

/etc/pki/ca-trust/extracted/pem:
合計 700
drwxr-xr-x. 2 root root   4096  5月 29 13:22 2018 .
drwxr-xr-x. 5 root root   4096  5月 29 12:15 2018 ..
-rw-r--r--  1 root root    897  5月 10 03:24 2017 README
-r--r--r--  1 root root 219978  5月 29 13:22 2018 email-ca-bundle.pem
-r--r--r--  1 root root 194238  5月 29 13:22 2018 objsign-ca-bundle.pem
-r--r--r--  1 root root 272797  5月 29 13:22 2018 tls-ca-bundle.pem
$ ll /etc/pki/tls/certs
合計 1936
drwxr-xr-x. 2 root root    4096  5月 29 12:15 2018 .
drwxr-xr-x. 5 root root    4096  5月 29 12:15 2018 ..
-rw-r--r--  1 root root    2242  1月 30 21:43 2017 Makefile
-rw-r--r--  1 root root  884985  5月 10 03:24 2017 ca-bundle.crt
-rw-r--r--  1 root root 1058960  5月 10 03:24 2017 ca-bundle.trust.crt
-rw-------. 1 root root    1139 12月 12 11:57 2012 localhost.crt
-rwxr-xr-x  1 root root     610  1月 30 21:43 2017 make-dummy-cert
-rwxr-xr-x  1 root root     829  1月 30 21:43 2017 renew-dummy-cert

作業で更新されたファイルが使えるのかを、環境変数を設定して確認してみた。結果 python でもOKだった。ということで、update-ca-trust コマンドの成果自体はOK。

$ export SSL_CERT_FILE=/etc/pki/ca-trust/extracted/openssl/ca-bundle.trust.crt
$ python3 manage.py shell
...

CentOS6の場合 : 設定

update-ca-trust コマンドによって、Pythonが直接参照している証明書ストアへの、証明書追加を反映させるように設定変更します。

$ sudo update-ca-trust enable
update-ca-trust: nss 32 bit is installed. You should install p11-kit-trust 32 bit.
update-ca-trust: aborting, because the nss / p11-kit setup is inconsistent.

ちょっと32bitのパッケージ不足らしいので、確認ついて対応

$ which /usr/lib/p11-kit/p11-kit-redhat-setup-trust
/usr/bin/which: no p11-kit-redhat-setup-trust in (/usr/lib/p11-kit)
$ which /usr/lib64/p11-kit/p11-kit-redhat-setup-trust
/usr/lib64/p11-kit/p11-kit-redhat-setup-trust
$ which /usr/lib/nss/libnssckbi.so
/usr/lib/nss/libnssckbi.so
$ which /usr/lib64/nss/libnssckbi.so
/usr/lib64/nss/libnssckbi.so

$ sudo yum search p11-kit-trust

...

$ which /usr/lib/p11-kit/p11-kit-redhat-setup-trust
/usr/lib/p11-kit/p11-kit-redhat-setup-trust

CentOS6の場合:設定後

エラーなくenableにできた。結果、Legacyと記載のあったファイル群が、シンボリックリンクに置き換わって、update-ca-trust管理下のファイルを参照してくれるようになった。

$ sudo update-ca-trust enable

$ ll /etc/pki/tls/certs
合計 24
drwxr-xr-x. 2 root root 4096  5月 29 20:01 2018 .
drwxr-xr-x. 6 root root 4096  5月 29 13:46 2018 ..
-rw-r--r--  1 root root 2242  1月 30 21:43 2017 Makefile
lrwxrwxrwx  1 root root   49  5月 29 20:01 2018 ca-bundle.crt -> /etc/pki/ca-trust/extracted/pem/tls-ca-bundle.pem
lrwxrwxrwx  1 root root   55  5月 29 20:01 2018 ca-bundle.trust.crt -> /etc/pki/ca-trust/extracted/openssl/ca-bundle.trust.crt
-rw-------. 1 root root 1139 12月 12 11:57 2012 localhost.crt
-rwxr-xr-x  1 root root  610  1月 30 21:43 2017 make-dummy-cert
-rwxr-xr-x  1 root root  829  1月 30 21:43 2017 renew-dummy-cert

その後

以上の対応により、 python でも curl でも、該当サイトをエラーなく取得しに行くことが可能になりました。


参考

対応後にみつけた、ざっくり明確に説明してあるポスト。

サイト

その他、以下の情報を参考にして対応しました。

今回行った設定は update-ca-trust による dynamic CA configuration feature と呼ぶみたいです。

上記に、乱雑な書き込みしちゃったのは私です。。。整理できておらず、すいません。

このファイル、どのパッケージがインストールしたんだろう的な確認に。

その他

python で requests ライブラリを使っている場合には、こちらを参照して certifi ライブラリをチェックするといいかもしれません

Windowsの証明書ストアは、MMCで確認ですね。