将Base-64 pfx转换为可用pkcs#12



我已经从API导出了一个基本的64编码证书,现在需要将其转换为pkcs12格式以上传到Azure。下面的PowerShell脚本完美地完成了这项工作,但我在将其转换为OpenSSL时遇到了问题。

$base64value = Get-Content -Path $base64FilePath
$byteArray = [System.Convert]::FromBase64String($base64value)
$cert = [System.Security.Cryptography.X509Certificates.X509Certificate2]::new($byteArray, $certPw, [System.Security.Cryptography.X509Certificates.X509KeyStorageFlags]::Exportable)
# Export the certificate as a PFX file
$bytesPfx = $cert.Export([System.Security.Cryptography.X509Certificates.X509ContentType]::Pfx, $certPw)
[System.IO.File]::WriteAllBytes($pkcs12FilePath, $bytesPfx)

我已经尝试了以下2个OpenSSL命令。。。

openssl base64 -d -a -in [base64.pfx] -out [converted.pfx]
openssl enc -base64 -d -in [base64.pfx] -out [converted.pfx]

但当我尝试使用证书时,这两个都会返回相同的错误:

34359836736:错误:0D07207B:asn1编码例程:asn1_get_object:标头太长:crypto/asn1/asn1_lib.c:101:pkcs12 错误

如有任何关于此转换的帮助,我们将不胜感激。提前感谢!

您必须使用-A(大写(参数,而不是-a

根据OpenSSL的文档,他们的base64解码器默认限制为76个字符。因此,pkcs12二进制文件的预期输出实际上被截断到了这个极限。这就是由于生成的文件损坏而导致header too long错误的原因。

文档建议您可以使用-A选项来克服此限制。

最后的命令是:

openssl base64 -d -A -in certificate.b64 -out certificate.pfx

奖金,如果你想将其进一步转换为PEM文件:

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

如果您只需要客户端证书,还可以包含-clcerts选项。

您可以尝试以下

从base64 openssl二进制格式转换pfx

openssl enc -base64 -d -in base64.pfx -out converted.pfx

然后将其转换为pkcs12:

openssl pkcs12 -in converted.pfx -out bundle.pem -clcerts -nodes

最新更新