]> git.cameronkatri.com Git - ldid.git/blob - ldid.cpp
Let the user set values for CS flags and platform.
[ldid.git] / ldid.cpp
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 <iostream>
27 #include <memory>
28 #include <set>
29 #include <sstream>
30 #include <string>
31 #include <vector>
32
33 #include <dirent.h>
34 #include <errno.h>
35 #include <fcntl.h>
36 #include <regex.h>
37 #include <stdbool.h>
38 #include <stdint.h>
39 #include <unistd.h>
40
41 #include <sys/mman.h>
42 #include <sys/stat.h>
43 #include <sys/types.h>
44
45 #ifndef LDID_NOSMIME
46 #include <openssl/err.h>
47 #include <openssl/pem.h>
48 #include <openssl/pkcs7.h>
49 #include <openssl/pkcs12.h>
50 #endif
51
52 #ifdef __APPLE__
53 #include <CommonCrypto/CommonDigest.h>
54
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
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
68 #else
69 #include <openssl/sha.h>
70
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
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
84 #endif
85
86 #ifndef LDID_NOPLIST
87 #include <plist/plist.h>
88 #elif __APPLE__
89 #include <CoreFoundation/CoreFoundation.h>
90 #endif
91
92 #include "ldid.hpp"
93
94 #define _assert___(line) \
95 #line
96 #define _assert__(line) \
97 _assert___(line)
98
99 #ifndef $
100 #define $(value) value
101 #endif
102
103 #ifdef __EXCEPTIONS
104 #define _assert_(expr, format, ...) \
105 do if (!(expr)) { \
106 fprintf(stderr, $("%s(%u): _assert(): " format "\n"), __FILE__, __LINE__, ## __VA_ARGS__); \
107 throw $(__FILE__ "(" _assert__(__LINE__) "): _assert(" #expr ")"); \
108 } while (false)
109 #else
110 // XXX: this is not acceptable
111 #define _assert_(expr, format, ...) \
112 do if (!(expr)) { \
113 fprintf(stderr, $("%s(%u): _assert(): " format "\n"), __FILE__, __LINE__, ## __VA_ARGS__); \
114 exit(-1); \
115 } while (false)
116 #endif
117
118 #define _assert(expr) \
119 _assert_(expr, "%s", $(#expr))
120
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; \
128 /* XXX: EINTR is included in this list to fix g++ */ \
129 for (auto success : (long[]) {EINTR, __VA_ARGS__}) \
130 if (error == success) \
131 return (decltype(expr)) -success; \
132 _assert_(false, "errno=%u", error); \
133 } }()
134
135 #define _trace() \
136 fprintf(stderr, $("_trace(%s:%u): %s\n"), __FILE__, __LINE__, $(__FUNCTION__))
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
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
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
211 struct fat_header {
212 uint32_t magic;
213 uint32_t nfat_arch;
214 } _packed;
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;
225 } _packed;
226
227 struct mach_header {
228 uint32_t magic;
229 uint32_t cputype;
230 uint32_t cpusubtype;
231 uint32_t filetype;
232 uint32_t ncmds;
233 uint32_t sizeofcmds;
234 uint32_t flags;
235 } _packed;
236
237 #define MH_MAGIC 0xfeedface
238 #define MH_CIGAM 0xcefaedfe
239
240 #define MH_MAGIC_64 0xfeedfacf
241 #define MH_CIGAM_64 0xcffaedfe
242
243 #define MH_DYLDLINK 0x4
244
245 #define MH_OBJECT 0x1
246 #define MH_EXECUTE 0x2
247 #define MH_DYLIB 0x6
248 #define MH_BUNDLE 0x8
249 #define MH_DYLIB_STUB 0x9
250
251 struct load_command {
252 uint32_t cmd;
253 uint32_t cmdsize;
254 } _packed;
255
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)
263 #define LC_SEGMENT_64 uint32_t(0x19)
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)
268 #define LC_ENCRYPTION_INFO uint32_t(0x21)
269 #define LC_DYLD_INFO uint32_t(0x22)
270 #define LC_DYLD_INFO_ONLY uint32_t(0x22 | LC_REQ_DYLD)
271 #define LC_ENCRYPTION_INFO_64 uint32_t(0x2c)
272
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
283 struct dylib {
284 uint32_t name;
285 uint32_t timestamp;
286 uint32_t current_version;
287 uint32_t compatibility_version;
288 } _packed;
289
290 struct dylib_command {
291 uint32_t cmd;
292 uint32_t cmdsize;
293 struct dylib dylib;
294 } _packed;
295
296 struct uuid_command {
297 uint32_t cmd;
298 uint32_t cmdsize;
299 uint8_t uuid[16];
300 } _packed;
301
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
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
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;
408 } _packed;
409
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;
422 } _packed;
423
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;
436 } _packed;
437
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;
450 uint32_t reserved3;
451 } _packed;
452
453 struct linkedit_data_command {
454 uint32_t cmd;
455 uint32_t cmdsize;
456 uint32_t dataoff;
457 uint32_t datasize;
458 } _packed;
459
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
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
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 };
510
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
517 static inline void get(std::streambuf &stream, void *data, size_t size) {
518 _assert(read(stream, data, size) == size);
519 }
520
521 static inline void put(std::streambuf &stream, const void *data, size_t size) {
522 _assert(stream.sputn(static_cast<const char *>(data), size) == size);
523 }
524
525 static inline void put(std::streambuf &stream, const void *data, size_t size, const ldid::Progress &progress) {
526 progress(0);
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;
531 progress(double(total) / size);
532 }
533 }
534
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
544 static inline void pad(std::streambuf &stream, size_t size) {
545 char padding[size];
546 memset(padding, 0, size);
547 put(stream, padding, size);
548 }
549
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
558 static const uint8_t PageShift_(0x0c);
559 static const uint32_t PageSize_(1 << PageShift_);
560
561 static inline uint16_t Swap_(uint16_t value) {
562 return
563 ((value >> 8) & 0x00ff) |
564 ((value << 8) & 0xff00);
565 }
566
567 static inline uint32_t Swap_(uint32_t value) {
568 value = ((value >> 8) & 0x00ff00ff) |
569 ((value << 8) & 0xff00ff00);
570 value = ((value >> 16) & 0x0000ffff) |
571 ((value << 16) & 0xffff0000);
572 return value;
573 }
574
575 static inline uint64_t Swap_(uint64_t value) {
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
582 static inline int16_t Swap_(int16_t value) {
583 return Swap_(static_cast<uint16_t>(value));
584 }
585
586 static inline int32_t Swap_(int32_t value) {
587 return Swap_(static_cast<uint32_t>(value));
588 }
589
590 static inline int64_t Swap_(int64_t value) {
591 return Swap_(static_cast<uint64_t>(value));
592 }
593
594 static bool little_(true);
595
596 static inline uint16_t Swap(uint16_t value) {
597 return little_ ? Swap_(value) : value;
598 }
599
600 static inline uint32_t Swap(uint32_t value) {
601 return little_ ? Swap_(value) : value;
602 }
603
604 static inline uint64_t Swap(uint64_t value) {
605 return little_ ? Swap_(value) : value;
606 }
607
608 static inline int16_t Swap(int16_t value) {
609 return Swap(static_cast<uint16_t>(value));
610 }
611
612 static inline int32_t Swap(int32_t value) {
613 return Swap(static_cast<uint32_t>(value));
614 }
615
616 static inline int64_t Swap(int64_t value) {
617 return Swap(static_cast<uint64_t>(value));
618 }
619
620 class Swapped {
621 protected:
622 bool swapped_;
623
624 Swapped() :
625 swapped_(false)
626 {
627 }
628
629 public:
630 Swapped(bool swapped) :
631 swapped_(swapped)
632 {
633 }
634
635 template <typename Type_>
636 Type_ Swap(Type_ value) const {
637 return swapped_ ? Swap_(value) : value;
638 }
639 };
640
641 class Data :
642 public Swapped
643 {
644 private:
645 void *base_;
646 size_t size_;
647
648 public:
649 Data(void *base, size_t size) :
650 base_(base),
651 size_(size)
652 {
653 }
654
655 void *GetBase() const {
656 return base_;
657 }
658
659 size_t GetSize() const {
660 return size_;
661 }
662 };
663
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;
678
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;
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
708 bool Bits64() const {
709 return bits64_;
710 }
711
712 struct mach_header *operator ->() const {
713 return mach_header_;
714 }
715
716 operator struct mach_header *() const {
717 return mach_header_;
718 }
719
720 uint32_t GetCPUType() const {
721 return Swap(mach_header_->cputype);
722 }
723
724 uint32_t GetCPUSubtype() const {
725 return Swap(mach_header_->cpusubtype) & 0xff;
726 }
727
728 struct load_command *GetLoadCommand() const {
729 return load_command_;
730 }
731
732 std::vector<struct load_command *> GetLoadCommands() const {
733 std::vector<struct load_command *> load_commands;
734
735 struct load_command *load_command = load_command_;
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 }
743
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
765 template <typename Target_>
766 Target_ *GetOffset(uint32_t offset) const {
767 return reinterpret_cast<Target_ *>(offset + (uint8_t *) mach_header_);
768 }
769 };
770
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
789 class FatHeader :
790 public Data
791 {
792 private:
793 fat_header *fat_header_;
794 std::vector<FatMachHeader> mach_headers_;
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;
807 mach_headers_.push_back(FatMachHeader(base, size, NULL));
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);
815 mach_headers_.push_back(FatMachHeader((uint8_t *) base + arch_offset, arch_size, fat_arch));
816 ++fat_arch;
817 }
818 }
819 }
820
821 std::vector<FatMachHeader> &GetMachHeaders() {
822 return mach_headers_;
823 }
824
825 bool IsFat() const {
826 return fat_header_ != NULL;
827 }
828
829 struct fat_header *operator ->() const {
830 return fat_header_;
831 }
832
833 operator struct fat_header *() const {
834 return fat_header_;
835 }
836 };
837
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)
846
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)
853 #define CSSLOT_ALTERNATE uint32_t(0x01000)
854
855 #define CSSLOT_SIGNATURESLOT uint32_t(0x10000)
856
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
861
862 struct BlobIndex {
863 uint32_t type;
864 uint32_t offset;
865 } _packed;
866
867 struct Blob {
868 uint32_t magic;
869 uint32_t length;
870 } _packed;
871
872 struct SuperBlob {
873 struct Blob blob;
874 uint32_t count;
875 struct BlobIndex index[];
876 } _packed;
877
878 struct CodeDirectory {
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;
888 uint8_t platform;
889 uint8_t pageSize;
890 uint32_t spare2;
891 uint32_t scatterOffset;
892 uint32_t teamIDOffset;
893 //uint32_t spare3;
894 //uint64_t codeLimit64;
895 } _packed;
896
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
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
955 #ifndef LDID_NOFLAGT
956 extern "C" uint32_t hash(uint8_t *k, uint32_t length, uint32_t initval);
957 #endif
958
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;
974
975 virtual const char *name() = 0;
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 }
997
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 }
1002
1003 virtual const char *name() {
1004 return "sha1";
1005 }
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 }
1032
1033 virtual const char *name() {
1034 return "sha256";
1035 }
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;
1049 }
1050
1051 struct CodesignAllocation {
1052 FatMachHeader mach_header_;
1053 uint32_t offset_;
1054 uint32_t size_;
1055 uint32_t limit_;
1056 uint32_t alloc_;
1057 uint32_t align_;
1058 const char *arch_;
1059
1060 CodesignAllocation(FatMachHeader mach_header, size_t offset, size_t size, size_t limit, size_t alloc, size_t align, const char *arch) :
1061 mach_header_(mach_header),
1062 offset_(offset),
1063 size_(size),
1064 limit_(limit),
1065 alloc_(alloc),
1066 align_(align),
1067 arch_(arch)
1068 {
1069 }
1070 };
1071
1072 #ifndef LDID_NOTOOLS
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
1088 void open(const char *path, int flags) {
1089 _assert(file_ == -1);
1090 file_ = _syscall(::open(path, flags));
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
1119 Map(const std::string &path, int oflag, int pflag, int mflag) :
1120 Map()
1121 {
1122 open(path, oflag, pflag, mflag);
1123 }
1124
1125 Map(const std::string &path, bool edit) :
1126 Map()
1127 {
1128 open(path, edit);
1129 }
1130
1131 ~Map() {
1132 clear();
1133 }
1134
1135 bool empty() const {
1136 return data_ == NULL;
1137 }
1138
1139 void open(const std::string &path, int oflag, int pflag, int mflag) {
1140 clear();
1141
1142 file_.open(path.c_str(), oflag);
1143 int file(file_.file());
1144
1145 struct stat stat;
1146 _syscall(fstat(file, &stat));
1147 size_ = stat.st_size;
1148
1149 data_ = _syscall(mmap(NULL, size_, pflag, mflag, file, 0));
1150 }
1151
1152 void open(const std::string &path, bool edit) {
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);
1157 }
1158
1159 void *data() const {
1160 return data_;
1161 }
1162
1163 size_t size() const {
1164 return size_;
1165 }
1166
1167 operator std::string() const {
1168 return std::string(static_cast<char *>(data_), size_);
1169 }
1170 };
1171 #endif
1172
1173 namespace ldid {
1174
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
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) {
1204 FatHeader source(const_cast<void *>(idata), isize);
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));
1234 if (symtab->stroff != 0 || symtab->strsize != 0) {
1235 _assert(end <= size);
1236 _assert(end >= size - 0x10);
1237 size = end;
1238 }
1239 }
1240
1241 size_t alloc(allocate(mach_header, size));
1242
1243 auto *fat_arch(mach_header.GetFatArch());
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
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
1286 offset = Align(offset, 1 << align);
1287
1288 uint32_t limit(size);
1289 if (alloc != 0)
1290 limit = Align(limit, 0x10);
1291
1292 allocations.push_back(CodesignAllocation(mach_header, offset, size, limit, alloc, align, arch));
1293 offset += size + alloc;
1294 offset = Align(offset, 0x10);
1295 }
1296
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 }
1318 }
1319
1320 _foreach (allocation, allocations) {
1321 progress(allocation.arch_);
1322 auto &mach_header(allocation.mach_header_);
1323
1324 pad(output, allocation.offset_ - position);
1325 position = allocation.offset_;
1326
1327 std::vector<std::string> commands;
1328
1329 _foreach (load_command, mach_header.GetLoadCommands()) {
1330 std::string copy(reinterpret_cast<const char *>(load_command), load_command->cmdsize);
1331
1332 switch (mach_header.Swap(load_command->cmd)) {
1333 case LC_CODE_SIGNATURE:
1334 continue;
1335 break;
1336
1337 case LC_SEGMENT: {
1338 auto segment_command(reinterpret_cast<struct segment_command *>(&copy[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;
1343 segment_command->vmsize = Align(size, 1 << allocation.align_);
1344 } break;
1345
1346 case LC_SEGMENT_64: {
1347 auto segment_command(reinterpret_cast<struct segment_command_64 *>(&copy[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;
1352 segment_command->vmsize = Align(size, 1 << allocation.align_);
1353 } break;
1354 }
1355
1356 commands.push_back(copy);
1357 }
1358
1359 if (allocation.alloc_ != 0) {
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 }
1367
1368 size_t begin(position);
1369
1370 uint32_t after(0);
1371 _foreach(command, commands)
1372 after += command.size();
1373
1374 std::stringbuf altern;
1375
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);
1382
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 }
1389
1390 _foreach(command, commands) {
1391 put(output, command.data(), command.size());
1392 put(altern, command.data(), command.size());
1393 position += command.size();
1394 }
1395
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 }
1402
1403 auto top(reinterpret_cast<char *>(mach_header.GetBase()));
1404
1405 std::string overlap(altern.str());
1406 overlap.append(top + overlap.size(), Align(overlap.size(), 0x1000) - overlap.size());
1407
1408 put(output, top + (position - begin), allocation.size_ - (position - begin), progress);
1409 position = begin + allocation.size_;
1410
1411 pad(output, allocation.limit_ - allocation.size_);
1412 position += allocation.limit_ - allocation.size_;
1413
1414 size_t saved(save(mach_header, output, allocation.limit_, overlap, top, progress));
1415 if (allocation.alloc_ > saved)
1416 pad(output, allocation.alloc_ - saved);
1417 else
1418 _assert(allocation.alloc_ == saved);
1419 position += allocation.alloc_;
1420 }
1421 }
1422
1423 }
1424
1425 typedef std::map<uint32_t, std::string> Blobs;
1426
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
1432 static const std::string &insert(Blobs &blobs, uint32_t slot, uint32_t magic, const std::stringbuf &buffer) {
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));
1438 auto &save(blobs[slot]);
1439 std::swap(save, value);
1440 return save;
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
1470 #ifndef LDID_NOSMIME
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);
1532
1533 _assert(key_ != NULL);
1534 _assert(cert_ != NULL);
1535
1536 if (ca_ == NULL)
1537 ca_ = sk_X509_new_null();
1538 _assert(ca_ != NULL);
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:
1575 Signature(const Stuff &stuff, const Buffer &data, const std::string &xml) {
1576 value_ = PKCS7_new();
1577 _assert(value_ != NULL);
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
1586 auto info(PKCS7_sign_add_signer(value_, stuff, stuff, NULL, PKCS7_NOSMIMECAP));
1587 _assert(info != NULL);
1588
1589 PKCS7_set_detached(value_, 1);
1590
1591 ASN1_OCTET_STRING *string(ASN1_OCTET_STRING_new());
1592 _assert(string != NULL);
1593 try {
1594 _assert(ASN1_STRING_set(string, xml.data(), xml.size()));
1595
1596 static auto nid(OBJ_create("1.2.840.113635.100.9.1", "", ""));
1597 _assert(PKCS7_add_signed_attribute(info, nid, V_ASN1_OCTET_STRING, string));
1598 } catch (...) {
1599 ASN1_OCTET_STRING_free(string);
1600 throw;
1601 }
1602
1603 _assert(PKCS7_final(value_, data, PKCS7_BINARY));
1604 }
1605
1606 ~Signature() {
1607 PKCS7_free(value_);
1608 }
1609
1610 operator PKCS7 *() const {
1611 return value_;
1612 }
1613 };
1614 #endif
1615
1616 class NullBuffer :
1617 public std::streambuf
1618 {
1619 public:
1620 virtual std::streamsize xsputn(const char_type *data, std::streamsize size) {
1621 return size;
1622 }
1623
1624 virtual int_type overflow(int_type next) {
1625 return next;
1626 }
1627 };
1628
1629 class Digest {
1630 public:
1631 uint8_t sha1_[LDID_SHA1_DIGEST_LENGTH];
1632 };
1633
1634 class HashBuffer :
1635 public std::streambuf
1636 {
1637 private:
1638 ldid::Hash &hash_;
1639
1640 LDID_SHA1_CTX sha1_;
1641 LDID_SHA256_CTX sha256_;
1642
1643 public:
1644 HashBuffer(ldid::Hash &hash) :
1645 hash_(hash)
1646 {
1647 LDID_SHA1_Init(&sha1_);
1648 LDID_SHA256_Init(&sha256_);
1649 }
1650
1651 ~HashBuffer() {
1652 LDID_SHA1_Final(reinterpret_cast<uint8_t *>(hash_.sha1_), &sha1_);
1653 LDID_SHA256_Final(reinterpret_cast<uint8_t *>(hash_.sha256_), &sha256_);
1654 }
1655
1656 virtual std::streamsize xsputn(const char_type *data, std::streamsize size) {
1657 LDID_SHA1_Update(&sha1_, data, size);
1658 LDID_SHA256_Update(&sha256_, data, size);
1659 return size;
1660 }
1661
1662 virtual int_type overflow(int_type next) {
1663 if (next == traits_type::eof())
1664 return sync();
1665 char value(next);
1666 xsputn(&value, 1);
1667 return next;
1668 }
1669 };
1670
1671 class HashProxy :
1672 public HashBuffer
1673 {
1674 private:
1675 std::streambuf &buffer_;
1676
1677 public:
1678 HashProxy(ldid::Hash &hash, std::streambuf &buffer) :
1679 HashBuffer(hash),
1680 buffer_(buffer)
1681 {
1682 }
1683
1684 virtual std::streamsize xsputn(const char_type *data, std::streamsize size) {
1685 _assert(HashBuffer::xsputn(data, size) == size);
1686 return buffer_.sputn(data, size);
1687 }
1688 };
1689
1690 #ifndef LDID_NOTOOLS
1691 static bool Starts(const std::string &lhs, const std::string &rhs) {
1692 return lhs.size() >= rhs.size() && lhs.compare(0, rhs.size(), rhs) == 0;
1693 }
1694
1695 class Split {
1696 public:
1697 std::string dir;
1698 std::string base;
1699
1700 Split(const std::string &path) {
1701 size_t slash(path.rfind('/'));
1702 if (slash == std::string::npos)
1703 base = path;
1704 else {
1705 dir = path.substr(0, slash + 1);
1706 base = path.substr(slash + 1);
1707 }
1708 }
1709 };
1710
1711 static void mkdir_p(const std::string &path) {
1712 if (path.empty())
1713 return;
1714 #ifdef __WIN32__
1715 if (_syscall(mkdir(path.c_str()), EEXIST) == -EEXIST)
1716 return;
1717 #else
1718 if (_syscall(mkdir(path.c_str(), 0755), EEXIST) == -EEXIST)
1719 return;
1720 #endif
1721 auto slash(path.rfind('/', path.size() - 1));
1722 if (slash == std::string::npos)
1723 return;
1724 mkdir_p(path.substr(0, slash));
1725 }
1726
1727 static std::string Temporary(std::filebuf &file, const Split &split) {
1728 std::string temp(split.dir + ".ldid." + split.base);
1729 mkdir_p(split.dir);
1730 _assert_(file.open(temp.c_str(), std::ios::out | std::ios::trunc | std::ios::binary) == &file, "open(): %s", temp.c_str());
1731 return temp;
1732 }
1733
1734 static void Commit(const std::string &path, const std::string &temp) {
1735 struct stat info;
1736 if (_syscall(stat(path.c_str(), &info), ENOENT) == 0) {
1737 #ifndef __WIN32__
1738 _syscall(chown(temp.c_str(), info.st_uid, info.st_gid));
1739 #endif
1740 _syscall(chmod(temp.c_str(), info.st_mode));
1741 }
1742
1743 _syscall(rename(temp.c_str(), path.c_str()));
1744 }
1745 #endif
1746
1747 namespace ldid {
1748
1749 #ifndef LDID_NOSMIME
1750 static void get(std::string &value, X509_NAME *name, int nid) {
1751 auto index(X509_NAME_get_index_by_NID(name, nid, -1));
1752 _assert(index >= 0);
1753 auto next(X509_NAME_get_index_by_NID(name, nid, index));
1754 _assert(next == -1);
1755 auto entry(X509_NAME_get_entry(name, index));
1756 _assert(entry != NULL);
1757 auto asn(X509_NAME_ENTRY_get_data(entry));
1758 _assert(asn != NULL);
1759 value.assign(reinterpret_cast<char *>(ASN1_STRING_data(asn)), ASN1_STRING_length(asn));
1760 }
1761 #endif
1762
1763 static void req(std::streambuf &buffer, uint32_t value) {
1764 value = Swap(value);
1765 put(buffer, &value, sizeof(value));
1766 }
1767
1768 static void req(std::streambuf &buffer, const std::string &value) {
1769 req(buffer, value.size());
1770 put(buffer, value.data(), value.size());
1771 static uint8_t zeros[] = {0,0,0,0};
1772 put(buffer, zeros, 3 - (value.size() + 3) % 4);
1773 }
1774
1775 template <size_t Size_>
1776 static void req(std::streambuf &buffer, uint8_t (&&data)[Size_]) {
1777 req(buffer, Size_);
1778 put(buffer, data, Size_);
1779 static uint8_t zeros[] = {0,0,0,0};
1780 put(buffer, zeros, 3 - (Size_ + 3) % 4);
1781 }
1782
1783 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) {
1784 Hash hash;
1785
1786
1787 std::string team;
1788 std::string common;
1789
1790 #ifndef LDID_NOSMIME
1791 if (!key.empty()) {
1792 Stuff stuff(key);
1793 auto name(X509_get_subject_name(stuff));
1794 _assert(name != NULL);
1795 get(team, name, NID_organizationalUnitName);
1796 get(common, name, NID_commonName);
1797 }
1798 #endif
1799
1800
1801 std::stringbuf backing;
1802
1803 if (!requirements.empty()) {
1804 put(backing, requirements.data(), requirements.size());
1805 } else {
1806 Blobs blobs;
1807
1808 std::stringbuf requirement;
1809 req(requirement, exprForm);
1810 req(requirement, opAnd);
1811 req(requirement, opIdent);
1812 req(requirement, identifier);
1813 req(requirement, opAnd);
1814 req(requirement, opAppleGenericAnchor);
1815 req(requirement, opAnd);
1816 req(requirement, opCertField);
1817 req(requirement, 0);
1818 req(requirement, "subject.CN");
1819 req(requirement, matchEqual);
1820 req(requirement, common);
1821 req(requirement, opCertGeneric);
1822 req(requirement, 1);
1823 req(requirement, (uint8_t []) {APPLE_EXTENSION_OID, 2, 1});
1824 req(requirement, matchExists);
1825 insert(blobs, 3, CSMAGIC_REQUIREMENT, requirement);
1826
1827 put(backing, CSMAGIC_REQUIREMENTS, blobs);
1828 }
1829
1830
1831 // XXX: this is just a "sufficiently large number"
1832 size_t certificate(0x3000);
1833
1834 Allocate(idata, isize, output, fun([&](const MachHeader &mach_header, size_t size) -> size_t {
1835 size_t alloc(sizeof(struct SuperBlob));
1836
1837 uint32_t normal((size + PageSize_ - 1) / PageSize_);
1838
1839 uint32_t special(0);
1840
1841 _foreach (slot, slots)
1842 special = std::max(special, slot.first);
1843
1844 mach_header.ForSection(fun([&](const char *segment, const char *section, void *data, size_t size) {
1845 if (strcmp(segment, "__TEXT") == 0 && section != NULL && strcmp(section, "__info_plist") == 0)
1846 special = std::max(special, CSSLOT_INFOSLOT);
1847 }));
1848
1849 special = std::max(special, CSSLOT_REQUIREMENTS);
1850 alloc += sizeof(struct BlobIndex);
1851 alloc += backing.str().size();
1852
1853 if (!entitlements.empty()) {
1854 special = std::max(special, CSSLOT_ENTITLEMENTS);
1855 alloc += sizeof(struct BlobIndex);
1856 alloc += sizeof(struct Blob);
1857 alloc += entitlements.size();
1858 }
1859
1860 size_t directory(0);
1861
1862 directory += sizeof(struct BlobIndex);
1863 directory += sizeof(struct Blob);
1864 directory += sizeof(struct CodeDirectory);
1865 directory += identifier.size() + 1;
1866
1867 if (!team.empty())
1868 directory += team.size() + 1;
1869
1870 for (Algorithm *algorithm : GetAlgorithms())
1871 alloc = Align(alloc + directory + (special + normal) * algorithm->size_, 16);
1872
1873 #ifndef LDID_NOSMIME
1874 if (!key.empty()) {
1875 alloc += sizeof(struct BlobIndex);
1876 alloc += sizeof(struct Blob);
1877 alloc += certificate;
1878 }
1879 #endif
1880
1881 return alloc;
1882 }), fun([&](const MachHeader &mach_header, std::streambuf &output, size_t limit, const std::string &overlap, const char *top, const Progress &progress) -> size_t {
1883 Blobs blobs;
1884
1885 if (true) {
1886 insert(blobs, CSSLOT_REQUIREMENTS, backing);
1887 }
1888
1889 if (!entitlements.empty()) {
1890 std::stringbuf data;
1891 put(data, entitlements.data(), entitlements.size());
1892 insert(blobs, CSSLOT_ENTITLEMENTS, CSMAGIC_EMBEDDED_ENTITLEMENTS, data);
1893 }
1894
1895 Slots posts(slots);
1896
1897 mach_header.ForSection(fun([&](const char *segment, const char *section, void *data, size_t size) {
1898 if (strcmp(segment, "__TEXT") == 0 && section != NULL && strcmp(section, "__info_plist") == 0) {
1899 auto &slot(posts[CSSLOT_INFOSLOT]);
1900 for (Algorithm *algorithm : GetAlgorithms())
1901 (*algorithm)(slot, data, size);
1902 }
1903 }));
1904
1905 unsigned total(0);
1906 for (Algorithm *pointer : GetAlgorithms()) {
1907 Algorithm &algorithm(*pointer);
1908
1909 std::stringbuf data;
1910
1911 uint32_t special(0);
1912 _foreach (blob, blobs)
1913 special = std::max(special, blob.first);
1914 _foreach (slot, posts)
1915 special = std::max(special, slot.first);
1916 uint32_t normal((limit + PageSize_ - 1) / PageSize_);
1917
1918 CodeDirectory directory;
1919 directory.version = Swap(uint32_t(0x00020200));
1920 directory.flags = Swap(uint32_t(flags));
1921 directory.nSpecialSlots = Swap(special);
1922 directory.codeLimit = Swap(uint32_t(limit));
1923 directory.nCodeSlots = Swap(normal);
1924 directory.hashSize = algorithm.size_;
1925 directory.hashType = algorithm.type_;
1926 directory.platform = platform ? 0x01 : 0x00;
1927 directory.pageSize = PageShift_;
1928 directory.spare2 = Swap(uint32_t(0));
1929 directory.scatterOffset = Swap(uint32_t(0));
1930 //directory.spare3 = Swap(uint32_t(0));
1931 //directory.codeLimit64 = Swap(uint64_t(0));
1932
1933 uint32_t offset(sizeof(Blob) + sizeof(CodeDirectory));
1934
1935 directory.identOffset = Swap(uint32_t(offset));
1936 offset += identifier.size() + 1;
1937
1938 if (team.empty())
1939 directory.teamIDOffset = Swap(uint32_t(0));
1940 else {
1941 directory.teamIDOffset = Swap(uint32_t(offset));
1942 offset += team.size() + 1;
1943 }
1944
1945 offset += special * algorithm.size_;
1946 directory.hashOffset = Swap(uint32_t(offset));
1947 offset += normal * algorithm.size_;
1948
1949 put(data, &directory, sizeof(directory));
1950
1951 put(data, identifier.c_str(), identifier.size() + 1);
1952 if (!team.empty())
1953 put(data, team.c_str(), team.size() + 1);
1954
1955 std::vector<uint8_t> storage((special + normal) * algorithm.size_);
1956 auto *hashes(&storage[special * algorithm.size_]);
1957
1958 memset(storage.data(), 0, special * algorithm.size_);
1959
1960 _foreach (blob, blobs) {
1961 auto local(reinterpret_cast<const Blob *>(&blob.second[0]));
1962 algorithm(hashes - blob.first * algorithm.size_, local, Swap(local->length));
1963 }
1964
1965 _foreach (slot, posts)
1966 memcpy(hashes - slot.first * algorithm.size_, algorithm[slot.second], algorithm.size_);
1967
1968 progress(0);
1969 if (normal != 1)
1970 for (size_t i = 0; i != normal - 1; ++i) {
1971 algorithm(hashes + i * algorithm.size_, (PageSize_ * i < overlap.size() ? overlap.data() : top) + PageSize_ * i, PageSize_);
1972 progress(double(i) / normal);
1973 }
1974 if (normal != 0)
1975 algorithm(hashes + (normal - 1) * algorithm.size_, top + PageSize_ * (normal - 1), ((limit - 1) % PageSize_) + 1);
1976 progress(1);
1977
1978 put(data, storage.data(), storage.size());
1979
1980 const auto &save(insert(blobs, total == 0 ? CSSLOT_CODEDIRECTORY : CSSLOT_ALTERNATE + total - 1, CSMAGIC_CODEDIRECTORY, data));
1981 algorithm(hash, save.data(), save.size());
1982
1983 ++total;
1984 }
1985
1986 #ifndef LDID_NOSMIME
1987 if (!key.empty()) {
1988 #ifdef LDID_NOPLIST
1989 auto plist(CFDictionaryCreateMutable(kCFAllocatorDefault, 0, &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks));
1990 _scope({ CFRelease(plist); });
1991
1992 auto cdhashes(CFArrayCreateMutable(kCFAllocatorDefault, 0, &kCFTypeArrayCallBacks));
1993 _scope({ CFRelease(cdhashes); });
1994
1995 CFDictionarySetValue(plist, CFSTR("cdhashes"), cdhashes);
1996 #else
1997 auto plist(plist_new_dict());
1998 _scope({ plist_free(plist); });
1999
2000 auto cdhashes(plist_new_array());
2001 plist_dict_set_item(plist, "cdhashes", cdhashes);
2002 #endif
2003
2004 unsigned total(0);
2005 for (Algorithm *pointer : GetAlgorithms()) {
2006 Algorithm &algorithm(*pointer);
2007 (void) algorithm;
2008
2009 const auto &blob(blobs[total == 0 ? CSSLOT_CODEDIRECTORY : CSSLOT_ALTERNATE + total - 1]);
2010 ++total;
2011
2012 std::vector<char> hash;
2013 algorithm(hash, blob.data(), blob.size());
2014 hash.resize(20);
2015
2016 #ifdef LDID_NOPLIST
2017 auto value(CFDataCreate(kCFAllocatorDefault, reinterpret_cast<const UInt8 *>(hash.data()), hash.size()));
2018 _scope({ CFRelease(value); });
2019 CFArrayAppendValue(cdhashes, value);
2020 #else
2021 plist_array_append_item(cdhashes, plist_new_data(hash.data(), hash.size()));
2022 #endif
2023 }
2024
2025 #ifdef LDID_NOPLIST
2026 auto created(CFPropertyListCreateXMLData(kCFAllocatorDefault, plist));
2027 _scope({ CFRelease(created); });
2028 auto xml(reinterpret_cast<const char *>(CFDataGetBytePtr(created)));
2029 auto size(CFDataGetLength(created));
2030 #else
2031 char *xml(NULL);
2032 uint32_t size;
2033 plist_to_xml(plist, &xml, &size);
2034 _scope({ free(xml); });
2035 #endif
2036
2037 std::stringbuf data;
2038 const std::string &sign(blobs[CSSLOT_CODEDIRECTORY]);
2039
2040 Stuff stuff(key);
2041 Buffer bio(sign);
2042
2043 Signature signature(stuff, sign, std::string(xml, size));
2044 Buffer result(signature);
2045 std::string value(result);
2046 put(data, value.data(), value.size());
2047
2048 const auto &save(insert(blobs, CSSLOT_SIGNATURESLOT, CSMAGIC_BLOBWRAPPER, data));
2049 _assert(save.size() <= certificate);
2050 }
2051 #endif
2052
2053 return put(output, CSMAGIC_EMBEDDED_SIGNATURE, blobs);
2054 }), progress);
2055
2056 return hash;
2057 }
2058
2059 #ifndef LDID_NOTOOLS
2060 static void Unsign(void *idata, size_t isize, std::streambuf &output, const Progress &progress) {
2061 Allocate(idata, isize, output, fun([](const MachHeader &mach_header, size_t size) -> size_t {
2062 return 0;
2063 }), fun([](const MachHeader &mach_header, std::streambuf &output, size_t limit, const std::string &overlap, const char *top, const Progress &progress) -> size_t {
2064 return 0;
2065 }), progress);
2066 }
2067
2068 std::string DiskFolder::Path(const std::string &path) const {
2069 return path_ + "/" + path;
2070 }
2071
2072 DiskFolder::DiskFolder(const std::string &path) :
2073 path_(path)
2074 {
2075 }
2076
2077 DiskFolder::~DiskFolder() {
2078 if (!std::uncaught_exception())
2079 for (const auto &commit : commit_)
2080 Commit(commit.first, commit.second);
2081 }
2082
2083 #ifndef __WIN32__
2084 std::string readlink(const std::string &path) {
2085 for (size_t size(1024); ; size *= 2) {
2086 std::string data;
2087 data.resize(size);
2088
2089 int writ(_syscall(::readlink(path.c_str(), &data[0], data.size())));
2090 if (size_t(writ) >= size)
2091 continue;
2092
2093 data.resize(writ);
2094 return data;
2095 }
2096 }
2097 #endif
2098
2099 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 {
2100 std::string path(Path(root) + base);
2101
2102 DIR *dir(opendir(path.c_str()));
2103 _assert(dir != NULL);
2104 _scope({ _syscall(closedir(dir)); });
2105
2106 while (auto child = readdir(dir)) {
2107 std::string name(child->d_name);
2108 if (name == "." || name == "..")
2109 continue;
2110 if (Starts(name, ".ldid."))
2111 continue;
2112
2113 bool directory;
2114
2115 #ifdef __WIN32__
2116 struct stat info;
2117 _syscall(stat((path + name).c_str(), &info));
2118 if (false);
2119 else if (S_ISDIR(info.st_mode))
2120 directory = true;
2121 else if (S_ISREG(info.st_mode))
2122 directory = false;
2123 else
2124 _assert_(false, "st_mode=%x", info.st_mode);
2125 #else
2126 switch (child->d_type) {
2127 case DT_DIR:
2128 directory = true;
2129 break;
2130 case DT_REG:
2131 directory = false;
2132 break;
2133 case DT_LNK:
2134 link(base + name, fun([&]() { return readlink(path + name); }));
2135 continue;
2136 default:
2137 _assert_(false, "d_type=%u", child->d_type);
2138 }
2139 #endif
2140
2141 if (directory)
2142 Find(root, base + name + "/", code, link);
2143 else
2144 code(base + name);
2145 }
2146 }
2147
2148 void DiskFolder::Save(const std::string &path, bool edit, const void *flag, const Functor<void (std::streambuf &)> &code) {
2149 if (!edit) {
2150 // XXX: use nullbuf
2151 std::stringbuf save;
2152 code(save);
2153 } else {
2154 std::filebuf save;
2155 auto from(Path(path));
2156 commit_[from] = Temporary(save, from);
2157 code(save);
2158 }
2159 }
2160
2161 bool DiskFolder::Look(const std::string &path) const {
2162 return _syscall(access(Path(path).c_str(), R_OK), ENOENT) == 0;
2163 }
2164
2165 void DiskFolder::Open(const std::string &path, const Functor<void (std::streambuf &, size_t, const void *)> &code) const {
2166 std::filebuf data;
2167 auto result(data.open(Path(path).c_str(), std::ios::binary | std::ios::in));
2168 _assert_(result == &data, "DiskFolder::Open(%s)", path.c_str());
2169
2170 auto length(data.pubseekoff(0, std::ios::end, std::ios::in));
2171 data.pubseekpos(0, std::ios::in);
2172 code(data, length, NULL);
2173 }
2174
2175 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 {
2176 Find(path, "", code, link);
2177 }
2178 #endif
2179
2180 SubFolder::SubFolder(Folder &parent, const std::string &path) :
2181 parent_(parent),
2182 path_(path)
2183 {
2184 }
2185
2186 void SubFolder::Save(const std::string &path, bool edit, const void *flag, const Functor<void (std::streambuf &)> &code) {
2187 return parent_.Save(path_ + path, edit, flag, code);
2188 }
2189
2190 bool SubFolder::Look(const std::string &path) const {
2191 return parent_.Look(path_ + path);
2192 }
2193
2194 void SubFolder::Open(const std::string &path, const Functor<void (std::streambuf &, size_t, const void *)> &code) const {
2195 return parent_.Open(path_ + path, code);
2196 }
2197
2198 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 {
2199 return parent_.Find(path_ + path, code, link);
2200 }
2201
2202 std::string UnionFolder::Map(const std::string &path) const {
2203 auto remap(remaps_.find(path));
2204 if (remap == remaps_.end())
2205 return path;
2206 return remap->second;
2207 }
2208
2209 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 {
2210 if (file.size() >= path.size() && file.substr(0, path.size()) == path)
2211 code(file.substr(path.size()));
2212 }
2213
2214 UnionFolder::UnionFolder(Folder &parent) :
2215 parent_(parent)
2216 {
2217 }
2218
2219 void UnionFolder::Save(const std::string &path, bool edit, const void *flag, const Functor<void (std::streambuf &)> &code) {
2220 return parent_.Save(Map(path), edit, flag, code);
2221 }
2222
2223 bool UnionFolder::Look(const std::string &path) const {
2224 auto file(resets_.find(path));
2225 if (file != resets_.end())
2226 return true;
2227 return parent_.Look(Map(path));
2228 }
2229
2230 void UnionFolder::Open(const std::string &path, const Functor<void (std::streambuf &, size_t, const void *)> &code) const {
2231 auto file(resets_.find(path));
2232 if (file == resets_.end())
2233 return parent_.Open(Map(path), code);
2234 auto &entry(file->second);
2235
2236 auto &data(*entry.data_);
2237 auto length(data.pubseekoff(0, std::ios::end, std::ios::in));
2238 data.pubseekpos(0, std::ios::in);
2239 code(data, length, entry.flag_);
2240 }
2241
2242 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 {
2243 for (auto &reset : resets_)
2244 Map(path, code, reset.first, fun([&](const Functor<void (std::streambuf &, size_t, const void *)> &code) {
2245 auto &entry(reset.second);
2246 auto &data(*entry.data_);
2247 auto length(data.pubseekoff(0, std::ios::end, std::ios::in));
2248 data.pubseekpos(0, std::ios::in);
2249 code(data, length, entry.flag_);
2250 }));
2251
2252 for (auto &remap : remaps_)
2253 Map(path, code, remap.first, fun([&](const Functor<void (std::streambuf &, size_t, const void *)> &code) {
2254 parent_.Open(remap.second, fun([&](std::streambuf &data, size_t length, const void *flag) {
2255 code(data, length, flag);
2256 }));
2257 }));
2258
2259 parent_.Find(path, fun([&](const std::string &name) {
2260 if (deletes_.find(path + name) == deletes_.end())
2261 code(name);
2262 }), fun([&](const std::string &name, const Functor<std::string ()> &read) {
2263 if (deletes_.find(path + name) == deletes_.end())
2264 link(name, read);
2265 }));
2266 }
2267
2268 #ifndef LDID_NOTOOLS
2269 static void copy(std::streambuf &source, std::streambuf &target, size_t length, const Progress &progress) {
2270 progress(0);
2271 size_t total(0);
2272 for (;;) {
2273 char data[4096 * 4];
2274 size_t writ(source.sgetn(data, sizeof(data)));
2275 if (writ == 0)
2276 break;
2277 _assert(target.sputn(data, writ) == writ);
2278 total += writ;
2279 progress(double(total) / length);
2280 }
2281 }
2282
2283 #ifndef LDID_NOPLIST
2284 static plist_t plist(const std::string &data) {
2285 plist_t plist(NULL);
2286 if (Starts(data, "bplist00"))
2287 plist_from_bin(data.data(), data.size(), &plist);
2288 else
2289 plist_from_xml(data.data(), data.size(), &plist);
2290 _assert(plist != NULL);
2291 return plist;
2292 }
2293
2294 static void plist_d(std::streambuf &buffer, size_t length, const Functor<void (plist_t)> &code) {
2295 std::stringbuf data;
2296 copy(buffer, data, length, dummy_);
2297 auto node(plist(data.str()));
2298 _scope({ plist_free(node); });
2299 _assert(plist_get_node_type(node) == PLIST_DICT);
2300 code(node);
2301 }
2302
2303 static std::string plist_s(plist_t node) {
2304 _assert(node != NULL);
2305 _assert(plist_get_node_type(node) == PLIST_STRING);
2306 char *data;
2307 plist_get_string_val(node, &data);
2308 _scope({ free(data); });
2309 return data;
2310 }
2311 #endif
2312
2313 enum Mode {
2314 NoMode,
2315 OptionalMode,
2316 OmitMode,
2317 NestedMode,
2318 TopMode,
2319 };
2320
2321 class Expression {
2322 private:
2323 regex_t regex_;
2324 std::vector<std::string> matches_;
2325
2326 public:
2327 Expression(const std::string &code) {
2328 _assert_(regcomp(&regex_, code.c_str(), REG_EXTENDED) == 0, "regcomp()");
2329 matches_.resize(regex_.re_nsub + 1);
2330 }
2331
2332 ~Expression() {
2333 regfree(&regex_);
2334 }
2335
2336 bool operator ()(const std::string &data) {
2337 regmatch_t matches[matches_.size()];
2338 auto value(regexec(&regex_, data.c_str(), matches_.size(), matches, 0));
2339 if (value == REG_NOMATCH)
2340 return false;
2341 _assert_(value == 0, "regexec()");
2342 for (size_t i(0); i != matches_.size(); ++i)
2343 matches_[i].assign(data.data() + matches[i].rm_so, matches[i].rm_eo - matches[i].rm_so);
2344 return true;
2345 }
2346
2347 const std::string &operator [](size_t index) const {
2348 return matches_[index];
2349 }
2350 };
2351
2352 struct Rule {
2353 unsigned weight_;
2354 Mode mode_;
2355 std::string code_;
2356
2357 mutable std::auto_ptr<Expression> regex_;
2358
2359 Rule(unsigned weight, Mode mode, const std::string &code) :
2360 weight_(weight),
2361 mode_(mode),
2362 code_(code)
2363 {
2364 }
2365
2366 Rule(const Rule &rhs) :
2367 weight_(rhs.weight_),
2368 mode_(rhs.mode_),
2369 code_(rhs.code_)
2370 {
2371 }
2372
2373 void Compile() const {
2374 regex_.reset(new Expression(code_));
2375 }
2376
2377 bool operator ()(const std::string &data) const {
2378 _assert(regex_.get() != NULL);
2379 return (*regex_)(data);
2380 }
2381
2382 bool operator <(const Rule &rhs) const {
2383 if (weight_ > rhs.weight_)
2384 return true;
2385 if (weight_ < rhs.weight_)
2386 return false;
2387 return mode_ > rhs.mode_;
2388 }
2389 };
2390
2391 struct RuleCode {
2392 bool operator ()(const Rule *lhs, const Rule *rhs) const {
2393 return lhs->code_ < rhs->code_;
2394 }
2395 };
2396
2397 #ifndef LDID_NOPLIST
2398 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) {
2399 // XXX: this is a miserable fail
2400 std::stringbuf temp;
2401 put(temp, prefix, size);
2402 copy(buffer, temp, length - size, progress);
2403 // XXX: this is a stupid hack
2404 pad(temp, 0x10 - (length & 0xf));
2405 auto data(temp.str());
2406
2407 HashProxy proxy(hash, save);
2408 return Sign(data.data(), data.size(), proxy, identifier, entitlements, requirements, key, slots, flags, platform, percent);
2409 }
2410
2411 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) {
2412 std::string executable;
2413 std::string identifier;
2414
2415 bool mac(false);
2416
2417 std::string info("Info.plist");
2418 if (!folder.Look(info) && folder.Look("Resources/" + info)) {
2419 mac = true;
2420 info = "Resources/" + info;
2421 }
2422
2423 folder.Open(info, fun([&](std::streambuf &buffer, size_t length, const void *flag) {
2424 plist_d(buffer, length, fun([&](plist_t node) {
2425 executable = plist_s(plist_dict_get_item(node, "CFBundleExecutable"));
2426 identifier = plist_s(plist_dict_get_item(node, "CFBundleIdentifier"));
2427 }));
2428 }));
2429
2430 if (!mac && folder.Look("MacOS/" + executable)) {
2431 executable = "MacOS/" + executable;
2432 mac = true;
2433 }
2434
2435 progress(root + "*");
2436
2437 std::string entitlements;
2438 folder.Open(executable, fun([&](std::streambuf &buffer, size_t length, const void *flag) {
2439 // XXX: this is a miserable fail
2440 std::stringbuf temp;
2441 copy(buffer, temp, length, progress);
2442 // XXX: this is a stupid hack
2443 pad(temp, 0x10 - (length & 0xf));
2444 auto data(temp.str());
2445 entitlements = alter(root, Analyze(data.data(), data.size()));
2446 }));
2447
2448 static const std::string directory("_CodeSignature/");
2449 static const std::string signature(directory + "CodeResources");
2450
2451 std::map<std::string, std::multiset<Rule>> versions;
2452
2453 auto &rules1(versions[""]);
2454 auto &rules2(versions["2"]);
2455
2456 const std::string resources(mac ? "Resources/" : "");
2457
2458 if (true) {
2459 rules1.insert(Rule{1, NoMode, "^" + resources});
2460 rules1.insert(Rule{1000, OptionalMode, "^" + resources + ".*\\.lproj/"});
2461 rules1.insert(Rule{1100, OmitMode, "^" + resources + ".*\\.lproj/locversion.plist$"});
2462 rules1.insert(Rule{1010, NoMode, "^Base\\.lproj/"});
2463 rules1.insert(Rule{1, NoMode, "^version.plist$"});
2464 }
2465
2466 if (true) {
2467 rules2.insert(Rule{11, NoMode, ".*\\.dSYM($|/)"});
2468 rules2.insert(Rule{20, NoMode, "^" + resources});
2469 rules2.insert(Rule{2000, OmitMode, "^(.*/)?\\.DS_Store$"});
2470 rules2.insert(Rule{10, NestedMode, "^(Frameworks|SharedFrameworks|PlugIns|Plug-ins|XPCServices|Helpers|MacOS|Library/(Automator|Spotlight|LoginItems))/"});
2471 rules2.insert(Rule{1, NoMode, "^.*"});
2472 rules2.insert(Rule{1000, OptionalMode, "^" + resources + ".*\\.lproj/"});
2473 rules2.insert(Rule{1100, OmitMode, "^" + resources + ".*\\.lproj/locversion.plist$"});
2474 rules2.insert(Rule{1010, NoMode, "^Base\\.lproj/"});
2475 rules2.insert(Rule{20, OmitMode, "^Info\\.plist$"});
2476 rules2.insert(Rule{20, OmitMode, "^PkgInfo$"});
2477 rules2.insert(Rule{10, NestedMode, "^[^/]+$"});
2478 rules2.insert(Rule{20, NoMode, "^embedded\\.provisionprofile$"});
2479 rules2.insert(Rule{20, NoMode, "^version\\.plist$"});
2480 }
2481
2482 std::map<std::string, Hash> local;
2483
2484 std::string failure(mac ? "Contents/|Versions/[^/]*/Resources/" : "");
2485 Expression nested("^(Frameworks/[^/]*\\.framework|PlugIns/[^/]*\\.appex(()|/[^/]*.app))/(" + failure + ")Info\\.plist$");
2486 std::map<std::string, Bundle> bundles;
2487
2488 folder.Find("", fun([&](const std::string &name) {
2489 if (!nested(name))
2490 return;
2491 auto bundle(root + Split(name).dir);
2492 bundle.resize(bundle.size() - resources.size());
2493 SubFolder subfolder(folder, bundle);
2494
2495 bundles[nested[1]] = Sign(bundle, subfolder, key, local, "", Starts(name, "PlugIns/") ? alter :
2496 static_cast<const Functor<std::string (const std::string &, const std::string &)> &>(fun([&](const std::string &, const std::string &) -> std::string { return entitlements; }))
2497 , progress);
2498 }), fun([&](const std::string &name, const Functor<std::string ()> &read) {
2499 }));
2500
2501 std::set<std::string> excludes;
2502
2503 auto exclude([&](const std::string &name) {
2504 // BundleDiskRep::adjustResources -> builder.addExclusion
2505 if (name == executable || Starts(name, directory) || Starts(name, "_MASReceipt/") || name == "CodeResources")
2506 return true;
2507
2508 for (const auto &bundle : bundles)
2509 if (Starts(name, bundle.first + "/")) {
2510 excludes.insert(name);
2511 return true;
2512 }
2513
2514 return false;
2515 });
2516
2517 std::map<std::string, std::string> links;
2518
2519 folder.Find("", fun([&](const std::string &name) {
2520 if (exclude(name))
2521 return;
2522
2523 if (local.find(name) != local.end())
2524 return;
2525 auto &hash(local[name]);
2526
2527 folder.Open(name, fun([&](std::streambuf &data, size_t length, const void *flag) {
2528 progress(root + name);
2529
2530 union {
2531 struct {
2532 uint32_t magic;
2533 uint32_t count;
2534 };
2535
2536 uint8_t bytes[8];
2537 } header;
2538
2539 auto size(most(data, &header.bytes, sizeof(header.bytes)));
2540
2541 if (name != "_WatchKitStub/WK" && size == sizeof(header.bytes))
2542 switch (Swap(header.magic)) {
2543 case FAT_MAGIC:
2544 // Java class file format
2545 if (Swap(header.count) >= 40)
2546 break;
2547 case FAT_CIGAM:
2548 case MH_MAGIC: case MH_MAGIC_64:
2549 case MH_CIGAM: case MH_CIGAM_64:
2550 folder.Save(name, true, flag, fun([&](std::streambuf &save) {
2551 Slots slots;
2552 Sign(header.bytes, size, data, hash, save, identifier, "", "", key, slots, length, 0, false, Progression(progress, root + name));
2553 }));
2554 return;
2555 }
2556
2557 folder.Save(name, false, flag, fun([&](std::streambuf &save) {
2558 HashProxy proxy(hash, save);
2559 put(proxy, header.bytes, size);
2560 copy(data, proxy, length - size, progress);
2561 }));
2562 }));
2563 }), fun([&](const std::string &name, const Functor<std::string ()> &read) {
2564 if (exclude(name))
2565 return;
2566
2567 links[name] = read();
2568 }));
2569
2570 auto plist(plist_new_dict());
2571 _scope({ plist_free(plist); });
2572
2573 for (const auto &version : versions) {
2574 auto files(plist_new_dict());
2575 plist_dict_set_item(plist, ("files" + version.first).c_str(), files);
2576
2577 for (const auto &rule : version.second)
2578 rule.Compile();
2579
2580 bool old(&version.second == &rules1);
2581
2582 for (const auto &hash : local)
2583 for (const auto &rule : version.second)
2584 if (rule(hash.first)) {
2585 if (!old && mac && excludes.find(hash.first) != excludes.end());
2586 else if (old && rule.mode_ == NoMode)
2587 plist_dict_set_item(files, hash.first.c_str(), plist_new_data(reinterpret_cast<const char *>(hash.second.sha1_), sizeof(hash.second.sha1_)));
2588 else if (rule.mode_ != OmitMode) {
2589 auto entry(plist_new_dict());
2590 plist_dict_set_item(entry, "hash", plist_new_data(reinterpret_cast<const char *>(hash.second.sha1_), sizeof(hash.second.sha1_)));
2591 if (!old)
2592 plist_dict_set_item(entry, "hash2", plist_new_data(reinterpret_cast<const char *>(hash.second.sha256_), sizeof(hash.second.sha256_)));
2593 if (rule.mode_ == OptionalMode)
2594 plist_dict_set_item(entry, "optional", plist_new_bool(true));
2595 plist_dict_set_item(files, hash.first.c_str(), entry);
2596 }
2597
2598 break;
2599 }
2600
2601 for (const auto &link : links)
2602 for (const auto &rule : version.second)
2603 if (rule(link.first)) {
2604 if (rule.mode_ != OmitMode) {
2605 auto entry(plist_new_dict());
2606 plist_dict_set_item(entry, "symlink", plist_new_string(link.second.c_str()));
2607 if (rule.mode_ == OptionalMode)
2608 plist_dict_set_item(entry, "optional", plist_new_bool(true));
2609 plist_dict_set_item(files, link.first.c_str(), entry);
2610 }
2611
2612 break;
2613 }
2614
2615 if (!old && mac)
2616 for (const auto &bundle : bundles) {
2617 auto entry(plist_new_dict());
2618 plist_dict_set_item(entry, "cdhash", plist_new_data(reinterpret_cast<const char *>(bundle.second.hash.sha256_), sizeof(bundle.second.hash.sha256_)));
2619 plist_dict_set_item(entry, "requirement", plist_new_string("anchor apple generic"));
2620 plist_dict_set_item(files, bundle.first.c_str(), entry);
2621 }
2622 }
2623
2624 for (const auto &version : versions) {
2625 auto rules(plist_new_dict());
2626 plist_dict_set_item(plist, ("rules" + version.first).c_str(), rules);
2627
2628 std::multiset<const Rule *, RuleCode> ordered;
2629 for (const auto &rule : version.second)
2630 ordered.insert(&rule);
2631
2632 for (const auto &rule : ordered)
2633 if (rule->weight_ == 1 && rule->mode_ == NoMode)
2634 plist_dict_set_item(rules, rule->code_.c_str(), plist_new_bool(true));
2635 else {
2636 auto entry(plist_new_dict());
2637 plist_dict_set_item(rules, rule->code_.c_str(), entry);
2638
2639 switch (rule->mode_) {
2640 case NoMode:
2641 break;
2642 case OmitMode:
2643 plist_dict_set_item(entry, "omit", plist_new_bool(true));
2644 break;
2645 case OptionalMode:
2646 plist_dict_set_item(entry, "optional", plist_new_bool(true));
2647 break;
2648 case NestedMode:
2649 plist_dict_set_item(entry, "nested", plist_new_bool(true));
2650 break;
2651 case TopMode:
2652 plist_dict_set_item(entry, "top", plist_new_bool(true));
2653 break;
2654 }
2655
2656 if (rule->weight_ >= 10000)
2657 plist_dict_set_item(entry, "weight", plist_new_uint(rule->weight_));
2658 else if (rule->weight_ != 1)
2659 plist_dict_set_item(entry, "weight", plist_new_real(rule->weight_));
2660 }
2661 }
2662
2663 folder.Save(signature, true, NULL, fun([&](std::streambuf &save) {
2664 HashProxy proxy(local[signature], save);
2665 char *xml(NULL);
2666 uint32_t size;
2667 plist_to_xml(plist, &xml, &size);
2668 _scope({ free(xml); });
2669 put(proxy, xml, size);
2670 }));
2671
2672 Bundle bundle;
2673 bundle.path = executable;
2674
2675 folder.Open(executable, fun([&](std::streambuf &buffer, size_t length, const void *flag) {
2676 progress(root + executable);
2677 folder.Save(executable, true, flag, fun([&](std::streambuf &save) {
2678 Slots slots;
2679 slots[1] = local.at(info);
2680 slots[3] = local.at(signature);
2681 bundle.hash = Sign(NULL, 0, buffer, local[executable], save, identifier, entitlements, requirements, key, slots, length, 0, false, Progression(progress, root + executable));
2682 }));
2683 }));
2684
2685 for (const auto &entry : local)
2686 remote[root + entry.first] = entry.second;
2687
2688 return bundle;
2689 }
2690
2691 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) {
2692 std::map<std::string, Hash> local;
2693 return Sign(root, folder, key, local, requirements, alter, progress);
2694 }
2695 #endif
2696
2697 #endif
2698 }
2699
2700 #ifndef LDID_NOTOOLS
2701 int main(int argc, char *argv[]) {
2702 #ifndef LDID_NOSMIME
2703 OpenSSL_add_all_algorithms();
2704 #endif
2705
2706 union {
2707 uint16_t word;
2708 uint8_t byte[2];
2709 } endian = {1};
2710
2711 little_ = endian.byte[0];
2712
2713 bool flag_r(false);
2714 bool flag_e(false);
2715 bool flag_q(false);
2716
2717 #ifndef LDID_NOFLAGT
2718 bool flag_T(false);
2719 #endif
2720
2721 bool flag_S(false);
2722 bool flag_s(false);
2723
2724 bool flag_D(false);
2725
2726 bool flag_A(false);
2727 bool flag_a(false);
2728
2729 bool flag_u(false);
2730
2731 uint32_t flags(0);
2732 bool platform(false);
2733
2734 uint32_t flag_CPUType(_not(uint32_t));
2735 uint32_t flag_CPUSubtype(_not(uint32_t));
2736
2737 const char *flag_I(NULL);
2738
2739 #ifndef LDID_NOFLAGT
2740 bool timeh(false);
2741 uint32_t timev(0);
2742 #endif
2743
2744 Map entitlements;
2745 Map requirements;
2746 Map key;
2747 ldid::Slots slots;
2748
2749 std::vector<std::string> files;
2750
2751 if (argc == 1) {
2752 fprintf(stderr, "usage: %s -S[entitlements.xml] <binary>\n", argv[0]);
2753 fprintf(stderr, " %s -e MobileSafari\n", argv[0]);
2754 fprintf(stderr, " %s -S cat\n", argv[0]);
2755 fprintf(stderr, " %s -Stfp.xml gdb\n", argv[0]);
2756 exit(0);
2757 }
2758
2759 for (int argi(1); argi != argc; ++argi)
2760 if (argv[argi][0] != '-')
2761 files.push_back(argv[argi]);
2762 else switch (argv[argi][1]) {
2763 case 'r':
2764 _assert(!flag_s);
2765 _assert(!flag_S);
2766 flag_r = true;
2767 break;
2768
2769 case 'e': flag_e = true; break;
2770
2771 case 'E': {
2772 const char *string = argv[argi] + 2;
2773 const char *colon = strchr(string, ':');
2774 _assert(colon != NULL);
2775 Map file(colon + 1, O_RDONLY, PROT_READ, MAP_PRIVATE);
2776 char *arge;
2777 unsigned number(strtoul(string, &arge, 0));
2778 _assert(arge == colon);
2779 auto &slot(slots[number]);
2780 for (Algorithm *algorithm : GetAlgorithms())
2781 (*algorithm)(slot, file.data(), file.size());
2782 } break;
2783
2784 case 'q': flag_q = true; break;
2785
2786 case 'Q': {
2787 const char *xml = argv[argi] + 2;
2788 requirements.open(xml, O_RDONLY, PROT_READ, MAP_PRIVATE);
2789 } break;
2790
2791 case 'D': flag_D = true; break;
2792
2793 case 'a': flag_a = true; break;
2794
2795 case 'A':
2796 _assert(!flag_A);
2797 flag_A = true;
2798 if (argv[argi][2] != '\0') {
2799 const char *cpu = argv[argi] + 2;
2800 const char *colon = strchr(cpu, ':');
2801 _assert(colon != NULL);
2802 char *arge;
2803 flag_CPUType = strtoul(cpu, &arge, 0);
2804 _assert(arge == colon);
2805 flag_CPUSubtype = strtoul(colon + 1, &arge, 0);
2806 _assert(arge == argv[argi] + strlen(argv[argi]));
2807 }
2808 break;
2809
2810 case 'C': {
2811 const char *name = argv[argi] + 2;
2812 if (false);
2813 else if (strcmp(name, "host") == 0)
2814 flags |= kSecCodeSignatureHost;
2815 else if (strcmp(name, "adhoc") == 0)
2816 flags |= kSecCodeSignatureAdhoc;
2817 else if (strcmp(name, "hard") == 0)
2818 flags |= kSecCodeSignatureForceHard;
2819 else if (strcmp(name, "kill") == 0)
2820 flags |= kSecCodeSignatureForceKill;
2821 else if (strcmp(name, "expires") == 0)
2822 flags |= kSecCodeSignatureForceExpiration;
2823 else if (strcmp(name, "restrict") == 0)
2824 flags |= kSecCodeSignatureRestrict;
2825 else if (strcmp(name, "enforcement") == 0)
2826 flags |= kSecCodeSignatureEnforcement;
2827 else if (strcmp(name, "library-validation") == 0)
2828 flags |= kSecCodeSignatureLibraryValidation;
2829 else _assert(false);
2830 } break;
2831
2832 case 'P':
2833 platform = true;
2834 break;
2835
2836 case 's':
2837 _assert(!flag_r);
2838 _assert(!flag_S);
2839 flag_s = true;
2840 break;
2841
2842 case 'S':
2843 _assert(!flag_r);
2844 _assert(!flag_s);
2845 flag_S = true;
2846 if (argv[argi][2] != '\0') {
2847 const char *xml = argv[argi] + 2;
2848 entitlements.open(xml, O_RDONLY, PROT_READ, MAP_PRIVATE);
2849 }
2850 break;
2851
2852 case 'K':
2853 if (argv[argi][2] != '\0')
2854 key.open(argv[argi] + 2, O_RDONLY, PROT_READ, MAP_PRIVATE);
2855 break;
2856
2857 #ifndef LDID_NOFLAGT
2858 case 'T': {
2859 flag_T = true;
2860 if (argv[argi][2] == '-')
2861 timeh = true;
2862 else {
2863 char *arge;
2864 timev = strtoul(argv[argi] + 2, &arge, 0);
2865 _assert(arge == argv[argi] + strlen(argv[argi]));
2866 }
2867 } break;
2868 #endif
2869
2870 case 'u': {
2871 flag_u = true;
2872 } break;
2873
2874 case 'I': {
2875 flag_I = argv[argi] + 2;
2876 } break;
2877
2878 default:
2879 goto usage;
2880 break;
2881 }
2882
2883 _assert(flag_S || key.empty());
2884 _assert(flag_S || flag_I == NULL);
2885
2886 if (files.empty()) usage: {
2887 exit(0);
2888 }
2889
2890 size_t filei(0), filee(0);
2891 _foreach (file, files) try {
2892 std::string path(file);
2893
2894 struct stat info;
2895 _syscall(stat(path.c_str(), &info));
2896
2897 if (S_ISDIR(info.st_mode)) {
2898 #ifndef LDID_NOPLIST
2899 _assert(!flag_r);
2900 ldid::DiskFolder folder(path);
2901 path += "/" + Sign("", folder, key, requirements, ldid::fun([&](const std::string &, const std::string &) -> std::string { return entitlements; }), dummy_).path;
2902 #else
2903 _assert(false);
2904 #endif
2905 } else if (flag_S || flag_r) {
2906 Map input(path, O_RDONLY, PROT_READ, MAP_PRIVATE);
2907
2908 std::filebuf output;
2909 Split split(path);
2910 auto temp(Temporary(output, split));
2911
2912 if (flag_r)
2913 ldid::Unsign(input.data(), input.size(), output, dummy_);
2914 else {
2915 std::string identifier(flag_I ?: split.base.c_str());
2916 ldid::Sign(input.data(), input.size(), output, identifier, entitlements, requirements, key, slots, flags, platform, dummy_);
2917 }
2918
2919 Commit(path, temp);
2920 }
2921
2922 bool modify(false);
2923 #ifndef LDID_NOFLAGT
2924 if (flag_T)
2925 modify = true;
2926 #endif
2927 if (flag_s)
2928 modify = true;
2929
2930 Map mapping(path, modify);
2931 FatHeader fat_header(mapping.data(), mapping.size());
2932
2933 _foreach (mach_header, fat_header.GetMachHeaders()) {
2934 struct linkedit_data_command *signature(NULL);
2935 struct encryption_info_command *encryption(NULL);
2936
2937 if (flag_A) {
2938 if (mach_header.GetCPUType() != flag_CPUType)
2939 continue;
2940 if (mach_header.GetCPUSubtype() != flag_CPUSubtype)
2941 continue;
2942 }
2943
2944 if (flag_a)
2945 printf("cpu=0x%x:0x%x\n", mach_header.GetCPUType(), mach_header.GetCPUSubtype());
2946
2947 _foreach (load_command, mach_header.GetLoadCommands()) {
2948 uint32_t cmd(mach_header.Swap(load_command->cmd));
2949
2950 if (false);
2951 else if (cmd == LC_CODE_SIGNATURE)
2952 signature = reinterpret_cast<struct linkedit_data_command *>(load_command);
2953 else if (cmd == LC_ENCRYPTION_INFO || cmd == LC_ENCRYPTION_INFO_64)
2954 encryption = reinterpret_cast<struct encryption_info_command *>(load_command);
2955 else if (cmd == LC_LOAD_DYLIB) {
2956 volatile struct dylib_command *dylib_command(reinterpret_cast<struct dylib_command *>(load_command));
2957 const char *name(reinterpret_cast<const char *>(load_command) + mach_header.Swap(dylib_command->dylib.name));
2958
2959 if (strcmp(name, "/System/Library/Frameworks/UIKit.framework/UIKit") == 0) {
2960 if (flag_u) {
2961 Version version;
2962 version.value = mach_header.Swap(dylib_command->dylib.current_version);
2963 printf("uikit=%u.%u.%u\n", version.major, version.minor, version.patch);
2964 }
2965 }
2966 }
2967 #ifndef LDID_NOFLAGT
2968 else if (cmd == LC_ID_DYLIB) {
2969 volatile struct dylib_command *dylib_command(reinterpret_cast<struct dylib_command *>(load_command));
2970
2971 if (flag_T) {
2972 uint32_t timed;
2973
2974 if (!timeh)
2975 timed = timev;
2976 else {
2977 dylib_command->dylib.timestamp = 0;
2978 timed = hash(reinterpret_cast<uint8_t *>(mach_header.GetBase()), mach_header.GetSize(), timev);
2979 }
2980
2981 dylib_command->dylib.timestamp = mach_header.Swap(timed);
2982 }
2983 }
2984 #endif
2985 }
2986
2987 if (flag_D) {
2988 _assert(encryption != NULL);
2989 encryption->cryptid = mach_header.Swap(0);
2990 }
2991
2992 if (flag_e) {
2993 _assert(signature != NULL);
2994
2995 uint32_t data = mach_header.Swap(signature->dataoff);
2996
2997 uint8_t *top = reinterpret_cast<uint8_t *>(mach_header.GetBase());
2998 uint8_t *blob = top + data;
2999 struct SuperBlob *super = reinterpret_cast<struct SuperBlob *>(blob);
3000
3001 for (size_t index(0); index != Swap(super->count); ++index)
3002 if (Swap(super->index[index].type) == CSSLOT_ENTITLEMENTS) {
3003 uint32_t begin = Swap(super->index[index].offset);
3004 struct Blob *entitlements = reinterpret_cast<struct Blob *>(blob + begin);
3005 fwrite(entitlements + 1, 1, Swap(entitlements->length) - sizeof(*entitlements), stdout);
3006 }
3007 }
3008
3009 if (flag_q) {
3010 _assert(signature != NULL);
3011
3012 uint32_t data = mach_header.Swap(signature->dataoff);
3013
3014 uint8_t *top = reinterpret_cast<uint8_t *>(mach_header.GetBase());
3015 uint8_t *blob = top + data;
3016 struct SuperBlob *super = reinterpret_cast<struct SuperBlob *>(blob);
3017
3018 for (size_t index(0); index != Swap(super->count); ++index)
3019 if (Swap(super->index[index].type) == CSSLOT_REQUIREMENTS) {
3020 uint32_t begin = Swap(super->index[index].offset);
3021 struct Blob *requirement = reinterpret_cast<struct Blob *>(blob + begin);
3022 fwrite(requirement, 1, Swap(requirement->length), stdout);
3023 }
3024 }
3025
3026 if (flag_s) {
3027 _assert(signature != NULL);
3028
3029 uint32_t data = mach_header.Swap(signature->dataoff);
3030
3031 uint8_t *top = reinterpret_cast<uint8_t *>(mach_header.GetBase());
3032 uint8_t *blob = top + data;
3033 struct SuperBlob *super = reinterpret_cast<struct SuperBlob *>(blob);
3034
3035 for (size_t index(0); index != Swap(super->count); ++index)
3036 if (Swap(super->index[index].type) == CSSLOT_CODEDIRECTORY) {
3037 uint32_t begin = Swap(super->index[index].offset);
3038 struct CodeDirectory *directory = reinterpret_cast<struct CodeDirectory *>(blob + begin + sizeof(Blob));
3039
3040 uint8_t (*hashes)[LDID_SHA1_DIGEST_LENGTH] = reinterpret_cast<uint8_t (*)[LDID_SHA1_DIGEST_LENGTH]>(blob + begin + Swap(directory->hashOffset));
3041 uint32_t pages = Swap(directory->nCodeSlots);
3042
3043 if (pages != 1)
3044 for (size_t i = 0; i != pages - 1; ++i)
3045 LDID_SHA1(top + PageSize_ * i, PageSize_, hashes[i]);
3046 if (pages != 0)
3047 LDID_SHA1(top + PageSize_ * (pages - 1), ((data - 1) % PageSize_) + 1, hashes[pages - 1]);
3048 }
3049 }
3050 }
3051
3052 ++filei;
3053 } catch (const char *) {
3054 ++filee;
3055 ++filei;
3056 }
3057
3058 return filee;
3059 }
3060 #endif