【问题标题】:Convert Azure-generated certificate to PFX using openssl使用 openssl 将 Azure 生成的证书转换为 PFX
【发布时间】:2021-02-06 14:02:25
【问题描述】:

我使用 Azure 门户中的 RunAs 帐户创建了一个自动化帐户。证书已自动生成。我想使用 openssl 实用程序从此证书创建一个 PFX 文件。

我可以通过以下步骤和代码使用 PowerShell Core 7.1.0-rc.2 来做到这一点:

  1. Azure 门户 > Azure Active Directory > 应用注册 > 自动化帐户的服务主体
  2. 从左侧菜单中选择 Manifest,在 JSON 中向下滚动到“keyCredentials”部分
  3. 将“value”属性的整个字符串复制到 PowerShell 中的变量:
    $base64value = "<contents of the value property here>"
  4. 用于创建 PFX 文件的 PowerShell 代码:
# Create a X509 certificate object
$byteArray = [System.Convert]::FromBase64String($base64value)
$cert = [System.Security.Cryptography.X509Certificates.X509Certificate2]::new($byteArray)

# Export the certificate as a PFX file
$bytesPfx = $cert.Export([System.Security.Cryptography.X509Certificates.X509ContentType]::Pfx)
[System.IO.File]::WriteAllBytes('filename.pfx', $bytesPfx)

鉴于自动化帐户的 JSON 清单中的 base64 字符串值,我想弄清楚的是如何使用 openssl 实用程序执行相同的过程。由于我能够将该 base64 字符串转换为字节数组,然后使用 .NET 类的构造函数转换为 X509 证书,我想我需要使用 openssl x509,但我找不到任何采用 base64 的选项字符串或二进制参数或文件。

【问题讨论】:

  • 如果filename.pfx 包含公钥和私钥,那么byteArray 已经代表一个PFX。因此,您应该能够对 blob 进行 base64 解码并直接将其保存为 PFX。

标签: azure openssl x509certificate2


【解决方案1】:

好的,感谢@bartonjs 的提示。我最终从 Linux shell 执行此操作:

echo "<huge base64-encoded string from value property>" > base64.data.txt
base64 --decode base64.data.txt > test01.pfx

然后我测试了使用该 PFX 文件调用 X509Certificate2 类的 .NET 构造函数,它成功创建了证书对象。

【讨论】:

    猜你喜欢
    • 2011-03-07
    • 2010-10-22
    • 2023-03-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-09-14
    相关资源
    最近更新 更多