标签归档:CentOS 6

使用Let’s Encrypt的Certbot为ngxin生成ECDSA证书

更新

20170312 本文是以CentOS 6.8系统为基础的,而对于CentOS 7系统,certbot则包含在了EPEL源中,可以启用EPEL后直接安装,且命令也由certbot-auto改为了certbot,但是两者的命令参数是通用的。
256位的ECDSA密钥提供的安全性和3072位的RSA密钥相当,而对于大多数网站来说,2048位RSA密钥提供的安全性已经足够。ECDSA证书在算法和密钥长度上的优势可以提供更快的HTTPS访问速度,但浏览器和平台的支持度不如后者广泛。Nginx 1.11.0版本即开始支持ECDSA和RSA双证书配置,可以通过同时配置RSA证书来解决ECDSA证书的兼容性问题。以下内容主要是记录一下如何用Let's Encrypt官方推荐的Certbot生成ECDSA证书,亦为ECC证书。取得Certbot环境、全自动生成和更新RSA证书和注意事项,不再赘述。
主要参考:
Status of and instructions for EC certification generation using CertBot?

详细步骤:
1 进入Certbot工作目录

cd /etc/certbot/

2 生成ECDSA私钥
使用secp384r1曲线算法

openssl ecparam -genkey -name secp384r1 > ec.key

3 生成支持多域名的证书请求文件CSR
certbot目前只能以–csr的方式加载证书请求文件来生成ECDSA的证书,并且要求csr为der格式csr已支持der和pem格式
使用–csr时,要将需要申请证书的域名全部包含在csr中,如co1dawn.com和www.co1dawn.com,即需要生成多域名的CSR。
3.1 方法一(推荐直接使用方法二)

cp /usr/local/ssl/openssl.cnf /etc/certbot/
vi openssl.cnf

在[ req ]区块找到并去掉注释“#”:

req_extensions = v3_req # The extensions to add to a certificate request

在[ v3_req ] 加入如下内容:

subjectAltName = @alt_names
[ alt_names ]
DNS.1 = co1dawn.com
DNS.2 = www.co1dawn.com

修改后的样子:

[ v3_req ]

# Extensions to add to a certificate request

basicConstraints = CA:FALSE
keyUsage = nonRepudiation, digitalSignature, keyEncipherment
subjectAltName = @alt_names
[ alt_names ]
DNS.1 = co1dawn.com
DNS.2 = www.co1dawn.com

通过openssl使用-config参数生成多域名证书请求文件:

openssl req -new -sha384 -key ec.key -out ec-der.csr -outform der -config /etc/certbot/openssl.cnf

只需填入Common Name (e.g. server FQDN or YOUR name) []:co1dawn.com即可。前几项输入“.”,即为空;最后后两项留空即可。

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]:.
State or Province Name (full name) [Some-State]:.
Locality Name (eg, city) []:.
Organization Name (eg, company) [Internet Widgits Pty Ltd]:.
Organizational Unit Name (eg, section) []:.
Common Name (e.g. server FQDN or YOUR name) []:co1dawn.com
Email Address []:.

Please enter the following 'extra' attributes
to be sent with your certificate request
A challenge password []:
An optional company name []:

3.2 方法二,更为简单,为推荐使用的方法

按需要做相应更改:

openssl req -new -sha384 -key ec.key -subj "/CN=co1dawn.com" -reqexts SAN -config <(cat /usr/local/ssl/openssl.cnf <(printf "[SAN]\nsubjectAltName=DNS:co1dawn.com,DNS:www.co1dawn.com")) -outform der -out ec-der.csr

/usr/local/ssl/openssl.cnf为openssl编译安装时所在位置,根据实际情况更改,考虑到安全性和兼容性,请编译升级到最新版的openssl 1.0.2
3.3查看csr是否正确

openssl req -inform der -in ec-der.csr -noout -text
        ...
        Subject: CN=co1dawn.com
        ...
            X509v3 Subject Alternative Name:
                DNS:co1dawn.com, DNS:www.co1dawn.com
        ...

4 使用certbot生成ECDSA证书

./certbot-auto certonly --webroot -w /var/www/html/ -d co1dawn.com -d www.co1dawn.com --email "youremail@youremail.com" --csr "/etc/certbot/ec-der.csr"

具体命令的意义可参照之前的部分

IMPORTANT NOTES:
 - Congratulations! Your certificate and chain have been saved at
   /etc/certbot/0001_chain.pem. Your cert will expire on
   2016-11-29. To obtain a new or tweaked version of this certificate
   in the future, simply run certbot-auto again. To non-interactively
   renew *all* of your certificates, run "certbot-auto renew"
 - If you like Certbot, please consider supporting our work by:

   Donating to ISRG / Let's Encrypt:   https://letsencrypt.org/donate
   Donating to EFF:                    https://eff.org/donate-le

如不指定获取的证书存放目录时,默认放在cerbot程序所在目录,即/etc/certbot/。
生成三个证书:

0000_cert.pem  0000_chain.pem   0001_chain.pem

与certbot自动化生成并更新的RSA证书的对应关系是:

   0000_cert.pem   = cert.pem
   0000_chain.pem  = chain.pem
   0001_chain.pem  = fullchain.pem

在nginx中用到的是0000_chain.pem和0001_chain.pem,具体使用方法和前面一致

使用cloudflare的chacha20/poly1305补丁编译nginx,在ngxin中配置ECDSA和RSA双证书后,需要提供并提升ECDSA和chacha20/poly1305的优先级,ssl_ciphers可参考该文进行配置:

