かなりゆるーいインフラエンジニアのブログ

zawayaブログ

インフラエンジニアとして活動しています。現場での経験や技術的なことを思ったように書いていきます。

nginxを動かしてみる part3 -クライアント認証対応-

1.概要

サーバSSL対応に続き、クライアント証明書認証を試した時の備忘録

2.実行環境

ubuntu version 18.04 TLS
nginx version: nginx/1.14.0 (Ubuntu) built with OpenSSL 1.1.1 11 Sep 2018 OpenSSL 1.1.1 ※ユーザー証明書発行に必要

3.ユーザー証明書の発行

ユーザー証明書用の秘密鍵を作成

#mkdir ./client_certificate
#cd  ./client_certificate
#openssl genrsa -aes256 -out key.pem 2048

Generating RSA private key, 2048 bit long modulus (2 primes)
...............................................................+++++
.............................+++++
e is 65537 (0x010001)
Enter pass phrase for key.pem:
Verifying - Enter pass phrase for key.pem:

ユーザー証明書要求を作成

openssl req -new -key user1.key -out user1.csr
Enter pass phrase for user1.key:
Can't load /root/.rnd into RNG
140210030563776:error:2406F079:random number generator:RAND_load_file:Cannot open file:../crypto/rand/randfile.c:88:Filename=/root/.rnd
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) []:Komae
Organization Name (eg, company) [Internet Widgits Pty Ltd]:company,inc.
Organizational Unit Name (eg, section) []:system
Common Name (e.g. server FQDN or YOUR name) []:user1
Email Address []:

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

ユーザー証明書に署名

openssl x509 -req -days 365 -in user1.csr -CA /etc
/nginx/server_cert/example.pem -CAkey /etc/nginx/server_cert/key.pem -set_serial 01 -out user1.pem
Signature ok
subject=C = JP, ST = Tokyo, L = Komae, O = "company,inc.", OU = system, CN = user1
Getting CA Private Key
Enter pass phrase for /etc/nginx/server_cert/key.pem:

pfk12形式に変換

penssl pkcs12 -export -out user1.pfx -inkey user1.key -in user1.pem -certfile /etc/nginx/server_cert/example.pem
Enter pass phrase for user1.key:
Enter Export Password:
Verifying - Enter Export Password:

ここまでで、証明書保管フォルダ(/etc/nginx/client_certificate)には以下のファイルが存在

  • key.pem ※秘密鍵
  • user1.pem ※証明書
  • user1.csr ※証明書要求
  • user1.pfx ※pfx形式の証明書

user1.pfxをHOST_OS側に転送しておきます。

4.nginxの設定

serverブロックを書き換えます。今回完全SSL化としますのでhttp⇒httpsへのリダイレクトの設定も行います。

/etc/nginx/sites-available/example.com

server {
       listen 80 default;
       return 308 https://$host$request_uri;
       }

server {

        listen 443 ssl;

        server_name  example.com;
        root /var/www/html/;

        ssl_certificate /etc/nginx/server_cert/example.pem;
        ssl_certificate_key /etc/nginx/server_cert/key.pem;
        ssl_password_file /etc/nginx/server_cert/key_pass;
        ssl_protocols  TLSv1.1 TLS1.2;
        ssl_ciphers   "ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-GCM-SHA384:ECDHE-ECDSA-CHACHA20-POLY1305:ECDHE-RSA-CHACHA20-POLY1305:DHE-RSA-AES128-GCM-SHA256:DHE-RSA-AES256-GCM-SHA384";
        ssl_session_cache shared:SSL:10m;
        ssl_session_timeout 10m;

        ssl_client_certificate /etc/nginx/server_cert/example.pem;  #追加
        ssl_verify_client on; ※追加
#       ssl_verify_depth 1
#       ssl_crl

        location / {
               index index.html;
       }

       location /page1 {
               root /var/www/html_tmp/;
               index index.html;
       }
       location /page2 {
               root /var/www/html_tmp/;
               index index.html;
       }

}

--ssl_client_certificate--
認証局証明書の指定です。今回は自己署名証明書を指定しています。

--ssl_verify_client --
クライアント署名を有効化

--ssl_verify_depth--
サーバがクライアントの身元を確認するために証明書のチェーンを何階層までたどるかを設定。中間CA等を利用する場合。
※今回は使用しない。

--ssl_crl--
証明書の失効確認を行う場合には指定。 crlは定期的に更新する必要がある。
※今回は使用しない。

5.nginxを起動

nginxを再起動し、example.comへ変更を適用します。

systemctl restart nginx.service

起動確認のためホストOSから接続をしてみます。

  • 証明書なしで接続をしてみる。

f:id:blackstar01:20200504200005p:plain

  • 証明書をインストールした後に接続をしてみる。

使用する証明書の確認がブラウザ上で表示される⇒OK f:id:blackstar01:20200504201302p:plain

サイトに接続される。 f:id:blackstar01:20200504201219p:plain

6.まとめ

実際に利用する場合は、接続端末の紛失や、社員退職等による証明書失効確認を行う必要があり、CRLやOCPSとの連携も必要。 機械があれば試してみる。

7.参考リンク

究極のノマドを追求したらカギっ子管理者になってたオレオレ証明書の巻!! - Qiita

Nginxでクライアント証明書による認証を行う - Qiita

http://nginx.org/en/docs/http/ngx_http_ssl_module.html#ssl_client_certificate