Archive for the ‘OpenSSL’ Category

5月
02

前回に引き続いてメールサーバーの設定を行っていきます。
今回はSSLの設定を行います。
flashcast:フリーで働くITエンジニア集団のブログ: 自宅サーバー再構築 その15 Apacheにサーバー証明書を導入するでWebサーバー用にサーバー証明書を作成済みですが、yasuの自宅サーバーはWebサーバー(www.sa-sa-ki.jp)とメールサーバー(mail.sa-sa-ki.jp)で名前が異なりますので、サーバー証明書もあえて作りわけたいと思います。

ちなみにメールサーバーにSSLを導入した場合の暗号化の範囲ですが、メールを送受信するクライアントと自宅サーバー間だけで、自宅サーバーから相手サーバー、相手サーバーから相手クライアントの間はSSLの暗号化対象ではありません。

自宅にあるPCから相手にメールを送る場合は緑線の1→2→3→4の順番でメールが送信されるのですが、暗号化されるのは1の区間だけです。
同様に外部のPCから相手にメールを送る場合は紫線のa→b→c→d→eの順番でメールが送信されるのですが、暗号化されるのはaとbの区間だけです。

OpenSSLの設定ファイル編集およびサーバー証明書作成準備

以前作成したWebサーバー用サーバー証明書作成ワークディレクトリ直下にメールサーバー用サーバー証明書作成ワークディレクトリを作成します。
[root@centos ~]# cd /etc/pki/tls/yasu
[root@centos yasu]# mkdir mailssl

以前作成したOpenSSLの設定ファイルをコピーして再利用します
[root@centos yasu]# cp openssl.cnf ./mailssl
[root@centos yasu]# cd mailssl
[root@centos mailssl]# ls -la
合計 32
drwxr-xr-x 2 root root 4096 4月 5 12:51 .
drw——- 3 root root 4096 4月 5 12:50 ..
-r——– 1 root root 9534 4月 5 12:51 openssl.cnf

中身が空っぽのテキストファイル index.txt と01と書いた serial ファイルを作成します
[root@centos mailssl]# touch index.txt
[root@centos mailssl]# echo 01 > serial
[root@centos mailssl]# ls -la
合計 44
drwxr-xr-x 2 root root 4096 4月 5 12:51 .
drw——- 3 root root 4096 4月 5 12:50 ..
-rw-r–r– 1 root root 0 4月 5 12:51 index.txt
-r——– 1 root root 9534 4月 5 12:51 openssl.cnf
-rw-r–r– 1 root root 3 4月 5 12:51 serial

サーバー秘密鍵作成
サーバー秘密鍵を作成します。

[root@centos mailssl]# openssl genrsa -out ./server.key -des3 1024
Generating RSA private key, 1024 bit long modulus
….++++++
……….++++++
e is 65537 (0×10001)
Enter pass phrase for ./server.key:任意のパスフレーズを入力
Verifying – Enter pass phrase for ./server.key:もう一度任意のパスフレーズを入力

メールサーバーにSSLでアクセスした時に毎回パスフレーズを聞かれないようにパスフレーズ付きサーバー秘密鍵を作成します
[root@centos mailssl]# openssl rsa -in ./server.key -out ./server.key
Enter pass phrase for ./server.key:上記パスフレーズを応答
writing RSA key

[root@centos mailssl]# ls -la
合計 52
drwxr-xr-x 2 root root 4096 4月 5 12:52 .
drw——- 3 root root 4096 4月 5 12:50 ..
-rw-r–r– 1 root root 0 4月 5 12:51 index.txt
-r——– 1 root root 9534 4月 5 12:51 openssl.cnf
-rw-r–r– 1 root root 3 4月 5 12:51 serial
-rw-r–r– 1 root root 963 4月 5 12:52 server.key

server.key というファイルが作成されました

CSR(証明書署名要求)作成
「サーバー秘密鍵作成」で作成したサーバー秘密鍵ファイルを使ってCSR(証明書署名要求)を作成します。

[root@centos mailssl]# openssl req -new -days 3650 -config ./openssl.cnf -key ./server.key -out ./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) [GB]:JP
State or Province Name (full name) [Berkshire]:chiba
Locality Name (eg, city) [Newbury]:urayasu
Organization Name (eg, company) [My Company Ltd]:sa-sa-ki.jp
Organizational Unit Name (eg, section) []:何も入力しないで[Enter]キーを押下
Common Name (eg, your name or your server’s hostname) []:mail.sa-sa-ki.jp
Email Address []:何も入力しないで[Enter]キーを押下

Please enter the following ‘extra’ attributes
to be sent with your certificate request
A challenge password []:何も入力しないで[Enter]キーを押下
An optional company name []:何も入力しないで[Enter]キーを押下
[root@centos mailssl]# ls -la
合計 60
drwxr-xr-x 2 root root 4096 4月 5 12:53 .
drw——- 3 root root 4096 4月 5 12:50 ..
-rw-r–r– 1 root root 0 4月 5 12:51 index.txt
-r——– 1 root root 9534 4月 5 12:51 openssl.cnf
-rw-r–r– 1 root root 3 4月 5 12:51 serial
-rw-r–r– 1 root root 639 4月 5 12:53 server.csr
-rw-r–r– 1 root root 887 4月 5 12:52 server.key

server.csr というファイルが作成されました

CSRに署名
CSRファイルに署名を行い、サーバー証明書を作成します。

[root@centos mailssl]# openssl ca -config ./openssl.cnf -days 3650 -in ./server.csr -keyfile ../cakey.pem -cert ../cacert.pem -out ./server.pem
※サーバー証明書を作成するのに必要な認証局の秘密鍵と認証局の証明書は以前Webサーバーのサーバー証明書を作成した時に作ったものを指定します
Using configuration from ./openssl.cnf
Enter pass phrase for ../cakey.pem:以前Webサーバーのサーバー証明書を作成した時に作った認証局のパスフレーズを応答
Check that the request matches the signature
Signature ok
Certificate Details:
Serial Number: 1 (0×1)
Validity
Not Before: Apr 5 04:01:14 2009 GMT
Not After : Apr 3 04:01:14 2019 GMT
Subject:
countryName = JP
stateOrProvinceName = chiba
organizationName = sa-sa-ki.jp
commonName = mail.sa-sa-ki.jp
X509v3 extensions:
X509v3 Basic Constraints:
CA:FALSE
Netscape Comment:
OpenSSL Generated Certificate
X509v3 Subject Key Identifier:
38:88:E0:47:C2:D6:C0:2F:C7:95:8A:DF:D1:BB:76:61:AB:47:CC:96
X509v3 Authority Key Identifier:
keyid:FE:40:F5:F1:60:78:3F:F4:BF:63:2B:FE:DE:CE:D9:32:5B:44:05:52

