使用openssl将Azure生成的证书转换为PFX



我已经从Azure门户创建了一个自动化帐户,该帐户具有RunAs帐户。证书已自动生成。我想使用openssl实用程序从这个证书创建一个PFX文件。

我可以使用PowerShell Core 7.1.0-rc.2通过以下步骤和代码来完成:

  1. Azure门户>Azure Active Directory>应用程序注册>自动化账户服务主体
  2. 从左侧菜单中选择Manifest,在JSON中向下滚动到";keyCredentials";截面
  3. 复制"的整个字符串;值";属性设置为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)

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

好的,谢谢@bartonjs的提示。我最终在一个Linux外壳上完成了这项工作:

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

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