OPENSSL中X509V3的多域名配置详解(含其它说明)

X509V3的全部详细属性请参照openssl官网以下内容:https://www.openssl.org/docs/manmaster/man5/x509v3_config.html

 

由于目前网上介绍自签发证书的内容大多数仅为最基本的内容,但是对于使用多个域名以及内网IP同时访问的用户来说,基本的自签发证书无法满足要求。直到有一天看到了google的证书里面的内容包含“使用者可选名称”。

该名称用于多域以及IP的申明。还有授权信息访问,CRL分发点等内容,这些内容在网上大多数教程中均没说明如何使用。

这些内容,都属于X509V3的相关配置,而这些配置,都在OPENSSL官网有说明。

 

在OPENSSL进行证书申请以及签发的时候,都可以通过直接指定配置文件来对证书或证书申请进行定制。下面,基于我现在使用的站点证书配置文件逐项进行说明。

 

命令行示例

 

首先,示例命令行,一个完整的服务器证书签发流程分为生成CA私钥,生成服务器私钥,生成服务器证书请求,通过CA证书以及私钥对服务器证书请求签发证书几个步骤。每个步骤示例命令行如下:

 

生成CA私钥,注意,后面为私钥位数,最大16384,至少2048,因为现在1024位会被认为不安全,而大于16384私钥签出来的证书WINDOWS不能正常识别且有一定几率导致后面签署服务器证书失败

openssl genrsa  -out ca.key 16384

生成服务器私钥

openssl genrsa -out server.key 16384

签发CA证书

其中-extensions 参数用于指定按照配置文件中的哪部分配置进行签发。

-subj 用于指定具体的证书DN-config 用于指定配置文件路径

openssl req  -new -x509 -sha512 -days 36600 -key ca.key -out ca.crt  -config D:\PHPX64\openssl\program\bin\0all.cfg  -subj "/C=CN/ST=SC/L=ChengDu/O=elsesky/OU=elsesky/CN=ca.elsesky" -extensions v3_ca

生成服务器请求

subj 用于指定服务的证书DN

-reqexts 用于指定按照配置文件中的哪部分配置生成请求

openssl req -new -
out server.csr -ke
y server.key -subj "/C=CN/ST=SC/L=ChengDu/O=elsesky/OU=elsesky/CN=*.elsesky.bid" -config D:\PHPX64\openssl\program\bin\0all.cfg -sha512 -reqexts v3_sign

X509含多域的方式签发:

-extensions 参数用于指定按照配置文件中的哪部分配置进行签发。

-extfile 用于指定配置文件路径

openssl x509 -req -in server.csr -out server.crt -CA ca.crt -CAkey ca.key -sha512 -days 366000 -extensions v3_sign -extfile D:\PHPX64\openssl\program\bin\0all.cfg -CAcreateserial

配置文件示例 

根据上述命令行,整个过程中调用的均为同一配置文件0all.cfg,只是调用的区域不同。

其中签发CA证书所使用的区域为v3_ca

生成服务器证书请求以及签发服务器证书时所使用的区域为v3_sign

配置文件0all.cfg的内容如下:

[ req ]
default_bits  = 2048
default_keyfile  = privkey.pem
distinguished_name = req_distinguished_name
string_mask = utf8only

[ req_distinguished_name ]

[ v3_ca ]
#用于签CA的根证
subjectKeyIdentifier=hash
authorityKeyIdentifier=keyid:always,issuer
basicConstraints = critical,CA:true
keyUsage = nonRepudiation, digitalSignature, keyEncipherment,dataEncipherment, keyAgreement, keyCertSign, cRLSign
extendedKeyUsage = serverAuth,clientAuth,OCSPSigning,emailProtection,codeSigning
noCheck = ignored
crlDistributionPoints=crldp_section

[ v3_sign ]
#用于生成服务器证书请求的扩展属性,以及签发服务器证书的扩展属性(共用)
subjectKeyIdentifier=hash
basicConstraints = critical,CA:FALSE
keyUsage = nonRepudiation, digitalSignature, keyEncipherment,dataEncipherment, keyAgreement, keyCertSign, cRLSign
extendedKeyUsage = serverAuth,clientAuth,OCSPSigning,emailProtection,codeSigning
noCheck = ignored
authorityInfoAccess = caIssuers;URI:http://ca.elsesky.bid/ca.html,OCSP;URI:http://ocsp.elsesky.bid/
nameConstraints=permitted;IP:0.0.0.0/0.0.0.0,permitted;DNS:*.elsesky.bid,permitted;DNS:*.elsesky.net,permitted;DNS:*.elsesky.org,permitted;DNS:*.fakesky.bid
crlDistributionPoints=crldp_section
subjectAltName = @alt_names
certificatePolicies=ia5org,1.3.6.1.4.1.11129.2.5.1,2.23.140.1.2.2,@polsect
#ARBITRARY EXTENSIONS定义
1.3.5.8=ASN1:SEQUENCE:content_list

