Openssl X509 v3 AuthorityKeyIdentifier实验与逻辑分析

Openssl是X509的事实标准,目前主流OS或个别安全性要求较高的设计场景,对X509的证书链验证已经不在停留在只从数字签名校验了,也就是仅仅从公钥验签的角度,在这些场景中,往往还会校验AuthorityKeyIdentifier和SubjectKeyIdentifier的一致性,也即下级证书的AuthorityKeyIdentifier应该与上级证书的SubjectKeyIdentifier一致,这两个参数是X509 v3 extensions的范围。

但是X509对SubjectKeyIdentifier与AuthorityKeyIdentifier本身以及它们之前的一致性校验逻辑,都没有严格的规定,因此我们只能follow Openssl的逻辑。

Openssl的SubjectKeyIdentifier可以为hash,此时是公钥的160bit sha-1散列,或者是一个hex字符串,此时是人为预设的SubjectKeyIdentifier。

Openssl的AuthorityKeyIdentifier是follow X509定义的,AuthorityKeyIdentifier实际上是一个组,有三个组成员,keyid,dirname,serialnum,keyid就是上级证书的SubjectKeyIdentifier。你可以提供authoritykeyidentifier=keyid,issuer,作为openssl参数,它会尽量把以上三个组成员给填满,为什么说尽量呢,因为上级证书可能压根就没有SubjectKeyIdentifier这个v3 extension属性。

本篇重点放在这里,Issuer的正确概念是一个签发机关,而Subject是证书所属部门,证书的Issuer是可以随意指定的,这容易理解,比如三级证书,root->ca->work,work的Issuer被指定为ca,这无可厚非,它说明work证书,是由二级证书ca签发的。但是,AuthorityKeyIdentifier中的Issuer并非是直接上级证书的Subject,而是Root证书的Subject。AuthorityKeyIdentifier其他两项参数都是跟随直接证书的,但唯独这个Issuer是跟随根证书的,这里表达的是整体"签发机关",如果把Issuer搞混了,一定过不了openssl verify。可利用x509ExtensionUtils.createAuthorityKeyIdentifier(signercert)来提取AKI。

Openssl的证书链校验,如果提供了issuer_checks参数也是可以校验以上一致性的。

openssl配置subjectKeyIdentifier = hash,此时openssl在签发证书时,会加入subjectKeyIdentifier参数,如果subjectKeyIdentifier这个参数没有的话,缺省是不会有任何subjectKeyIdentifier被加入的。

此时如果authorityKeyIdentifier = keyid,issuer,那么只有issuer有效,也即会加入上级证书的dirname,serialnum。

当subjectKeyIdentifier = hash;authorityKeyIdentifier = keyid,issuer时,产生的下级证书如下图。

当没有subjectKeyIdentifier = hash;authorityKeyIdentifier = keyid,issuer:always时,产生的下级证书如下图。其中issuer:always强制加入dirname,serialnum两项。

当没有subjectKeyIdentifier配置;仅有authorityKeyIdentifier = keyid,issuer时,产生的下级证书如下图。可以看到CA证书没有 subjectKeyIdentifier,下级证书没办法复制到subjectKeyIdentifier因此authorityKeyIdentifier 仅有其他两项。

以上各种情况,均可以通过以下命令验证通过。

openssl verify -CAfile cacert.pem --issuer_checks certwork.pem

以下为用到的配置文件与测试命令。

复制代码
[ req ]
distinguished_name = req_distinguished_name
policy             = policy_match
x509_extensions     = v3_ca

# For the CA policy
[ policy_match ]
countryName             = optional
stateOrProvinceName     = optional
organizationName        = optional
organizationalUnitName  = optional
commonName              = supplied
emailAddress            = optional

[ req_distinguished_name ]
countryName                     = Country Name (2 letter code)
countryName_default             = IN
countryName_min                 = 2
countryName_max                 = 2
stateOrProvinceName             = State or Province Name (full name) ## Print this message
stateOrProvinceName_default     = KARNATAKA ## This is the default value
localityName                    = Locality Name (eg, city) ## Print this message
localityName_default            = BANGALORE ## This is the default value
0.organizationName              = Organization Name (eg, company) ## Print this message
0.organizationName_default      = GoLinuxCloud ## This is the default value
organizationalUnitName          = Organizational Unit Name (eg, section) ## Print this message
organizationalUnitName_default  = Admin ## This is the default value
commonName                      = Common Name (eg, your name or your server hostname) ## Print this message
commonName_max                  = 64
emailAddress                    = Email Address ## Print this message
emailAddress_max                = 64

[ v3_ca ]
#subjectKeyIdentifier = hash
authorityKeyIdentifier = keyid,issuer
basicConstraints = critical,CA:true
nsComment = "OpenSSL Generated Certificate"

[ v3_work ]
subjectKeyIdentifier = hash
authorityKeyIdentifier = keyid:awlays,issuer:always
basicConstraints = critical,CA:true
nsComment = "OpenSSL Generated Certificate"
复制代码
openssl genrsa -out private.pem 2048

openssl req -new -x509 -days 3650 -config opensslroot.cfg -key private.pem -out cacert.pem

openssl x509 -text -noout -in cacert.pem

openssl genrsa -out workkey.pem 2048

openssl req -new -key workkey.pem -config opensslroot.cfg -out certwork.csr

openssl req -text -in certwork.csr

openssl x509 -req -days 365 -CA cacert.pem -extfile opensslroot.cfg -extensions v3_work -CAcreateserial -CAkey private.pem -in certwork.csr -out certwork.pem

openssl x509 -in certwork.pem -text
相关推荐
Lsetea1 天前
curl指定CA目录仍报证书不受信任:--capath与OpenSSL rehash排查
运维·https·ssl·openssl·curl
Lsetea2 天前
OpenSSL报path length constraint exceeded:CA层级与pathlen排查
运维·https·ssl证书·openssl·证书链
Lsetea2 天前
OpenSSL s_client退出码为0却证书验证失败:严格校验与Shell管道排查
https·shell·ssl证书·openssl·tls
Lsetea2 天前
OpenSSL报permitted subtree violation:证书SAN与CA名称约束排查
运维·https·ssl证书·openssl·证书链
Lsetea3 天前
OpenSSL verify报error 20:CAfile、fullchain与本地信任链分层排查
nginx·https·ssl证书·openssl·证书链
Lsetea11 天前
证书没到期却报certificate has expired:OpenSSL定位中间证书与系统时间
运维·https·ssl证书·openssl·证书链
Lsetea11 天前
Nginx报No required SSL certificate was sent:mTLS客户端证书排查
运维·nginx·https·ssl·openssl
Lsetea13 天前
部署证书后提示“证书名称与输入不匹配”:Safari 报错的三层排查
nginx·https·safari·ssl证书·openssl
Lsetea14 天前
OpenSSL快速生成域名证书:含SAN的自签命令、验证与排错
nginx·https·ssl证书·openssl·自签证书
Lsetea17 天前
CDN换了SSL证书仍显示旧证书?按边缘节点、SNI和缓存逐层排查
https·cdn·ssl证书·openssl·tls