Public/Test-PWSHYBKPIVDecryption.ps1
|
function Test-PWSHYBKPIVDecryption { <# .SYNOPSIS Tests that the key in a PIV slot of a locally attached YubiKey can encrypt and decipher data. .DESCRIPTION Wraps "yubico-piv-tool.exe --action test-decipher". Follows Test- verb convention: returns $true/$false rather than throwing on failure. When -Pin is supplied and is wrong, the attempt still consumes one of the YubiKey's limited PIN retries, so this cmdlet supports -WhatIf/-Confirm (ConfirmImpact 'Low', matching Test-PWSHYBKPIVPin). Also writes a Write-Warning (independent of the module's opt-in Logging config), same as Test-PWSHYBKPIVPin, when yubico-piv-tool.exe's failure text indicates the PIN retry count is low or has hit zero - the PIN is now blocked and needs Unblock-PWSHYBKPIVPin (with the PUK) or Reset-PWSHYBKPIVDevice before it will accept a PIN again. Accepts the "test-decipher" action's config-declared options (-Slot, -Pin, -Reader) as dynamic parameters built from Config\Posh-YBKPIV.json. .PARAMETER Slot The PIV slot holding the key to test, e.g. '9a'. .PARAMETER Pin The PIN authorizing the decipher operation, as a SecureString, if the slot's PIN policy requires one. .PARAMETER Reader Name of the smart card reader to target, when more than one is attached. If omitted, yubico-piv-tool.exe uses its own default reader selection. .INPUTS None. This cmdlet does not accept pipeline input. .OUTPUTS Boolean. $true if the encrypt/decipher round-trip succeeded, $false otherwise. .NOTES -Slot, -Pin, and -Reader are declared dynamically from Config\Posh-YBKPIV.json and therefore do not appear in Get-Help's PARAMETERS/SYNTAX sections. Run `Get-Command Test-PWSHYBKPIVDecryption -Syntax` for the authoritative, current parameter list. .EXAMPLE Test-PWSHYBKPIVDecryption -Slot '9a' Tests whether the key in slot 9a can encrypt and decipher data. .LINK https://developers.yubico.com/yubico-piv-tool/Actions/ .LINK Test-PWSHYBKPIVSignature #> [CmdletBinding(SupportsShouldProcess, ConfirmImpact = 'Low')] [OutputType([bool])] param() DynamicParam { $dynamicConfig = Read-PWSHYBKPIVConfigFile Get-PWSHYBKPIVActionParameter -Action 'test-decipher' -CmdletWrapping $dynamicConfig.cmdletWrapping } begin { $config = Read-PWSHYBKPIVConfigFile Write-PWSHYBKPIVLog -Config $config -Level Debug -CmdletName $MyInvocation.MyCommand.Name ` -Message 'Cmdlet invoked' -BoundParameters $PSBoundParameters $architecture = Resolve-PWSHYBKPIVArchitecture -Architecture $config.installation.architecture $exePath = Get-PWSHYBKPIVInstallPath -Installation $config.installation -Architecture $architecture if (-not (Test-Path -Path $exePath -PathType Leaf)) { throw "yubico-piv-tool.exe was not found at '$exePath'. Run Install-PWSHYBKPIVTool first." } } end { if (-not $PSCmdlet.ShouldProcess("PIV slot $($PSBoundParameters['Slot'])", 'Test decipher')) { return } try { $null = Invoke-PWSHYBKPIVTool -ExePath $exePath -Action 'test-decipher' ` -CmdletWrapping $config.cmdletWrapping -BoundParameters $PSBoundParameters Write-PWSHYBKPIVLog -Config $config -Level Information -CmdletName $MyInvocation.MyCommand.Name ` -Message 'Decipher test succeeded' $true } catch { Write-PWSHYBKPIVLog -Config $config -Level Warning -CmdletName $MyInvocation.MyCommand.Name ` -Message "Decipher test failed: $_" Write-PWSHYBKPIVPinRetryWarning -Message $_.Exception.Message $false } } } |