Certificate is to be certified until Apr 3 04:01:14 2019 GMT (3650 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

各ファイルをrootユーザ読み取り専用に権限を変更します
[root@centos mailssl]# chmod 400 *
[root@centos mailssl]# ls -l
合計 48
-r——– 1 root root 0 4月 5 12:51 index.txt
-r——– 1 root root 9534 4月 5 12:51 openssl.cnf
-r——– 1 root root 0 4月 5 12:55 serial
-r——– 1 root root 639 4月 5 12:53 server.csr
-r——– 1 root root 887 4月 5 12:52 server.key
-r——– 1 root root 830 4月 5 12:54 server.pem

server.pemというファイルが作成されました

サーバー証明書ファイルとサーバー秘密鍵ファイルコピー
サーバー証明書ファイルとサーバー秘密鍵ファイルを所定の場所にコピーします。

[root@centos mailssl]# cp server.key /etc/postfix
[root@centos mailssl]# cp server.pem /etc/postfix
[root@centos mailssl]# cp server.key /etc/pki/dovecot
[root@centos mailssl]# cp server.pem /etc/pki/dovecot

Postfix(送信サーバー)のSSL用設定ファイルを変更
Postfix(送信サーバー)用設定ファイルを変更します。

[root@centos mailssl]# cd
[root@centos ~]# vi /etc/postfix/main.cf

viエディタが起動するので追加します

# SMTP SSL
smtpd_use_tls = yes
smtpd_tls_cert_file = /etc/postfix/server.pem
smtpd_tls_key_file = /etc/postfix/server.key
smtpd_tls_session_cache_database = btree:/etc/postfix/smtpd_scache

内容を保存しviエディタを終了します

[root@centos ~]# vi /etc/postfix/master.cf

viエディタが起動するので修正します

#
# Postfix master process configuration file. For details on the format
# of the file, see the master(5) manual page (command: “man 5 master”).
#
# ==========================================================================
# service type private unpriv chroot wakeup maxproc command + args
# (yes) (yes) (yes) (never) (100)
# ==========================================================================
smtp inet n – n – – smtpd
#submission inet n – n – – smtpd
# -o smtpd_enforce_tls=yes
# -o smtpd_sasl_auth_enable=yes
# -o smtpd_client_restrictions=permit_sasl_authenticated,reject
以下4行の先頭の#を外してコメントを解除します
smtps inet n – n – – smtpd
-o smtpd_tls_wrappermode=yes
-o smtpd_sasl_auth_enable=yes
-o smtpd_client_restrictions=permit_sasl_authenticated,reject

#628 inet n – n – – qmqpd
pickup fifo n – n 60 1 pickup
cleanup unix n – n – 0 cleanup
qmgr fifo n – n 300 1 qmgr
#qmgr fifo n – n 300 1 oqmgr
tlsmgr unix – – n 1000? 1 tlsmgr
rewrite unix – – n – – trivial-rewrite
bounce unix – – n – 0 bounce
defer unix – – n – 0 bounce
trace unix – – n – 0 bounce
verify unix – – n – 1 verify
flush unix n – n 1000? 0 flush
proxymap unix – – n – – proxymap
smtp unix – – n – – smtp
# When relaying mail as backup MX, disable fallback_relay to avoid MX loops
relay unix – – n – – smtp
-o fallback_relay=
# -o smtp_helo_timeout=5 -o smtp_connect_timeout=5
showq unix n – n – – showq
error unix – – n – – error
discard unix – – n – – discard
local unix – n n – – local
virtual unix – n n – – virtual
lmtp unix – – n – – lmtp
anvil unix – – n – 1 anvil
scache unix – – n – 1 scache

内容を保存しviエディタを終了します

Dovecot(受信サーバー)のSSL用設定ファイルを変更
Dovecot(受信サーバー)用設定ファイルを変更します。

[root@centos ~]# vi /etc/dovecot.conf

viエディタが起動するので追加します

# Disable SSL/TLS support.
#ssl_disable = no
ssl_disable = no

# PEM encoded X.509 SSL/TLS certificate and private key. They’re opened before
# dropping root privileges, so keep the key file unreadable by anyone but
# root. Included doc/mkcert.sh can be used to easily generate self-signed
# certificate, just make sure to update the domains in dovecot-openssl.cnf
#ssl_cert_file = /etc/pki/dovecot/certs/dovecot.pem
#ssl_key_file = /etc/pki/dovecot/private/dovecot.pem
ssl_cert_file = /etc/pki/dovecot/server.pem
ssl_key_file = /etc/pki/dovecot/server.key

内容を保存しviエディタを終了します

ファイアウォールの設定変更

[root@centos ~]# vi /etc/sysconfig/iptables

viエディタが起動するので追加します

# Firewall configuration written by system-config-securitylevel
# Manual customization of this file is not recommended.
*filter
:INPUT ACCEPT [0:0]
:FORWARD ACCEPT [0:0]
:OUTPUT ACCEPT [0:0]
:RH-Firewall-1-INPUT – [0:0]
-A INPUT -j RH-Firewall-1-INPUT
-A FORWARD -j RH-Firewall-1-INPUT
-A RH-Firewall-1-INPUT -i lo -j ACCEPT
-A RH-Firewall-1-INPUT -p icmp –icmp-type any -j ACCEPT
-A RH-Firewall-1-INPUT -p 50 -j ACCEPT
-A RH-Firewall-1-INPUT -p 51 -j ACCEPT
-A RH-Firewall-1-INPUT -p udp –dport 5353 -d 224.0.0.251 -j ACCEPT
-A RH-Firewall-1-INPUT -p udp -m udp –dport 631 -j ACCEPT
-A RH-Firewall-1-INPUT -p tcp -m tcp –dport 631 -j ACCEPT
-A RH-Firewall-1-INPUT -m state –state ESTABLISHED,RELATED -j ACCEPT
-A RH-Firewall-1-INPUT -m state –state NEW -m tcp -p tcp –dport 22 -j ACCEPT
-A RH-Firewall-1-INPUT -m state –state NEW -m tcp -p tcp –dport 25 -j ACCEPT
-A RH-Firewall-1-INPUT -m state –state NEW -m tcp -p tcp –dport 53 -j ACCEPT
-A RH-Firewall-1-INPUT -m state –state NEW -m udp -p udp –dport 53 -j ACCEPT
-A RH-Firewall-1-INPUT -m state –state NEW -m tcp -p tcp –dport 80 -j ACCEPT
-A RH-Firewall-1-INPUT -m state –state NEW -m tcp -p tcp –dport 110 -j ACCEPT
-A RH-Firewall-1-INPUT -m state –state NEW -m tcp -p tcp –dport 123 -j ACCEPT
-A RH-Firewall-1-INPUT -m state –state NEW -m udp -p udp –dport 123 -j ACCEPT
-A RH-Firewall-1-INPUT -m state –state NEW -m tcp -p tcp –dport 143 -j ACCEPT
-A RH-Firewall-1-INPUT -m state –state NEW -m tcp -p tcp –dport 443 -j ACCEPT
下記6行を追加してTCPの465番ポート(smtps)、993番ポート(imaps)、995番ポート(pop3s)を開放します
IP制御をしてローカルネットワーク内と特定の外部からのみ接続できるようにします

-A RH-Firewall-1-INPUT -m state –state NEW -m tcp -p tcp –dport 465 -s 192.168.0.0/24 -j ACCEPT
-A RH-Firewall-1-INPUT -m state –state NEW -m tcp -p tcp –dport 465 -s xxx.xxx.xx.xx(外部IPアドレス) -j ACCEPT
-A RH-Firewall-1-INPUT -m state –state NEW -m tcp -p tcp –dport 993 -s 192.168.0.0/24 -j ACCEPT
-A RH-Firewall-1-INPUT -m state –state NEW -m tcp -p tcp –dport 993 -s xxx.xxx.xx.xx(外部IPアドレス) -j ACCEPT
-A RH-Firewall-1-INPUT -m state –state NEW -m tcp -p tcp –dport 995 -s 192.168.0.0/24 -j ACCEPT
-A RH-Firewall-1-INPUT -m state –state NEW -m tcp -p tcp –dport 995 -s xxx.xxx.xx.xx(外部IPアドレス) -j ACCEPT

-A RH-Firewall-1-INPUT -j REJECT –reject-with icmp-host-prohibited
COMMIT

内容を保存しviエディタを終了します

設定内容を反映します
[root@centos ~]# /etc/rc.d/init.d/iptables restart
ファイアウォールルールを適用中: [ OK ]
チェインポリシーを ACCEPT に設定中filter [ OK ]
iptables モジュールを取り外し中 [ OK ]
iptables ファイアウォールルールを適用中: [ OK ]
iptables モジュールを読み込み中ip_conntrack_netbios_ns [ OK ]

ルータの設定を変更変更
ルータの設定を変更してTCPの465番ポート(smtps)、993番ポート(imaps)、995番ポート(pop3s)を開放します。
開放方法はルータの設定手順に従って実施してください。

次回はThunderbirdの設定を行い、メールの送受信を行います。

■ 参考サイト ■
flashcast:フリーで働くITエンジニア集団のブログ: 自宅サーバー再構築 その15 Apacheにサーバー証明書を導入する
メールサーバー間通信内容暗号化(OpenSSL+Postfix+Dovecot) – CentOSで自宅サーバー構築
はじめての自宅サーバ構築 Fedora/CentOS – 通信内容暗号化(OpenSSL & mod_SSL) 〜 メール編 〜

————————————————————————————————–
flashcast:フリーで働くITエンジニア集団のブログ: 自宅サーバー再構築 その21 CentOS 5.3 メールサーバー構築(Dovecot設定編)
flashcast:フリーで働くITエンジニア集団のブログ: 自宅サーバー再構築 その20 CentOS 5.3 メールサーバー構築(Postfix設定編)
flashcast:フリーで働くITエンジニア集団のブログ: 自宅サーバー再構築 その19 CentOS 5.3 アンチウィルスソフトのバージョンアップ
flashcast:フリーで働くITエンジニア集団のブログ: 自宅サーバー再構築 その18 CentOS 5.3 にNTPサーバーを導入して自動時間合わせ
flashcast:フリーで働くITエンジニア集団のブログ: 自宅サーバー再構築 その17 WordPressのセキュリティ対策
flashcast:フリーで働くITエンジニア集団のブログ: 自宅サーバー再構築 番外編その1 CentOS 5.3 のリリースと導入済みソフトウェアのアップデート
flashcast:フリーで働くITエンジニア集団のブログ: 自宅サーバー再構築 その16 CentOS 5.2 にWordPressを導入してブログサイトを構築する
flashcast:フリーで働くITエンジニア集団のブログ: 自宅サーバー再構築 その15 Apacheにサーバー証明書を導入する
flashcast:フリーで働くITエンジニア集団のブログ: 自宅サーバー再構築 その14 CentOS 5.2 にLAMP環境構築(MySQL編)
flashcast:フリーで働くITエンジニア集団のブログ: 自宅サーバー再構築 その13 CentOS 5.2 にLAMP環境構築(Apache、PHP編)
flashcast:フリーで働くITエンジニア集団のブログ: 自宅サーバー再構築 その12 CentOS 5.2 にDNSサーバーを導入して名前解決(確認編)
flashcast:フリーで働くITエンジニア集団のブログ: 自宅サーバー再構築 その11 CentOS 5.2 にDNSサーバーを導入して名前解決(設定編)
flashcast:フリーで働くITエンジニア集団のブログ: 自宅サーバー再構築 その10 CentOS 5.2 SSHサーバーを構築して外部からリモート接続&リモートファイル転送
flashcast:フリーで働くITエンジニア集団のブログ: 自宅サーバー再構築 その9 CentOS 5.2 にバッファオーバーフロー攻撃対策を行う
flashcast:フリーで働くITエンジニア集団のブログ: 自宅サーバー再構築 その8 CentOS 5.2 にrootkit検知ツールを導入する
flashcast:フリーで働くITエンジニア集団のブログ: 自宅サーバー再構築 その7 CentOS 5.2 にファイル改竄検知システムを導入する
flashcast:フリーで働くITエンジニア集団のブログ: 自宅サーバー再構築 その6 CentOS 5.2 にアンチウィルスソフトを導入する
flashcast:フリーで働くITエンジニア集団のブログ: 自宅サーバー再構築 その5 CentOS 5.2 ファイル転送準備
flashcast:フリーで働くITエンジニア集団のブログ: 自宅サーバー再構築 その4 CentOS 5.2 インストール後の初期設定
flashcast:フリーで働くITエンジニア集団のブログ: 自宅サーバー再構築 その3 インストールしたCentOS 5.2 にリモート接続準備
flashcast:フリーで働くITエンジニア集団のブログ: 自宅サーバー再構築 その2 CentOS 5.2 をインストールする
flashcast:フリーで働くITエンジニア集団のブログ: 自宅サーバー再構築 その1 導入を検討する

3月
30

今回はApacheにサーバー証明書を導入してみたいと思います。
以前IISでも同じような事をやりましたが、今回はそれのApache版といったところでしょうか。

なおApacheでSSL通信を行う場合、mod_sslというモジュールが必要になりますのでインストールされてない方は
yum -y install mod_ssl を実行してインストールしてください。
yasuはLAMP環境構築の時に一緒にインストールしているので、ここではインストール手順を割愛します。

1.OpenSSLの設定ファイル編集およびサーバー証明書作成準備

サーバー証明書作成用作業ディレクトリを作成します
[root@centos ~]# mkdir /etc/pki/tls/yasu
サーバー証明書作成用作業ディレクトリはrootユーザのみ読み書きできるように権限を変更します
[root@centos ~]# chmod 600 /etc/pki/tls/yasu

OpenSSLの設定ファイルを編集します
/etc/pki/tls直下にOpenSSLの設定ファイルがあるので、それを/etc/pki/tls/yasuにコピーします

[root@centos ~]# cp -p /etc/pki/tls/openssl.cnf /etc/pki/tls/yasu

/etc/pki/tls/yasuにコピーしたOpenSSLの設定ファイルをviエディタで開き、以下の箇所を編集します
[root@centos ~]# cd /etc/pki/tls/yasu
[root@centos yasu]# vi openssl.cnf
■修正前
[37~39行目]
dir = ./demoCA # Where everything is kept
certs = $dir/certs # Where the issued certs are kept
crl_dir = $dir/crl # Where the issued crl are kept
[43行目]
new_certs_dir = $dir/newcerts # default place for new certs.
[50~51行目]
private_key = $dir/private/cakey.pem# The private key
RANDFILE = $dir/private/.rand # private random number file

■修正後
[37~39行目]
dir = . # Where everything is kept
certs = $dir # Where the issued certs are kept
crl_dir = $dir # Where the issued crl are kept
[43行目]
new_certs_dir = $dir # default place for new certs.
[50~51行目]
private_key = $dir/cakey.pem# The private key
RANDFILE = $dir/.rand # private random number file

/etc/pki/tls/yasu に中身が空っぽのテキストファイル index.txt と01と書いた serial ファイルを作成します
[root@centos yasu]# touch index.txt
[root@centos yasu]# echo 01 > serial

[root@centos yasu]# ls -l
合計 28
-rw-r–r– 1 root root 0 3月 17 11:31 index.txt
-rw-r–r– 1 root root 9534 3月 17 11:30 openssl.cnf
-rw-r–r– 1 root root 3 3月 17 11:31 serial

2.認証局の秘密鍵作成
認証局の秘密鍵を作成します。

[root@centos yasu]# openssl genrsa -out ./cakey.pem -des3 1024
Generating RSA private key, 1024 bit long modulus
………………++++++
……………………………………………++++++
e is 65537 (0×10001)
Enter pass phrase for ./cakey.pem:任意のパスフレーズを入力
Verifying – Enter pass phrase for ./cakey.pem:もう一度任意のパスフレーズを入力

[root@centos yasu]# ls -l
合計 36
-rw-r–r– 1 root root 963 3月 17 15:28 cakey.pem
-rw-r–r– 1 root root 0 3月 17 11:31 index.txt
-rw-r–r– 1 root root 9534 3月 17 11:30 openssl.cnf
-rw-r–r– 1 root root 3 3月 17 11:31 serial

これで/etc/pki/tls/yasu ディレクトリにはcakey.pemというファイルが作成されましたね。
これが認証局の秘密鍵になります。

3.認証局の証明書作成
「2.認証局の秘密鍵作成」で作成した認証局の秘密鍵ファイルを使って認証局の証明書を作成します。

[root@centos yasu]# openssl req -new -x509 -days 3650 -config ./openssl.cnf -key ./cakey.pem -out ./cacert.pem
Enter pass phrase for ./cakey.pem:「2.認証局の秘密鍵作成」で指定したパスフレーズを応答
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]:chiba
Locality Name (eg, city) [Newbury]:urayasu
Organization Name (eg, company) [My Company Ltd]:sa-sa-ki.jp
Organizational Unit Name (eg, section) []:何も入力しないで[Enter]キーを押下
Common Name (eg, your name or your server’s hostname) []:www.sa-sa-ki.jp
Email Address []:何も入力しないで[Enter]キーを押下

