[Nginx]オレオレCA認証局を利用したクライアント証明書通信をする手順。

目次

記事の目的

Linuxサーバ(今回はNgixn)のクライアント証明書の通信に必要な、

  • クライアント証明書(.crtファイル、.pemファイル)
  • 秘密鍵(.keyファイル)
  • クライアント証明書署名要求(.csrファイル)
  • PKCS#12形式の秘密鍵+証明書ファイル (.pfxファイル)

を作成する。

この記事の内容を実施する前に以下の記事を必ず実施すること。

クライアント証明書通信に必要なファイル

SSL通信に必要なファイルは以下である。

  • クライアント証明書(clinet.crtファイル)
  • 秘密鍵(clinet.key)
  • クライアント証明書署名要求(clinet.csr)
  • PKCS#12形式の秘密鍵+証明書ファイル (clinet.pfx)
  • CA証明書(cacert.pem)

CA証明書は[Linux] Opensslを使用したオレオレCA証明局の作成で作成しているファイル。

クライアント証明書通信について理解が足りない場合は以下の証明書・秘密鍵・証明局の関係をきちんと理解することをおすすめする。

準備中

クライアント証明書の秘密鍵を作成

まずは通信を暗号化するための秘密鍵を作成する。

openssl genrsa -out client.key 2048

証明書署名要求を作成

CA認証局に依頼するための証明書署名要求(CSR)を作成する。

openssl req -new -key client.key -out client.csr

証明書情報は任意に入力すること。

You are about to be asked to enter information that will be incorporated
into your certificate request.
What you are about to enter is what is called a Distinguished Name or a DN.
There are quite a few fields but you can leave some blank
For some fields there will be a default value,
If you enter '.', the field will be left blank.
-----
Country Name (2 letter code) [AU]:JP
State or Province Name (full name) [Some-State]:Tokyo
Locality Name (eg, city) []:Minato-ku
Organization Name (eg, company) [Internet Widgits Pty Ltd]:TodoONada
Organizational Unit Name (eg, section) []:IT
Common Name (e.g. server FQDN or YOUR name) []:libproc.com
Email Address []:

Please enter the following 'extra' attributes
to be sent with your certificate request
A challenge password []:
An optional company name []:

CA署名したクライアント証明書を作成

[Linux] Opensslを使用したオレオレCA証明局の作成 のCA認証局で署名したサーバ証明書を作成。

openssl caで署名付きサーバ証明書作成する。

openssl ca -out client.crt -in client.csr

すると以下のようになる。

Using configuration from /usr/lib/ssl/openssl.cnf
Enter pass phrase for /etc/ssl/CA/private/cakey.pem:
Check that the request matches the signature
Signature ok
Certificate Details:
        Serial Number: 1 (0x1)
        Validity
            Not Before: Sep  7 10:27:52 2021 GMT
            Not After : Sep  7 10:27:52 2022 GMT
        Subject:
            countryName               = JP
            stateOrProvinceName       = Tokyo
            organizationName          = TodoONada
            organizationalUnitName    = IT
            commonName                = *.libproc
        X509v3 extensions:
            X509v3 Basic Constraints:
                CA:FALSE
            Netscape Comment:
                OpenSSL Generated Certificate
            X509v3 Subject Key Identifier:
                FF:11:11:22:33:44:55:66:AA:BB:7F:2E:18:7D:B7:71:B3:DB:C3:C2
            X509v3 Authority Key Identifier:
                keyid:FF:11:11:22:33:44:55:66:AA:BB:7F:2E:18:7D:B7:71:B3:DB:C3:C2

Certificate is to be certified until Sep  7 10:27:52 2022 GMT (365 days)
Sign the certificate? [y/n]:y

1 out of 1 certificate requests certified, commit? [y/n]y
Write out database with 1 new entries
Data Base Updated

PKCS#12形式の秘密鍵+証明書ファイルの作成

PKCS#12形式のファイルは秘密鍵と証明書を1つにまとめたファイルである。
このファイルはパスワードで保護する必要があり作成時にパスフレーズを入力する必要がある。

openssl pkcs12 -export -out client.pfx -inkey client.key -in client.crt -certfile /etc/ssl/CA/cacert.pem

Nginxでクライアント証明書を有効化する。

nginx.confに以下を設定。

server {
        ...
        ssl_client_certificate "/etc/ssl/CA/cacert.pem";
        ssl_verify_client on;
        ...

各種ファイルは以下に配置

PKCS#12ファイルをクライアント(ローカルPC)に設定

クライアント端末でclient.pfxファイルを実行すればWindows・Macともに証明書が有効化される。

さらに詳細を知りたい場合は、

  • Windows+クライアント証明書+インポート
  • Mac+クライアント証明書+インポート

たくさん情報が出てくるので参考にすること。

よかったらシェアしてね!
  • URLをコピーしました!
目次