一、开启Postfix(smtp)的TLS功能
STARTTLS(TLS)介绍
TLS - Transport Layer Security ( 为网路 OSI 七层中的第四层加密技术,SSL - Secure Socket Layer 协定为之前身 )
SASL - Simple Authentication and Security Layer ( 简易的安全认证,SASL 同时也提供 SMTP 认证,定义在 RFC-2554
[url]http://www.ietf.org/rfc/rfc2554.txt[/url] )
Key - 安全连线金钥 ( key-pair ) CA - 授权凭证 ( Certification Authority )
网路上有些独立公正单位例如 VeriSign 及 GlobalSign 等等……它们就是专门贩卖安全金钥 Key 以及授权凭证 CA 的单位,我们这边�裼米孕醒u作安全金钥以及授权凭证,自行�u作的安全金钥与授权凭证,在功能上与买来的相同,只是向独立公正单位购买来的 Key 跟 CA,会让 Client 使用者心理上觉得比较有保障,因为我们不做生意所以也不需要花钱买Key 跟 CA ,这边 Postfix + TLS 主要目的是保障我的连线加密防止帐号密码外��。
(一)SMTP和STARTTLS
[转自Marion博客]
SMTP协议默认使用明文在网络传送用户名和密码,这是十分危险的。为此,Postfix使用TLS(Transport Layer Security)来解决这个潜在的危险问题。TLS不仅可以用来加密用户名和密码,还可以加密传送信件的内容。Sendmail使用starttls命令来启动一个SMTP会话加密进程(TLS会话)。Micorsoft的outlook软件和其它的许多MUA也支持STARTTLS。
STARTTLS能实现什么样的功能?
1.实现对一次E-MAIL通讯中server端以及client端彼此身份的校验;
2.加密传输信件(身份不能确认的情况下亦可以实现);
3.认证转发;
由于SMTP是一个存储转发协议,并且一封电子邮件的转交投递可能会涉及到多台SMTP服务器,因此,一个MUA就算能够通过认证并把E-MAIL加密传输至将要经过的第一台SMTP服务器,让最终用户的SMTP通过发送端的身份认证,并认为信件传送过程中没有被篡改也是不太可能的。故而,RFC文件声明说一个实现公共Email的SMTP服务器是不必苛求于在传送邮件中实现上述功能的。不过,对于一个私有网络中的邮件传输来讲,能够以要求必需提供有效证书来实现严格的身份认证和加密传输,因为我们可以将证书部署到需要传送EMAIL服务的每个客户端。
当然,尽管基于TLS的SMTP服务所提供的功能不像基于SSL的HTTP服务那样完美,但在服务器和客户端之间的一次认证会话间加密传输用户名和密码却是完全可以实现的。
(二)openssl简明使用手册
[转自Marion博客]
此处简要介绍了使用openssl来生成CA证书、申请证书、颁发证书以及撤销证书的过程,临时补上openssl一课。
1. 首先建立CA密钥:
openssl genrsa -des3 -out ca.key 1024 (创建密钥)
chmod 400 ca.key (修改权限为仅root能访问)
openssl rsa -noout -text -in ca.key (查看创建的证书)
2. 利用CA密钥自签署CA证书:
openssl req -new -x509 -days 3650 -key ca.key -out ca.crt
chmod 400 ca.crt (修改权限为仅root能访问)
openssl x509 -noout -text -in ca.crt (查看创建的证书)
3. 创建服务器证书签署申请:
openssl genrsa -des3 -out client.key 1024
chmod 400 server.key (修改权限为仅root能访问)
openssl rsa -noout -text -in client.key (查看创建的证书)
4. 利用证书签署申请生成请求:
openssl req -new -key client.key -out client.csr
openssl req -noout -text -in client.csr (查看创建的请求)
5.进行证书签署:
这时候需要先设置一下openssl的配置文件。
modify /usr/share/ssl/openssl.cnf并根据这个配置文件创建相应的目录和文件。
在创建了serial文件之后,还需要添加当前的八进制的serial number,如:01
然后执行:
openssl ca -keyfile ca.key -cert ca.crt -in client.csr -out client.pem
6.证书撤销:
openssl ca -keyfile ca.key -cert ca.crt -revoke client.pem
这时数据库被更新证书被标记上撤销的标志,需要生成新的证书撤销列表:
openssl ca -gencrl -keyfile ca.key -cert ca.crt -out crl/test.crl
查看证书撤销列表:
openssl crl -noout -text -in crl/test.crl
证书撤销列表文件要在WEB站点上可以使用,必须将crldays或crlhours和crlexts加到证书中:
openssl ca -gencrl -config /etc/openssl.cnf -crldays 7 -crlexts crl_ext -out crl/sopac-ca.crl
1. 制作CA证书与Private Key给Postfix SSL/TLS
#建立存放CA的文件夹(非必须)
#mkdir /usr/local/postfix/CA
#cd /usr/local/postfix/CA/
使用CA.pl这个脚本可以方便的制作证书,将此脚本复制到刚刚建立的CA目录下
a. Become your own Certificate Authority, so that you can sign your own public key(官方原文)
执行perl脚本
#perl CA.pl -newca [code]
回车后出现如下代码:
[code]CA certificate filename (or enter to create) #此处我直接回车,保持默认证书名称
Making CA certificate ...
Generating a 1024 bit RSA private key
.......++++++
...++++++
writing new private key to './demoCA/private/cakey.pem'
Enter PEM pass phrase: #输入密码
Verifying - Enter PEM pass phrase: #再次输入密码
-----
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) [AU]:CN ←此处为国家名称代码
State or Province Name (full name) [Some-State]:GD ←此处为地域(省)名称
Locality Name (eg, city) []:DG ←此处为城市名称
Organization Name (eg, company) [Internet Widgits Pty Ltd]:lanexpert ←此处为单位名称
Organizational Unit Name (eg, section) []:IT ←此处为部分名称
Common Name (eg, YOUR name) []:lingping ←此处为你的名称或服务器名字或省略
Email Address []:
[email protected] ←此处为管理邮件
Please enter the following 'extra' attributes
to be sent with your certificate request
A challenge password []:123456 ←输入密码
An optional company name []: ←输入公司名称或省略
Using configuration from /usr/local/ssl/openssl.cnf
Enter pass phrase for ./demoCA/private/cakey.pem:
Check that the request matches the signature
Signature ok
Certificate Details:
Serial Number:
c9:46:33:18:b3:8d:33:d2
Validity
Not Before: Jul 14 03:17:09 2008 GMT
Not After : Jul 14 03:17:09 2011 GMT
Subject:
countryName = CN
stateOrProvinceName = GD
organizationName = lanexpert
organizationalUnitName = IT
commonName = lingping
emailAddress =
[email protected]
X509v3 extensions:
X509v3 Subject Key Identifier:
5F:5D:AB:C6:D1:8E:56:7E:01:24:12:16:70:3C:5F:F9:B2:FE:78:C7
X509v3 Authority Key Identifier:
keyid:5F:5D:AB:C6:D1:8E:56:7E:01:24:12:16:70:3C:5F:F9:B2:FE:78:C7
DirName:/C=CN/ST=GD/O=lanexpert/OU=IT/CN=lingping/
[email protected]
serial:C9:46:33:18:B3:8D:33:D2
X509v3 Basic Constraints:
CA:TRUE
Certificate is to be certified until Jul 14 03:17:09 2011 GMT (1095 days)
b. Create an unpassworded private key for host mail.lanexpert.local and create an unsigned public key certificate.(官方原文,大意为创建一个没有密码的私有Key给主机和创建一个没有签名的公共Key证书)
# openssl req -new -nodes -keyout lp-key.pem -out lp-req.pem -days 3650
Generating a 1024 bit RSA private key
.++++++
...........................++++++
writing new private key to 'lp-key.pem'
-----
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) [AU]:CN
State or Province Name (full name) [Some-State]:GD
Locality Name (eg, city) []:DG
Organization Name (eg, company) [Internet Widgits Pty Ltd]:lanexpert
Organizational Unit Name (eg, section) []:IT
Common Name (eg, YOUR name) []:lingping
Email Address []:
[email protected]
Please enter the following 'extra' attributes
to be sent with your certificate request
A challenge password []:123456
An optional company name []:
c. Sign the public key certificate for host mail.lanexpert.local with the Certification Authority private key that we created a few steps ago.
# openssl ca -out lp-cert.pem -infiles lp-req.pem
Using configuration from /usr/local/ssl/openssl.cnf
Enter pass phrase for ./demoCA/private/cakey.pem:
Check that the request matches the signature
Signature ok
Certificate Details:
Serial Number:
c9:46:33:18:b3:8d:33:d3
Validity
Not Before: Jul 14 03:19:53 2008 GMT
Not After : Jul 14 03:19:53 2009 GMT
Subject:
countryName = CN
stateOrProvinceName = GD
organizationName = lanexpert
organizationalUnitName = IT
commonName = lingping
emailAddress =
[email protected]
X509v3 extensions:
X509v3 Basic Constraints:
CA:FALSE
Netscape Comment:
OpenSSL Generated Certificate
X509v3 Subject Key Identifier:
21:B8:4C:AB:8D:D6:E3:E8:E1:14:E4:50:8E:0B:AF:C7:0D:08:4C:86
X509v3 Authority Key Identifier:
keyid:5F:5D:AB:C6:D1:8E:56:7E:01:24:12:16:70:3C:5F:F9:B2:FE:78:C7
Certificate is to be certified until Jul 14 03:19:53 2009 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
d. Install the host private key, the host public key certificate, and the Certification Authority certificate files. This requires super-user privileges.(官方原文)
为了管理方便,将以上创建的证书全部复制到/etc/postfix目录下,并设置相应的权限
#cp demoCA/cacert.pem lp-key.pem lp-cert.pem /etc/postfix/
#chmod 644 /etc/postfix/lp-cert.pem /etc/postfix/cacert.pem
#chmod 400 /etc/postfix/lp-key.pem
2. 修改Postfix配置文件,添加启用TLS选项
#nano /etc/postfix/main.cf
添加以下内容:
# Smtp TLS config
smtp_use_tls = yes
smtp_tls_security_level = may
smtp_tls_session_cache_databases = btree:/var/lib/postfix/smtp_tls_session_cache
smtpd_use_tls = yes
smtp_tls_note_starttls_offer = yes
smtpd_tls_key_file = /etc/postfix/lp-key.pem (注意路径是否正确)
smtpd_tls_cert_file = /etc/postfix/lp-cert.pem (注意路径是否正确)
smtpd_tls_CAfile = /etc/postfix/cacert.pem (注意路径是否正确)
smtpd_tls_loglevel = 1
smtpd_tls_received_header = yes
smtpd_tls_session_cache_timeout = 3600s
smtpd_tls_security_level = may
smtpd_tls_session_cache_databases = btree:/var/lib/postfix/smtpd_tls_session_cache
tls_random_source = dev:/dev/urandom
#nano /etc/postfix/master.cf
启用以下选项:
smtps inet n - n - - smtpd
-o smtpd_tls_wrappermode=yes
-o smtpd_sasl_auth_enable=yes
注意“-o”前面的空格
重新加载Postfix配置文件
检查并测试TLS是否启动成功
Smtp默认SSL端口是465
# netstat -tunlp |grep 465
tcp 0 0 0.0.0.0:465 0.0.0.0:* LISTEN 20559/master
#telnet localhost 25 #测试是否启动TLS加密
Trying 127.0.0.1...
Connected to localhost.localdomain (127.0.0.1).
Escape character is '^]'.
220 Welcome to our mail.lanexpert.cn ESMTP,Warning:Version not Available!
ehlo lanexpert.local
250-mail.lanexpert.local
250-PIPELINING
250-SIZE 14336000
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 #显示此字样为成功启用
quit
quit
Connection closed by foreign host.
在客户端(如Outlook)设置加密的smtp发信测试是否正常
经测试,使用加密后的smtp发送邮件是正常的。
配置文件部分参考资料:
[url]http://www.postfix.org/TLS_README.html[/url]
PS.开始时没有按照官方文档配置(参考此文档
[url]http://www.lihuawei.cn/FreeBSD/d[/url] ... 2_courier-imap.html),出现如下错误(未解决),后来按照官方文档重新配置,此问题解决:
Report:同样telnet localhost 25 ‘out’表示自动出现的,’In’表示手动输入的
Out: 220 Welcome to our mail.lanexpert.cn ESMTP,Warning:Version not Available!
In: ehlo lanexpert.cn
Out: 250-mail.lanexpert.local
Out: 250-PIPELINING
Out: 250-SIZE 14336000
Out: 250-VRFY
Out: 250-ETRN
Out: 250-STARTTLS
Out: 250-AUTH LOGIN PLAIN
Out: 250-AUTH=LOGIN PLAIN
Out: 250-ENHANCEDSTATUSCODES
Out: 250-8BITMIME
Out: 250 DSN
In: starttls
Out: 454 4.7.0 TLS not available due to local problem
In: quit
Out: 221 2.0.0 Bye
二、启用POP3及IMAP加密功能
1.设置POP3s (Port:995)证书产生的配置文件
# nano /usr/local/courier-imap/etc/pop3d.cnf
根据自己的实据情况作出相应的修改:
RANDFILE = /usr/local/courier-imap/share/pop3d.rand
[ req ]
default_bits = 1024
encrypt_key = yes
distinguished_name = req_dn
x509_extensions = cert_type
prompt = no
[ req_dn ]
C=CN #国家代码
ST=GD #地域或省名称
L=DG #城市名称
O=lanexpert #组织名称
OU=IT #单位名称
CN=localhost
[email protected] #管理邮件
[ cert_type ]
nsCertType = server
2.设置IMAPs(Port:993)证书产生的配置文件
# nano /usr/local/courier-imap/etc/imapd.cnf
RANDFILE = /usr/local/courier-imap/share/imapd.rand
[ req ]
default_bits = 1024
encrypt_key = yes
distinguished_name = req_dn
x509_extensions = cert_type
prompt = no
[ req_dn ]
C=CN
ST=GD
L=DG
O=lanexpert
OU=IT
CN=localhost
[email protected]
[ cert_type ]
nsCertType = server
3. 运行以下命令生成安全登陆认证的金钥imapd.pem和pop3d.pem文件,存放于/usr/local/courier-imap/share/目录下
#/usr/local/courier-imap/sbin/mkimapdcert
#/usr/local/courier-imap/sbin/mkpop3dcert
检查证书文件是否成功生成:
# ll /usr/local/courier-imap/share/*.pem
-rw------- 1 root root 1999 Jul 14 12:24 /usr/local/courier-imap/share/imapd.pem
-rw------- 1 root root 2003 Jul 14 12:24 /usr/local/courier-imap/share/pop3d.pem
4. 修改imapd-ssl及pop3d-ssl配置文件,启用imapd-ssl及pop3d-ssl
#nano /usr/local/courier-imap/etc/imapd-ssl
将 IMAPDSSLSTART=NO
改为:
#nano /usr/local/courier-imap/etc/pop3d-ssl
将POP3_STARTTLS=NO
改为:
保存并退出,重启courier-imapd 服务
service courier-imapd restart
检查imapd-ssl及pop3d-ssl启动是否正常:
# netstat -tunlp |grep :993
tcp 0 0 0.0.0.0:993 0.0.0.0:* LISTEN 21361/couriertcpd
# netstat -tunlp |grep :995
tcp 0 0 0.0.0.0:995 0.0.0.0:* LISTEN 21373/couriertcpd
从以上检查结果可知,993&995在正常监听状态中,基本上,imapd-ssl与pop3d-ssl正常启用并运行,可以在客户端(outlook)设置测试接收邮件是否正常来验正其实现情况。
配置部分参考资料:
[url]http://www.lihuawei.cn/FreeBSD/d[/url] ... 2_courier-imap.html