郵件TLS/SSL加密通訊

案例1:郵件TLS/SSL加密通訊

1 案例1:郵件TLS/SSL加密通訊
1.1 問題html

本案例要求爲基於Postfix+Dovecot的郵件服務器提供加密通訊支持,主要完成如下任務操做:vim

爲SMTP服務(postfix)添加TLS/SSL加密通訊支持
基於dovecot配置POP3s+IMAPS加密通訊支持
客戶端收發信測試,確保加密的郵件通訊可用

1.2 方案安全

使用兩臺RHEL7虛擬機,其中svr7做爲CA服務器,而mail做爲測試用的Postfix+Dovecot郵件服務器。另外可準備一臺pc120做爲收發郵件的Windows測試機,安裝郵件客戶端軟件或Outlook 2010。
1.3 步驟服務器

實現此案例須要按照以下步驟進行。併發

步驟一:準備一個簡單的Postfix+Dovecot郵件服務器,支持SMTP認證dom

1) 快速安裝郵件相關軟件、添加郵箱帳號tcp

確認已安裝postfix、dovecot、cyrus-sasl軟件包,啓動saslauthd服務:ide

[root@www ~]# yum -y install postfix dovecot cyrus-sasl
.. ..
[root@www ~]# vim /etc/sasl2/smtpd.conf
pwcheck_method: saslauthd
mech_list: plain login
[root@www ~]# service saslauthd start ; chkconfig saslauthd on
正在啓動 saslauthd:                           [肯定]

添加兩個郵箱帳號mickey、minnie。post

[root@www ~]# useradd mickey
[root@www ~]# echo 123456 | passwd --stdin mickeyy
更改用戶 mickeyy 的密碼 。
passwd: 全部的身份驗證令牌已經成功更新。
[root@www ~]# useradd minnie
[root@www ~]# echo 123456 | passwd --stdin minnie
更改用戶 minnie 的密碼 。
passwd: 全部的身份驗證令牌已經成功更新。

2) 配置並啓動postfix服務測試

[root@mail ~]# cd /etc/postfix/
[root@mail postfix]# cp main.cf main.cf.origin
[root@mail postfix]# vim main.cf
.. ..
myhostname = mail.tedu.cn
mydomain = tedu.cn
myorigin = $mydomain
inet_interfaces = all
mydestination = $myhostname, localhost.$mydomain, localhost, $mydomain
mynetworks = 127.0.0.0/8
home_mailbox = Maildir/                                  //設置郵箱路徑
smtpd_sasl_auth_enable = yes
smtpd_sasl_security_options = noanonymous
smtpd_recipient_restrictions =
  permit_mynetworks,
  permit_sasl_authenticated,
  reject_unauth_destination
[root@mail postfix]# systemctl restart  postfix
[root@mail postfix]# netstat -anpt | grep master
tcp        0      0 0.0.0.0:25       0.0.0.0:*       LISTEN      32120/master

3) 配置並啓動dovecot服務

[root@mail dovecot]# vim /etc/dovecot/conf.d/10-mail.conf
mail_location = maildir:~/Maildir                       //設置郵箱路徑
.. ..
 [root@mail dovecot]# vim /etc/dovecot/conf.d/10-ssl.conf
.. ..
ssl = no                                              //先禁用SSL
#ssl_cert = </etc/pki/dovecot/certs/dovecot.pem          //註釋掉此處兩行內容
#ssl_key = </etc/pki/dovecot/private/dovecot.pem
[root@mail postfix]# systemctl restart  dovecot
正在啓動 Dovecot Imap:                        [肯定]
[root@mail postfix]# netstat -anpt | grep dovecot
tcp        0      0 0.0.0.0:110       0.0.0.0:*       LISTEN      32243/dovecot
tcp        0      0 0.0.0.0:143       0.0.0.0:*       LISTEN      32243/dovecot

4) 簡單測試一下,確認未做TLS/SSL加密時郵件收發可用

由root給mickey用戶發送一封郵件,確認mickey的郵箱能收到該郵件。

[root@mail ~]# echo "Hello Mickey" | mail -s "Test Mail XXXX" mickey@tedu.cn
[root@mail ~]# cat /home/mickey/Maildir/new/137690..        //找最新的一封郵件
Return-Path: <root@tedu.cn>
X-Original-To: mickey@tedu.cn
Delivered-To: mickey@tedu.cn
Received: by mail.tedu.cn (Postfix, from userid 0)
        id 28846836EA; Mon, 19 Aug 2017 17:36:58 +0800 (CST)
Date: Mon, 19 Aug 2017 17:36:58 +0800
To: mickey@tedu.cn
Subject: Test Mail XXXX
User-Agent: Heirloom mailx 12.4 7/29/08
MIME-Version: 1.0
Content-Type: text/plain; charset=us-ascii
Content-Transfer-Encoding: 7bit
Message-Id: <20130819093658.28846836EA@mail.tedu.cn>
From: root@tedu.cn (root)
Hello Mickey

步驟二:建立CSR證書籤發申請,提交給CA服務器簽署,下載簽署後的證書

1) 在mail服務器上,建立服務私鑰

因爲此例中的私鑰主要用於加密的郵件通訊,爲了方便服務控制,不要設置私鑰口令(在postfix中也很差配置) 。

[root@mail ~]# cd /etc/pki/tls/private/
[root@mail private]# openssl  genrsa  2048 > mail.key          //不設置私鑰口令
Generating RSA private key, 2048 bit long modulus
............................................................+++
................+++
e is 65537 (0x10001)
[root@mail private]# chmod 600 mail.key

