|
1 | 1 | # frozen_string_literal: true |
2 | 2 |
|
3 | | -require "open3" |
4 | | - |
5 | 3 | require_relative "macho/utils" |
6 | 4 | require_relative "macho/structure" |
7 | 5 | require_relative "macho/view" |
8 | 6 | require_relative "macho/headers" |
| 7 | +require_relative "macho/code_signing" |
9 | 8 | require_relative "macho/load_commands" |
10 | 9 | require_relative "macho/sections" |
11 | 10 | require_relative "macho/macho_file" |
@@ -42,20 +41,20 @@ def self.open(filename) |
42 | 41 | file |
43 | 42 | end |
44 | 43 |
|
45 | | - # Signs the dylib using an ad-hoc identity. |
46 | | - # Necessary after making any changes to a dylib, since otherwise |
47 | | - # changing a signed file invalidates its signature. |
| 44 | + # Signs a thin or fat Mach-O using an ad-hoc identity. |
| 45 | + # Necessary after changing signed Mach-O data because the signature covers |
| 46 | + # the header, load commands and all bytes preceding the signature. |
48 | 47 | # @param filename [String] the file being opened |
49 | 48 | # @return [void] |
50 | | - # @raise [ModificationError] if the operation fails |
| 49 | + # @raise [CodeSigningError] if the operation fails |
51 | 50 | def self.codesign!(filename) |
52 | | - raise ArgumentError, "codesign binary is not available on Linux" if RUBY_PLATFORM !~ /darwin/ |
53 | 51 | raise ArgumentError, "#{filename}: no such file" unless File.file?(filename) |
54 | 52 |
|
55 | | - _, _, status = Open3.capture3("codesign", "--sign", "-", "--force", |
56 | | - "--preserve-metadata=entitlements,requirements,flags,runtime", |
57 | | - filename) |
58 | | - |
59 | | - raise CodeSigningError, "#{filename}: signing failed!" unless status.success? |
| 53 | + file = MachO.open(filename) |
| 54 | + file.codesign! |
| 55 | + file.write! |
| 56 | + nil |
| 57 | + rescue MachOError => e |
| 58 | + raise CodeSigningError, "#{filename}: signing failed: #{e.message}" |
60 | 59 | end |
61 | 60 | end |
0 commit comments