○ 認証局と証明書の発行

・独自のCA (認証局)をつくり、証明書を発行します
・fedoraのパッケージをCentOS用にアレンジします
・openssl-1.0.0.cの場合です
・CA管理者とサーバ管理者と別けて考えます
・作成スクリプト (misc/CA.pl) を使います
・サーバ認証用の証明書を発行します

・ログイン端末で "#" はroot、"$" は一般ユーザのプロンプトの記号です
・設定ファイルで "#" で始まる行はコメントとして解釈されます

○参照ファイル

最初の設定 (Xen)
opensslのインストール

○作業環境

:OS CentOS 5.5
:selinux disabled or permissive
:作業 /etc/pki
:言語 LANG=en_US.UTF-8
:ユーザー  rootで実行



○スクリプトの修正

:/etc/pki/tls/misc/CA.pl (変更分)

・CA.plを使うにはopenssl-perlをインストールします
・サーバ証明書の有効期間を変更します(デフォルトでは1年)
・CA証明書の有効期間を変更します(デフォルトでは3年)
・# 記号はコメント (設定の無効) を示します
紫色部分 追加変更

・省略

$SSLEAY_CONFIG=$ENV{"SSLEAY_CONFIG"};
$DAYS="-days 730";
$CADAYS="-days 3650";
$REQ="$openssl req $SSLEAY_CONFIG";
$CA="$openssl ca $SSLEAY_CONFIG";
$VERIFY="$openssl verify";
$X509="$openssl x509";
$PKCS12="$openssl pkcs12";

・省略


○コンフィグファイルの修正

:/etc/pki/tls/openssl.cnf

・サーバ証明書の有効期間を変更します (デフォルトでは1年)
・CA.plで -days を使用しない場合に、こちらが使われます
・certタイプを server にします
・# 記号はコメント (設定の無効) を示します

[ CA_default ]
#default_days = 365
default_days = 730

[ usr_cert ]
#nsCertType = server
nsCertType = server

[ v3_ca ]
nsCertType = sslCA, emailCA



○ルートCA (認証局) の証明書と秘密鍵の作成

・CA管理者として作業します

# cd /etc/pki/tls/misc
[root@jupiter:/etc/pki/tls/misc]
# ./CA.pl -newca
CA certificate filename (or enter to create)← そのままEnterキー