[content_list]
detail = UTF8:This is elsesky cert.
desc = UTF8:Elsesky selfsigned cert for self.

#CRL的相关配置
[crldp_section]
fullname=URI:https://ca.elsesky.bid/ca.crl
CRLissuer=dirName:issuer_sect
reasons=keyCompromise, CACompromise
[issuer_sect]
C=CN
O=elsesky
CN=ca.elsesky

#服务器证书的扩展使用名
[ alt_names ]
DNS.1 = *.elsesky.bid
DNS.2 = *.elsesky.net
DNS.3 = *.elsesky.org
DNS.4 = *.fakesky.bid
IP.1 = 10.0.2.1
IP.2 = 10.0.2.2
IP.3 = 10.0.2.3
IP.4 = 10.0.2.4
IP.5 = 10.0.2.5
email.1 = elsesky@elsesky.bid,RID:1.0

#证书策略配置
[polsect]
policyIdentifier = 1.3.5.8
CPS.1="http://crl.elsesky.bid/"
CPS.2="http://ocsp.elsesky.bid/"
CPS.3="http://cps.elsesky.bid/"
userNotice.1=@notice
[notice]
explicitText="Please visit https://blog.elsesky.bid to view details"
organization="elsesky"
noticeNumbers=4,4,6,5

关于签发的证书是否为CA的设置:

basicConstraints = critical,CA:true

当CA属性设置为true时,代表该签发的证书为CA证书。在签发服务器证书的时候需要设置为false

distinguished_name

该配置用于指定证书DN,由于在命令行中已经显式指定,在配置文件中可以留空,但必须保留该配置项。

 

X509 V3 最常用配置

多域兼容配置

该配置主要通过在配置文件中申明subjectAltName实现。

借用官方文档的说法:

Subject Alternative Name.

The subject alternative name extension allows various literal values to be included in the configuration file. These include email (an email address) URI a uniform resource indicator, DNS(a DNS domain name), RID (a registered ID: OBJECT IDENTIFIER), IP (an IP address), dirName(a distinguished name) and otherName.

The email option include a special 'copy' value. This will automatically include any email addresses contained in the certificate subject name in the extension.

The IP address used in the IP options can be in either IPv4 or IPv6 format.

The value of dirName should point to a section containing the distinguished name to use as a set of name value pairs. Multi values AVAs can be formed by prefacing the name with a +character.

otherName can include arbitrary data associated with an OID

总结起来,就是SAN既可以为我们常用的域名(也支持泛域名),也可以是邮箱、IP、甚至用户DN以及自定义属性(通过申明othername来实现)。

官方的例子如下:

 subjectAltName=email:copy,email:my@other.address,URI:http://my.url.here/
 subjectAltName=IP:192.168.7.1
 subjectAltName=IP:13::17
 subjectAltName=email:my@other.address,RID:1.2.3.4
 subjectAltName=otherName:1.2.3.4;UTF8:some other identifier

 subjectAltName=dirName:dir_sect

 [dir_sect]
 C=UK
 O=My Organization
 OU=My Unit
 CN=My Name

借用上文中的示例配置文件来对应说明:

#首先,申明subjectAltName所调用的配置区域
subjectAltName = @alt_names

#在配置区域中申明服务器证书的扩展使用名
#该配置支持多值属性,如果为单值,只需要显式指定属性名,不用跟数字。
#如果为多值,则需后面跟“.数字”
#该配置的对象可以为:DNS、IP、email、URI、othername
#DNS用于指定该证书所支持的域名
#IP用于指定该证书所支持的IP地址
#enamil用于指定该证书所支持的邮件地址
#URI用于指定该证书所支持的具体URL连接
#othername用于指定该证书所支持的其它自定义识别标识符
[ alt_names ]
DNS.1 = *.elsesky.bid
DNS.2 = *.elsesky.net
DNS.3 = *.elsesky.org
DNS.4 = *.fakesky.bid
IP.1 = 10.0.2.1
IP.2 = 10.0.2.2
IP.3 = 10.0.2.3
IP.4 = 10.0.2.4
IP.5 = 10.0.2.5
email.1 = elsesky@elsesky.bid,RID:1.0
#上述配置文件表明该证书支持域名为*.elsesky.bid、*.elsesky.net、*.elsesky.org、*.fakesky.bid以及IP为10.0.2.1至5的所有网站

 PS:openssl的配置文件均支持简单多值申明或者完整多值申明。上文中采用的完整多值申明,便于查看。如果为了简介,可以采用简单多值申明,即上文中的

subjectAltName=email:copy,email:my@other.address,URI:http://my.url.here/

