0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

OpenSSL で複数のホスト名に対応した自己署名証明書を作成するには

Posted at

1 つのホスト名の場合は、-subj オプションで /CN にホスト名を指定して作成します。

$ openssl req -new -x509 -days 3650 -nodes -text -out test.crt -keyout test.key -subj /CN=$(hostname)
Generating a RSA private key
.................+++++
..................................................................+++++
writing new private key to 'test.key'
-----

複数のホスト名の場合は、req コマンドで秘密鍵を作成し、x509 コマンドで自己署名証明書を作成する際、拡張属性を読み込むファイルを -extfile オプションで指定し、v3_ca セクション内の subjectAltName 属性にホスト名は DNS:、IP アドレスは IP: をつけ、, (カンマ) 区切りで指定します。

ここでは、拡張属性を標準入力 /dev/stdin から読み込み、最初に標準の設定ファイル /etc/pki/tls/openssl.cnf (環境によって異なります) を出力し、それに追加する形で v3_ca セクション内の subjectAltName 属性にドメインつきのホスト名、ホスト名のみ、IP アドレスの設定を行っています。なお、コマンドの入力は EOF までになります。

$ openssl req -new -nodes -text -out test.csr -keyout test.key -subj /CN=$(hostname)
Generating a RSA private key
...............................+++++
.....................................................+++++
writing new private key to 'test.key'
-----
$ openssl x509 -req -in test.csr -text -days 3650 -extfile /dev/stdin -extensions v3_ca -signkey test.key -out test.crt <<EOF
$(cat /etc/pki/tls/openssl.cnf)
[ v3_ca ]
subjectAltName = DNS:$(hostname), DNS:$(hostname -s), IP:$(hostname -i)
EOF
Signature ok
subject=CN = node-1.example.com
Getting Private key
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?