認証局 (CA) を構築して体験してみるのが一番かもしれない、と思ってチャレンジしてみた。
(1) 何をするか?
今回、何をどうするのか少し整理しておきたい。
目標は、Web サーバーの SSL 化である。
まず、証明書を発行するまでの手順は以下のとおり。
+----+ +-------------+
| <-----(*1)-----+ |
| CA | | Webサーバー |
| +-----(*2)-----> |
+----+ +-------------+
1. まず、CA を構築する
2. Web サーバーの証明書署名要求 (CSR) を作成し、CA に提出 --- (*1)
3. CA は証明書を発行 --- (*2)
そして、Apache で SSL を使用する設定を行って完了である。
(2) CA 構築から証明書発行まで
【CA側】
まず、CA を構築する。
たとえば次のように実行する。
※Fedora Core では、/etc/pki/CA というディレクトリがすでに用意されていた。
# cd /etc/pki/CA
# openssl req -new -x509 -newkey rsa:1024 -keyout private/cakey.pem -out cacert.pem -days 365
Generating a 1024 bit RSA private key
................................++++++
...++++++
writing new private key to 'private/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) [GB]:JP
State or Province Name (full name) [Berkshire]:Tokyo
Locality Name (eg, city) [Newbury]:Shinjuku-ku
Organization Name (eg, company) [My Company Ltd]:K-ISHIK
Organizational Unit Name (eg, section) []:
Common Name (eg, your name or your server's hostname) []:camel.zoo.park
Email Address []:root@camel.zoo.park作成した証明書ファイル (cacert.pem) の内容は、
# openssl x509 -in cacert.pem -textで確認できる。
CA の準備はこれで完了。
【Webサーバー側】
ディレクトリを用意し、Web サーバー用の秘密鍵を作成する。
※Fedora Core では、/etc/pki/tls というディレクトリが用意されていた。
RSA の秘密鍵ファイルを生成する。
# cd /etc/pki/tls
# openssl genrsa -des3 -out private/serverkey.pem 1024(または、秘密鍵ファイルを暗号化しない場合は
-des3 オプションをつけない)# openssl genrsa -out private/serverkey.pem 1024
# chmod 0400 private/serverkey.pem生成される /etc/pki/tls/private/serverkey.pem が秘密鍵。
この秘密鍵を使って CSR を作成する。
# openssl req -new -key private/serverkey.pem -out server.csr
Enter pass phrase for private/serverkey.pem:
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) [GB]:JP
State or Province Name (full name) [Berkshire]:Tokyo
Locality Name (eg, city) [Newbury]:Shinjuku-ku
Organization Name (eg, company) [My Company Ltd]:K-ISHIK
Organizational Unit Name (eg, section) []:
Common Name (eg, your name or your server's hostname) []:puma.zoo.park
Email Address []:root@puma.zoo.park
Please enter the following 'extra' attributes
to be sent with your certificate request
A challenge password []:
An optional company name []:※Country Name などは CA と同じものにしておかなければならないらしい (CA と CSR を出すホストが同一の場合のみ?)
※また、Common Name は、Apache の設定ファイルで指定した ServerName と一致しないといけないらしい
生成された server.csr を CA にコピーする。
【CA側】
Web サーバーから受け取った CSR に対して証明書を発行する。
まずは、ディレクトリとファイルの作成。
※以下で作成するディレクトリとファイルは、OpenSSL の設定ファイル openssl.cnf の設定に合わせる。
# cd /etc/pki/CA
# mkdir newcerts
# touch index.txt
# echo 01 > serialそして、証明書の発行。
# openssl ca -in WebサーバーのCSRファイル -keyfile private/cakey.pem -cert cacert.pem -out ホスト名.pem
Using configuration from /etc/pki/tls/openssl.cnf
Enter pass phrase for cakey.pem:
Check that the request matches the signature
Signature ok
Certificate Details:
Serial Number: 1 (0x1)
Validity
Not Before: Jul 18 18:37:33 2007 GMT
Not After : Jul 17 18:37:33 2008 GMT
Subject:
countryName = JP
stateOrProvinceName = Tokyo
organizationName = K-ISHIK
commonName = puma.zoo.park
emailAddress = root@puma.zoo.park
X509v3 extensions:
X509v3 Basic Constraints:
CA:FALSE
Netscape Comment:
OpenSSL Generated Certificate
X509v3 Subject Key Identifier:
9A:41:36:27:DD:83:4D:04:05:12:09:07:AB:65:7F:5A:B4:C5:40:71
X509v3 Authority Key Identifier:
keyid:69:A9:92:64:4F:24:34:C8:85:B5:FD:92:AE:8A:53:76:3C:32:F6:86
Certificate is to be certified until Jul 17 18:37:33 2008 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生成された ホスト名.pem が証明書である。
(3) Apache の SSL 化
次に Apache を SSL 化する。
まず Web サーバーに、mod_ssl をインストールする。
パッケージによるが、たとえば Fedora Core の標準的な mod_ssl パッケージをインストールすると /etc/httpd/conf.d/ssl.conf に SSL 関連の設定が用意される。
SSLCertificateFile ディレクティブには cert.pem のパス (/etc/pki/tls/cert.pem) を、SSLCertificateKeyFile ディレクティブには serverkey.pem のパス (/etc/pki/tls/private/serverkey.pem) を指定する。
これで、Apache の SSL 化が完了する。
(4) 発行した証明書の無効化
発行した証明書を無効化するには、失効証明書 (CRL) を発行する。
# cd ~/CA/newcerts
# openssl ca -revoke シリアル番号.pem【変更履歴】
2010.10.15 WebサーバーのRSA秘密鍵ファイルを暗号化しない方法を追記

