native-packages 0.10.0 → 0.11.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,284 @@
1
+ # frozen_string_literal: true
2
+
3
+ module NativePackages
4
+ # Authenticode signing through Azure Artifact Signing (formerly Trusted
5
+ # Signing). The configuration opts in; the account coordinates come from the
6
+ # environment, and Azure authentication from an existing Azure CLI sign-in
7
+ # (`az login`, or azure/login with OIDC in CI). No secret is read or stored.
8
+ class WindowsSigning
9
+ include Support
10
+ PROVIDER = "azure-artifact-signing"
11
+ KEYS = %w[provider description description_url publisher].freeze
12
+ VARIABLES = %w[AZURE_SIGNING_ENDPOINT AZURE_SIGNING_ACCOUNT AZURE_SIGNING_PROFILE].freeze
13
+ TIMESTAMP = "http://timestamp.acs.microsoft.com"
14
+ # Every DefaultAzureCredential source except the Azure CLI's, so a stray
15
+ # client secret or cached account can never sign in its place.
16
+ EXCLUDED_CREDENTIALS = %w[EnvironmentCredential WorkloadIdentityCredential ManagedIdentityCredential
17
+ SharedTokenCacheCredential VisualStudioCredential VisualStudioCodeCredential AzurePowerShellCredential
18
+ AzureDeveloperCliCredential InteractiveBrowserCredential].freeze
19
+ # The versions azure/artifact-signing-action 2.0.0 installs, pinned by hash.
20
+ TOOLS = {
21
+ "signtool" => { "package" => "microsoft.windows.sdk.buildtools", "version" => "10.0.26100.4188",
22
+ "sha256" => "180deb372659029864c10a0c04787833234d64aacd1d2c0661d2c00295d8e022",
23
+ "directory" => "bin/10.0.26100.0/x64", "file" => "signtool.exe", "override" => "NATIVE_PACKAGES_SIGNTOOL" },
24
+ "dlib" => { "package" => "microsoft.artifactsigning.client", "version" => "1.0.128",
25
+ "sha256" => "74bd7d27e6ce1051409c38d9b46bc8df0400ecd643d51ffbf2ac00869061e40b",
26
+ "directory" => "bin/x64", "file" => "Azure.CodeSigning.Dlib.dll", "override" => "NATIVE_PACKAGES_ARTIFACT_SIGNING_DLIB" }
27
+ }.freeze
28
+ # Command lines on Windows hold 32,767 characters; leave room for options.
29
+ BATCH_CHARACTERS = 16_000
30
+ SIGNATURES = <<~'POWERSHELL'
31
+ $ErrorActionPreference = 'Stop'
32
+ $simple = [System.Security.Cryptography.X509Certificates.X509NameType]::SimpleName
33
+ $paths = @(Get-Content -LiteralPath $env:NATIVE_PACKAGES_SIGNATURE_LIST -Encoding UTF8)
34
+ $result = @(foreach ($line in $paths) {
35
+ # Plain strings: lines from Get-Content carry note properties that
36
+ # ConvertTo-Json would otherwise write out as an object.
37
+ $path = [string]$line
38
+ $signature = Get-AuthenticodeSignature -LiteralPath $path
39
+ $certificate = $signature.SignerCertificate
40
+ $publisher = $null
41
+ $subject = $null
42
+ if ($certificate) {
43
+ $publisher = $certificate.GetNameInfo($simple, $false)
44
+ $subject = $certificate.Subject
45
+ }
46
+ [pscustomobject]@{ path = $path; status = [string]$signature.Status; message = [string]$signature.StatusMessage;
47
+ publisher = $publisher; subject = $subject; timestamped = [bool]$signature.TimeStamperCertificate }
48
+ })
49
+ [Convert]::ToBase64String([Text.Encoding]::UTF8.GetBytes((ConvertTo-Json -InputObject $result -Compress -Depth 3)))
50
+ POWERSHELL
51
+
52
+ # Only the tool's own directory leaves the NuGet package (a ZIP file).
53
+ EXTRACT = <<~'POWERSHELL'
54
+ $ErrorActionPreference = 'Stop'
55
+ Add-Type -AssemblyName System.IO.Compression.FileSystem
56
+ $prefix = $env:NATIVE_PACKAGES_PREFIX + '/'
57
+ $zip = [System.IO.Compression.ZipFile]::OpenRead($env:NATIVE_PACKAGES_ARCHIVE)
58
+ try {
59
+ foreach ($entry in $zip.Entries) {
60
+ if (-not $entry.FullName.StartsWith($prefix) -or -not $entry.Name -or $entry.FullName.Contains('..')) { continue }
61
+ $target = Join-Path $env:NATIVE_PACKAGES_DESTINATION $entry.FullName
62
+ New-Item -ItemType Directory -Force -Path (Split-Path -Parent $target) | Out-Null
63
+ [System.IO.Compression.ZipFileExtensions]::ExtractToFile($entry, $target, $true)
64
+ }
65
+ } finally {
66
+ $zip.Dispose()
67
+ }
68
+ POWERSHELL
69
+
70
+ attr_reader :root, :settings
71
+
72
+ def self.validate(data)
73
+ windows = data.fetch("windows", {})
74
+ raise Error, "windows: expected a mapping" unless windows.is_a?(Hash)
75
+ unknown = windows.keys - %w[signing]
76
+ raise Error, "windows: unknown fields #{unknown.join(', ')}" unless unknown.empty?
77
+ return unless windows.key?("signing")
78
+ signing = windows["signing"]
79
+ raise Error, "windows.signing: expected a mapping" unless signing.is_a?(Hash)
80
+ unknown = signing.keys - KEYS
81
+ raise Error, "windows.signing: unknown fields #{unknown.join(', ')}" unless unknown.empty?
82
+ raise Error, "windows.signing.provider: use #{PROVIDER}" unless signing["provider"] == PROVIDER
83
+ %w[description publisher].each do |key|
84
+ next unless signing.key?(key)
85
+ raise Error, "windows.signing.#{key}: expected a nonempty string" unless signing[key].is_a?(String) && !signing[key].strip.empty?
86
+ end
87
+ if signing.key?("description_url") && !(signing["description_url"].is_a?(String) && %r{\Ahttps://\S+\z}.match?(signing["description_url"]))
88
+ raise Error, "windows.signing.description_url: expected an https:// URL"
89
+ end
90
+ end
91
+
92
+ def initialize(configuration, environment = ENV)
93
+ @root = configuration.root
94
+ self.class.validate(configuration.data)
95
+ @settings = configuration.data.fetch("windows", {}).fetch("signing", nil)
96
+ @environment = environment
97
+ @values = VARIABLES.to_h { |name| [name, environment[name].to_s.strip] }
98
+ @description = settings && settings.fetch("description", configuration.name)
99
+ @description_url = settings && (settings["description_url"] || configuration.data.fetch("nfpm")["homepage"])
100
+ end
101
+
102
+ def configured? = !settings.nil?
103
+ def enabled? = configured? && @values.values.any? { |value| !value.empty? }
104
+ def state = !configured? ? "off" : enabled? ? "enabled" : "off: #{VARIABLES.join(', ')} not set"
105
+
106
+ def doctor
107
+ return unless enabled?
108
+ missing = @values.select { |_, value| value.empty? }.keys
109
+ raise Error, "incomplete Azure Artifact Signing settings: set #{missing.join(', ')}" unless missing.empty?
110
+ unless %r{\Ahttps://[a-z0-9-]+\.codesigning\.azure\.net/?\z}.match?(@values.fetch("AZURE_SIGNING_ENDPOINT"))
111
+ raise Error, "AZURE_SIGNING_ENDPOINT must be an Artifact Signing endpoint such as https://weu.codesigning.azure.net/"
112
+ end
113
+ %w[AZURE_SIGNING_ACCOUNT AZURE_SIGNING_PROFILE].each do |name|
114
+ raise Error, "#{name} must be a name, not a path or URL" unless /\A[A-Za-z0-9][A-Za-z0-9-]*\z/.match?(@values.fetch(name))
115
+ end
116
+ raise Error, "Windows signing must run on Windows" unless NativeRecipe.host == "windows"
117
+ tools = %w[powershell az]
118
+ tools << "curl" if TOOLS.values.any? { |tool| @environment[tool.fetch("override")].to_s.empty? }
119
+ missing_tools = tools.reject { |tool| available?(tool) }
120
+ raise Error, "install required Windows signing tools: #{missing_tools.join(', ')}" unless missing_tools.empty?
121
+ TOOLS.each_value do |tool|
122
+ path = @environment[tool.fetch("override")].to_s
123
+ raise Error, "#{tool.fetch('override')} does not name a file: #{path}" unless path.empty? || File.file?(path)
124
+ end
125
+ begin
126
+ execute "az", "account", "show", "--output", "none"
127
+ rescue Error
128
+ raise Error, "sign in to Azure before signing: az login, or azure/login with OIDC in GitHub Actions"
129
+ end
130
+ nil
131
+ end
132
+
133
+ def with_identity
134
+ doctor
135
+ return yield(nil) unless enabled?
136
+ Dir.mktmpdir("native-packages-windows-signing-") do |directory|
137
+ directory = Pathname.new(directory)
138
+ @signtool = tool("signtool", directory)
139
+ @dlib = tool("dlib", directory)
140
+ @metadata = directory / "metadata.json"
141
+ write(@metadata, JSON.pretty_generate("Endpoint" => @values.fetch("AZURE_SIGNING_ENDPOINT"),
142
+ "CodeSigningAccountName" => @values.fetch("AZURE_SIGNING_ACCOUNT"),
143
+ "CertificateProfileName" => @values.fetch("AZURE_SIGNING_PROFILE"),
144
+ "ExcludeCredentials" => EXCLUDED_CREDENTIALS) + "\n")
145
+ yield self
146
+ ensure
147
+ @signtool = @dlib = @metadata = nil
148
+ end
149
+ end
150
+
151
+ # A package recipe gets a signed copy of its input. Code that already
152
+ # carries a valid signature, ours from sign-windows or a vendor's, stays.
153
+ def sign_payload(payload)
154
+ binaries = NativeRecipe.new(root).tree(payload).filter_map do |relative, kind, *|
155
+ path = payload / relative
156
+ path if kind == "file" && portable_executable?(path)
157
+ end
158
+ unsigned = signatures(binaries).reject { |record| record.fetch("status") == "Valid" }.map { |record| Pathname.new(record.fetch("path")) }
159
+ sign(unsigned) unless unsigned.empty?
160
+ puts "Signed #{unsigned.length} of #{binaries.length} Windows binaries in the package input."
161
+ unsigned.length
162
+ end
163
+
164
+ def sign_package(package)
165
+ record = sign([package]).first
166
+ { "signature" => "authenticode", "provider" => PROVIDER, "publisher" => record.fetch("publisher"),
167
+ "timestamped" => true }
168
+ end
169
+
170
+ # Signs files in place, in as few SignTool calls as fit a command line,
171
+ # then checks every signature.
172
+ def sign(paths)
173
+ paths = paths.map { |path| Pathname.new(path).expand_path(root) }
174
+ paths.each { |path| raise Error, "not a regular file: #{path}" unless path.file? && !path.symlink? }
175
+ batches = paths.each_with_object([[]]) do |path, groups|
176
+ length = groups.last.sum { |item| item.to_s.length + 3 } + path.to_s.length + 3
177
+ groups << [] if !groups.last.empty? && length > BATCH_CHARACTERS
178
+ groups.last << path
179
+ end
180
+ batches.each do |batch|
181
+ arguments = [@signtool, "sign", "/v", "/fd", "SHA256", "/tr", TIMESTAMP, "/td", "SHA256",
182
+ "/dlib", @dlib, "/dmdf", @metadata, "/d", @description]
183
+ arguments += ["/du", @description_url] if @description_url
184
+ output = signtool(*arguments, *batch)
185
+ puts output unless output.empty?
186
+ end
187
+ verify(paths)
188
+ end
189
+
190
+ def verify(paths)
191
+ signatures(paths).map do |record|
192
+ path = record.fetch("path")
193
+ unless record.fetch("status") == "Valid" && record.fetch("timestamped")
194
+ raise Error, "#{path} is not validly signed and timestamped: #{record.fetch('status')} #{record.fetch('message')}".strip
195
+ end
196
+ if settings["publisher"] && record.fetch("publisher") != settings["publisher"]
197
+ raise Error, "#{path} is signed by #{record.fetch('publisher').inspect}, not windows.signing.publisher #{settings['publisher'].inspect}"
198
+ end
199
+ puts "#{File.basename(path)}: signed by #{record.fetch('subject')}, timestamped"
200
+ record
201
+ end
202
+ end
203
+
204
+ def signatures(paths)
205
+ return [] if paths.empty?
206
+ records = Dir.mktmpdir("native-packages-signatures-") do |directory|
207
+ list = Pathname.new(directory) / "files.txt"
208
+ write(list, paths.map { |path| Pathname.new(path).expand_path(root).to_s }.join("\n") + "\n")
209
+ JSON.parse(powershell(SIGNATURES, "NATIVE_PACKAGES_SIGNATURE_LIST" => list.to_s).strip.unpack1("m0").force_encoding(Encoding::UTF_8))
210
+ end
211
+ raise Error, "unexpected signature report" unless records.is_a?(Array) && records.length == paths.length
212
+ records
213
+ rescue ArgumentError, JSON::ParserError
214
+ raise Error, "could not read Authenticode signatures"
215
+ end
216
+
217
+ # `native-packages sign-windows FILE...`: explicit, so it never skips.
218
+ def sign_files(paths)
219
+ raise Error, "add windows.signing to the configuration to sign Windows files" unless configured?
220
+ raise Error, "set #{VARIABLES.join(', ')} to sign Windows files" unless enabled?
221
+ raise Error, "sign-windows needs at least one file" if paths.empty?
222
+ with_identity { |signing| signing.sign(paths) }
223
+ end
224
+
225
+ def portable_executable?(path)
226
+ File.open(path, "rb") do |file|
227
+ next false unless file.size >= 64 && file.read(2) == "MZ"
228
+ file.seek(0x3c)
229
+ offset = file.read(4).unpack1("L<")
230
+ next false unless offset >= 64 && offset + 4 <= file.size
231
+ file.seek(offset)
232
+ file.read(4) == "PE\0\0"
233
+ end
234
+ end
235
+
236
+ # SignTool and the Artifact Signing client from nuget.org, hash-checked on
237
+ # every use, or local copies named by the override variables.
238
+ def tool(name, directory)
239
+ tool = TOOLS.fetch(name)
240
+ override = @environment[tool.fetch("override")].to_s
241
+ return Pathname.new(override).expand_path unless override.empty?
242
+ package, version = tool.values_at("package", "version")
243
+ archive = cache_directory / "#{package}.#{version}.nupkg"
244
+ download("https://api.nuget.org/v3-flatcontainer/#{package}/#{version}/#{package}.#{version}.nupkg", archive)
245
+ unless sha256(archive) == tool.fetch("sha256")
246
+ archive.delete
247
+ raise Error, "#{package} #{version} from nuget.org does not match its pinned SHA-256"
248
+ end
249
+ destination = directory / name
250
+ destination.mkpath
251
+ powershell(EXTRACT, "NATIVE_PACKAGES_ARCHIVE" => archive.to_s, "NATIVE_PACKAGES_PREFIX" => tool.fetch("directory"),
252
+ "NATIVE_PACKAGES_DESTINATION" => destination.to_s)
253
+ path = destination / tool.fetch("directory") / tool.fetch("file")
254
+ raise Error, "#{package} #{version} has no #{tool.fetch('file')}" unless path.file?
255
+ path
256
+ end
257
+
258
+ def cache_directory
259
+ base = @environment["LOCALAPPDATA"].to_s.empty? ? File.join(Dir.home, ".cache") : @environment["LOCALAPPDATA"]
260
+ Pathname.new(base) / "native-packages/windows-signing"
261
+ end
262
+
263
+ # SignTool and the Artifact Signing client report failures on standard
264
+ # output, so a failure shows both streams.
265
+ def signtool(*arguments)
266
+ output, status = Open3.capture2e(*arguments.map(&:to_s), chdir: root.to_s, binmode: true)
267
+ raise Error, "SignTool failed:\n#{output.strip}" unless status.success?
268
+ output.strip
269
+ end
270
+
271
+ # Windows PowerShell started from PowerShell 7 inherits a module path
272
+ # where its own Microsoft.PowerShell.Security cannot load; without one, it
273
+ # uses its default.
274
+ def powershell(script, env)
275
+ encoded = [script.encode(Encoding::UTF_16LE)].pack("m0")
276
+ execute("powershell", "-NoProfile", "-NonInteractive", "-ExecutionPolicy", "Bypass", "-EncodedCommand", encoded,
277
+ env: env.merge("PSModulePath" => nil))
278
+ end
279
+
280
+ def execute(*arguments, env: {})
281
+ capture(*arguments, env: env)
282
+ end
283
+ end
284
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: native-packages
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.10.0
4
+ version: 0.11.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Carmine Paolino
@@ -27,9 +27,11 @@ files:
27
27
  - docs/_guides/github-actions.md
28
28
  - docs/_guides/multi-platform.md
29
29
  - docs/_guides/native-recipes.md
30
+ - docs/_guides/omarchy.md
30
31
  - docs/_guides/prereleases.md
31
32
  - docs/_guides/publishing.md
32
33
  - docs/_guides/release-signing.md
34
+ - docs/_guides/windows-signing.md
33
35
  - docs/_reference/commands.md
34
36
  - docs/_reference/configuration.md
35
37
  - docs/_reference/platforms.md
@@ -59,11 +61,13 @@ files:
59
61
  - lib/native_packages/inspection.rb
60
62
  - lib/native_packages/macos_signing.rb
61
63
  - lib/native_packages/native_recipe.rb
64
+ - lib/native_packages/omarchy.rb
62
65
  - lib/native_packages/project.rb
63
66
  - lib/native_packages/release_signing.rb
64
67
  - lib/native_packages/repositories.rb
65
68
  - lib/native_packages/scaffold.rb
66
69
  - lib/native_packages/support.rb
70
+ - lib/native_packages/windows_signing.rb
67
71
  homepage: https://github.com/crmne/native-packages
68
72
  licenses:
69
73
  - MIT