FreeBSD 14
Sponsored Link

SSL 証明書を取得する (Let's Encrypt)2023/12/20

 
Let's Encrypt から 発行料 無料の SSL/TLS サーバー証明書を取得します。
Let's Encrypt は Linux Foundation の協業プロジェクトで、Web 全体の安全性を改善することをミッションに掲げています。
発行料 無料 とはいえ、あやしいものではありません。
Let's Encrypt の詳細は公式サイトを参照ください。
⇒ https://letsencrypt.org/
Let's Encrypt では 一般的な ドメイン認証 (DV) の証明書を無料で発行しています。
無料ですが Let's Encrypt の中間証明書は、大手認証局 (CA) のルート証明書によってクロス署名されているため、多くの主要 Web ブラウザーで信頼済みとして扱われます。
なお、一回の作業で得られる証明書の有効期限は 90日です。
ただし、Certbot パッケージには、Systemd の Timer が付属しているため、初回の取得以降の更新作業は自動で実行されます。
[1] 証明書を取得するためのツール Certbot クライアントをインストールします。
root@dlp:~ #
pkg install -y py39-certbot
[2] 証明書を取得します。
下例は Apache httpd や Nginx などの Web サーバーが稼働していることが前提となります。
作業を実施するサーバーで Web サーバーが稼働していない場合は [3] を参照ください。
また、インターネット側から、作業を実施するサーバー (証明書を取得したい FQDN のサーバー) の 80 ポート宛てにアクセス可能であることも前提です。
# [--webroot] 指定で稼働中 Web サーバーの公開ディレクトリ配下を認証用の一時領域に使用
# -w [ドキュメントルート] -d [証明書を取得したい FQDN]
# FQDN (Fully Qualified Domain Name) : ホスト名.ドメイン名を省略なしで表記
# ドキュメントルートはバーチャルホストで複数のホスト定義がある場合、該当するホスト定義のものを指定
# 証明書を取得したい FQDN が複数ある場合は、-d [証明書を取得したい FQDN] を複数指定
# 例 : srv.world/dlp.srv.world の 2つについて取得する場合
# ⇒ [-d srv.world -d dlp.srv.world]
root@dlp:~ #
certbot certonly --webroot -w /usr/local/www/apache24/data -d dns.srv.world

Saving debug log to /var/log/letsencrypt/letsencrypt.log
Enter email address (used for urgent renewal and security notices)
# 初回のみメールアドレスの登録と利用条件への同意が必要
# 受信可能なメールアドレスを指定
 (Enter 'c' to cancel): root@mail.srv.world

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Please read the Terms of Service at
https://letsencrypt.org/documents/LE-SA-v1.3-September-21-2022.pdf. You must
agree in order to register with the ACME server. Do you agree?
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# 利用条件に同意する
(Y)es/(N)o: Y

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Would you be willing, once your first certificate is successfully issued, to
share your email address with the Electronic Frontier Foundation, a founding
partner of the Let's Encrypt project and the non-profit organization that
develops Certbot? We'd like to send you email about our work encrypting the web,
EFF news, campaigns, and ways to support digital freedom.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# Certbot 開発の非営利団体 Electronic Frontier Foundation にもメールアドレスを登録するか否か
(Y)es/(N)o: Y
Account registered.
Requesting a certificate for dns.srv.world

Successfully received certificate.
Certificate is saved at: /usr/local/etc/letsencrypt/live/dns.srv.world/fullchain.pem
Key is saved at:         /usr/local/etc/letsencrypt/live/dns.srv.world/privkey.pem
This certificate expires on 2024-03-19.
These files will be updated when the certificate renews.

NEXT STEPS:
- The certificate will need to be renewed before it expires. Certbot can automatically renew the certificate in the background, but you may need to take steps to enable that functionality. See https://certbot.org/renewal-setup for instructions.

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
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
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# [Successfully received certificate] と表示さえれば成功
# メッセージ中に記載の通り [/etc/letsencrypt/live/(FQDN)/] 配下に証明書が取得されている

# cert.pem       ⇒ SSLサーバー証明書 (公開鍵含む)
# chain.pem      ⇒ 中間証明書
# fullchain.pem  ⇒ cert.pem と chain.pem が結合されたファイル
# privkey.pem    ⇒ 公開鍵に対する秘密鍵
[3] 作業を実施するサーバーで Webサーバーが稼働していない場合でも、Certbot の簡易 Web サーバー機能を使用して証明書の取得が可能です。 いずれにしろ、インターネット側から当作業を実施するサーバーの 80 ポート宛てにアクセス可能であることが前提となります。
# [--standalone] 指定で Certbot の簡易 Web サーバー機能を使用
# -d [証明書を取得したい FQDN]
# FQDN (Fully Qualified Domain Name) : [ホスト名.ドメイン名] を省略なしで表記
# 証明書を取得したい FQDN が複数ある場合は -d [証明書を取得したい FQDN] を複数指定
# 例 : [srv.world/mail.srv.world] について取得する場合 ⇒ [-d srv.world -d mail.srv.world]
root@dlp:~ #
certbot certonly --standalone -d rx-9.srv.world

Saving debug log to /var/log/letsencrypt/letsencrypt.log
Requesting a certificate for rx-9.srv.world

Successfully received certificate.
Certificate is saved at: /usr/local/etc/letsencrypt/live/rx-9.srv.world/fullchain.pem
Key is saved at:         /usr/local/etc/letsencrypt/live/rx-9.srv.world/privkey.pem
This certificate expires on 2024-03-19.
These files will be updated when the certificate renews.

NEXT STEPS:
- The certificate will need to be renewed before it expires. Certbot can automatically renew the certificate in the background, but you may need to take steps to enable that functionality. See https://certbot.org/renewal-setup for instructions.

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
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
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
[4] 取得済みの証明書をまとめて更新する場合は、[renew] サブコマンドを使用します。
有効期限が 30日未満の証明書が全て更新されます。
有効期限の残り日数に関わらず更新したい場合は [--force-renew] を合わせて指定します。
また、Certbot パッケージには更新スクリプトが付属しているため、そちらを有効にすることで自動更新にすることができます。
# 手動で更新する場合は以下

root@dlp:~ #
certbot renew
# パッケージには更新スクリプトが付属

root@dlp:~ #
ls -l /usr/local/etc/periodic/weekly/500.certbot-3.9

-r-xr-xr-x  1 root wheel 2494 Dec 13 07:53 /usr/local/etc/periodic/weekly/500.certbot-3.9

# 更新スクリプトを有効にする場合は以下

root@dlp:~ #
vi /etc/periodic.conf
# 新規作成
weekly_certbot_enable="YES"
[5] Windows 向けに PKCS12 (PFX) 形式に変換するには以下のように実行します。
root@dlp:~ #
openssl pkcs12 -export -in fullchain.pem -inkey privkey.pem -out dlp_for_iis.pfx

Enter Export Password:     # 任意のエクスポートパスワードを設定
Verifying - Enter Export Password:
関連コンテンツ