前回導入したメールサーバーではSMTPやPOP3/IMAP4のパスワードをチャレンジレスポンス認証にしました。
今回はさらにセキュリティを向上するためにSSLを導入したいと思います。
1.OpenSSLの設定ファイル編集およびサーバー証明書作成準備
CentOS5の時にも数回やりましたが、SSL証明を自分で作成する前に下準備を行います。
[root@sl6 ~]# cd /etc/pki/tls
[root@sl6 tls]# mkdir mail
# 設定ファイルを作業用ディレクトリにコピーします
[root@sl6 tls]# cp openssl.cnf ./mail
[root@sl6 tls]# cd mail
# 作業用ディレクトリの内容を確認しておきます
[root@sl6 mail]# ls -la
合計 20
drwxr-xr-x. 2 root root 4096 6月 16 16:30 2011 .
drwxr-xr-x. 6 root root 4096 6月 16 16:30 2011 ..
-rw-r--r--. 1 root root 10906 6月 16 16:30 2011 openssl.cnf
# コピーしてきたopenssl.cnfファイルがあることを確認します
# 設定ファイルを編集します
[root@sl6 mail]# vi openssl.cnf
# 42行目〜44行目、48行目、55行目〜56行目を修正します
dir = . # Where everything is kept # /etc/pki/CA から . に変更
certs = $dir # Where the issued certs are kept # $dir/certs から $dir に変更
crl_dir = $dir # Where the issued crl are kept # $dir/crl から $dir に変更
new_certs_dir = $dir # default place for new certs. # $dir/newcerts から $dir に変更
private_key = $dir/cakey.pem # The private key # $dir/private/cakey.pem から $dir/cakey.pem に変更
RANDFILE = $dir/.rand # private random number file # $dir/private/.rand から $dir/.rand に変更
# 保存してviエディタを終了します
# index.txtという空ファイルと01と書き込んだserialファイルを作成します
[root@sl6 mail]# touch index.txt
[root@sl6 mail]# echo 01 > serial
# 作業用ディレクトリの内容を確認しておきます
[root@sl6 mail]# ls -la
合計 24
drwxr-xr-x. 2 root root 4096 6月 16 16:48 2011 .
drwxr-xr-x. 6 root root 4096 6月 16 16:30 2011 ..
-rw-r--r--. 1 root root 0 6月 16 16:31 2011 index.txt
-rw-r--r--. 1 root root 10897 6月 16 16:48 2011 openssl.cnf
-rw-r--r--. 1 root root 3 6月 16 16:31 2011 serial
# index.txtファイルとserialファイルがあることを確認します
2.認証局の秘密鍵作成
認証局の秘密鍵を作成します。
Generating RSA private key, 2048 bit long modulus
......++++++
...........................................++++++
e is 65537 (0x10001)
Enter pass phrase for ./cakey.pem: パスフレーズを入力
Verifying - Enter pass phrase for ./cakey.pem: パスフレーズを再入力
# 作業用ディレクトリの内容を確認しておきます
[root@sl6 mail]# ls -la
合計 28
drwxr-xr-x. 2 root root 4096 6月 16 16:49 2011 .
drwxr-xr-x. 6 root root 4096 6月 16 16:30 2011 ..
-rw-r--r--. 1 root root 963 6月 16 16:49 2011 cakey.pem
-rw-r--r--. 1 root root 0 6月 16 16:31 2011 index.txt
-rw-r--r--. 1 root root 10897 6月 16 16:48 2011 openssl.cnf
-rw-r--r--. 1 root root 3 6月 16 16:31 2011 serial
# 認証局の秘密鍵ファイル[cakey.pem]が作成されたことを確認します
3.認証局の証明書作成
「2.認証局の秘密鍵作成」で作成した認証局の秘密鍵ファイルを使って認証局の証明書を作成します。
Enter pass phrase for ./cakey.pem: cakey.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) [XX]:JP # 日本なのでJP
State or Province Name (full name) []:chiba # 都道府県
Locality Name (eg, city) [Default City]:urayasu # 市区町村
Organization Name (eg, company) [Default 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 # メールサーバーのFQDN名
Email Address []: 入力せずEnter
# 作業用ディレクトリの内容を確認しておきます
[root@sl6 mail]# ls -la
合計 32
drwxr-xr-x. 2 root root 4096 6月 16 16:50 2011 .
drwxr-xr-x. 6 root root 4096 6月 16 16:30 2011 ..
-rw-r--r--. 1 root root 948 6月 16 16:50 2011 cacert.pem
-rw-r--r--. 1 root root 963 6月 16 16:49 2011 cakey.pem
-rw-r--r--. 1 root root 0 6月 16 16:31 2011 index.txt
-rw-r--r--. 1 root root 10897 6月 16 16:48 2011 openssl.cnf
-rw-r--r--. 1 root root 3 6月 16 16:31 2011 serial
# 認証局の証明書ファイル[cacert.pem]が作成されたことを確認します
4.サーバー秘密鍵作成
サーバー秘密鍵を作成します。
Generating RSA private key, 2048 bit long modulus
.............++++++
.................++++++
e is 65537 (0x10001)
Enter pass phrase for ./server.key: パスフレーズ入力
Verifying - Enter pass phrase for ./server.key: パスフレーズ再入力
# メールサーバーにSSLでアクセスした時に毎回パスフレーズを聞かれないようにパスフレーズ付きサーバー秘密鍵を作成します
[root@sl6 mail]# openssl rsa -in ./server.key -out ./server.key
Enter pass phrase for ./server.key: server.keyファイル作成時のパスフレーズ入力
writing RSA key
# 作業用ディレクトリの内容を確認しておきます
[root@sl6 mail]# ls -la
合計 36
drwxr-xr-x. 2 root root 4096 6月 16 16:52 2011 .
drwxr-xr-x. 6 root root 4096 6月 16 16:30 2011 ..
-rw-r--r--. 1 root root 948 6月 16 16:50 2011 cacert.pem
-rw-r--r--. 1 root root 963 6月 16 16:49 2011 cakey.pem
-rw-r--r--. 1 root root 0 6月 16 16:31 2011 index.txt
-rw-r--r--. 1 root root 10897 6月 16 16:48 2011 openssl.cnf
-rw-r--r--. 1 root root 3 6月 16 16:31 2011 serial
-rw-r--r--. 1 root root 887 6月 16 16:53 2011 server.key
# サーバー秘密鍵ファイル[server.key]が作成されたことを確認します
5.サーバー証明要求書作成
「4.サーバー秘密鍵作成」で作成したサーバー秘密鍵ファイルを使ってサーバー証明要求書を作成します。
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) []:chiba
Locality Name (eg, city) [Default City]:urayasu
Organization Name (eg, company) [Default 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@sl6 mail]# ls -la
合計 40
drwxr-xr-x. 2 root root 4096 6月 16 16:54 2011 .
drwxr-xr-x. 6 root root 4096 6月 16 16:30 2011 ..
-rw-r--r--. 1 root root 948 6月 16 16:50 2011 cacert.pem
-rw-r--r--. 1 root root 963 6月 16 16:49 2011 cakey.pem
-rw-r--r--. 1 root root 0 6月 16 16:31 2011 index.txt
-rw-r--r--. 1 root root 10897 6月 16 16:48 2011 openssl.cnf
-rw-r--r--. 1 root root 3 6月 16 16:31 2011 serial
-rw-r--r--. 1 root root 639 6月 16 16:54 2011 server.csr
-rw-r--r--. 1 root root 887 6月 16 16:53 2011 server.key
# サーバー証明要求書ファイル[server.csr]が作成されたことを確認します
6.サーバー証明要求書に署名
サーバー証明要求書ファイルに書名を行い、サーバー証明書を作成します。
Using configuration from ./openssl.cnf
Enter pass phrase for ./cakey.pem: cakey.pemのパスフレーズを入力します
Check that the request matches the signature
Signature ok
Certificate Details:
Serial Number: 1 (0x1)
Validity
Not Before: Jun 16 08:04:25 2011 GMT
Not After : Jun 13 08:04:25 2021 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:
xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx
X509v3 Authority Key Identifier:
keyid:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx
Certificate is to be certified until Jun 13 08:04:25 2021 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@sl6 mail]# ls -la
合計 60
drwxr-xr-x. 2 root root 4096 6月 16 17:04 2011 .
drwxr-xr-x. 6 root root 4096 6月 16 16:30 2011 ..
-rw-r--r--. 1 root root 3041 6月 16 17:04 2011 01.pem
-rw-r--r--. 1 root root 948 6月 16 16:50 2011 cacert.pem
-rw-r--r--. 1 root root 963 6月 16 16:49 2011 cakey.pem
-rw-r--r--. 1 root root 77 6月 16 17:04 2011 index.txt
-rw-r--r--. 1 root root 21 6月 16 17:04 2011 index.txt.attr
-rw-r--r--. 1 root root 0 6月 16 16:31 2011 index.txt.old
-rw-r--r--. 1 root root 10867 6月 16 17:04 2011 openssl.cnf
-rw-r--r--. 1 root root 3 6月 16 17:04 2011 serial
-rw-r--r--. 1 root root 3 6月 16 16:31 2011 serial.old
-rw-r--r--. 1 root root 3041 6月 16 17:04 2011 server.cer
-rw-r--r--. 1 root root 639 6月 16 16:58 2011 server.csr
-rw-r--r--. 1 root root 887 6月 16 16:53 2011 server.key
# サーバー証明書ファイル[server.cer]が作成されたことを確認します
# 各ファイルをrootユーザ読み取り専用に権限を変更します
[root@sl6 mail]# chmod 400 *
# 作業用ディレクトリの内容を確認しておきます
[root@sl6 mail]# ls -la
合計 60
drwxr-xr-x. 2 root root 4096 6月 16 17:04 2011 .
drwxr-xr-x. 6 root root 4096 6月 16 16:30 2011 ..
-r--------. 1 root root 3041 6月 16 17:04 2011 01.pem
-r--------. 1 root root 948 6月 16 16:50 2011 cacert.pem
-r--------. 1 root root 963 6月 16 16:49 2011 cakey.pem
-r--------. 1 root root 77 6月 16 17:04 2011 index.txt
-r--------. 1 root root 21 6月 16 17:04 2011 index.txt.attr
-r--------. 1 root root 0 6月 16 16:31 2011 index.txt.old
-r--------. 1 root root 10867 6月 16 17:04 2011 openssl.cnf
-r--------. 1 root root 3 6月 16 17:04 2011 serial
-r--------. 1 root root 3 6月 16 16:31 2011 serial.old
-r--------. 1 root root 3041 6月 16 17:04 2011 server.cer
-r--------. 1 root root 639 6月 16 16:58 2011 server.csr
-r--------. 1 root root 887 6月 16 16:53 2011 server.key
# 全ファイルの権限が読み取りのみになっていることを確認しておきます
7.証明書の設定
作成したサーバー証明書ファイル使ってSSL通信できるようにPostfixとDovecotの設定ファイルを変更します。
[root@centos mail]# vi /etc/postfix/main.cf
# 設定ファイルの一番下に追記します
# SMTP SSL
smtpd_use_tls = yes
smtpd_tls_cert_file = /etc/pki/tls/mail/server.cer
smtpd_tls_key_file = /etc/pki/tls/mail/server.key
smtpd_tls_session_cache_database = btree:/etc/postfix/smtpd_scache
# 保存してviエディタを終了します
[root@sl6 mail]# vi /etc/postfix/master.cf
#
# Postfix master process configuration file. For details on the format
# of the file, see the master(5) manual page (command: "man 5 master").
#
# Do not forget to execute "postfix reload" after editing this file.
#
# ==========================================================================
# 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_tls_security_level=encrypt
# -o smtpd_sasl_auth_enable=yes
# -o smtpd_client_restrictions=permit_sasl_authenticated,reject
# -o milter_macro_daemon_name=ORIGINATING
smtps inet n - n - - smtpd # 先頭の#を削除してコメント解除
-o smtpd_tls_wrappermode=yes # 先頭の#を削除してコメント解除
-o smtpd_sasl_auth_enable=yes # 先頭の#を削除してコメント解除
-o smtpd_client_restrictions=permit_sasl_authenticated,reject # 先頭の#を削除してコメント解除
# -o milter_macro_daemon_name=ORIGINATING
# 保存してviエディタを終了します
# Dovecotの設定ファイルを変更します
[root@sl6 mail]# vi /etc/dovecot/conf.d/10-ssl.conf
##
## SSL settings
##
# SSL/TLS support: yes, no, required.
ssl = yes # 先頭の#を削除してコメント解除
# 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 = </etc/pki/tls/mail/server.cer # /etc/pki/dovecot/certs/dovecot.pemから/etc/pki/tls/mail/server.cerに変更
ssl_key = </etc/pki/tls/mail/server.key # /etc/pki/dovecot/private/dovecot.pemから/etc/pki/tls/mail/server.keyに変更
# 保存してviエディタを終了します
PostfixとDovecotを再起動して設定ファイルを反映します
[root@sl6 mail]# /etc/rc.d/init.d/postfix restart
postfix を停止中: [ OK ]
postfix を起動中: [ OK ]
[root@sl6 mail]# /etc/rc.d/init.d/dovecot restart
Dovecot Imap を停止中: [ OK ]
Dovecot Imap を起動中: [ OK ]
8.ファイヤーウォールの設定変更
SMTPS、POP3S、IMAP4Sのポート番号のアクセス許可設定を行います。
# Firewall configuration written by system-config-firewall
# Manual customization of this file is not recommended.
*filter
:INPUT ACCEPT [0:0]
:FORWARD ACCEPT [0:0]
:OUTPUT ACCEPT [0:0]
-A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT
-A INPUT -p icmp -j ACCEPT
-A INPUT -i lo -j ACCEPT
-A INPUT -m state --state NEW -m tcp -p tcp --dport 22 -j ACCEPT
-A INPUT -m state --state NEW -m tcp -p tcp --dport 25 -j ACCEPT
-A INPUT -m state --state NEW -m tcp -p tcp --dport 53 -j ACCEPT
-A INPUT -m state --state NEW -m udp -p udp --dport 53 -j ACCEPT
-A INPUT -m state --state NEW -m tcp -p tcp --dport 110 -j ACCEPT
-A INPUT -m state --state NEW -m tcp -p tcp --dport 123 -j ACCEPT
-A INPUT -m state --state NEW -m udp -p udp --dport 123 -j ACCEPT
-A INPUT -m state --state NEW -m tcp -p tcp --dport 143 -j ACCEPT
-A INPUT -m state --state NEW -m tcp -p tcp --dport 465 -j ACCEPT
-A INPUT -m state --state NEW -m tcp -p tcp --dport 993 -j ACCEPT
-A INPUT -m state --state NEW -m tcp -p tcp --dport 995 -j ACCEPT
-A INPUT -j LOG --log-level debug --log-prefix '[iptables]:'
-A INPUT -j REJECT --reject-with icmp-host-prohibited
-A FORWARD -j REJECT --reject-with icmp-host-prohibited
COMMIT
# 保存してviエディタを終了します
# iptablesを再起動して設定を反映します
[root@sl6 mail]# /etc/rc.d/init.d/iptables restart
iptables: ファイアウォールルールを消去中: [ OK ]
iptables: チェインをポリシー ACCEPT へ設定中filter [ OK ]
iptables: モジュールを取り外し中: [ OK ]
iptables: ファイアウォールルールを適用中: [ OK ]
[root@sl6 mail]# /etc/rc.d/init.d/postfix restart
postfix を停止中: [ OK ]
postfix を起動中: [ OK ]
[root@sl6 mail]# /etc/rc.d/init.d/dovecot restart
Dovecot Imap を停止中: [ OK ]
Dovecot Imap を起動中: [ OK ]
9.ルータの設定変更
ルータがない場合は不要な手順になりますが、yasuの家にはCTUとサーバーの間にルータがありますので併せてNAT設定で465番ポート、993番ポート、995番ポートをサーバーに向けます。
設定方法はルータの設定手順に従ってください。
これでSSL+チャレンジレスポンス認証になり、よりセキュアなメールサーバーができたのではないかと思います。
■関連記事
Scientific Linux 6で自宅サーバー構築 その1 サーバー機Express 5800 GT110bのオンボードRAIDでRAID1設定
Scientific Linux 6で自宅サーバー構築 その2 Scientific Linux 6をExpress5800/GT110bにインストール
Scientific Linux 6で自宅サーバー構築 その3 Scientific Linux 6インストール後の初期設定
Scientific Linux 6で自宅サーバー構築 番外編その1 Scientific Linux 6のcronについて
Scientific Linux 6で自宅サーバー構築 その4 Scientific Linux 6 にClamAVをインストールしてウィルス対策
Scientific Linux 6で自宅サーバー構築 その5 Scientific Linux 6 にTripwireをインストールしてファイル改ざんを検知する
Scientific Linux 6で自宅サーバー構築 その6 Scientific Linux 6 にchkrootkitをインストールしてrootkit感染を検知する
Scientific Linux 6で自宅サーバー構築 その7 NTPサーバーで自動時刻合わせ
Scientific Linux 6で自宅サーバー構築 その8 バッファオーバーフロー攻撃対策を行う
Scientific Linux 6で自宅サーバー構築 その9 logwatchでサーバー監視
Scientific Linux 6で自宅サーバー構築 その10 DNSサーバーを構築
Scientific Linux 6で自宅サーバー構築 番外編その2 Scientific Linuxを6.0から6.1にアップグレードする
Scientific Linux 6で自宅サーバー構築 その11 PostfixとDovecotとSMTP-AUTHでメールサーバーを構築する
Scientific Linux 6で自宅サーバー構築 その12 メールサーバーにSSLを導入する
Scientific Linux 6で自宅サーバー構築 その13 Apache PHP MySQLをインストール
Scientific Linux 6で自宅サーバー構築 その14 WebサーバーにSSLを導入する
Scientific Linux 6で自宅サーバー構築 その15 WordPressを導入する
Scientific Linux 6で自宅サーバー構築 その16 Piwikでアクセスログ集計
Scientific Linux 6で自宅サーバー構築 その17 Squidをインストール
Scientific Linux 6で自宅サーバー構築 その18 sshで鍵認証を導入
Scientific Linux 6で自宅サーバー構築 その19 WebDavの設定
Scientific Linux 6で自宅サーバー構築 その20 lm_sensorをインストール

