错误信息
x509: invalid signature: parent certificate cannot sign this kind of certificate
也可能输出为
- x509: certificate signed by unknown authority (possibly because of "x509: invalid signature: parent certificate cannot sign this kind of certificate" while trying to verify candidate authority certificate "Example Issuing Server")
Go 把它包装为“x509: certificate signed by unknown authority (possibly because of \"x509: invalid signature: parent certificate cannot sign this kind of certificate\" while trying to verify candidate authority certificate \"<issuer CN>\")”。其中的颁发者名称来自你的证书链。
Go crypto/x509 — ConstraintViolationError: the issuer is not a CA (basicConstraints CA:FALSE or missing keyCertSign)
含义
Go 为某张证书找到了一个候选颁发者(即末尾引号中的名称),但该颁发者不允许签发证书:它的 basicConstraints 为 CA:FALSE(或者缺失),或者它的 keyUsage 中没有 keyCertSign。因此 Go 丢弃了这个候选者,又没有其他路径可用,于是报告“certificate signed by unknown authority”,并引用这个原因作为可能的起因。
原因
用非 CA 证书签发的证书
常见自制的 PKI 脚本用另一张服务器证书来签发服务器证书,或者用默认(非 CA)配置创建了“中间证书”。
没有 keyCertSign 的 CA 证书
偶见在 Go 看来,keyUsage 只有 digitalSignature(缺少 keyCertSign)的中间证书不能签发证书。
证书链文件中混入了非 CA 证书
偶见如果证书包中本该放中间证书的位置放的是另一张叶子证书,Go 就会尝试把那张叶子证书当作颁发者。
修复方法
- 1.
检查颁发者的 basicConstraints 和 keyUsage
引号中的候选证书必须具有 CA:TRUE 和 keyCertSign。OpenSSL 3.5 把同一缺陷报告为错误 79:“invalid CA certificate”。
bash # Every issuing certificate needs CA:TRUE (and keyCertSign if keyUsage is present) openssl x509 -in intermediate.pem -noout -subject -ext basicConstraints,keyUsage openssl verify -CAfile root.pem -untrusted intermediate.pem server.pem # OpenSSL 3.5: error 79, invalid CA certificate - 2.
将中间证书重新签发为 CA 证书
签发时设置 basicConstraints critical CA:TRUE(按需设置 pathlen)以及 keyUsage keyCertSign、cRLSign,然后重新签发叶子证书,或者把它们重新接到新的证书链上。
bash # Issue the intermediate as a real CA printf 'basicConstraints=critical,CA:TRUE,pathlen:0\nkeyUsage=critical,keyCertSign,cRLSign\n' > ca.ext openssl x509 -req -in int.csr -CA root.pem -CAkey root.key -days 1825 -extfile ca.ext -out int.pem