]>
Commit | Line | Data |
---|---|---|
ab113d22 | 1 | /* ldid - (Mach-O) Link-Loader Identity Editor |
16efdde5 | 2 | * Copyright (C) 2007-2015 Jay Freeman (saurik) |
a362a82f JF |
3 | */ |
4 | ||
dd6c06ed JF |
5 | /* GNU Affero General Public License, Version 3 {{{ */ |
6 | /* | |
7 | * This program is free software: you can redistribute it and/or modify | |
8 | * it under the terms of the GNU Affero General Public License as published by | |
e0524446 JF |
9 | * the Free Software Foundation, either version 3 of the License, or |
10 | * (at your option) any later version. | |
dd6c06ed | 11 | |
e0524446 JF |
12 | * This program is distributed in the hope that it will be useful, |
13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
dd6c06ed JF |
15 | * GNU Affero General Public License for more details. |
16 | ||
17 | * You should have received a copy of the GNU Affero General Public License | |
e0524446 JF |
18 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
19 | **/ | |
20 | /* }}} */ | |
a362a82f | 21 | |
fa59c178 JF |
22 | #include <cstdio> |
23 | #include <cstdlib> | |
a362a82f | 24 | #include <cstring> |
4d4682ef | 25 | #include <fstream> |
23c11ee8 | 26 | #include <iostream> |
4527f2af | 27 | #include <memory> |
23c11ee8 | 28 | #include <set> |
4d4682ef | 29 | #include <sstream> |
a362a82f JF |
30 | #include <string> |
31 | #include <vector> | |
32 | ||
54a0f854 | 33 | #include <dirent.h> |
fa59c178 | 34 | #include <errno.h> |
dede6121 | 35 | #include <fcntl.h> |
23c11ee8 | 36 | #include <regex.h> |
fa59c178 JF |
37 | #include <stdbool.h> |
38 | #include <stdint.h> | |
a7f01a65 | 39 | #include <unistd.h> |
dede6121 JF |
40 | |
41 | #include <sys/mman.h> | |
42 | #include <sys/stat.h> | |
23c11ee8 | 43 | #include <sys/types.h> |
dede6121 | 44 | |
489c97b8 | 45 | #ifndef LDID_NOSMIME |
30c64adb JF |
46 | #include <openssl/err.h> |
47 | #include <openssl/pem.h> | |
48 | #include <openssl/pkcs7.h> | |
49 | #include <openssl/pkcs12.h> | |
489c97b8 | 50 | #endif |
84d29a1c JF |
51 | |
52 | #ifdef __APPLE__ | |
53 | #include <CommonCrypto/CommonDigest.h> | |
9ec8439b | 54 | |
84d29a1c JF |
55 | #define LDID_SHA1_DIGEST_LENGTH CC_SHA1_DIGEST_LENGTH |
56 | #define LDID_SHA1 CC_SHA1 | |
57 | #define LDID_SHA1_CTX CC_SHA1_CTX | |
58 | #define LDID_SHA1_Init CC_SHA1_Init | |
59 | #define LDID_SHA1_Update CC_SHA1_Update | |
60 | #define LDID_SHA1_Final CC_SHA1_Final | |
9ec8439b JF |
61 | |
62 | #define LDID_SHA256_DIGEST_LENGTH CC_SHA256_DIGEST_LENGTH | |
63 | #define LDID_SHA256 CC_SHA256 | |
64 | #define LDID_SHA256_CTX CC_SHA256_CTX | |
65 | #define LDID_SHA256_Init CC_SHA256_Init | |
66 | #define LDID_SHA256_Update CC_SHA256_Update | |
67 | #define LDID_SHA256_Final CC_SHA256_Final | |
84d29a1c | 68 | #else |
a50bb1be | 69 | #include <openssl/sha.h> |
9ec8439b | 70 | |
84d29a1c JF |
71 | #define LDID_SHA1_DIGEST_LENGTH SHA_DIGEST_LENGTH |
72 | #define LDID_SHA1 SHA1 | |
73 | #define LDID_SHA1_CTX SHA_CTX | |
74 | #define LDID_SHA1_Init SHA1_Init | |
75 | #define LDID_SHA1_Update SHA1_Update | |
76 | #define LDID_SHA1_Final SHA1_Final | |
9ec8439b JF |
77 | |
78 | #define LDID_SHA256_DIGEST_LENGTH SHA256_DIGEST_LENGTH | |
79 | #define LDID_SHA256 SHA256 | |
80 | #define LDID_SHA256_CTX SHA256_CTX | |
81 | #define LDID_SHA256_Init SHA256_Init | |
82 | #define LDID_SHA256_Update SHA256_Update | |
83 | #define LDID_SHA256_Final SHA256_Final | |
84d29a1c | 84 | #endif |
a50bb1be | 85 | |
489c97b8 | 86 | #ifndef LDID_NOPLIST |
8dfe0afc | 87 | #include <plist/plist.h> |
1190f447 JF |
88 | #elif __APPLE__ |
89 | #include <CoreFoundation/CoreFoundation.h> | |
489c97b8 | 90 | #endif |
15babeef | 91 | |
a0c715e9 JF |
92 | #include "ldid.hpp" |
93 | ||
fa59c178 JF |
94 | #define _assert___(line) \ |
95 | #line | |
96 | #define _assert__(line) \ | |
97 | _assert___(line) | |
fa59c178 | 98 | |
3736a011 JF |
99 | #ifndef $ |
100 | #define $(value) value | |
101 | #endif | |
102 | ||
489c97b8 | 103 | #ifdef __EXCEPTIONS |
eff987d2 | 104 | #define _assert_(expr, format, ...) \ |
fa59c178 | 105 | do if (!(expr)) { \ |
3736a011 JF |
106 | fprintf(stderr, $("%s(%u): _assert(): " format "\n"), __FILE__, __LINE__, ## __VA_ARGS__); \ |
107 | throw $(__FILE__ "(" _assert__(__LINE__) "): _assert(" #expr ")"); \ | |
fa59c178 | 108 | } while (false) |
489c97b8 JF |
109 | #else |
110 | // XXX: this is not acceptable | |
111 | #define _assert_(expr, format, ...) \ | |
112 | do if (!(expr)) { \ | |
3736a011 | 113 | fprintf(stderr, $("%s(%u): _assert(): " format "\n"), __FILE__, __LINE__, ## __VA_ARGS__); \ |
489c97b8 JF |
114 | exit(-1); \ |
115 | } while (false) | |
116 | #endif | |
fa59c178 | 117 | |
eff987d2 | 118 | #define _assert(expr) \ |
3736a011 | 119 | _assert_(expr, "%s", $(#expr)) |
eff987d2 | 120 | |
23c11ee8 JF |
121 | #define _syscall(expr, ...) [&] { for (;;) { \ |
122 | auto _value(expr); \ | |
123 | if ((long) _value != -1) \ | |
124 | return _value; \ | |
125 | int error(errno); \ | |
126 | if (error == EINTR) \ | |
127 | continue; \ | |
5c303467 JF |
128 | /* XXX: EINTR is included in this list to fix g++ */ \ |
129 | for (auto success : (long[]) {EINTR, __VA_ARGS__}) \ | |
23c11ee8 JF |
130 | if (error == success) \ |
131 | return (decltype(expr)) -success; \ | |
132 | _assert_(false, "errno=%u", error); \ | |
133 | } }() | |
fa59c178 JF |
134 | |
135 | #define _trace() \ | |
3736a011 | 136 | fprintf(stderr, $("_trace(%s:%u): %s\n"), __FILE__, __LINE__, $(__FUNCTION__)) |
fa59c178 JF |
137 | |
138 | #define _not(type) \ | |
139 | ((type) ~ (type) 0) | |
140 | ||
141 | #define _packed \ | |
142 | __attribute__((packed)) | |
143 | ||
144 | template <typename Type_> | |
145 | struct Iterator_ { | |
146 | typedef typename Type_::const_iterator Result; | |
147 | }; | |
148 | ||
149 | #define _foreach(item, list) \ | |
150 | for (bool _stop(true); _stop; ) \ | |
151 | for (const __typeof__(list) &_list = (list); _stop; _stop = false) \ | |
152 | for (Iterator_<__typeof__(list)>::Result _item = _list.begin(); _item != _list.end(); ++_item) \ | |
153 | for (bool _suck(true); _suck; _suck = false) \ | |
154 | for (const __typeof__(*_item) &item = *_item; _suck; _suck = false) | |
155 | ||
23c11ee8 JF |
156 | class _Scope { |
157 | }; | |
158 | ||
159 | template <typename Function_> | |
160 | class Scope : | |
161 | public _Scope | |
162 | { | |
163 | private: | |
164 | Function_ function_; | |
165 | ||
166 | public: | |
167 | Scope(const Function_ &function) : | |
168 | function_(function) | |
169 | { | |
170 | } | |
171 | ||
172 | ~Scope() { | |
173 | function_(); | |
174 | } | |
175 | }; | |
176 | ||
177 | template <typename Function_> | |
178 | Scope<Function_> _scope(const Function_ &function) { | |
179 | return Scope<Function_>(function); | |
180 | } | |
181 | ||
182 | #define _scope__(counter, function) \ | |
183 | __attribute__((__unused__)) \ | |
184 | const _Scope &_scope ## counter(_scope([&]function)) | |
185 | #define _scope_(counter, function) \ | |
186 | _scope__(counter, function) | |
187 | #define _scope(function) \ | |
188 | _scope_(__COUNTER__, function) | |
189 | ||
6f420711 JF |
190 | #define CPU_ARCH_MASK uint32_t(0xff000000) |
191 | #define CPU_ARCH_ABI64 uint32_t(0x01000000) | |
192 | ||
193 | #define CPU_TYPE_ANY uint32_t(-1) | |
194 | #define CPU_TYPE_VAX uint32_t( 1) | |
195 | #define CPU_TYPE_MC680x0 uint32_t( 6) | |
196 | #define CPU_TYPE_X86 uint32_t( 7) | |
197 | #define CPU_TYPE_MC98000 uint32_t(10) | |
198 | #define CPU_TYPE_HPPA uint32_t(11) | |
199 | #define CPU_TYPE_ARM uint32_t(12) | |
200 | #define CPU_TYPE_MC88000 uint32_t(13) | |
201 | #define CPU_TYPE_SPARC uint32_t(14) | |
202 | #define CPU_TYPE_I860 uint32_t(15) | |
203 | #define CPU_TYPE_POWERPC uint32_t(18) | |
204 | ||
205 | #define CPU_TYPE_I386 CPU_TYPE_X86 | |
206 | ||
207 | #define CPU_TYPE_ARM64 (CPU_ARCH_ABI64 | CPU_TYPE_ARM) | |
208 | #define CPU_TYPE_POWERPC64 (CPU_ARCH_ABI64 | CPU_TYPE_POWERPC) | |
209 | #define CPU_TYPE_X86_64 (CPU_ARCH_ABI64 | CPU_TYPE_X86) | |
210 | ||
a362a82f JF |
211 | struct fat_header { |
212 | uint32_t magic; | |
213 | uint32_t nfat_arch; | |
c05d9758 | 214 | } _packed; |
a362a82f JF |
215 | |
216 | #define FAT_MAGIC 0xcafebabe | |
217 | #define FAT_CIGAM 0xbebafeca | |
218 | ||
219 | struct fat_arch { | |
220 | uint32_t cputype; | |
221 | uint32_t cpusubtype; | |
222 | uint32_t offset; | |
223 | uint32_t size; | |
224 | uint32_t align; | |
c05d9758 | 225 | } _packed; |
a362a82f JF |
226 | |
227 | struct mach_header { | |
228 | uint32_t magic; | |
229 | uint32_t cputype; | |
04802ab1 | 230 | uint32_t cpusubtype; |
a362a82f JF |
231 | uint32_t filetype; |
232 | uint32_t ncmds; | |
233 | uint32_t sizeofcmds; | |
234 | uint32_t flags; | |
c05d9758 | 235 | } _packed; |
a362a82f | 236 | |
fdb119ef | 237 | #define MH_MAGIC 0xfeedface |
a362a82f JF |
238 | #define MH_CIGAM 0xcefaedfe |
239 | ||
3cbc6463 JF |
240 | #define MH_MAGIC_64 0xfeedfacf |
241 | #define MH_CIGAM_64 0xcffaedfe | |
242 | ||
82813bde JF |
243 | #define MH_DYLDLINK 0x4 |
244 | ||
4a864785 | 245 | #define MH_OBJECT 0x1 |
efd6cd4a JF |
246 | #define MH_EXECUTE 0x2 |
247 | #define MH_DYLIB 0x6 | |
248 | #define MH_BUNDLE 0x8 | |
249 | #define MH_DYLIB_STUB 0x9 | |
a362a82f JF |
250 | |
251 | struct load_command { | |
252 | uint32_t cmd; | |
253 | uint32_t cmdsize; | |
c05d9758 | 254 | } _packed; |
a362a82f | 255 | |
4a57e66c JF |
256 | #define LC_REQ_DYLD uint32_t(0x80000000) |
257 | ||
258 | #define LC_SEGMENT uint32_t(0x01) | |
259 | #define LC_SYMTAB uint32_t(0x02) | |
260 | #define LC_DYSYMTAB uint32_t(0x0b) | |
261 | #define LC_LOAD_DYLIB uint32_t(0x0c) | |
262 | #define LC_ID_DYLIB uint32_t(0x0d) | |
289ccbbc | 263 | #define LC_SEGMENT_64 uint32_t(0x19) |
4a57e66c JF |
264 | #define LC_UUID uint32_t(0x1b) |
265 | #define LC_CODE_SIGNATURE uint32_t(0x1d) | |
266 | #define LC_SEGMENT_SPLIT_INFO uint32_t(0x1e) | |
267 | #define LC_REEXPORT_DYLIB uint32_t(0x1f | LC_REQ_DYLD) | |
c0cc1574 | 268 | #define LC_ENCRYPTION_INFO uint32_t(0x21) |
4a57e66c JF |
269 | #define LC_DYLD_INFO uint32_t(0x22) |
270 | #define LC_DYLD_INFO_ONLY uint32_t(0x22 | LC_REQ_DYLD) | |
843aea8c | 271 | #define LC_ENCRYPTION_INFO_64 uint32_t(0x2c) |
a362a82f | 272 | |
56f0487d JF |
273 | union Version { |
274 | struct { | |
275 | uint8_t patch; | |
276 | uint8_t minor; | |
277 | uint16_t major; | |
278 | } _packed; | |
279 | ||
280 | uint32_t value; | |
281 | }; | |
282 | ||
a362a82f JF |
283 | struct dylib { |
284 | uint32_t name; | |
285 | uint32_t timestamp; | |
286 | uint32_t current_version; | |
287 | uint32_t compatibility_version; | |
c05d9758 | 288 | } _packed; |
a362a82f JF |
289 | |
290 | struct dylib_command { | |
291 | uint32_t cmd; | |
292 | uint32_t cmdsize; | |
293 | struct dylib dylib; | |
c05d9758 | 294 | } _packed; |
a362a82f JF |
295 | |
296 | struct uuid_command { | |
297 | uint32_t cmd; | |
298 | uint32_t cmdsize; | |
299 | uint8_t uuid[16]; | |
c05d9758 | 300 | } _packed; |
a362a82f | 301 | |
20e7eb29 JF |
302 | struct symtab_command { |
303 | uint32_t cmd; | |
304 | uint32_t cmdsize; | |
305 | uint32_t symoff; | |
306 | uint32_t nsyms; | |
307 | uint32_t stroff; | |
308 | uint32_t strsize; | |
309 | } _packed; | |
310 | ||
4a57e66c JF |
311 | struct dyld_info_command { |
312 | uint32_t cmd; | |
313 | uint32_t cmdsize; | |
314 | uint32_t rebase_off; | |
315 | uint32_t rebase_size; | |
316 | uint32_t bind_off; | |
317 | uint32_t bind_size; | |
318 | uint32_t weak_bind_off; | |
319 | uint32_t weak_bind_size; | |
320 | uint32_t lazy_bind_off; | |
321 | uint32_t lazy_bind_size; | |
322 | uint32_t export_off; | |
323 | uint32_t export_size; | |
324 | } _packed; | |
325 | ||
326 | struct dysymtab_command { | |
327 | uint32_t cmd; | |
328 | uint32_t cmdsize; | |
329 | uint32_t ilocalsym; | |
330 | uint32_t nlocalsym; | |
331 | uint32_t iextdefsym; | |
332 | uint32_t nextdefsym; | |
333 | uint32_t iundefsym; | |
334 | uint32_t nundefsym; | |
335 | uint32_t tocoff; | |
336 | uint32_t ntoc; | |
337 | uint32_t modtaboff; | |
338 | uint32_t nmodtab; | |
339 | uint32_t extrefsymoff; | |
340 | uint32_t nextrefsyms; | |
341 | uint32_t indirectsymoff; | |
342 | uint32_t nindirectsyms; | |
343 | uint32_t extreloff; | |
344 | uint32_t nextrel; | |
345 | uint32_t locreloff; | |
346 | uint32_t nlocrel; | |
347 | } _packed; | |
348 | ||
349 | struct dylib_table_of_contents { | |
350 | uint32_t symbol_index; | |
351 | uint32_t module_index; | |
352 | } _packed; | |
353 | ||
354 | struct dylib_module { | |
355 | uint32_t module_name; | |
356 | uint32_t iextdefsym; | |
357 | uint32_t nextdefsym; | |
358 | uint32_t irefsym; | |
359 | uint32_t nrefsym; | |
360 | uint32_t ilocalsym; | |
361 | uint32_t nlocalsym; | |
362 | uint32_t iextrel; | |
363 | uint32_t nextrel; | |
364 | uint32_t iinit_iterm; | |
365 | uint32_t ninit_nterm; | |
366 | uint32_t objc_module_info_addr; | |
367 | uint32_t objc_module_info_size; | |
368 | } _packed; | |
369 | ||
370 | struct dylib_reference { | |
371 | uint32_t isym:24; | |
372 | uint32_t flags:8; | |
373 | } _packed; | |
374 | ||
375 | struct relocation_info { | |
376 | int32_t r_address; | |
377 | uint32_t r_symbolnum:24; | |
378 | uint32_t r_pcrel:1; | |
379 | uint32_t r_length:2; | |
380 | uint32_t r_extern:1; | |
381 | uint32_t r_type:4; | |
382 | } _packed; | |
383 | ||
384 | struct nlist { | |
385 | union { | |
386 | char *n_name; | |
387 | int32_t n_strx; | |
388 | } n_un; | |
389 | ||
390 | uint8_t n_type; | |
391 | uint8_t n_sect; | |
392 | uint8_t n_desc; | |
393 | uint32_t n_value; | |
394 | } _packed; | |
395 | ||
6e83315b JF |
396 | struct segment_command { |
397 | uint32_t cmd; | |
398 | uint32_t cmdsize; | |
399 | char segname[16]; | |
400 | uint32_t vmaddr; | |
401 | uint32_t vmsize; | |
402 | uint32_t fileoff; | |
403 | uint32_t filesize; | |
404 | uint32_t maxprot; | |
405 | uint32_t initprot; | |
406 | uint32_t nsects; | |
407 | uint32_t flags; | |
429e34a5 | 408 | } _packed; |
6e83315b | 409 | |
289ccbbc JF |
410 | struct segment_command_64 { |
411 | uint32_t cmd; | |
412 | uint32_t cmdsize; | |
413 | char segname[16]; | |
414 | uint64_t vmaddr; | |
415 | uint64_t vmsize; | |
416 | uint64_t fileoff; | |
417 | uint64_t filesize; | |
418 | uint32_t maxprot; | |
419 | uint32_t initprot; | |
420 | uint32_t nsects; | |
421 | uint32_t flags; | |
429e34a5 | 422 | } _packed; |
289ccbbc | 423 | |
6e83315b JF |
424 | struct section { |
425 | char sectname[16]; | |
426 | char segname[16]; | |
427 | uint32_t addr; | |
428 | uint32_t size; | |
429 | uint32_t offset; | |
430 | uint32_t align; | |
431 | uint32_t reloff; | |
432 | uint32_t nreloc; | |
433 | uint32_t flags; | |
434 | uint32_t reserved1; | |
435 | uint32_t reserved2; | |
429e34a5 | 436 | } _packed; |
6e83315b | 437 | |
289ccbbc JF |
438 | struct section_64 { |
439 | char sectname[16]; | |
440 | char segname[16]; | |
441 | uint64_t addr; | |
442 | uint64_t size; | |
443 | uint32_t offset; | |
444 | uint32_t align; | |
445 | uint32_t reloff; | |
446 | uint32_t nreloc; | |
447 | uint32_t flags; | |
448 | uint32_t reserved1; | |
449 | uint32_t reserved2; | |
e4d33eec | 450 | uint32_t reserved3; |
429e34a5 | 451 | } _packed; |
289ccbbc | 452 | |
fdb119ef JF |
453 | struct linkedit_data_command { |
454 | uint32_t cmd; | |
455 | uint32_t cmdsize; | |
456 | uint32_t dataoff; | |
457 | uint32_t datasize; | |
c05d9758 | 458 | } _packed; |
fdb119ef | 459 | |
c0cc1574 JF |
460 | struct encryption_info_command { |
461 | uint32_t cmd; | |
462 | uint32_t cmdsize; | |
463 | uint32_t cryptoff; | |
464 | uint32_t cryptsize; | |
465 | uint32_t cryptid; | |
466 | } _packed; | |
467 | ||
4a864785 JF |
468 | #define BIND_OPCODE_MASK 0xf0 |
469 | #define BIND_IMMEDIATE_MASK 0x0f | |
470 | #define BIND_OPCODE_DONE 0x00 | |
471 | #define BIND_OPCODE_SET_DYLIB_ORDINAL_IMM 0x10 | |
472 | #define BIND_OPCODE_SET_DYLIB_ORDINAL_ULEB 0x20 | |
473 | #define BIND_OPCODE_SET_DYLIB_SPECIAL_IMM 0x30 | |
474 | #define BIND_OPCODE_SET_SYMBOL_TRAILING_FLAGS_IMM 0x40 | |
475 | #define BIND_OPCODE_SET_TYPE_IMM 0x50 | |
476 | #define BIND_OPCODE_SET_ADDEND_SLEB 0x60 | |
477 | #define BIND_OPCODE_SET_SEGMENT_AND_OFFSET_ULEB 0x70 | |
478 | #define BIND_OPCODE_ADD_ADDR_ULEB 0x80 | |
479 | #define BIND_OPCODE_DO_BIND 0x90 | |
480 | #define BIND_OPCODE_DO_BIND_ADD_ADDR_ULEB 0xa0 | |
481 | #define BIND_OPCODE_DO_BIND_ADD_ADDR_IMM_SCALED 0xb0 | |
482 | #define BIND_OPCODE_DO_BIND_ULEB_TIMES_SKIPPING_ULEB 0xc0 | |
483 | ||
b4b49374 JF |
484 | struct : ldid::Progress { |
485 | virtual void operator()(const std::string &value) const { | |
486 | } | |
487 | ||
488 | virtual void operator()(double value) const { | |
489 | } | |
490 | } dummy_; | |
491 | ||
492 | struct Progression : ldid::Progress { | |
493 | const ldid::Progress &progress_; | |
494 | std::string name_; | |
495 | ||
496 | Progression(const ldid::Progress &progress, const std::string &name) : | |
497 | progress_(progress), | |
498 | name_(name) | |
499 | { | |
500 | } | |
501 | ||
502 | virtual void operator()(const std::string &value) const { | |
503 | return progress_(name_ + " (" + value + ")"); | |
504 | } | |
505 | ||
506 | virtual void operator()(double value) const { | |
507 | return progress_(value); | |
508 | } | |
509 | }; | |
e1d26767 | 510 | |
f9cd1d1c JF |
511 | static std::streamsize read(std::streambuf &stream, void *data, size_t size) { |
512 | auto writ(stream.sgetn(static_cast<char *>(data), size)); | |
513 | _assert(writ >= 0); | |
514 | return writ; | |
515 | } | |
516 | ||
ef3241bb | 517 | static inline void get(std::streambuf &stream, void *data, size_t size) { |
f9cd1d1c | 518 | _assert(read(stream, data, size) == size); |
4d4682ef JF |
519 | } |
520 | ||
ef3241bb | 521 | static inline void put(std::streambuf &stream, const void *data, size_t size) { |
4d4682ef JF |
522 | _assert(stream.sputn(static_cast<const char *>(data), size) == size); |
523 | } | |
524 | ||
b4b49374 JF |
525 | static inline void put(std::streambuf &stream, const void *data, size_t size, const ldid::Progress &progress) { |
526 | progress(0); | |
e1d26767 JF |
527 | for (size_t total(0); total != size;) { |
528 | auto writ(std::min(size - total, size_t(4096 * 4))); | |
529 | _assert(stream.sputn(static_cast<const char *>(data) + total, writ) == writ); | |
530 | total += writ; | |
b4b49374 | 531 | progress(double(total) / size); |
e1d26767 JF |
532 | } |
533 | } | |
534 | ||
f9cd1d1c JF |
535 | static size_t most(std::streambuf &stream, void *data, size_t size) { |
536 | size_t total(size); | |
537 | while (size > 0) | |
538 | if (auto writ = read(stream, data, size)) | |
539 | size -= writ; | |
540 | else break; | |
541 | return total - size; | |
542 | } | |
543 | ||
ef3241bb | 544 | static inline void pad(std::streambuf &stream, size_t size) { |
4d4682ef JF |
545 | char padding[size]; |
546 | memset(padding, 0, size); | |
547 | put(stream, padding, size); | |
548 | } | |
549 | ||
4374152f JF |
550 | template <typename Type_> |
551 | Type_ Align(Type_ value, size_t align) { | |
552 | value += align - 1; | |
553 | value /= align; | |
554 | value *= align; | |
555 | return value; | |
556 | } | |
557 | ||
b9652d6e JF |
558 | static const uint8_t PageShift_(0x0c); |
559 | static const uint32_t PageSize_(1 << PageShift_); | |
560 | ||
f65c321b | 561 | static inline uint16_t Swap_(uint16_t value) { |
fdb119ef JF |
562 | return |
563 | ((value >> 8) & 0x00ff) | | |
564 | ((value << 8) & 0xff00); | |
565 | } | |
566 | ||
f65c321b | 567 | static inline uint32_t Swap_(uint32_t value) { |
fdb119ef JF |
568 | value = ((value >> 8) & 0x00ff00ff) | |
569 | ((value << 8) & 0xff00ff00); | |
570 | value = ((value >> 16) & 0x0000ffff) | | |
571 | ((value << 16) & 0xffff0000); | |
572 | return value; | |
573 | } | |
574 | ||
f65c321b | 575 | static inline uint64_t Swap_(uint64_t value) { |
4374152f JF |
576 | value = (value & 0x00000000ffffffff) << 32 | (value & 0xffffffff00000000) >> 32; |
577 | value = (value & 0x0000ffff0000ffff) << 16 | (value & 0xffff0000ffff0000) >> 16; | |
578 | value = (value & 0x00ff00ff00ff00ff) << 8 | (value & 0xff00ff00ff00ff00) >> 8; | |
579 | return value; | |
580 | } | |
581 | ||
f65c321b | 582 | static inline int16_t Swap_(int16_t value) { |
fdb119ef JF |
583 | return Swap_(static_cast<uint16_t>(value)); |
584 | } | |
585 | ||
f65c321b | 586 | static inline int32_t Swap_(int32_t value) { |
fdb119ef JF |
587 | return Swap_(static_cast<uint32_t>(value)); |
588 | } | |
589 | ||
f65c321b | 590 | static inline int64_t Swap_(int64_t value) { |
4374152f JF |
591 | return Swap_(static_cast<uint64_t>(value)); |
592 | } | |
593 | ||
f65c321b | 594 | static bool little_(true); |
5525a5a7 | 595 | |
f65c321b | 596 | static inline uint16_t Swap(uint16_t value) { |
5525a5a7 | 597 | return little_ ? Swap_(value) : value; |
fdb119ef JF |
598 | } |
599 | ||
f65c321b | 600 | static inline uint32_t Swap(uint32_t value) { |
5525a5a7 | 601 | return little_ ? Swap_(value) : value; |
fdb119ef JF |
602 | } |
603 | ||
f65c321b | 604 | static inline uint64_t Swap(uint64_t value) { |
4374152f JF |
605 | return little_ ? Swap_(value) : value; |
606 | } | |
607 | ||
f65c321b | 608 | static inline int16_t Swap(int16_t value) { |
fdb119ef JF |
609 | return Swap(static_cast<uint16_t>(value)); |
610 | } | |
611 | ||
f65c321b | 612 | static inline int32_t Swap(int32_t value) { |
fdb119ef JF |
613 | return Swap(static_cast<uint32_t>(value)); |
614 | } | |
615 | ||
f65c321b | 616 | static inline int64_t Swap(int64_t value) { |
4374152f JF |
617 | return Swap(static_cast<uint64_t>(value)); |
618 | } | |
619 | ||
4d4682ef | 620 | class Swapped { |
0a033524 | 621 | protected: |
a362a82f JF |
622 | bool swapped_; |
623 | ||
4d4682ef | 624 | Swapped() : |
0a033524 JF |
625 | swapped_(false) |
626 | { | |
627 | } | |
628 | ||
4d4682ef JF |
629 | public: |
630 | Swapped(bool swapped) : | |
631 | swapped_(swapped) | |
632 | { | |
a362a82f JF |
633 | } |
634 | ||
4d4682ef JF |
635 | template <typename Type_> |
636 | Type_ Swap(Type_ value) const { | |
4374152f JF |
637 | return swapped_ ? Swap_(value) : value; |
638 | } | |
4d4682ef | 639 | }; |
4374152f | 640 | |
4d4682ef JF |
641 | class Data : |
642 | public Swapped | |
643 | { | |
644 | private: | |
645 | void *base_; | |
646 | size_t size_; | |
a362a82f | 647 | |
4d4682ef JF |
648 | public: |
649 | Data(void *base, size_t size) : | |
650 | base_(base), | |
651 | size_(size) | |
652 | { | |
4374152f JF |
653 | } |
654 | ||
0a033524 JF |
655 | void *GetBase() const { |
656 | return base_; | |
657 | } | |
a362a82f | 658 | |
0a033524 JF |
659 | size_t GetSize() const { |
660 | return size_; | |
661 | } | |
662 | }; | |
a362a82f | 663 | |
0a033524 JF |
664 | class MachHeader : |
665 | public Data | |
666 | { | |
667 | private: | |
668 | bool bits64_; | |
669 | ||
670 | struct mach_header *mach_header_; | |
671 | struct load_command *load_command_; | |
672 | ||
673 | public: | |
674 | MachHeader(void *base, size_t size) : | |
675 | Data(base, size) | |
676 | { | |
677 | mach_header_ = (mach_header *) base; | |
a362a82f | 678 | |
3cbc6463 JF |
679 | switch (Swap(mach_header_->magic)) { |
680 | case MH_CIGAM: | |
681 | swapped_ = !swapped_; | |
682 | case MH_MAGIC: | |
683 | bits64_ = false; | |
684 | break; | |
685 | ||
686 | case MH_CIGAM_64: | |
687 | swapped_ = !swapped_; | |
688 | case MH_MAGIC_64: | |
689 | bits64_ = true; | |
690 | break; | |
691 | ||
692 | default: | |
693 | _assert(false); | |
694 | } | |
695 | ||
696 | void *post = mach_header_ + 1; | |
697 | if (bits64_) | |
698 | post = (uint32_t *) post + 1; | |
699 | load_command_ = (struct load_command *) post; | |
a362a82f JF |
700 | |
701 | _assert( | |
702 | Swap(mach_header_->filetype) == MH_EXECUTE || | |
703 | Swap(mach_header_->filetype) == MH_DYLIB || | |
704 | Swap(mach_header_->filetype) == MH_BUNDLE | |
705 | ); | |
706 | } | |
707 | ||
4d4682ef JF |
708 | bool Bits64() const { |
709 | return bits64_; | |
710 | } | |
711 | ||
afbb7c8e JF |
712 | struct mach_header *operator ->() const { |
713 | return mach_header_; | |
714 | } | |
715 | ||
4374152f JF |
716 | operator struct mach_header *() const { |
717 | return mach_header_; | |
718 | } | |
719 | ||
04802ab1 JF |
720 | uint32_t GetCPUType() const { |
721 | return Swap(mach_header_->cputype); | |
722 | } | |
723 | ||
4374152f | 724 | uint32_t GetCPUSubtype() const { |
04802ab1 JF |
725 | return Swap(mach_header_->cpusubtype) & 0xff; |
726 | } | |
727 | ||
4374152f JF |
728 | struct load_command *GetLoadCommand() const { |
729 | return load_command_; | |
730 | } | |
731 | ||
0a033524 | 732 | std::vector<struct load_command *> GetLoadCommands() const { |
a362a82f JF |
733 | std::vector<struct load_command *> load_commands; |
734 | ||
3cbc6463 | 735 | struct load_command *load_command = load_command_; |
a362a82f JF |
736 | for (uint32_t cmd = 0; cmd != Swap(mach_header_->ncmds); ++cmd) { |
737 | load_commands.push_back(load_command); | |
738 | load_command = (struct load_command *) ((uint8_t *) load_command + Swap(load_command->cmdsize)); | |
739 | } | |
740 | ||
741 | return load_commands; | |
742 | } | |
6e83315b | 743 | |
e4d33eec JF |
744 | void ForSection(const ldid::Functor<void (const char *, const char *, void *, size_t)> &code) const { |
745 | _foreach (load_command, GetLoadCommands()) | |
746 | switch (Swap(load_command->cmd)) { | |
747 | case LC_SEGMENT: { | |
748 | auto segment(reinterpret_cast<struct segment_command *>(load_command)); | |
749 | code(segment->segname, NULL, GetOffset<void>(segment->fileoff), segment->filesize); | |
750 | auto section(reinterpret_cast<struct section *>(segment + 1)); | |
751 | for (uint32_t i(0), e(Swap(segment->nsects)); i != e; ++i, ++section) | |
752 | code(segment->segname, section->sectname, GetOffset<void>(segment->fileoff + section->offset), section->size); | |
753 | } break; | |
754 | ||
755 | case LC_SEGMENT_64: { | |
756 | auto segment(reinterpret_cast<struct segment_command_64 *>(load_command)); | |
757 | code(segment->segname, NULL, GetOffset<void>(segment->fileoff), segment->filesize); | |
758 | auto section(reinterpret_cast<struct section_64 *>(segment + 1)); | |
759 | for (uint32_t i(0), e(Swap(segment->nsects)); i != e; ++i, ++section) | |
760 | code(segment->segname, section->sectname, GetOffset<void>(segment->fileoff + section->offset), section->size); | |
761 | } break; | |
762 | } | |
763 | } | |
764 | ||
6e83315b | 765 | template <typename Target_> |
6e1a740e JF |
766 | Target_ *GetOffset(uint32_t offset) const { |
767 | return reinterpret_cast<Target_ *>(offset + (uint8_t *) mach_header_); | |
6e83315b JF |
768 | } |
769 | }; | |
770 | ||
289ccbbc JF |
771 | class FatMachHeader : |
772 | public MachHeader | |
773 | { | |
774 | private: | |
775 | fat_arch *fat_arch_; | |
776 | ||
777 | public: | |
778 | FatMachHeader(void *base, size_t size, fat_arch *fat_arch) : | |
779 | MachHeader(base, size), | |
780 | fat_arch_(fat_arch) | |
781 | { | |
782 | } | |
783 | ||
784 | fat_arch *GetFatArch() const { | |
785 | return fat_arch_; | |
786 | } | |
787 | }; | |
788 | ||
0a033524 JF |
789 | class FatHeader : |
790 | public Data | |
791 | { | |
792 | private: | |
793 | fat_header *fat_header_; | |
289ccbbc | 794 | std::vector<FatMachHeader> mach_headers_; |
0a033524 JF |
795 | |
796 | public: | |
797 | FatHeader(void *base, size_t size) : | |
798 | Data(base, size) | |
799 | { | |
800 | fat_header_ = reinterpret_cast<struct fat_header *>(base); | |
801 | ||
802 | if (Swap(fat_header_->magic) == FAT_CIGAM) { | |
803 | swapped_ = !swapped_; | |
804 | goto fat; | |
805 | } else if (Swap(fat_header_->magic) != FAT_MAGIC) { | |
806 | fat_header_ = NULL; | |
289ccbbc | 807 | mach_headers_.push_back(FatMachHeader(base, size, NULL)); |
0a033524 JF |
808 | } else fat: { |
809 | size_t fat_narch = Swap(fat_header_->nfat_arch); | |
810 | fat_arch *fat_arch = reinterpret_cast<struct fat_arch *>(fat_header_ + 1); | |
811 | size_t arch; | |
812 | for (arch = 0; arch != fat_narch; ++arch) { | |
813 | uint32_t arch_offset = Swap(fat_arch->offset); | |
814 | uint32_t arch_size = Swap(fat_arch->size); | |
289ccbbc | 815 | mach_headers_.push_back(FatMachHeader((uint8_t *) base + arch_offset, arch_size, fat_arch)); |
0a033524 JF |
816 | ++fat_arch; |
817 | } | |
818 | } | |
819 | } | |
820 | ||
289ccbbc | 821 | std::vector<FatMachHeader> &GetMachHeaders() { |
0a033524 JF |
822 | return mach_headers_; |
823 | } | |
20e7eb29 JF |
824 | |
825 | bool IsFat() const { | |
826 | return fat_header_ != NULL; | |
827 | } | |
6b38c173 JF |
828 | |
829 | struct fat_header *operator ->() const { | |
830 | return fat_header_; | |
831 | } | |
4374152f JF |
832 | |
833 | operator struct fat_header *() const { | |
834 | return fat_header_; | |
835 | } | |
0a033524 JF |
836 | }; |
837 | ||
4d4682ef JF |
838 | #define CSMAGIC_REQUIREMENT uint32_t(0xfade0c00) |
839 | #define CSMAGIC_REQUIREMENTS uint32_t(0xfade0c01) | |
840 | #define CSMAGIC_CODEDIRECTORY uint32_t(0xfade0c02) | |
841 | #define CSMAGIC_EMBEDDED_SIGNATURE uint32_t(0xfade0cc0) | |
842 | #define CSMAGIC_EMBEDDED_SIGNATURE_OLD uint32_t(0xfade0b02) | |
843 | #define CSMAGIC_EMBEDDED_ENTITLEMENTS uint32_t(0xfade7171) | |
844 | #define CSMAGIC_DETACHED_SIGNATURE uint32_t(0xfade0cc1) | |
845 | #define CSMAGIC_BLOBWRAPPER uint32_t(0xfade0b01) | |
c05d9758 | 846 | |
4d4682ef JF |
847 | #define CSSLOT_CODEDIRECTORY uint32_t(0x00000) |
848 | #define CSSLOT_INFOSLOT uint32_t(0x00001) | |
849 | #define CSSLOT_REQUIREMENTS uint32_t(0x00002) | |
850 | #define CSSLOT_RESOURCEDIR uint32_t(0x00003) | |
851 | #define CSSLOT_APPLICATION uint32_t(0x00004) | |
852 | #define CSSLOT_ENTITLEMENTS uint32_t(0x00005) | |
f74652ab | 853 | #define CSSLOT_ALTERNATE uint32_t(0x01000) |
4d4682ef JF |
854 | |
855 | #define CSSLOT_SIGNATURESLOT uint32_t(0x10000) | |
856 | ||
f74652ab JF |
857 | #define CS_HASHTYPE_SHA160_160 1 |
858 | #define CS_HASHTYPE_SHA256_256 2 | |
859 | #define CS_HASHTYPE_SHA256_160 3 | |
860 | #define CS_HASHTYPE_SHA386_386 4 | |
fdb119ef JF |
861 | |
862 | struct BlobIndex { | |
863 | uint32_t type; | |
864 | uint32_t offset; | |
c05d9758 | 865 | } _packed; |
fdb119ef | 866 | |
c05d9758 | 867 | struct Blob { |
fdb119ef JF |
868 | uint32_t magic; |
869 | uint32_t length; | |
c05d9758 JF |
870 | } _packed; |
871 | ||
872 | struct SuperBlob { | |
873 | struct Blob blob; | |
fdb119ef JF |
874 | uint32_t count; |
875 | struct BlobIndex index[]; | |
c05d9758 | 876 | } _packed; |
fdb119ef JF |
877 | |
878 | struct CodeDirectory { | |
fdb119ef JF |
879 | uint32_t version; |
880 | uint32_t flags; | |
881 | uint32_t hashOffset; | |
882 | uint32_t identOffset; | |
883 | uint32_t nSpecialSlots; | |
884 | uint32_t nCodeSlots; | |
885 | uint32_t codeLimit; | |
886 | uint8_t hashSize; | |
887 | uint8_t hashType; | |
296fd38e | 888 | uint8_t platform; |
fdb119ef JF |
889 | uint8_t pageSize; |
890 | uint32_t spare2; | |
b73d2333 JF |
891 | uint32_t scatterOffset; |
892 | uint32_t teamIDOffset; | |
71465d5f JF |
893 | //uint32_t spare3; |
894 | //uint64_t codeLimit64; | |
c05d9758 | 895 | } _packed; |
fdb119ef | 896 | |
296fd38e JF |
897 | enum CodeSignatureFlags { |
898 | kSecCodeSignatureHost = 0x0001, | |
899 | kSecCodeSignatureAdhoc = 0x0002, | |
900 | kSecCodeSignatureForceHard = 0x0100, | |
901 | kSecCodeSignatureForceKill = 0x0200, | |
902 | kSecCodeSignatureForceExpiration = 0x0400, | |
903 | kSecCodeSignatureRestrict = 0x0800, | |
904 | kSecCodeSignatureEnforcement = 0x1000, | |
905 | kSecCodeSignatureLibraryValidation = 0x2000, | |
906 | }; | |
907 | ||
7cb8c1fc JF |
908 | enum Kind : uint32_t { |
909 | exprForm = 1, // prefix expr form | |
910 | }; | |
911 | ||
912 | enum ExprOp : uint32_t { | |
913 | opFalse, // unconditionally false | |
914 | opTrue, // unconditionally true | |
915 | opIdent, // match canonical code [string] | |
916 | opAppleAnchor, // signed by Apple as Apple's product | |
917 | opAnchorHash, // match anchor [cert hash] | |
918 | opInfoKeyValue, // *legacy* - use opInfoKeyField [key; value] | |
919 | opAnd, // binary prefix expr AND expr [expr; expr] | |
920 | opOr, // binary prefix expr OR expr [expr; expr] | |
921 | opCDHash, // match hash of CodeDirectory directly [cd hash] | |
922 | opNot, // logical inverse [expr] | |
923 | opInfoKeyField, // Info.plist key field [string; match suffix] | |
924 | opCertField, // Certificate field [cert index; field name; match suffix] | |
925 | opTrustedCert, // require trust settings to approve one particular cert [cert index] | |
926 | opTrustedCerts, // require trust settings to approve the cert chain | |
927 | opCertGeneric, // Certificate component by OID [cert index; oid; match suffix] | |
928 | opAppleGenericAnchor, // signed by Apple in any capacity | |
929 | opEntitlementField, // entitlement dictionary field [string; match suffix] | |
930 | opCertPolicy, // Certificate policy by OID [cert index; oid; match suffix] | |
931 | opNamedAnchor, // named anchor type | |
932 | opNamedCode, // named subroutine | |
933 | opPlatform, // platform constraint [integer] | |
934 | exprOpCount // (total opcode count in use) | |
935 | }; | |
936 | ||
937 | enum MatchOperation { | |
938 | matchExists, // anything but explicit "false" - no value stored | |
939 | matchEqual, // equal (CFEqual) | |
940 | matchContains, // partial match (substring) | |
941 | matchBeginsWith, // partial match (initial substring) | |
942 | matchEndsWith, // partial match (terminal substring) | |
943 | matchLessThan, // less than (string with numeric comparison) | |
944 | matchGreaterThan, // greater than (string with numeric comparison) | |
945 | matchLessEqual, // less or equal (string with numeric comparison) | |
946 | matchGreaterEqual, // greater or equal (string with numeric comparison) | |
947 | }; | |
948 | ||
949 | #define OID_ISO_MEMBER 42 | |
950 | #define OID_US OID_ISO_MEMBER, 134, 72 | |
951 | #define APPLE_OID OID_US, 0x86, 0xf7, 0x63 | |
952 | #define APPLE_ADS_OID APPLE_OID, 0x64 | |
953 | #define APPLE_EXTENSION_OID APPLE_ADS_OID, 6 | |
954 | ||
fdbee693 | 955 | #ifndef LDID_NOFLAGT |
a362a82f | 956 | extern "C" uint32_t hash(uint8_t *k, uint32_t length, uint32_t initval); |
fdbee693 | 957 | #endif |
a362a82f | 958 | |
f74652ab JF |
959 | struct Algorithm { |
960 | size_t size_; | |
961 | uint8_t type_; | |
962 | ||
963 | Algorithm(size_t size, uint8_t type) : | |
964 | size_(size), | |
965 | type_(type) | |
966 | { | |
967 | } | |
968 | ||
969 | virtual const uint8_t *operator [](const ldid::Hash &hash) const = 0; | |
970 | ||
971 | virtual void operator ()(uint8_t *hash, const void *data, size_t size) const = 0; | |
972 | virtual void operator ()(ldid::Hash &hash, const void *data, size_t size) const = 0; | |
973 | virtual void operator ()(std::vector<char> &hash, const void *data, size_t size) const = 0; | |
2da83dea JF |
974 | |
975 | virtual const char *name() = 0; | |
f74652ab JF |
976 | }; |
977 | ||
978 | struct AlgorithmSHA1 : | |
979 | Algorithm | |
980 | { | |
981 | AlgorithmSHA1() : | |
982 | Algorithm(LDID_SHA1_DIGEST_LENGTH, CS_HASHTYPE_SHA160_160) | |
983 | { | |
984 | } | |
985 | ||
986 | virtual const uint8_t *operator [](const ldid::Hash &hash) const { | |
987 | return hash.sha1_; | |
988 | } | |
989 | ||
990 | void operator ()(uint8_t *hash, const void *data, size_t size) const { | |
991 | LDID_SHA1(static_cast<const uint8_t *>(data), size, hash); | |
992 | } | |
993 | ||
994 | void operator ()(ldid::Hash &hash, const void *data, size_t size) const { | |
995 | return operator()(hash.sha1_, data, size); | |
996 | } | |
fdb119ef | 997 | |
f74652ab JF |
998 | void operator ()(std::vector<char> &hash, const void *data, size_t size) const { |
999 | hash.resize(LDID_SHA1_DIGEST_LENGTH); | |
1000 | return operator ()(reinterpret_cast<uint8_t *>(hash.data()), data, size); | |
1001 | } | |
2da83dea JF |
1002 | |
1003 | virtual const char *name() { | |
1004 | return "sha1"; | |
1005 | } | |
f74652ab JF |
1006 | }; |
1007 | ||
1008 | struct AlgorithmSHA256 : | |
1009 | Algorithm | |
1010 | { | |
1011 | AlgorithmSHA256() : | |
1012 | Algorithm(LDID_SHA256_DIGEST_LENGTH, CS_HASHTYPE_SHA256_256) | |
1013 | { | |
1014 | } | |
1015 | ||
1016 | virtual const uint8_t *operator [](const ldid::Hash &hash) const { | |
1017 | return hash.sha256_; | |
1018 | } | |
1019 | ||
1020 | void operator ()(uint8_t *hash, const void *data, size_t size) const { | |
1021 | LDID_SHA256(static_cast<const uint8_t *>(data), size, hash); | |
1022 | } | |
1023 | ||
1024 | void operator ()(ldid::Hash &hash, const void *data, size_t size) const { | |
1025 | return operator()(hash.sha256_, data, size); | |
1026 | } | |
1027 | ||
1028 | void operator ()(std::vector<char> &hash, const void *data, size_t size) const { | |
1029 | hash.resize(LDID_SHA256_DIGEST_LENGTH); | |
1030 | return operator ()(reinterpret_cast<uint8_t *>(hash.data()), data, size); | |
1031 | } | |
2da83dea JF |
1032 | |
1033 | virtual const char *name() { | |
1034 | return "sha256"; | |
1035 | } | |
f74652ab JF |
1036 | }; |
1037 | ||
1038 | static const std::vector<Algorithm *> &GetAlgorithms() { | |
1039 | static AlgorithmSHA1 sha1; | |
1040 | static AlgorithmSHA256 sha256; | |
1041 | ||
1042 | static Algorithm *array[] = { | |
1043 | &sha1, | |
1044 | &sha256, | |
1045 | }; | |
1046 | ||
1047 | static std::vector<Algorithm *> algorithms(array, array + sizeof(array) / sizeof(array[0])); | |
1048 | return algorithms; | |
707027e8 JF |
1049 | } |
1050 | ||
0a033524 | 1051 | struct CodesignAllocation { |
4374152f JF |
1052 | FatMachHeader mach_header_; |
1053 | uint32_t offset_; | |
1054 | uint32_t size_; | |
4d4682ef | 1055 | uint32_t limit_; |
4374152f | 1056 | uint32_t alloc_; |
e4b7adc1 | 1057 | uint32_t align_; |
b4b49374 | 1058 | const char *arch_; |
4374152f | 1059 | |
b4b49374 | 1060 | CodesignAllocation(FatMachHeader mach_header, size_t offset, size_t size, size_t limit, size_t alloc, size_t align, const char *arch) : |
4374152f JF |
1061 | mach_header_(mach_header), |
1062 | offset_(offset), | |
1063 | size_(size), | |
4d4682ef | 1064 | limit_(limit), |
e4b7adc1 | 1065 | alloc_(alloc), |
b4b49374 JF |
1066 | align_(align), |
1067 | arch_(arch) | |
0a033524 JF |
1068 | { |
1069 | } | |
1070 | }; | |
1071 | ||
0326ab11 | 1072 | #ifndef LDID_NOTOOLS |
dede6121 JF |
1073 | class File { |
1074 | private: | |
1075 | int file_; | |
1076 | ||
1077 | public: | |
1078 | File() : | |
1079 | file_(-1) | |
1080 | { | |
1081 | } | |
1082 | ||
1083 | ~File() { | |
1084 | if (file_ != -1) | |
1085 | _syscall(close(file_)); | |
1086 | } | |
1087 | ||
cad40c43 | 1088 | void open(const char *path, int flags) { |
dede6121 | 1089 | _assert(file_ == -1); |
fc7a76da | 1090 | file_ = _syscall(::open(path, flags)); |
dede6121 JF |
1091 | } |
1092 | ||
1093 | int file() const { | |
1094 | return file_; | |
1095 | } | |
1096 | }; | |
1097 | ||
1098 | class Map { | |
1099 | private: | |
1100 | File file_; | |
1101 | void *data_; | |
1102 | size_t size_; | |
1103 | ||
1104 | void clear() { | |
1105 | if (data_ == NULL) | |
1106 | return; | |
1107 | _syscall(munmap(data_, size_)); | |
1108 | data_ = NULL; | |
1109 | size_ = 0; | |
1110 | } | |
1111 | ||
1112 | public: | |
1113 | Map() : | |
1114 | data_(NULL), | |
1115 | size_(0) | |
1116 | { | |
1117 | } | |
1118 | ||
d4187e53 | 1119 | Map(const std::string &path, int oflag, int pflag, int mflag) : |
cad40c43 JF |
1120 | Map() |
1121 | { | |
1122 | open(path, oflag, pflag, mflag); | |
1123 | } | |
1124 | ||
d4187e53 | 1125 | Map(const std::string &path, bool edit) : |
cad40c43 JF |
1126 | Map() |
1127 | { | |
1128 | open(path, edit); | |
dede6121 JF |
1129 | } |
1130 | ||
1131 | ~Map() { | |
1132 | clear(); | |
1133 | } | |
1134 | ||
fac3d3e3 JF |
1135 | bool empty() const { |
1136 | return data_ == NULL; | |
1137 | } | |
1138 | ||
d4187e53 | 1139 | void open(const std::string &path, int oflag, int pflag, int mflag) { |
dede6121 JF |
1140 | clear(); |
1141 | ||
d4187e53 | 1142 | file_.open(path.c_str(), oflag); |
dede6121 JF |
1143 | int file(file_.file()); |
1144 | ||
1145 | struct stat stat; | |
1146 | _syscall(fstat(file, &stat)); | |
1147 | size_ = stat.st_size; | |
1148 | ||
fc7a76da | 1149 | data_ = _syscall(mmap(NULL, size_, pflag, mflag, file, 0)); |
cad40c43 JF |
1150 | } |
1151 | ||
d4187e53 | 1152 | void open(const std::string &path, bool edit) { |
cad40c43 JF |
1153 | if (edit) |
1154 | open(path, O_RDWR, PROT_READ | PROT_WRITE, MAP_SHARED); | |
1155 | else | |
1156 | open(path, O_RDONLY, PROT_READ, MAP_PRIVATE); | |
dede6121 JF |
1157 | } |
1158 | ||
1159 | void *data() const { | |
1160 | return data_; | |
1161 | } | |
1162 | ||
1163 | size_t size() const { | |
1164 | return size_; | |
1165 | } | |
f4b2df35 JF |
1166 | |
1167 | operator std::string() const { | |
1168 | return std::string(static_cast<char *>(data_), size_); | |
1169 | } | |
dede6121 | 1170 | }; |
0326ab11 | 1171 | #endif |
dede6121 | 1172 | |
a0c715e9 JF |
1173 | namespace ldid { |
1174 | ||
cdd9c0fd JF |
1175 | std::string Analyze(const void *data, size_t size) { |
1176 | std::string entitlements; | |
1177 | ||
1178 | FatHeader fat_header(const_cast<void *>(data), size); | |
1179 | _foreach (mach_header, fat_header.GetMachHeaders()) | |
1180 | _foreach (load_command, mach_header.GetLoadCommands()) | |
1181 | if (mach_header.Swap(load_command->cmd) == LC_CODE_SIGNATURE) { | |
1182 | auto signature(reinterpret_cast<struct linkedit_data_command *>(load_command)); | |
1183 | auto offset(mach_header.Swap(signature->dataoff)); | |
1184 | auto pointer(reinterpret_cast<uint8_t *>(mach_header.GetBase()) + offset); | |
1185 | auto super(reinterpret_cast<struct SuperBlob *>(pointer)); | |
1186 | ||
1187 | for (size_t index(0); index != Swap(super->count); ++index) | |
1188 | if (Swap(super->index[index].type) == CSSLOT_ENTITLEMENTS) { | |
1189 | auto begin(Swap(super->index[index].offset)); | |
1190 | auto blob(reinterpret_cast<struct Blob *>(pointer + begin)); | |
1191 | auto writ(Swap(blob->length) - sizeof(*blob)); | |
1192 | ||
1193 | if (entitlements.empty()) | |
1194 | entitlements.assign(reinterpret_cast<char *>(blob + 1), writ); | |
1195 | else | |
1196 | _assert(entitlements.compare(0, entitlements.size(), reinterpret_cast<char *>(blob + 1), writ) == 0); | |
1197 | } | |
1198 | } | |
1199 | ||
1200 | return entitlements; | |
1201 | } | |
1202 | ||
b4b49374 | 1203 | static void Allocate(const void *idata, size_t isize, std::streambuf &output, const Functor<size_t (const MachHeader &, size_t)> &allocate, const Functor<size_t (const MachHeader &, std::streambuf &output, size_t, const std::string &, const char *, const Progress &)> &save, const Progress &progress) { |
ffdd1183 | 1204 | FatHeader source(const_cast<void *>(idata), isize); |
f4b2df35 JF |
1205 | |
1206 | size_t offset(0); | |
1207 | if (source.IsFat()) | |
1208 | offset += sizeof(fat_header) + sizeof(fat_arch) * source.Swap(source->nfat_arch); | |
1209 | ||
1210 | std::vector<CodesignAllocation> allocations; | |
1211 | _foreach (mach_header, source.GetMachHeaders()) { | |
1212 | struct linkedit_data_command *signature(NULL); | |
1213 | struct symtab_command *symtab(NULL); | |
1214 | ||
1215 | _foreach (load_command, mach_header.GetLoadCommands()) { | |
1216 | uint32_t cmd(mach_header.Swap(load_command->cmd)); | |
1217 | if (false); | |
1218 | else if (cmd == LC_CODE_SIGNATURE) | |
1219 | signature = reinterpret_cast<struct linkedit_data_command *>(load_command); | |
1220 | else if (cmd == LC_SYMTAB) | |
1221 | symtab = reinterpret_cast<struct symtab_command *>(load_command); | |
1222 | } | |
1223 | ||
1224 | size_t size; | |
1225 | if (signature == NULL) | |
1226 | size = mach_header.GetSize(); | |
1227 | else { | |
1228 | size = mach_header.Swap(signature->dataoff); | |
1229 | _assert(size <= mach_header.GetSize()); | |
1230 | } | |
1231 | ||
1232 | if (symtab != NULL) { | |
1233 | auto end(mach_header.Swap(symtab->stroff) + mach_header.Swap(symtab->strsize)); | |
422bad37 JF |
1234 | if (symtab->stroff != 0 || symtab->strsize != 0) { |
1235 | _assert(end <= size); | |
1236 | _assert(end >= size - 0x10); | |
1237 | size = end; | |
1238 | } | |
f4b2df35 JF |
1239 | } |
1240 | ||
e4d33eec | 1241 | size_t alloc(allocate(mach_header, size)); |
f4b2df35 JF |
1242 | |
1243 | auto *fat_arch(mach_header.GetFatArch()); | |
6f420711 JF |
1244 | uint32_t align; |
1245 | ||
1246 | if (fat_arch != NULL) | |
1247 | align = source.Swap(fat_arch->align); | |
1248 | else switch (mach_header.GetCPUType()) { | |
1249 | case CPU_TYPE_POWERPC: | |
1250 | case CPU_TYPE_POWERPC64: | |
1251 | case CPU_TYPE_X86: | |
1252 | case CPU_TYPE_X86_64: | |
1253 | align = 0xc; | |
1254 | break; | |
1255 | case CPU_TYPE_ARM: | |
1256 | case CPU_TYPE_ARM64: | |
1257 | align = 0xe; | |
1258 | break; | |
1259 | default: | |
1260 | align = 0x0; | |
1261 | break; | |
1262 | } | |
1263 | ||
b4b49374 JF |
1264 | const char *arch(NULL); |
1265 | switch (mach_header.GetCPUType()) { | |
1266 | case CPU_TYPE_POWERPC: | |
1267 | arch = "ppc"; | |
1268 | break; | |
1269 | case CPU_TYPE_POWERPC64: | |
1270 | arch = "ppc64"; | |
1271 | break; | |
1272 | case CPU_TYPE_X86: | |
1273 | arch = "i386"; | |
1274 | break; | |
1275 | case CPU_TYPE_X86_64: | |
1276 | arch = "x86_64"; | |
1277 | break; | |
1278 | case CPU_TYPE_ARM: | |
1279 | arch = "arm"; | |
1280 | break; | |
1281 | case CPU_TYPE_ARM64: | |
1282 | arch = "arm64"; | |
1283 | break; | |
1284 | } | |
1285 | ||
f4b2df35 JF |
1286 | offset = Align(offset, 1 << align); |
1287 | ||
4d4682ef JF |
1288 | uint32_t limit(size); |
1289 | if (alloc != 0) | |
1290 | limit = Align(limit, 0x10); | |
1291 | ||
b4b49374 | 1292 | allocations.push_back(CodesignAllocation(mach_header, offset, size, limit, alloc, align, arch)); |
f4b2df35 | 1293 | offset += size + alloc; |
b9652d6e | 1294 | offset = Align(offset, 0x10); |
f4b2df35 JF |
1295 | } |
1296 | ||
4d4682ef JF |
1297 | size_t position(0); |
1298 | ||
1299 | if (source.IsFat()) { | |
1300 | fat_header fat_header; | |
1301 | fat_header.magic = Swap(FAT_MAGIC); | |
1302 | fat_header.nfat_arch = Swap(uint32_t(allocations.size())); | |
1303 | put(output, &fat_header, sizeof(fat_header)); | |
1304 | position += sizeof(fat_header); | |
1305 | ||
1306 | _foreach (allocation, allocations) { | |
1307 | auto &mach_header(allocation.mach_header_); | |
1308 | ||
1309 | fat_arch fat_arch; | |
1310 | fat_arch.cputype = Swap(mach_header->cputype); | |
1311 | fat_arch.cpusubtype = Swap(mach_header->cpusubtype); | |
1312 | fat_arch.offset = Swap(allocation.offset_); | |
1313 | fat_arch.size = Swap(allocation.limit_ + allocation.alloc_); | |
1314 | fat_arch.align = Swap(allocation.align_); | |
1315 | put(output, &fat_arch, sizeof(fat_arch)); | |
1316 | position += sizeof(fat_arch); | |
1317 | } | |
f4b2df35 JF |
1318 | } |
1319 | ||
1320 | _foreach (allocation, allocations) { | |
b4b49374 | 1321 | progress(allocation.arch_); |
4d4682ef | 1322 | auto &mach_header(allocation.mach_header_); |
f4b2df35 | 1323 | |
4d4682ef JF |
1324 | pad(output, allocation.offset_ - position); |
1325 | position = allocation.offset_; | |
f4b2df35 | 1326 | |
4d4682ef | 1327 | std::vector<std::string> commands; |
f4b2df35 | 1328 | |
4d4682ef JF |
1329 | _foreach (load_command, mach_header.GetLoadCommands()) { |
1330 | std::string copy(reinterpret_cast<const char *>(load_command), load_command->cmdsize); | |
f4b2df35 | 1331 | |
a7f01a65 | 1332 | switch (mach_header.Swap(load_command->cmd)) { |
4d4682ef JF |
1333 | case LC_CODE_SIGNATURE: |
1334 | continue; | |
1335 | break; | |
1336 | ||
1337 | case LC_SEGMENT: { | |
1338 | auto segment_command(reinterpret_cast<struct segment_command *>(©[0])); | |
1339 | if (strncmp(segment_command->segname, "__LINKEDIT", 16) != 0) | |
1340 | break; | |
1341 | size_t size(mach_header.Swap(allocation.limit_ + allocation.alloc_ - mach_header.Swap(segment_command->fileoff))); | |
1342 | segment_command->filesize = size; | |
6f420711 | 1343 | segment_command->vmsize = Align(size, 1 << allocation.align_); |
4d4682ef JF |
1344 | } break; |
1345 | ||
1346 | case LC_SEGMENT_64: { | |
1347 | auto segment_command(reinterpret_cast<struct segment_command_64 *>(©[0])); | |
1348 | if (strncmp(segment_command->segname, "__LINKEDIT", 16) != 0) | |
1349 | break; | |
1350 | size_t size(mach_header.Swap(allocation.limit_ + allocation.alloc_ - mach_header.Swap(segment_command->fileoff))); | |
1351 | segment_command->filesize = size; | |
6f420711 | 1352 | segment_command->vmsize = Align(size, 1 << allocation.align_); |
4d4682ef JF |
1353 | } break; |
1354 | } | |
f4b2df35 | 1355 | |
4d4682ef | 1356 | commands.push_back(copy); |
f4b2df35 JF |
1357 | } |
1358 | ||
e0098838 | 1359 | if (allocation.alloc_ != 0) { |
4d4682ef JF |
1360 | linkedit_data_command signature; |
1361 | signature.cmd = mach_header.Swap(LC_CODE_SIGNATURE); | |
1362 | signature.cmdsize = mach_header.Swap(uint32_t(sizeof(signature))); | |
1363 | signature.dataoff = mach_header.Swap(allocation.limit_); | |
1364 | signature.datasize = mach_header.Swap(allocation.alloc_); | |
1365 | commands.push_back(std::string(reinterpret_cast<const char *>(&signature), sizeof(signature))); | |
1366 | } | |
f4b2df35 | 1367 | |
4d4682ef | 1368 | size_t begin(position); |
f4b2df35 | 1369 | |
4d4682ef JF |
1370 | uint32_t after(0); |
1371 | _foreach(command, commands) | |
1372 | after += command.size(); | |
f4b2df35 | 1373 | |
4d4682ef | 1374 | std::stringbuf altern; |
f4b2df35 | 1375 | |
4d4682ef JF |
1376 | struct mach_header header(*mach_header); |
1377 | header.ncmds = mach_header.Swap(uint32_t(commands.size())); | |
1378 | header.sizeofcmds = mach_header.Swap(after); | |
1379 | put(output, &header, sizeof(header)); | |
1380 | put(altern, &header, sizeof(header)); | |
1381 | position += sizeof(header); | |
f4b2df35 | 1382 | |
4d4682ef JF |
1383 | if (mach_header.Bits64()) { |
1384 | auto pad(mach_header.Swap(uint32_t(0))); | |
1385 | put(output, &pad, sizeof(pad)); | |
1386 | put(altern, &pad, sizeof(pad)); | |
1387 | position += sizeof(pad); | |
1388 | } | |
f4b2df35 | 1389 | |
4d4682ef JF |
1390 | _foreach(command, commands) { |
1391 | put(output, command.data(), command.size()); | |
1392 | put(altern, command.data(), command.size()); | |
1393 | position += command.size(); | |
1394 | } | |
f4b2df35 | 1395 | |
4d4682ef JF |
1396 | uint32_t before(mach_header.Swap(mach_header->sizeofcmds)); |
1397 | if (before > after) { | |
1398 | pad(output, before - after); | |
1399 | pad(altern, before - after); | |
1400 | position += before - after; | |
1401 | } | |
f4b2df35 | 1402 | |
4d4682ef | 1403 | auto top(reinterpret_cast<char *>(mach_header.GetBase())); |
f4b2df35 | 1404 | |
4d4682ef JF |
1405 | std::string overlap(altern.str()); |
1406 | overlap.append(top + overlap.size(), Align(overlap.size(), 0x1000) - overlap.size()); | |
f4b2df35 | 1407 | |
b4b49374 | 1408 | put(output, top + (position - begin), allocation.size_ - (position - begin), progress); |
4d4682ef | 1409 | position = begin + allocation.size_; |
f4b2df35 | 1410 | |
4d4682ef JF |
1411 | pad(output, allocation.limit_ - allocation.size_); |
1412 | position += allocation.limit_ - allocation.size_; | |
f4b2df35 | 1413 | |
b4b49374 | 1414 | size_t saved(save(mach_header, output, allocation.limit_, overlap, top, progress)); |
67205335 JF |
1415 | if (allocation.alloc_ > saved) |
1416 | pad(output, allocation.alloc_ - saved); | |
4f63b590 JF |
1417 | else |
1418 | _assert(allocation.alloc_ == saved); | |
67205335 | 1419 | position += allocation.alloc_; |
e0098838 JF |
1420 | } |
1421 | } | |
f4b2df35 | 1422 | |
a0c715e9 JF |
1423 | } |
1424 | ||
efad8a44 JF |
1425 | typedef std::map<uint32_t, std::string> Blobs; |
1426 | ||
bb591e2b JF |
1427 | static void insert(Blobs &blobs, uint32_t slot, const std::stringbuf &buffer) { |
1428 | auto value(buffer.str()); | |
1429 | std::swap(blobs[slot], value); | |
1430 | } | |
1431 | ||
4ebf3494 | 1432 | static const std::string &insert(Blobs &blobs, uint32_t slot, uint32_t magic, const std::stringbuf &buffer) { |
8f310b91 JF |
1433 | auto value(buffer.str()); |
1434 | Blob blob; | |
1435 | blob.magic = Swap(magic); | |
1436 | blob.length = Swap(uint32_t(sizeof(blob) + value.size())); | |
1437 | value.insert(0, reinterpret_cast<char *>(&blob), sizeof(blob)); | |
4ebf3494 JF |
1438 | auto &save(blobs[slot]); |
1439 | std::swap(save, value); | |
1440 | return save; | |
efad8a44 JF |
1441 | } |
1442 | ||
1443 | static size_t put(std::streambuf &output, uint32_t magic, const Blobs &blobs) { | |
1444 | size_t total(0); | |
1445 | _foreach (blob, blobs) | |
1446 | total += blob.second.size(); | |
1447 | ||
1448 | struct SuperBlob super; | |
1449 | super.blob.magic = Swap(magic); | |
1450 | super.blob.length = Swap(uint32_t(sizeof(SuperBlob) + blobs.size() * sizeof(BlobIndex) + total)); | |
1451 | super.count = Swap(uint32_t(blobs.size())); | |
1452 | put(output, &super, sizeof(super)); | |
1453 | ||
1454 | size_t offset(sizeof(SuperBlob) + sizeof(BlobIndex) * blobs.size()); | |
1455 | ||
1456 | _foreach (blob, blobs) { | |
1457 | BlobIndex index; | |
1458 | index.type = Swap(blob.first); | |
1459 | index.offset = Swap(uint32_t(offset)); | |
1460 | put(output, &index, sizeof(index)); | |
1461 | offset += blob.second.size(); | |
1462 | } | |
1463 | ||
1464 | _foreach (blob, blobs) | |
1465 | put(output, blob.second.data(), blob.second.size()); | |
1466 | ||
1467 | return offset; | |
1468 | } | |
1469 | ||
489c97b8 | 1470 | #ifndef LDID_NOSMIME |
30c64adb JF |
1471 | class Buffer { |
1472 | private: | |
1473 | BIO *bio_; | |
1474 | ||
1475 | public: | |
1476 | Buffer(BIO *bio) : | |
1477 | bio_(bio) | |
1478 | { | |
1479 | _assert(bio_ != NULL); | |
1480 | } | |
1481 | ||
1482 | Buffer() : | |
1483 | bio_(BIO_new(BIO_s_mem())) | |
1484 | { | |
1485 | } | |
1486 | ||
1487 | Buffer(const char *data, size_t size) : | |
1488 | Buffer(BIO_new_mem_buf(const_cast<char *>(data), size)) | |
1489 | { | |
1490 | } | |
1491 | ||
1492 | Buffer(const std::string &data) : | |
1493 | Buffer(data.data(), data.size()) | |
1494 | { | |
1495 | } | |
1496 | ||
1497 | Buffer(PKCS7 *pkcs) : | |
1498 | Buffer() | |
1499 | { | |
1500 | _assert(i2d_PKCS7_bio(bio_, pkcs) != 0); | |
1501 | } | |
1502 | ||
1503 | ~Buffer() { | |
1504 | BIO_free_all(bio_); | |
1505 | } | |
1506 | ||
1507 | operator BIO *() const { | |
1508 | return bio_; | |
1509 | } | |
1510 | ||
1511 | explicit operator std::string() const { | |
1512 | char *data; | |
1513 | auto size(BIO_get_mem_data(bio_, &data)); | |
1514 | return std::string(data, size); | |
1515 | } | |
1516 | }; | |
1517 | ||
1518 | class Stuff { | |
1519 | private: | |
1520 | PKCS12 *value_; | |
1521 | EVP_PKEY *key_; | |
1522 | X509 *cert_; | |
1523 | STACK_OF(X509) *ca_; | |
1524 | ||
1525 | public: | |
1526 | Stuff(BIO *bio) : | |
1527 | value_(d2i_PKCS12_bio(bio, NULL)), | |
1528 | ca_(NULL) | |
1529 | { | |
1530 | _assert(value_ != NULL); | |
1531 | _assert(PKCS12_parse(value_, "", &key_, &cert_, &ca_) != 0); | |
f1df4833 | 1532 | |
30c64adb JF |
1533 | _assert(key_ != NULL); |
1534 | _assert(cert_ != NULL); | |
f1df4833 JF |
1535 | |
1536 | if (ca_ == NULL) | |
1537 | ca_ = sk_X509_new_null(); | |
1538 | _assert(ca_ != NULL); | |
30c64adb JF |
1539 | } |
1540 | ||
1541 | Stuff(const std::string &data) : | |
1542 | Stuff(Buffer(data)) | |
1543 | { | |
1544 | } | |
1545 | ||
1546 | ~Stuff() { | |
1547 | sk_X509_pop_free(ca_, X509_free); | |
1548 | X509_free(cert_); | |
1549 | EVP_PKEY_free(key_); | |
1550 | PKCS12_free(value_); | |
1551 | } | |
1552 | ||
1553 | operator PKCS12 *() const { | |
1554 | return value_; | |
1555 | } | |
1556 | ||
1557 | operator EVP_PKEY *() const { | |
1558 | return key_; | |
1559 | } | |
1560 | ||
1561 | operator X509 *() const { | |
1562 | return cert_; | |
1563 | } | |
1564 | ||
1565 | operator STACK_OF(X509) *() const { | |
1566 | return ca_; | |
1567 | } | |
1568 | }; | |
1569 | ||
1570 | class Signature { | |
1571 | private: | |
1572 | PKCS7 *value_; | |
1573 | ||
1574 | public: | |
fe54c5fb JF |
1575 | Signature(const Stuff &stuff, const Buffer &data, const std::string &xml) { |
1576 | value_ = PKCS7_new(); | |
30c64adb | 1577 | _assert(value_ != NULL); |
fe54c5fb JF |
1578 | |
1579 | _assert(PKCS7_set_type(value_, NID_pkcs7_signed)); | |
1580 | _assert(PKCS7_content_new(value_, NID_pkcs7_data)); | |
1581 | ||
1582 | STACK_OF(X509) *certs(stuff); | |
1583 | for (unsigned i(0), e(sk_X509_num(certs)); i != e; i++) | |
1584 | _assert(PKCS7_add_certificate(value_, sk_X509_value(certs, e - i - 1))); | |
1585 | ||
7818dc9c JF |
1586 | // XXX: this is the same as PKCS7_sign_add_signer(value_, stuff, stuff, NULL, PKCS7_NOSMIMECAP) |
1587 | _assert(X509_check_private_key(stuff, stuff)); | |
1588 | auto info(PKCS7_add_signature(value_, stuff, stuff, EVP_sha1())); | |
fe54c5fb | 1589 | _assert(info != NULL); |
7818dc9c JF |
1590 | _assert(PKCS7_add_certificate(value_, stuff)); |
1591 | _assert(PKCS7_add_signed_attribute(info, NID_pkcs9_contentType, V_ASN1_OBJECT, OBJ_nid2obj(NID_pkcs7_data))); | |
fe54c5fb JF |
1592 | |
1593 | PKCS7_set_detached(value_, 1); | |
1594 | ||
1595 | ASN1_OCTET_STRING *string(ASN1_OCTET_STRING_new()); | |
1596 | _assert(string != NULL); | |
1597 | try { | |
1598 | _assert(ASN1_STRING_set(string, xml.data(), xml.size())); | |
1599 | ||
1600 | static auto nid(OBJ_create("1.2.840.113635.100.9.1", "", "")); | |
1601 | _assert(PKCS7_add_signed_attribute(info, nid, V_ASN1_OCTET_STRING, string)); | |
1602 | } catch (...) { | |
1603 | ASN1_OCTET_STRING_free(string); | |
1604 | throw; | |
1605 | } | |
1606 | ||
7818dc9c JF |
1607 | // XXX: this is the same as PKCS7_final(value_, data, PKCS7_BINARY) |
1608 | BIO *bio(PKCS7_dataInit(value_, NULL)); | |
1609 | _assert(bio != NULL); | |
1610 | _scope({ BIO_free_all(bio); }); | |
1611 | SMIME_crlf_copy(data, bio, PKCS7_BINARY); | |
1612 | BIO_flush(bio); | |
1613 | _assert(PKCS7_dataFinal(value_, bio)); | |
30c64adb JF |
1614 | } |
1615 | ||
1616 | ~Signature() { | |
1617 | PKCS7_free(value_); | |
1618 | } | |
1619 | ||
1620 | operator PKCS7 *() const { | |
1621 | return value_; | |
1622 | } | |
1623 | }; | |
489c97b8 | 1624 | #endif |
30c64adb | 1625 | |
23c11ee8 JF |
1626 | class NullBuffer : |
1627 | public std::streambuf | |
1628 | { | |
1629 | public: | |
1630 | virtual std::streamsize xsputn(const char_type *data, std::streamsize size) { | |
1631 | return size; | |
1632 | } | |
1633 | ||
1634 | virtual int_type overflow(int_type next) { | |
1635 | return next; | |
1636 | } | |
1637 | }; | |
1638 | ||
15764b0b JF |
1639 | class Digest { |
1640 | public: | |
1641 | uint8_t sha1_[LDID_SHA1_DIGEST_LENGTH]; | |
1642 | }; | |
1643 | ||
23c11ee8 JF |
1644 | class HashBuffer : |
1645 | public std::streambuf | |
1646 | { | |
1647 | private: | |
f74652ab | 1648 | ldid::Hash &hash_; |
9ec8439b JF |
1649 | |
1650 | LDID_SHA1_CTX sha1_; | |
1651 | LDID_SHA256_CTX sha256_; | |
23c11ee8 JF |
1652 | |
1653 | public: | |
f74652ab | 1654 | HashBuffer(ldid::Hash &hash) : |
23c11ee8 JF |
1655 | hash_(hash) |
1656 | { | |
9ec8439b JF |
1657 | LDID_SHA1_Init(&sha1_); |
1658 | LDID_SHA256_Init(&sha256_); | |
23c11ee8 JF |
1659 | } |
1660 | ||
1661 | ~HashBuffer() { | |
9ec8439b JF |
1662 | LDID_SHA1_Final(reinterpret_cast<uint8_t *>(hash_.sha1_), &sha1_); |
1663 | LDID_SHA256_Final(reinterpret_cast<uint8_t *>(hash_.sha256_), &sha256_); | |
23c11ee8 JF |
1664 | } |
1665 | ||
1666 | virtual std::streamsize xsputn(const char_type *data, std::streamsize size) { | |
9ec8439b JF |
1667 | LDID_SHA1_Update(&sha1_, data, size); |
1668 | LDID_SHA256_Update(&sha256_, data, size); | |
23c11ee8 JF |
1669 | return size; |
1670 | } | |
1671 | ||
1672 | virtual int_type overflow(int_type next) { | |
1673 | if (next == traits_type::eof()) | |
1674 | return sync(); | |
1675 | char value(next); | |
1676 | xsputn(&value, 1); | |
1677 | return next; | |
1678 | } | |
1679 | }; | |
1680 | ||
1681 | class HashProxy : | |
1682 | public HashBuffer | |
1683 | { | |
1684 | private: | |
1685 | std::streambuf &buffer_; | |
1686 | ||
1687 | public: | |
f74652ab | 1688 | HashProxy(ldid::Hash &hash, std::streambuf &buffer) : |
23c11ee8 JF |
1689 | HashBuffer(hash), |
1690 | buffer_(buffer) | |
1691 | { | |
1692 | } | |
1693 | ||
1694 | virtual std::streamsize xsputn(const char_type *data, std::streamsize size) { | |
1695 | _assert(HashBuffer::xsputn(data, size) == size); | |
1696 | return buffer_.sputn(data, size); | |
1697 | } | |
1698 | }; | |
1699 | ||
0326ab11 | 1700 | #ifndef LDID_NOTOOLS |
23c11ee8 JF |
1701 | static bool Starts(const std::string &lhs, const std::string &rhs) { |
1702 | return lhs.size() >= rhs.size() && lhs.compare(0, rhs.size(), rhs) == 0; | |
1703 | } | |
1704 | ||
1705 | class Split { | |
1706 | public: | |
1707 | std::string dir; | |
1708 | std::string base; | |
1709 | ||
1710 | Split(const std::string &path) { | |
1711 | size_t slash(path.rfind('/')); | |
1712 | if (slash == std::string::npos) | |
1713 | base = path; | |
1714 | else { | |
1715 | dir = path.substr(0, slash + 1); | |
1716 | base = path.substr(slash + 1); | |
1717 | } | |
1718 | } | |
1719 | }; | |
1720 | ||
1721 | static void mkdir_p(const std::string &path) { | |
1722 | if (path.empty()) | |
1723 | return; | |
31cc0388 JF |
1724 | #ifdef __WIN32__ |
1725 | if (_syscall(mkdir(path.c_str()), EEXIST) == -EEXIST) | |
1726 | return; | |
1727 | #else | |
23c11ee8 JF |
1728 | if (_syscall(mkdir(path.c_str(), 0755), EEXIST) == -EEXIST) |
1729 | return; | |
31cc0388 | 1730 | #endif |
23c11ee8 JF |
1731 | auto slash(path.rfind('/', path.size() - 1)); |
1732 | if (slash == std::string::npos) | |
1733 | return; | |
1734 | mkdir_p(path.substr(0, slash)); | |
1735 | } | |
1736 | ||
1737 | static std::string Temporary(std::filebuf &file, const Split &split) { | |
1738 | std::string temp(split.dir + ".ldid." + split.base); | |
1739 | mkdir_p(split.dir); | |
1740 | _assert_(file.open(temp.c_str(), std::ios::out | std::ios::trunc | std::ios::binary) == &file, "open(): %s", temp.c_str()); | |
1741 | return temp; | |
1742 | } | |
1743 | ||
d4187e53 JF |
1744 | static void Commit(const std::string &path, const std::string &temp) { |
1745 | struct stat info; | |
23c11ee8 | 1746 | if (_syscall(stat(path.c_str(), &info), ENOENT) == 0) { |
d4187e53 | 1747 | #ifndef __WIN32__ |
23c11ee8 | 1748 | _syscall(chown(temp.c_str(), info.st_uid, info.st_gid)); |
d4187e53 | 1749 | #endif |
23c11ee8 JF |
1750 | _syscall(chmod(temp.c_str(), info.st_mode)); |
1751 | } | |
1752 | ||
d4187e53 JF |
1753 | _syscall(rename(temp.c_str(), path.c_str())); |
1754 | } | |
0326ab11 | 1755 | #endif |
d4187e53 | 1756 | |
a0c715e9 | 1757 | namespace ldid { |
e57b1f91 | 1758 | |
c1f70967 | 1759 | #ifndef LDID_NOSMIME |
7cb8c1fc JF |
1760 | static void get(std::string &value, X509_NAME *name, int nid) { |
1761 | auto index(X509_NAME_get_index_by_NID(name, nid, -1)); | |
1762 | _assert(index >= 0); | |
1763 | auto next(X509_NAME_get_index_by_NID(name, nid, index)); | |
1764 | _assert(next == -1); | |
1765 | auto entry(X509_NAME_get_entry(name, index)); | |
1766 | _assert(entry != NULL); | |
1767 | auto asn(X509_NAME_ENTRY_get_data(entry)); | |
1768 | _assert(asn != NULL); | |
1769 | value.assign(reinterpret_cast<char *>(ASN1_STRING_data(asn)), ASN1_STRING_length(asn)); | |
1770 | } | |
c1f70967 | 1771 | #endif |
7cb8c1fc JF |
1772 | |
1773 | static void req(std::streambuf &buffer, uint32_t value) { | |
1774 | value = Swap(value); | |
1775 | put(buffer, &value, sizeof(value)); | |
1776 | } | |
1777 | ||
1778 | static void req(std::streambuf &buffer, const std::string &value) { | |
1779 | req(buffer, value.size()); | |
1780 | put(buffer, value.data(), value.size()); | |
1781 | static uint8_t zeros[] = {0,0,0,0}; | |
1782 | put(buffer, zeros, 3 - (value.size() + 3) % 4); | |
1783 | } | |
1784 | ||
1785 | template <size_t Size_> | |
1786 | static void req(std::streambuf &buffer, uint8_t (&&data)[Size_]) { | |
1787 | req(buffer, Size_); | |
1788 | put(buffer, data, Size_); | |
1789 | static uint8_t zeros[] = {0,0,0,0}; | |
1790 | put(buffer, zeros, 3 - (Size_ + 3) % 4); | |
1791 | } | |
1792 | ||
296fd38e | 1793 | Hash Sign(const void *idata, size_t isize, std::streambuf &output, const std::string &identifier, const std::string &entitlements, const std::string &requirements, const std::string &key, const Slots &slots, uint32_t flags, bool platform, const Progress &progress) { |
f74652ab | 1794 | Hash hash; |
4ebf3494 | 1795 | |
7cb8c1fc | 1796 | |
b73d2333 | 1797 | std::string team; |
7cb8c1fc | 1798 | std::string common; |
b73d2333 JF |
1799 | |
1800 | #ifndef LDID_NOSMIME | |
1801 | if (!key.empty()) { | |
b73d2333 JF |
1802 | Stuff stuff(key); |
1803 | auto name(X509_get_subject_name(stuff)); | |
1804 | _assert(name != NULL); | |
7cb8c1fc JF |
1805 | get(team, name, NID_organizationalUnitName); |
1806 | get(common, name, NID_commonName); | |
b73d2333 JF |
1807 | } |
1808 | #endif | |
1809 | ||
7cb8c1fc JF |
1810 | |
1811 | std::stringbuf backing; | |
1812 | ||
1813 | if (!requirements.empty()) { | |
1814 | put(backing, requirements.data(), requirements.size()); | |
1815 | } else { | |
1816 | Blobs blobs; | |
1817 | ||
1818 | std::stringbuf requirement; | |
1819 | req(requirement, exprForm); | |
1820 | req(requirement, opAnd); | |
1821 | req(requirement, opIdent); | |
1822 | req(requirement, identifier); | |
1823 | req(requirement, opAnd); | |
1824 | req(requirement, opAppleGenericAnchor); | |
1825 | req(requirement, opAnd); | |
1826 | req(requirement, opCertField); | |
1827 | req(requirement, 0); | |
1828 | req(requirement, "subject.CN"); | |
1829 | req(requirement, matchEqual); | |
1830 | req(requirement, common); | |
1831 | req(requirement, opCertGeneric); | |
1832 | req(requirement, 1); | |
1833 | req(requirement, (uint8_t []) {APPLE_EXTENSION_OID, 2, 1}); | |
1834 | req(requirement, matchExists); | |
1835 | insert(blobs, 3, CSMAGIC_REQUIREMENT, requirement); | |
1836 | ||
1837 | put(backing, CSMAGIC_REQUIREMENTS, blobs); | |
1838 | } | |
1839 | ||
1840 | ||
ee5f5556 JF |
1841 | // XXX: this is just a "sufficiently large number" |
1842 | size_t certificate(0x3000); | |
1843 | ||
e4d33eec | 1844 | Allocate(idata, isize, output, fun([&](const MachHeader &mach_header, size_t size) -> size_t { |
e0098838 | 1845 | size_t alloc(sizeof(struct SuperBlob)); |
f4b2df35 | 1846 | |
f74652ab JF |
1847 | uint32_t normal((size + PageSize_ - 1) / PageSize_); |
1848 | ||
e0098838 | 1849 | uint32_t special(0); |
f4b2df35 | 1850 | |
f74652ab JF |
1851 | _foreach (slot, slots) |
1852 | special = std::max(special, slot.first); | |
1853 | ||
1854 | mach_header.ForSection(fun([&](const char *segment, const char *section, void *data, size_t size) { | |
1855 | if (strcmp(segment, "__TEXT") == 0 && section != NULL && strcmp(section, "__info_plist") == 0) | |
1856 | special = std::max(special, CSSLOT_INFOSLOT); | |
1857 | })); | |
1858 | ||
e0098838 JF |
1859 | special = std::max(special, CSSLOT_REQUIREMENTS); |
1860 | alloc += sizeof(struct BlobIndex); | |
7cb8c1fc | 1861 | alloc += backing.str().size(); |
f4b2df35 | 1862 | |
faa1b1f8 | 1863 | if (!entitlements.empty()) { |
e0098838 JF |
1864 | special = std::max(special, CSSLOT_ENTITLEMENTS); |
1865 | alloc += sizeof(struct BlobIndex); | |
1866 | alloc += sizeof(struct Blob); | |
1867 | alloc += entitlements.size(); | |
1868 | } | |
f4b2df35 | 1869 | |
f74652ab JF |
1870 | size_t directory(0); |
1871 | ||
1872 | directory += sizeof(struct BlobIndex); | |
1873 | directory += sizeof(struct Blob); | |
1874 | directory += sizeof(struct CodeDirectory); | |
1875 | directory += identifier.size() + 1; | |
f4b2df35 | 1876 | |
b73d2333 | 1877 | if (!team.empty()) |
f74652ab JF |
1878 | directory += team.size() + 1; |
1879 | ||
1880 | for (Algorithm *algorithm : GetAlgorithms()) | |
1881 | alloc = Align(alloc + directory + (special + normal) * algorithm->size_, 16); | |
b73d2333 | 1882 | |
fd50da22 | 1883 | #ifndef LDID_NOSMIME |
30c64adb JF |
1884 | if (!key.empty()) { |
1885 | alloc += sizeof(struct BlobIndex); | |
1886 | alloc += sizeof(struct Blob); | |
ee5f5556 | 1887 | alloc += certificate; |
30c64adb | 1888 | } |
fd50da22 | 1889 | #endif |
30c64adb | 1890 | |
e0098838 | 1891 | return alloc; |
b4b49374 | 1892 | }), fun([&](const MachHeader &mach_header, std::streambuf &output, size_t limit, const std::string &overlap, const char *top, const Progress &progress) -> size_t { |
efad8a44 | 1893 | Blobs blobs; |
f4b2df35 | 1894 | |
e0098838 | 1895 | if (true) { |
7cb8c1fc | 1896 | insert(blobs, CSSLOT_REQUIREMENTS, backing); |
e0098838 | 1897 | } |
4d4682ef | 1898 | |
faa1b1f8 | 1899 | if (!entitlements.empty()) { |
e0098838 | 1900 | std::stringbuf data; |
e0098838 | 1901 | put(data, entitlements.data(), entitlements.size()); |
8f310b91 | 1902 | insert(blobs, CSSLOT_ENTITLEMENTS, CSMAGIC_EMBEDDED_ENTITLEMENTS, data); |
e0098838 JF |
1903 | } |
1904 | ||
f74652ab | 1905 | Slots posts(slots); |
e0098838 | 1906 | |
f74652ab JF |
1907 | mach_header.ForSection(fun([&](const char *segment, const char *section, void *data, size_t size) { |
1908 | if (strcmp(segment, "__TEXT") == 0 && section != NULL && strcmp(section, "__info_plist") == 0) { | |
1909 | auto &slot(posts[CSSLOT_INFOSLOT]); | |
1910 | for (Algorithm *algorithm : GetAlgorithms()) | |
1911 | (*algorithm)(slot, data, size); | |
1912 | } | |
1913 | })); | |
e4d33eec | 1914 | |
f74652ab JF |
1915 | unsigned total(0); |
1916 | for (Algorithm *pointer : GetAlgorithms()) { | |
1917 | Algorithm &algorithm(*pointer); | |
1918 | ||
1919 | std::stringbuf data; | |
e4d33eec | 1920 | |
e0098838 JF |
1921 | uint32_t special(0); |
1922 | _foreach (blob, blobs) | |
1923 | special = std::max(special, blob.first); | |
e4d33eec | 1924 | _foreach (slot, posts) |
e57b1f91 | 1925 | special = std::max(special, slot.first); |
b9652d6e | 1926 | uint32_t normal((limit + PageSize_ - 1) / PageSize_); |
e0098838 | 1927 | |
e0098838 | 1928 | CodeDirectory directory; |
b73d2333 | 1929 | directory.version = Swap(uint32_t(0x00020200)); |
296fd38e | 1930 | directory.flags = Swap(uint32_t(flags)); |
e0098838 JF |
1931 | directory.nSpecialSlots = Swap(special); |
1932 | directory.codeLimit = Swap(uint32_t(limit)); | |
1933 | directory.nCodeSlots = Swap(normal); | |
f74652ab JF |
1934 | directory.hashSize = algorithm.size_; |
1935 | directory.hashType = algorithm.type_; | |
296fd38e | 1936 | directory.platform = platform ? 0x01 : 0x00; |
b9652d6e | 1937 | directory.pageSize = PageShift_; |
e0098838 | 1938 | directory.spare2 = Swap(uint32_t(0)); |
b73d2333 | 1939 | directory.scatterOffset = Swap(uint32_t(0)); |
71465d5f JF |
1940 | //directory.spare3 = Swap(uint32_t(0)); |
1941 | //directory.codeLimit64 = Swap(uint64_t(0)); | |
b73d2333 JF |
1942 | |
1943 | uint32_t offset(sizeof(Blob) + sizeof(CodeDirectory)); | |
1944 | ||
1945 | directory.identOffset = Swap(uint32_t(offset)); | |
1946 | offset += identifier.size() + 1; | |
1947 | ||
1948 | if (team.empty()) | |
1949 | directory.teamIDOffset = Swap(uint32_t(0)); | |
1950 | else { | |
1951 | directory.teamIDOffset = Swap(uint32_t(offset)); | |
1952 | offset += team.size() + 1; | |
1953 | } | |
1954 | ||
f74652ab | 1955 | offset += special * algorithm.size_; |
b73d2333 | 1956 | directory.hashOffset = Swap(uint32_t(offset)); |
f74652ab | 1957 | offset += normal * algorithm.size_; |
b73d2333 | 1958 | |
e0098838 JF |
1959 | put(data, &directory, sizeof(directory)); |
1960 | ||
ffdd1183 | 1961 | put(data, identifier.c_str(), identifier.size() + 1); |
d74ad7c4 JF |
1962 | if (!team.empty()) |
1963 | put(data, team.c_str(), team.size() + 1); | |
e0098838 | 1964 | |
f74652ab JF |
1965 | std::vector<uint8_t> storage((special + normal) * algorithm.size_); |
1966 | auto *hashes(&storage[special * algorithm.size_]); | |
e0098838 | 1967 | |
f74652ab | 1968 | memset(storage.data(), 0, special * algorithm.size_); |
4d4682ef JF |
1969 | |
1970 | _foreach (blob, blobs) { | |
e0098838 | 1971 | auto local(reinterpret_cast<const Blob *>(&blob.second[0])); |
f74652ab | 1972 | algorithm(hashes - blob.first * algorithm.size_, local, Swap(local->length)); |
4d4682ef | 1973 | } |
f4b2df35 | 1974 | |
f74652ab JF |
1975 | _foreach (slot, posts) |
1976 | memcpy(hashes - slot.first * algorithm.size_, algorithm[slot.second], algorithm.size_); | |
e57b1f91 | 1977 | |
b4b49374 | 1978 | progress(0); |
e0098838 | 1979 | if (normal != 1) |
e1d26767 | 1980 | for (size_t i = 0; i != normal - 1; ++i) { |
f74652ab | 1981 | algorithm(hashes + i * algorithm.size_, (PageSize_ * i < overlap.size() ? overlap.data() : top) + PageSize_ * i, PageSize_); |
b4b49374 | 1982 | progress(double(i) / normal); |
e1d26767 | 1983 | } |
e0098838 | 1984 | if (normal != 0) |
f74652ab | 1985 | algorithm(hashes + (normal - 1) * algorithm.size_, top + PageSize_ * (normal - 1), ((limit - 1) % PageSize_) + 1); |
b4b49374 | 1986 | progress(1); |
f4b2df35 | 1987 | |
f74652ab JF |
1988 | put(data, storage.data(), storage.size()); |
1989 | ||
1990 | const auto &save(insert(blobs, total == 0 ? CSSLOT_CODEDIRECTORY : CSSLOT_ALTERNATE + total - 1, CSMAGIC_CODEDIRECTORY, data)); | |
1991 | algorithm(hash, save.data(), save.size()); | |
e0098838 | 1992 | |
f74652ab | 1993 | ++total; |
f4b2df35 | 1994 | } |
e0098838 | 1995 | |
489c97b8 | 1996 | #ifndef LDID_NOSMIME |
30c64adb | 1997 | if (!key.empty()) { |
1190f447 JF |
1998 | #ifdef LDID_NOPLIST |
1999 | auto plist(CFDictionaryCreateMutable(kCFAllocatorDefault, 0, &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks)); | |
2000 | _scope({ CFRelease(plist); }); | |
2001 | ||
2002 | auto cdhashes(CFArrayCreateMutable(kCFAllocatorDefault, 0, &kCFTypeArrayCallBacks)); | |
2003 | _scope({ CFRelease(cdhashes); }); | |
2004 | ||
2005 | CFDictionarySetValue(plist, CFSTR("cdhashes"), cdhashes); | |
2006 | #else | |
fe54c5fb JF |
2007 | auto plist(plist_new_dict()); |
2008 | _scope({ plist_free(plist); }); | |
2009 | ||
2010 | auto cdhashes(plist_new_array()); | |
2011 | plist_dict_set_item(plist, "cdhashes", cdhashes); | |
1190f447 | 2012 | #endif |
fe54c5fb JF |
2013 | |
2014 | unsigned total(0); | |
2015 | for (Algorithm *pointer : GetAlgorithms()) { | |
2016 | Algorithm &algorithm(*pointer); | |
2017 | (void) algorithm; | |
2018 | ||
2019 | const auto &blob(blobs[total == 0 ? CSSLOT_CODEDIRECTORY : CSSLOT_ALTERNATE + total - 1]); | |
2020 | ++total; | |
2021 | ||
2022 | std::vector<char> hash; | |
2023 | algorithm(hash, blob.data(), blob.size()); | |
2024 | hash.resize(20); | |
2025 | ||
1190f447 JF |
2026 | #ifdef LDID_NOPLIST |
2027 | auto value(CFDataCreate(kCFAllocatorDefault, reinterpret_cast<const UInt8 *>(hash.data()), hash.size())); | |
2028 | _scope({ CFRelease(value); }); | |
2029 | CFArrayAppendValue(cdhashes, value); | |
2030 | #else | |
fe54c5fb | 2031 | plist_array_append_item(cdhashes, plist_new_data(hash.data(), hash.size())); |
1190f447 | 2032 | #endif |
fe54c5fb JF |
2033 | } |
2034 | ||
1190f447 JF |
2035 | #ifdef LDID_NOPLIST |
2036 | auto created(CFPropertyListCreateXMLData(kCFAllocatorDefault, plist)); | |
2037 | _scope({ CFRelease(created); }); | |
2038 | auto xml(reinterpret_cast<const char *>(CFDataGetBytePtr(created))); | |
2039 | auto size(CFDataGetLength(created)); | |
2040 | #else | |
fe54c5fb JF |
2041 | char *xml(NULL); |
2042 | uint32_t size; | |
2043 | plist_to_xml(plist, &xml, &size); | |
2044 | _scope({ free(xml); }); | |
1190f447 | 2045 | #endif |
fe54c5fb | 2046 | |
30c64adb JF |
2047 | std::stringbuf data; |
2048 | const std::string &sign(blobs[CSSLOT_CODEDIRECTORY]); | |
2049 | ||
2050 | Stuff stuff(key); | |
2051 | Buffer bio(sign); | |
2052 | ||
fe54c5fb | 2053 | Signature signature(stuff, sign, std::string(xml, size)); |
30c64adb JF |
2054 | Buffer result(signature); |
2055 | std::string value(result); | |
2056 | put(data, value.data(), value.size()); | |
2057 | ||
ee5f5556 JF |
2058 | const auto &save(insert(blobs, CSSLOT_SIGNATURESLOT, CSMAGIC_BLOBWRAPPER, data)); |
2059 | _assert(save.size() <= certificate); | |
30c64adb | 2060 | } |
489c97b8 | 2061 | #endif |
30c64adb | 2062 | |
efad8a44 | 2063 | return put(output, CSMAGIC_EMBEDDED_SIGNATURE, blobs); |
b4b49374 | 2064 | }), progress); |
4ebf3494 JF |
2065 | |
2066 | return hash; | |
e0098838 JF |
2067 | } |
2068 | ||
0326ab11 | 2069 | #ifndef LDID_NOTOOLS |
b4b49374 | 2070 | static void Unsign(void *idata, size_t isize, std::streambuf &output, const Progress &progress) { |
e4d33eec | 2071 | Allocate(idata, isize, output, fun([](const MachHeader &mach_header, size_t size) -> size_t { |
e0098838 | 2072 | return 0; |
b4b49374 | 2073 | }), fun([](const MachHeader &mach_header, std::streambuf &output, size_t limit, const std::string &overlap, const char *top, const Progress &progress) -> size_t { |
e0098838 | 2074 | return 0; |
b4b49374 | 2075 | }), progress); |
f4b2df35 JF |
2076 | } |
2077 | ||
addc31c5 | 2078 | std::string DiskFolder::Path(const std::string &path) const { |
23c11ee8 JF |
2079 | return path_ + "/" + path; |
2080 | } | |
2081 | ||
2082 | DiskFolder::DiskFolder(const std::string &path) : | |
2083 | path_(path) | |
2084 | { | |
2085 | } | |
2086 | ||
2087 | DiskFolder::~DiskFolder() { | |
2088 | if (!std::uncaught_exception()) | |
2089 | for (const auto &commit : commit_) | |
2090 | Commit(commit.first, commit.second); | |
2091 | } | |
2092 | ||
51ced023 JF |
2093 | #ifndef __WIN32__ |
2094 | std::string readlink(const std::string &path) { | |
2095 | for (size_t size(1024); ; size *= 2) { | |
2096 | std::string data; | |
2097 | data.resize(size); | |
2098 | ||
2099 | int writ(_syscall(::readlink(path.c_str(), &data[0], data.size()))); | |
2100 | if (size_t(writ) >= size) | |
2101 | continue; | |
2102 | ||
2103 | data.resize(writ); | |
2104 | return data; | |
2105 | } | |
2106 | } | |
2107 | #endif | |
2108 | ||
addc31c5 | 2109 | void DiskFolder::Find(const std::string &root, const std::string &base, const Functor<void (const std::string &)> &code, const Functor<void (const std::string &, const Functor<std::string ()> &)> &link) const { |
54a0f854 JF |
2110 | std::string path(Path(root) + base); |
2111 | ||
2112 | DIR *dir(opendir(path.c_str())); | |
2113 | _assert(dir != NULL); | |
2114 | _scope({ _syscall(closedir(dir)); }); | |
2115 | ||
2116 | while (auto child = readdir(dir)) { | |
261b9963 | 2117 | std::string name(child->d_name); |
54a0f854 JF |
2118 | if (name == "." || name == "..") |
2119 | continue; | |
2120 | if (Starts(name, ".ldid.")) | |
2121 | continue; | |
2122 | ||
b6d8da4e JF |
2123 | bool directory; |
2124 | ||
2125 | #ifdef __WIN32__ | |
2126 | struct stat info; | |
255e1652 | 2127 | _syscall(stat((path + name).c_str(), &info)); |
b6d8da4e JF |
2128 | if (false); |
2129 | else if (S_ISDIR(info.st_mode)) | |
2130 | directory = true; | |
2131 | else if (S_ISREG(info.st_mode)) | |
2132 | directory = false; | |
2133 | else | |
2134 | _assert_(false, "st_mode=%x", info.st_mode); | |
2135 | #else | |
54a0f854 JF |
2136 | switch (child->d_type) { |
2137 | case DT_DIR: | |
b6d8da4e JF |
2138 | directory = true; |
2139 | break; | |
54a0f854 | 2140 | case DT_REG: |
b6d8da4e JF |
2141 | directory = false; |
2142 | break; | |
51ced023 JF |
2143 | case DT_LNK: |
2144 | link(base + name, fun([&]() { return readlink(path + name); })); | |
2145 | continue; | |
54a0f854 JF |
2146 | default: |
2147 | _assert_(false, "d_type=%u", child->d_type); | |
54a0f854 | 2148 | } |
b6d8da4e JF |
2149 | #endif |
2150 | ||
2151 | if (directory) | |
51ced023 | 2152 | Find(root, base + name + "/", code, link); |
b6d8da4e | 2153 | else |
addc31c5 | 2154 | code(base + name); |
54a0f854 JF |
2155 | } |
2156 | } | |
2157 | ||
addc31c5 JF |
2158 | void DiskFolder::Save(const std::string &path, bool edit, const void *flag, const Functor<void (std::streambuf &)> &code) { |
2159 | if (!edit) { | |
2160 | // XXX: use nullbuf | |
2161 | std::stringbuf save; | |
2162 | code(save); | |
2163 | } else { | |
2164 | std::filebuf save; | |
2165 | auto from(Path(path)); | |
2166 | commit_[from] = Temporary(save, from); | |
2167 | code(save); | |
2168 | } | |
23c11ee8 JF |
2169 | } |
2170 | ||
addc31c5 | 2171 | bool DiskFolder::Look(const std::string &path) const { |
878260fc JF |
2172 | return _syscall(access(Path(path).c_str(), R_OK), ENOENT) == 0; |
2173 | } | |
2174 | ||
e1d26767 | 2175 | void DiskFolder::Open(const std::string &path, const Functor<void (std::streambuf &, size_t, const void *)> &code) const { |
23c11ee8 JF |
2176 | std::filebuf data; |
2177 | auto result(data.open(Path(path).c_str(), std::ios::binary | std::ios::in)); | |
878260fc | 2178 | _assert_(result == &data, "DiskFolder::Open(%s)", path.c_str()); |
e1d26767 JF |
2179 | |
2180 | auto length(data.pubseekoff(0, std::ios::end, std::ios::in)); | |
2181 | data.pubseekpos(0, std::ios::in); | |
2182 | code(data, length, NULL); | |
23c11ee8 JF |
2183 | } |
2184 | ||
addc31c5 | 2185 | void DiskFolder::Find(const std::string &path, const Functor<void (const std::string &)> &code, const Functor<void (const std::string &, const Functor<std::string ()> &)> &link) const { |
51ced023 | 2186 | Find(path, "", code, link); |
23c11ee8 | 2187 | } |
0326ab11 | 2188 | #endif |
23c11ee8 | 2189 | |
2443500c | 2190 | SubFolder::SubFolder(Folder &parent, const std::string &path) : |
23c11ee8 JF |
2191 | parent_(parent), |
2192 | path_(path) | |
2193 | { | |
2194 | } | |
2195 | ||
addc31c5 JF |
2196 | void SubFolder::Save(const std::string &path, bool edit, const void *flag, const Functor<void (std::streambuf &)> &code) { |
2197 | return parent_.Save(path_ + path, edit, flag, code); | |
23c11ee8 JF |
2198 | } |
2199 | ||
addc31c5 | 2200 | bool SubFolder::Look(const std::string &path) const { |
878260fc JF |
2201 | return parent_.Look(path_ + path); |
2202 | } | |
2203 | ||
e1d26767 | 2204 | void SubFolder::Open(const std::string &path, const Functor<void (std::streambuf &, size_t, const void *)> &code) const { |
2443500c | 2205 | return parent_.Open(path_ + path, code); |
23c11ee8 JF |
2206 | } |
2207 | ||
addc31c5 | 2208 | void SubFolder::Find(const std::string &path, const Functor<void (const std::string &)> &code, const Functor<void (const std::string &, const Functor<std::string ()> &)> &link) const { |
51ced023 | 2209 | return parent_.Find(path_ + path, code, link); |
23c11ee8 JF |
2210 | } |
2211 | ||
addc31c5 | 2212 | std::string UnionFolder::Map(const std::string &path) const { |
7c49f771 JF |
2213 | auto remap(remaps_.find(path)); |
2214 | if (remap == remaps_.end()) | |
2215 | return path; | |
2216 | return remap->second; | |
2217 | } | |
2218 | ||
e1d26767 | 2219 | void UnionFolder::Map(const std::string &path, const Functor<void (const std::string &)> &code, const std::string &file, const Functor<void (const Functor<void (std::streambuf &, size_t, const void *)> &)> &save) const { |
7c49f771 | 2220 | if (file.size() >= path.size() && file.substr(0, path.size()) == path) |
addc31c5 | 2221 | code(file.substr(path.size())); |
7c49f771 JF |
2222 | } |
2223 | ||
886cb3f1 JF |
2224 | UnionFolder::UnionFolder(Folder &parent) : |
2225 | parent_(parent) | |
2226 | { | |
2227 | } | |
2228 | ||
addc31c5 JF |
2229 | void UnionFolder::Save(const std::string &path, bool edit, const void *flag, const Functor<void (std::streambuf &)> &code) { |
2230 | return parent_.Save(Map(path), edit, flag, code); | |
886cb3f1 JF |
2231 | } |
2232 | ||
addc31c5 | 2233 | bool UnionFolder::Look(const std::string &path) const { |
878260fc JF |
2234 | auto file(resets_.find(path)); |
2235 | if (file != resets_.end()) | |
2236 | return true; | |
2237 | return parent_.Look(Map(path)); | |
2238 | } | |
2239 | ||
e1d26767 | 2240 | void UnionFolder::Open(const std::string &path, const Functor<void (std::streambuf &, size_t, const void *)> &code) const { |
7c49f771 JF |
2241 | auto file(resets_.find(path)); |
2242 | if (file == resets_.end()) | |
2243 | return parent_.Open(Map(path), code); | |
fefcecb0 | 2244 | auto &entry(file->second); |
886cb3f1 | 2245 | |
4e6d856a | 2246 | auto &data(*entry.data_); |
e1d26767 | 2247 | auto length(data.pubseekoff(0, std::ios::end, std::ios::in)); |
886cb3f1 | 2248 | data.pubseekpos(0, std::ios::in); |
4e6d856a | 2249 | code(data, length, entry.flag_); |
886cb3f1 JF |
2250 | } |
2251 | ||
addc31c5 | 2252 | void UnionFolder::Find(const std::string &path, const Functor<void (const std::string &)> &code, const Functor<void (const std::string &, const Functor<std::string ()> &)> &link) const { |
7c49f771 | 2253 | for (auto &reset : resets_) |
e1d26767 | 2254 | Map(path, code, reset.first, fun([&](const Functor<void (std::streambuf &, size_t, const void *)> &code) { |
fefcecb0 | 2255 | auto &entry(reset.second); |
4e6d856a JF |
2256 | auto &data(*entry.data_); |
2257 | auto length(data.pubseekoff(0, std::ios::end, std::ios::in)); | |
2258 | data.pubseekpos(0, std::ios::in); | |
2259 | code(data, length, entry.flag_); | |
7c49f771 JF |
2260 | })); |
2261 | ||
2262 | for (auto &remap : remaps_) | |
e1d26767 JF |
2263 | Map(path, code, remap.first, fun([&](const Functor<void (std::streambuf &, size_t, const void *)> &code) { |
2264 | parent_.Open(remap.second, fun([&](std::streambuf &data, size_t length, const void *flag) { | |
2265 | code(data, length, flag); | |
886cb3f1 | 2266 | })); |
7c49f771 | 2267 | })); |
66f2305d JF |
2268 | |
2269 | parent_.Find(path, fun([&](const std::string &name) { | |
2270 | if (deletes_.find(path + name) == deletes_.end()) | |
2271 | code(name); | |
2272 | }), fun([&](const std::string &name, const Functor<std::string ()> &read) { | |
2273 | if (deletes_.find(path + name) == deletes_.end()) | |
2274 | link(name, read); | |
2275 | })); | |
886cb3f1 JF |
2276 | } |
2277 | ||
0326ab11 | 2278 | #ifndef LDID_NOTOOLS |
b4b49374 JF |
2279 | static void copy(std::streambuf &source, std::streambuf &target, size_t length, const Progress &progress) { |
2280 | progress(0); | |
23c11ee8 JF |
2281 | size_t total(0); |
2282 | for (;;) { | |
e1d26767 | 2283 | char data[4096 * 4]; |
23c11ee8 JF |
2284 | size_t writ(source.sgetn(data, sizeof(data))); |
2285 | if (writ == 0) | |
2286 | break; | |
2287 | _assert(target.sputn(data, writ) == writ); | |
2288 | total += writ; | |
b4b49374 | 2289 | progress(double(total) / length); |
23c11ee8 | 2290 | } |
23c11ee8 JF |
2291 | } |
2292 | ||
489c97b8 | 2293 | #ifndef LDID_NOPLIST |
8dfe0afc JF |
2294 | static plist_t plist(const std::string &data) { |
2295 | plist_t plist(NULL); | |
2296 | if (Starts(data, "bplist00")) | |
2297 | plist_from_bin(data.data(), data.size(), &plist); | |
2298 | else | |
2299 | plist_from_xml(data.data(), data.size(), &plist); | |
2300 | _assert(plist != NULL); | |
2301 | return plist; | |
23c11ee8 JF |
2302 | } |
2303 | ||
e1d26767 | 2304 | static void plist_d(std::streambuf &buffer, size_t length, const Functor<void (plist_t)> &code) { |
23c11ee8 | 2305 | std::stringbuf data; |
b4b49374 | 2306 | copy(buffer, data, length, dummy_); |
8dfe0afc JF |
2307 | auto node(plist(data.str())); |
2308 | _scope({ plist_free(node); }); | |
2309 | _assert(plist_get_node_type(node) == PLIST_DICT); | |
2310 | code(node); | |
23c11ee8 JF |
2311 | } |
2312 | ||
8dfe0afc JF |
2313 | static std::string plist_s(plist_t node) { |
2314 | _assert(node != NULL); | |
2315 | _assert(plist_get_node_type(node) == PLIST_STRING); | |
2316 | char *data; | |
2317 | plist_get_string_val(node, &data); | |
2318 | _scope({ free(data); }); | |
2319 | return data; | |
23c11ee8 | 2320 | } |
489c97b8 | 2321 | #endif |
23c11ee8 JF |
2322 | |
2323 | enum Mode { | |
2324 | NoMode, | |
2325 | OptionalMode, | |
2326 | OmitMode, | |
2327 | NestedMode, | |
2328 | TopMode, | |
2329 | }; | |
2330 | ||
2331 | class Expression { | |
2332 | private: | |
2333 | regex_t regex_; | |
4ebf3494 | 2334 | std::vector<std::string> matches_; |
23c11ee8 JF |
2335 | |
2336 | public: | |
2337 | Expression(const std::string &code) { | |
4ebf3494 JF |
2338 | _assert_(regcomp(®ex_, code.c_str(), REG_EXTENDED) == 0, "regcomp()"); |
2339 | matches_.resize(regex_.re_nsub + 1); | |
23c11ee8 JF |
2340 | } |
2341 | ||
2342 | ~Expression() { | |
2343 | regfree(®ex_); | |
2344 | } | |
2345 | ||
4ebf3494 JF |
2346 | bool operator ()(const std::string &data) { |
2347 | regmatch_t matches[matches_.size()]; | |
2348 | auto value(regexec(®ex_, data.c_str(), matches_.size(), matches, 0)); | |
23c11ee8 JF |
2349 | if (value == REG_NOMATCH) |
2350 | return false; | |
2351 | _assert_(value == 0, "regexec()"); | |
4ebf3494 JF |
2352 | for (size_t i(0); i != matches_.size(); ++i) |
2353 | matches_[i].assign(data.data() + matches[i].rm_so, matches[i].rm_eo - matches[i].rm_so); | |
23c11ee8 JF |
2354 | return true; |
2355 | } | |
4ebf3494 JF |
2356 | |
2357 | const std::string &operator [](size_t index) const { | |
2358 | return matches_[index]; | |
2359 | } | |
23c11ee8 JF |
2360 | }; |
2361 | ||
2362 | struct Rule { | |
2363 | unsigned weight_; | |
2364 | Mode mode_; | |
2365 | std::string code_; | |
2366 | ||
2367 | mutable std::auto_ptr<Expression> regex_; | |
2368 | ||
2369 | Rule(unsigned weight, Mode mode, const std::string &code) : | |
2370 | weight_(weight), | |
2371 | mode_(mode), | |
2372 | code_(code) | |
2373 | { | |
2374 | } | |
2375 | ||
2376 | Rule(const Rule &rhs) : | |
2377 | weight_(rhs.weight_), | |
2378 | mode_(rhs.mode_), | |
2379 | code_(rhs.code_) | |
2380 | { | |
2381 | } | |
2382 | ||
2383 | void Compile() const { | |
2384 | regex_.reset(new Expression(code_)); | |
2385 | } | |
2386 | ||
2387 | bool operator ()(const std::string &data) const { | |
2388 | _assert(regex_.get() != NULL); | |
2389 | return (*regex_)(data); | |
2390 | } | |
2391 | ||
2392 | bool operator <(const Rule &rhs) const { | |
2393 | if (weight_ > rhs.weight_) | |
2394 | return true; | |
2395 | if (weight_ < rhs.weight_) | |
2396 | return false; | |
2397 | return mode_ > rhs.mode_; | |
2398 | } | |
2399 | }; | |
2400 | ||
2401 | struct RuleCode { | |
2402 | bool operator ()(const Rule *lhs, const Rule *rhs) const { | |
2403 | return lhs->code_ < rhs->code_; | |
2404 | } | |
2405 | }; | |
2406 | ||
489c97b8 | 2407 | #ifndef LDID_NOPLIST |
296fd38e | 2408 | static Hash Sign(const uint8_t *prefix, size_t size, std::streambuf &buffer, Hash &hash, std::streambuf &save, const std::string &identifier, const std::string &entitlements, const std::string &requirements, const std::string &key, const Slots &slots, size_t length, uint32_t flags, bool platform, const Progress &progress) { |
7f5ad603 JF |
2409 | // XXX: this is a miserable fail |
2410 | std::stringbuf temp; | |
f9cd1d1c | 2411 | put(temp, prefix, size); |
b4b49374 | 2412 | copy(buffer, temp, length - size, progress); |
c4f8d243 | 2413 | // XXX: this is a stupid hack |
e1d26767 | 2414 | pad(temp, 0x10 - (length & 0xf)); |
7f5ad603 JF |
2415 | auto data(temp.str()); |
2416 | ||
2417 | HashProxy proxy(hash, save); | |
7b048e81 | 2418 | return Sign(data.data(), data.size(), proxy, identifier, entitlements, requirements, key, slots, flags, platform, progress); |
7f5ad603 JF |
2419 | } |
2420 | ||
b4b49374 | 2421 | Bundle Sign(const std::string &root, Folder &folder, const std::string &key, std::map<std::string, Hash> &remote, const std::string &requirements, const Functor<std::string (const std::string &, const std::string &)> &alter, const Progress &progress) { |
23c11ee8 JF |
2422 | std::string executable; |
2423 | std::string identifier; | |
2424 | ||
4ebf3494 JF |
2425 | bool mac(false); |
2426 | ||
2427 | std::string info("Info.plist"); | |
2428 | if (!folder.Look(info) && folder.Look("Resources/" + info)) { | |
2429 | mac = true; | |
2430 | info = "Resources/" + info; | |
2431 | } | |
23c11ee8 | 2432 | |
e1d26767 JF |
2433 | folder.Open(info, fun([&](std::streambuf &buffer, size_t length, const void *flag) { |
2434 | plist_d(buffer, length, fun([&](plist_t node) { | |
8dfe0afc JF |
2435 | executable = plist_s(plist_dict_get_item(node, "CFBundleExecutable")); |
2436 | identifier = plist_s(plist_dict_get_item(node, "CFBundleIdentifier")); | |
23c11ee8 | 2437 | })); |
878260fc | 2438 | })); |
23c11ee8 | 2439 | |
4ebf3494 JF |
2440 | if (!mac && folder.Look("MacOS/" + executable)) { |
2441 | executable = "MacOS/" + executable; | |
2442 | mac = true; | |
2443 | } | |
2444 | ||
b4b49374 JF |
2445 | progress(root + "*"); |
2446 | ||
9b693b34 | 2447 | std::string entitlements; |
e1d26767 | 2448 | folder.Open(executable, fun([&](std::streambuf &buffer, size_t length, const void *flag) { |
9b693b34 JF |
2449 | // XXX: this is a miserable fail |
2450 | std::stringbuf temp; | |
b4b49374 | 2451 | copy(buffer, temp, length, progress); |
9b693b34 | 2452 | // XXX: this is a stupid hack |
e1d26767 | 2453 | pad(temp, 0x10 - (length & 0xf)); |
9b693b34 JF |
2454 | auto data(temp.str()); |
2455 | entitlements = alter(root, Analyze(data.data(), data.size())); | |
2456 | })); | |
2457 | ||
d9c2341d JF |
2458 | static const std::string directory("_CodeSignature/"); |
2459 | static const std::string signature(directory + "CodeResources"); | |
2460 | ||
23c11ee8 JF |
2461 | std::map<std::string, std::multiset<Rule>> versions; |
2462 | ||
2463 | auto &rules1(versions[""]); | |
2464 | auto &rules2(versions["2"]); | |
2465 | ||
4ebf3494 JF |
2466 | const std::string resources(mac ? "Resources/" : ""); |
2467 | ||
23c11ee8 | 2468 | if (true) { |
4ebf3494 | 2469 | rules1.insert(Rule{1, NoMode, "^" + resources}); |
4ebf3494 JF |
2470 | rules1.insert(Rule{1000, OptionalMode, "^" + resources + ".*\\.lproj/"}); |
2471 | rules1.insert(Rule{1100, OmitMode, "^" + resources + ".*\\.lproj/locversion.plist$"}); | |
ca8445da | 2472 | rules1.insert(Rule{1010, NoMode, "^Base\\.lproj/"}); |
23c11ee8 JF |
2473 | rules1.insert(Rule{1, NoMode, "^version.plist$"}); |
2474 | } | |
2475 | ||
2476 | if (true) { | |
2477 | rules2.insert(Rule{11, NoMode, ".*\\.dSYM($|/)"}); | |
4ebf3494 | 2478 | rules2.insert(Rule{20, NoMode, "^" + resources}); |
23c11ee8 | 2479 | rules2.insert(Rule{2000, OmitMode, "^(.*/)?\\.DS_Store$"}); |
23c11ee8 JF |
2480 | rules2.insert(Rule{10, NestedMode, "^(Frameworks|SharedFrameworks|PlugIns|Plug-ins|XPCServices|Helpers|MacOS|Library/(Automator|Spotlight|LoginItems))/"}); |
2481 | rules2.insert(Rule{1, NoMode, "^.*"}); | |
4ebf3494 JF |
2482 | rules2.insert(Rule{1000, OptionalMode, "^" + resources + ".*\\.lproj/"}); |
2483 | rules2.insert(Rule{1100, OmitMode, "^" + resources + ".*\\.lproj/locversion.plist$"}); | |
ca8445da | 2484 | rules2.insert(Rule{1010, NoMode, "^Base\\.lproj/"}); |
23c11ee8 JF |
2485 | rules2.insert(Rule{20, OmitMode, "^Info\\.plist$"}); |
2486 | rules2.insert(Rule{20, OmitMode, "^PkgInfo$"}); | |
23c11ee8 JF |
2487 | rules2.insert(Rule{10, NestedMode, "^[^/]+$"}); |
2488 | rules2.insert(Rule{20, NoMode, "^embedded\\.provisionprofile$"}); | |
2489 | rules2.insert(Rule{20, NoMode, "^version\\.plist$"}); | |
2490 | } | |
2491 | ||
7d19f8dd | 2492 | std::map<std::string, Hash> local; |
23c11ee8 | 2493 | |
4ebf3494 | 2494 | std::string failure(mac ? "Contents/|Versions/[^/]*/Resources/" : ""); |
7d19f8dd | 2495 | Expression nested("^(Frameworks/[^/]*\\.framework|PlugIns/[^/]*\\.appex(()|/[^/]*.app))/(" + failure + ")Info\\.plist$"); |
4ebf3494 | 2496 | std::map<std::string, Bundle> bundles; |
23c11ee8 | 2497 | |
addc31c5 | 2498 | folder.Find("", fun([&](const std::string &name) { |
23c11ee8 JF |
2499 | if (!nested(name)) |
2500 | return; | |
2501 | auto bundle(root + Split(name).dir); | |
4ebf3494 | 2502 | bundle.resize(bundle.size() - resources.size()); |
2443500c | 2503 | SubFolder subfolder(folder, bundle); |
9b693b34 JF |
2504 | |
2505 | bundles[nested[1]] = Sign(bundle, subfolder, key, local, "", Starts(name, "PlugIns/") ? alter : | |
4230d78b | 2506 | static_cast<const Functor<std::string (const std::string &, const std::string &)> &>(fun([&](const std::string &, const std::string &) -> std::string { return entitlements; })) |
b4b49374 | 2507 | , progress); |
51ced023 | 2508 | }), fun([&](const std::string &name, const Functor<std::string ()> &read) { |
23c11ee8 JF |
2509 | })); |
2510 | ||
1acc78fb | 2511 | std::set<std::string> excludes; |
51ced023 | 2512 | |
4ebf3494 | 2513 | auto exclude([&](const std::string &name) { |
d9c2341d JF |
2514 | // BundleDiskRep::adjustResources -> builder.addExclusion |
2515 | if (name == executable || Starts(name, directory) || Starts(name, "_MASReceipt/") || name == "CodeResources") | |
4ebf3494 | 2516 | return true; |
23c11ee8 | 2517 | |
7d19f8dd | 2518 | for (const auto &bundle : bundles) |
1acc78fb JF |
2519 | if (Starts(name, bundle.first + "/")) { |
2520 | excludes.insert(name); | |
4ebf3494 | 2521 | return true; |
1acc78fb | 2522 | } |
4ebf3494 JF |
2523 | |
2524 | return false; | |
2525 | }); | |
2526 | ||
1acc78fb JF |
2527 | std::map<std::string, std::string> links; |
2528 | ||
addc31c5 | 2529 | folder.Find("", fun([&](const std::string &name) { |
4ebf3494 JF |
2530 | if (exclude(name)) |
2531 | return; | |
2532 | ||
7d19f8dd JF |
2533 | if (local.find(name) != local.end()) |
2534 | return; | |
2535 | auto &hash(local[name]); | |
23c11ee8 | 2536 | |
e1d26767 JF |
2537 | folder.Open(name, fun([&](std::streambuf &data, size_t length, const void *flag) { |
2538 | progress(root + name); | |
2539 | ||
f9cd1d1c JF |
2540 | union { |
2541 | struct { | |
2542 | uint32_t magic; | |
2543 | uint32_t count; | |
2544 | }; | |
2545 | ||
2546 | uint8_t bytes[8]; | |
2547 | } header; | |
2548 | ||
2549 | auto size(most(data, &header.bytes, sizeof(header.bytes))); | |
7d19f8dd JF |
2550 | |
2551 | if (name != "_WatchKitStub/WK" && size == sizeof(header.bytes)) | |
f9cd1d1c JF |
2552 | switch (Swap(header.magic)) { |
2553 | case FAT_MAGIC: | |
2554 | // Java class file format | |
2555 | if (Swap(header.count) >= 40) | |
2556 | break; | |
2557 | case FAT_CIGAM: | |
2558 | case MH_MAGIC: case MH_MAGIC_64: | |
2559 | case MH_CIGAM: case MH_CIGAM_64: | |
addc31c5 JF |
2560 | folder.Save(name, true, flag, fun([&](std::streambuf &save) { |
2561 | Slots slots; | |
296fd38e | 2562 | Sign(header.bytes, size, data, hash, save, identifier, "", "", key, slots, length, 0, false, Progression(progress, root + name)); |
addc31c5 | 2563 | })); |
f9cd1d1c JF |
2564 | return; |
2565 | } | |
2566 | ||
addc31c5 JF |
2567 | folder.Save(name, false, flag, fun([&](std::streambuf &save) { |
2568 | HashProxy proxy(hash, save); | |
2569 | put(proxy, header.bytes, size); | |
b4b49374 | 2570 | copy(data, proxy, length - size, progress); |
addc31c5 | 2571 | })); |
23c11ee8 | 2572 | })); |
51ced023 | 2573 | }), fun([&](const std::string &name, const Functor<std::string ()> &read) { |
4ebf3494 JF |
2574 | if (exclude(name)) |
2575 | return; | |
2576 | ||
51ced023 | 2577 | links[name] = read(); |
23c11ee8 JF |
2578 | })); |
2579 | ||
8dfe0afc JF |
2580 | auto plist(plist_new_dict()); |
2581 | _scope({ plist_free(plist); }); | |
23c11ee8 JF |
2582 | |
2583 | for (const auto &version : versions) { | |
8dfe0afc JF |
2584 | auto files(plist_new_dict()); |
2585 | plist_dict_set_item(plist, ("files" + version.first).c_str(), files); | |
23c11ee8 JF |
2586 | |
2587 | for (const auto &rule : version.second) | |
2588 | rule.Compile(); | |
2589 | ||
9ec8439b JF |
2590 | bool old(&version.second == &rules1); |
2591 | ||
7d19f8dd | 2592 | for (const auto &hash : local) |
23c11ee8 JF |
2593 | for (const auto &rule : version.second) |
2594 | if (rule(hash.first)) { | |
1acc78fb JF |
2595 | if (!old && mac && excludes.find(hash.first) != excludes.end()); |
2596 | else if (old && rule.mode_ == NoMode) | |
f74652ab | 2597 | plist_dict_set_item(files, hash.first.c_str(), plist_new_data(reinterpret_cast<const char *>(hash.second.sha1_), sizeof(hash.second.sha1_))); |
9ec8439b | 2598 | else if (rule.mode_ != OmitMode) { |
8dfe0afc | 2599 | auto entry(plist_new_dict()); |
f74652ab | 2600 | plist_dict_set_item(entry, "hash", plist_new_data(reinterpret_cast<const char *>(hash.second.sha1_), sizeof(hash.second.sha1_))); |
9ec8439b | 2601 | if (!old) |
f74652ab | 2602 | plist_dict_set_item(entry, "hash2", plist_new_data(reinterpret_cast<const char *>(hash.second.sha256_), sizeof(hash.second.sha256_))); |
9ec8439b JF |
2603 | if (rule.mode_ == OptionalMode) |
2604 | plist_dict_set_item(entry, "optional", plist_new_bool(true)); | |
8dfe0afc | 2605 | plist_dict_set_item(files, hash.first.c_str(), entry); |
23c11ee8 JF |
2606 | } |
2607 | ||
51ced023 JF |
2608 | break; |
2609 | } | |
2610 | ||
2611 | for (const auto &link : links) | |
2612 | for (const auto &rule : version.second) | |
2613 | if (rule(link.first)) { | |
4ebf3494 | 2614 | if (rule.mode_ != OmitMode) { |
51ced023 JF |
2615 | auto entry(plist_new_dict()); |
2616 | plist_dict_set_item(entry, "symlink", plist_new_string(link.second.c_str())); | |
2617 | if (rule.mode_ == OptionalMode) | |
2618 | plist_dict_set_item(entry, "optional", plist_new_bool(true)); | |
2619 | plist_dict_set_item(files, link.first.c_str(), entry); | |
2620 | } | |
2621 | ||
23c11ee8 JF |
2622 | break; |
2623 | } | |
4ebf3494 | 2624 | |
7d19f8dd JF |
2625 | if (!old && mac) |
2626 | for (const auto &bundle : bundles) { | |
2627 | auto entry(plist_new_dict()); | |
f74652ab | 2628 | plist_dict_set_item(entry, "cdhash", plist_new_data(reinterpret_cast<const char *>(bundle.second.hash.sha256_), sizeof(bundle.second.hash.sha256_))); |
7d19f8dd JF |
2629 | plist_dict_set_item(entry, "requirement", plist_new_string("anchor apple generic")); |
2630 | plist_dict_set_item(files, bundle.first.c_str(), entry); | |
2631 | } | |
23c11ee8 JF |
2632 | } |
2633 | ||
2634 | for (const auto &version : versions) { | |
8dfe0afc JF |
2635 | auto rules(plist_new_dict()); |
2636 | plist_dict_set_item(plist, ("rules" + version.first).c_str(), rules); | |
23c11ee8 JF |
2637 | |
2638 | std::multiset<const Rule *, RuleCode> ordered; | |
2639 | for (const auto &rule : version.second) | |
2640 | ordered.insert(&rule); | |
2641 | ||
2642 | for (const auto &rule : ordered) | |
2643 | if (rule->weight_ == 1 && rule->mode_ == NoMode) | |
8dfe0afc | 2644 | plist_dict_set_item(rules, rule->code_.c_str(), plist_new_bool(true)); |
23c11ee8 | 2645 | else { |
8dfe0afc JF |
2646 | auto entry(plist_new_dict()); |
2647 | plist_dict_set_item(rules, rule->code_.c_str(), entry); | |
23c11ee8 JF |
2648 | |
2649 | switch (rule->mode_) { | |
2650 | case NoMode: | |
2651 | break; | |
2652 | case OmitMode: | |
8dfe0afc | 2653 | plist_dict_set_item(entry, "omit", plist_new_bool(true)); |
23c11ee8 JF |
2654 | break; |
2655 | case OptionalMode: | |
8dfe0afc | 2656 | plist_dict_set_item(entry, "optional", plist_new_bool(true)); |
23c11ee8 JF |
2657 | break; |
2658 | case NestedMode: | |
8dfe0afc | 2659 | plist_dict_set_item(entry, "nested", plist_new_bool(true)); |
23c11ee8 JF |
2660 | break; |
2661 | case TopMode: | |
8dfe0afc | 2662 | plist_dict_set_item(entry, "top", plist_new_bool(true)); |
23c11ee8 JF |
2663 | break; |
2664 | } | |
2665 | ||
2666 | if (rule->weight_ >= 10000) | |
8dfe0afc | 2667 | plist_dict_set_item(entry, "weight", plist_new_uint(rule->weight_)); |
23c11ee8 | 2668 | else if (rule->weight_ != 1) |
8dfe0afc | 2669 | plist_dict_set_item(entry, "weight", plist_new_real(rule->weight_)); |
23c11ee8 | 2670 | } |
23c11ee8 JF |
2671 | } |
2672 | ||
addc31c5 | 2673 | folder.Save(signature, true, NULL, fun([&](std::streambuf &save) { |
7d19f8dd | 2674 | HashProxy proxy(local[signature], save); |
8dfe0afc JF |
2675 | char *xml(NULL); |
2676 | uint32_t size; | |
2677 | plist_to_xml(plist, &xml, &size); | |
2678 | _scope({ free(xml); }); | |
2679 | put(proxy, xml, size); | |
23c11ee8 JF |
2680 | })); |
2681 | ||
4ebf3494 JF |
2682 | Bundle bundle; |
2683 | bundle.path = executable; | |
2684 | ||
e1d26767 JF |
2685 | folder.Open(executable, fun([&](std::streambuf &buffer, size_t length, const void *flag) { |
2686 | progress(root + executable); | |
addc31c5 | 2687 | folder.Save(executable, true, flag, fun([&](std::streambuf &save) { |
23c11ee8 | 2688 | Slots slots; |
7d19f8dd JF |
2689 | slots[1] = local.at(info); |
2690 | slots[3] = local.at(signature); | |
296fd38e | 2691 | bundle.hash = Sign(NULL, 0, buffer, local[executable], save, identifier, entitlements, requirements, key, slots, length, 0, false, Progression(progress, root + executable)); |
23c11ee8 JF |
2692 | })); |
2693 | })); | |
2694 | ||
7d19f8dd JF |
2695 | for (const auto &entry : local) |
2696 | remote[root + entry.first] = entry.second; | |
23c11ee8 | 2697 | |
4ebf3494 | 2698 | return bundle; |
23c11ee8 | 2699 | } |
9ec8439b | 2700 | |
b4b49374 | 2701 | Bundle Sign(const std::string &root, Folder &folder, const std::string &key, const std::string &requirements, const Functor<std::string (const std::string &, const std::string &)> &alter, const Progress &progress) { |
7d19f8dd | 2702 | std::map<std::string, Hash> local; |
b4b49374 | 2703 | return Sign(root, folder, key, local, requirements, alter, progress); |
9ec8439b | 2704 | } |
489c97b8 | 2705 | #endif |
23c11ee8 | 2706 | |
0326ab11 | 2707 | #endif |
a0c715e9 JF |
2708 | } |
2709 | ||
0326ab11 | 2710 | #ifndef LDID_NOTOOLS |
a7f01a65 | 2711 | int main(int argc, char *argv[]) { |
489c97b8 | 2712 | #ifndef LDID_NOSMIME |
30c64adb | 2713 | OpenSSL_add_all_algorithms(); |
489c97b8 | 2714 | #endif |
30c64adb | 2715 | |
5525a5a7 JF |
2716 | union { |
2717 | uint16_t word; | |
2718 | uint8_t byte[2]; | |
2719 | } endian = {1}; | |
2720 | ||
2721 | little_ = endian.byte[0]; | |
2722 | ||
20e7eb29 | 2723 | bool flag_r(false); |
9c83be90 | 2724 | bool flag_e(false); |
9914b1b3 | 2725 | bool flag_q(false); |
a362a82f | 2726 | |
fdbee693 | 2727 | #ifndef LDID_NOFLAGT |
a362a82f | 2728 | bool flag_T(false); |
fdbee693 | 2729 | #endif |
20c5f1e8 | 2730 | |
fdb119ef | 2731 | bool flag_S(false); |
20c5f1e8 | 2732 | bool flag_s(false); |
a362a82f | 2733 | |
c0cc1574 | 2734 | bool flag_D(false); |
c0cc1574 | 2735 | |
8c417842 JF |
2736 | bool flag_A(false); |
2737 | bool flag_a(false); | |
2738 | ||
56f0487d JF |
2739 | bool flag_u(false); |
2740 | ||
296fd38e JF |
2741 | uint32_t flags(0); |
2742 | bool platform(false); | |
2743 | ||
c0cc1574 JF |
2744 | uint32_t flag_CPUType(_not(uint32_t)); |
2745 | uint32_t flag_CPUSubtype(_not(uint32_t)); | |
2746 | ||
5d7ceb5d JF |
2747 | const char *flag_I(NULL); |
2748 | ||
fdbee693 | 2749 | #ifndef LDID_NOFLAGT |
a362a82f JF |
2750 | bool timeh(false); |
2751 | uint32_t timev(0); | |
fdbee693 | 2752 | #endif |
a362a82f | 2753 | |
4d4682ef | 2754 | Map entitlements; |
8afb9766 | 2755 | Map requirements; |
30c64adb | 2756 | Map key; |
a0c715e9 | 2757 | ldid::Slots slots; |
c05d9758 | 2758 | |
a362a82f JF |
2759 | std::vector<std::string> files; |
2760 | ||
a960f392 JF |
2761 | if (argc == 1) { |
2762 | fprintf(stderr, "usage: %s -S[entitlements.xml] <binary>\n", argv[0]); | |
9c83be90 | 2763 | fprintf(stderr, " %s -e MobileSafari\n", argv[0]); |
a960f392 JF |
2764 | fprintf(stderr, " %s -S cat\n", argv[0]); |
2765 | fprintf(stderr, " %s -Stfp.xml gdb\n", argv[0]); | |
2766 | exit(0); | |
2767 | } | |
2768 | ||
a362a82f JF |
2769 | for (int argi(1); argi != argc; ++argi) |
2770 | if (argv[argi][0] != '-') | |
2771 | files.push_back(argv[argi]); | |
2772 | else switch (argv[argi][1]) { | |
fac3d3e3 JF |
2773 | case 'r': |
2774 | _assert(!flag_s); | |
2775 | _assert(!flag_S); | |
2776 | flag_r = true; | |
2777 | break; | |
2778 | ||
9c83be90 | 2779 | case 'e': flag_e = true; break; |
c05d9758 | 2780 | |
e57b1f91 | 2781 | case 'E': { |
f74652ab JF |
2782 | const char *string = argv[argi] + 2; |
2783 | const char *colon = strchr(string, ':'); | |
e57b1f91 JF |
2784 | _assert(colon != NULL); |
2785 | Map file(colon + 1, O_RDONLY, PROT_READ, MAP_PRIVATE); | |
2786 | char *arge; | |
f74652ab | 2787 | unsigned number(strtoul(string, &arge, 0)); |
e57b1f91 | 2788 | _assert(arge == colon); |
f74652ab JF |
2789 | auto &slot(slots[number]); |
2790 | for (Algorithm *algorithm : GetAlgorithms()) | |
2791 | (*algorithm)(slot, file.data(), file.size()); | |
e57b1f91 JF |
2792 | } break; |
2793 | ||
9914b1b3 JF |
2794 | case 'q': flag_q = true; break; |
2795 | ||
2796 | case 'Q': { | |
2797 | const char *xml = argv[argi] + 2; | |
8afb9766 | 2798 | requirements.open(xml, O_RDONLY, PROT_READ, MAP_PRIVATE); |
9914b1b3 JF |
2799 | } break; |
2800 | ||
c0cc1574 | 2801 | case 'D': flag_D = true; break; |
c0cc1574 | 2802 | |
8c417842 JF |
2803 | case 'a': flag_a = true; break; |
2804 | ||
2805 | case 'A': | |
fac3d3e3 | 2806 | _assert(!flag_A); |
8c417842 JF |
2807 | flag_A = true; |
2808 | if (argv[argi][2] != '\0') { | |
2809 | const char *cpu = argv[argi] + 2; | |
2810 | const char *colon = strchr(cpu, ':'); | |
2811 | _assert(colon != NULL); | |
2812 | char *arge; | |
2813 | flag_CPUType = strtoul(cpu, &arge, 0); | |
2814 | _assert(arge == colon); | |
2815 | flag_CPUSubtype = strtoul(colon + 1, &arge, 0); | |
2816 | _assert(arge == argv[argi] + strlen(argv[argi])); | |
2817 | } | |
2818 | break; | |
2819 | ||
296fd38e JF |
2820 | case 'C': { |
2821 | const char *name = argv[argi] + 2; | |
2822 | if (false); | |
2823 | else if (strcmp(name, "host") == 0) | |
2824 | flags |= kSecCodeSignatureHost; | |
2825 | else if (strcmp(name, "adhoc") == 0) | |
2826 | flags |= kSecCodeSignatureAdhoc; | |
2827 | else if (strcmp(name, "hard") == 0) | |
2828 | flags |= kSecCodeSignatureForceHard; | |
2829 | else if (strcmp(name, "kill") == 0) | |
2830 | flags |= kSecCodeSignatureForceKill; | |
2831 | else if (strcmp(name, "expires") == 0) | |
2832 | flags |= kSecCodeSignatureForceExpiration; | |
2833 | else if (strcmp(name, "restrict") == 0) | |
2834 | flags |= kSecCodeSignatureRestrict; | |
2835 | else if (strcmp(name, "enforcement") == 0) | |
2836 | flags |= kSecCodeSignatureEnforcement; | |
2837 | else if (strcmp(name, "library-validation") == 0) | |
2838 | flags |= kSecCodeSignatureLibraryValidation; | |
2839 | else _assert(false); | |
2840 | } break; | |
2841 | ||
2842 | case 'P': | |
2843 | platform = true; | |
2844 | break; | |
2845 | ||
20c5f1e8 | 2846 | case 's': |
fac3d3e3 | 2847 | _assert(!flag_r); |
20c5f1e8 JF |
2848 | _assert(!flag_S); |
2849 | flag_s = true; | |
2850 | break; | |
2851 | ||
c05d9758 | 2852 | case 'S': |
fac3d3e3 | 2853 | _assert(!flag_r); |
20c5f1e8 | 2854 | _assert(!flag_s); |
c05d9758 JF |
2855 | flag_S = true; |
2856 | if (argv[argi][2] != '\0') { | |
2857 | const char *xml = argv[argi] + 2; | |
4d4682ef | 2858 | entitlements.open(xml, O_RDONLY, PROT_READ, MAP_PRIVATE); |
c05d9758 JF |
2859 | } |
2860 | break; | |
a362a82f | 2861 | |
30c64adb | 2862 | case 'K': |
d73a3d58 JF |
2863 | if (argv[argi][2] != '\0') |
2864 | key.open(argv[argi] + 2, O_RDONLY, PROT_READ, MAP_PRIVATE); | |
30c64adb JF |
2865 | break; |
2866 | ||
fdbee693 | 2867 | #ifndef LDID_NOFLAGT |
a362a82f JF |
2868 | case 'T': { |
2869 | flag_T = true; | |
2870 | if (argv[argi][2] == '-') | |
2871 | timeh = true; | |
2872 | else { | |
2873 | char *arge; | |
2874 | timev = strtoul(argv[argi] + 2, &arge, 0); | |
2875 | _assert(arge == argv[argi] + strlen(argv[argi])); | |
2876 | } | |
2877 | } break; | |
fdbee693 | 2878 | #endif |
a362a82f | 2879 | |
56f0487d JF |
2880 | case 'u': { |
2881 | flag_u = true; | |
2882 | } break; | |
2883 | ||
5d7ceb5d JF |
2884 | case 'I': { |
2885 | flag_I = argv[argi] + 2; | |
2886 | } break; | |
2887 | ||
a362a82f JF |
2888 | default: |
2889 | goto usage; | |
2890 | break; | |
2891 | } | |
2892 | ||
fac3d3e3 JF |
2893 | _assert(flag_S || key.empty()); |
2894 | _assert(flag_S || flag_I == NULL); | |
f4b2df35 | 2895 | |
a362a82f JF |
2896 | if (files.empty()) usage: { |
2897 | exit(0); | |
2898 | } | |
2899 | ||
2900 | size_t filei(0), filee(0); | |
2901 | _foreach (file, files) try { | |
d4187e53 | 2902 | std::string path(file); |
5d7ceb5d | 2903 | |
23c11ee8 JF |
2904 | struct stat info; |
2905 | _syscall(stat(path.c_str(), &info)); | |
8762082f | 2906 | |
23c11ee8 | 2907 | if (S_ISDIR(info.st_mode)) { |
489c97b8 | 2908 | #ifndef LDID_NOPLIST |
23c11ee8 JF |
2909 | _assert(!flag_r); |
2910 | ldid::DiskFolder folder(path); | |
b4b49374 | 2911 | path += "/" + Sign("", folder, key, requirements, ldid::fun([&](const std::string &, const std::string &) -> std::string { return entitlements; }), dummy_).path; |
489c97b8 JF |
2912 | #else |
2913 | _assert(false); | |
2914 | #endif | |
23c11ee8 JF |
2915 | } else if (flag_S || flag_r) { |
2916 | Map input(path, O_RDONLY, PROT_READ, MAP_PRIVATE); | |
8762082f | 2917 | |
4d4682ef | 2918 | std::filebuf output; |
23c11ee8 JF |
2919 | Split split(path); |
2920 | auto temp(Temporary(output, split)); | |
4d4682ef | 2921 | |
e0098838 | 2922 | if (flag_r) |
b4b49374 | 2923 | ldid::Unsign(input.data(), input.size(), output, dummy_); |
8762082f | 2924 | else { |
23c11ee8 | 2925 | std::string identifier(flag_I ?: split.base.c_str()); |
296fd38e | 2926 | ldid::Sign(input.data(), input.size(), output, identifier, entitlements, requirements, key, slots, flags, platform, dummy_); |
8762082f | 2927 | } |
34f4c1d8 | 2928 | |
d4187e53 | 2929 | Commit(path, temp); |
fdb119ef JF |
2930 | } |
2931 | ||
fdbee693 JF |
2932 | bool modify(false); |
2933 | #ifndef LDID_NOFLAGT | |
2934 | if (flag_T) | |
2935 | modify = true; | |
2936 | #endif | |
2937 | if (flag_s) | |
2938 | modify = true; | |
2939 | ||
2940 | Map mapping(path, modify); | |
dede6121 | 2941 | FatHeader fat_header(mapping.data(), mapping.size()); |
a362a82f | 2942 | |
0a033524 | 2943 | _foreach (mach_header, fat_header.GetMachHeaders()) { |
d840a088 | 2944 | struct linkedit_data_command *signature(NULL); |
7b496abd JF |
2945 | struct encryption_info_command *encryption(NULL); |
2946 | ||
8c417842 JF |
2947 | if (flag_A) { |
2948 | if (mach_header.GetCPUType() != flag_CPUType) | |
2949 | continue; | |
2950 | if (mach_header.GetCPUSubtype() != flag_CPUSubtype) | |
2951 | continue; | |
2952 | } | |
2953 | ||
2954 | if (flag_a) | |
2955 | printf("cpu=0x%x:0x%x\n", mach_header.GetCPUType(), mach_header.GetCPUSubtype()); | |
2956 | ||
0a033524 JF |
2957 | _foreach (load_command, mach_header.GetLoadCommands()) { |
2958 | uint32_t cmd(mach_header.Swap(load_command->cmd)); | |
2959 | ||
cad40c43 | 2960 | if (false); |
0a033524 JF |
2961 | else if (cmd == LC_CODE_SIGNATURE) |
2962 | signature = reinterpret_cast<struct linkedit_data_command *>(load_command); | |
843aea8c | 2963 | else if (cmd == LC_ENCRYPTION_INFO || cmd == LC_ENCRYPTION_INFO_64) |
7b496abd | 2964 | encryption = reinterpret_cast<struct encryption_info_command *>(load_command); |
56f0487d JF |
2965 | else if (cmd == LC_LOAD_DYLIB) { |
2966 | volatile struct dylib_command *dylib_command(reinterpret_cast<struct dylib_command *>(load_command)); | |
2967 | const char *name(reinterpret_cast<const char *>(load_command) + mach_header.Swap(dylib_command->dylib.name)); | |
2968 | ||
2969 | if (strcmp(name, "/System/Library/Frameworks/UIKit.framework/UIKit") == 0) { | |
2970 | if (flag_u) { | |
2971 | Version version; | |
2972 | version.value = mach_header.Swap(dylib_command->dylib.current_version); | |
2973 | printf("uikit=%u.%u.%u\n", version.major, version.minor, version.patch); | |
2974 | } | |
2975 | } | |
2976 | } | |
fdbee693 | 2977 | #ifndef LDID_NOFLAGT |
cad40c43 | 2978 | else if (cmd == LC_ID_DYLIB) { |
0a033524 | 2979 | volatile struct dylib_command *dylib_command(reinterpret_cast<struct dylib_command *>(load_command)); |
a362a82f | 2980 | |
0a033524 JF |
2981 | if (flag_T) { |
2982 | uint32_t timed; | |
a362a82f | 2983 | |
0a033524 JF |
2984 | if (!timeh) |
2985 | timed = timev; | |
2986 | else { | |
2987 | dylib_command->dylib.timestamp = 0; | |
2988 | timed = hash(reinterpret_cast<uint8_t *>(mach_header.GetBase()), mach_header.GetSize(), timev); | |
2989 | } | |
a362a82f | 2990 | |
0a033524 JF |
2991 | dylib_command->dylib.timestamp = mach_header.Swap(timed); |
2992 | } | |
7b496abd | 2993 | } |
fdbee693 | 2994 | #endif |
7b496abd JF |
2995 | } |
2996 | ||
7b496abd JF |
2997 | if (flag_D) { |
2998 | _assert(encryption != NULL); | |
2999 | encryption->cryptid = mach_header.Swap(0); | |
a362a82f | 3000 | } |
a362a82f | 3001 | |
0a033524 JF |
3002 | if (flag_e) { |
3003 | _assert(signature != NULL); | |
9c83be90 | 3004 | |
0a033524 | 3005 | uint32_t data = mach_header.Swap(signature->dataoff); |
9c83be90 | 3006 | |
0a033524 JF |
3007 | uint8_t *top = reinterpret_cast<uint8_t *>(mach_header.GetBase()); |
3008 | uint8_t *blob = top + data; | |
3009 | struct SuperBlob *super = reinterpret_cast<struct SuperBlob *>(blob); | |
9c83be90 | 3010 | |
0a033524 JF |
3011 | for (size_t index(0); index != Swap(super->count); ++index) |
3012 | if (Swap(super->index[index].type) == CSSLOT_ENTITLEMENTS) { | |
3013 | uint32_t begin = Swap(super->index[index].offset); | |
3014 | struct Blob *entitlements = reinterpret_cast<struct Blob *>(blob + begin); | |
b9652d6e | 3015 | fwrite(entitlements + 1, 1, Swap(entitlements->length) - sizeof(*entitlements), stdout); |
0a033524 JF |
3016 | } |
3017 | } | |
9c83be90 | 3018 | |
9914b1b3 JF |
3019 | if (flag_q) { |
3020 | _assert(signature != NULL); | |
3021 | ||
3022 | uint32_t data = mach_header.Swap(signature->dataoff); | |
3023 | ||
3024 | uint8_t *top = reinterpret_cast<uint8_t *>(mach_header.GetBase()); | |
3025 | uint8_t *blob = top + data; | |
3026 | struct SuperBlob *super = reinterpret_cast<struct SuperBlob *>(blob); | |
3027 | ||
3028 | for (size_t index(0); index != Swap(super->count); ++index) | |
3029 | if (Swap(super->index[index].type) == CSSLOT_REQUIREMENTS) { | |
3030 | uint32_t begin = Swap(super->index[index].offset); | |
3031 | struct Blob *requirement = reinterpret_cast<struct Blob *>(blob + begin); | |
3032 | fwrite(requirement, 1, Swap(requirement->length), stdout); | |
3033 | } | |
3034 | } | |
3035 | ||
0a033524 JF |
3036 | if (flag_s) { |
3037 | _assert(signature != NULL); | |
20c5f1e8 | 3038 | |
0a033524 | 3039 | uint32_t data = mach_header.Swap(signature->dataoff); |
20c5f1e8 | 3040 | |
0a033524 JF |
3041 | uint8_t *top = reinterpret_cast<uint8_t *>(mach_header.GetBase()); |
3042 | uint8_t *blob = top + data; | |
3043 | struct SuperBlob *super = reinterpret_cast<struct SuperBlob *>(blob); | |
20c5f1e8 | 3044 | |
0a033524 JF |
3045 | for (size_t index(0); index != Swap(super->count); ++index) |
3046 | if (Swap(super->index[index].type) == CSSLOT_CODEDIRECTORY) { | |
3047 | uint32_t begin = Swap(super->index[index].offset); | |
b2e6a299 | 3048 | struct CodeDirectory *directory = reinterpret_cast<struct CodeDirectory *>(blob + begin + sizeof(Blob)); |
20c5f1e8 | 3049 | |
84d29a1c | 3050 | uint8_t (*hashes)[LDID_SHA1_DIGEST_LENGTH] = reinterpret_cast<uint8_t (*)[LDID_SHA1_DIGEST_LENGTH]>(blob + begin + Swap(directory->hashOffset)); |
0a033524 | 3051 | uint32_t pages = Swap(directory->nCodeSlots); |
20c5f1e8 | 3052 | |
0a033524 JF |
3053 | if (pages != 1) |
3054 | for (size_t i = 0; i != pages - 1; ++i) | |
f74652ab | 3055 | LDID_SHA1(top + PageSize_ * i, PageSize_, hashes[i]); |
0a033524 | 3056 | if (pages != 0) |
f74652ab | 3057 | LDID_SHA1(top + PageSize_ * (pages - 1), ((data - 1) % PageSize_) + 1, hashes[pages - 1]); |
0a033524 JF |
3058 | } |
3059 | } | |
fdb119ef JF |
3060 | } |
3061 | ||
a362a82f JF |
3062 | ++filei; |
3063 | } catch (const char *) { | |
3064 | ++filee; | |
3065 | ++filei; | |
3066 | } | |
3067 | ||
3068 | return filee; | |
3069 | } | |
0326ab11 | 3070 | #endif |