< Summary - Kestrun — Combined Coverage

Information
Class: Public.Certificate.Test-KrCertificate
Assembly: Kestrun.PowerShell.Public
File(s): /home/runner/work/Kestrun/Kestrun/src/PowerShell/Kestrun/Public/Certificate/Test-KrCertificate.ps1
Tag: Kestrun/Kestrun@5f1d2b981c9d7292c11fd448428c6ab6c811c5de
Line coverage
40%
Covered lines: 8
Uncovered lines: 12
Coverable lines: 20
Total lines: 113
Line coverage: 40%
Branch coverage
N/A
Covered branches: 0
Total branches: 0
Branch coverage: N/A
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Coverage history

Coverage history 0 25 50 75 100 11/19/2025 - 17:40:50 Line coverage: 0% (0/6) Total lines: 61 Tag: Kestrun/Kestrun@fcf33342333cef0516fe0d0912a86709874fd02602/18/2026 - 08:33:07 Line coverage: 50% (3/6) Total lines: 61 Tag: Kestrun/Kestrun@bf8a937cfb7e8936c225b9df4608f8ddd85558b104/19/2026 - 15:52:57 Line coverage: 40% (8/20) Total lines: 113 Tag: Kestrun/Kestrun@765a8f13c573c01494250a29d6392b6037f087c9

Coverage delta

Coverage delta 50 -50

Metrics

File(s)

/home/runner/work/Kestrun/Kestrun/src/PowerShell/Kestrun/Public/Certificate/Test-KrCertificate.ps1

#LineLine coverage
 1<#
 2    .SYNOPSIS
 3        Validates a certificate’s chain, EKU, and cryptographic strength.
 4    .DESCRIPTION
 5        This function checks the validity of a given X509Certificate2 object by verifying its certificate chain,
 6        enhanced key usage (EKU), and cryptographic strength. It can also check for self-signed certificates and
 7        validate against expected purposes.
 8    .PARAMETER Certificate
 9        The X509Certificate2 object to validate.
 10    .PARAMETER CheckRevocation
 11        Indicates whether to check the certificate's revocation status.
 12    .PARAMETER AllowWeakAlgorithms
 13        Indicates whether to allow weak cryptographic algorithms.
 14    .PARAMETER DenySelfSigned
 15        Indicates whether to deny self-signed certificates.
 16    .PARAMETER ExpectedPurpose
 17        The expected purposes (OID) for the certificate.
 18        If specified, the certificate will be validated against these purposes.
 19    .PARAMETER StrictPurpose
 20        Indicates whether to enforce strict matching of the expected purposes.
 21    .PARAMETER CertificateChain
 22        Optional additional certificates used to build trust for the target certificate, such as
 23        a private development root CA or intermediate certificates.
 24    .PARAMETER FailureReasonVariable
 25        Optional variable name that will receive the validation failure reason in the caller scope.
 26        When validation succeeds, the target variable is set to an empty string.
 27    .EXAMPLE
 28        Test-KrCertificate -Certificate $cert -DenySelfSigned -CheckRevocation
 29    .EXAMPLE
 30        Test-KrCertificate -Certificate $cert -AllowWeakAlgorithms -ExpectedPurpose '1.3.6.1.5.5.7.3.1'
 31    .EXAMPLE
 32        Test-KrCertificate -Certificate $cert -StrictPurpose
 33        If specified, the certificate will be validated against these purposes.
 34    .EXAMPLE
 35        $bundle = New-KrSelfSignedCertificate -Development -Exportable
 36        $isValid = Test-KrCertificate -Certificate $bundle.LeafCertificate -CertificateChain $bundle.RootCertificate -Fa
 37        if (-not $isValid) { Write-Host "Validation failed: $reason" }
 38    .EXAMPLE
 39        $isValid = Test-KrCertificate -Certificate $cert -FailureReasonVariable 'reason'
 40        if (-not $isValid) { Write-Host "Validation failed: $reason" }
 41    .NOTES
 42        This function is designed to be used in the context of Kestrun's certificate management.
 43        It leverages the Kestrun.Certificates.CertificateManager for validation.
 44#>
 45function Test-KrCertificate {
 46    [KestrunRuntimeApi('Everywhere')]
 47    [CmdletBinding()]
 48    [OutputType([bool])]
 49    param(
 50        [Parameter(Mandatory)]
 51        [System.Security.Cryptography.X509Certificates.X509Certificate2] $Certificate,
 52
 53        [Parameter()]
 54        [switch]$CheckRevocation,
 55
 56        [Parameter()]
 57        [switch]$AllowWeakAlgorithms,
 58
 59        [Parameter()]
 60        [switch]$DenySelfSigned,
 61
 62        [Parameter()]
 63        [string[]]$ExpectedPurpose,
 64
 65        [Parameter()]
 66        [switch]$StrictPurpose,
 67
 68        [Parameter()]
 69        [System.Security.Cryptography.X509Certificates.X509Certificate2[]]$CertificateChain,
 70
 71        [Parameter()]
 72        [string]$FailureReasonVariable
 73    )
 74
 175    $oidColl = if ($ExpectedPurpose) {
 076        $oc = [System.Security.Cryptography.OidCollection]::new()
 077        foreach ($p in $ExpectedPurpose) { $oc.Add([System.Security.Cryptography.Oid]::new($p)) }
 078        $oc
 179    } else { $null }
 80
 181    $chainCollection = if ($CertificateChain) {
 082        $collection = [System.Security.Cryptography.X509Certificates.X509Certificate2Collection]::new()
 083        foreach ($chainCertificate in $CertificateChain) {
 084            [void]$collection.Add($chainCertificate)
 85        }
 086        $collection
 187    } else { $null }
 88
 189    $reason = ''
 190    $isValid = [Kestrun.Certificates.CertificateManager]::Validate($Certificate,
 91        $CheckRevocation.IsPresent,
 92        $AllowWeakAlgorithms.IsPresent,
 93        $DenySelfSigned.IsPresent,
 94        $oidColl,
 95        $StrictPurpose.IsPresent,
 96        $chainCollection,
 97        [ref]$reason)
 98
 199    if ($PSBoundParameters.ContainsKey('FailureReasonVariable')) {
 0100        if ([string]::IsNullOrWhiteSpace($FailureReasonVariable)) {
 0101            throw 'FailureReasonVariable cannot be null or whitespace when provided.'
 102        }
 103
 0104        if ($FailureReasonVariable -match '^[A-Za-z]+:') {
 0105            Set-Variable -Name $FailureReasonVariable -Value $reason -Force
 106        } else {
 0107            Set-Variable -Name $FailureReasonVariable -Scope 2 -Value $reason -Force
 108        }
 109    }
 110
 1111    return $isValid
 112}
 113

Methods/Properties

Test-KrCertificate()