認証局(CA)の構築
認証局構築にはopensslを利用します。opensslは、FreeのSSL構築環境を一通り提供しています。環境設定ファイルの所在等はFreeBSDをベースにしていますが、各自の環境に応じて読み換えてください。サーバ証明用に認証局を構築することを前提として、以下の設定をopenssl.cnfファイルに導入します。FreeBSDシステムでは、/usr/local/openssl/以下に存在します。
35 [ CA_default ]
36
37 dir = /usr/local/openssl/i-sys # Where everything is kept
123 [ req_distinguished_name ]
124 countryName = Country Name (2 letter code)
125 countryName_default = JP
126 countryName_min = 2
127 countryName_max = 2
128
129 stateOrProvinceName = State or Province Name (full name)
130 stateOrProvinceName_default = Ishikawa Pref.
131
132 localityName = Locality Name (eg, city)
133 localityName_default = Nanao City
134
135 0.organizationName = Organization Name (eg, company)
136 0.organizationName_default = Integrated Systems Co. Ltd.
137
138 # we can do this but it is not needed normally :-)
139 #1.organizationName = Second Organization Name (eg, company)
140 #1.organizationName_default = World Wide Web Pty Ltd
141
142 organizationalUnitName = Organizational Unit Name (eg, section)
143 organizationalUnitName_default = Network
169 # Here are some examples of the usage of nsCertType. If it is omitted
170 # the certificate can be used for anything *except* object signing.
171
172 # This is OK for an SSL server.
173 nsCertType = server
認証局の作成にはopenssl付属のCA.shを利用します。FreeBSDでports環境を利用してopensslを導入した場合には、
/usr/local/openssl/misc/CA.sh
にインストールされています。このスクリプトは内容を修正して利用しますので、認証局作成ごとにコピーして利用します。
以下手順では、
/usr/local/openssl/i-sys
以下に認証局を構築していきます。
# mkdir -p /usr/local/openssl/i-sys
# cd /usr/local/openssl/i-sys
# cp /usr/local/openssl/bin/CA.sh ./
コピーしたCA.sh内容を修正します。
CATOP=/usr/local/openssl/i-sys
CAKEY=./cakey.pem
CAREQ=./careq.pem
CACERT=./cacert.pem
各変数名と対応する内容は以下の通りです。
CATOP | 認証局構築ディレクトリ |
CAKEY | 秘密鍵ファイル名 |
CAREQ |
証明書署名要求ファイル名 |
CACERT | 証明書ファイル名 |
以上で準備は完了です。CA.shスクリプトを利用して認証局の情報を登録します。
ksakurai@nsa# ./CA.sh -newca
mkdir: /usr/local/openssl/i-sys: File exists
CA certificate filename (or enter to create) ← [Enter]を入力
Making CA certificate ...
Generating a 1024 bit RSA private key
....................................++++++
.............................++++++
writing new private key to '/usr/local/openssl/rso-sys/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) [JP]: ←国名を入力(日本は通常JP)
State or Province Name (full name) [Ishikawa]: ←都道府県名を入力
Locality Name (eg, city) [Nanao]: ←市区町村名を入力
Organization Name (eg, company) [Integrated Systems Inc.]: ←組織名を入力
Organizational Unit Name (eg, section) [Network]: ←部署名を入力
Common Name (eg, YOUR name) []:www.i-sys.biz ←サーバ名を入力
Email Address []:ksakurai@i-sys.biz ←連絡先E-Mailアドレスを入力
Please enter the following 'extra' attributes
to be sent with your certificate request
A challenge password []: ←特に入力なし
An optional company name []: ←特に入力なし
Using configuration from /usr/local/openssl/openssl.cnf
Enter pass phrase for /usr/local/openssl/rso-sys/private/./cakey.pem:
Check that the request matches the signature
Signature ok
正常に完了すると、指定ディレクトリ以下に一連のファイルが作成されます。
ksakurai@nsa# ls -la
total 24
drwxr-xr-x 6 root wheel 512 Nov 7 02:34 ./
drwxr-xr-x 9 root wheel 512 Nov 7 02:33 ../
-rwxr-xr-x 1 root wheel 3776 Nov 7 02:33 CA.sh*
-rw-r--r-- 1 root wheel 3319 Nov 7 02:34 cacert.pem
-rw-r--r-- 1 root wheel 729 Nov 7 02:34 careq.pem
drwxr-xr-x 2 root wheel 512 Nov 7 02:33 certs/
drwxr-xr-x 2 root wheel 512 Nov 7 02:33 crl/
-rw-r--r-- 1 root wheel 0 Nov 7 02:33 index.txt
drwxr-xr-x 2 root wheel 512 Nov 7 02:33 newcerts/
drwxr-xr-x 2 root wheel 512 Nov 7 02:33 private/
-rw-r--r-- 1 root wheel 3 Nov 7 02:33 serial
作成された証明書は、opensslコマンドを利用して内容確認できます。
ksakurai@nsa# openssl x509 -in cacert.pem -text
Certificate:
Data:
Version: 3 (0x2)
Serial Number: 0 (0x0)
Signature Algorithm: sha1WithRSAEncryption
Issuer: C=JP, ST=Ishikawa, O=Integrated Systems Inc., OU=Network, CN=i-sys/emailAddress=info@i-sys.biz
Validity
Not Before: Sep 30 06:43:06 2008 GMT
Not After : Sep 30 06:43:06 2011 GMT
Subject: C=JP, ST=Ishikawa, O=Integrated Systems Inc., OU=Network, CN=i-sys/emailAddress=info@i-sys.biz
Subject Public Key Info:
Public Key Algorithm: rsaEncryption
RSA Public Key: (1024 bit)
・・・
作成されたPEMフォーマットの証明書をバイナリDERフォーマットに変換する場合には以下コマンドを利用します。
ksakurai@nsa# openssl x509 -in cacert.pem -outform DER -out cacert.der
秘密鍵(KEY)の作成
Apacheでの利用を考えているため、以下作業は次のディレクトリで作業することを前提とします。
/usr/local/apache22/ssl
利用するサービス対象が違ったり、システムが異なる場合にはサーバ証明書を配置するディレクトリで作業を行ってください。事前にテストを行う場合には各ユーザのホームディレクトリ以下など適当な場所をご用意ください。
秘密鍵の作成は以下コマンドで行います。
ksakurai@nsa# openssl genrsa -des3 -out i-sys.key 1024
Generating RSA private key, 1024 bit long modulus
..++++++
.++++++
e is 65537 (0x10001)
Enter pass phrase for i-sys.key: ←秘密鍵パスワードを入力
Verifying - Enter pass phrase for i-sys.key: ←上記パスワードを再入力
ここで作成されたファイル(i-sys.key)が秘密鍵になります。この秘密鍵をApacheで利用しますが、パスワードを設定している場合Apache起動時にパスフレーズの入力が必要になります。パスワード入力を完了しない限りApacheが起動せず、サーバ起動時に起動プロセスが停止してしまいますので、パスフレーズの解除を以下手順で行います。
ksakurai@nsa# mv i-sys.key i-sys-withpass.key
ksakurai@nsa# openssl rsa -in i-sys-withpass.key -out i-sys.key
Enter pass phrase for rso-sys-withpass.key: ←秘密鍵パスワードを入力
writing RSA key
署名要求書(CSR)の作成
作成した秘密鍵をもとに署名要求書(CSR)を作成します。
ksakurai@nsa# openssl req -new -days 365 -key i-sys.key -out i-sys.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) [JP]: ←国名を入力(日本は通常JP)
State or Province Name (full name) [Ishikawa]: ←都道府県名を入力
Locality Name (eg, city) [Nanao]: ←市区町村名を入力
Organization Name (eg, company) [Integrated Systems Inc.]: ←組織名を入力
Organizational Unit Name (eg, section) [Network]: ←部署名を入力
Common Name (eg, YOUR name) []:www.i-sys.biz ←サーバ名を入力
Email Address []:ksakurai@i-sys.biz ←連絡先E-Mailアドレスを入力
Please enter the following 'extra' attributes
to be sent with your certificate request
A challenge password []: ←特に入力なし
An optional company name []: ←特に入力なし
署名要求書(i-sys.csr)が作成されていることを確認してください。
ksakurai@nsa# ls -la i-sys.csr
-rw-r--r-- 1 root wheel 729 Nov 7 03:23 i-sys.csr
認証局を利用した証明書発行
署名要求書(CSR)をもとに、証明書を認証局を利用して作成します。
ksakurai@nsa# cd /usr/local/openssl/i-sys
ksakurai@nsa# openssl ca -config /usr/local/openssl/openssl.cnf -in /usr/local/etc/apache22/ssl/i-sys.csr -keyfile private/cakey.pem -cert cacert.pem -out /usr/local/etc/apache22/ssl/i-sys.cert
Using configuration from /usr/local/openssl/openssl.cnf
Enter pass phrase for private/cakey.pem: ←秘密鍵パスワードを入力
Check that the request matches the signature
Signature ok
Certificate Details:
Serial Number: 3 (0x3)
Validity
Not Before: Nov 6 18:37:37 2008 GMT
Not After : Nov 6 18:37:37 2009 GMT
Subject:
countryName = JP
stateOrProvinceName = Ishikawa
organizationName = Integrated Systems Inc.
organizationalUnitName = Network
commonName = www.i-sys.com
emailAddress = ksakurai@i-sys.biz
X509v3 extensions:
X509v3 Basic Constraints:
CA:FALSE
Netscape Cert Type:
SSL Server
Netscape Comment:
OpenSSL Generated Certificate
X509v3 Subject Key Identifier:
F8:1B:8E:23:88:7A:6D:FF:B2:43:78:FE:4A:48:A6:AE:4A:C7:5B:E1
X509v3 Authority Key Identifier:
keyid:61:34:68:7E:A3:56:97:93:05:96:C5:61:F8:8F:34:BF:60:BD:41:BD
Certificate is to be certified until Nov 6 18:37:37 2009 GMT (365 days)
Sign the certificate? [y/n]:y ←[y]を入力
1 out of 1 certificate requests certified, commit? [y/n]y ←[y]を入力
Write out database with 1 new entries
Data Base Updated
以上により、証明書ファイルが作成されていることを確認してください。
証明書更新作業
サーバ証明書の有効期限が切れた場合は更新の必要があります。日付の更新のみを行うことができませんので、いったんデータベースから対象となる証明書を削除し、再登録を行います。証明書の削除手順は次の通りです。
# openssl ca -revoke /usr/local/etc/apache22/ssl/i-sys.cert
Enter pass phrase for /usr/local/openssl/i-sys/private/cakey.pem: ←秘密鍵パスワードを入力
Revoking Certificate 01.
Data Base Updated
RHEL系での作業手順
RHEL系ではCA構築用のスクリプト配置ディレクトリが以下のようになっています。適宜読み換えて作業を行ってください。
openssl.cnf |
/etc/pki/tls/openssl.cnf |
CA.sh |
/etc/pki/tls/misc/CA |