使用openssl将pfx转换为pem

问题描述 投票:0回答:6

如何使用 OpenSSL 从 PFX 文件生成

.pem
CA 证书客户端证书

openssl pem pfx
6个回答
296
投票

在 Linux 上执行此操作的另一个角度...以下是如何执行此操作,以便生成的单个文件包含解密的私钥,以便 HAProxy 之类的东西可以使用它而不提示您输入密码。

openssl pkcs12 -in file.pfx -out file.pem -nodes

然后您可以配置 HAProxy 以使用 file.pem 文件。


这是对以前版本的编辑,我执行了多个步骤,直到我意识到 -nodes 选项只是简单地绕过了私钥加密。但我把它留在这里,因为它可能对教学有所帮助。

openssl pkcs12 -in file.pfx -out file.nokey.pem -nokeys
openssl pkcs12 -in file.pfx -out file.withkey.pem
openssl rsa -in file.withkey.pem -out file.key
cat file.nokey.pem file.key > file.combo.pem
  1. 第一步会提示您输入密码以打开PFX。
  2. 第二步提示您输入该加号,并为密钥创建一个密码。
  3. 第3步提示您输入刚刚设置的密码来存储解密。
  4. 第四个将所有内容放入 1 个文件中。

然后您可以配置 HAProxy 以使用 file.combo.pem 文件。

之所以需要 2 个单独的步骤,其中指定一个文件包含密钥,另一个文件不包含密钥,是因为如果您有一个文件同时包含加密密钥和解密密钥,HAProxy 之类的东西仍然会提示您输入密码当它使用它时。


146
投票

您可以使用 OpenSSL 命令行工具。以下命令应该可以解决问题

openssl pkcs12 -in client_ssl.pfx -out client_ssl.pem -clcerts

openssl pkcs12 -in client_ssl.pfx -out root.pem -cacerts

如果您希望您的文件受到密码保护等,那么还有其他选项。

您可以在此处阅读整个文档。


106
投票

尽管其他答案是正确的并且解释得很透彻,但我发现理解它们有些困难。这是我使用的方法(取自这里):

第一种情况:将 PFX 文件转换为包含证书和私钥的 PEM 文件:

openssl pkcs12 -in filename.pfx -out cert.pem -nodes

第二种情况:将 PFX 文件转换为单独的公钥和私钥 PEM 文件:

将私钥从 PFX 提取到 PEM 文件:

openssl pkcs12 -in filename.pfx -nocerts -out key.pem

导出证书(仅包含公钥):

openssl pkcs12 -in filename.pfx -clcerts -nokeys -out cert.pem

从提取的私钥中删除密码(释义)(可选):

openssl rsa -in key.pem -out server.key

7
投票

您可以使用它从 .pfx 中提取 ca-bundle、.crt 和 .key。

# Extracting ca-certs..."
  openssl pkcs12 -in ${filename}.pfx -nodes -nokeys -cacerts -out ${filename}-ca.crt

# Extracting key file..."
  openssl pkcs12 -in ${filename}.pfx -nocerts -out ${filename}.key

# Extracting crt..."
  openssl pkcs12 -in ${filename}.pfx -clcerts -nokeys -out ${filename}.crt

# combine ca-certs and cert files
  cat  ${filename}.crt ${filename}-ca.crt > ${filename}-full.crt

# Removing passphrase from keyfile"
  openssl rsa -in ${filename}.key -out ${filename}.key

链接:https://gist.github.com/mediaupstream/a2694859b1afa59f26be5e8f6fd4806a


2
投票

对于使用密码锁定的 PFX

建议将密码参数与转换结合在一个命令中,以避免错误。

像这样: 该命令用于提取私钥

openssl pkcs12 -in "blablabla.pfx" -out key.key -nodes -passin pass:blablabla   

这个命令用于提取公钥

openssl pkcs12 -in "blablabla.pfx" -clcerts -nokeys -out crt.crtpem -nodes -passin pass:blablabla

0
投票

是的,正如本所说,如果您的证书有密码,您必须在 openssl 命令上提供 --passin pass:xxxxx 参数,否则您的 pem 文件将无法正确生成。

openssl pkcs12 -in file.pfx -out file.pem -nodes -passin pass:cert_pass
© www.soinside.com 2019 - 2024. All rights reserved.