一部の情報は非常に古いもの(20年以上前〜)ですので、利用する際はご注意ください(Java 1.4 とか .NET 1.0 とか、Windows 2000 とか)
お問い合せは wiki@shise.net まで。Gmail に転送されるので、スパムは全部カットされます。
概要
Linux の OpenSSL を使って、CA とサーバ証明書を作ります。
自己証明のオレオレ証明書です。
環境
CentOS 4.6
又は
CentOS 5.2
準備
共通
作業ディレクトリを作成し、そこへ移動します。
mkdir cert cd cert
必要なディレクトリ、ファイルを作成します。
mkdir -p demoCA/{private,crl,certs,newcerts} touch demoCA/index.txt echo "01" > demoCA/serial
CentOS 5
次のコマンドも打ちます
echo "01" > demoCA/crlnumber
設定情報ファイルを用意して、設定を書き換えます。
CentOS 5 の場合は、次のように操作します。
vi /etc/pki/tls/openssl.cnf
dir = ../../CA
を
dir = ./demoCA
と書き換えて保存します。
続けて次のように操作します。
cp /etc/pki/tls/openssl.cnf ./openssl_ca.cnf vi ./openssl_ca.cnf
CentOS 4
次のように操作します。
cp /usr/share/ssl/openssl.cnf ./openssl_ca.cnf vi ./openssl_ca.cnf
共通
以下の項目の値を書き換えます(任意の値に書き換えます)
証明書の有効期限の初期値 default_days = 365 default_crl_days = 30
書き換え後(10年) default_days = 3650 default_crl_days = 3650
ファイルを保存して、エディタを終了します。
設定情報ファイルを複製します。
cp openssl_ca.cnf openssl_server.cnf
設定を書き換えます。
vi openssl_server.cnf
以下の項目を書き換えます。
証明書の種類の定義(未指定)
# nsCertType = server 変更内容(サーバー証明書として指定) nsCertType = server
ファイルを保存して、エディタを終了します。
CAの作成
CAの秘密鍵を作成します(一行で入力します)
openssl req -new -x509 -newkey rsa:2048 -out demoCA/cacert.pem -keyout demoCA/private/cakey.pem -config openssl_ca.cnf
次のように表示され、パスフレーズの入力を要求されます。
CAの秘密鍵のパスワードにしたい文字列を入力します(画面上には表示されません)
一度入力すると、確認のためもう一度入力を要求されます。
Generating a 2048 bit RSA private key .........+++ ...........................+++ writing new private key to 'demoCA/private/cakey.pem' Enter PEM pass phrase: Verifying - Enter PEM pass phrase:
パスフレーズに間違いが無ければ、次のように表示されます。
CA証明書に記載する情報の入力を求められます。
設定したい値を入力します
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) [JP]: JP State or Province Name (full name) [Tokyo]: Tokyo Locality Name (eg, city) [hoge]: hoge Organization Name (eg, company) [shise]: shise Organizational Unit Name (eg, section) [test]:test Common Name (eg, your name or your server's hostname) [myca]:myca Email Address []:
シンボリックリンクを作成します(一行で入力します)
ln -s ../cacert.pem demoCA/certs/`openssl x509 -noout -hash < demoCA/cacert.pem`.0
CRLを作成します
openssl ca -gencrl -out demoCA/crl.pem
CAのパスフレーズを要求されるので、CAの秘密鍵の作成時に入力したパスフレーズを入力します(画面上には表示されません)
Using configuration from /usr/share/ssl/openssl.cnf Enter pass phrase for ./demoCA/private/cakey.pem:
シンボリックリンクを作成します。
ln -s ../crl.pem demoCA/crl/`openssl crl -noout -hash < demoCA/crl.pem`.r0
以上で、CAの作成が完了しました。
CA証明書
demoCA/cacert.pem
CA CRL
demoCA/crl.pem
サーバー証明書の作成
サーバー証明書を作成します。
サーバーの秘密鍵を作成します。
openssl genrsa -des3 -out server_key.pem 1024
次のように表示され、パスフレーズの入力を要求されます。
サーバーの秘密鍵のパスワードにしたい文字列を入力します(画面上には表示されません)
一度入力すると、確認のためもう一度入力を要求されます。
Generating RSA private key, 1024 bit long modulus .................++++++ ..........++++++ e is 65537 (0x10001) Enter pass phrase for server_key.pem: Verifying - Enter pass phrase for server_key.pem:
秘密鍵は作成しましたが、このキーにはパスワードがかかっている状態です。
このまま使用すると、apache が立ち上がるたびにパスワードの入力が必要になります。
apache が、自動的に起動する事を前提にしている場合は、このままでは問題なので、秘密鍵のパスワードを無しにします。
openssl rsa -in server_key.pem -out server_key_nopass.pem
次のように表示され、サーバーの秘密鍵のパスフレーズの入力を要求されるので入力します。
Enter pass phrase for server_key.pem: writing RSA key
サーバー証明書を作成します。
次のコマンドを入力します
openssl req -new -key server_key_nopass.pem -out server_request.pem
サーバー証明書に記載する情報の入力を求められます。
CAの証明書作成時と同じ内容を入力します
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) [JP]: JP State or Province Name (full name) [Tokyo]: Tokyo Locality Name (eg, city) [hoge]: hoge Organization Name (eg, company) [shise]: shise Organizational Unit Name (eg, section) [test]: test Common Name (eg, your name or your server's hostname) [www.shise.net]: www.shise.net Email Address []: Please enter the following 'extra' attributes to be sent with your certificate request A challenge password []: An optional company name []:
CAの証明書と秘密鍵を使って、サーバー証明書を署名します。
次のコマンドを入力します
openssl ca -in server_request.pem -keyfile demoCA/private/cakey.pem -cert demoCA/cacert.pem -out server_cert.pem -config openssl_server.cnf
CAの秘密鍵のパスフレーズの入力を求められるので、入力します。
Using configuration from openssl_server.cnf Enter pass phrase for demoCA/private/cakey.pem:
パスフレーズを入力すると、サーバー証明書の情報が表示され、本当にCAで署名してもよいか確認が表示されます。「y」を入力します。
Check that the request matches the signature Signature ok Certificate Details: Serial Number: 4 (0x4) Validity Not Before: Nov 6 11:49:51 2008 GMT Not After : Nov 4 11:49:51 2018 GMT Subject: countryName = JP stateOrProvinceName = Tokyo organizationName = shise organizationalUnitName = test commonName = www.shise.net X509v3 extensions: X509v3 Basic Constraints: CA:FALSE Netscape Cert Type: SSL Server Netscape Comment: OpenSSL Generated Certificate X509v3 Subject Key Identifier: 71:C8:A8:0D:96:89:24:08:52:22:2F:4B:57:5A:C9:D4:27:9B:0D:AB X509v3 Authority Key Identifier: keyid:89:6D:33:19:42:56:59:82:D0:51:D6:A3:E5:CE:D6:7D:68:DD:14:CC DirName:/C=JP/ST=Tokyo/L=hoge/O=shise/OU=test/CN=www.shise.net serial:00
Certificate is to be certified until Nov 4 11:49:51 2018 GMT (3650 days) Sign the certificate? [y/n]:y
署名処理を完了させるかどうかを聞かれているので「y」を入力します。
1 out of 1 certificate requests certified, commit? [y/n]y Write out database with 1 new entries Data Base Updated
サーバー証明書が作成されました。
server_cert.pem server_key_nopass.pem