sign-windows.ps1 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. param(
  2. [Parameter(ValueFromRemainingArguments = $true)]
  3. [string[]] $Path
  4. )
  5. $ErrorActionPreference = "Stop"
  6. if (-not $Path -or $Path.Count -eq 0) {
  7. throw "At least one path is required"
  8. }
  9. if ($env:GITHUB_ACTIONS -ne "true") {
  10. throw "Windows release signing requires the configured GitHub Actions signing environment"
  11. }
  12. $vars = @{
  13. endpoint = $env:AZURE_TRUSTED_SIGNING_ENDPOINT
  14. account = $env:AZURE_TRUSTED_SIGNING_ACCOUNT_NAME
  15. profile = $env:AZURE_TRUSTED_SIGNING_CERTIFICATE_PROFILE
  16. }
  17. if ($vars.Values | Where-Object { -not $_ }) {
  18. throw "Azure Artifact Signing is not configured; refusing to produce an unsigned release"
  19. }
  20. $moduleVersion = "0.5.8"
  21. $module = Get-Module -ListAvailable -Name TrustedSigning | Where-Object { $_.Version -eq [version] $moduleVersion }
  22. if (-not $module) {
  23. try {
  24. Install-PackageProvider -Name NuGet -MinimumVersion 2.8.5.201 -Force -Scope CurrentUser | Out-Null
  25. }
  26. catch {
  27. Write-Host "NuGet package provider install skipped: $($_.Exception.Message)"
  28. }
  29. Install-Module -Name TrustedSigning -RequiredVersion $moduleVersion -Force -Repository PSGallery -Scope CurrentUser
  30. }
  31. Import-Module TrustedSigning -RequiredVersion $moduleVersion -Force
  32. $files = @($Path | ForEach-Object { Resolve-Path $_ -ErrorAction SilentlyContinue } | Select-Object -ExpandProperty Path -Unique)
  33. if (-not $files -or $files.Count -eq 0) {
  34. throw "No files matched the requested paths"
  35. }
  36. $params = @{
  37. Endpoint = $vars.endpoint
  38. CodeSigningAccountName = $vars.account
  39. CertificateProfileName = $vars.profile
  40. Files = ($files -join ",")
  41. FileDigest = "SHA256"
  42. TimestampDigest = "SHA256"
  43. TimestampRfc3161 = "http://timestamp.acs.microsoft.com"
  44. ExcludeEnvironmentCredential = $true
  45. ExcludeWorkloadIdentityCredential = $true
  46. ExcludeManagedIdentityCredential = $true
  47. ExcludeSharedTokenCacheCredential = $true
  48. ExcludeVisualStudioCredential = $true
  49. ExcludeVisualStudioCodeCredential = $true
  50. ExcludeAzureCliCredential = $false
  51. ExcludeAzurePowerShellCredential = $true
  52. ExcludeAzureDeveloperCliCredential = $true
  53. ExcludeInteractiveBrowserCredential = $true
  54. }
  55. Invoke-TrustedSigning @params
  56. foreach ($file in $files) {
  57. $signature = Get-AuthenticodeSignature -FilePath $file
  58. if ($signature.Status -ne 'Valid') {
  59. throw "Signature verification failed for $file : $($signature.Status)"
  60. }
  61. }