如何使用 SSH 签署文件并使用证书颁发机构验证它?

如何使用 SSH 签署文件并使用证书颁发机构验证它?

我想让一定数量的开发人员能够在系统上运行代码,并使用 OpenSSH 的-Y verify功能对其进行验证。

# Generate the certificates, and sign the dev cert with the root CA
ssh-keygen -t ed25519 -C "Example Root CA" -N "" -f ./rootca
ssh-keygen -t ed25519 -C "Some Developer Inc." -N "" -f ./developer
ssh-keygen -s ./rootca -I [email protected] ./developer.pub  # <- Creates ./developer-cert.pub

# Sign "example.bin"
echo "hello" > ./example.bin
ssh-keygen -Y sign -f ./developer -n "[email protected]" ./example.bin

现在,给定“example.bin”、“example.bin.sig”、“rootca.pub”和“developer-cert.pub”,您如何验证它?

ssh-keygen -Y verify命令不接受“developer-cert.pub”文件的参数,该文件对于完成 CA 和签名之间的信任链至关重要。

我可以使用ssh-keygen -L,但它似乎不安全(实际上检查证书中的根 CA 签名?)。

看来我目前唯一的选择是使用 libssh,还有其他方法吗?

编辑:感谢@user1686 的回答,我完成了示例:

# Generate the certificates
ssh-keygen -t ed25519 -C "Example Root CA" -N "" -f ./rootca
ssh-keygen -t ed25519 -C "Some Developer Inc." -N "" -f ./developer

# Sign the dev cert with the root CA, granting a "codesign+foo" entitlement
ssh-keygen -s ./rootca -I [email protected] -n [email protected] ./developer.pub  # <- Creates ./developer-cert.pub

# Sign "example.bin" using the "[email protected]" namespace (doesn't grant any entitlements, just used for security)
echo "hello" > example.bin
ssh-keygen -Y sign -f ./developer-cert.pub -n "[email protected]" ./example.bin

# Verifying the "[email protected]" entitlement
echo "[email protected] cert-authority $(cat rootca.pub)" > allowed_signers.conf
ssh-keygen -Y verify -I "[email protected]" -n "[email protected]" -s example.bin.sig -f allowed_signers.conf < example.bin

答案1

您必须指定证书创建签名时,这样它就会嵌入到签名的public_key字段中,而不是普通的公钥中。(签名时,ssh-keygen 会自动找到附近的私钥,-f developer-cert.pub但反之则不会。)

稍后验证时,将根据其中嵌入的公钥来验证签名,并根据“允许的签名者”文件(其中可以使用 指定您的 CA)来检查公钥cert-authority

(OpenSSH 证书格式不支持中间 CA,因此没有像 X.509 中那样嵌入或指定“证书链”的选项,并且“根 CA”一词没有太大意义。)

相关内容