macOSでオレオレ認証局を立てて証明書を発行する

macOSでオレオレ証明書を発行します。
認証局用の秘密鍵と証明書を作成してオレオレ認証局を立ててから、証明したいドメイン(FQDN)の証明書を発行します。

事前準備

opensslインストール

brew install openssl

設定ファイルは/usr/local/etc/openssl/openssl.cnfです。

本来は、ここに色々設定を入れる必要があるのですが、おそらく設定した内容経緯をすぐ忘れそうなので、設定ファイルはいじらず、出来る限りコマンドラインオプションでなんとかする方針とします。

どうしてもコマンドラインで対応できなかったのが、serialファイルとindex.txtがあるディレクトリのパス指定です。 これは、openssl.confの中で./demoCAというディレクトリにあることになっていますので、デフォルト設定に従って予め

cd 【作業ディレクトリ】
mkdir demoCA
touch demoCA/index.txt
echo 00 > demoCA/serial

しておきます。

オレオレ認証局の秘密鍵作成

openssl genrsa -out ./ca.key 2048

オレオレ認証局のCSR作成

openssl req -new -key ca.key -out ca.csr -subj '/C=JP/ST=Tokyo/L=Shibuya-ku/O=Oreore CA inc./OU=Oreore Gr./CN=Oreore CA'

オレオレ認証局のCSRに署名して証明書作成

openssl x509 -days 3650 -in ./ca.csr -req -signkey ./ca.key -out ca.crt

example.comの鍵作成

openssl genrsa -out example.com.key 2048

example.comのCSR作成

openssl req -new -key example.com.key -out example.com.csr -subj '/C=JP/ST=Tokyo/L=Tokyo/O=Oreore CA inc./OU=example Gr./CN=example.com'

example.comのSSL証明書作成

yes | openssl ca -config <(cat /usr/local/etc/openssl/openssl.cnf <( printf "\n[usr_cert]\nsubjectAltName=DNS:example.com,DNS:hoge.example.com,DNS:fuga.example.com")) -keyfile ./ca.key -outdir ./ -cert ca.crt -in example.com.csr -out example.com.crt -days 3650

生成物

ファイル 説明
ca.crt オレオレ認証局の証明書
ca.csr オレオレ認証局のCSR
ca.key オレオレ認証局の秘密鍵
demoCA 設定ファイル格納ディレクトリ
example.com.crt example.comの証明書
example.com.csr example.comのCSR
example.com.key example.comの秘密鍵
00.pem example.comの証明書(ファイル名違いで2個出て来る)

参考URL

https://qiita.com/ll_kuma_ll/items/13c962a6a74874af39c6 https://security.stackexchange.com/questions/74345/provide-subjectaltname-to-openssl-directly-on-command-line