TLS 証明書のサンプルを用意する#
テスト用に TLS 証明書を用意したい。本物の認証局に依頼すると費用がかかるため、手元で代わりのものを作成する。
基本方針#
本物の認証局で行うことになる手順を openssl コマンドで再現する。
openssl コマンドによる証明書作成#
ここでは、テスト用のサーバの証明書の作成を行うとした場合の手順を示す。作業ディレクトリ(場所は問わない)には、手順が成功すれば以下のファイルが生成される。
ファイル名 |
内容 |
---|---|
openssl-ca.cnf |
認証局の証明書発行用の設定ファイル |
ca.crt |
認証局の証明書 |
ca.key |
認証局の秘密鍵 |
ca.srl |
認証局が署名した証明書のシリアル番号 |
openssl-server.cnf |
サーバの証明書署名要求発行時の設定ファイル |
server.crt |
サーバの証明書 |
server.csr |
サーバの証明書署名要求 |
server.key |
サーバの秘密鍵 |
Caution
ここで作成するのは勝手に作成した証明書(いわゆる「オレオレ証明書」)のため、アプリケーションに認証局の証明書として登録しなければ使用できない。
Hint
複数の証明書を用意したい場合、2 件目以降は 3. ユーザ側の設定ファイルの作成から始めること。
認証局の設定ファイル作成
認証局の証明書の発行に使用する設定ファイルを作成する。ここでは、以下のような内容の openssl-ca.cnf ファイルを作成する。
[ req ] default_bits = 4096 default_keyfile = privkey.pem distinguished_name = req_distinguished_name attributes = req_attributes req_extensions = v3_ca prompt = no output_password = test dirstring_type = nobmp [ req_distinguished_name ] countryName = JP #countryName_default = AU #countryName_min = 2 #countryName_max = 2 localityName = Test organizationalUnitName = Test commonName = Test #commonName_max = 64 emailAddress = test@example.com #emailAddress_max = 40 [ req_attributes ] challengePassword = test #challengePassword_min = 4 #challengePassword_max = 20 [ v3_ca ] subjectKeyIdentifier=hash authorityKeyIdentifier=keyid:always,issuer:always basicConstraints = critical, CA:true
認証局の自己署名証明書と秘密鍵の作成
$ openssl req -config openssl-ca.cnf -new -x509 -days 3650 -keyout ca.key -out ca.crt Generating a RSA private key ..............................................................++++ ............................++++ writing new private key to 'ca.key' -----
ca.crt, ca.key が生成される。
ユーザ側の設定ファイルの作成
ユーザの証明書署名要求の生成に使用する設定ファイルを作成する。ここでは、以下のような内容の openssl-server.cnf ファイルを作成する。
[ req ] default_bits = 4096 default_keyfile = privkey.pem distinguished_name = req_distinguished_name attributes = req_attributes req_extensions = v3_ca prompt = no output_password = test dirstring_type = nobmp [ req_distinguished_name ] countryName = JP #countryName_default = AU #countryName_min = 2 #countryName_max = 2 localityName = Test organizationalUnitName = Test commonName = 127.0.0.1 #commonName_max = 64 emailAddress = test@example.com #emailAddress_max = 40 [ req_attributes ] challengePassword = test #challengePassword_min = 4 #challengePassword_max = 20 [ v3_ca ] subjectKeyIdentifier=hash
Important
commonName はサーバへアクセスする際の名前として使用されるもの。正しく入力しなければ認証に失敗する。
証明書署名要求の作成
$ openssl req -config openssl-server.cnf -new -keyout server.key -out server.csr Generating a RSA private key ......................++++ ......................................................++++ writing new private key to 'server.key' -----
server.csr, server.key が生成される。
証明書への署名
$ openssl x509 -req -in server.csr -CA ca.crt -CAkey ca.key -CAcreateserial -out server.crt -days 3650 -passin pass:test Signature ok subject=C = JP, L = Test, OU = Test, CN = 127.0.0.1, emailAddress = test@example.com Getting CA Private Key
ca.srl, server.crt が生成される。(ca.srl は発行済み証明書のシリアル番号一覧となっている。)
Hint
-passin オプションにある test は証明書のパスワード (output_password) として設定したもの。
証明書のチェック
$ openssl verify -CAfile ca.crt server.crt server.crt: OK