CLOVER🍀

That was when it all began.

OpenSSLでCSRを非インタラクティブに作成する

これは、なにをしたくて書いたもの?

OpenSSLでCSRを作る時に、Subjectの指定を求められます。

Country Name (2 letter code) [AU]:
State or Province Name (full name) [Some-State]:
Locality Name (eg, city) []:
Organization Name (eg, company) [Internet Widgits Pty Ltd]:
Organizational Unit Name (eg, section) []:
Common Name (e.g. server FQDN or YOUR name) []:
Email Address []:

これを非インタラクティブにやる方法はないのかな?ということでちょっと調べてみました。

-subjオプションを使うようです。

環境

今回の環境は、こちら。

$ openssl version
OpenSSL 3.0.2 15 Mar 2022 (Library: OpenSSL 3.0.2 15 Mar 2022)

ふつうにCSRを作成する

最初に、ふつうに証明書を作成してみます。この中でCSRも出てきます。

鍵の作成。

$ openssl genrsa -out sample.key 2048

CSRの作成。

$ openssl req -new -key sample.key -out sample.csr

適当に入力。

Country Name (2 letter code) [AU]:JP
State or Province Name (full name) [Some-State]:example
Locality Name (eg, city) []:example
Organization Name (eg, company) [Internet Widgits Pty Ltd]:example
Organizational Unit Name (eg, section) []:example
Common Name (e.g. server FQDN or YOUR name) []:example.com
Email Address []:hoge@example.com

自己署名。

$ openssl x509 -req -days 365 -in sample.csr -signkey sample.key -out sample.crt

この時出てくる情報。

subject=C = JP, ST = example, L = example, O = example, OU = example, CN = example.com, emailAddress = hoge@example.com

以下のコマンドでも確認できます。

$ openssl x509 -text -in sample.crt --noout

こんな感じで。

        Subject: C = JP, ST = example, L = example, O = example, OU = example, CN = example.com, emailAddress = hoge@example.com

CSRを非インタラクティブに作成する

次に、CSRを非インタラクティブに作成してみます。

鍵の作成。

$ openssl genrsa -out sample.key 2048

CSRの作成。この時に-subjオプションでSubjectを/[名前]=[値]の形式で指定できます。

$ openssl req -new -key sample.key -out sample.csr -subj '/C=JP/ST=example/L=example/O=example/OU=example/CN=example.com/emailAddress=hoge@example.com'

以下に書かれていますね。

Sets subject name for new request or supersedes the subject name when processing a certificate request.

The arg must be formatted as /type0=value0/type1=value1/type2=.... Special characters may be escaped by \ (backslash), whitespace is retained. Empty values are permitted, but the corresponding type will not be included in the request. Giving a single / will lead to an empty sequence of RDNs (a NULL-DN). Multi-valued RDNs can be formed by placing a + character instead of a / between the AttributeValueAssertions (AVAs) that specify the members of the set. Example:

/DC=org/DC=OpenSSL/DC=users/UID=123456+CN=John Doe

openssl-req

すべて指定する必要はなく、以下のようにどれかひとつでも指定すれば他は省略していると見なされます。

$ openssl req -new -key sample.key -out sample.csr -subj '/C=JP'


$ openssl req -new -key sample.key -out sample.csr -subj '/CN=example.com'

空にはできません。

$ openssl req -new -key sample.key -out sample.csr -subj ''
req: subject name is expected to be in the format /type0=value0/type1=value1/type2=... where characters may be escaped by \. This name is not in that format: ''

インタラクティブかつ全部未指定にしたい場合は、こんな感じで。

$ openssl req -new -key sample.key -out sample.csr -subj '/C=/ST=/L=/O=/OU=/CN=/emailAddress='
req: No value provided for subject name attribute "C", skipped
req: No value provided for subject name attribute "ST", skipped
req: No value provided for subject name attribute "L", skipped
req: No value provided for subject name attribute "O", skipped
req: No value provided for subject name attribute "OU", skipped
req: No value provided for subject name attribute "CN", skipped
req: No value provided for subject name attribute "emailAddress", skipped

これでも結局同じことなのですが。

$ openssl req -new -key sample.key -out sample.csr -subj '/C='
req: No value provided for subject name attribute "C", skipped

自己署名。

$ openssl x509 -req -days 365 -in sample.csr -signkey sample.key -out sample.crt

こんなところでしょうか。