Skip to content

Instantly share code, notes, and snippets.

@JayDoubleu
Last active November 21, 2023 10:28
Show Gist options
  • Save JayDoubleu/f7442fea645df01b1c5fc3e27eece0f9 to your computer and use it in GitHub Desktop.
Save JayDoubleu/f7442fea645df01b1c5fc3e27eece0f9 to your computer and use it in GitHub Desktop.
Code to extract all Windows store certificates as PEM to be used by WSL behind corporate SSL proxy
function Export-CACertificates {
    param(
        [string]$OutputFile = 'all_ca_certificates.pem',
        [ValidateSet("DOS", "UNIX")]
        [string]$NewLineFormat = "DOS"
    )

    $newline = if ($NewLineFormat -eq "DOS") { "`r`n" } else { "`n" }

    $certificateType = [System.Security.Cryptography.X509Certificates.X509Certificate2]
    $includedStores = @("TrustedPublisher", "Root", "CA", "AuthRoot")

    $certificates = $includedStores.ForEach({
        Get-ChildItem Cert:\CurrentUser\$_ | Where-Object { $_ -is $certificateType}
    })

    $pemCertificates = $certificates.ForEach({
        $pemCertificateContent = [System.Convert]::ToBase64String($_.RawData,1)
        "-----BEGIN CERTIFICATE-----$newline${pemCertificateContent}$newline-----END CERTIFICATE-----"
    })

    $uniquePemCertificates = $pemCertificates | select -Unique
    $uniquePemCertificates -join $newline | Set-Content -Path $OutputFile -NoNewline
}
# Export-ModuleMember -Function Export-CACertificates
# Import-Module Export-CACertificates
# Export-CACertificates -OutputFile "my_ca_certificates_dos.pem" -NewLineFormat "DOS"
Export-CACertificates -OutputFile "my_ca_certificates_unix.pem" -NewLineFormat "UNIX"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment