【问题标题】:PowerShell Export Pfx from Azure Key Vault using Az.KeyVaultPowerShell 使用 Az.KeyVault 从 Azure Key Vault 导出 Pfx
【发布时间】:2021-07-22 19:38:09
【问题描述】:

我正在 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://docs.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 出错

证书导入出错

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

另外,值得一提的是我正在使用 PowerShell 7

【问题讨论】:

    标签: azure powershell certificate


    【解决方案1】:

    根据我的测试,我们需要在创建证书策略时将keytype更改为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://docs.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
    

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-07-28
    • 2018-07-13
    • 2021-04-24
    • 2021-10-29
    • 2020-05-04
    • 2018-03-24
    • 2021-08-23
    • 2022-07-22
    相关资源
    最近更新 更多