How to serialize and deserialize a PFX certificate in Azure Key Vault?

后端 未结 5 2071
被撕碎了的回忆
被撕碎了的回忆 2020-11-30 10:02

I have a bunch of strings and pfx certificates, which I want to store in Azure Key vault, where only allowed users/apps will be able to get them. It is not hard to do store

5条回答
  •  陌清茗
    陌清茗 (楼主)
    2020-11-30 10:33

    Here's a PowerShell script for you. Replace the file path, password, vault name, secret name.

    $pfxFilePath = 'C:\mycert.pfx'
    $pwd = '123'
    $flag = [System.Security.Cryptography.X509Certificates.X509KeyStorageFlags]::Exportable
    $collection = New-Object System.Security.Cryptography.X509Certificates.X509Certificate2Collection 
    $collection.Import($pfxFilePath, $pwd, $flag)
    $pkcs12ContentType = [System.Security.Cryptography.X509Certificates.X509ContentType]::Pkcs12
    $clearBytes = $collection.Export($pkcs12ContentType)
    $fileContentEncoded = [System.Convert]::ToBase64String($clearBytes)
    $secret = ConvertTo-SecureString -String $fileContentEncoded -AsPlainText –Force
    $secretContentType = 'application/x-pkcs12'
    Set-AzureKeyVaultSecret -VaultName 'myVaultName' -Name 'mySecretName' -SecretValue $Secret -ContentType $secretContentType
    

    This is a common question, so we are going to polish this up and release as a helper.

    The script above strips the password because there's no value in having a password protected PFX and then storing the password next to it.

提交回复
热议问题