| | | 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 | | #> |
| | | 59 | | function 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 { |
| | 0 | 84 | | switch ($PSCmdlet.ParameterSetName) { |
| | | 85 | | 'ByCertificate' { |
| | 0 | 86 | | $cert = $Certificate |
| | | 87 | | |
| | 0 | 88 | | if ($null -eq $cert) { |
| | 0 | 89 | | throw "Failed to obtain certificate instance." |
| | | 90 | | } |
| | | 91 | | |
| | 0 | 92 | | if ($IncludePrivateParameters.IsPresent -and -not $cert.HasPrivateKey) { |
| | 0 | 93 | | throw "Certificate does not contain a private key, cannot include private parameters in JWK." |
| | | 94 | | } |
| | | 95 | | |
| | 0 | 96 | | $json = [Kestrun.Certificates.CertificateManager]::CreateJwkJsonFromCertificate( |
| | | 97 | | $cert, |
| | | 98 | | $IncludePrivateParameters.IsPresent |
| | | 99 | | ) |
| | | 100 | | |
| | 0 | 101 | | Write-KrLog -Level Verbose -Message "Converted certificate to JWK JSON (IncludePrivateParameters=$($Incl |
| | 0 | 102 | | $json |
| | | 103 | | } |
| | | 104 | | |
| | | 105 | | 'ByRsaPrivateKeyPem' { |
| | 0 | 106 | | if ([string]::IsNullOrWhiteSpace($RsaPrivateKeyPem)) { |
| | 0 | 107 | | throw "RsaPrivateKeyPem parameter cannot be null or empty." |
| | | 108 | | } |
| | | 109 | | |
| | | 110 | | # For private key PEM we always get a full private JWK. |
| | 0 | 111 | | $json = [Kestrun.Certificates.CertificateManager]::CreateJwkJsonFromRsaPrivateKeyPem( |
| | | 112 | | $RsaPrivateKeyPem, |
| | | 113 | | $null |
| | | 114 | | ) |
| | | 115 | | |
| | 0 | 116 | | Write-KrLog -Level Verbose -Message "Converted RSA private key PEM (inline) to JWK JSON (private key mat |
| | 0 | 117 | | $json |
| | | 118 | | } |
| | | 119 | | |
| | | 120 | | 'ByRsaPrivateKeyPath' { |
| | 0 | 121 | | if ([string]::IsNullOrWhiteSpace($RsaPrivateKeyPath)) { |
| | 0 | 122 | | throw "RsaPrivateKeyPath parameter cannot be null or empty." |
| | | 123 | | } |
| | | 124 | | |
| | 0 | 125 | | $resolved = Resolve-KrPath -Path $RsaPrivateKeyPath -KestrunRoot |
| | 0 | 126 | | if (-not (Test-Path -LiteralPath $resolved)) { |
| | 0 | 127 | | throw "RSA private key PEM file not found: $resolved" |
| | | 128 | | } |
| | | 129 | | |
| | 0 | 130 | | $pem = Get-Content -LiteralPath $resolved -Raw |
| | 0 | 131 | | $json = [Kestrun.Certificates.CertificateManager]::CreateJwkJsonFromRsaPrivateKeyPem( |
| | | 132 | | $pem, |
| | | 133 | | $null |
| | | 134 | | ) |
| | | 135 | | |
| | 0 | 136 | | Write-KrLog -Level Verbose -Message "Converted RSA private key PEM from '$resolved' to JWK JSON (private |
| | 0 | 137 | | $json |
| | | 138 | | } |
| | | 139 | | } |
| | | 140 | | } |
| | | 141 | | } |