值与值之间用","(英文逗号)隔开,键值对之间用":"(英文冒号)连接,例如上面的配置可以写成:

subjectAltName = DNS.1:*.elsesky.bid,DNS.2 : *.elsesky.net,DNS.3: *.elsesky.org,DNS.4: *.fakesky.bid,IP.1 :10.0.2.1,IP.2:10.0.2.2,IP.3:10.0.2.3,IP.4:10.0.2.4,IP.5:10.0.2.5,email.1:elsesky@elsesky.bid,RID:1.0

注意,每个变量在配置文件中只能申明一次,如果多次申明,则只有最后一个生效。

keyUsage

该选项对应证书的“密钥用法

用于指定该证书的用途,主要包含以下内容

nonRepudiation
digitalSignature
keyEncipherment
dataEncipherment
keyAgreement
keyCertSign
cRLSign 
encipherOnly
decipherOnly

 extendedKeyUsage

该选项对应证书的“增强型密钥用法

用于指定该证书的扩展用途,主要包含以下内容

(官方资料)

Value                  Meaning
 -----                  -------
 serverAuth             SSL/TLS Web Server Authentication.
 clientAuth             SSL/TLS Web Client Authentication.
 codeSigning            Code signing.
 emailProtection        E-mail Protection (S/MIME).
 timeStamping           Trusted Timestamping
 OCSPSigning            OCSP Signing
 ipsecIKE               ipsec Internet Key Exchange
 msCodeInd              Microsoft Individual Code Signing (authenticode)
 msCodeCom              Microsoft Commercial Code Signing (authenticode)
 msCTLSign              Microsoft Trust List Signing
 msEFS                  Microsoft Encrypted File System

 X509 V3 其它配置

理论上,配置好subjectAltName之后就没什么需要额外配置的了,如果大家喜欢花式作死玩创意,则可继续往下看。

noCheck

该选项对应证书的“OCSP不撤销检查

用于申明不撤销OCSP检查,就一个固定值:ignored,在配置文件中这样写:

noCheck = ignored

 

authorityInfoAccess

该选项对应证书的“授权信息访问

用于申明授权信息访问,至于具体干啥大家自己看英文去吧,总之这货能各种自定义内容,比如申明证书颁发机构的颁发者以及它的访问连接或者申明OSCP的访问连接等。

 官方例子:

 authorityInfoAccess = OCSP;URI:http://ocsp.my.host/
 authorityInfoAccess = caIssuers;URI:http://my.ca/ca.html

 nameConstraints

该选项对应证书的“名称限制

可以是IP、DNS、email。操作包含Permitted和excluded

申明方式参见上文例子

官方例子:

 nameConstraints=permitted;IP:192.168.0.0/255.255.0.0

 nameConstraints=permitted;email:.somedomain.com

 nameConstraints=excluded;email:.com

 

crlDistributionPoints

该选项对应证书的“CRL分发点

该选项的用途为指定访问该证书的对象去何处获取该证书对应CA所签发的证书吊销列表,以确认该证书现在是否处于可用状态。(如果看不明白前面这句话,就只需要知道这又是一个可以玩出各种花样的配置项就行)

官方例子简单版:

 crlDistributionPoints=URI:http://myhost.com/myca.crl
 crlDistributionPoints=URI:http://my.com/my.crl,URI:http://oth.com/my.crl

完整申明版:

 crlDistributionPoints=crldp1_section

 [crldp1_section]

 fullname=URI:http://myhost.com/myca.crl
 CRLissuer=dirName:issuer_sect
 reasons=keyCompromise, CACompromise

 [issuer_sect]
 C=UK
 O=Organisation
 CN=Some Name

 

certificatePolicies

该选项对应证书的“证书策略

具体干啥看官网说明吧,总之可以指定各种自定义内容

官方例子:

 certificatePolicies=ia5org,1.2.3.4,1.5.6.7.8,@polsect

 [polsect]

 policyIdentifier = 1.3.5.8
 CPS.1="http://my.host.name/"
 CPS.2="http://my.your.name/"
 userNotice.1=@notice

 [notice]

 explicitText="Explicit Text Here"
 organization="Organisation Name"
 noticeNumbers=1,2,3,4

ARBITRARY EXTENSIONS

该部分用于进行扩展定义(即在证书中加入自己的扩展内容)

官方例子:

 1.2.3.4=critical,ASN1:UTF8String:Some random data

 1.2.3.4=ASN1:SEQUENCE:seq_sect

 [seq_sect]

 field1 = UTF8:field1
 field2 = UTF8:field2

这样申明之后,在签发的服务器证书中就会多出1.2.3.4这样一项内容

关于X509V3的其它内容,由于可玩性不高,就不介绍了,有需要的同学自行查官网。