|
1 | 1 | function Get-CryptoProviders { |
2 | | - # Retrieves the list of available Crypto Service Providers using certutil |
| 2 | + # Retrieves the list of available Crypto Service Providers. |
| 3 | + # |
| 4 | + # Preferred source is the registry because CSP names are stored as |
| 5 | + # culture-invariant subkey names under |
| 6 | + # HKLM:\SOFTWARE\Microsoft\Cryptography\Defaults\Provider |
| 7 | + |
3 | 8 | try { |
4 | | - Write-Information "[VERBOSE] Retrieving Crypto Service Providers using certutil..." |
5 | | - $certUtilOutput = certutil -csplist |
6 | | - |
7 | | - # Parse the output to extract CSP names |
| 9 | + Write-Information "[VERBOSE] Retrieving Crypto Service Providers from registry..." |
| 10 | + |
| 11 | + $regPath = 'HKLM:\SOFTWARE\Microsoft\Cryptography\Defaults\Provider' |
8 | 12 | $cspInfoList = @() |
9 | | - foreach ($line in $certUtilOutput) { |
10 | | - if ($line -match "Provider Name:") { |
11 | | - $cspName = ($line -split ":")[1].Trim() |
12 | | - $cspInfoList += $cspName |
| 13 | + |
| 14 | + if (Test-Path -LiteralPath $regPath) { |
| 15 | + $cspInfoList = @( |
| 16 | + Get-ChildItem -LiteralPath $regPath -ErrorAction Stop | |
| 17 | + Select-Object -ExpandProperty PSChildName |
| 18 | + ) |
| 19 | + } |
| 20 | + |
| 21 | + if ($cspInfoList.Count -eq 0) { |
| 22 | + Write-Information "[VERBOSE] Registry enumeration returned no CSPs; falling back to certutil." |
| 23 | + |
| 24 | + $prevOutEncoding = [Console]::OutputEncoding |
| 25 | + try { |
| 26 | + [Console]::OutputEncoding = [System.Text.Encoding]::UTF8 |
| 27 | + $certUtilOutput = & certutil.exe -csplist 2>&1 |
| 28 | + } finally { |
| 29 | + [Console]::OutputEncoding = $prevOutEncoding |
| 30 | + } |
| 31 | + |
| 32 | + # The "Provider Name:" label is localized, so match on the structural |
| 33 | + # shape of the line instead of the English text: an un-indented |
| 34 | + # "<label>: <value>" line whose value is not a numeric provider type |
| 35 | + # ("1 - PROV_RSA_FULL", etc.). |
| 36 | + foreach ($line in $certUtilOutput) { |
| 37 | + if ($line -match '^\S[^:]+:\s+(\S.+)$') { |
| 38 | + $value = $Matches[1].Trim() |
| 39 | + if ($value -notmatch '^\d+\s*-\s*\S') { |
| 40 | + $cspInfoList += $value |
| 41 | + } |
| 42 | + } |
13 | 43 | } |
14 | 44 | } |
15 | 45 |
|
16 | 46 | if ($cspInfoList.Count -eq 0) { |
17 | | - throw "No Crypto Service Providers were found. Ensure certutil is functioning properly." |
| 47 | + throw "No Crypto Service Providers were found." |
18 | 48 | } |
19 | 49 |
|
20 | 50 | Write-Information "[VERBOSE] Retrieved the following CSPs:" |
|
0 commit comments