Let's Encrypt導入(ワイルドカード対応)

Contents


はじめに

前回、Apacheの環境にLet's Encrypt でSSL証明書の自動更新まで試してみましたが、ワイルドカード証明書に関しては、取得にDNS-01 による認証を行うことが必須のようなので、改めて試してみることにしました。

仕組み

Let's EncryptはACME(Automatic Certificate Management Environment)というプロトコルが採用されており、バージョンがACME v2 になったことでワイルドカート証明書に対応可能となったそうです。 DNS-01 による認証とはLet's Encryptの認証局から発行されたトークンをDNSのTXTレコードに登録して認証を行うものです。

手順

certbotコマンドを実行します。
ワイルドカードのみの指定だと、ベースドメイン(サブドメインなし)が有効にならないので、併せて指定しておきます。
※ -d eastforest.jp と-d *.eastforest.jp を両方指定する
# certbot certonly \
--manual \
--preferred-challenges dns-01 \
--server https://acme-v02.api.letsencrypt.org/directory \
--agree-tos \
--manual-public-ip-logging-ok \
-d eastforest.jp \
-d *.eastforest.jp \
-m <Mail Address>
"Press Enter to Continue" のメッセージが表示されたら一旦このプロンプトはこのままにしておきます。
Saving debug log to /var/log/letsencrypt/letsencrypt.log
Plugins selected: Authenticator manual, Installer None
Starting new HTTPS connection (1): acme-v02.api.letsencrypt.org
Obtaining a new certificate
Performing the following challenges:
dns-01 challenge for eastforest.jp
dns-01 challenge for eastforest.jp

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Please deploy a DNS TXT record under the name
_acme-challenge.eastforest.jp with the following value:

dp0TFa40yRfcb_fc1Kof6jyJV56_KRcwdhdG-DxaR94

Before continuing, verify the record is deployed.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Press Enter to Continue
"dp0TFa40yRfcb_fc1Kof6jyJV56_KRcwdhdG-DxaR94" のように表示される文字列がトークンになりますので、表示されたトークンをドメイン管理しているDNSのTXTレコードに登録します。
txt _acme-challenge dp0TFa40yRfcb_fc1Kof6jyJV56_KRcwdhdG-DxaR94
DNSに登録したら、digでTXTレコードを確認します。
私はConoHaのDNSを利用しているので、ConoHaのDNSを指定して確認してます。
$ dig @ns-a1.conoha.io _acme-challenge.eastforest.jp txt +short
"dp0TFa40yRfcb_fc1Kof6jyJV56_KRcwdhdG-DxaR94"
DNSに登録したTXTレコードが確認できたら、"Press Enter to Continue"の画面に戻ってEnterで実行します。問題なければ以下のようなメッセージが表示されます。
IMPORTANT NOTES:
Congratulations! Your certificate and chain have been saved at:
/etc/letsencrypt/live/eastforest.jp-0001/fullchain.pem
Your key file has been saved at:
/etc/letsencrypt/live/eastforest.jp-0001/privkey.pem
Your cert will expire on 2019-04-27. To obtain a new or tweaked
version of this certificate in the future, simply run certbot
again. To non-interactively renew all of your certificates, run
"certbot renew"
If you like Certbot, please consider supporting our work by:
Donating to ISRG / Let's Encrypt: https://letsencrypt.org/donate
Donating to EFF: https://eff.org/donate-le
もともとワイルドカードなしのLet's Encrypt のフォルダがあったためか、あらたにディレクトリが作成されます。
ls -l /etc/letsencrypt/live/
total 4
drwxr-xr-x 2 root root 93 Dec 22 15:47 eastforest.jp
drwxr-xr-x 2 root root 93 Jan 27 16:13 eastforest.jp-0001
-rw-r--r-- 1 root root 740 Dec 22 15:47 README
また上記メッセージのようにcertbot renewで更新できるようです。
SSL/TLSサーバ証明書の有効期限は90日で、残り30日未満で更新が可能となるのはワイルドカードなしのときと同じです。
#certbot renew
Saving debug log to /var/log/letsencrypt/letsencrypt.log

Processing /etc/letsencrypt/renewal/eastforest.jp.conf

Cert not yet due for renewal

Processing /etc/letsencrypt/renewal/eastforest.jp-0001.conf

Cert not yet due for renewal

The following certs are not due for renewal yet:
/etc/letsencrypt/live/eastforest.jp/fullchain.pem expires on 2019-03-22 (skipped)
/etc/letsencrypt/live/eastforest.jp-0001/fullchain.pem expires on 2019-04-27 (skipped)
No renewals were attempted.
今回、ワイルドカードなしからワイルドカードありの証明書に変更して、ディレクトリパスが変わったので、Apacheの設定も変更しておきます。
# cp httpd-vhosts.conf{,.`date +"%Y%m%d"`}

# diff httpd-vhosts.conf.20190127 httpd-vhosts.conf
38,40c38,40
<   SSLCertificateFile "/etc/letsencrypt/live/eastforest.jp/cert.pem"
<   SSLCertificateKeyFile "/etc/letsencrypt/live/eastforest.jp/privkey.pem"
<   SSLCertificateChainFile "/etc/letsencrypt/live/eastforest.jp/chain.pem"
---
>   SSLCertificateFile "/etc/letsencrypt/live/eastforest.jp-0001/cert.pem"
>   SSLCertificateKeyFile "/etc/letsencrypt/live/eastforest.jp-0001/privkey.pem"
>   SSLCertificateChainFile "/etc/letsencrypt/live/eastforest.jp-0001/chain.pem"
Apache再起動します。
# systemctl restart httpd
ブラウザURLの鍵マークのところで有効期限を確認しておきます。問題なければDNS-01認証でDNSに登録したTXTレコードは消しても良いかなと思います。
前回同様、以下のようにcron仕込んで次回自動更新がされているか確認したいと思います。
certbot renew --pre-hook "systemctl stop httpd.service" --post-hook "systemctl start httpd.service"
以上です。
【2019/03/10 追記①】 自動更新について"certbot renew"だと更新可能期間にならないと処理がされないため、実際に自動更新が可能なのかわからない。 強制更新をドライランする方法で実際に自動更新が可能か確認することができます。
certbot renew --force-renewal --dry-run
これでわかったのは、"certbot renew"だけだとエラーになるということ。 ワイルドカードはDNS-01認証でトークンをDNSのTXTレコードに登録する必要があるので、やはりDNSに自動でTXTレコードを登録する仕組みを自前で用意する必要がありそうです。 このあたりはまた改めて試してみたいと思います。
【2019/03/10 追記②】 今回はワイルドカードなしを使っていたサーバに、ワイルドカードの設定を試してみましたが、更新時に参照されると思われる以下のディレクトリにワイルドカードなしのときの設定が残っているので、リネームして読み込まれないようにしておいた方がいいと思います。
# ls -l /etc/letsencrypt/renewal/
total 8
-rw-r--r-- 1 root root 587 Mar 10 22:59 eastforest.jp-0001.conf
-rw-r--r-- 1 root root 697 Mar 10 22:58 eastforest.jp.conf

# mv eastforest.jp.conf eastforest.jp.conf.bk

【2019/03/30 追記③】 追記①で「"certbot renew"だけだとエラーになる」と記載しましたが、初回SSL証明書取得のときに自動でDNS認証できるようにしておけば、その情報が /etc/letsencrypt/renewal/ 配下のファイルに記録されるので、以後 certbot renew だけで更新可能になるようです。 参考

ACME v2 とワイルドカード証明書の技術情報
Let’s Encrypt でワイルドカードを使う