2)在mail服務器上,建立CSR證書籤發請求

基於前一步建立的服務私鑰來創建CSR請求,根據提示設置的國家、省、市、組織信息要與CA根證書的設置保持一致。

[root@mail private]# openssl req -new -key mail.key > ~/mail.csr
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]:CN
State or Province Name (full name) []:Beijing
Locality Name (eg, city) [Default City]:Beijing
Organization Name (eg, company) [Default Company Ltd]:Tedu Technology Ltd
Organizational Unit Name (eg, section) []:
Common Name (eg, your name or your server's hostname) []:mail.tedu.cn
Email Address []:postmaster@tedu.cn
Please enter the following 'extra' attributes
to be sent with your certificate request
A challenge password []:
An optional company name []:

3)在CA服務器svr7上,簽署併發布證書

首先得到mail服務器(好比SCP方式)提交的CSR證書籤發請求文件,而後正式簽署並經過httpd服務提供下載。

[root@svr7 ~]# scp 192.168.4.120:/root/mail.csr ./
root@192.168.4.120's password:
mail.csr                                 100% 1062     1.0KB/s   00:00 
[root@svr7 ~]# cd /etc/pki/CA/certs/
[root@svr7 certs]# openssl ca -in ~/mail.csr > mail.crt      //簽署證書
Using configuration from /etc/pki/tls/openssl.cnf
Enter pass phrase for /etc/pki/CA/private/my-ca.key:          //驗證私鑰口令
Check that the request matches the signature
Signature ok
Certificate Details:
        .. ..
Certificate is to be certified until Aug 19 08:31:12 2014 GMT (365 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@svr7 certs]# cp mail.crt /var/www/html/certs/      //複製到Web下載目錄

4)在mail服務器上,下載簽發好的證書文件,確認私鑰、證書的存放路徑

[root@mail ~]# cd /etc/pki/tls/certs/
[root@mail certs]# wget http://192.168.4.7/certs/mail.crt
.. ..
2017-05-17 16:35:27 (300 MB/s) - 已保存 「mail.crt」 [4633/4633])
[root@mail certs]# ls -lh /etc/pki/tls/certs/mail.crt
-rw-r--r--. 1 root root 4.6K 8月  19 16:32 /etc/pki/tls/certs/mail.crt
[root@mail certs]# ls -lh /etc/pki/tls/private/mail.key
-rw-------. 1 root root 1.7K 8月  19 16:22 /etc/pki/tls/private/mail.key

步驟三:分別爲postfix、dovecot添加TLS/SSL加密通訊支持

大多數狀況下,加密的和非加密的服務會同時提供,容許郵箱用戶自行選擇 。固然,若是確實有須要,能夠只提供加密的收發信服務,禁用非TLS/SSL加密的收發信服務。

1) 修改postfix服務配置,啓用SSL加密通訊

[root@svr7 ~]# vim 
.. ..
smtpd_use_tls = yes
#smtpd_tls_auth_only = yes              //若啓用此項,則非TLS的SMTP通訊將被阻止
smtpd_tls_key_file = /etc/pki/tls/private/mail.key
smtpd_tls_cert_file = /etc/pki/tls/certs/mail.crt
#smtpd_tls_loglevel = 1                  //排錯階段可啓用此配置
[root@mail ~]# service postfix reload
從新載入postfix:                                          [肯定]

2)修改dovecot服務配置,啓用SSL加密通訊

[root@mail ~]# vim /etc/dovecot/conf.d/10-ssl.conf
.. ..
ssl = yes
#ssl_cert = </etc/pki/dovecot/certs/dovecot.pem
#ssl_key = </etc/pki/dovecot/private/dovecot.pem
ssl_cert = </etc/pki/tls/certs/mailsvr.crt
ssl_key = </etc/pki/tls/private/mailsvr.key
[root@mail ~]# netstat -anpt | grep dovecot
tcp        0      0 0.0.0.0:110       0.0.0.0:*       LISTEN      32243/dovecot
tcp        0      0 0.0.0.0:143       0.0.0.0:*       LISTEN      32243/dovecot
tcp        0      0 0.0.0.0:993       0.0.0.0:*       LISTEN      32243/dovecot
tcp        0      0 0.0.0.0:995       0.0.0.0:*       LISTEN      32243/dovecot

注意:若要禁用非加密的POP三、IMAP通訊,能夠參考如下配置(可選)。

[root@mail ~]# vim /etc/dovecot/conf.d/10-master.conf
inet_listener imap {  
  port = 0                                    //停用非加密的imap服務
}  
inet_listener pop3 {  
  port = 0                                    //停用非加密的pop3服務
}

步驟四:在郵件客戶端(好比Outlook Express)驗證加密的郵件通訊

1)爲測試用戶mickey配置郵件收發帳號

設置好電子郵件地址、用戶帳號密碼、收發信服務器等屬性。接收郵件選POP3或IMAP,勾選安全鏈接(SSL) ,如圖-1所示。

圖-1

2)加密的收發信測試

新建一封測試郵件,發送給minnie@tedu.cn、抄送給本身,確認可以成功發送並接收郵件。首次發送郵件時會出現安全提示,如圖-2所示,選「是」繼續便可。

圖-2

成功發出郵件之後,便可收取到抄送給本身的郵件,如圖-3所示。

圖-3

相關文章
相關標籤/搜索