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 5 years have passed since last update.

[OpenSSL]簡易版:OpenSSLを利用してクライアント証明書を作成する

Posted at

はじめに

簡便にクライアント証明書を作成したい場合の作業例です。
「簡便に」とは、WORKディレクトリのみで作業が完結することを意図しています。

Webサービスの呼び出しを行う際にクライアント認証を実装するケース等で利用することがある為、
個人的な備忘を含め、記事にしました。

1. 環境情報

# cat /etc/redhat-release
Red Hat Enterprise Linux Server release 7.4 (Maipo)
# openssl version
OpenSSL 1.0.2k-fips  26 Jan 2017

2. クライアント証明書作成

2.1. クライアント証明書を発行する認証局用の秘密鍵・証明書を作成

$ openssl req -x509 -new \
              -newkey rsa:2048 -keyout rootCA.key -nodes \
              -sha256 \
              -days 3650 \
              -out rootCA.crt \
              -subj "/C=JP/O=example.com/CN=EXAMPLE PKI ROOT"
  • 主要なオプションのメモ
オプション 説明
-x509 署名要求ではなく、証明書を発行
-days 3650 証明書の有効期間(例:10年間)
-newkey rsa:2048 秘密鍵を鍵長2048bit、RSA 暗号方式で生成
-nodes 出力する秘密鍵を暗号化しない
-sha256 ダイジェストアルゴリズム
-subj 証明書のサブジェクトを指定(利用しないと対話型で証明書のサブジェクトを定義する)

2.2. クライアント証明書用の秘密鍵・署名要求を作成

$ openssl req -newkey rsa:2048 -keyout client1.key -nodes -out client1.csr \
              -subj "/C=JP/O=example.com/CN=CLIENT1"

2.3. 認証局用秘密鍵/証明書でクライアント証明書の署名要求に署名

$ openssl x509 -req \
               -in client1.csr \
               -CA rootCA.crt -CAkey rootCA.key -CAcreateserial \
               -sha256 \
               -days 365 \
               -out client1.crt 
  • 主要なオプションのメモ
オプション 説明
-req 入力が署名要求でであり、署名し出力を行う
-CA rootCA.crt 認証局の証明書
-CAkey rootCA.key 認証局の秘密鍵
-CAcreateserial シリアルナンバーファイルを生成
-sha256 ダイジェストアルゴリズム
-days 365 クライアント証明書の有効期間(例:1年間)

2.3. クライアント証明書をPKCS#12形式でまとめる

$ openssl pkcs12 -export \
                 -out client1.pfx \
                 -inkey client1.key \
                 -in client1.crt \
                 -certfile rootCA.crt \
                 -passout pass:<password>
  • 主要なオプションのメモ
オプション 説明
-export PKCS#12形式で出力
-inkey client1.key クライアント証明書の秘密鍵
-in client1.crt クライアント証明書
-certfile rootCA.crt 認証局の証明書
-passout PKCS#12ファイルを保護するパスワード(ブラウザにインポートする際などに聞かれる)

おまけ

設定ファイルを作成し、opensslでの署名時にオプションを設定することで証明書へタイプ、鍵用途などの属性を付与できる。

$ openssl req ~省略~ -config openssl.cnf
$ openssl x509 ~省略~ -extfile openssl.cnf -extensions usr_cert
openssl.cnf
[ req ]
default_bits            = 2048
distinguished_name      = req_distinguished_name
x509_extensions         = v3_ca # The extentions to add to the self signed cert

[ req_distinguished_name ]

[ v3_ca ]
subjectKeyIdentifier   = hash
authorityKeyIdentifier = keyid:always,issuer:always
basicConstraints       = CA:true
keyUsage               = cRLSign, keyCertSign
nsCertType             = sslCA

[ usr_cert ]
basicConstraints       = CA:FALSE
nsCertType             = client
keyUsage               = nonRepudiation, digitalSignature, keyEncipherment
subjectKeyIdentifier   = hash
authorityKeyIdentifier = keyid,issuer
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?