< Summary - Kestrun — Combined Coverage

Information
Class: Public.Certificate.ConvertTo-KrJwkJson
Assembly: Kestrun.PowerShell.Public
File(s): /home/runner/work/Kestrun/Kestrun/src/PowerShell/Kestrun/Public/Certificate/ConvertTo-KrJwkJson.ps1
Tag: Kestrun/Kestrun@0d738bf294e6281b936d031e1979d928007495ff
Line coverage
0%
Covered lines: 0
Uncovered lines: 23
Coverable lines: 23
Total lines: 141
Line coverage: 0%
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 - 02:25:56 Line coverage: 0% (0/23) Total lines: 141 Tag: Kestrun/Kestrun@98ff905e5605a920343154665980a71211a03c6d

Metrics

File(s)

/home/runner/work/Kestrun/Kestrun/src/PowerShell/Kestrun/Public/Certificate/ConvertTo-KrJwkJson.ps1

#LineLine coverage
 1<#
 2    .SYNOPSIS
 3        Converts an X509 certificate or RSA private key PEM to an RSA JWK JSON string.
 4    .DESCRIPTION
 5        This function converts either:
 6          - an X509 certificate object, or
 7          - an RSA private key in PEM format
 8        to a JSON Web Key (JWK) representation using the
 9        Kestrun.Certificates.CertificateManager backend.
 10
 11        For certificates:
 12          - By default it exports only the public parameters (suitable for JWKS).
 13          - If -IncludePrivateParameters is specified, the private RSA parameters
 14            are included as well (for local/secure use only; never publish those).
 15
 16        For RSA private key PEM:
 17          - The output is always a full private JWK (public + private parameters),
 18            as the source is inherently private key material.
 19    .PARAMETER Certificate
 20        The X509Certificate2 object to convert to JWK JSON.
 21        Typically obtained from Import-KrCertificate and passed via the pipeline.
 22    .PARAMETER RsaPrivateKeyPem
 23        A string containing an RSA private key in PEM format
 24        (e.g. "-----BEGIN RSA PRIVATE KEY----- ...").
 25    .PARAMETER RsaPrivateKeyPath
 26        Path to a file containing an RSA private key PEM. The file is read as raw text
 27        and passed to CertificateManager.CreateJwkJsonFromRsaPrivateKeyPem().
 28    .PARAMETER IncludePrivateParameters
 29        When converting from a certificate:
 30            If specified, includes private RSA parameters (d, p, q, dp, dq, qi) in the JWK JSON.
 31            Requires that the certificate has a private key.
 32        When converting from RSA private key PEM:
 33            Ignored. The output always contains private parameters because the source
 34            is a private key.
 35    .OUTPUTS
 36        [string] – the JWK JSON string.
 37    .EXAMPLE
 38        Import-KrCertificate -Path './certs/client.pfx' |
 39            ConvertTo-KrJwkJson
 40
 41        Imports a certificate and converts it to a public-only JWK JSON string.
 42    .EXAMPLE
 43        Import-KrCertificate -Path './certs/client.pfx' |
 44            ConvertTo-KrJwkJson -IncludePrivateParameters
 45
 46        Imports the certificate and returns a full private JWK JSON string.
 47    .EXAMPLE
 48        $pem = Get-Content './Assets/certs/private.pem' -Raw
 49        ConvertTo-KrJwkJson -RsaPrivateKeyPem $pem
 50
 51        Converts the RSA private key PEM to a full private JWK JSON string.
 52    .EXAMPLE
 53        ConvertTo-KrJwkJson -RsaPrivateKeyPath './Assets/certs/private.pem'
 54
 55        Reads the RSA private key PEM from disk and converts it to a full private JWK JSON string.
 56    .NOTES
 57        Requires the Kestrun module and the Kestrun.Certificates assembly to be loaded.
 58#>
 59function ConvertTo-KrJwkJson {
 60    [KestrunRuntimeApi('Everywhere')]
 61    [CmdletBinding(DefaultParameterSetName = 'ByCertificate')]
 62    [OutputType([string])]
 63    param(
 64        # Certificate instance (from pipeline)
 65        [Parameter(Mandatory = $true, ValueFromPipeline = $true, ParameterSetName = 'ByCertificate')]
 66        [System.Security.Cryptography.X509Certificates.X509Certificate2]
 67        $Certificate,
 68
 69        # Raw RSA private key PEM
 70        [Parameter(Mandatory = $true, ParameterSetName = 'ByRsaPrivateKeyPem')]
 71        [string]
 72        $RsaPrivateKeyPem,
 73
 74        # RSA private key PEM file path
 75        [Parameter(Mandatory = $true, ParameterSetName = 'ByRsaPrivateKeyPath')]
 76        [string]
 77        $RsaPrivateKeyPath,
 78
 79        [Parameter()]
 80        [switch]
 81        $IncludePrivateParameters
 82    )
 83    process {
 084        switch ($PSCmdlet.ParameterSetName) {
 85            'ByCertificate' {
 086                $cert = $Certificate
 87
 088                if ($null -eq $cert) {
 089                    throw "Failed to obtain certificate instance."
 90                }
 91
 092                if ($IncludePrivateParameters.IsPresent -and -not $cert.HasPrivateKey) {
 093                    throw "Certificate does not contain a private key, cannot include private parameters in JWK."
 94                }
 95
 096                $json = [Kestrun.Certificates.CertificateManager]::CreateJwkJsonFromCertificate(
 97                    $cert,
 98                    $IncludePrivateParameters.IsPresent
 99                )
 100
 0101                Write-KrLog -Level Verbose -Message "Converted certificate to JWK JSON (IncludePrivateParameters=$($Incl
 0102                $json
 103            }
 104
 105            'ByRsaPrivateKeyPem' {
 0106                if ([string]::IsNullOrWhiteSpace($RsaPrivateKeyPem)) {
 0107                    throw "RsaPrivateKeyPem parameter cannot be null or empty."
 108                }
 109
 110                # For private key PEM we always get a full private JWK.
 0111                $json = [Kestrun.Certificates.CertificateManager]::CreateJwkJsonFromRsaPrivateKeyPem(
 112                    $RsaPrivateKeyPem,
 113                    $null
 114                )
 115
 0116                Write-KrLog -Level Verbose -Message "Converted RSA private key PEM (inline) to JWK JSON (private key mat
 0117                $json
 118            }
 119
 120            'ByRsaPrivateKeyPath' {
 0121                if ([string]::IsNullOrWhiteSpace($RsaPrivateKeyPath)) {
 0122                    throw "RsaPrivateKeyPath parameter cannot be null or empty."
 123                }
 124
 0125                $resolved = Resolve-KrPath -Path $RsaPrivateKeyPath -KestrunRoot
 0126                if (-not (Test-Path -LiteralPath $resolved)) {
 0127                    throw "RSA private key PEM file not found: $resolved"
 128                }
 129
 0130                $pem = Get-Content -LiteralPath $resolved -Raw
 0131                $json = [Kestrun.Certificates.CertificateManager]::CreateJwkJsonFromRsaPrivateKeyPem(
 132                    $pem,
 133                    $null
 134                )
 135
 0136                Write-KrLog -Level Verbose -Message "Converted RSA private key PEM from '$resolved' to JWK JSON (private
 0137                $json
 138            }
 139        }
 140    }
 141}

Methods/Properties

ConvertTo-KrJwkJson()