ApacheでSSL自己認証するための簡単な方法(サーバ認証) †OpenSSLで自己認証する手順のまとめ。少し順序立ててまとめてみた。 CA(認証局)作成しないとき †認証局は作成せずopne-sslをインストールしたときできたものを使用する 以下のファイルが作成時にできる。apacheで指定するファイルはserver.key 秘密鍵 (Private Key)とserver.crt サーバ証明書(CRT) の2つだけ。
基本手順 †
※普通の認証局は自分の秘密鍵で証明書署名要求ファイルに署名してサーバ証明書(server.crt)を作成し、返却する。 秘密鍵(Private Key)の作成(server.key作成) †RSA の秘密鍵を作成 $ openssl genrsa 2048 > server.key Generating RSA private key, 2048 bit long modulus ........+++ .....................................+++ e is 65537 (0x10001) 秘密鍵確認 $ openssl rsa -text < server.key Private-Key: (2048 bit) modulus: 00:b5:88:51:9a:04:05:d0:7d:21:92:c7:6f:d6:94: ee:00:13:b5:09:7f:ab:20:99:ac:63:08:30:d9:64: 6a:ec:71:4e:e5:2b:1c:3f:73:93:75:28:03:23:85: 95:bc:7b:27:84:f7:ba:e2:8c:1e:5a:76:a6:78:06: (略) B4GV6t0CgYAS3w2NtFde7zcLhgefFITShRItSTgZyyTtanf3AFdh+xzVKL6WzNZk yYNV7Dxa2jO/lnNNgGfqBT7bQ8AtmbNDEbOFG3+PoexNG389H5uACvE7Iu5w4dVJ msSMuIhmQLhytdQHRpAHWf6IUSWM52bFwnd+S6rg0Owb9J1LAHxcIQ== -----END RSA PRIVATE KEY----- 証明書署名要求(CSR; Certificate Signing Request)の作成(server.csr作成) †※通常ならば、server.csrを第三認証機関に送り、署名してもらう オレオレ証明書ということで自分で署名することにする。つまり、「server.csrはserver.keyから作ったけど、これをserver.keyで署名する」 暗号強度:sha256 $ openssl req -new -sha256 -key server.key > server.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]:Mie Locality Name (eg, city) []:Ise Organization Name (eg, company) [Internet Widgits Pty Ltd]:ism21 Organizational Unit Name (eg, section) []:Web Team Common Name (e.g. server FQDN or YOUR name) []:www.ism21.net Email Address []:okada@ism21.net Please enter the following 'extra' attributes to be sent with your certificate request A challenge password []: An optional company name []: 証明書署名要求(CSR)確認 $ openssl req -text < server.csr Certificate Request: Data: Version: 0 (0x0) Subject: C=JP, ST=Mie, L=Ise, O=ism21, OU=Web Team, CN=www.ism21.net/emailAddress=okada@ism21.net Subject Public Key Info: Public Key Algorithm: rsaEncryption Public-Key: (2048 bit) Modulus: 00:b5:88:51:9a:04:05:d0:7d:21:92:c7:6f:d6:94: ee:00:13:b5:09:7f:ab:20:99:ac:63:08:30:d9:64: 6a:ec:71:4e:e5:2b:1c:3f:73:93:75:28:03:23:85: (略) 1e:dd Exponent: 65537 (0x10001) Attributes: a0:00 Signature Algorithm: sha256WithRSAEncryption ←「sha256」になっている (略) EEvmKiyj6LHHervfYnYegKzb7QKxBVksRUz5EH61briiZrFgBnQD4uJi+4K5kZqk jKv2nN7IEoDPBLfIt4LjJy//cGYFzv/cVCU5aAR6cpwoy1/3aEGWRopKOLLy8w== -----END CERTIFICATE REQUEST----- server.csrをserver.keyで署名する(自己認証署名のため) †server.csr ⇒ server_nouseCA.crt(署名後) 6年間(2190日)有効 / 暗号強度:sha256 openssl x509 -sha256 -req -days 2190 -signkey (サーバ内の秘密鍵) < (証明書署名要求ファイル) > (認証局に認証された後のファイル) $ openssl x509 -sha256 -req -days 2190 -signkey server.key < server.csr > server_nouseCA.crt Signature ok subject=/C=JP/ST=Mie/L=Ise/O=ism21/OU=Web Team/CN=www.ism21.net/emailAddress=okada@ism21.net Getting Private key サーバ証明書確認 $ openssl x509 -noout -text -in server_nouseCA.crt Certificate: Data: Version: 1 (0x0) Serial Number: fc:eb:c0:fd:ff:55:f8:0f Signature Algorithm: sha256WithRSAEncryption Issuer: C=JP, ST=Mie, L=Ise, O=ism21, OU=Web Team, CN=www.ism21.net/emailAddress=okada@ism21.net Validity Not Before: Oct 16 01:16:16 2019 GMT Not After : Oct 14 01:16:16 2025 GMT Subject: C=JP, ST=Mie, L=Ise, O=ism21, OU=Web Team, CN=www.ism21.net/emailAddress=okada@ism21.net Subject Public Key Info: Public Key Algorithm: rsaEncryption Public-Key: (2048 bit) Modulus: 00:b5:88:51:9a:04:05:d0:7d:21:92:c7:6f:d6:94: ee:00:13:b5:09:7f:ab:20:99:ac:63:08:30:d9:64: (略) Exponent: 65537 (0x10001) Signature Algorithm: sha256WithRSAEncryption ←「sha256」になっている 5a:0c:83:4a:0d:39:84:53:2a:3a:1a:a3:1e:67:65:9d:b4:61: d3:fc:7f:5e:be:7f:6b:d2:d4:96:53:00:a2:52:f9:d8:07:ee: (略) /etc/apache2/conf.d/ssl.conf SSLCertificateFile /etc/apache2/conf/my_ssl_2019/server_nouseCA.crt SSLCertificateKeyFile /etc/apache2/conf/my_ssl_2019/server.key
CA(認証局)作成するとき †CA証明書/秘密鍵作成 †
認証局 10年間(3650日)有効 / 暗号強度 sha256 / 公開鍵暗号方式 RSA 2048bit $ openssl req -new -x509 -sha256 -newkey rsa:2048 -out cacert.pem -keyout cakey.pem -days 3650 Generating a 2048 bit RSA private key ..............................................................+++ .............+++ writing new private key to 'cakey.pem' Enter PEM pass phrase: Verifying - Enter PEM pass phrase: ----- 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]:Mie Locality Name (eg, city) []:Ise Organization Name (eg, company) [Internet Widgits Pty Ltd]:ism_CA_2019←証明書署名要求(CSR)と変更したほうがよいみたい Organizational Unit Name (eg, section) []:Web Team Common Name (e.g. server FQDN or YOUR name) []:www.ism21.net Email Address []:okada@ism21.net CA証明書の秘密鍵パスワード削除 $ openssl rsa -in cakey.pem -out cakey_nopass.pem Enter pass phrase for cakey.pem: writing RSA key CA証明書確認 $ openssl x509 -noout -text -inform pem -in cacert.pem Certificate: Data: Version: 3 (0x2) Serial Number: a3:05:9a:a4:6e:7f:d4:19 Signature Algorithm: sha256WithRSAEncryption Issuer: C=JP, ST=Mie, L=Ise, O=ism_CA_2019, OU=Web Team, CN=www.ism21.net/emailAddress=okada@ism21.net Validity Not Before: Oct 16 01:33:49 2019 GMT Not After : Oct 13 01:33:49 2029 GMT Subject: C=JP, ST=Mie, L=Ise, O=ism_CA_2019, OU=Web Team, CN=www.ism21.net/emailAddress=okada@ism21.net Subject Public Key Info: Public Key Algorithm: rsaEncryption Public-Key: (2048 bit) Modulus: 00:b3:48:c7:0d:ed:5b:3d:cd:e1:39:68:74:83:2b: c4:0a:64:0e:30:2e:b1:b7:5b:5b:88:fd:5a:37:13: (略) CA秘密鍵確認 $ ls cacert.pem cakey_nopass.pem server.key cakey.pem server.csr server_nouseCA.crt ←(認証局作成しないとき出来たファイル) $ openssl rsa -text < cakey_nopass.pem Private-Key: (2048 bit) modulus: 00:b3:48:c7:0d:ed:5b:3d:cd:e1:39:68:74:83:2b: c4:0a:64:0e:30:2e:b1:b7:5b:5b:88:fd:5a:37:13: 1e:e6:3a:84:3a:4b:ea:11:05:fb:e3:15:60:41:33: (略) server.csrを自己署名CA証明書と自己署名CA証明書の秘密鍵を使用して署名する †「自己認証CA局を作成し、使用する」ときは「自己認証CA局を作成しない」とき作成した証明書署名要求ファイルに署名する。 SANとは: SANsにサーバー名を指定することで、証明書のコモンネーム(CN)で指定されたサーバーとは別のWebサーバーでも、SSL/TLS通信が可能になる。 SANを設定するファイルを指定 複数ドメインに対しては「DNS:mz80.ism21.net ,DNS:www.ism21.net」のように指定する
マルチドメイン、 今回は1ドメインを指定 san.txt subjectAltName = DNS:www.ism21.net, DNS:ism.ism21.net
証明書 6年間(2190日)有効 openssl x509 -req -days 1825 -CAcreateserial -in (証明書署名要求ファイル) -CA (自己署名CA証明書) -CAkey (自己署名CA証明書の秘密鍵) -out (認証局に認証された後のファイル) -extfile (SANを設定するファイル) $ openssl x509 -req -sha256 -days 1825 -CAcreateserial -in server.csr -CA cacert.pem -CAkey cakey_nopass.pem -out server.crt -extfile san.txt Signature ok subject=/C=JP/ST=Mie/L=Ise/O=ism21/OU=Web Team/CN=www.ism21.net/emailAddress=okada@ism21.net Getting CA Private Key $ ls -lt 合計 40 -rw-r--r-- 1 okada okada 17 10月 29 14:06 cacert.srl ←こんなファイルが出来た -rw-r--r-- 1 okada okada 1359 10月 29 14:06 server.crt -rw-r--r-- 1 okada okada 54 10月 29 14:05 san.txt -rw-r--r-- 1 okada okada 1005 10月 16 14:49 ism_CA_2019.der -rw-r--r-- 1 okada okada 1675 10月 16 10:58 cakey_nopass.pem -rw-r--r-- 1 okada okada 1415 10月 16 10:33 cacert.pem -rw-r--r-- 1 okada okada 1834 10月 16 10:33 cakey.pem -rw-r--r-- 1 okada okada 1281 10月 16 10:16 server_nouseCA.crt -rw-r--r-- 1 okada okada 1045 10月 16 10:09 server.csr -rw-r--r-- 1 okada okada 1679 10月 16 10:06 server.key サーバ証明書確認 $ openssl x509 -noout -text -in server.crt Certificate: Data: Version: 3 (0x2) Serial Number: f9:80:e2:b1:e2:c9:f3:b9 Signature Algorithm: sha256WithRSAEncryption Issuer: C=JP, ST=Mie, L=Ise, O=ism_CA_2019, OU=Web Team, CN=www.ism21.net/emailAddress=okada@ism21.net Validity Not Before: Oct 29 05:06:19 2019 GMT Not After : Oct 27 05:06:19 2024 GMT Subject: C=JP, ST=Mie, L=Ise, O=ism21, OU=Web Team, CN=www.ism21.net/emailAddress=okada@ism21.net Subject Public Key Info: Public Key Algorithm: rsaEncryption Public-Key: (2048 bit) Modulus: 00:b5:88:51:9a:04:05:d0:7d:21:92:c7:6f:d6:94: (略) 1e:dd Exponent: 65537 (0x10001) X509v3 extensions: ←ここにSANがある X509v3 Subject Alternative Name: DNS:www.ism21.net, DNS:ism.ism21.net Signature Algorithm: sha256WithRSAEncryption 2d:3a:67:c9:92:09:36:10:f0:75:de:14:58:5f:21:47:68:f9: (略) /etc/apache2/conf.d/ssl.conf SSLCertificateFile /etc/apache2/conf/my_ssl_2019/server.crt SSLCertificateKeyFile /etc/apache2/conf/my_ssl_2019/server.key
Apche起動時にssl_error_logファイルに以下のエラーがある場合 「SSL Library Error: 185073780 error:0B080074:x509 certificate routines:X509_check_private_key:key values mismatch」 サーバ秘密鍵(server.keyの)modulus:とサーバ証明書(server.crt)のModulus:が一致しているか確認すること。 $ openssl x509 -noout -text -in server.crt -modulus Certificate: Data: Version: 1 (0x0) Serial Number: 9c:67:87:bf:ea:67:91:db Signature Algorithm: sha1WithRSAEncryption Issuer: C=JP, ST=Mie, L=Ise, O=ism21_CA_2019, OU=Web Team, CN=www.ism21.net/emailAddress=okada@ism21.net Validity Not Before: Oct 15 07:33:56 2019 GMT Not After : Oct 13 07:33:56 2024 GMT Subject: C=JP, ST=Mie, L=Ise, O=ism21, OU=Web Team, CN=www.ism21.net/emailAddress=okada@ism21.net Subject Public Key Info: Public Key Algorithm: rsaEncryption Public-Key: (2048 bit) Modulus: 00:c1:d7:92:da:9c:3c:fc:f6:c1:1d:40:c6:8b:31: 4d:c4:e5:cb:86:30:39:1d:a6:c5:d6:b7:46:4c:c6: f5:03:64:4e:05:f3:93:c1:ba:b0:99:3b:63:4a:4a: 3b:32:5c:18:fb:56:e4:60:b6:e3:be:d2:d7:53:33: $ openssl rsa -noout -text -in server.key -modulus Private-Key: (2048 bit) modulus: 00:c1:d7:92:da:9c:3c:fc:f6:c1:1d:40:c6:8b:31: 4d:c4:e5:cb:86:30:39:1d:a6:c5:d6:b7:46:4c:c6: f5:03:64:4e:05:f3:93:c1:ba:b0:99:3b:63:4a:4a: 3b:32:5c:18:fb:56:e4:60:b6:e3:be:d2:d7:53:33: 46:1e:ba:af:8f:64:dc:26:06:9b:fb:51:64:ac:c6: 5a:b5:6e:30:8e:62:fc:39:9f:00:08:c6:47:0c:f2: ブラウザにCA証明書を組み込む †クライアントのブラウザでページアクセス時に警告を出さないようにするために、「CA証明書(cacert.pem)」を取り込む ブラウザに取り込むためには「cacert.pem」をDER形式に変更する。 LinuxではPME形式で証明書をファイルに保存して取り扱うことが比較的多い(PEM自体はエンコードにBase64を用いるデータフォーマットであり、証明書以外のデータにも利用されている)。 PEMからDERに変換 $ openssl x509 -inform PEM -outform DER -in cacert.pem -out cacert.der DER形式のCA証明書確認 $ openssl x509 -noout -text -inform der -in cacert.der Certificate: Data: Version: 3 (0x2) Serial Number: cc:63:f5:98:9a:13:29:8a Signature Algorithm: sha1WithRSAEncryption Issuer: C=JP, ST=Mie, L=Ise, O=ism21, OU=Web Team, CN=www.ism21.net/emailAddress=okada@ism21.net Validity Not Before: Oct 9 07:07:48 2019 GMT Not After : Oct 7 07:07:48 2024 GMT Subject: C=JP, ST=Mie, L=Ise, O=ism21, OU=Web Team, CN=www.ism21.net/emailAddress=okada@ism21.net Subject Public Key Info: Public Key Algorithm: rsaEncryption Public-Key: (2048 bit) Modulus: 00:cc:71:3c:60:36:62:ed:f6:18:91:d2:f0:25:e0: 80:e4:04:a6:5c:e8:ac:c9:e3:f7:14:f3:45:d5:df: ブラウザでの証明書内容
参考 †RSA鍵、証明書のファイルフォーマットについて |