标签归档:ecdsa

CentOS 7 上使用Certbot申请通配符证书(ACMEv2 Wildcard Certificates)

通配符证书(泛域名证书)对于小博客来说,毫无用处,但是要赶个时髦。本文记录下申请RSA和ECDSA通配符证书的过程。

1 安装Certbot

签署通配符证书需要Certbot 0.22以上。如果以前安装过certbot,一般是直接yum update即可。如果是全新安装,则如下:
先升级:

yum update -y

查看系统版本:

cat  /etc/centos-release CentOS Linux release 7.4.1708 (Core)

安装epel源:

yum install epel-release -y

安装certbot:

yum install certbot -y

查看certbot版本:

certbot --version certbot 0.22.0

2 申请RSA通配符证书的过程

2.1 用如下命令申请证书

co1dawn.com和*.co1dawn.com换成自己的域名;执行该命令时不依赖nginx。

certbot -d co1dawn.com -d *.co1dawn.com --manual --preferred-challenges dns-01 --server https://acme-v02.api.letsencrypt.org/directory certonly --agree-tos

输入应急邮箱,证书到期前会有邮件提示:

Enter email address (used for urgent renewal and security notices) (Enter 'c' to
cancel):

如果想跳过输入邮箱的步骤,可在申请命令后面加上:

--register-unsafely-without-email

之后出现如下提示:要公开记录申请该证书的IP地址,是否同意?不同意就无法继续。

-------------------------------------------------------------------------------
NOTE: The IP of this machine will be publicly logged as having requested this
certificate. If you're running certbot in manual mode on a machine that is not
your server, please ensure you're okay with that.

Are you OK with your IP being logged?
-------------------------------------------------------------------------------
(Y)es/(N)o: y

同意之后,出现如下提示,第一个“Press Enter to Continue”处直接回车,第二个“Press Enter to Continue”不要按回车:

-------------------------------------------------------------------------------
Please deploy a DNS TXT record under the name
_acme-challenge.co1dawn.com with the following value:

iLS0NjcdP3RR1KphB6xbbVnKS_NS2uMW-xdDRzz85OM

Before continuing, verify the record is deployed.
-------------------------------------------------------------------------------
Press Enter to Continue             #此处直接回车

-------------------------------------------------------------------------------
Please deploy a DNS TXT record under the name
_acme-challenge.co1dawn.com with the following value:

f3V7aw5GPm5yzNsJFanQQaUFMyVQcqriUe3UjIDUHn0

Before continuing, verify the record is deployed.
-------------------------------------------------------------------------------
Press Enter to Continue             #此处不要按回车
2.2 为DNS解析增加TXT记录

进入自己域名的DNS记录管理页面,增加两条TXT记录,多数情况下,仅需在域名(Name)处填入_acme-challenge,在内容(Target)处填入上一步Certbot生成的内容即可,不同DNS提供商处可能会略有不同,根据实际情况修改:

    Name                                     Target
_acme-challenge             iLS0NjcdP3RR1KphB6xbbVnKS_NS2uMW-xdDRzz85OM
_acme-challenge             f3V7aw5GPm5yzNsJFanQQaUFMyVQcqriUe3UjIDUHn0

稍等片刻,等TXT记录解析生效。查看是否生效的命令和生效后的查询结果如下:

host -t txt _acme-challenge.co1dawn.com
_acme-challenge.co1dawn.com descriptive text "iLS0NjcdP3RR1KphB6xbbVnKS_NS2uMW-xdDRzz85OM" 
_acme-challenge.co1dawn.com descriptive text "f3V7aw5GPm5yzNsJFanQQaUFMyVQcqriUe3UjIDUHn0"
2.3 继续申请证书

当第2.2步查看TXT记录解析成功后,回到申请证书的第2.1步处,直接回车,等待:

Waiting for verification...
Resetting dropped connection: acme-v02.api.letsencrypt.org
Cleaning up challenges

IMPORTANT NOTES:
 - Congratulations! Your certificate and chain have been saved at:
   /etc/letsencrypt/live/co1dawn.com-0001/fullchain.pem
   Your key file has been saved at:
   /etc/letsencrypt/live/co1dawn.com-0001/privkey.pem
   Your cert will expire on 2018-06-14. To obtain a new or tweaked
   version of this certificate in the future, simply run certbot
   again. To non-interactively renew *all* of your certificates, run
   "certbot 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

这表示已成功生成新的证书和密钥,修改nginx的配置文件定位新证书和密钥的位置后重启nginx即可。

3 申请ECDSA通配符证书

3.1 首先是生成支持通配符证书的请求文件

步骤请参考这篇文章:使用Let’s Encrypt的Certbot为ngxin生成ECDSA证书,以下内容中的文件名基本和这篇文章相同。
生成ECDSA私钥:

openssl ecparam -genkey -name secp384r1 > ec.key

生成通配符证书的请求文件的命令需要改为:

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:*.co1dawn.com")) -outform der -out ec-der.csr

ec.key 是自己生成的私钥
co1dawn.com 改成自己的域名
ec-der.csr 支持通配符证书的请求文件,假设放到/usr/local/src下,下面会用到

3.2 申请通配符证书

步骤和申请默认的RSA通配符证书基本一致,而且TXT记录相同,无需再次添加了。

certbot -d co1dawn.com -d *.co1dawn.com --manual --preferred-challenges dns-01 --server https://acme-v02.api.letsencrypt.org/directory certonly --csr "/usr/local/src/ec-der.csr"

之后一路回车即可。

使用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上讨论直接生成双证书的可行性,那将大大简化配置双证书的复杂度,只是还没有具体的时间表。