Retrieve PFX Certificate from Azure Key Vault with Powershell

WordPress PFX Certificate Key Vault Powershell

PFX (Personal Information Exchange Format) Certificates serve the purpose of encrypting data. For example, encrypting traffic to your WordPress website. Or encrypting data in transfer between two machines.

Therefore, encryption gives us confidence that a bad actor in the middle of the transfer will not be able to view the raw or plain data. Therefore, PFX gives us security.

If you happen to be using Microsoft Azure then no doubt you came across Key Vault. The purpose of Key Vault is to provide secure store for your sensitive configuration. This can also include PFX Certificates.

I assume you already have Key Vault provisioned in Azure and you have already uploaded your Certificate into it. Therefore, all we are concerned here in this article is to use Powershell to extract the PFX Certificate from Key Vault. We’ll then be able to pass it down stream – be it file system or another process.

Common scenario is to access Key Vault in Azure DevOps pipelines, so we’ll use this hypothetical example below.

The following script takes hold of the Certificate Key stored in Key Vault as $(KV_Key). In addition, we also acquire the actual PFX Certificate from Key Vault with this variable $(KV_Cert). Once we have these two pieces of information, we can re-create the PFX Certificate, as demonstrated below.

$CertKey = '$(KV_Key)'

$PfxUnprotectedBytes = [Convert]::FromBase64String('$(KV_Cert)')

$Pfx = New-Object System.Security.Cryptography.X509Certificates.X509Certificate2Collection

$Pfx.Import($PfxUnprotectedBytes, $null, [Security.Cryptography.X509Certificates.X509KeyStorageFlags]::Exportable)

$PfxProtectedBytes = $Pfx.Export([Security.Cryptography.X509Certificates.X509ContentType]::Pkcs12, $CertKey)

$CertPfx = [System.Convert]::ToBase64String($PfxProtectedBytes)

I mentioned earlier that you can use PFX Certificates to secure access to websites, such as WordPress. If you’d want to improve Search Engine Optimisation (SEO) on your WordPress site then you’d also want to make sure your WordPress site is always serving content over HTTPS protocol.

This is explained in one of my other posts on How to redirect HTTP to HTTPS on WordPress.

Marcin Narloch

Marcin Narloch

Creative and out-of-the-box thinker with strong interests and knowledge in technology and innovation.
WordPress Google Analytics tracking plugin Previous post Writing Custom Plugin for Google Analytics in WordPress
Reset Git Branch Commits Next post Azure Authentication Cheat Sheet

Leave a Reply

Your email address will not be published.