|
| 1 | +Add-Type -AssemblyName PresentationFramework |
| 2 | +Add-Type -AssemblyName System.Windows.Forms |
| 3 | + |
| 4 | +# XAML for the GUI |
| 5 | +[xml]$xaml = @" |
| 6 | +<Window |
| 7 | + xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" |
| 8 | + xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" |
| 9 | + Title="Signing Tool" Height="400" Width="600" |
| 10 | + WindowStartupLocation="CenterScreen"> |
| 11 | + <Grid Margin="10"> |
| 12 | + <Grid.RowDefinitions> |
| 13 | + <RowDefinition Height="Auto"/> |
| 14 | + <RowDefinition Height="*"/> |
| 15 | + <RowDefinition Height="Auto"/> |
| 16 | + </Grid.RowDefinitions> |
| 17 | +
|
| 18 | + <!-- Header --> |
| 19 | + <StackPanel Grid.Row="0" Margin="0,0,0,10"> |
| 20 | + <TextBlock Text="Signing Tool" FontSize="24" FontWeight="Bold" Margin="0,0,0,10"/> |
| 21 | + <Separator/> |
| 22 | + </StackPanel> |
| 23 | +
|
| 24 | + <!-- Main Content --> |
| 25 | + <TabControl Grid.Row="1" Margin="0,10"> |
| 26 | + <!-- Sign Tab --> |
| 27 | + <TabItem Header="Sign Executable"> |
| 28 | + <Grid Margin="10"> |
| 29 | + <Grid.RowDefinitions> |
| 30 | + <RowDefinition Height="Auto"/> |
| 31 | + <RowDefinition Height="Auto"/> |
| 32 | + <RowDefinition Height="*"/> |
| 33 | + </Grid.RowDefinitions> |
| 34 | +
|
| 35 | + <GroupBox Header="Certificate" Grid.Row="0" Margin="0,0,0,10"> |
| 36 | + <StackPanel Margin="5"> |
| 37 | + <TextBlock Text="Using certificate:" Margin="0,0,0,5"/> |
| 38 | + <TextBox Name="txtCertPath" IsReadOnly="True" Margin="0,0,0,5"/> |
| 39 | + </StackPanel> |
| 40 | + </GroupBox> |
| 41 | +
|
| 42 | + <GroupBox Header="Executable" Grid.Row="1" Margin="0,0,0,10"> |
| 43 | + <Grid Margin="5"> |
| 44 | + <Grid.ColumnDefinitions> |
| 45 | + <ColumnDefinition Width="*"/> |
| 46 | + <ColumnDefinition Width="Auto"/> |
| 47 | + </Grid.ColumnDefinitions> |
| 48 | + <TextBox Name="txtExePath" Grid.Column="0" Margin="0,0,5,0"/> |
| 49 | + <Button Name="btnBrowseExe" Content="Browse..." Grid.Column="1" Width="80"/> |
| 50 | + </Grid> |
| 51 | + </GroupBox> |
| 52 | +
|
| 53 | + <GroupBox Header="Status" Grid.Row="2"> |
| 54 | + <TextBox Name="txtLog" IsReadOnly="True" TextWrapping="Wrap" |
| 55 | + VerticalScrollBarVisibility="Auto" Margin="5"/> |
| 56 | + </GroupBox> |
| 57 | + </Grid> |
| 58 | + </TabItem> |
| 59 | +
|
| 60 | + <!-- Create Certificate Tab --> |
| 61 | + <TabItem Header="Create Certificate"> |
| 62 | + <Grid Margin="10"> |
| 63 | + <Grid.RowDefinitions> |
| 64 | + <RowDefinition Height="Auto"/> |
| 65 | + <RowDefinition Height="*"/> |
| 66 | + </Grid.RowDefinitions> |
| 67 | +
|
| 68 | + <StackPanel Grid.Row="0"> |
| 69 | + <TextBlock TextWrapping="Wrap" Margin="0,0,0,10"> |
| 70 | + This will create a new self-signed certificate for code signing. |
| 71 | + The certificate will be valid for 5 years. |
| 72 | + </TextBlock> |
| 73 | + <Button Name="btnCreateCert" Content="Create Certificate" |
| 74 | + HorizontalAlignment="Left" Padding="20,5"/> |
| 75 | + </StackPanel> |
| 76 | +
|
| 77 | + <GroupBox Header="Status" Grid.Row="1" Margin="0,10,0,0"> |
| 78 | + <TextBox Name="txtCertLog" IsReadOnly="True" TextWrapping="Wrap" |
| 79 | + VerticalScrollBarVisibility="Auto" Margin="5"/> |
| 80 | + </GroupBox> |
| 81 | + </Grid> |
| 82 | + </TabItem> |
| 83 | + </TabControl> |
| 84 | +
|
| 85 | + <!-- Footer --> |
| 86 | + <StackPanel Grid.Row="2" Orientation="Horizontal" HorizontalAlignment="Right"> |
| 87 | + <Button Name="btnSign" Content="Sign" Width="80" Margin="0,0,10,0"/> |
| 88 | + <Button Name="btnClose" Content="Close" Width="80"/> |
| 89 | + </StackPanel> |
| 90 | + </Grid> |
| 91 | +</Window> |
| 92 | +"@ |
| 93 | + |
| 94 | +# Create the window |
| 95 | +$reader = New-Object System.Xml.XmlNodeReader $xaml |
| 96 | +$window = [Windows.Markup.XamlReader]::Load($reader) |
| 97 | + |
| 98 | +# Get controls |
| 99 | +$controls = @{} |
| 100 | +$xaml.SelectNodes("//*[@Name]") | ForEach-Object { |
| 101 | + $controls[$_.Name] = $window.FindName($_.Name) |
| 102 | +} |
| 103 | + |
| 104 | +# Script variables |
| 105 | +$script:certPath = Join-Path (Split-Path -Parent $MyInvocation.MyCommand.Path) "cert.pfx" |
| 106 | +$script:certPassword = "Adasjusk2025" |
| 107 | + |
| 108 | +# Update certificate path textbox |
| 109 | +$controls.txtCertPath.Text = $script:certPath |
| 110 | + |
| 111 | +# Helper function to write to log |
| 112 | +function Write-Log { |
| 113 | + param($Message, $LogBox) |
| 114 | + $controls.$LogBox.AppendText("$Message`n") |
| 115 | + $controls.$LogBox.ScrollToEnd() |
| 116 | +} |
| 117 | + |
| 118 | +# Browse for executable |
| 119 | +$controls.btnBrowseExe.Add_Click({ |
| 120 | + $dialog = New-Object System.Windows.Forms.OpenFileDialog |
| 121 | + $dialog.Filter = "Executable files (*.exe)|*.exe|All files (*.*)|*.*" |
| 122 | + if ($dialog.ShowDialog() -eq 'OK') { |
| 123 | + $controls.txtExePath.Text = $dialog.FileName |
| 124 | + } |
| 125 | +}) |
| 126 | + |
| 127 | +# Create certificate |
| 128 | +$controls.btnCreateCert.Add_Click({ |
| 129 | + try { |
| 130 | + Write-Log "Creating new certificate..." "txtCertLog" |
| 131 | + $cert = New-SelfSignedCertificate ` |
| 132 | + -Subject "CN=InterJava Projects" ` |
| 133 | + -KeyUsage DigitalSignature ` |
| 134 | + -KeySpec Signature ` |
| 135 | + -KeyLength 2048 ` |
| 136 | + -HashAlgorithm SHA256 ` |
| 137 | + -Provider "Microsoft Enhanced RSA and AES Cryptographic Provider" ` |
| 138 | + -Type CodeSigningCert ` |
| 139 | + -NotAfter (Get-Date).AddYears(5) ` |
| 140 | + -CertStoreLocation "Cert:\CurrentUser\My" |
| 141 | + |
| 142 | + Write-Log "Exporting certificate to $script:certPath" "txtCertLog" |
| 143 | + |
| 144 | + $securePassword = ConvertTo-SecureString -String $script:certPassword -AsPlainText -Force |
| 145 | + $cert | Export-PfxCertificate -FilePath $script:certPath -Password $securePassword |
| 146 | + |
| 147 | + if (Test-Path $script:certPath) { |
| 148 | + Write-Log "Certificate created successfully!" "txtCertLog" |
| 149 | + $controls.txtCertPath.Text = $script:certPath |
| 150 | + } else { |
| 151 | + throw "Failed to create certificate file" |
| 152 | + } |
| 153 | + } catch { |
| 154 | + Write-Log "Error: $_" "txtCertLog" |
| 155 | + } |
| 156 | +}) |
| 157 | + |
| 158 | +# Sign executable |
| 159 | +$controls.btnSign.Add_Click({ |
| 160 | + if (-not $controls.txtExePath.Text) { |
| 161 | + [System.Windows.MessageBox]::Show("Please select an executable to sign.", "Error", "OK", "Error") |
| 162 | + return |
| 163 | + } |
| 164 | + |
| 165 | + if (-not (Test-Path $script:certPath)) { |
| 166 | + [System.Windows.MessageBox]::Show("Certificate not found. Please create a certificate first.", "Error", "OK", "Error") |
| 167 | + return |
| 168 | + } |
| 169 | + |
| 170 | + try { |
| 171 | + Write-Log "Starting signing process..." "txtLog" |
| 172 | + |
| 173 | + # Try to find signtool.exe |
| 174 | + $signToolPaths = @( |
| 175 | + "C:\Program Files (x86)\Windows Kits\10\bin\*\x64\signtool.exe", |
| 176 | + "C:\Program Files (x86)\Windows Kits\10\App Certification Kit\signtool.exe", |
| 177 | + "C:\Program Files\Microsoft SDKs\Windows\v*\bin\signtool.exe" |
| 178 | + ) |
| 179 | + |
| 180 | + $signTool = Get-ChildItem -Path $signToolPaths -ErrorAction SilentlyContinue | |
| 181 | + Sort-Object -Property VersionInfo.ProductVersion -Descending | |
| 182 | + Select-Object -First 1 -ExpandProperty FullName |
| 183 | + |
| 184 | + if (-not $signTool) { |
| 185 | + throw "signtool.exe not found. Please install Windows SDK." |
| 186 | + } Write-Log "Using signtool: $signTool" "txtLog" |
| 187 | + Write-Log "Signing $($controls.txtExePath.Text)..." "txtLog" |
| 188 | + |
| 189 | + $arguments = @( |
| 190 | + "sign", |
| 191 | + "/f", $script:certPath, |
| 192 | + "/p", $script:certPassword, |
| 193 | + "/fd", "sha256", |
| 194 | + "/tr", "http://timestamp.digicert.com", |
| 195 | + "/td", "sha256", |
| 196 | + $controls.txtExePath.Text |
| 197 | + ) |
| 198 | + |
| 199 | + $process = Start-Process -FilePath $signTool -ArgumentList $arguments -NoNewWindow -Wait -PassThru |
| 200 | + |
| 201 | + if ($process.ExitCode -eq 0) { |
| 202 | + Write-Log "File signed successfully!" "txtLog" |
| 203 | + [System.Windows.MessageBox]::Show("File signed successfully!", "Success", "OK", "Information") |
| 204 | + } else { |
| 205 | + throw "Signing failed with exit code: $($process.ExitCode)" |
| 206 | + } |
| 207 | + } catch { |
| 208 | + Write-Log "Error: $_" "txtLog" |
| 209 | + [System.Windows.MessageBox]::Show("Error: $_", "Error", "OK", "Error") |
| 210 | + } |
| 211 | +}) |
| 212 | + |
| 213 | +# Close button |
| 214 | +$controls.btnClose.Add_Click({ $window.Close() }) |
| 215 | + |
| 216 | +# Show the window |
| 217 | +$window.ShowDialog() | Out-Null |
0 commit comments