]>
Commit | Line | Data |
---|---|---|
1 | /* ldid - (Mach-O) Link-Loader Identity Editor | |
2 | * Copyright (C) 2007-2015 Jay Freeman (saurik) | |
3 | */ | |
4 | ||
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 | |
9 | * the Free Software Foundation, either version 3 of the License, or | |
10 | * (at your option) any later version. | |
11 | ||
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 | |
15 | * GNU Affero General Public License for more details. | |
16 | ||
17 | * You should have received a copy of the GNU Affero General Public License | |
18 | * along with this program. If not, see <http://www.gnu.org/licenses/>. | |
19 | **/ | |
20 | /* }}} */ | |
21 | ||
22 | #include <cstdio> | |
23 | #include <cstdlib> | |
24 | #include <cstring> | |
25 | #include <fstream> | |
26 | #include <map> | |
27 | #include <sstream> | |
28 | #include <string> | |
29 | #include <vector> | |
30 | ||
31 | #include <errno.h> | |
32 | #include <fcntl.h> | |
33 | #include <stdbool.h> | |
34 | #include <stdint.h> | |
35 | #include <unistd.h> | |
36 | ||
37 | #include <sys/mman.h> | |
38 | #include <sys/stat.h> | |
39 | ||
40 | #include <openssl/err.h> | |
41 | #include <openssl/pem.h> | |
42 | #include <openssl/pkcs7.h> | |
43 | #include <openssl/pkcs12.h> | |
44 | #include <openssl/sha.h> | |
45 | ||
46 | #include <plist/plist.h> | |
47 | ||
48 | #define _assert___(line) \ | |
49 | #line | |
50 | #define _assert__(line) \ | |
51 | _assert___(line) | |
52 | #define _assert_(e) \ | |
53 | throw __FILE__ "(" _assert__(__LINE__) "): _assert(" e ")" | |
54 | ||
55 | #define _assert(expr) \ | |
56 | do if (!(expr)) { \ | |
57 | fprintf(stderr, "%s(%u): _assert(%s); errno=%u\n", __FILE__, __LINE__, #expr, errno); \ | |
58 | _assert_(#expr); \ | |
59 | } while (false) | |
60 | ||
61 | #define _syscall(expr) ({ \ | |
62 | __typeof__(expr) _value; \ | |
63 | do if ((long) (_value = (expr)) != -1) \ | |
64 | break; \ | |
65 | else switch (errno) { \ | |
66 | case EINTR: \ | |
67 | continue; \ | |
68 | default: \ | |
69 | _assert(false); \ | |
70 | } while (true); \ | |
71 | _value; \ | |
72 | }) | |
73 | ||
74 | #define _trace() \ | |
75 | fprintf(stderr, "_trace(%s:%u): %s\n", __FILE__, __LINE__, __FUNCTION__) | |
76 | ||
77 | #define _not(type) \ | |
78 | ((type) ~ (type) 0) | |
79 | ||
80 | #define _packed \ | |
81 | __attribute__((packed)) | |
82 | ||
83 | template <typename Type_> | |
84 | struct Iterator_ { | |
85 | typedef typename Type_::const_iterator Result; | |
86 | }; | |
87 | ||
88 | #define _foreach(item, list) \ | |
89 | for (bool _stop(true); _stop; ) \ | |
90 | for (const __typeof__(list) &_list = (list); _stop; _stop = false) \ | |
91 | for (Iterator_<__typeof__(list)>::Result _item = _list.begin(); _item != _list.end(); ++_item) \ | |
92 | for (bool _suck(true); _suck; _suck = false) \ | |
93 | for (const __typeof__(*_item) &item = *_item; _suck; _suck = false) | |
94 | ||
95 | struct fat_header { | |
96 | uint32_t magic; | |
97 | uint32_t nfat_arch; | |
98 | } _packed; | |
99 | ||
100 | #define FAT_MAGIC 0xcafebabe | |
101 | #define FAT_CIGAM 0xbebafeca | |
102 | ||
103 | struct fat_arch { | |
104 | uint32_t cputype; | |
105 | uint32_t cpusubtype; | |
106 | uint32_t offset; | |
107 | uint32_t size; | |
108 | uint32_t align; | |
109 | } _packed; | |
110 | ||
111 | struct mach_header { | |
112 | uint32_t magic; | |
113 | uint32_t cputype; | |
114 | uint32_t cpusubtype; | |
115 | uint32_t filetype; | |
116 | uint32_t ncmds; | |
117 | uint32_t sizeofcmds; | |
118 | uint32_t flags; | |
119 | } _packed; | |
120 | ||
121 | #define MH_MAGIC 0xfeedface | |
122 | #define MH_CIGAM 0xcefaedfe | |
123 | ||
124 | #define MH_MAGIC_64 0xfeedfacf | |
125 | #define MH_CIGAM_64 0xcffaedfe | |
126 | ||
127 | #define MH_DYLDLINK 0x4 | |
128 | ||
129 | #define MH_OBJECT 0x1 | |
130 | #define MH_EXECUTE 0x2 | |
131 | #define MH_DYLIB 0x6 | |
132 | #define MH_BUNDLE 0x8 | |
133 | #define MH_DYLIB_STUB 0x9 | |
134 | ||
135 | struct load_command { | |
136 | uint32_t cmd; | |
137 | uint32_t cmdsize; | |
138 | } _packed; | |
139 | ||
140 | #define LC_REQ_DYLD uint32_t(0x80000000) | |
141 | ||
142 | #define LC_SEGMENT uint32_t(0x01) | |
143 | #define LC_SYMTAB uint32_t(0x02) | |
144 | #define LC_DYSYMTAB uint32_t(0x0b) | |
145 | #define LC_LOAD_DYLIB uint32_t(0x0c) | |
146 | #define LC_ID_DYLIB uint32_t(0x0d) | |
147 | #define LC_SEGMENT_64 uint32_t(0x19) | |
148 | #define LC_UUID uint32_t(0x1b) | |
149 | #define LC_CODE_SIGNATURE uint32_t(0x1d) | |
150 | #define LC_SEGMENT_SPLIT_INFO uint32_t(0x1e) | |
151 | #define LC_REEXPORT_DYLIB uint32_t(0x1f | LC_REQ_DYLD) | |
152 | #define LC_ENCRYPTION_INFO uint32_t(0x21) | |
153 | #define LC_DYLD_INFO uint32_t(0x22) | |
154 | #define LC_DYLD_INFO_ONLY uint32_t(0x22 | LC_REQ_DYLD) | |
155 | #define LC_ENCRYPTION_INFO_64 uint32_t(0x2c) | |
156 | ||
157 | struct dylib { | |
158 | uint32_t name; | |
159 | uint32_t timestamp; | |
160 | uint32_t current_version; | |
161 | uint32_t compatibility_version; | |
162 | } _packed; | |
163 | ||
164 | struct dylib_command { | |
165 | uint32_t cmd; | |
166 | uint32_t cmdsize; | |
167 | struct dylib dylib; | |
168 | } _packed; | |
169 | ||
170 | struct uuid_command { | |
171 | uint32_t cmd; | |
172 | uint32_t cmdsize; | |
173 | uint8_t uuid[16]; | |
174 | } _packed; | |
175 | ||
176 | struct symtab_command { | |
177 | uint32_t cmd; | |
178 | uint32_t cmdsize; | |
179 | uint32_t symoff; | |
180 | uint32_t nsyms; | |
181 | uint32_t stroff; | |
182 | uint32_t strsize; | |
183 | } _packed; | |
184 | ||
185 | struct dyld_info_command { | |
186 | uint32_t cmd; | |
187 | uint32_t cmdsize; | |
188 | uint32_t rebase_off; | |
189 | uint32_t rebase_size; | |
190 | uint32_t bind_off; | |
191 | uint32_t bind_size; | |
192 | uint32_t weak_bind_off; | |
193 | uint32_t weak_bind_size; | |
194 | uint32_t lazy_bind_off; | |
195 | uint32_t lazy_bind_size; | |
196 | uint32_t export_off; | |
197 | uint32_t export_size; | |
198 | } _packed; | |
199 | ||
200 | struct dysymtab_command { | |
201 | uint32_t cmd; | |
202 | uint32_t cmdsize; | |
203 | uint32_t ilocalsym; | |
204 | uint32_t nlocalsym; | |
205 | uint32_t iextdefsym; | |
206 | uint32_t nextdefsym; | |
207 | uint32_t iundefsym; | |
208 | uint32_t nundefsym; | |
209 | uint32_t tocoff; | |
210 | uint32_t ntoc; | |
211 | uint32_t modtaboff; | |
212 | uint32_t nmodtab; | |
213 | uint32_t extrefsymoff; | |
214 | uint32_t nextrefsyms; | |
215 | uint32_t indirectsymoff; | |
216 | uint32_t nindirectsyms; | |
217 | uint32_t extreloff; | |
218 | uint32_t nextrel; | |
219 | uint32_t locreloff; | |
220 | uint32_t nlocrel; | |
221 | } _packed; | |
222 | ||
223 | struct dylib_table_of_contents { | |
224 | uint32_t symbol_index; | |
225 | uint32_t module_index; | |
226 | } _packed; | |
227 | ||
228 | struct dylib_module { | |
229 | uint32_t module_name; | |
230 | uint32_t iextdefsym; | |
231 | uint32_t nextdefsym; | |
232 | uint32_t irefsym; | |
233 | uint32_t nrefsym; | |
234 | uint32_t ilocalsym; | |
235 | uint32_t nlocalsym; | |
236 | uint32_t iextrel; | |
237 | uint32_t nextrel; | |
238 | uint32_t iinit_iterm; | |
239 | uint32_t ninit_nterm; | |
240 | uint32_t objc_module_info_addr; | |
241 | uint32_t objc_module_info_size; | |
242 | } _packed; | |
243 | ||
244 | struct dylib_reference { | |
245 | uint32_t isym:24; | |
246 | uint32_t flags:8; | |
247 | } _packed; | |
248 | ||
249 | struct relocation_info { | |
250 | int32_t r_address; | |
251 | uint32_t r_symbolnum:24; | |
252 | uint32_t r_pcrel:1; | |
253 | uint32_t r_length:2; | |
254 | uint32_t r_extern:1; | |
255 | uint32_t r_type:4; | |
256 | } _packed; | |
257 | ||
258 | struct nlist { | |
259 | union { | |
260 | char *n_name; | |
261 | int32_t n_strx; | |
262 | } n_un; | |
263 | ||
264 | uint8_t n_type; | |
265 | uint8_t n_sect; | |
266 | uint8_t n_desc; | |
267 | uint32_t n_value; | |
268 | } _packed; | |
269 | ||
270 | struct segment_command { | |
271 | uint32_t cmd; | |
272 | uint32_t cmdsize; | |
273 | char segname[16]; | |
274 | uint32_t vmaddr; | |
275 | uint32_t vmsize; | |
276 | uint32_t fileoff; | |
277 | uint32_t filesize; | |
278 | uint32_t maxprot; | |
279 | uint32_t initprot; | |
280 | uint32_t nsects; | |
281 | uint32_t flags; | |
282 | } _packed; | |
283 | ||
284 | struct segment_command_64 { | |
285 | uint32_t cmd; | |
286 | uint32_t cmdsize; | |
287 | char segname[16]; | |
288 | uint64_t vmaddr; | |
289 | uint64_t vmsize; | |
290 | uint64_t fileoff; | |
291 | uint64_t filesize; | |
292 | uint32_t maxprot; | |
293 | uint32_t initprot; | |
294 | uint32_t nsects; | |
295 | uint32_t flags; | |
296 | } _packed; | |
297 | ||
298 | struct section { | |
299 | char sectname[16]; | |
300 | char segname[16]; | |
301 | uint32_t addr; | |
302 | uint32_t size; | |
303 | uint32_t offset; | |
304 | uint32_t align; | |
305 | uint32_t reloff; | |
306 | uint32_t nreloc; | |
307 | uint32_t flags; | |
308 | uint32_t reserved1; | |
309 | uint32_t reserved2; | |
310 | } _packed; | |
311 | ||
312 | struct section_64 { | |
313 | char sectname[16]; | |
314 | char segname[16]; | |
315 | uint64_t addr; | |
316 | uint64_t size; | |
317 | uint32_t offset; | |
318 | uint32_t align; | |
319 | uint32_t reloff; | |
320 | uint32_t nreloc; | |
321 | uint32_t flags; | |
322 | uint32_t reserved1; | |
323 | uint32_t reserved2; | |
324 | } _packed; | |
325 | ||
326 | struct linkedit_data_command { | |
327 | uint32_t cmd; | |
328 | uint32_t cmdsize; | |
329 | uint32_t dataoff; | |
330 | uint32_t datasize; | |
331 | } _packed; | |
332 | ||
333 | struct encryption_info_command { | |
334 | uint32_t cmd; | |
335 | uint32_t cmdsize; | |
336 | uint32_t cryptoff; | |
337 | uint32_t cryptsize; | |
338 | uint32_t cryptid; | |
339 | } _packed; | |
340 | ||
341 | #define BIND_OPCODE_MASK 0xf0 | |
342 | #define BIND_IMMEDIATE_MASK 0x0f | |
343 | #define BIND_OPCODE_DONE 0x00 | |
344 | #define BIND_OPCODE_SET_DYLIB_ORDINAL_IMM 0x10 | |
345 | #define BIND_OPCODE_SET_DYLIB_ORDINAL_ULEB 0x20 | |
346 | #define BIND_OPCODE_SET_DYLIB_SPECIAL_IMM 0x30 | |
347 | #define BIND_OPCODE_SET_SYMBOL_TRAILING_FLAGS_IMM 0x40 | |
348 | #define BIND_OPCODE_SET_TYPE_IMM 0x50 | |
349 | #define BIND_OPCODE_SET_ADDEND_SLEB 0x60 | |
350 | #define BIND_OPCODE_SET_SEGMENT_AND_OFFSET_ULEB 0x70 | |
351 | #define BIND_OPCODE_ADD_ADDR_ULEB 0x80 | |
352 | #define BIND_OPCODE_DO_BIND 0x90 | |
353 | #define BIND_OPCODE_DO_BIND_ADD_ADDR_ULEB 0xa0 | |
354 | #define BIND_OPCODE_DO_BIND_ADD_ADDR_IMM_SCALED 0xb0 | |
355 | #define BIND_OPCODE_DO_BIND_ULEB_TIMES_SKIPPING_ULEB 0xc0 | |
356 | ||
357 | inline void get(std::streambuf &stream, void *data, size_t size) { | |
358 | _assert(stream.sgetn(static_cast<char *>(data), size) == size); | |
359 | } | |
360 | ||
361 | inline void put(std::streambuf &stream, const void *data, size_t size) { | |
362 | _assert(stream.sputn(static_cast<const char *>(data), size) == size); | |
363 | } | |
364 | ||
365 | inline void pad(std::streambuf &stream, size_t size) { | |
366 | char padding[size]; | |
367 | memset(padding, 0, size); | |
368 | put(stream, padding, size); | |
369 | } | |
370 | ||
371 | template <typename Type_> | |
372 | Type_ Align(Type_ value, size_t align) { | |
373 | value += align - 1; | |
374 | value /= align; | |
375 | value *= align; | |
376 | return value; | |
377 | } | |
378 | ||
379 | static const uint8_t PageShift_(0x0c); | |
380 | static const uint32_t PageSize_(1 << PageShift_); | |
381 | ||
382 | static inline uint16_t Swap_(uint16_t value) { | |
383 | return | |
384 | ((value >> 8) & 0x00ff) | | |
385 | ((value << 8) & 0xff00); | |
386 | } | |
387 | ||
388 | static inline uint32_t Swap_(uint32_t value) { | |
389 | value = ((value >> 8) & 0x00ff00ff) | | |
390 | ((value << 8) & 0xff00ff00); | |
391 | value = ((value >> 16) & 0x0000ffff) | | |
392 | ((value << 16) & 0xffff0000); | |
393 | return value; | |
394 | } | |
395 | ||
396 | static inline uint64_t Swap_(uint64_t value) { | |
397 | value = (value & 0x00000000ffffffff) << 32 | (value & 0xffffffff00000000) >> 32; | |
398 | value = (value & 0x0000ffff0000ffff) << 16 | (value & 0xffff0000ffff0000) >> 16; | |
399 | value = (value & 0x00ff00ff00ff00ff) << 8 | (value & 0xff00ff00ff00ff00) >> 8; | |
400 | return value; | |
401 | } | |
402 | ||
403 | static inline int16_t Swap_(int16_t value) { | |
404 | return Swap_(static_cast<uint16_t>(value)); | |
405 | } | |
406 | ||
407 | static inline int32_t Swap_(int32_t value) { | |
408 | return Swap_(static_cast<uint32_t>(value)); | |
409 | } | |
410 | ||
411 | static inline int64_t Swap_(int64_t value) { | |
412 | return Swap_(static_cast<uint64_t>(value)); | |
413 | } | |
414 | ||
415 | static bool little_(true); | |
416 | ||
417 | static inline uint16_t Swap(uint16_t value) { | |
418 | return little_ ? Swap_(value) : value; | |
419 | } | |
420 | ||
421 | static inline uint32_t Swap(uint32_t value) { | |
422 | return little_ ? Swap_(value) : value; | |
423 | } | |
424 | ||
425 | static inline uint64_t Swap(uint64_t value) { | |
426 | return little_ ? Swap_(value) : value; | |
427 | } | |
428 | ||
429 | static inline int16_t Swap(int16_t value) { | |
430 | return Swap(static_cast<uint16_t>(value)); | |
431 | } | |
432 | ||
433 | static inline int32_t Swap(int32_t value) { | |
434 | return Swap(static_cast<uint32_t>(value)); | |
435 | } | |
436 | ||
437 | static inline int64_t Swap(int64_t value) { | |
438 | return Swap(static_cast<uint64_t>(value)); | |
439 | } | |
440 | ||
441 | template <typename Target_> | |
442 | class Pointer; | |
443 | ||
444 | class Swapped { | |
445 | protected: | |
446 | bool swapped_; | |
447 | ||
448 | Swapped() : | |
449 | swapped_(false) | |
450 | { | |
451 | } | |
452 | ||
453 | public: | |
454 | Swapped(bool swapped) : | |
455 | swapped_(swapped) | |
456 | { | |
457 | } | |
458 | ||
459 | template <typename Type_> | |
460 | Type_ Swap(Type_ value) const { | |
461 | return swapped_ ? Swap_(value) : value; | |
462 | } | |
463 | }; | |
464 | ||
465 | class Data : | |
466 | public Swapped | |
467 | { | |
468 | private: | |
469 | void *base_; | |
470 | size_t size_; | |
471 | ||
472 | public: | |
473 | Data(void *base, size_t size) : | |
474 | base_(base), | |
475 | size_(size) | |
476 | { | |
477 | } | |
478 | ||
479 | void *GetBase() const { | |
480 | return base_; | |
481 | } | |
482 | ||
483 | size_t GetSize() const { | |
484 | return size_; | |
485 | } | |
486 | }; | |
487 | ||
488 | class MachHeader : | |
489 | public Data | |
490 | { | |
491 | private: | |
492 | bool bits64_; | |
493 | ||
494 | struct mach_header *mach_header_; | |
495 | struct load_command *load_command_; | |
496 | ||
497 | public: | |
498 | MachHeader(void *base, size_t size) : | |
499 | Data(base, size) | |
500 | { | |
501 | mach_header_ = (mach_header *) base; | |
502 | ||
503 | switch (Swap(mach_header_->magic)) { | |
504 | case MH_CIGAM: | |
505 | swapped_ = !swapped_; | |
506 | case MH_MAGIC: | |
507 | bits64_ = false; | |
508 | break; | |
509 | ||
510 | case MH_CIGAM_64: | |
511 | swapped_ = !swapped_; | |
512 | case MH_MAGIC_64: | |
513 | bits64_ = true; | |
514 | break; | |
515 | ||
516 | default: | |
517 | _assert(false); | |
518 | } | |
519 | ||
520 | void *post = mach_header_ + 1; | |
521 | if (bits64_) | |
522 | post = (uint32_t *) post + 1; | |
523 | load_command_ = (struct load_command *) post; | |
524 | ||
525 | _assert( | |
526 | Swap(mach_header_->filetype) == MH_EXECUTE || | |
527 | Swap(mach_header_->filetype) == MH_DYLIB || | |
528 | Swap(mach_header_->filetype) == MH_BUNDLE | |
529 | ); | |
530 | } | |
531 | ||
532 | bool Bits64() const { | |
533 | return bits64_; | |
534 | } | |
535 | ||
536 | struct mach_header *operator ->() const { | |
537 | return mach_header_; | |
538 | } | |
539 | ||
540 | operator struct mach_header *() const { | |
541 | return mach_header_; | |
542 | } | |
543 | ||
544 | uint32_t GetCPUType() const { | |
545 | return Swap(mach_header_->cputype); | |
546 | } | |
547 | ||
548 | uint32_t GetCPUSubtype() const { | |
549 | return Swap(mach_header_->cpusubtype) & 0xff; | |
550 | } | |
551 | ||
552 | struct load_command *GetLoadCommand() const { | |
553 | return load_command_; | |
554 | } | |
555 | ||
556 | std::vector<struct load_command *> GetLoadCommands() const { | |
557 | std::vector<struct load_command *> load_commands; | |
558 | ||
559 | struct load_command *load_command = load_command_; | |
560 | for (uint32_t cmd = 0; cmd != Swap(mach_header_->ncmds); ++cmd) { | |
561 | load_commands.push_back(load_command); | |
562 | load_command = (struct load_command *) ((uint8_t *) load_command + Swap(load_command->cmdsize)); | |
563 | } | |
564 | ||
565 | return load_commands; | |
566 | } | |
567 | ||
568 | std::vector<segment_command *> GetSegments(const char *segment_name) const { | |
569 | std::vector<struct segment_command *> segment_commands; | |
570 | ||
571 | _foreach (load_command, GetLoadCommands()) { | |
572 | if (Swap(load_command->cmd) == LC_SEGMENT) { | |
573 | segment_command *segment_command = reinterpret_cast<struct segment_command *>(load_command); | |
574 | if (strncmp(segment_command->segname, segment_name, 16) == 0) | |
575 | segment_commands.push_back(segment_command); | |
576 | } | |
577 | } | |
578 | ||
579 | return segment_commands; | |
580 | } | |
581 | ||
582 | std::vector<segment_command_64 *> GetSegments64(const char *segment_name) const { | |
583 | std::vector<struct segment_command_64 *> segment_commands; | |
584 | ||
585 | _foreach (load_command, GetLoadCommands()) { | |
586 | if (Swap(load_command->cmd) == LC_SEGMENT_64) { | |
587 | segment_command_64 *segment_command = reinterpret_cast<struct segment_command_64 *>(load_command); | |
588 | if (strncmp(segment_command->segname, segment_name, 16) == 0) | |
589 | segment_commands.push_back(segment_command); | |
590 | } | |
591 | } | |
592 | ||
593 | return segment_commands; | |
594 | } | |
595 | ||
596 | std::vector<section *> GetSections(const char *segment_name, const char *section_name) const { | |
597 | std::vector<section *> sections; | |
598 | ||
599 | _foreach (segment, GetSegments(segment_name)) { | |
600 | section *section = (struct section *) (segment + 1); | |
601 | ||
602 | uint32_t sect; | |
603 | for (sect = 0; sect != Swap(segment->nsects); ++sect) { | |
604 | if (strncmp(section->sectname, section_name, 16) == 0) | |
605 | sections.push_back(section); | |
606 | ++section; | |
607 | } | |
608 | } | |
609 | ||
610 | return sections; | |
611 | } | |
612 | ||
613 | template <typename Target_> | |
614 | Pointer<Target_> GetPointer(uint32_t address, const char *segment_name = NULL) const { | |
615 | load_command *load_command = (struct load_command *) (mach_header_ + 1); | |
616 | uint32_t cmd; | |
617 | ||
618 | for (cmd = 0; cmd != Swap(mach_header_->ncmds); ++cmd) { | |
619 | if (Swap(load_command->cmd) == LC_SEGMENT) { | |
620 | segment_command *segment_command = (struct segment_command *) load_command; | |
621 | if (segment_name != NULL && strncmp(segment_command->segname, segment_name, 16) != 0) | |
622 | goto next_command; | |
623 | ||
624 | section *sections = (struct section *) (segment_command + 1); | |
625 | ||
626 | uint32_t sect; | |
627 | for (sect = 0; sect != Swap(segment_command->nsects); ++sect) { | |
628 | section *section = §ions[sect]; | |
629 | //printf("%s %u %p %p %u\n", segment_command->segname, sect, address, section->addr, section->size); | |
630 | if (address >= Swap(section->addr) && address < Swap(section->addr) + Swap(section->size)) { | |
631 | //printf("0x%.8x %s\n", address, segment_command->segname); | |
632 | return Pointer<Target_>(this, reinterpret_cast<Target_ *>(address - Swap(section->addr) + Swap(section->offset) + (char *) mach_header_)); | |
633 | } | |
634 | } | |
635 | } | |
636 | ||
637 | next_command: | |
638 | load_command = (struct load_command *) ((char *) load_command + Swap(load_command->cmdsize)); | |
639 | } | |
640 | ||
641 | return Pointer<Target_>(this); | |
642 | } | |
643 | ||
644 | template <typename Target_> | |
645 | Pointer<Target_> GetOffset(uint32_t offset) { | |
646 | return Pointer<Target_>(this, reinterpret_cast<Target_ *>(offset + (uint8_t *) mach_header_)); | |
647 | } | |
648 | }; | |
649 | ||
650 | class FatMachHeader : | |
651 | public MachHeader | |
652 | { | |
653 | private: | |
654 | fat_arch *fat_arch_; | |
655 | ||
656 | public: | |
657 | FatMachHeader(void *base, size_t size, fat_arch *fat_arch) : | |
658 | MachHeader(base, size), | |
659 | fat_arch_(fat_arch) | |
660 | { | |
661 | } | |
662 | ||
663 | fat_arch *GetFatArch() const { | |
664 | return fat_arch_; | |
665 | } | |
666 | }; | |
667 | ||
668 | class FatHeader : | |
669 | public Data | |
670 | { | |
671 | private: | |
672 | fat_header *fat_header_; | |
673 | std::vector<FatMachHeader> mach_headers_; | |
674 | ||
675 | public: | |
676 | FatHeader(void *base, size_t size) : | |
677 | Data(base, size) | |
678 | { | |
679 | fat_header_ = reinterpret_cast<struct fat_header *>(base); | |
680 | ||
681 | if (Swap(fat_header_->magic) == FAT_CIGAM) { | |
682 | swapped_ = !swapped_; | |
683 | goto fat; | |
684 | } else if (Swap(fat_header_->magic) != FAT_MAGIC) { | |
685 | fat_header_ = NULL; | |
686 | mach_headers_.push_back(FatMachHeader(base, size, NULL)); | |
687 | } else fat: { | |
688 | size_t fat_narch = Swap(fat_header_->nfat_arch); | |
689 | fat_arch *fat_arch = reinterpret_cast<struct fat_arch *>(fat_header_ + 1); | |
690 | size_t arch; | |
691 | for (arch = 0; arch != fat_narch; ++arch) { | |
692 | uint32_t arch_offset = Swap(fat_arch->offset); | |
693 | uint32_t arch_size = Swap(fat_arch->size); | |
694 | mach_headers_.push_back(FatMachHeader((uint8_t *) base + arch_offset, arch_size, fat_arch)); | |
695 | ++fat_arch; | |
696 | } | |
697 | } | |
698 | } | |
699 | ||
700 | std::vector<FatMachHeader> &GetMachHeaders() { | |
701 | return mach_headers_; | |
702 | } | |
703 | ||
704 | bool IsFat() const { | |
705 | return fat_header_ != NULL; | |
706 | } | |
707 | ||
708 | struct fat_header *operator ->() const { | |
709 | return fat_header_; | |
710 | } | |
711 | ||
712 | operator struct fat_header *() const { | |
713 | return fat_header_; | |
714 | } | |
715 | }; | |
716 | ||
717 | template <typename Target_> | |
718 | class Pointer { | |
719 | private: | |
720 | const MachHeader *framework_; | |
721 | const Target_ *pointer_; | |
722 | ||
723 | public: | |
724 | Pointer(const MachHeader *framework = NULL, const Target_ *pointer = NULL) : | |
725 | framework_(framework), | |
726 | pointer_(pointer) | |
727 | { | |
728 | } | |
729 | ||
730 | operator const Target_ *() const { | |
731 | return pointer_; | |
732 | } | |
733 | ||
734 | const Target_ *operator ->() const { | |
735 | return pointer_; | |
736 | } | |
737 | ||
738 | Pointer<Target_> &operator ++() { | |
739 | ++pointer_; | |
740 | return *this; | |
741 | } | |
742 | ||
743 | template <typename Value_> | |
744 | Value_ Swap(Value_ value) { | |
745 | return framework_->Swap(value); | |
746 | } | |
747 | }; | |
748 | ||
749 | #define CSMAGIC_REQUIREMENT uint32_t(0xfade0c00) | |
750 | #define CSMAGIC_REQUIREMENTS uint32_t(0xfade0c01) | |
751 | #define CSMAGIC_CODEDIRECTORY uint32_t(0xfade0c02) | |
752 | #define CSMAGIC_EMBEDDED_SIGNATURE uint32_t(0xfade0cc0) | |
753 | #define CSMAGIC_EMBEDDED_SIGNATURE_OLD uint32_t(0xfade0b02) | |
754 | #define CSMAGIC_EMBEDDED_ENTITLEMENTS uint32_t(0xfade7171) | |
755 | #define CSMAGIC_DETACHED_SIGNATURE uint32_t(0xfade0cc1) | |
756 | #define CSMAGIC_BLOBWRAPPER uint32_t(0xfade0b01) | |
757 | ||
758 | #define CSSLOT_CODEDIRECTORY uint32_t(0x00000) | |
759 | #define CSSLOT_INFOSLOT uint32_t(0x00001) | |
760 | #define CSSLOT_REQUIREMENTS uint32_t(0x00002) | |
761 | #define CSSLOT_RESOURCEDIR uint32_t(0x00003) | |
762 | #define CSSLOT_APPLICATION uint32_t(0x00004) | |
763 | #define CSSLOT_ENTITLEMENTS uint32_t(0x00005) | |
764 | ||
765 | #define CSSLOT_SIGNATURESLOT uint32_t(0x10000) | |
766 | ||
767 | #define CS_HASHTYPE_SHA1 1 | |
768 | ||
769 | struct BlobIndex { | |
770 | uint32_t type; | |
771 | uint32_t offset; | |
772 | } _packed; | |
773 | ||
774 | struct Blob { | |
775 | uint32_t magic; | |
776 | uint32_t length; | |
777 | } _packed; | |
778 | ||
779 | struct SuperBlob { | |
780 | struct Blob blob; | |
781 | uint32_t count; | |
782 | struct BlobIndex index[]; | |
783 | } _packed; | |
784 | ||
785 | struct CodeDirectory { | |
786 | uint32_t version; | |
787 | uint32_t flags; | |
788 | uint32_t hashOffset; | |
789 | uint32_t identOffset; | |
790 | uint32_t nSpecialSlots; | |
791 | uint32_t nCodeSlots; | |
792 | uint32_t codeLimit; | |
793 | uint8_t hashSize; | |
794 | uint8_t hashType; | |
795 | uint8_t spare1; | |
796 | uint8_t pageSize; | |
797 | uint32_t spare2; | |
798 | } _packed; | |
799 | ||
800 | extern "C" uint32_t hash(uint8_t *k, uint32_t length, uint32_t initval); | |
801 | ||
802 | static void sha1(uint8_t *hash, const void *data, size_t size) { | |
803 | SHA1(static_cast<const uint8_t *>(data), size, hash); | |
804 | } | |
805 | ||
806 | struct CodesignAllocation { | |
807 | FatMachHeader mach_header_; | |
808 | uint32_t offset_; | |
809 | uint32_t size_; | |
810 | uint32_t limit_; | |
811 | uint32_t alloc_; | |
812 | uint32_t align_; | |
813 | ||
814 | CodesignAllocation(FatMachHeader mach_header, size_t offset, size_t size, size_t limit, size_t alloc, size_t align) : | |
815 | mach_header_(mach_header), | |
816 | offset_(offset), | |
817 | size_(size), | |
818 | limit_(limit), | |
819 | alloc_(alloc), | |
820 | align_(align) | |
821 | { | |
822 | } | |
823 | }; | |
824 | ||
825 | class File { | |
826 | private: | |
827 | int file_; | |
828 | ||
829 | public: | |
830 | File() : | |
831 | file_(-1) | |
832 | { | |
833 | } | |
834 | ||
835 | ~File() { | |
836 | if (file_ != -1) | |
837 | _syscall(close(file_)); | |
838 | } | |
839 | ||
840 | void open(const char *path, int flags) { | |
841 | _assert(file_ == -1); | |
842 | _syscall(file_ = ::open(path, flags)); | |
843 | } | |
844 | ||
845 | int file() const { | |
846 | return file_; | |
847 | } | |
848 | }; | |
849 | ||
850 | class Map { | |
851 | private: | |
852 | File file_; | |
853 | void *data_; | |
854 | size_t size_; | |
855 | ||
856 | void clear() { | |
857 | if (data_ == NULL) | |
858 | return; | |
859 | _syscall(munmap(data_, size_)); | |
860 | data_ = NULL; | |
861 | size_ = 0; | |
862 | } | |
863 | ||
864 | public: | |
865 | Map() : | |
866 | data_(NULL), | |
867 | size_(0) | |
868 | { | |
869 | } | |
870 | ||
871 | Map(const char *path, int oflag, int pflag, int mflag) : | |
872 | Map() | |
873 | { | |
874 | open(path, oflag, pflag, mflag); | |
875 | } | |
876 | ||
877 | Map(const char *path, bool edit) : | |
878 | Map() | |
879 | { | |
880 | open(path, edit); | |
881 | } | |
882 | ||
883 | ~Map() { | |
884 | clear(); | |
885 | } | |
886 | ||
887 | void open(const char *path, int oflag, int pflag, int mflag) { | |
888 | clear(); | |
889 | ||
890 | file_.open(path, oflag); | |
891 | int file(file_.file()); | |
892 | ||
893 | struct stat stat; | |
894 | _syscall(fstat(file, &stat)); | |
895 | size_ = stat.st_size; | |
896 | ||
897 | _syscall(data_ = mmap(NULL, size_, pflag, mflag, file, 0)); | |
898 | } | |
899 | ||
900 | void open(const char *path, bool edit) { | |
901 | if (edit) | |
902 | open(path, O_RDWR, PROT_READ | PROT_WRITE, MAP_SHARED); | |
903 | else | |
904 | open(path, O_RDONLY, PROT_READ, MAP_PRIVATE); | |
905 | } | |
906 | ||
907 | void *data() const { | |
908 | return data_; | |
909 | } | |
910 | ||
911 | size_t size() const { | |
912 | return size_; | |
913 | } | |
914 | ||
915 | operator std::string() const { | |
916 | return std::string(static_cast<char *>(data_), size_); | |
917 | } | |
918 | }; | |
919 | ||
920 | // I wish Apple cared about providing quality toolchains :/ | |
921 | ||
922 | template <typename Function_> | |
923 | class Functor; | |
924 | ||
925 | template <typename Type_, typename... Args_> | |
926 | class Functor<Type_ (Args_...)> { | |
927 | public: | |
928 | virtual Type_ operator ()(Args_... args) const = 0; | |
929 | }; | |
930 | ||
931 | template <typename Function_> | |
932 | class FunctorImpl; | |
933 | ||
934 | template <typename Value_, typename Type_, typename... Args_> | |
935 | class FunctorImpl<Type_ (Value_::*)(Args_...) const> : | |
936 | public Functor<Type_ (Args_...)> | |
937 | { | |
938 | private: | |
939 | const Value_ *value_; | |
940 | ||
941 | public: | |
942 | FunctorImpl() : | |
943 | value_(NULL) | |
944 | { | |
945 | } | |
946 | ||
947 | FunctorImpl(const Value_ &value) : | |
948 | value_(&value) | |
949 | { | |
950 | } | |
951 | ||
952 | virtual Type_ operator ()(Args_... args) const { | |
953 | return (*value_)(args...); | |
954 | } | |
955 | }; | |
956 | ||
957 | template <typename Function_> | |
958 | FunctorImpl<decltype(&Function_::operator())> fun(const Function_ &value) { | |
959 | return value; | |
960 | } | |
961 | ||
962 | static void resign(void *idata, size_t isize, std::streambuf &output, const Functor<size_t (size_t)> &allocate, const Functor<size_t (std::streambuf &output, size_t, const std::string &, const char *)> &save) { | |
963 | FatHeader source(idata, isize); | |
964 | ||
965 | size_t offset(0); | |
966 | if (source.IsFat()) | |
967 | offset += sizeof(fat_header) + sizeof(fat_arch) * source.Swap(source->nfat_arch); | |
968 | ||
969 | std::vector<CodesignAllocation> allocations; | |
970 | _foreach (mach_header, source.GetMachHeaders()) { | |
971 | struct linkedit_data_command *signature(NULL); | |
972 | struct symtab_command *symtab(NULL); | |
973 | ||
974 | _foreach (load_command, mach_header.GetLoadCommands()) { | |
975 | uint32_t cmd(mach_header.Swap(load_command->cmd)); | |
976 | if (false); | |
977 | else if (cmd == LC_CODE_SIGNATURE) | |
978 | signature = reinterpret_cast<struct linkedit_data_command *>(load_command); | |
979 | else if (cmd == LC_SYMTAB) | |
980 | symtab = reinterpret_cast<struct symtab_command *>(load_command); | |
981 | } | |
982 | ||
983 | size_t size; | |
984 | if (signature == NULL) | |
985 | size = mach_header.GetSize(); | |
986 | else { | |
987 | size = mach_header.Swap(signature->dataoff); | |
988 | _assert(size <= mach_header.GetSize()); | |
989 | } | |
990 | ||
991 | if (symtab != NULL) { | |
992 | auto end(mach_header.Swap(symtab->stroff) + mach_header.Swap(symtab->strsize)); | |
993 | _assert(end <= size); | |
994 | _assert(end >= size - 0x10); | |
995 | size = end; | |
996 | } | |
997 | ||
998 | size_t alloc(allocate(size)); | |
999 | ||
1000 | auto *fat_arch(mach_header.GetFatArch()); | |
1001 | uint32_t align(fat_arch == NULL ? 0 : source.Swap(fat_arch->align)); | |
1002 | offset = Align(offset, 1 << align); | |
1003 | ||
1004 | uint32_t limit(size); | |
1005 | if (alloc != 0) | |
1006 | limit = Align(limit, 0x10); | |
1007 | ||
1008 | allocations.push_back(CodesignAllocation(mach_header, offset, size, limit, alloc, align)); | |
1009 | offset += size + alloc; | |
1010 | offset = Align(offset, 0x10); | |
1011 | } | |
1012 | ||
1013 | size_t position(0); | |
1014 | ||
1015 | if (source.IsFat()) { | |
1016 | fat_header fat_header; | |
1017 | fat_header.magic = Swap(FAT_MAGIC); | |
1018 | fat_header.nfat_arch = Swap(uint32_t(allocations.size())); | |
1019 | put(output, &fat_header, sizeof(fat_header)); | |
1020 | position += sizeof(fat_header); | |
1021 | ||
1022 | _foreach (allocation, allocations) { | |
1023 | auto &mach_header(allocation.mach_header_); | |
1024 | ||
1025 | fat_arch fat_arch; | |
1026 | fat_arch.cputype = Swap(mach_header->cputype); | |
1027 | fat_arch.cpusubtype = Swap(mach_header->cpusubtype); | |
1028 | fat_arch.offset = Swap(allocation.offset_); | |
1029 | fat_arch.size = Swap(allocation.limit_ + allocation.alloc_); | |
1030 | fat_arch.align = Swap(allocation.align_); | |
1031 | put(output, &fat_arch, sizeof(fat_arch)); | |
1032 | position += sizeof(fat_arch); | |
1033 | } | |
1034 | } | |
1035 | ||
1036 | _foreach (allocation, allocations) { | |
1037 | auto &mach_header(allocation.mach_header_); | |
1038 | ||
1039 | pad(output, allocation.offset_ - position); | |
1040 | position = allocation.offset_; | |
1041 | ||
1042 | std::vector<std::string> commands; | |
1043 | ||
1044 | _foreach (load_command, mach_header.GetLoadCommands()) { | |
1045 | std::string copy(reinterpret_cast<const char *>(load_command), load_command->cmdsize); | |
1046 | ||
1047 | switch (mach_header.Swap(load_command->cmd)) { | |
1048 | case LC_CODE_SIGNATURE: | |
1049 | continue; | |
1050 | break; | |
1051 | ||
1052 | case LC_SEGMENT: { | |
1053 | auto segment_command(reinterpret_cast<struct segment_command *>(©[0])); | |
1054 | if (strncmp(segment_command->segname, "__LINKEDIT", 16) != 0) | |
1055 | break; | |
1056 | size_t size(mach_header.Swap(allocation.limit_ + allocation.alloc_ - mach_header.Swap(segment_command->fileoff))); | |
1057 | segment_command->filesize = size; | |
1058 | segment_command->vmsize = Align(size, PageSize_); | |
1059 | } break; | |
1060 | ||
1061 | case LC_SEGMENT_64: { | |
1062 | auto segment_command(reinterpret_cast<struct segment_command_64 *>(©[0])); | |
1063 | if (strncmp(segment_command->segname, "__LINKEDIT", 16) != 0) | |
1064 | break; | |
1065 | size_t size(mach_header.Swap(allocation.limit_ + allocation.alloc_ - mach_header.Swap(segment_command->fileoff))); | |
1066 | segment_command->filesize = size; | |
1067 | segment_command->vmsize = Align(size, PageSize_); | |
1068 | } break; | |
1069 | } | |
1070 | ||
1071 | commands.push_back(copy); | |
1072 | } | |
1073 | ||
1074 | if (allocation.alloc_ != 0) { | |
1075 | linkedit_data_command signature; | |
1076 | signature.cmd = mach_header.Swap(LC_CODE_SIGNATURE); | |
1077 | signature.cmdsize = mach_header.Swap(uint32_t(sizeof(signature))); | |
1078 | signature.dataoff = mach_header.Swap(allocation.limit_); | |
1079 | signature.datasize = mach_header.Swap(allocation.alloc_); | |
1080 | commands.push_back(std::string(reinterpret_cast<const char *>(&signature), sizeof(signature))); | |
1081 | } | |
1082 | ||
1083 | size_t begin(position); | |
1084 | ||
1085 | uint32_t after(0); | |
1086 | _foreach(command, commands) | |
1087 | after += command.size(); | |
1088 | ||
1089 | std::stringbuf altern; | |
1090 | ||
1091 | struct mach_header header(*mach_header); | |
1092 | header.ncmds = mach_header.Swap(uint32_t(commands.size())); | |
1093 | header.sizeofcmds = mach_header.Swap(after); | |
1094 | put(output, &header, sizeof(header)); | |
1095 | put(altern, &header, sizeof(header)); | |
1096 | position += sizeof(header); | |
1097 | ||
1098 | if (mach_header.Bits64()) { | |
1099 | auto pad(mach_header.Swap(uint32_t(0))); | |
1100 | put(output, &pad, sizeof(pad)); | |
1101 | put(altern, &pad, sizeof(pad)); | |
1102 | position += sizeof(pad); | |
1103 | } | |
1104 | ||
1105 | _foreach(command, commands) { | |
1106 | put(output, command.data(), command.size()); | |
1107 | put(altern, command.data(), command.size()); | |
1108 | position += command.size(); | |
1109 | } | |
1110 | ||
1111 | uint32_t before(mach_header.Swap(mach_header->sizeofcmds)); | |
1112 | if (before > after) { | |
1113 | pad(output, before - after); | |
1114 | pad(altern, before - after); | |
1115 | position += before - after; | |
1116 | } | |
1117 | ||
1118 | auto top(reinterpret_cast<char *>(mach_header.GetBase())); | |
1119 | ||
1120 | std::string overlap(altern.str()); | |
1121 | overlap.append(top + overlap.size(), Align(overlap.size(), 0x1000) - overlap.size()); | |
1122 | ||
1123 | put(output, top + (position - begin), allocation.size_ - (position - begin)); | |
1124 | position = begin + allocation.size_; | |
1125 | ||
1126 | pad(output, allocation.limit_ - allocation.size_); | |
1127 | position += allocation.limit_ - allocation.size_; | |
1128 | ||
1129 | size_t saved(save(output, allocation.limit_, overlap, top)); | |
1130 | if (allocation.alloc_ > saved) | |
1131 | pad(output, allocation.alloc_ - saved); | |
1132 | position += allocation.alloc_; | |
1133 | } | |
1134 | } | |
1135 | ||
1136 | typedef std::map<uint32_t, std::string> Blobs; | |
1137 | ||
1138 | static void insert(Blobs &blobs, uint32_t slot, const std::stringbuf &buffer) { | |
1139 | auto value(buffer.str()); | |
1140 | std::swap(blobs[slot], value); | |
1141 | } | |
1142 | ||
1143 | static void insert(Blobs &blobs, uint32_t slot, uint32_t magic, const std::stringbuf &buffer) { | |
1144 | auto value(buffer.str()); | |
1145 | Blob blob; | |
1146 | blob.magic = Swap(magic); | |
1147 | blob.length = Swap(uint32_t(sizeof(blob) + value.size())); | |
1148 | value.insert(0, reinterpret_cast<char *>(&blob), sizeof(blob)); | |
1149 | std::swap(blobs[slot], value); | |
1150 | } | |
1151 | ||
1152 | static size_t put(std::streambuf &output, uint32_t magic, const Blobs &blobs) { | |
1153 | size_t total(0); | |
1154 | _foreach (blob, blobs) | |
1155 | total += blob.second.size(); | |
1156 | ||
1157 | struct SuperBlob super; | |
1158 | super.blob.magic = Swap(magic); | |
1159 | super.blob.length = Swap(uint32_t(sizeof(SuperBlob) + blobs.size() * sizeof(BlobIndex) + total)); | |
1160 | super.count = Swap(uint32_t(blobs.size())); | |
1161 | put(output, &super, sizeof(super)); | |
1162 | ||
1163 | size_t offset(sizeof(SuperBlob) + sizeof(BlobIndex) * blobs.size()); | |
1164 | ||
1165 | _foreach (blob, blobs) { | |
1166 | BlobIndex index; | |
1167 | index.type = Swap(blob.first); | |
1168 | index.offset = Swap(uint32_t(offset)); | |
1169 | put(output, &index, sizeof(index)); | |
1170 | offset += blob.second.size(); | |
1171 | } | |
1172 | ||
1173 | _foreach (blob, blobs) | |
1174 | put(output, blob.second.data(), blob.second.size()); | |
1175 | ||
1176 | return offset; | |
1177 | } | |
1178 | ||
1179 | class Buffer { | |
1180 | private: | |
1181 | BIO *bio_; | |
1182 | ||
1183 | public: | |
1184 | Buffer(BIO *bio) : | |
1185 | bio_(bio) | |
1186 | { | |
1187 | _assert(bio_ != NULL); | |
1188 | } | |
1189 | ||
1190 | Buffer() : | |
1191 | bio_(BIO_new(BIO_s_mem())) | |
1192 | { | |
1193 | } | |
1194 | ||
1195 | Buffer(const char *data, size_t size) : | |
1196 | Buffer(BIO_new_mem_buf(const_cast<char *>(data), size)) | |
1197 | { | |
1198 | } | |
1199 | ||
1200 | Buffer(const std::string &data) : | |
1201 | Buffer(data.data(), data.size()) | |
1202 | { | |
1203 | } | |
1204 | ||
1205 | Buffer(PKCS7 *pkcs) : | |
1206 | Buffer() | |
1207 | { | |
1208 | _assert(i2d_PKCS7_bio(bio_, pkcs) != 0); | |
1209 | } | |
1210 | ||
1211 | ~Buffer() { | |
1212 | BIO_free_all(bio_); | |
1213 | } | |
1214 | ||
1215 | operator BIO *() const { | |
1216 | return bio_; | |
1217 | } | |
1218 | ||
1219 | explicit operator std::string() const { | |
1220 | char *data; | |
1221 | auto size(BIO_get_mem_data(bio_, &data)); | |
1222 | return std::string(data, size); | |
1223 | } | |
1224 | }; | |
1225 | ||
1226 | class Stuff { | |
1227 | private: | |
1228 | PKCS12 *value_; | |
1229 | EVP_PKEY *key_; | |
1230 | X509 *cert_; | |
1231 | STACK_OF(X509) *ca_; | |
1232 | ||
1233 | public: | |
1234 | Stuff(BIO *bio) : | |
1235 | value_(d2i_PKCS12_bio(bio, NULL)), | |
1236 | ca_(NULL) | |
1237 | { | |
1238 | _assert(value_ != NULL); | |
1239 | _assert(PKCS12_parse(value_, "", &key_, &cert_, &ca_) != 0); | |
1240 | _assert(key_ != NULL); | |
1241 | _assert(cert_ != NULL); | |
1242 | } | |
1243 | ||
1244 | Stuff(const std::string &data) : | |
1245 | Stuff(Buffer(data)) | |
1246 | { | |
1247 | } | |
1248 | ||
1249 | ~Stuff() { | |
1250 | sk_X509_pop_free(ca_, X509_free); | |
1251 | X509_free(cert_); | |
1252 | EVP_PKEY_free(key_); | |
1253 | PKCS12_free(value_); | |
1254 | } | |
1255 | ||
1256 | operator PKCS12 *() const { | |
1257 | return value_; | |
1258 | } | |
1259 | ||
1260 | operator EVP_PKEY *() const { | |
1261 | return key_; | |
1262 | } | |
1263 | ||
1264 | operator X509 *() const { | |
1265 | return cert_; | |
1266 | } | |
1267 | ||
1268 | operator STACK_OF(X509) *() const { | |
1269 | return ca_; | |
1270 | } | |
1271 | }; | |
1272 | ||
1273 | class Signature { | |
1274 | private: | |
1275 | PKCS7 *value_; | |
1276 | ||
1277 | public: | |
1278 | Signature(const Stuff &stuff, const Buffer &data) : | |
1279 | value_(PKCS7_sign(stuff, stuff, stuff, data, PKCS7_BINARY | PKCS7_DETACHED)) | |
1280 | { | |
1281 | _assert(value_ != NULL); | |
1282 | } | |
1283 | ||
1284 | ~Signature() { | |
1285 | PKCS7_free(value_); | |
1286 | } | |
1287 | ||
1288 | operator PKCS7 *() const { | |
1289 | return value_; | |
1290 | } | |
1291 | }; | |
1292 | ||
1293 | void resign(void *idata, size_t isize, std::streambuf &output, const std::string &name, const std::string &entitlements, const std::string &key) { | |
1294 | resign(idata, isize, output, fun([&](size_t size) -> size_t { | |
1295 | size_t alloc(sizeof(struct SuperBlob)); | |
1296 | ||
1297 | uint32_t special(0); | |
1298 | ||
1299 | special = std::max(special, CSSLOT_REQUIREMENTS); | |
1300 | alloc += sizeof(struct BlobIndex); | |
1301 | alloc += 0xc; | |
1302 | ||
1303 | if (!entitlements.empty()) { | |
1304 | special = std::max(special, CSSLOT_ENTITLEMENTS); | |
1305 | alloc += sizeof(struct BlobIndex); | |
1306 | alloc += sizeof(struct Blob); | |
1307 | alloc += entitlements.size(); | |
1308 | } | |
1309 | ||
1310 | special = std::max(special, CSSLOT_CODEDIRECTORY); | |
1311 | alloc += sizeof(struct BlobIndex); | |
1312 | alloc += sizeof(struct Blob); | |
1313 | alloc += sizeof(struct CodeDirectory); | |
1314 | alloc += name.size() + 1; | |
1315 | ||
1316 | if (!key.empty()) { | |
1317 | alloc += sizeof(struct BlobIndex); | |
1318 | alloc += sizeof(struct Blob); | |
1319 | // XXX: this is just a "sufficiently large number" | |
1320 | alloc += 0x3000; | |
1321 | } | |
1322 | ||
1323 | uint32_t normal((size + PageSize_ - 1) / PageSize_); | |
1324 | alloc = Align(alloc + (special + normal) * SHA_DIGEST_LENGTH, 16); | |
1325 | return alloc; | |
1326 | }), fun([&](std::streambuf &output, size_t limit, const std::string &overlap, const char *top) -> size_t { | |
1327 | Blobs blobs; | |
1328 | ||
1329 | if (true) { | |
1330 | std::stringbuf data; | |
1331 | ||
1332 | Blobs requirements; | |
1333 | put(data, CSMAGIC_REQUIREMENTS, requirements); | |
1334 | ||
1335 | insert(blobs, CSSLOT_REQUIREMENTS, data); | |
1336 | } | |
1337 | ||
1338 | if (!entitlements.empty()) { | |
1339 | std::stringbuf data; | |
1340 | put(data, entitlements.data(), entitlements.size()); | |
1341 | insert(blobs, CSSLOT_ENTITLEMENTS, CSMAGIC_EMBEDDED_ENTITLEMENTS, data); | |
1342 | } | |
1343 | ||
1344 | if (true) { | |
1345 | std::stringbuf data; | |
1346 | ||
1347 | uint32_t special(0); | |
1348 | _foreach (blob, blobs) | |
1349 | special = std::max(special, blob.first); | |
1350 | uint32_t normal((limit + PageSize_ - 1) / PageSize_); | |
1351 | ||
1352 | CodeDirectory directory; | |
1353 | directory.version = Swap(uint32_t(0x00020001)); | |
1354 | directory.flags = Swap(uint32_t(0)); | |
1355 | directory.hashOffset = Swap(uint32_t(sizeof(Blob) + sizeof(CodeDirectory) + name.size() + 1 + SHA_DIGEST_LENGTH * special)); | |
1356 | directory.identOffset = Swap(uint32_t(sizeof(Blob) + sizeof(CodeDirectory))); | |
1357 | directory.nSpecialSlots = Swap(special); | |
1358 | directory.codeLimit = Swap(uint32_t(limit)); | |
1359 | directory.nCodeSlots = Swap(normal); | |
1360 | directory.hashSize = SHA_DIGEST_LENGTH; | |
1361 | directory.hashType = CS_HASHTYPE_SHA1; | |
1362 | directory.spare1 = 0x00; | |
1363 | directory.pageSize = PageShift_; | |
1364 | directory.spare2 = Swap(uint32_t(0)); | |
1365 | put(data, &directory, sizeof(directory)); | |
1366 | ||
1367 | put(data, name.c_str(), name.size() + 1); | |
1368 | ||
1369 | uint8_t storage[special + normal][SHA_DIGEST_LENGTH]; | |
1370 | uint8_t (*hashes)[SHA_DIGEST_LENGTH] = storage + special; | |
1371 | ||
1372 | memset(storage, 0, sizeof(*storage) * special); | |
1373 | ||
1374 | _foreach (blob, blobs) { | |
1375 | auto local(reinterpret_cast<const Blob *>(&blob.second[0])); | |
1376 | sha1((uint8_t *) (hashes - blob.first), local, Swap(local->length)); | |
1377 | } | |
1378 | ||
1379 | if (normal != 1) | |
1380 | for (size_t i = 0; i != normal - 1; ++i) | |
1381 | sha1(hashes[i], (PageSize_ * i < overlap.size() ? overlap.data() : top) + PageSize_ * i, PageSize_); | |
1382 | if (normal != 0) | |
1383 | sha1(hashes[normal - 1], top + PageSize_ * (normal - 1), ((limit - 1) % PageSize_) + 1); | |
1384 | ||
1385 | put(data, storage, sizeof(storage)); | |
1386 | ||
1387 | insert(blobs, CSSLOT_CODEDIRECTORY, CSMAGIC_CODEDIRECTORY, data); | |
1388 | } | |
1389 | ||
1390 | if (!key.empty()) { | |
1391 | std::stringbuf data; | |
1392 | const std::string &sign(blobs[CSSLOT_CODEDIRECTORY]); | |
1393 | ||
1394 | Stuff stuff(key); | |
1395 | Buffer bio(sign); | |
1396 | ||
1397 | Signature signature(stuff, sign); | |
1398 | Buffer result(signature); | |
1399 | std::string value(result); | |
1400 | put(data, value.data(), value.size()); | |
1401 | ||
1402 | insert(blobs, CSSLOT_SIGNATURESLOT, CSMAGIC_BLOBWRAPPER, data); | |
1403 | } | |
1404 | ||
1405 | return put(output, CSMAGIC_EMBEDDED_SIGNATURE, blobs); | |
1406 | })); | |
1407 | } | |
1408 | ||
1409 | static void resign(void *idata, size_t isize, std::streambuf &output) { | |
1410 | resign(idata, isize, output, fun([](size_t size) -> size_t { | |
1411 | return 0; | |
1412 | }), fun([](std::streambuf &output, size_t limit, const std::string &overlap, const char *top) -> size_t { | |
1413 | return 0; | |
1414 | })); | |
1415 | } | |
1416 | ||
1417 | int main(int argc, char *argv[]) { | |
1418 | OpenSSL_add_all_algorithms(); | |
1419 | ||
1420 | union { | |
1421 | uint16_t word; | |
1422 | uint8_t byte[2]; | |
1423 | } endian = {1}; | |
1424 | ||
1425 | little_ = endian.byte[0]; | |
1426 | ||
1427 | bool flag_r(false); | |
1428 | bool flag_e(false); | |
1429 | ||
1430 | bool flag_T(false); | |
1431 | ||
1432 | bool flag_S(false); | |
1433 | bool flag_s(false); | |
1434 | ||
1435 | bool flag_D(false); | |
1436 | ||
1437 | bool flag_A(false); | |
1438 | bool flag_a(false); | |
1439 | ||
1440 | uint32_t flag_CPUType(_not(uint32_t)); | |
1441 | uint32_t flag_CPUSubtype(_not(uint32_t)); | |
1442 | ||
1443 | const char *flag_I(NULL); | |
1444 | ||
1445 | bool timeh(false); | |
1446 | uint32_t timev(0); | |
1447 | ||
1448 | Map entitlements; | |
1449 | Map key; | |
1450 | ||
1451 | std::vector<std::string> files; | |
1452 | ||
1453 | if (argc == 1) { | |
1454 | fprintf(stderr, "usage: %s -S[entitlements.xml] <binary>\n", argv[0]); | |
1455 | fprintf(stderr, " %s -e MobileSafari\n", argv[0]); | |
1456 | fprintf(stderr, " %s -S cat\n", argv[0]); | |
1457 | fprintf(stderr, " %s -Stfp.xml gdb\n", argv[0]); | |
1458 | exit(0); | |
1459 | } | |
1460 | ||
1461 | for (int argi(1); argi != argc; ++argi) | |
1462 | if (argv[argi][0] != '-') | |
1463 | files.push_back(argv[argi]); | |
1464 | else switch (argv[argi][1]) { | |
1465 | case 'r': flag_r = true; break; | |
1466 | case 'e': flag_e = true; break; | |
1467 | ||
1468 | case 'D': flag_D = true; break; | |
1469 | ||
1470 | case 'a': flag_a = true; break; | |
1471 | ||
1472 | case 'A': | |
1473 | flag_A = true; | |
1474 | if (argv[argi][2] != '\0') { | |
1475 | const char *cpu = argv[argi] + 2; | |
1476 | const char *colon = strchr(cpu, ':'); | |
1477 | _assert(colon != NULL); | |
1478 | char *arge; | |
1479 | flag_CPUType = strtoul(cpu, &arge, 0); | |
1480 | _assert(arge == colon); | |
1481 | flag_CPUSubtype = strtoul(colon + 1, &arge, 0); | |
1482 | _assert(arge == argv[argi] + strlen(argv[argi])); | |
1483 | } | |
1484 | break; | |
1485 | ||
1486 | case 's': | |
1487 | _assert(!flag_S); | |
1488 | flag_s = true; | |
1489 | break; | |
1490 | ||
1491 | case 'S': | |
1492 | _assert(!flag_s); | |
1493 | flag_S = true; | |
1494 | if (argv[argi][2] != '\0') { | |
1495 | const char *xml = argv[argi] + 2; | |
1496 | entitlements.open(xml, O_RDONLY, PROT_READ, MAP_PRIVATE); | |
1497 | } | |
1498 | break; | |
1499 | ||
1500 | case 'K': | |
1501 | key.open(argv[argi] + 2, O_RDONLY, PROT_READ, MAP_PRIVATE); | |
1502 | break; | |
1503 | ||
1504 | case 'T': { | |
1505 | flag_T = true; | |
1506 | if (argv[argi][2] == '-') | |
1507 | timeh = true; | |
1508 | else { | |
1509 | char *arge; | |
1510 | timev = strtoul(argv[argi] + 2, &arge, 0); | |
1511 | _assert(arge == argv[argi] + strlen(argv[argi])); | |
1512 | } | |
1513 | } break; | |
1514 | ||
1515 | case 'I': { | |
1516 | flag_I = argv[argi] + 2; | |
1517 | } break; | |
1518 | ||
1519 | default: | |
1520 | goto usage; | |
1521 | break; | |
1522 | } | |
1523 | ||
1524 | _assert(!flag_S || !flag_r); | |
1525 | ||
1526 | if (files.empty()) usage: { | |
1527 | exit(0); | |
1528 | } | |
1529 | ||
1530 | size_t filei(0), filee(0); | |
1531 | _foreach (file, files) try { | |
1532 | const char *path(file.c_str()); | |
1533 | const char *base = strrchr(path, '/'); | |
1534 | ||
1535 | std::string dir; | |
1536 | if (base != NULL) | |
1537 | dir.assign(path, base++ - path + 1); | |
1538 | else | |
1539 | base = path; | |
1540 | ||
1541 | const char *name(flag_I ?: base); | |
1542 | std::string temp; | |
1543 | ||
1544 | if (flag_S || flag_r) { | |
1545 | Map input(path, O_RDONLY, PROT_READ, MAP_PRIVATE); | |
1546 | ||
1547 | temp = dir + "." + base + ".cs"; | |
1548 | std::filebuf output; | |
1549 | _assert(output.open(temp.c_str(), std::ios::out | std::ios::trunc | std::ios::binary) == &output); | |
1550 | ||
1551 | if (flag_r) | |
1552 | resign(input.data(), input.size(), output); | |
1553 | else { | |
1554 | resign(input.data(), input.size(), output, name, entitlements, key); | |
1555 | } | |
1556 | } | |
1557 | ||
1558 | Map mapping(!temp.empty() ? temp.c_str() : path, flag_T || flag_s); | |
1559 | FatHeader fat_header(mapping.data(), mapping.size()); | |
1560 | ||
1561 | _foreach (mach_header, fat_header.GetMachHeaders()) { | |
1562 | struct linkedit_data_command *signature(NULL); | |
1563 | struct encryption_info_command *encryption(NULL); | |
1564 | ||
1565 | if (flag_A) { | |
1566 | if (mach_header.GetCPUType() != flag_CPUType) | |
1567 | continue; | |
1568 | if (mach_header.GetCPUSubtype() != flag_CPUSubtype) | |
1569 | continue; | |
1570 | } | |
1571 | ||
1572 | if (flag_a) | |
1573 | printf("cpu=0x%x:0x%x\n", mach_header.GetCPUType(), mach_header.GetCPUSubtype()); | |
1574 | ||
1575 | _foreach (load_command, mach_header.GetLoadCommands()) { | |
1576 | uint32_t cmd(mach_header.Swap(load_command->cmd)); | |
1577 | ||
1578 | if (false); | |
1579 | else if (cmd == LC_CODE_SIGNATURE) | |
1580 | signature = reinterpret_cast<struct linkedit_data_command *>(load_command); | |
1581 | else if (cmd == LC_ENCRYPTION_INFO || cmd == LC_ENCRYPTION_INFO_64) | |
1582 | encryption = reinterpret_cast<struct encryption_info_command *>(load_command); | |
1583 | else if (cmd == LC_ID_DYLIB) { | |
1584 | volatile struct dylib_command *dylib_command(reinterpret_cast<struct dylib_command *>(load_command)); | |
1585 | ||
1586 | if (flag_T) { | |
1587 | uint32_t timed; | |
1588 | ||
1589 | if (!timeh) | |
1590 | timed = timev; | |
1591 | else { | |
1592 | dylib_command->dylib.timestamp = 0; | |
1593 | timed = hash(reinterpret_cast<uint8_t *>(mach_header.GetBase()), mach_header.GetSize(), timev); | |
1594 | } | |
1595 | ||
1596 | dylib_command->dylib.timestamp = mach_header.Swap(timed); | |
1597 | } | |
1598 | } | |
1599 | } | |
1600 | ||
1601 | if (flag_D) { | |
1602 | _assert(encryption != NULL); | |
1603 | encryption->cryptid = mach_header.Swap(0); | |
1604 | } | |
1605 | ||
1606 | if (flag_e) { | |
1607 | _assert(signature != NULL); | |
1608 | ||
1609 | uint32_t data = mach_header.Swap(signature->dataoff); | |
1610 | ||
1611 | uint8_t *top = reinterpret_cast<uint8_t *>(mach_header.GetBase()); | |
1612 | uint8_t *blob = top + data; | |
1613 | struct SuperBlob *super = reinterpret_cast<struct SuperBlob *>(blob); | |
1614 | ||
1615 | for (size_t index(0); index != Swap(super->count); ++index) | |
1616 | if (Swap(super->index[index].type) == CSSLOT_ENTITLEMENTS) { | |
1617 | uint32_t begin = Swap(super->index[index].offset); | |
1618 | struct Blob *entitlements = reinterpret_cast<struct Blob *>(blob + begin); | |
1619 | fwrite(entitlements + 1, 1, Swap(entitlements->length) - sizeof(*entitlements), stdout); | |
1620 | } | |
1621 | } | |
1622 | ||
1623 | if (flag_s) { | |
1624 | _assert(signature != NULL); | |
1625 | ||
1626 | uint32_t data = mach_header.Swap(signature->dataoff); | |
1627 | ||
1628 | uint8_t *top = reinterpret_cast<uint8_t *>(mach_header.GetBase()); | |
1629 | uint8_t *blob = top + data; | |
1630 | struct SuperBlob *super = reinterpret_cast<struct SuperBlob *>(blob); | |
1631 | ||
1632 | for (size_t index(0); index != Swap(super->count); ++index) | |
1633 | if (Swap(super->index[index].type) == CSSLOT_CODEDIRECTORY) { | |
1634 | uint32_t begin = Swap(super->index[index].offset); | |
1635 | struct CodeDirectory *directory = reinterpret_cast<struct CodeDirectory *>(blob + begin); | |
1636 | ||
1637 | uint8_t (*hashes)[SHA_DIGEST_LENGTH] = reinterpret_cast<uint8_t (*)[SHA_DIGEST_LENGTH]>(blob + begin + Swap(directory->hashOffset)); | |
1638 | uint32_t pages = Swap(directory->nCodeSlots); | |
1639 | ||
1640 | if (pages != 1) | |
1641 | for (size_t i = 0; i != pages - 1; ++i) | |
1642 | sha1(hashes[i], top + PageSize_ * i, PageSize_); | |
1643 | if (pages != 0) | |
1644 | sha1(hashes[pages - 1], top + PageSize_ * (pages - 1), ((data - 1) % PageSize_) + 1); | |
1645 | } | |
1646 | } | |
1647 | } | |
1648 | ||
1649 | if (!temp.empty()) { | |
1650 | struct stat info; | |
1651 | _syscall(stat(path, &info)); | |
1652 | #ifndef __WIN32__ | |
1653 | _syscall(chown(temp.c_str(), info.st_uid, info.st_gid)); | |
1654 | #endif | |
1655 | _syscall(chmod(temp.c_str(), info.st_mode)); | |
1656 | _syscall(unlink(path)); | |
1657 | _syscall(rename(temp.c_str(), path)); | |
1658 | } | |
1659 | ||
1660 | ++filei; | |
1661 | } catch (const char *) { | |
1662 | ++filee; | |
1663 | ++filei; | |
1664 | } | |
1665 | ||
1666 | return filee; | |
1667 | } |