ssl_ciphers 'ECDHE+aECDSA+CHACHA20:ECDHE+aRSA+CHACHA20:ECDHE+aECDSA+AESGCM:ECDHE+aRSA+AESGCM:ECDHE+aECDSA+AES256+SHA384:ECDHE+aRSA+AES256+SHA384:ECDHE+aECDSA+AES256+SHA:ECDHE+aRSA+AES256+SHA';

5 局限性
5.1 OCSP stapling
目前Let’s encrypt使用的中间证书和RSA证书是同一个,因此如果同时使用certbot生成的ECDSA和RSA双证书时,在nginx的OCSP stapling配置部分的ssl_trusted_certificate用0000_chain.pem或者chain.pem都可以,目前这两个中间证书其实是同一个证书。官方预计在2017年3月31日前使用ECDSA算法的中间证书;
5.2 无法自动更新
现在certbot还不能像RSA证书那样智能化的生成并更新ECDSA证书,只能手动运行,而且再次运行时文件名会递增,需要更改文件名或在nginx中更改证书位置。
6 展望
Certbot的作者们早已在github上讨论直接生成双证书的可行性,那将大大简化配置双证书的复杂度,只是还没有具体的时间表。

在CentOS 6 的LNMP服务器上部署Let’s Encrypt的SSL证书

Let's Encrypt官方推荐的客户端certbot自2016年2月3日#2344开始真正支持Python 2.6,而在此之前需要Python 2.7,盲目升级CentOS 6 自带的Python 2.6到2.7,可能导致yum不能使用等问题。certbot-auto作为certbot的自动化脚本可以自动安装依赖并自动配置运行环境,使得获取Let's Encrypt证书简便快捷。

系统要求:本文是在CentOS 6.8已经部署好LNMP的服务器上部署certbot-auto,需要拥有服务器的root权限,在certbot准备运行环境阶段需要的内存较大,实测至少需要240M以上的内存
主要参考:
Let’s Encrypt and Nginx
建立个人博客,推荐使用搬瓦工年付19.99美元KVM虚拟VPS,512M内存,10G硬盘,500G月流量。

详细步骤:

1 取得 certbot-auto

访问https://certbot.eff.org/,在

I'm using Software on System.

分别选择nginx,CentOS 6,将弹出安装使用的详细方法。
1.1 首先需要安装certbot所依赖的源

yum install epel-release

1.2 下载certbot,并赋予运行权限
假设将certbot放在 /etc/certbot/目录下,

mkdir /etc/certbot/ && cd /etc/certbot/
wget https://dl.eff.org/certbot-auto

赋予运行权限:

chmod +x ./certbot-auto

安装依赖并配置自动运行环境:

./certbot-auto

2 运行certbot-auto取得证书

对于正常运行的LNMP服务器来说,推荐使用–webroot插件来获取证书,可以不必停止nginx的服务器的正常运行,否则需要停止nginx。
2.1 取得证书的命令如下:

/etc/certbot/certbot-auto certonly --webroot -w /var/www/html/ -d co1dawn.com -d www.co1dawn.com --staple-ocsp --hsts --rsa-key-size 4096 -m youremail@youremail.com

上述命令详解如下:

certonly     生成证书,但不安装。对于nginx,安装模式目前支持尚不完善,我们自己在nginx中配置证书。

–webroot   借助正在运行nginx的网站所在目录来生成证书请求的临时文件,例:/var/www/html/.well-known/

-w               用来指定目前正常运行nginx的服务器的网站所在目录,假设为:/var/www/html/

-d                co1dawn.com,www.co1dawn.com是两个不同的域名,根据自己的实际情况填写。

–staple-ocsp   开启证书的OCSP验证。

–hsts             强制浏览器使用加密连接访问网站。

–rsa-key-size 4096  请求4096bit的证书,默认为2048位已经足够。

-m                 自己的电子邮件地址。

2.2 可能碰到的错误

If you're using the webroot plugin, you should also verify that you are serving files from the webroot path you provided.

需要在nginx的配置文件中SSL区块中指定certbot的临时目录

       location ~ /.well-known {
                   allow all; 
       }

2.3 生成的证书位置
顺利的话,会给出成功的提示,并显示证书所在位置。

IMPORTANT NOTES:
 - Congratulations! Your certificate and chain have been saved at /etc/letsencrypt/live/co1dawn.com/fullchain.pem (success)

3 在nginx中配置证书

共获得一枚私钥,三枚证书,我们需要用到其中的三个,在nginx的配置文件中SSL区块配置如下L:

...
ssl on;
ssl_certificate /etc/letsencrypt/live/co1dawn.com/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/co1dawn.com/privkey.pem;

#Enable SSL stapling on;
ssl_stapling_verify on;
ssl_trusted_certificate /etc/letsencrypt/live/co1dawn.com/chain.pem;
resolver 8.8.8.8 8.8.4.4 valid=300s;
resolver_timeout 5s;
...

然后重启nginx服务:

service nginx restart

4 自动更新证书

90天的时间还是太短,因此需要自动更新证书
4.1 测试是否能自动更新

/etc/certbot/certbot-auto renew --dry-run

成功后有提示,但不会真正生成新的证书。
4.2 安装定时任务crontab

yum install -y vixie-cron

或者

yum install -y cronie

启动定时任务

service crond start

设crontab为开机自动启动

chkconfig crond on

4.3 将certbot-auto renew 加入定时运行

crontab -e

写入下列任务并保存退出:

14 06 * * * /etc/certbot/certbot-auto renew --post-hook "sudo service nginx reload" --quiet >> /var/log/certbot.log

如果出现

Unable to find post-hook command sudo in the PATH.
(PATH is /usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin)

说明是以root权限运行的,可以去掉sudo,或者建立非root用户再运行。
这样每天早上6点14分,自动运行证书更新请求,并将运行日志记录在 /var/log/certbot.log,方便查看。