azure - PowerShell 使用 Az.KeyVault 从 Azure Key Vault 导出 Pfx

标签 azure powershell certificate

我正在 Azure Key Vault 中创建证书,然后尝试使用私钥将其导出为 PFX。

# Create new Certificate in Key Vault
$policy = New-AzKeyVaultCertificatePolicy -SecretContentType "application/x-pkcs12" -SubjectName "CN=contoso" -IssuerName "Self" -ValidityInMonths 12 -ReuseKeyOnRenewal -KeySize 4096 -KeyType 'RSA-HSM';
Add-AzKeyVaultCertificate -VaultName $VaultName -Name $ADServicePrincipalCertificateName -CertificatePolicy $policy;

# From https://learn.microsoft.com/en-us/powershell/module/az.keyvault/get-azkeyvaultcertificate?view=azps-5.8.0
# Export new Key Vault Certificate as PFX
$securePassword = "fBoFXYD%dg^Q" | ConvertTo-SecureString -AsPlainText -Force; # This is a throwaway password
$certificate = Get-AzKeyVaultCertificate -VaultName $VaultName -Name $ADServicePrincipalCertificateName;
$secret = Get-AzKeyVaultSecret -VaultName $vaultName -Name $certificate.Name -AsPlainText;
$secretByte = [Convert]::FromBase64String($secret)
$x509Cert = New-Object System.Security.Cryptography.X509Certificates.X509Certificate2($secretByte, "", "Exportable,PersistKeySet")
$type = [System.Security.Cryptography.X509Certificates.X509ContentType]::Pfx
$pfxFileByte = $x509Cert.Export($type, $securePassword);
[System.IO.File]::WriteAllBytes("C:\Repos\Certificate.pfx", $pfxFileByte)
Get-PnPAzureCertificate -Path "C:\Repos\Certificate.pfx" -Password $securePassword

但是,PFX 文件无效

Get-PnPAzureCertificate 出错

Error with Get-PnPAzureCertificate

证书导入错误

Error with Certificate Import

有什么想法吗?使用 Import-AzKeyVaultCertificate 不是一个选项,因为在具有强制 key 长度的策略的环境中存在错误

此外,可能值得一提的是我正在使用 PowerShell 7

最佳答案

根据我的测试,我们在创建证书策略时需要将 key 类型更改为RSA

例如

$VaultName=""
$ADServicePrincipalCertificateName=""
$policy = New-AzKeyVaultCertificatePolicy -SecretContentType "application/x-pkcs12" `
   -SubjectName "CN=contoso.com" -IssuerName "Self" `
   -ValidityInMonths 12 -ReuseKeyOnRenewal `
   -KeySize 4096 -KeyType 'RSA';
Add-AzKeyVaultCertificate -VaultName $VaultName -Name $ADServicePrincipalCertificateName -CertificatePolicy $policy;

Start-Sleep -Seconds 30
# From https://learn.microsoft.com/en-us/powershell/module/az.keyvault/get-azkeyvaultcertificate?view=azps-5.8.0
# Export new Key Vault Certificate as PFX
$securePassword = "fBoFXYD%dg^Q" | ConvertTo-SecureString -AsPlainText -Force; # This is a throwaway password
$certificate = Get-AzKeyVaultCertificate -VaultName $VaultName -Name $ADServicePrincipalCertificateName;
$secret = Get-AzKeyVaultSecret -VaultName $vaultName -Name $certificate.Name -AsPlainText;
$secretByte = [Convert]::FromBase64String($secret)
$x509Cert = New-Object System.Security.Cryptography.X509Certificates.X509Certificate2($secretByte, "", "Exportable,PersistKeySet")
$type = [System.Security.Cryptography.X509Certificates.X509ContentType]::Pfx
$pfxFileByte = $x509Cert.Export($type, $securePassword);
[System.IO.File]::WriteAllBytes("E:\Certificate.pfx", $pfxFileByte)
Get-PnPAzureCertificate -Path "E:\Certificate.pfx" -Password $securePassword

enter image description here

关于azure - PowerShell 使用 Az.KeyVault 从 Azure Key Vault 导出 Pfx,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67317310/

相关文章:

xml - 在PowerShell中将exiftool XML读入变量

iis - 即使添加到 curl-ca-bundle.crt,curl openssl 也无法验证 IIS 7 自签名证书

c# - HttpClient 证书错误

.net - 订阅不具有类型 'S0' 的 SKU 'QnAMaker' 所需的 QuotaId/功能

azure - 用于访问 Cloudlyn 的 API

powershell - 使用 PowerShell 递归查找文件扩展名的数量

Java 在 cmd 中运行,而不是在 PowerShell 中运行

iOS:用于 Beta 测试/TestFlight 上传的 iOS 应用分发

c# - LINQ 语句中的条件 IF 语句

azure - 是否可以通过用户租户上的 ARM 模板在 Azure 上自动注册应用程序?