証明書の作成

Let's Encrypt で証明書を取得しよう。

オレオレ証明書はもう古い。

証明書を参照するときのパーミッションは、postfix が root:mail で動いてるのでそのように。

Postfixのビルド

Postfix は既に導入されているものとする。

% make tidy ← make distcleanみたいなもの
% make makefiles \
     CCARGS="-DUSE_TLS -I/usr/local/ssl/include" \
     AUXLIBS="-L/usr/local/ssl/lib -lssl -lcrypto"

% make
# make update
なお、SASL化をする場合はmake makefilesを実行する際に を追加する。

設定ファイルの編集

main.cfのみを変更する。

#
# smtp_*
# smtp クライアントとして振る舞う (送信側になる) ときの SSL 設定。
#

# 証明書を発行した認証局の中間証明書を指定する。(Let's Encrypt のchain.pem)
smtp_tls_CAfile = $config_directory/ca/chain.pem
# 発行された証明書を指定する。
smtp_tls_cert_file = $config_directory/ca/cert.pem
# 発行された証明書の秘密鍵を指定する。パーミッションは root:mail の 600 で。
smtp_tls_key_file = $config_directory/ca/privkey.pem
# ロギングレベルの指定。Trust/Untrust の表示が出るのは "2" なのでまずは "2" を指定する。
# 動作に問題がなければ 0 か 1 で。
smtp_tls_loglevel = 2
# SSL の動作を指定する。"may" は対向のサーバーが SSL に対応しているならば SSL を使用する。
smtp_tls_security_level = may

#
# smtp_*
# smtp クライアントとして振る舞う (送信側になる) ときの SSL 設定。
#

# CAfile/cert_file/key_file/loglevel は smtp_* に同じ。 smtpd_tls_CAfile = $config_directory/ca/chain.pem
smtpd_tls_cert_file = $config_directory/ca/cert.pem
smtpd_tls_key_file = $config_directory/ca/privkey.pem
smtpd_tls_loglevel = 2
# Received ヘッダに SSL 情報を書き加える。no でも良い。
smtpd_tls_received_header = yes
# SSL の動作を指定する。"may" は送信元が SSL に対応しているならば SSL を使用する。
smtpd_tls_security_level = may
設定が完了したらpostfixのstop→startを行う。
maillogにエラーが出力されていないかをチェック。

動作確認

簡単なチェックとしてはtelnet localhost 25を使用し、STARTTLSが使用可能かをチェックする。

% telnet localhost 25
Trying 127.0.0.1...
Connected to localhost.
Escape character is '^]'.
220 mail.griffonworks.net ESMTP Postfix
EHLO localhost
(省略)
250-ETRN
250-STARTTLS
250 DSN
STARTTLS
220 2.0.0 Ready to start TLS
QUIT ←既にTLSモードになっているので
QUIT ←QUITを2回入力
STARTTLSを実行するとmaillogに以下のようなログが出力される。
Dec 24 14:09:29 leviathan postfix/smtpd[28448]: setting up TLS connection from localhost[127.0.0.1]

詳細チェックはMUAで実際にSSLを有効にしてメールを送信し、パケットキャプチャを行いSSL化されているかを確認する。
今回はtcpdump -w FILENAMEでtcpdumpのログファイルを出力した後、Wiresharkでそのログを読み込ませ、フィルタルールを"smtp"にしてログを見た。

TLS無し
暗号化されていないのでAUTHの文字列が丸見えとなる。
AUTH行は認証メカニズムがPLAINなので平文のMIME64化された文字列となる。
No. Time        Source         Destination    Protocol  Info
 97 12.071746   192.168.1.10   192.168.1.5    SMTP      Response: 220 mail.griffonworks.net ESMTP Postfix
 99 12.098032   192.168.1.5    192.168.1.10   SMTP      Command: EHLO [192.168.1.5]
101 12.098482   192.168.1.10   192.168.1.5    SMTP      Response: 250-mail.griffonworks.net
103 12.110204   192.168.1.5    192.168.1.10   SMTP      Command: AUTH PLAIN **************************
107 12.119038   192.168.1.10   192.168.1.5    SMTP      Response: 235 2.0.0 Authentication successful
108 12.120048   192.168.1.5    192.168.1.10   SMTP      Command: RSET
109 12.120392   192.168.1.10   192.168.1.5    SMTP      Response: 250 2.0.0 Ok
110 12.122054   192.168.1.5    192.168.1.10   SMTP      Command: MAIL FROM:
111 12.122484   192.168.1.10   192.168.1.5    SMTP      Response: 250 2.1.0 Ok
112 12.124233   192.168.1.5    192.168.1.10   SMTP      Command: RCPT TO:
113 12.176873   192.168.1.10   192.168.1.5    SMTP      Response: 250 2.1.5 Ok
114 12.181880   192.168.1.5    192.168.1.10   SMTP      Command: DATA
115 12.182263   192.168.1.10   192.168.1.5    SMTP      Response: 354 End data with .
116 12.185247   192.168.1.5    192.168.1.10   SMTP      Message Body
118 12.276049   192.168.1.5    192.168.1.10   SMTP      EOM: .
119 12.293406   192.168.1.10   192.168.1.5    SMTP      Response: 250 2.0.0 Ok: queued as 473141E701
121 12.317704   192.168.1.5    192.168.1.10   SMTP      Command: QUIT
122 12.317964   192.168.1.10   192.168.1.5    SMTP      Response: 221 2.0.0 Bye
TLS有り
暗号化されているので、STARTSSLした後の情報が何も見えない。
STARTSSLはHELO/EHLOコマンドを発行した後に発行されるので、AUTHなどの文字列も見えなくなる。
No.     Time    Source         Destination    Protocol Info
 85 16.431098   192.168.1.10   192.168.1.5    SMTP     Response: 220 mail.griffonworks.net ESMTP Postfix
 86 16.431799   192.168.1.5    192.168.1.10   SMTP     Command: EHLO [192.168.1.5]
 88 16.432285   192.168.1.10   192.168.1.5    SMTP     Response: 250-mail.griffonworks.net
 89 16.436001   192.168.1.5    192.168.1.10   SMTP     Message Body
 91 16.436326   192.168.1.10   192.168.1.5    SMTP     Response: 220 2.0.0 Ready to start TLS
 92 16.492331   192.168.1.5    192.168.1.10   SMTP     Message Body
 93 16.495783   192.168.1.10   192.168.1.5    SMTP     Response: \026\003(以下省略)
 94 16.495836   192.168.1.10   192.168.1.5    SMTP     Response: \243\3659L/(以下省略)
 97 16.656981   192.168.1.10   192.168.1.5    SMTP     Response: \024\003\(以下省略)
 98 16.657843   192.168.1.5    192.168.1.10   SMTP     Message Body
 99 16.658418   192.168.1.10   192.168.1.5    SMTP     Response: \027\003(以下省略)
100 16.663364   192.168.1.5    192.168.1.10   SMTP     Message Body
104 16.674267   192.168.1.5    192.168.1.10   SMTP     Message Body
106 16.711364   192.168.1.5    192.168.1.10   SMTP     Message Body
111 16.814976   192.168.1.5    192.168.1.10   SMTP     Message Body
115 16.923927   192.168.1.10   192.168.1.5    SMTP     Response: \027\003(以下省略)
116 16.951901   192.168.1.5    192.168.1.10   SMTP     Message Body