Making CA certificate ...
Generating a 2048 bit RSA private key
....................+++
...........+++
writing new private key to '/etc/pki/CA/private/cakey.pem'
Enter PEM pass phrase:← CA管理者パスフレーズ
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) [XX]:JP
State or Province Name (full name) []:Saitama
Locality Name (eg, city) [Default City]:Mycity
Organization Name (eg, company) [Default Company Ltd]:ko-jiyaCA
Organizational Unit Name (eg, section) []:Admin
Common Name (eg, your name or your server's hostname) []:528p.com
Email Address []:designbox@528p.com

Please enter the following 'extra' attributes
to be sent with your certificate request
A challenge password []:.
An optional company name []:.
Using configuration from /etc/pki/tls/openssl.cnf
Enter pass phrase for /etc/pki/CA/private/cakey.pem:← CA管理者パスフレーズ

・省略

Write out database with 1 new entries
Data Base Updated       

・CAディレクトリ、証明書 (cacert.pem)、秘密鍵 (cakey.pem) がつくられます
・秘密鍵の扱いに注意 パーミッションを変更しておきます
・さらにサーバ証明書作成後、別の場所に保管したほうが安全です
・最初から cacert.pem,cakey.pem を作り直す場合は
・/etc/pki/CA に作られたファイルを削除します

# cd /etc/pki/CA
[root@jupiter:/etc/pki/CA]
# chmod 600 cacert.pem
# chmod 600 careq.pem
# chmod 600 private/cakey.pem
# chmod 600 newcerts/*.pem

・作成された証明書の確認

# cd /etc/pki/CA
[root@jupiter:/etc/pki/CA]
# openssl x509 -in cacert.pem -text



○サーバの公開鍵とCAへの申請書の作成

・サーバ管理者として作業します
・使用するサーバ毎に作ります(例 メールサーバ用)
・Common Name に証明書を使用するサーバのフルドメイン名を入れます

・注意 パスフレーズはrootのパスワードではありません

# cd /etc/pki/tls/misc
[root@jupiter:/etc/pki/tls/misc]
# ./CA.pl -newreq
Generating a 2048 bit RSA private key
.................+++
......+++
writing new private key to 'newreq.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) [XX]:JP
State or Province Name (full name) []:Saitama
Locality Name (eg, city) [Default City]:Mycity
Organization Name (eg, company) [Default Company Ltd]:ko-jiya528
Organizational Unit Name (eg, section) []:Admin
Common Name (eg, your name or your server's hostname) []:mail.528p.com
Email Address []:designbox@528p.com

Please enter the following 'extra' attributes
to be sent with your certificate request
A challenge password []:.
An optional company name []:.
Request is in newreq.pem, private key is in newkey.pem



○認証局によるサーバ申請書への署名

・CA管理者として作業します
・/etc/pki/tls/misc/newcert.pemが作られます
・時刻設定にUTCを使用している場合、ずれが生じます

# cd /etc/pki/tls/misc
[root@jupiter:/etc/pki/tls/misc]
# ./CA.pl -sign
Using configuration from /etc/pki/tls/openssl.cnf
Enter pass phrase for /etc/pki/CA/private/cakey.pem:← CA管理者のパスフレーズ
Check that the request matches the signature
Signature ok
Certificate Details:
        Serial Number:
            93:35:9a:96:aa:8e:90:10
        Validity
            Not Before: Jan 27 03:52:46 2011 GMT
            Not After : Jan 26 03:52:46 2013 GMT

・省略

Certificate is to be certified until Jan 26 03:52:46 2013 GMT (730 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
Signed certificate is in newcert.pem

・newreq.pem、newkey.pem、newcert.pemのパーミッションの変更
・/etc/pki/CA/newcerts/*.pemのパーミッションの変更

# cd /etc/pki/tls/misc
[root@jupiter:/etc/pki/tls/misc]
# chmod 600 newreq.pem
# chmod 600 newkey.pem
# chmod 600 newcert.pem
# chmod 600 ../../CA/newcerts/*.pem

・newreq.pem、newkey.pem、newcert.pem は次回作成時にコンフリクトします
・作業終了後、適当なディレクトリに保存し管理します



○サーバ用の証明書・公開鍵の使用

・サーバ用CA証明書 (cacert.crt) を作ります

# cd /etc/pki/tls
[root@jupiter:/etc/pki/tls]
# openssl x509 -in ../CA/cacert.pem -out cacert.crt

・CA証明書をバイナリDERフォーマットにします (ブラウザに読み込んで使用します)

# cd /etc/pki/tls
[root@jupiter:/etc/pki/tls]
# openssl x509 -inform pem -in ../CA/cacert.pem -outform der -out cacert.der

・newcert.pem からサーバ証明書 (mail-server.crt) を作ります

# cd /etc/pki/tls/misc
[root@jupiter:/etc/pki/tls/misc]
# openssl x509 -in newcert.pem -out mail-server.crt

・newkey.pem からパスフレーズを除いた公開鍵 (mail-server.key) を作ります
・サーバの自動起動でパスフレーズを要求されないためです

# cd /etc/pki/tls/misc
[root@jupiter:/etc/pki/tls/misc]
# openssl rsa -in newkey.pem -out mail-server.key
Enter pass phrase for newkey.pem:← サーバ管理者のパスフレーズ
writing RSA key

・パスフレーズを付けたければ、次のコマンドを実行してパスフレーズを指定します

# openssl rsa -des3 -in mail-server.key -out mail-server.key

・ファイルのパーミッション・グループの変更をします
・証明書と公開鍵をサーバで設定した場所にコピーします

# cd /etc/pki/tls/misc
[root@jupiter:/etc/pki/tls/misc]
# cp -a mail-server.crt ../certs/
# cp -a mail-server.key ../private/

・postfix(ユーザ・グループ)作成後に実行

# cd /etc/pki/tls/certs
[root@jupiter:/etc/pki/tls/certs]
# chmod 440 mail-server.crt
# chgrp postfix mail-server.crt
# cd ../private
[root@jupiter:/etc/pki/tls/private]
# chmod 440 mail-server.key
# chgrp postfix mail-server.key

○selinuxヘの対応 (selinuxが有効の場合)

# cd ~
[root@jupiter:~]
# restorecon -Rv /etc

○ Copyright(c) 528p.com All Rights Reserved.