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

标签 azure powershell openssl ssl-certificate

我已从 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:error:0D07207B:asn1 encoding routines:ASN1_get_object:header too long:crypto/asn1/asn1_lib.c:101: error in pkcs12

任何有关此转换的帮助将不胜感激。提前致谢!

最佳答案

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

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

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

最终的命令是:

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

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

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

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

关于azure - 将 Base-64 pfx 转换为可用的 pkcs#12,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68167562/

相关文章:

azure - 没有从 Azure Devops 收到组审批者的通知

Powershell 匹配行列表中的精确字符串

windows - 无法在 Windows 上从/usr/local/ssl/openssl.cnf 加载配置信息

c++ - C++ 中的 Winsock2 + OpenSSL : How can I catch their exceptions?

Tomcat tcnative 在 solaris 上构建

azure - 尽管具有全局管理员角色,但无法删除租户的广泛 azure 策略

azure - 即使满足条件,逻辑应用程序在第一个警报后也不会触发警报

c# - CloudBlockBlob 的 URI 在 C# 中给出特殊字符

windows - 使用 PowerShell 的 IIS 输出缓存设置

powershell - 如何将ScriptStackTrace保留在远程计算机上的Invoke-Command中引发的异常中?