[root@centos yasu]# ls -l
合計 44
-rw-r–r– 1 root root 1164 3月 17 15:33 cacert.pem
-rw-r–r– 1 root root 963 3月 17 15:28 cakey.pem
-rw-r–r– 1 root root 0 3月 17 11:31 index.txt
-rw-r–r– 1 root root 9534 3月 17 11:30 openssl.cnf
-rw-r–r– 1 root root 3 3月 17 11:31 serial

cacert.pem というファイルが作成されました。

4.認証局の証明書をDER形式バイナリエンコード(ブラウザインポート用)
「3.認証局の証明書作成」で作成した認証局の証明書をブラウザにインポートする為、DER形式にバイナリエンコードします。

[root@centos yasu]# openssl x509 -days 3650 -inform pem -outform der -in ./cacert.pem -out ./ca.der
[
[root@centos yasu]# ls -l
合計 52
-rw-r–r– 1 root root 818 3月 17 15:36 ca.der
-rw-r–r– 1 root root 1164 3月 17 15:33 cacert.pem
-rw-r–r– 1 root root 963 3月 17 15:28 cakey.pem
-rw-r–r– 1 root root 0 3月 17 11:31 index.txt
-rw-r–r– 1 root root 9534 3月 17 11:30 openssl.cnf
-rw-r–r– 1 root root 3 3月 17 11:31 serial

ca.der というファイルが作成されました。
「13.SSLの動作確認および認証局の証明書インポート」で使うのでそれまで大切に保管してください。

5.サーバー秘密鍵作成
サーバー秘密鍵を作成します。

[root@centos yasu]# openssl genrsa -out ./server.key -des3 1024
Generating RSA private key, 1024 bit long modulus
….++++++
…..++++++
e is 65537 (0×10001)
Enter pass phrase for ./server.key:任意のパスフレーズを入力
Verifying – Enter pass phrase for ./server.key:もう一度任意のパスフレーズを入力

WebサーバーにSSLでアクセスした時に毎回パスフレーズを聞かれないようにサーバー秘密鍵のパスフレーズを削除します。
[root@centos yasu]# openssl rsa -in ./server.key -out ./server.key
Enter pass phrase for server.key:上記パスフレーズを応答
writing RSA key

[root@centos yasu]# ls -l
合計 60
-rw-r–r– 1 root root 818 3月 17 15:36 ca.der
-rw-r–r– 1 root root 1164 3月 17 15:33 cacert.pem
-rw-r–r– 1 root root 963 3月 17 15:28 cakey.pem
-rw-r–r– 1 root root 0 3月 17 11:31 index.txt
-rw-r–r– 1 root root 9534 3月 17 11:30 openssl.cnf
-rw-r–r– 1 root root 3 3月 17 11:31 serial
-rw-r–r– 1 root root 887 3月 17 15:38 server.key

server.key というファイルが作成されました。

6.サーバー証明要求書作成
「5.サーバー秘密鍵作成」で作成したサーバー秘密鍵ファイルを使ってサーバー証明要求書を作成します。

[root@centos yasu]# openssl req -new -days 3650 -config ./openssl.cnf -key ./server.key -out ./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) [GB]:JP
State or Province Name (full name) [Berkshire]:chiba
Locality Name (eg, city) [Newbury]:urayasu
Organization Name (eg, company) [My Company Ltd]:sa-sa-ki.jp
Organizational Unit Name (eg, section) []:何も入力しないで[Enter]キーを押下
Common Name (eg, your name or your server’s hostname) []:www.sa-sa-ki.jp
Email Address []:何も入力しないで[Enter]キーを押下

Please enter the following ‘extra’ attributes
to be sent with your certificate request
A challenge password []:何も入力しないで[Enter]キーを押下
An optional company name []:何も入力しないで[Enter]キーを押下

[root@centos yasu]# ls -l
合計 68
-rw-r–r– 1 root root 773 3月 17 16:49 ca.der
-rw-r–r– 1 root root 1103 3月 17 16:48 cacert.pem
-rw-r–r– 1 root root 963 3月 17 16:48 cakey.pem
-rw-r–r– 1 root root 0 3月 17 11:31 index.txt
-rw-r–r– 1 root root 9534 3月 17 11:30 openssl.cnf
-rw-r–r– 1 root root 3 3月 17 11:31 serial
-rw-r–r– 1 root root 639 3月 17 16:49 server.csr
-rw-r–r– 1 root root 891 3月 17 16:46 server.key

server.csr というファイルが作成されました。

7.サーバー証明要求書に署名
サーバー証明要求書ファイルに書名を行い、サーバー証明書を作成します。

[root@centos yasu]# openssl ca -config ./openssl.cnf -days 3650 -in ./server.csr -keyfile ./cakey.pem -cert ./cacert.pem -out ./server.cer
Using configuration from ./openssl.cnf
Enter pass phrase for ./cakey.pem:「2.認証局の秘密鍵作成」で指定したパスフレーズを応答
Check that the request matches the signature
Signature ok
Certificate Details:
Serial Number: 1 (0×1)
Validity
Not Before: Mar 17 07:50:57 2009 GMT
Not After : Mar 15 07:50:57 2019 GMT
Subject:
countryName = JP
stateOrProvinceName = chiba
organizationName = sa-sa-ki.jp
commonName = www.sa-sa-ki.jp
X509v3 extensions:
X509v3 Basic Constraints:
CA:FALSE
Netscape Comment:
OpenSSL Generated Certificate
X509v3 Subject Key Identifier:
F9:35:8B:89:22:16:B6:C8:F5:7E:C9:CB:28:DE:FF:0B:F9:7D:54:77
X509v3 Authority Key Identifier:
keyid:FE:40:F5:F1:60:78:3F:F4:BF:63:2B:FE:DE:CE:D9:32:5B:44:05:52

Certificate is to be certified until Mar 15 07:50:57 2019 GMT (3650 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

[root@centos yasu]# ls -l
合計 108
-rw-r–r– 1 root root 3050 3月 17 16:51 01.pem
-rw-r–r– 1 root root 773 3月 17 16:49 ca.der
-rw-r–r– 1 root root 1103 3月 17 16:48 cacert.pem
-rw-r–r– 1 root root 963 3月 17 16:48 cakey.pem
-rw-r–r– 1 root root 76 3月 17 16:51 index.txt
-rw-r–r– 1 root root 21 3月 17 16:51 index.txt.attr
-rw-r–r– 1 root root 0 3月 17 11:31 index.txt.old
-rw-r–r– 1 root root 9534 3月 17 11:30 openssl.cnf
-rw-r–r– 1 root root 3 3月 17 16:51 serial
-rw-r–r– 1 root root 3 3月 17 11:31 serial.old
-rw-r–r– 1 root root 3050 3月 17 16:51 server.cer
-rw-r–r– 1 root root 639 3月 17 16:49 server.csr
-rw-r–r– 1 root root 891 3月 17 16:46 server.key

server.cer というファイルが作成されました。

各ファイルをrootユーザ読み取り専用に権限を変更します。
[root@centos yasu]# chmod 400 *
[root@centos yasu]# ls -l
合計 108
-r——– 1 root root 3050 3月 17 16:51 01.pem
-r——– 1 root root 773 3月 17 16:49 ca.der
-r——– 1 root root 1103 3月 17 16:48 cacert.pem
-r——– 1 root root 963 3月 17 16:48 cakey.pem
-r——– 1 root root 76 3月 17 16:51 index.txt
-r——– 1 root root 21 3月 17 16:51 index.txt.attr
-r——– 1 root root 0 3月 17 11:31 index.txt.old
-r——– 1 root root 9534 3月 17 11:30 openssl.cnf
-r——– 1 root root 3 3月 17 16:51 serial
-r——– 1 root root 3 3月 17 11:31 serial.old
-r——– 1 root root 3050 3月 17 16:51 server.cer
-r——– 1 root root 639 3月 17 16:49 server.csr
-r——– 1 root root 891 3月 17 16:46 server.key

8.ApacheのSSL用設定ファイルを変更
ApacheのSSL用設定ファイル(ssl.conf)を変更します。

[root@centos yasu]# vi /etc/httpd/conf.d/ssl.conf
viエディタが起動するので設定ファイルを変更します。
#
# This is the Apache server configuration file providing SSL support.
# It contains the configuration directives to instruct the server how to
# serve pages over an https connection. For detailing information about these
# directives see
#
# Do NOT simply read the instructions in here without understanding
# what they do. They’re here only as hints or reminders. If you are unsure
# consult the online docs. You have been warned.
#

LoadModule ssl_module modules/mod_ssl.so

#
# When we also provide SSL we have to listen to the
# the HTTPS port in addition.
#
Listen 443

##
## SSL Global Context
##
## All SSL configuration in this context applies both to
## the main server and all SSL-enabled virtual hosts.
##

#
# Some MIME-types for downloading Certificates and CRLs
#
AddType application/x-x509-ca-cert .crt
AddType application/x-pkcs7-crl .crl

# Pass Phrase Dialog:
# Configure the pass phrase gathering process.
# The filtering dialog program (`builtin’ is a internal
# terminal dialog) has to provide the pass phrase on stdout.
SSLPassPhraseDialog builtin

# Inter-Process Session Cache:
# Configure the SSL Session Cache: First the mechanism
# to use and second the expiring timeout (in seconds).
#SSLSessionCache dc:UNIX:/var/cache/mod_ssl/distcache
SSLSessionCache shmcb:/var/cache/mod_ssl/scache(512000)
SSLSessionCacheTimeout 300

# Semaphore:
# Configure the path to the mutual exclusion semaphore the
# SSL engine uses internally for inter-process synchronization.
SSLMutex default

# Pseudo Random Number Generator (PRNG):
# Configure one or more sources to seed the PRNG of the
# SSL library. The seed data should be of good random quality.
# WARNING! On some platforms /dev/random blocks if not enough entropy
# is available. This means you then cannot use the /dev/random device
# because it would lead to very long connection times (as long as
# it requires to make more entropy available). But usually those
# platforms additionally provide a /dev/urandom device which doesn’t
# block. So, if available, use this one instead. Read the mod_ssl User
# Manual for more details.
SSLRandomSeed startup file:/dev/urandom 256
SSLRandomSeed connect builtin
#SSLRandomSeed startup file:/dev/random 512
#SSLRandomSeed connect file:/dev/random 512
#SSLRandomSeed connect file:/dev/urandom 512

#
# Use “SSLCryptoDevice” to enable any supported hardware
# accelerators. Use “openssl engine -v” to list supported
# engine names. NOTE: If you enable an accelerator and the
# server does not start, consult the error logs and ensure
# your accelerator is functioning properly.
#
SSLCryptoDevice builtin
#SSLCryptoDevice ubsec

##
## SSL Virtual Host Context
##

# General setup for the virtual host, inherited from global configuration
先頭の#を外してコメントを解除します
DocumentRoot “/var/www/html”
先頭の#を外してコメントを解除し、サーバー名を修正します
ServerName www.sa-sa-ki.jp:443

# Use separate log files for the SSL virtual host; note that LogLevel
# is not inherited from httpd.conf.
ErrorLog logs/ssl_error_log
TransferLogのファイル名をssl_access_logからssl_transfer_logに変更します
TransferLog logs/ssl_transfer_log
LogLevel warn

ログファイルのフォーマットを定義します
#
# The following directives define some format nicknames for use with
# a CustomLog directive (see below).
#
LogFormat “%h %l %u %t \”%r\” %>s %b \”%{Referer}i\” \”%{User-Agent}i\”" combined
LogFormat “%h %l %u %t \”%r\” %>s %b” common
LogFormat “%{Referer}i -> %U” referer
LogFormat “%{User-agent}i” agent

#
# For a single logfile with access, agent, and referer information
# (Combined Logfile Format), use the following directive:
#
CustomLog logs/ssl_access_log combined

# SSL Engine Switch:
# Enable/Disable SSL for this virtual host.
SSLEngine on

# SSL Protocol support:
# List the enable protocol levels with which clients will be able to
# connect. Disable SSLv2 access by default:
SSLProtocol all -SSLv2

# SSL Cipher Suite:
# List the ciphers that the client is permitted to negotiate.
# See the mod_ssl documentation for a complete list.
SSLCipherSuite ALL:!ADH:!EXPORT:!SSLv2:RC4+RSA:+HIGH:+MEDIUM:+LOW

# Server Certificate:
# Point SSLCertificateFile at a PEM encoded certificate. If
# the certificate is encrypted, then you will be prompted for a
# pass phrase. Note that a kill -HUP will prompt again. A new
# certificate can be generated using the genkey(1) command.
先頭に#をつけてコメント化します
#SSLCertificateFile /etc/pki/tls/certs/localhost.crt
追加します
SSLCertificateFile /etc/pki/tls/certs/server.cer

# Server Private Key:
# If the key is not combined with the certificate, use this
# directive to point at the key file. Keep in mind that if
# you’ve both a RSA and a DSA private key you can configure
# both in parallel (to also allow the use of DSA ciphers, etc.)
先頭に#をつけてコメント化します
#SSLCertificateKeyFile /etc/pki/tls/private/localhost.key
追加します
SSLCertificateKeyFile /etc/pki/tls/private/server.key

# Server Certificate Chain:
# Point SSLCertificateChainFile at a file containing the
# concatenation of PEM encoded CA certificates which form the
# certificate chain for the server certificate. Alternatively
# the referenced file can be the same as SSLCertificateFile
# when the CA certificates are directly appended to the server
# certificate for convinience.
#SSLCertificateChainFile /etc/pki/tls/certs/server-chain.crt

# Certificate Authority (CA):
# Set the CA certificate verification path where to find CA
# certificates for client authentication or alternatively one
# huge file containing all of them (file must be PEM encoded)
#SSLCACertificateFile /etc/pki/tls/certs/ca-bundle.crt

# Client Authentication (Type):
# Client certificate verification type and depth. Types are
# none, optional, require and optional_no_ca. Depth is a
# number which specifies how deeply to verify the certificate
# issuer chain before deciding the certificate is not valid.
#SSLVerifyClient require
#SSLVerifyDepth 10

# Access Control:
# With SSLRequire you can do per-directory access control based
# on arbitrary complex boolean expressions containing server
# variable checks and other lookup directives. The syntax is a
# mixture between C and Perl. See the mod_ssl documentation
# for more details.
#
#SSLRequire ( %{SSL_CIPHER} !~ m/^(EXP|NULL)/ \
# and %{SSL_CLIENT_S_DN_O} eq “Snake Oil, Ltd.” \
# and %{SSL_CLIENT_S_DN_OU} in {”Staff”, “CA”, “Dev”} \
# and %{TIME_WDAY} >= 1 and %{TIME_WDAY} < = 5 \
# and %{TIME_HOUR} >= 8 and %{TIME_HOUR} < = 20 ) \
# or %{REMOTE_ADDR} =~ m/^192\.76\.162\.[0-9]+$/
#

# SSL Engine Options:
# Set various options for the SSL engine.
# o FakeBasicAuth:
# Translate the client X.509 into a Basic Authorisation. This means that
# the standard Auth/DBMAuth methods can be used for access control. The
# user name is the `one line’ version of the client’s X.509 certificate.
# Note that no password is obtained from the user. Every entry in the user
# file needs this password: `xxj31ZMTZzkVA’.
# o ExportCertData:
# This exports two additional environment variables: SSL_CLIENT_CERT and
# SSL_SERVER_CERT. These contain the PEM-encoded certificates of the
# server (always existing) and the client (only existing when client
# authentication is used). This can be used to import the certificates
# into CGI scripts.
# o StdEnvVars:
# This exports the standard SSL/TLS related `SSL_*’ environment variables.
# Per default this exportation is switched off for performance reasons,
# because the extraction step is an expensive operation and is usually
# useless for serving static content. So one usually enables the
# exportation for CGI and SSI requests only.
# o StrictRequire:
# This denies access when “SSLRequireSSL” or “SSLRequire” applied even
# under a “Satisfy any” situation, i.e. when it applies access is denied
# and no other module can change it.
# o OptRenegotiate:
# This enables optimized SSL connection renegotiation handling when SSL
# directives are used in per-directory context.
#SSLOptions +FakeBasicAuth +ExportCertData +StrictRequire

SSLOptions +StdEnvVars


SSLOptions +StdEnvVars

# SSL Protocol Adjustments:
# The safe and default but still SSL/TLS standard compliant shutdown
# approach is that mod_ssl sends the close notify alert but doesn’t wait for
# the close notify alert from client. When you need a different shutdown
# approach you can use one of the following variables:
# o ssl-unclean-shutdown:
# This forces an unclean shutdown when the connection is closed, i.e. no
# SSL close notify alert is send or allowed to received. This violates
# the SSL/TLS standard but is needed for some brain-dead browsers. Use
# this when you receive I/O errors because of the standard approach where
# mod_ssl sends the close notify alert.
# o ssl-accurate-shutdown:
# This forces an accurate shutdown when the connection is closed, i.e. a
# SSL close notify alert is send and mod_ssl waits for the close notify
# alert of the client. This is 100% SSL/TLS standard compliant, but in
# practice often causes hanging connections with brain-dead browsers. Use
# this only for browsers where you know that their SSL implementation
# works correctly.
# Notice: Most problems of broken clients are also related to the HTTP
# keep-alive facility, so you usually additionally want to disable
# keep-alive for those clients, too. Use variable “nokeepalive” for this.
# Similarly, one has to force some clients to use HTTP/1.0 to workaround
# their broken HTTP/1.1 implementation. Use variables “downgrade-1.0″ and
# “force-response-1.0″ for this.
SetEnvIf User-Agent “.*MSIE.*” \
nokeepalive ssl-unclean-shutdown \
downgrade-1.0 force-response-1.0

# Per-Server Logging:
# The home of a custom SSL log file. Use this when you want a
# compact non-error SSL logfile on a virtual host basis.
CustomLog logs/ssl_request_log \
“%t %h %{SSL_PROTOCOL}x %{SSL_CIPHER}x \”%r\” %b”

内容を保存しviエディタを終了します

9.サーバー証明書ファイルとサーバー秘密鍵ファイルコピー
サーバー証明書ファイルとサーバー秘密鍵ファイルを所定の場所にコピーします。

[root@centos yasu]# cp -p server.cer /etc/pki/tls/certs/
[root@centos yasu]# cp -p server.key /etc/pki/tls/private/
[root@centos yasu]# ls -l /etc/pki/tls/certs/ /etc/pki/tls/private/
/etc/pki/tls/certs/:
合計 472
-rw-r–r– 1 root root 2240 1月 9 08:40 Makefile
-rw-r–r– 1 root root 441017 1月 9 08:40 ca-bundle.crt
-rw——- 1 root root 1444 2月 3 23:11 localhost.crt
-rwxr-xr-x 1 root root 610 1月 9 08:40 make-dummy-cert
-r——– 1 root root 3050 3月 17 16:51 server.cer

/etc/pki/tls/private/:
合計 16
-rw——- 1 root root 891 2月 3 23:11 localhost.key
-r——– 1 root root 891 3月 17 16:46 server.key

10.ファイアウォールの設定変更

[root@centos yasu]# vi /etc/sysconfig/iptables
viエディタが起動するので設定ファイルを変更します。
# Firewall configuration written by system-config-securitylevel
# Manual customization of this file is not recommended.
*filter
:INPUT ACCEPT [0:0]
:FORWARD ACCEPT [0:0]
:OUTPUT ACCEPT [0:0]
:RH-Firewall-1-INPUT – [0:0]
-A INPUT -j RH-Firewall-1-INPUT
-A FORWARD -j RH-Firewall-1-INPUT
-A RH-Firewall-1-INPUT -i lo -j ACCEPT
-A RH-Firewall-1-INPUT -p icmp –icmp-type any -j ACCEPT
-A RH-Firewall-1-INPUT -p 50 -j ACCEPT
-A RH-Firewall-1-INPUT -p 51 -j ACCEPT
-A RH-Firewall-1-INPUT -p udp –dport 5353 -d 224.0.0.251 -j ACCEPT
-A RH-Firewall-1-INPUT -p udp -m udp –dport 631 -j ACCEPT
-A RH-Firewall-1-INPUT -p tcp -m tcp –dport 631 -j ACCEPT
-A RH-Firewall-1-INPUT -m state –state ESTABLISHED,RELATED -j ACCEPT
-A RH-Firewall-1-INPUT -m state –state NEW -m tcp -p tcp –dport 22 -j ACCEPT
-A RH-Firewall-1-INPUT -m state –state NEW -m tcp -p tcp –dport 53 -j ACCEPT
-A RH-Firewall-1-INPUT -m state –state NEW -m udp -p udp –dport 53 -j ACCEPT
-A RH-Firewall-1-INPUT -m state –state NEW -m tcp -p tcp –dport 80 -j ACCEPT
下記1行を追加してTCPの443番ポート(https)を開放します
-A RH-Firewall-1-INPUT -m state –state NEW -m tcp -p tcp –dport 443 -j ACCEPT
-A RH-Firewall-1-INPUT -j REJECT –reject-with icmp-host-prohibited
COMMIT

内容を保存しviエディタを終了します

設定内容を反映します
[root@centos ~]# /etc/rc.d/init.d/iptables restart
ファイアウォールルールを適用中: [ OK ]
チェインポリシーを ACCEPT に設定中filter [ OK ]
iptables モジュールを取り外し中 [ OK ]
iptables ファイアウォールルールを適用中: [ OK ]
iptables モジュールを読み込み中ip_conntrack_netbios_ns [ OK ]

11.ルータの設定を変更変更
ルータの設定を変更してTCPの443番ポートを開放します。
開放方法はルータの設定手順に従って実施してください。

12.Apache再起動

Apacheを再起動してSSLの設定を反映させます
[root@centos yasu]# /etc/rc.d/init.d/httpd restart
httpd を停止中: [ OK ]
httpd を起動中: [ OK ]

13.SSLの動作確認および認証局の証明書インポート
SSLの動作確認の為にブラウザを起動してhttps://www.sa-sa-ki.jpにアクセスしてみます。

警告が表示されるので[このサイトの閲覧を続行する(推奨されません)。]をクリックします。

するとページが表示されました。

SSLの動作確認としてはこれでOKなのですが、毎回警告がでるのも鬱陶しいので警告がでないように認証局の証明書インポートします。

「4.認証局の証明書をDER形式バイナリエンコード(ブラウザインポート用)」で作成したca.derファイルをサーバーからクライアントPCにコピーします。
ca.derファイルを右クリックし[証明書をインストール(I)]を選択します。

証明書のインポートウィザードが表示されるので[次へ(N)]ボタンをクリックします。

[次へ(N)]ボタンをクリックします。

[完了]ボタンをクリックします。

[OK]ボタンをクリックします。

もう一度Internet Explorerを起動しなおして、再度https://www.sa-sa-ki.jpにアクセスしてみます。

すると警告が表示されなくなりました。

■ 参考サイト ■
flashcast:フリーで働くITエンジニア集団のブログ: OpenSSLでIISのサーバー証明書を作成・導入する
Webサーバー間通信内容暗号化(Apache+mod_SSL) – CentOSで自宅サーバー構築
はじめての自宅サーバ構築 Fedora/CentOS – 通信内容暗号化(OpenSSL & mod_SSL) ~ Webページ編 ~

————————————————————————————————–
flashcast:フリーで働くITエンジニア集団のブログ: 自宅サーバー再構築 その14 CentOS 5.2 にLAMP環境構築(MySQL編)
flashcast:フリーで働くITエンジニア集団のブログ: 自宅サーバー再構築 その13 CentOS 5.2 にLAMP環境構築(Apache、PHP編)
flashcast:フリーで働くITエンジニア集団のブログ: 自宅サーバー再構築 その12 CentOS 5.2 にDNSサーバーを導入して名前解決(確認編)
flashcast:フリーで働くITエンジニア集団のブログ: 自宅サーバー再構築 その11 CentOS 5.2 にDNSサーバーを導入して名前解決(設定編)
flashcast:フリーで働くITエンジニア集団のブログ: 自宅サーバー再構築 その10 CentOS 5.2 SSHサーバーを構築して外部からリモート接続&リモートファイル転送
flashcast:フリーで働くITエンジニア集団のブログ: 自宅サーバー再構築 その9 CentOS 5.2 にバッファオーバーフロー攻撃対策を行う
flashcast:フリーで働くITエンジニア集団のブログ: 自宅サーバー再構築 その8 CentOS 5.2 にrootkit検知ツールを導入する
flashcast:フリーで働くITエンジニア集団のブログ: 自宅サーバー再構築 その7 CentOS 5.2 にファイル改竄検知システムを導入する
flashcast:フリーで働くITエンジニア集団のブログ: 自宅サーバー再構築 その6 CentOS 5.2 にアンチウィルスソフトを導入する
flashcast:フリーで働くITエンジニア集団のブログ: 自宅サーバー再構築 その5 CentOS 5.2 ファイル転送準備
flashcast:フリーで働くITエンジニア集団のブログ: 自宅サーバー再構築 その4 CentOS 5.2 インストール後の初期設定
flashcast:フリーで働くITエンジニア集団のブログ: 自宅サーバー再構築 その3 インストールしたCentOS 5.2 にリモート接続準備
flashcast:フリーで働くITエンジニア集団のブログ: 自宅サーバー再構築 その2 CentOS 5.2 をインストールする
flashcast:フリーで働くITエンジニア集団のブログ: 自宅サーバー再構築 その1 導入を検討する

12月
29

Web開発をしているといつも気になる事があるのですが、Webサーバーにはサーバー証明書が導入されているのでアクセスするときにはhttpsプロトコルで通信するのに、自分の使っているパソコン(以降、ローカル環境)にWebサーバーと同様の環境を作ってテストする時はサーバー証明書を導入していないのでhttpプロトコルで通信していますよね?
自分もローカル環境でテストする時はhttpプロトコルで確認している一人なんですけど、どうせならローカル環境でテストする時もhttpsプロトコルで通信したいと思い、ローカル環境のIISにOpenSSLでサーバー証明書を作って導入してみる事にしました。

今回、確認した環境はWindowsXP Pro SP3のIIS5.1です。

■全体の作業手順

 1.OpenSSL、Visual C++ 2008 Redistributables、ActivePerlの
   ダウンロード
 2.ActivePerl、Visual C++ 2008 Redistributables、OpenSSLの
   インストール
 3.IISのサーバー証明書要求作成
 4.OpenSSLの設定ファイル編集およびサーバー証明書作成準備
 5.認証局の秘密鍵作成
 6.認証局の証明書作成
 7.認証局の証明書をDER形式バイナリエンコード(ブラウザインポート用)
 8.IISのサーバー証明書要求に署名
 9.IISにサーバー証明書インストール
10.ブラウザにDER形式認証局の証明書インポート

1.OpenSSL、Visual C++ 2008 Redistributables、ActivePerlのダウンロード
まずShining Light Productions – Win32 OpenSSLのページから
32bit版OpenSSL
Visual C++ 2008 Redistributables
をダウンロードします。
※Visual C++ 2008 Redistributables はMicrosoftのサイトに遷移しますのでそちらからダウンロードしてください
あとActivePerlが必要なので、ActiveStateのサイトからActivePerl-5.10.0.1004-MSWin32-x86-287188.msiをダウンロードします。

2.OpenSSL、Visual C++ 2008 Redistributables、ActivePerlのインストール
Active Perlからインストールします。
インストール手順は以前ご紹介したflashcast:フリーで働くITエンジニア集団のブログ: サイボウズのスケジュールをWindows Mobile(Advanced/W-ZERO3 [es])に自動同期するを参考にしてください。

次にVisual C++ 2008 Redistributables をインストールします。
ダウンロードしたvcredist_x86.exeをダブルクリックします。
セキュリティ警告ダイアログが表示されます。[実行]ボタンをクリックします。

インストール画面が表示されるので、[Next>]ボタンをクリックします。

「I have read and accept the license terms.」にチェックをして[Install>]ボタンをクリックします。

インストールが終了したら[Finish]ボタンをクリックします。

続いてOpenSSLをインストールします。
ダウンロードしたWin32OpenSSL-0_9_8i.exeをダブルクリックします。
セキュリティ警告ダイアログが表示されます。[実行]ボタンをクリックします。

インストール画面が表示されるので、[Next>]ボタンをクリックします。

「I accept the agreement」にチェックをして[Next>]ボタンをクリックします。

インストールフォルダを指定します。
デフォルトのまま(C:¥OpenSSL)[Next>]ボタンをクリックします。

スタートメニューに登録するときの名前を指定します。
デフォルトのまま(OpenSSL)[Next>]ボタンをクリックします。

OpenSSL DLLのインストール先を指定します。
デフォルトは「The Windows system directory」なのですが、ここでは「The OpenSSL binaries [/bin] directory」を選択して[Next>]ボタンをクリックします。

インストールオプションの確認をして[Install]ボタンをクリックします。

インストールが完了したら[Finish]ボタンをクリックします。

3.IISのサーバー証明書要求作成
スタートアップメニューの[スタート]-[プログラム]-[管理ツール]-[インターネット インフォメーション サービス]を選択してインターネットインフォメーションサービスの管理画面を起動します。
もしくは[スタート]-[設定]-[コントロールパネル]-[管理ツール]-[インターネット インフォメーション サービス]を選択してインターネットインフォメーションサービスの管理画面を起動します。
もしインターネットインフォメーションサービスがない場合は、コントロールパネルの[プログラムの追加と削除]から[Windowsコンポーネントの追加と削除]を選び、インターネットインフォメーションサービスをインストールしてください。
インターネットインフォメーションサービスの管理画面が起動したら、左ペインにあるツリーを[インターネットインフォメーションサービス]-[コンピュータ名(実際のコンピュータ名)]-[Web サイト]-[規定の Web サイト]の順番で展開していきます。

規定のWebサイトのプロパティ画面が開いたら、「ディレクトリ セキュリティ」タグを選択して、[サーバー証明書]ボタンをクリックします。

サーバー証明書ウィザードが起動するので、[次へ(N)>]ボタンをクリックします。

「証明書の新規作成」を選択して[次へ(N)>]ボタンをクリックします。

「証明書の要求を作成して後で送信する」を選択して[次へ(N)>]ボタンをクリックします。

証明書の登録名とビット長の指定をします。
デフォルトのままで[次へ(N)>]ボタンをクリックします。

組織と部門名を入力します。
ここでは組織を「yasu corp」、部門を「system」として[次へ(N)>]ボタンをクリックします。

一般名を入力します。
ここでは「yasu」として[次へ(N)>]ボタンをクリックします。
※この一般名にはWebページにアクセスするときに指定するFQDN(完全修飾ドメイン名)を入力するようにしてください。
(https://FQDN(完全修飾ドメイン名)/...)
もしくはここで入力した一般名でローカルPCのIPアドレスを逆引きできるようにDNSやhostsファイルを設定するようにしてください。
なぜか?は最後にネタ明かしします。

国を選択、都道府県と市区町村を入力します。
ここでは国を「JP(日本)」、都道府県を「tokyo」、市区町村を「koutouku」として[次へ(N)>]ボタンをクリックします。

証明書の要求ファイル名を指定します。
ここでは「C:\certreq.txt」として[次へ(N)>]ボタンをクリックします。

入力内容を確認して[次へ(N)>]ボタンをクリックします。

ウィザードが完了したら[完了]ボタンをクリックします。

4.OpenSSLの設定ファイル編集およびサーバー証明書作成準備
環境変数のPathに以下のパスを追加します。
C:\OpenSSL\;C:\OpenSSL\bin\;

サーバー証明書作成用作業フォルダを作成します。
ここでは C:\OpenSSL\bin\PEM\yasu というフォルダを作成します。

OpenSSLの設定ファイルを編集します。
C:\OpenSSL\bin直下にopenssl.cfgファイルがあるので、それをC:\OpenSSL\bin\PEM\yasuにコピーします。

C:\OpenSSL\bin\PEM\yasu\openssl.cfgファイルをメモ帳などテキストエディタで開き、以下の箇所を修正します。
■修正前

[37~39行目]
dir = ./demoCA # Where everything is kept
certs = $dir/certs # Where the issued certs are kept
crl_dir = $dir/crl # Where the issued crl are kept
[43行目]
new_certs_dir = $dir/newcerts # default place for new certs.
[50~51行目]
private_key = $dir/private/cakey.pem# The private key
RANDFILE = $dir/private/.rand # private random number file

■修正後

[37~39行目]
dir = . # Where everything is kept
certs = $dir # Where the issued certs are kept
crl_dir = $dir # Where the issued crl are kept
[43行目]
new_certs_dir = $dir # default place for new certs.
[50~51行目]
private_key = $dir/cakey.pem# The private key
RANDFILE = $dir/.rand # private random number file

C:\OpenSSL\bin\PEM\yasu に中身が空っぽのテキストファイル index.txt と01と書いた serial ファイルを作成します。
■index.txt のイメージ(空っぽです)

■serial のイメージ

手順3.IISのサーバー証明書要求作成で作成したサーバー証明書要求ファイル C:\certreq.txt をC:\OpenSSL\bin\PEM\yasu フォルダに移動します。

C:\OpenSSL\bin\PEM\yasu フォルダには

certreq.txt
index.txt
openssl.cfg
serial

の4つのファイルが保存されているはずです。

5.認証局の秘密鍵作成
スタートアップメニューの[ファイル名を指定して実行(R)]を選択し、cmd と入力して[OK]ボタンをクリックします。
コマンドプロンプトが起動しますので、認証局の秘密鍵を作成します。

 cd \OpenSSL\bin\PEM\yasu
 openssl genrsa -out .\cakey.pem 1024
 Loading ’screen’ into random state – done
 Generating RSA private key, 1024 bit long modulus
 ……………………………………………………………++++++
 ………………….++++++
 e is 65537 (0×10001)

これでC:\OpenSSL\bin\PEM\yasu フォルダにはcakey.pemというファイルが作成されましたね。
これが認証局の秘密鍵になります。

6.認証局の証明書作成
認証局の証明書を作成します。

 openssl req -new -x509 -config .\openssl.cfg -key .\cakey.pem -out .\cacert.pem
 Loading ’screen’ into random state – done
 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]:tokyo
 Locality Name (eg, city) []:koutouku
 Organization Name (eg, company) [Internet Widgits Pty Ltd]:yasu corp
 Organizational Unit Name (eg, section) []:system
 Common Name (eg, YOUR name) []:yasu
 Email Address []:

cacert.pem というファイルが作成されました。

7.認証局の証明書をDER形式バイナリエンコード(ブラウザインポート用)
手順6で作成した認証局の証明書をブラウザにインポートする為、DER形式にバイナリエンコードします。

 openssl x509 -inform pem -outform der -in .\cacert.pem -out ca.der

ca.der というファイルが作成されました。
手順10で使うのでそれまで大切に保管してください。

8.IISのサーバー証明書要求に署名
IISの証明書要求ウィザードで出力したファイルに署名を行い、サーバー証明書を作成します。

 openssl ca -config .\openssl.cfg -days 3650 -out .\server_.cer -infiles .\certreq.txt
 Using configuration from .\openssl.cfg
 Loading ’screen’ into random state – done
 Check that the request matches the signature
 Signature ok
 Certificate Details:
    Serial Number: 1 (0×1)
    Validity
      Not Before: Dec 26 08:08:00 2008 GMT
      Not After : Dec 24 08:08:00 2018 GMT
    Subject:
      countryName = JP
      stateOrProvinceName = tokyo
      organizationName = yasu corp
      organizationalUnitName = system
      commonName = yasu
    X509v3 extensions:
      X509v3 Basic Constraints:
        CA:FALSE
      Netscape Comment:
        OpenSSL Generated Certificate
      X509v3 Subject Key Identifier:
        86:2A:62:50:DE:54:CE:CA:72:5C:8B:55:ED:DA:FE:AD:0D:74:80:21
      X509v3 Authority Key Identifier:
        keyid:4D:0B:4E:34:FB:81:88:5F:40:9F:18:0A:26:EA:44:9F:F6:DD:B4:F6

 Certificate is to be certified until Dec 24 08:08:00 2018 GMT (3650 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

 openssl x509 -in .\server_.cer -out .\server.cer

以下7ファイル作成されました。

server_.cer
.rnd
01.pem
index.txt.attr
index.txt.old
serial.old
server.cer

server.cer ファイルがIISにインストールするサーバー証明書ファイルになります。

9.IISにサーバー証明書インストール
手順3同様、インターネットインフォメーションサービスの管理画面から規定のWebサイトのプロパティ画面を開き、「ディレクトリ セキュリティ」タグを選択して、[サーバー証明書]ボタンをクリックします。

サーバー証明書ウィザードが起動するので[次へ(N)>]ボタンをクリックします。

「保留中の要求を処理し、証明書をインストールする(P)」を選択して[次へ(N)>]ボタンをクリックします。

サーバー証明書ファイルを入力します。
[参照]ボタンをクリックしてC:\OpenSSL\bin\PEM\yasu\server.cer ファイルを選択し、[次へ(N)>]ボタンをクリックします。

証明書の内容を確認して[次へ(N)>]ボタンをクリックします。

ウィザードが完了したら[完了]ボタンをクリックします。

これでIISにSSLを導入する事ができました。

10.ブラウザにDER形式認証局の証明書インポート
このままだとhttpsプロトコルでページアクセスする度にセキュリティ警告

が表示されてしまうので、手順7で作成した証明書をクライアント側にインポートしてセキュリティ警告が表示されないようにします。

手順7で作成したca.der ファイルを選択して右クリックし、[証明書のインストール]を選択します。
すると証明書インポートウィザードが起動するので[次へ(N)>]ボタンをクリックします。

「証明書をすべて次のストアに配置する(P)」を選択し、[参照]ボタンから「信頼されたルート証明機関」を選択して[次へ(N)>]ボタンをクリックします。

[完了]ボタンをクリックします。

証明書をインストールしようとするとセキュリティ警告が表示されますが、[はい(Y)]ボタンをクリックします。

[OK]ボタンをクリックします。

これでhttpsプロトコルでローカル環境のWebページにアクセスしてもセキュリティ警告が表示されなくなります。
試しに規定のWebサイトのホームディレクトリ直下に適当なテストページを作り、ブラウザのアドレスに「https://yasu/test.html」のような形でアクセスしたところセキュリティ警告が表示されずにhttpsプロトコルでアクセスできるようになりました。
※最初ブラウザに証明書をインポートした後、セキュリティ警告が表示されない事を確認する為に「https://localhost/test.html」のようにFQDN(完全修飾ドメイン名)をlocalhostでやっていたのですが、何度やっても警告が表示されてしまいました。証明書のストア先を変更したりして試してもダメで途方にくれていたのですが、まさかの手順3.IISのサーバー証明書要求作成のところで入力した「一般名」にFQDN(完全修飾ドメイン名)を入力する必要がある事を知らずに全然別の適当な名前を入力していたのでアクセスしたページのFQDN(完全修飾ドメイン名)が信頼されておらずセキュリティ警告が出続けていました。
いやぁ、これで1時間近くはまったはまった・・・(汗

また今回はこちらのページに非常にお世話になりました。
IIS + OpenSSL:.knowledge – frog.raindrop.jp

WindowsXPで試しましたが、Windows2000、WindowsServer2003、WindowsVista、WindowsServer2008のIISでもサーバー証明書要求ファイルを作り、OpenSSLでサーバー証明書を作ってインストールすれば、IISのWebページにhttpsプロトコルでアクセスできると思います。
またOpenSSLは今回Windows用のものをダウンロード・インストールして使いましたが、OpenSSLはマルチプラットフォーム対応(LinuxやSolaris、Macなど)されていますので手順5〜8までは他OSのOpenSSLでも代用できると思います。

ローカル環境にセキュアなWebサーバ環境を安く構築したい時にとっても有効です。
ちょっと手順が長いですけど頑張って導入してみてください。

  • Search:
  • flashcastとは?

    東京を中心に、現在フリーランスとして活動しているITエンジニア、および、かつてフリーランスとして活動していた起業家達が立ち上げたコミュニティーです。

    みんなで集まって面白いことをやろう!形に残そう!ということで、ブログをはじめました。

    技術情報や、フリーエンジニアに役立つ情報などを、ご紹介できたらと思っています。

    お問い合わせ:
    info@flashcast.jp
  • カレンダー

    2010年7月
    日曜日 月曜日 火曜日 水曜日 木曜日 金曜日 土曜日
    « 6月    
     123
    45678910
    11121314151617
    18192021222324
    25262728293031
  • メンバー紹介

    もじゃもじゃ
    flashcastのリーダー

    3年ほどフリーのITエンジニアとして活動。現在は、社員2名の株式会社を経営しています。

    一攫千金を夢見る野心家です。

    ライブキャスト

    yasu
    ダイバー

    自宅サーバーでホームページを作り始めました。

    少しずつ記事を増やしていきますので足を運んでください。

    よろしくお願いします。

    sa-sa-ki.jp

    のら
    たびびと

    ねこ好きに悪人はいなーいっ!!

    バイクや車も好きです。

    めぐ
    デザイナーのたまご

    音楽とデザインとお酒をこよなく愛する永遠のダイエッター。

    現在ペンタブレットでイラストを勉強中。

    Hiro
    コンサル

    PMやSEの案件を業務委託で請けることが多いですが、小規模案件も受託でやっています。

    得意な分野はマイクロソフト製品や関連技術によるシステム構築です。

    KEI
    取締役の風格

    最年少なのに、メンバーで1番の貫禄の持ち主!?

    C#や.netなどサーバ側の開発が得意。

    ろっきー
    美食家★パパ

    自分にとっての息抜きは、ドライブして温泉に入って、美味しいご飯を食べる事。

    ココロとカラダのリフレッシュを大切にし、日々の仕事に励む一児の父親です。

    郵便番号検索

    my-hobby

    とのさま
    げーむのおうさま

    大人なのに好きなことしかやらない駄目人間。

    Web系が得意、アクセスは苦手><

    tonosamart.com

    セクレタリアト
    ギャンブラー

    フリーランス時代は仲間の現場を探すことが多く、それをきっかけに会社を設立。

    現在はSI業に特化せず、他の業種にも興味を持ち始めています。

    メドレー株式会社

  • 広告