為了使用 TLS,Postfix 服務器通常需要’證書’和’私鑰’。
兩者都必須是 “PEM” 格式, 私鑰不得加密.
CA證書, 需要發行機構 (付費 或是 自己就是發行機構)
受信任的 CA
dnf info ca-certificates
產生 SSL 憑證
利用 dovecot 的 腳本 mkcert.sh 生成
vi /etc/pki/dovecot/dovecot-openssl.cnf (dovecot-openssl.cnf 樣板)
/usr/libexec/dovecot/mkcert.sh (默認情況下)
證書 創建為 /etc/pki/tls/certs/mail.dxqerp.com.pem
私鑰 創建為 /etc/pki/tls/private/mail.dxqerp.com.pem
此外,默認情況下,證書將在 365 天后過期。
如果您想更改其中任何一項,請修改 mkcert.sh 腳本
smtpd_tls_cert_file =/etc/pki/tls/certs/mail.dxqerp.com.pem
smtpd_tls_key_file =/etc/pki/tls/private/mail.dxqerp.com.pem
證書 和 私鑰
PEM 格式是證書頒發機構頒發證書的最常見格式。
PEM 證書通常具有擴展名,例如 .pem、.crt、.cer 和 .key。
它們是 Base64 編碼的 ASCII 文件,
包含“—–BEGIN CERTIFICATE—–”和“—–END CERTIFICATE—–”語句。
服務器證書、中間證書和私鑰都可以放入 PEM 格式。
確認
openssl x509 -in mail.dxqerp.com.pem -noout -subject
openssl x509 -in mail.dxqerp.com.pem -noout -text
配置
Smtp Server
smtpd_tls_security_level = may # 預設 空字串
# 转发邮件配置启用SSL/TLS,若remote SMTP server支持 TLS/SSL,则使用TLS/SSL传输,否则使用传统的SMTP+TCP。
smtpd_tls_loglevel = 1 # 預設 0
# 僅記錄 TLS 握手完成時的摘要消息
smtpd_tls_received_header = yes # 預設 空字串
# 請求 Postfix SMTP 服務器生成 Received: 消息標頭,其中包含有關使用的協議和密碼的信息
smtpd_tls_auth_only = yes
#強迫 SASL 驗證採用 TLS 並禁止純文字驗證發生,除非已經建立了一個 TLS 工作階段。
(在測試時註釋掉 smtpd_tls_auth_only = yes 也許會有用,好讓我們能測試 SSL/TLS 能否運作,但當 SSL/TLS 失效時仍有純文字 SASL 驗證作為後備)。
如果一切如期運作,我們應該會看見伺服器提供 STARTTLS,而且因為我們指定 'smtpd_tls_auth_only = yes',純文字 SASL 驗證(AUTH PLAIN LOGIN 及 AUTH=PLAIN LOGIN)已不再被支援。
Smtp Client
smtp_tls_security_level = encrypt # 預設 may
# Postfix SMTP 客戶端的安全級別 ( encrypt 加密)
smtp_tls_note_starttls_offer = yes
# 當尚未為該服務器啟用 TLS 時,記錄提供 STARTTLS 的遠程 SMTP 服務器的主機名。
systemctl restart postfix
測試
echo "Body: This is a test mail. Hallo Charlie" | mail -s "Subject: A big test" qq@mail.dxqerp.com
Connection to the SMTP server
openssl s_client -connect localhost:25 -starttls smtp
STARTTLS Command
nc localhost 25
THE END
參考
https://datacadamia.com/marketing/email/postfix/tls#starttls_command
https://wiki.centos.org/zh-tw/HowTos/postfix_sasl
https://www.linuxtechi.com/install-configure-postfix-mailserver-centos-8/