signer.ps1 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. #
  2. # Copyright (c) Contributors to the Open 3D Engine Project.
  3. # For complete copyright and license terms please see the LICENSE at the root of this distribution.
  4. #
  5. # SPDX-License-Identifier: Apache-2.0 OR MIT
  6. #
  7. #
  8. param (
  9. [String[]] $exePath,
  10. [String[]] $packagePath,
  11. [String[]] $bootstrapPath,
  12. [String[]] $certificate
  13. )
  14. # Get prerequisites, certs, and paths ready
  15. $tempPath = [System.IO.Path]::GetTempPath() # Order of operations defined here: https://docs.microsoft.com/en-us/dotnet/api/system.io.path.gettemppath?view=net-5.0&tabs=windows#remarks
  16. $certThumbprint = Get-ChildItem -Path Cert:LocalMachine\MY -CodeSigningCert -ErrorAction Stop | Select-Object -ExpandProperty Thumbprint # Grab first certificate from local machine store
  17. if ($certificate) {
  18. Write-Output "Checking certificate thumbprint $certificate"
  19. Get-ChildItem -Path Cert:LocalMachine\MY -ErrorAction SilentlyContinue | Where-Object {$_.Thumbprint -eq $certificate} # Prints certificate Thumbprint and Subject if found
  20. if($?) {
  21. $certThumbprint = $certificate
  22. }
  23. else {
  24. Write-Error "$certificate thumbprint not found, using $certThumbprint thumbprint instead"
  25. }
  26. }
  27. Try {
  28. $signtoolPath = Resolve-Path "C:\Program Files*\Windows Kits\10\bin\*\x64\signtool.exe" -ErrorAction Stop | Select-Object -Last 1 -ExpandProperty Path
  29. $insigniaPath = Resolve-Path "C:\Program Files*\WiX*\bin\insignia.exe" -ErrorAction Stop | Select-Object -Last 1 -ExpandProperty Path
  30. }
  31. Catch {
  32. Write-Error "Signtool or Wix insignia not found! Exiting."
  33. }
  34. function Write-Signature {
  35. param (
  36. $signtool,
  37. $thumbprint,
  38. $filename
  39. )
  40. $attempts = 2
  41. $sleepSec = 5
  42. Do {
  43. $attempts--
  44. Try {
  45. & $signtool sign /tr http://timestamp.digicert.com /td sha256 /fd sha256 /sha1 $thumbprint /sm $filename
  46. & $signtool verify /pa /v $filename
  47. return
  48. }
  49. Catch {
  50. Write-Error $_.Exception.InnerException.Message -ErrorAction Continue
  51. Start-Sleep -Seconds $sleepSec
  52. }
  53. } while ($attempts -lt 0)
  54. throw "Failed to sign $filename" # Bypassed in try block if the command is successful
  55. }
  56. # Looping through each path insteaad of globbing to prevent hitting maximum command string length limit
  57. if ($exePath) {
  58. Write-Output "### Signing EXE files ###"
  59. $files = @(Get-ChildItem $exePath -Recurse *.exe | % { $_.FullName })
  60. foreach ($file in $files) {
  61. Write-Signature -signtool $signtoolPath -thumbprint $certThumbprint -filename $file
  62. }
  63. }
  64. if ($packagePath) {
  65. Write-Output "### Signing CAB files ###"
  66. $files = @(Get-ChildItem $packagePath -Recurse *.cab | % { $_.FullName })
  67. foreach ($file in $files) {
  68. Write-Signature -signtool $signtoolPath -thumbprint $certThumbprint -filename $file
  69. }
  70. Write-Output "### Signing MSI files ###"
  71. $files = @(Get-ChildItem $packagePath -Recurse *.msi | % { $_.FullName })
  72. foreach ($file in $files) {
  73. & $insigniaPath -im $files
  74. Write-Signature -signtool $signtoolPath -thumbprint $certThumbprint -filename $file
  75. }
  76. }
  77. if ($bootstrapPath) {
  78. Write-Output "### Signing bootstrapper EXE ###"
  79. $files = @(Get-ChildItem $bootstrapPath -Recurse *.exe | % { $_.FullName })
  80. foreach ($file in $files) {
  81. & $insigniaPath -ib $file -o $tempPath\engine.exe
  82. Write-Signature -signtool $signtoolPath -thumbprint $certThumbprint -filename $tempPath\engine.exe
  83. & $insigniaPath -ab $tempPath\engine.exe $file -o $file
  84. Write-Signature -signtool $signtoolPath -thumbprint $certThumbprint -filename $file
  85. Remove-Item -Force $tempPath\engine.exe
  86. }
  87. }