如何在 Windows 上生成 .pfx 和 .cer 文件

How to generate a .pfx and .cer file on Windows

几年前,我参与了这个 .NET Standard 项目,该项目使用私钥(在受密码保护的 .pfx 文件中)及其相应的 public 密钥(在 .cer 文件中)对一些内容进行签名验证签名。

在代码中,我使用 .NET 的 X509Certificate2 获取相应的密钥来签署和验证签名。

现在,我已经记不起在Windows上用什么工具生成这些文件了。有人能指出我正确的方向吗?查看 openssl,我可以生成 public/private 个密钥对,但不是 X509Certificate2 可以读取的格式。

X.509 证书包含 公钥,但与公钥不同。 X509Certificate2 可以读取证书或包含证书的 PKCS7,或包含证书 匹配私钥以及可能的任何相关 'chain' 证书的 PFX/PKCS12 ),但它不能读取(并且更普遍地不能存在)这样的公钥或私钥本身。要签名,您需要第二种选择(证书和私钥)。

openssl 程序可以通过三个基本步骤的不同组合以多种方式创建 PFX/PKCS12:

  1. 在文件中生成实际的密钥对(私钥和公钥)

  2. 为公钥创建或获取证书。这本身可以通过较小的步骤完成,具体取决于您是否使用来自 'real'(外部)CA 的证书,例如 LetsEncrypt、GoDaddy、Digicert;或者来自私人或个人 CA,例如 Windows AD/CS,或您自己;或 'self-signed' 证书(不是来自任何 CA),它在大多数情况下不受信任,但可用于测试,或用于仅将证书用作公钥容器格式而不用于信任的应用程序,听起来您的情况可能就是这样。

  3. 将来自 1 的私钥和来自 2 的证书,加上任何 'chain' 证书(如果 required/desired)合并到 PFX/pkcs12

对于 self-signed 证书(最简单的情况,正如我所说的听起来适合你),openssl 可以在

的单个操作中执行 1 和 2
openssl req [-config conffile] -newkey $spec -keyout keyfile [-nodes] -x509 $otherfields -out certfile
# $spec can be rsa:$size or dsa:$paramfile or ecdsa:$paramfile
# $otherfields can include subject name (else it is prompted or set per the config)
# and/or -days $num -addext $type:$value -days $num -$hash; see the man page
# and/or many existing questions, or at least ask more specifically,
# but if you're not using the cert for trust probably none of these matter
#
# for all openssl commands except genpkey the flags may be given in any order
# but I show a sequence-based order that can be more easily compared 

或者可以使用几种方法之一执行 1,然后使用一个或两个部分执行 2

# 1: old methods
openssl genrsa $size [-$cipher] >keyfile
openssl gendsa $size >keyfile
openssl ecparam -curve $name -genkey [-noout] >keyfile
# or 1: new method
openssl genpkey {-algorithm rsa | -paramfile $dsa_or_ecdsa_file} [-pkeyopt $name:$value]... >keyfile
#
# then 2: single step
openssl req [-config conffile] -new -key keyfile -x509 $otherfields >certfile
# or 2: two steps
openssl req [-config conffile] -new -key keyfile $someotherfields >reqfile
openssl x509 -req <reqfile -signkey keyfile [-extfile $file [-extsect $sect]] -days $num -$hash >certfile

或者它可以将 1 与 2 的第一部分组合,然后是 2 的第二部分

openssl req [-config conffile] -newkey $spec -keyout keyfile [-nodes] $someotherfields >reqfile
openssl x509 -req <reqfile -signkey keyfile [-extfile $file [-extsect $sect]] -days $num $hash >certfile 

对于 CA-issued 证书,第 1 步和第 2 步的第一部分 (req -new[key]) 分开或组合是相同的,但第 2 步的第二部分 (x509 -req -signkey) 替换为向 CA 提交 reqfile 并取回证书的过程;这又千差万别,一个答案无法解决,所以我不会尝试。

在上述任一步骤之后 openssl 第 3 步(总是简单):

openssl pkcs12 -export -in certfile -inkey keyfile -out pfxfile
# add -certfile chaincerts if and as needed

比较 How to generate a self-signed SSL certificate using OpenSSL?,它显示了过去十年中该领域的发展,并更多地考虑了使证书特别受浏览器信任的问题,但没有像我上面那样系统地列出选项不包括第 3 步(尽管许多其他问题分别涵盖了该步骤:搜索 'convert PEM to PKCS12' 或 'convert PEM to PFX')。 如果您直接在 Windows(不是 WSL)上使用 OpenSSL,例如来自 ShiningLight 的手册页可在 https://www.openssl.org/docs/manpages.html .

在线获取

最后,作为替代方案,powershell 可以使用 New-SelfSignedCertificate since at least Eight; I think it was in Seven but can't check now. It generates the keypair and certificate corresponding to steps 1 and 2 but (both) in the Windows store instead of files, so you follow with Export-PFXCertificate 将它们放入 PFX 文件中(或者您可以使用 MMC/certmgr.msc 中的导出任务,但是如果您你已经很时髦了吗?)。