Amazon Web Services 資料一覧

Amazon Linux 2上でcurl実行時に「SSL certificate problem: unable to get local issuer certificate」というエラーが出る場合の対応

2019/12/24更新

対応バージョン: Amazon Linux 2

Amazon Linux 2上でcurlで任意のWebサイトにアクセスしようとすると以下のようなエラーが出ることがある。
$ curl https://foo.bar.com/
:
curl: (60) SSL certificate problem: unable to get local issuer certificate
More details here: https://curl.haxx.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.

これはローカルに保存されているルート証明書の中にアクセス先サイトのSSL証明書を証明する者(issuer)がいないことが原因なので、証明者をシステムに組み込んであげればよい。

ここではアクセス先サイトのSSL証明書がLet's Encrypt発行のものだった場合の対応手順を示す。

まずSSL証明書の内容を調べる
$ openssl s_client -connect foo.bar.com:443 < /dev/null 2> /dev/null | openssl x509 -text
Certificate:
    Data:
        Version: 3 (0x2)
        Serial Number:
            04:e3:91:24:eb:0e:28:a7:9e:e7:84:ac:b4:65:8d:4d:b9:c7
        Signature Algorithm: sha256WithRSAEncryption
        Issuer: C = US, O = Let's Encrypt, CN = Let's Encrypt Authority X3
        Validity
            Not Before: Oct 26 13:34:11 2019 GMT
            Not After : Jan 24 13:34:11 2020 GMT
:

IssuerがLet's Encryptであることが確認できる。

次に以下からクロス署名されたLet’s Encryptの中間証明書を取得する

手順は以下の通り。

$ curl https://letsencrypt.org/certs/lets-encrypt-x3-cross-signed.pem.txt \
-o /tmp/lets-encrypt-x3-cross-signed.pem

$ openssl verify /tmp/lets-encrypt-x3-cross-signed.pem
/tmp/lets-encrypt-x3-cross-signed.pem: OK
この中間証明書をシステムに組み込む

/etc/pki/ca-trust/extracted配下のファイルが更新されるので念のためバックアップを取っておく。

$ cd /etc/pki/ca-trust/extracted
$ sudo tar cvf crt.tar *

証明書をシステムに組み込む。

$ sudo cp /tmp/lets-encrypt-x3-cross-signed.pem /etc/pki/ca-trust/source/anchors
$ sudo update-ca-trust extract

これでcurlでエラーにならずにコンテンツが取得できるようになる。

$ curl https://foo.bar.com/
<!DOCTYPE html>
<html>
:

Ubuntuの場合

ちなみにUbuntu 18.04の場合は以下のようにすれば同様の対応が可能である。

$ sudo cp -p /etc/ssl/certs/ca-certificates.crt{,.org}

$ curl https://letsencrypt.org/certs/lets-encrypt-x3-cross-signed.pem.txt \
-o /tmp/lets-encrypt-x3-cross-signed.pem

$ sudo sh -c "cat /tmp/lets-encrypt-x3-cross-signed.pem >> /etc/ssl/certs/ca-certificates.crt"

$ openssl verify /etc/ssl/certs/ca-certificates.crt
/etc/ssl/certs/ca-certificates.crt: OK

参考サイト