|
| 1 | +<# |
| 2 | + .SYNOPSIS |
| 3 | + Sets TLS/SSL protocol registry values for enabling or disabling protocols. |
| 4 | +
|
| 5 | + .DESCRIPTION |
| 6 | + Internal helper function that writes SCHANNEL protocol registry values |
| 7 | + under HKLM:\SYSTEM\CurrentControlSet\Control\SecurityProviders\SCHANNEL\Protocols. |
| 8 | + This function creates the registry key if it does not exist and sets |
| 9 | + the Enabled DWORD value. Optionally sets the DisabledByDefault value. |
| 10 | +
|
| 11 | + The function handles ShouldProcess confirmation using the caller's |
| 12 | + PSCmdlet object. |
| 13 | +
|
| 14 | + .PARAMETER Protocol |
| 15 | + One or more protocol names to set registry values for. Accepts values |
| 16 | + from the `[System.Security.Authentication.SslProtocols]` enum such as |
| 17 | + `Ssl2`, `Ssl3`, `Tls`, `Tls11`, `Tls12`, `Tls13`. |
| 18 | +
|
| 19 | + .PARAMETER Enable |
| 20 | + Enables the protocol by setting Enabled to 1 and DisabledByDefault to 0. |
| 21 | +
|
| 22 | + .PARAMETER Disable |
| 23 | + Disables the protocol by setting Enabled to 0 and DisabledByDefault to 1. |
| 24 | +
|
| 25 | + .PARAMETER Client |
| 26 | + When specified, operate on the protocol `Client` registry key instead of |
| 27 | + the default `Server` key. |
| 28 | +
|
| 29 | + .PARAMETER SetDisabledByDefault |
| 30 | + When specified, also set the DisabledByDefault registry value. |
| 31 | +
|
| 32 | + .PARAMETER Force |
| 33 | + When specified, bypasses confirmation prompts and suppresses |
| 34 | + ShouldProcess confirmations. |
| 35 | +
|
| 36 | + .PARAMETER Cmdlet |
| 37 | + The PSCmdlet object from the calling command, used to perform |
| 38 | + ShouldProcess confirmation. |
| 39 | +
|
| 40 | + .INPUTS |
| 41 | + None. |
| 42 | +
|
| 43 | + .OUTPUTS |
| 44 | + None. |
| 45 | +
|
| 46 | + .EXAMPLE |
| 47 | + Set-TlsProtocolRegistryValue -Protocol Tls12 -Enable -SetDisabledByDefault |
| 48 | +
|
| 49 | + Enables TLS 1.2 for server-side connections by setting the Enabled |
| 50 | + registry value to 1 and DisabledByDefault to 0. |
| 51 | +
|
| 52 | + .EXAMPLE |
| 53 | + Set-TlsProtocolRegistryValue -Protocol Tls12, Tls13 -Enable |
| 54 | +
|
| 55 | + Enables TLS 1.2 and TLS 1.3 for server-side connections. |
| 56 | +
|
| 57 | + .EXAMPLE |
| 58 | + Set-TlsProtocolRegistryValue -Protocol Ssl3 -Disable -Client |
| 59 | +
|
| 60 | + Disables SSL 3.0 for client-side connections by setting the Enabled |
| 61 | + registry value to 0. |
| 62 | +#> |
| 63 | +function Set-TlsProtocolRegistryValue |
| 64 | +{ |
| 65 | + [CmdletBinding(SupportsShouldProcess = $true, ConfirmImpact = 'Medium', DefaultParameterSetName = 'Enable')] |
| 66 | + [OutputType()] |
| 67 | + param |
| 68 | + ( |
| 69 | + [Parameter(Mandatory = $true)] |
| 70 | + [System.Security.Authentication.SslProtocols[]] |
| 71 | + $Protocol, |
| 72 | + |
| 73 | + [Parameter(Mandatory = $true, ParameterSetName = 'Enable')] |
| 74 | + [System.Management.Automation.SwitchParameter] |
| 75 | + $Enable, |
| 76 | + |
| 77 | + [Parameter(Mandatory = $true, ParameterSetName = 'Disable')] |
| 78 | + [System.Management.Automation.SwitchParameter] |
| 79 | + $Disable, |
| 80 | + |
| 81 | + [Parameter()] |
| 82 | + [System.Management.Automation.SwitchParameter] |
| 83 | + $Client, |
| 84 | + |
| 85 | + [Parameter()] |
| 86 | + [System.Management.Automation.SwitchParameter] |
| 87 | + $SetDisabledByDefault, |
| 88 | + |
| 89 | + [Parameter()] |
| 90 | + [System.Management.Automation.SwitchParameter] |
| 91 | + $Force |
| 92 | + ) |
| 93 | + |
| 94 | + # Need to do this check with Get-Variable instead of $Confirm due to strict mode. |
| 95 | + if ($Force.IsPresent -and -not (Get-Variable -Name 'Confirm' -ValueOnly -ErrorAction SilentlyContinue)) |
| 96 | + { |
| 97 | + $ConfirmPreference = 'None' |
| 98 | + } |
| 99 | + |
| 100 | + foreach ($currentProtocol in $Protocol) |
| 101 | + { |
| 102 | + $protocolKeyName = ConvertTo-TlsProtocolRegistryKeyName -Protocol $currentProtocol |
| 103 | + $target = Get-TlsProtocolTargetRegistryName -Client:$Client |
| 104 | + |
| 105 | + if ($Enable.IsPresent) |
| 106 | + { |
| 107 | + $enabledValue = 1 |
| 108 | + $disabledByDefaultValue = 0 |
| 109 | + $descriptionMessage = $script:localizedData.Set_TlsProtocolRegistryValue_Enable_ShouldProcessDescription -f $protocolKeyName, $target |
| 110 | + $confirmationMessage = $script:localizedData.Set_TlsProtocolRegistryValue_Enable_ShouldProcessConfirmation -f $protocolKeyName |
| 111 | + $captionMessage = $script:localizedData.Set_TlsProtocolRegistryValue_Enable_ShouldProcessCaption |
| 112 | + $errorMessage = $script:localizedData.Set_TlsProtocolRegistryValue_FailedToEnable |
| 113 | + $errorId = 'STPRV0001' |
| 114 | + } |
| 115 | + else |
| 116 | + { |
| 117 | + $enabledValue = 0 |
| 118 | + $disabledByDefaultValue = 1 |
| 119 | + $descriptionMessage = $script:localizedData.Set_TlsProtocolRegistryValue_Disable_ShouldProcessDescription -f $protocolKeyName, $target |
| 120 | + $confirmationMessage = $script:localizedData.Set_TlsProtocolRegistryValue_Disable_ShouldProcessConfirmation -f $protocolKeyName |
| 121 | + $captionMessage = $script:localizedData.Set_TlsProtocolRegistryValue_Disable_ShouldProcessCaption |
| 122 | + $errorMessage = $script:localizedData.Set_TlsProtocolRegistryValue_FailedToDisable |
| 123 | + $errorId = 'STPRV0002' |
| 124 | + } |
| 125 | + |
| 126 | + if ($PSCmdlet.ShouldProcess($descriptionMessage, $confirmationMessage, $captionMessage)) |
| 127 | + { |
| 128 | + $regPath = Get-TlsProtocolRegistryPath -Protocol $currentProtocol -Client:$Client |
| 129 | + |
| 130 | + try |
| 131 | + { |
| 132 | + $null = New-Item -Path $regPath -Force -ErrorAction 'Stop' |
| 133 | + $null = New-ItemProperty -Path $regPath -Name 'Enabled' -Value $enabledValue -PropertyType DWord -Force -ErrorAction 'Stop' |
| 134 | + |
| 135 | + if ($SetDisabledByDefault.IsPresent) |
| 136 | + { |
| 137 | + $null = New-ItemProperty -Path $regPath -Name 'DisabledByDefault' -Value $disabledByDefaultValue -PropertyType DWord -Force -ErrorAction 'Stop' |
| 138 | + } |
| 139 | + } |
| 140 | + catch |
| 141 | + { |
| 142 | + $errorMessage = $errorMessage -f $currentProtocol |
| 143 | + |
| 144 | + $exception = New-Exception -Message $errorMessage -ErrorRecord $_ |
| 145 | + $errorRecord = New-ErrorRecord -Exception $exception -ErrorId $errorId -ErrorCategory 'InvalidOperation' -TargetObject $currentProtocol |
| 146 | + $PSCmdlet.ThrowTerminatingError($errorRecord) |
| 147 | + } |
| 148 | + } |
| 149 | + } |
| 150 | +} |
0 commit comments