Search code examples
azurepowershellcertificate

PowerShell Export Pfx from Azure Key Vault using Az.KeyVault


I am creating a certificate inside Azure Key Vault and then attempting to export it with private key as a 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

However, the PFX file is not valid

Error with Get-PnPAzureCertificate

Error with Get-PnPAzureCertificate

Error with Certificate Import

Error with Certificate Import

Any ideas? Using Import-AzKeyVaultCertificate is not an option because there's a bug with it in environments that have policies that forces key lengths

Also, might be worth mentioning that I am using PowerShell 7


Solution

  • According to my test, we need to change keytype as RSA when we create cert policy.

    For example

    $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