#requires -Version 7.4 # Keep the non-exportable private key in CurrentUser/My. Never install a root. [CmdletBinding()] param([string]$ConfigPath = $env:RMIC_WINDOWS_SIGNING_CONFIG) $ErrorActionPreference = 'Stop' Set-StrictMode -Version Latest if (-not $ConfigPath) { $ConfigPath = Join-Path $env:LOCALAPPDATA 'RMIC-2026/signing/windows.json' } if (Test-Path -LiteralPath $ConfigPath) { $saved = Get-Content -LiteralPath $ConfigPath -Raw | ConvertFrom-Json if ($saved.schema -ne 1 -or $saved.certificateThumbprint -notmatch '^[0-9A-F]{40}$') { throw 'Invalid saved Windows signing identity. Restore the local signing configuration.' } $cert = Get-Item -LiteralPath "Cert:\CurrentUser\My\$($saved.certificateThumbprint)" -ErrorAction SilentlyContinue if (-not $cert -or -not $cert.HasPrivateKey -or $cert.NotAfter -le (Get-Date)) { throw 'Saved signing certificate/key is missing or expired. Restore the original identity or explicitly provision a replacement; it will not be silently rotated.' } $saved return } $existing = @(Get-ChildItem Cert:\CurrentUser\My -CodeSigningCert | Where-Object FriendlyName -eq 'RMIC-2026 self-signed code signing') if ($existing.Count -gt 1) { throw 'Multiple RMIC signing identities exist. Select one explicitly with RMIC_WINDOWS_CERT_THUMBPRINT.' } if ($existing.Count -eq 1) { $cert = $existing[0] if (-not $cert.HasPrivateKey -or $cert.NotAfter -le (Get-Date)) { throw 'Existing RMIC signing identity is unusable; provision a replacement explicitly.' } } else { $cert = New-SelfSignedCertificate -Type CodeSigningCert -Subject 'CN=RMIC-2026 Self-signed Code Signing' ` -FriendlyName 'RMIC-2026 self-signed code signing' -KeyAlgorithm RSA -KeyLength 3072 ` -HashAlgorithm SHA256 -KeyUsage DigitalSignature -KeyExportPolicy NonExportable ` -Provider 'Microsoft Software Key Storage Provider' -CertStoreLocation 'Cert:\CurrentUser\My' ` -NotBefore (Get-Date).AddMinutes(-5) -NotAfter (Get-Date).AddYears(3) } $saved = [ordered]@{ schema = 1; certificateThumbprint = $cert.Thumbprint; kind = 'self-signed'; subject = $cert.Subject; expiresUtc = $cert.NotAfter.ToUniversalTime().ToString('o') } New-Item -ItemType Directory -Force -Path (Split-Path -Parent ([IO.Path]::GetFullPath($ConfigPath))) | Out-Null [IO.File]::WriteAllText([IO.Path]::GetFullPath($ConfigPath), ($saved | ConvertTo-Json), [Text.UTF8Encoding]::new($false)) [pscustomobject]$saved