如何使用OpenSSL从PFX文件生成.pem格式的CA证书和客户端证书。
在 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
然后,您可以配置HAProxy使用file.combo.pem文件。
需要在两个不同的步骤中指定带有密钥和不带密钥的文件,原因是如果您有一个同时包含加密和解密密钥的文件(如HAProxy),在使用它时仍会提示您键入密码。
您可以使用OpenSSL命令行工具。以下命令应该能解决问题。
openssl pkcs12 -in client_ssl.pfx -out client_ssl.pem -clcerts
openssl pkcs12 -in client_ssl.pfx -out root.pem -cacerts
如果您希望对文件进行密码保护等操作,则有其他选项。
您可以在此处阅读完整的文档here。
openssl
时,会出现“openssl pfx to pem error:0D0680A8:asn1 encoding routines:asn1_check_tlen:wrong”错误。 - Chris Halcrow尽管其他答案都是正确且详细解释的,但我在理解它们时遇到了一些困难。这里是我使用的方法(来自这里):
openssl pkcs12 -in filename.pfx -out cert.pem -nodes
从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
pkcs12
后面加上 -nodes
来禁用 key.pem 的 PEM 密码设置。这样做可以使内容更加通俗易懂,但不改变原来的意思。 - leonheess您可以使用以下方法从.pfx文件中提取根证书,.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
建议在一个命令中结合密码参数和转换操作,以避免错误。
例如: 此命令用于提取私钥。
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