Postfix SMTP + SSL/TSL
參考資料:
SMTP 在預設的情況下,在傳輸過程中並不會有資料加密的動作,因此在使用者寄件時,中間的封包被側錄,那麼傳輸的文字內容馬上就會被清清楚的呈現。
為了資料傳送安全,現在可以使用 Postfix 的 SMTP 服務,再上 SSL 的的加密作業,讓使用者在寄件時能更加的安全。也就是平時常聽到的 smtps 功能。
編譯與安裝
在手動編譯之前,需要讓 Postfix 支援 SSL/TLS 才行,所以在您的主機上需要備有 openssl 的函式庫。
使用下列方式,可以清除之前舊有設定檔,並加入 SSL/TLS 支援,重新編譯並安裝。
0001 0002 0003 0004 0005 0006 |
root # make tidy root # make clean root # make makefiles CCARGS="-DUSE_TLS" AUXLIBS="-lssl -lcrypto" root # make root # make install |
建立憑證
使用 openssl 指令,建立憑證檔案,在此會產生一組 key 與 csr 檔案,csr 檔案在稍後由憑證中心簽屬完成憑證的認證。
0001 0002 |
root # openssl genrsa -out l-penguin.idv.tw.key 1024 root # openssl req -new -key l-penguin.idv.tw.key -out l-penguin.idv.tw.csr |
由 RootCA 簽立憑證
本範例是由 RootCA 使憑證的認證,若是您的組織之內沒有憑證中心,可以參考 更安全的的連線 Apache + SSL (new window) 來為自己簽屬憑證。
0001 |
root # openssl x509 -req -days 365 -in l-penguin.idv.tw.csr -CA ../ca.l-penguin.idv.tw.crt -CAkey ../ca.l-penguin.idv.tw.key -CAcreateserial -out l-penguin.idv.tw.crt |
若要建立 RootCA 憑證中心,可參考 建立一個可信任的單位根簽證 (Root CA) (new window)。
接下來,把簽屬完成的憑證,放在憑證資料夾裡。請注意,您的私有憑證與簽屬過的發佈用憑證,可放在任一目錄(本例為 /CA),但是私有憑證,除了 root 之外不應該還有其它使用者可以讀寫。
簽屬完的憑證為 crt 檔案,因此也需要保留好此一憑證。
0001 0002 0003 0004 0005 0006 |
root # pwd; ls -l /CA/l-penguin total 12 -rw-r--r-- 1 root root 1058 Jan 9 09:42 l-penguin.idv.tw.crt -rw-r--r-- 1 root root 733 Jan 9 09:42 l-penguin.idv.tw.csr -rw------- 1 root root 887 Jan 9 09:41 l-penguin.idv.tw.key |
廣 告 |
設定 Postfix
當您取得憑證,並且成功編譯並安裝完 Postfix 之後,您的 Postfix 就會支援 SSL/TLS 的加密傳輸,要啟用此一功能,需要編輯 Postfix 設定檔:main.cf 與 master.cf。
請在您的 Postfix 設定檔目錄下的 main.cf(一般為 /etc/postifx)最後輸入如下設定:
0001 0002 0003 0004 0005 0006 |
smtpd_tls_cert_file = /CA/l-penguin/l-penguin.idv.tw.crt smtpd_tls_key_file = /CA/l-penguin/l-penguin.idv.tw.key smtpd_use_tls = yes tls_random_source = dev:/dev/urandom tls_daemon_random_source = dev:/dev/urandom |
請注意,您應該要為 smtpd_tls_cert_file 與 smtpd_tls_key_file 設定正確的憑證位置。
另外,在 master.cf 下,加入 smtps 的設定如下:
0001 |
smtps inet n - n - - smtpd -v -o smtpd_tls_wrappermode=yes -o smtpd_sasl_auth_enable=yes |
重新啟動 Postfix。
0001 |
root # postfix stop; postfix start |
當您覺得一切都很順利時,請使用 telnet 的方式做最後的確認,以保證當您在啟用 SSL/TLS 時,Postfix 可以接收傳輸加密的功能。
0001 0002 0003 0004 0005 0006 0007 0008 0009 0010 0011 0012 0013 0014 0015 0016 0017 0018 0019 0020 |
# telnet localhost 25 Trying 127.0.0.1... Connected to localhost.localdomain (127.0.0.1). Escape character is '^]'. 220 l-penguin.idv.tw ESMTP Postfix ehlo localhost 250-l-penguin.idv.tw 250-PIPELINING 250-SIZE 10240000 250-VRFY 250-ETRN 250-STARTTLS 250-AUTH LOGIN PLAIN 250-AUTH=LOGIN PLAIN 250-ENHANCEDSTATUSCODES 250-8BITMIME 250 DSN STARTTLS 220 2.0.0 Ready to start TLS |
OK,當您的 Postfix 回應最後一行 Ready to start TLS 時,就可以確定 TLS 加密協定是成功的。
Client 設定
Outlook 帳號,請把加密類型修改為 TLS 即可。
以下為 Thunderbird 的 SMTP 設定,把 TLS 選起來即可。
下圖為側錄 SMTP 加密的內容,若使用 TLS 方式傳輸的話,其內容都會被加密而難以解密。
01/14/2009