2020年4月17日 星期五

將 Let’s Encrypt 申請到的 SSL 憑證用在 Postfix、Dovecot

Let’s Encrypt申請的憑證會以申請的domain name當作資料夾名稱放到 /etc/letsencrypt/live/ 目錄下,檔案會有四個
檔案名稱說明
cert.pem申請網域的憑證
chain.pemLet's Encrypt 的憑證
fullchain.pemcert.pem 及 chain.pem 合併檔案
privkey.pem申請網域的憑證密鑰

Postfix:
編輯/etc/postfix/mail.cf,修改以下設定:

# TLS parameters

#default: empty;none:TLS will not be used;may:Opportunistic TLS;encrypt:Mandatory TLS encryption

#2.3以後版本使用,取代2.2以前的smtpd_use_tls=yes

#user client smtp to server

smtpd_tls_security_level = encrypt

#mail server smtp to mail server

smtp_tls_security_level = encrypt

#Discovering servers that support TLS

smtp_tls_note_starttls_offer = yes

#default: empty;Name of the file containing the optional Postfix SMTP server TLS session cache.

smtpd_tls_session_cache_database = btree:${data_directory}/smtpd_scache

#default: empty;Name of the file containing the optional Postfix SMTP client TLS session cache.

smtp_tls_session_cache_database = btree:${data_directory}/smtp_scache


#憑證位置 

#postfix <= 3.3

smtpd_tls_cert_file = /etc/letsencrypt/live/你申請的Domain/fullchain.pem
smtpd_tls_key_file = /etc/letsencrypt/live/你申請的Domain/privkey.pem
smtp_tls_CApath = /etc/letsencrypt/live/你申請的Domain/

#postfix >= 3.4

smtpd_tls_chain_files = /etc/letsencrypt/archive/你申請的Domain/privkey.pem,

                        /etc/letsencrypt/archive/你申請的Domain/fullchain.pem


#在mail的header加上SSL/TLS 相關資訊

smtpd_tls_received_header = yes

#客戶端/客戶發送郵件到我的服務器時,是否強制加密

smtpd_tls_auth_only = yes

#Log

smtpd_tls_loglevel = 1



存檔後重新啟動postfix即可生效!
使用telnet測試:
$ telnet localhost 25
接著輸入ehlo localhost
ehlo localhost  
250-mail.liaohaha.tw
250-PIPELINING
250-SIZE 10240000
250-VRFY
250-ETRN
250-STARTTLS
250-AUTH PLAIN LOGIN
250-ENHANCEDSTATUSCODES
250-8BITMIME
250-DSN
250 SMTPUTF8
有顯示 STARTTLS 即代表功能有正常運作!
再修改master.cf,將以下兩行註解拿掉
  -o smtpd_tls_security_level=may
  -o smtpd_sasl_auth_enable=yes
存檔後重新啟動postfix即可生效!

上述方式是開啟 SMTP STARTTLS 的加密傳輸,但在Microsoft outlook上,這個協定經常無法正常使用....
可以下列方式開啟SMTPS TLS加密(PORT 465)
編輯 /etc/postfix/master.cf,找到以下三行,拿掉行首的註解,若找不到,新增以下三行:
smtps     inet  n       -       y       -       -       smtpd
  -o smtpd_tls_wrappermode=yes
  -o smtpd_sasl_auth_enable=yes
重新啟動postfix。



Dovecot:  
編輯 /etc/dovecot/conf.d/10-ssl.conf

ssl_cert = </etc/letsencrypt/archive/你申請的Domain/cert.pem

ssl_key = </etc/letsencrypt/archive/你申請的Domain/privkey.pem

ssl_ca = /etc/letsencrypt/archive/你申請的Domain/fullchain.pem


ssl_min_protocol = TLSv1.2


存檔後重新啟動dovecot!