1- # Usage: Publish.ps1 -arch <x64|arm64> -version <version> [-buildPath <path>] [-outputPath <path>]
1+ # Usage: Publish.ps1 -arch <x64|arm64> -version <version> [-msiOutputPath <path>] [-outputPath <path>] [-sign ]
22param (
33 [ValidateSet (" x64" , " arm64" )]
44 [Parameter (Mandatory = $true )]
@@ -50,6 +50,8 @@ function Find-EnvironmentVariables([string[]] $variables) {
5050 }
5151}
5252
53+ Find-Dependencies @ (" dotnet.exe" , " wix.exe" )
54+
5355if ($sign ) {
5456 Write-Host " Signing is enabled"
5557 Find-Dependencies java
@@ -73,6 +75,12 @@ function Add-CoderSignature([string] $path) {
7375 -- tsaurl $env: EV_TSA_URL `
7476 $path
7577 if ($LASTEXITCODE -ne 0 ) { throw " Failed to sign $path " }
78+
79+ # Verify that the output exe is authenticode signed
80+ $sig = Get-AuthenticodeSignature $path
81+ if ($sig.Status -ne " Valid" ) {
82+ throw " File $path is not authenticode signed"
83+ }
7684}
7785
7886# CD to the root of the repo
@@ -97,13 +105,16 @@ if (Test-Path $outputPath.Replace(".exe", ".wixpdb")) {
97105}
98106
99107# Create a publish directory
100- $buildPath = Join-Path $repoRoot " publish\buildtemp-$ ( $version ) -$ ( $arch ) "
108+ $publishDir = Join-Path $repoRoot " publish"
109+ $buildPath = Join-Path $publishDir " buildtemp-$ ( $version ) -$ ( $arch ) "
101110if (Test-Path $buildPath ) {
102111 Remove-Item - Recurse - Force $buildPath
103112}
104113New-Item - ItemType Directory - Path $buildPath - Force
105114
106115# Build in release mode
116+ & dotnet.exe restore
117+ if ($LASTEXITCODE -ne 0 ) { throw " Failed to dotnet restore" }
107118$servicePublishDir = Join-Path $buildPath " service"
108119& dotnet.exe publish .\Vpn.Service\Vpn.Service.csproj - c Release - a $arch - o $servicePublishDir
109120if ($LASTEXITCODE -ne 0 ) { throw " Failed to build Vpn.Service" }
@@ -126,8 +137,12 @@ Copy-Item "scripts\files\License.txt" $buildPath
126137$vpnFilesPath = Join-Path $buildPath " vpn"
127138New-Item - ItemType Directory - Path $vpnFilesPath - Force
128139Copy-Item " scripts\files\LICENSE.WINTUN.txt" $vpnFilesPath
129- $wintunDllPath = Join-Path $vpnFilesPath " wintun.dll"
130- Copy-Item " scripts\files\wintun-*-$ ( $arch ) .dll" $wintunDllPath
140+ $wintunDllSrc = Get-Item " scripts\files\wintun-*-$ ( $arch ) .dll"
141+ if ($null -eq $wintunDllSrc ) {
142+ throw " Failed to find wintun DLL"
143+ }
144+ $wintunDllDest = Join-Path $vpnFilesPath " wintun.dll"
145+ Copy-Item $wintunDllSrc $wintunDllDest
131146
132147# Build the MSI installer
133148& dotnet.exe run -- project .\Installer\Installer.csproj - c Release -- `
@@ -158,7 +173,39 @@ Add-CoderSignature $msiOutputPath
158173 -- msi- path $msiOutputPath `
159174 -- logo- png " scripts\files\logo.png"
160175if ($LASTEXITCODE -ne 0 ) { throw " Failed to build bootstrapper" }
161- Add-CoderSignature $outputPath
176+
177+ # Sign the bootstrapper, which is not as simple as just signing the exe.
178+ if ($sign ) {
179+ $burnIntermediate = Join-Path $publishDir " burn-intermediate-$ ( $version ) -$ ( $arch ) "
180+ New-Item - ItemType Directory - Path $burnIntermediate - Force
181+ $burnEngine = Join-Path $publishDir " burn-engine-$ ( $version ) -$ ( $arch ) .exe"
182+
183+ # Move the current output path
184+ $unsignedOutputPath = Join-Path (Split-Path $outputPath - Parent) (" UNSIGNED-" + (Split-Path $outputPath - Leaf))
185+ Move-Item $outputPath $unsignedOutputPath
186+
187+ # Extract the engine from the bootstrapper
188+ & wix.exe burn detach $unsignedOutputPath - intermediateFolder $burnIntermediate - engine $burnEngine
189+ if ($LASTEXITCODE -ne 0 ) { throw " Failed to extract engine from bootstrapper" }
190+
191+ # Sign the engine
192+ Add-CoderSignature $burnEngine
193+
194+ # Re-attach the signed engine to the bootstrapper
195+ & wix.exe burn reattach $unsignedOutputPath - intermediateFolder $burnIntermediate - engine $burnEngine - out $outputPath
196+ if ($LASTEXITCODE -ne 0 ) { throw " Failed to re-attach signed engine to bootstrapper" }
197+ if (! (Test-Path $outputPath )) { throw " Failed to create reattached bootstrapper at $outputPath " }
198+
199+ # Now sign the output path
200+ Add-CoderSignature $outputPath
201+
202+ # Clean up the intermediate files
203+ if (! $keepBuildTemp ) {
204+ Remove-Item - Force $unsignedOutputPath
205+ Remove-Item - Recurse - Force $burnIntermediate
206+ Remove-Item - Force $burnEngine
207+ }
208+ }
162209
163210if (! $keepBuildTemp ) {
164211 Remove-Item - Recurse - Force $buildPath
0 commit comments