]> git.saurik.com Git - ldid.git/blob - ldid.cpp
The behavior of usage made absolutely no sense :/.
[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 bool do_sha1(true);
1039 static bool do_sha256(true);
1040
1041 static const std::vector<Algorithm *> &GetAlgorithms() {
1042 static AlgorithmSHA1 sha1;
1043 static AlgorithmSHA256 sha256;
1044
1045 static std::vector<Algorithm *> algorithms;
1046 if (algorithms.empty()) {
1047 if (do_sha1)
1048 algorithms.push_back(&sha1);
1049 if (do_sha256)
1050 algorithms.push_back(&sha256);
1051 }
1052
1053 return algorithms;
1054 }
1055
1056 struct Baton {
1057 std::string entitlements_;
1058 };
1059
1060 struct CodesignAllocation {
1061 FatMachHeader mach_header_;
1062 uint32_t offset_;
1063 uint32_t size_;
1064 uint32_t limit_;
1065 uint32_t alloc_;
1066 uint32_t align_;
1067 const char *arch_;
1068 Baton baton_;
1069
1070 CodesignAllocation(FatMachHeader mach_header, size_t offset, size_t size, size_t limit, size_t alloc, size_t align, const char *arch, const Baton &baton) :
1071 mach_header_(mach_header),
1072 offset_(offset),
1073 size_(size),
1074 limit_(limit),
1075 alloc_(alloc),
1076 align_(align),
1077 arch_(arch),
1078 baton_(baton)
1079 {
1080 }
1081 };
1082
1083 #ifndef LDID_NOTOOLS
1084 class File {
1085 private:
1086 int file_;
1087
1088 public:
1089 File() :
1090 file_(-1)
1091 {
1092 }
1093
1094 ~File() {
1095 if (file_ != -1)
1096 _syscall(close(file_));
1097 }
1098
1099 void open(const char *path, int flags) {
1100 _assert(file_ == -1);
1101 file_ = _syscall(::open(path, flags));
1102 }
1103
1104 int file() const {
1105 return file_;
1106 }
1107 };
1108
1109 class Map {
1110 private:
1111 File file_;
1112 void *data_;
1113 size_t size_;
1114
1115 void clear() {
1116 if (data_ == NULL)
1117 return;
1118 _syscall(munmap(data_, size_));
1119 data_ = NULL;
1120 size_ = 0;
1121 }
1122
1123 public:
1124 Map() :
1125 data_(NULL),
1126 size_(0)
1127 {
1128 }
1129
1130 Map(const std::string &path, int oflag, int pflag, int mflag) :
1131 Map()
1132 {
1133 open(path, oflag, pflag, mflag);
1134 }
1135
1136 Map(const std::string &path, bool edit) :
1137 Map()
1138 {
1139 open(path, edit);
1140 }
1141
1142 ~Map() {
1143 clear();
1144 }
1145
1146 bool empty() const {
1147 return data_ == NULL;
1148 }
1149
1150 void open(const std::string &path, int oflag, int pflag, int mflag) {
1151 clear();
1152
1153 file_.open(path.c_str(), oflag);
1154 int file(file_.file());
1155
1156 struct stat stat;
1157 _syscall(fstat(file, &stat));
1158 size_ = stat.st_size;
1159
1160 data_ = _syscall(mmap(NULL, size_, pflag, mflag, file, 0));
1161 }
1162
1163 void open(const std::string &path, bool edit) {
1164 if (edit)
1165 open(path, O_RDWR, PROT_READ | PROT_WRITE, MAP_SHARED);
1166 else
1167 open(path, O_RDONLY, PROT_READ, MAP_PRIVATE);
1168 }
1169
1170 void *data() const {
1171 return data_;
1172 }
1173
1174 size_t size() const {
1175 return size_;
1176 }
1177
1178 operator std::string() const {
1179 return std::string(static_cast<char *>(data_), size_);
1180 }
1181 };
1182 #endif
1183
1184 namespace ldid {
1185
1186 #ifndef LDID_NOPLIST
1187 static plist_t plist(const std::string &data);
1188 #endif
1189
1190 void Analyze(const MachHeader &mach_header, const Functor<void (const char *data, size_t size)> &entitle) {
1191 _foreach (load_command, mach_header.GetLoadCommands())
1192 if (mach_header.Swap(load_command->cmd) == LC_CODE_SIGNATURE) {
1193 auto signature(reinterpret_cast<struct linkedit_data_command *>(load_command));
1194 auto offset(mach_header.Swap(signature->dataoff));
1195 auto pointer(reinterpret_cast<uint8_t *>(mach_header.GetBase()) + offset);
1196 auto super(reinterpret_cast<struct SuperBlob *>(pointer));
1197
1198 for (size_t index(0); index != Swap(super->count); ++index)
1199 if (Swap(super->index[index].type) == CSSLOT_ENTITLEMENTS) {
1200 auto begin(Swap(super->index[index].offset));
1201 auto blob(reinterpret_cast<struct Blob *>(pointer + begin));
1202 auto writ(Swap(blob->length) - sizeof(*blob));
1203 entitle(reinterpret_cast<char *>(blob + 1), writ);
1204 }
1205 }
1206 }
1207
1208 std::string Analyze(const void *data, size_t size) {
1209 std::string entitlements;
1210
1211 FatHeader fat_header(const_cast<void *>(data), size);
1212 _foreach (mach_header, fat_header.GetMachHeaders())
1213 Analyze(mach_header, fun([&](const char *data, size_t size) {
1214 if (entitlements.empty())
1215 entitlements.assign(data, size);
1216 else
1217 _assert(entitlements.compare(0, entitlements.size(), data, size) == 0);
1218 }));
1219
1220 return entitlements;
1221 }
1222
1223 static void Allocate(const void *idata, size_t isize, std::streambuf &output, const Functor<size_t (const MachHeader &, Baton &, size_t)> &allocate, const Functor<size_t (const MachHeader &, const Baton &, std::streambuf &output, size_t, const std::string &, const char *, const Progress &)> &save, const Progress &progress) {
1224 FatHeader source(const_cast<void *>(idata), isize);
1225
1226 size_t offset(0);
1227 if (source.IsFat())
1228 offset += sizeof(fat_header) + sizeof(fat_arch) * source.Swap(source->nfat_arch);
1229
1230 std::vector<CodesignAllocation> allocations;
1231 _foreach (mach_header, source.GetMachHeaders()) {
1232 struct linkedit_data_command *signature(NULL);
1233 struct symtab_command *symtab(NULL);
1234
1235 _foreach (load_command, mach_header.GetLoadCommands()) {
1236 uint32_t cmd(mach_header.Swap(load_command->cmd));
1237 if (false);
1238 else if (cmd == LC_CODE_SIGNATURE)
1239 signature = reinterpret_cast<struct linkedit_data_command *>(load_command);
1240 else if (cmd == LC_SYMTAB)
1241 symtab = reinterpret_cast<struct symtab_command *>(load_command);
1242 }
1243
1244 size_t size;
1245 if (signature == NULL)
1246 size = mach_header.GetSize();
1247 else {
1248 size = mach_header.Swap(signature->dataoff);
1249 _assert(size <= mach_header.GetSize());
1250 }
1251
1252 if (symtab != NULL) {
1253 auto end(mach_header.Swap(symtab->stroff) + mach_header.Swap(symtab->strsize));
1254 if (symtab->stroff != 0 || symtab->strsize != 0) {
1255 _assert(end <= size);
1256 _assert(end >= size - 0x10);
1257 size = end;
1258 }
1259 }
1260
1261 Baton baton;
1262 size_t alloc(allocate(mach_header, baton, size));
1263
1264 auto *fat_arch(mach_header.GetFatArch());
1265 uint32_t align;
1266
1267 if (fat_arch != NULL)
1268 align = source.Swap(fat_arch->align);
1269 else switch (mach_header.GetCPUType()) {
1270 case CPU_TYPE_POWERPC:
1271 case CPU_TYPE_POWERPC64:
1272 case CPU_TYPE_X86:
1273 case CPU_TYPE_X86_64:
1274 align = 0xc;
1275 break;
1276 case CPU_TYPE_ARM:
1277 case CPU_TYPE_ARM64:
1278 align = 0xe;
1279 break;
1280 default:
1281 align = 0x0;
1282 break;
1283 }
1284
1285 const char *arch(NULL);
1286 switch (mach_header.GetCPUType()) {
1287 case CPU_TYPE_POWERPC:
1288 arch = "ppc";
1289 break;
1290 case CPU_TYPE_POWERPC64:
1291 arch = "ppc64";
1292 break;
1293 case CPU_TYPE_X86:
1294 arch = "i386";
1295 break;
1296 case CPU_TYPE_X86_64:
1297 arch = "x86_64";
1298 break;
1299 case CPU_TYPE_ARM:
1300 arch = "arm";
1301 break;
1302 case CPU_TYPE_ARM64:
1303 arch = "arm64";
1304 break;
1305 }
1306
1307 offset = Align(offset, 1 << align);
1308
1309 uint32_t limit(size);
1310 if (alloc != 0)
1311 limit = Align(limit, 0x10);
1312
1313 allocations.push_back(CodesignAllocation(mach_header, offset, size, limit, alloc, align, arch, baton));
1314 offset += size + alloc;
1315 offset = Align(offset, 0x10);
1316 }
1317
1318 size_t position(0);
1319
1320 if (source.IsFat()) {
1321 fat_header fat_header;
1322 fat_header.magic = Swap(FAT_MAGIC);
1323 fat_header.nfat_arch = Swap(uint32_t(allocations.size()));
1324 put(output, &fat_header, sizeof(fat_header));
1325 position += sizeof(fat_header);
1326
1327 _foreach (allocation, allocations) {
1328 auto &mach_header(allocation.mach_header_);
1329
1330 fat_arch fat_arch;
1331 fat_arch.cputype = Swap(mach_header->cputype);
1332 fat_arch.cpusubtype = Swap(mach_header->cpusubtype);
1333 fat_arch.offset = Swap(allocation.offset_);
1334 fat_arch.size = Swap(allocation.limit_ + allocation.alloc_);
1335 fat_arch.align = Swap(allocation.align_);
1336 put(output, &fat_arch, sizeof(fat_arch));
1337 position += sizeof(fat_arch);
1338 }
1339 }
1340
1341 _foreach (allocation, allocations) {
1342 progress(allocation.arch_);
1343 auto &mach_header(allocation.mach_header_);
1344
1345 pad(output, allocation.offset_ - position);
1346 position = allocation.offset_;
1347
1348 std::vector<std::string> commands;
1349
1350 _foreach (load_command, mach_header.GetLoadCommands()) {
1351 std::string copy(reinterpret_cast<const char *>(load_command), load_command->cmdsize);
1352
1353 switch (mach_header.Swap(load_command->cmd)) {
1354 case LC_CODE_SIGNATURE:
1355 continue;
1356 break;
1357
1358 case LC_SEGMENT: {
1359 auto segment_command(reinterpret_cast<struct segment_command *>(&copy[0]));
1360 if (strncmp(segment_command->segname, "__LINKEDIT", 16) != 0)
1361 break;
1362 size_t size(mach_header.Swap(allocation.limit_ + allocation.alloc_ - mach_header.Swap(segment_command->fileoff)));
1363 segment_command->filesize = size;
1364 segment_command->vmsize = Align(size, 1 << allocation.align_);
1365 } break;
1366
1367 case LC_SEGMENT_64: {
1368 auto segment_command(reinterpret_cast<struct segment_command_64 *>(&copy[0]));
1369 if (strncmp(segment_command->segname, "__LINKEDIT", 16) != 0)
1370 break;
1371 size_t size(mach_header.Swap(allocation.limit_ + allocation.alloc_ - mach_header.Swap(segment_command->fileoff)));
1372 segment_command->filesize = size;
1373 segment_command->vmsize = Align(size, 1 << allocation.align_);
1374 } break;
1375 }
1376
1377 commands.push_back(copy);
1378 }
1379
1380 if (allocation.alloc_ != 0) {
1381 linkedit_data_command signature;
1382 signature.cmd = mach_header.Swap(LC_CODE_SIGNATURE);
1383 signature.cmdsize = mach_header.Swap(uint32_t(sizeof(signature)));
1384 signature.dataoff = mach_header.Swap(allocation.limit_);
1385 signature.datasize = mach_header.Swap(allocation.alloc_);
1386 commands.push_back(std::string(reinterpret_cast<const char *>(&signature), sizeof(signature)));
1387 }
1388
1389 size_t begin(position);
1390
1391 uint32_t after(0);
1392 _foreach(command, commands)
1393 after += command.size();
1394
1395 std::stringbuf altern;
1396
1397 struct mach_header header(*mach_header);
1398 header.ncmds = mach_header.Swap(uint32_t(commands.size()));
1399 header.sizeofcmds = mach_header.Swap(after);
1400 put(output, &header, sizeof(header));
1401 put(altern, &header, sizeof(header));
1402 position += sizeof(header);
1403
1404 if (mach_header.Bits64()) {
1405 auto pad(mach_header.Swap(uint32_t(0)));
1406 put(output, &pad, sizeof(pad));
1407 put(altern, &pad, sizeof(pad));
1408 position += sizeof(pad);
1409 }
1410
1411 _foreach(command, commands) {
1412 put(output, command.data(), command.size());
1413 put(altern, command.data(), command.size());
1414 position += command.size();
1415 }
1416
1417 uint32_t before(mach_header.Swap(mach_header->sizeofcmds));
1418 if (before > after) {
1419 pad(output, before - after);
1420 pad(altern, before - after);
1421 position += before - after;
1422 }
1423
1424 auto top(reinterpret_cast<char *>(mach_header.GetBase()));
1425
1426 std::string overlap(altern.str());
1427 overlap.append(top + overlap.size(), Align(overlap.size(), 0x1000) - overlap.size());
1428
1429 put(output, top + (position - begin), allocation.size_ - (position - begin), progress);
1430 position = begin + allocation.size_;
1431
1432 pad(output, allocation.limit_ - allocation.size_);
1433 position += allocation.limit_ - allocation.size_;
1434
1435 size_t saved(save(mach_header, allocation.baton_, output, allocation.limit_, overlap, top, progress));
1436 if (allocation.alloc_ > saved)
1437 pad(output, allocation.alloc_ - saved);
1438 else
1439 _assert(allocation.alloc_ == saved);
1440 position += allocation.alloc_;
1441 }
1442 }
1443
1444 }
1445
1446 typedef std::map<uint32_t, std::string> Blobs;
1447
1448 static void insert(Blobs &blobs, uint32_t slot, const std::stringbuf &buffer) {
1449 auto value(buffer.str());
1450 std::swap(blobs[slot], value);
1451 }
1452
1453 static const std::string &insert(Blobs &blobs, uint32_t slot, uint32_t magic, const std::stringbuf &buffer) {
1454 auto value(buffer.str());
1455 Blob blob;
1456 blob.magic = Swap(magic);
1457 blob.length = Swap(uint32_t(sizeof(blob) + value.size()));
1458 value.insert(0, reinterpret_cast<char *>(&blob), sizeof(blob));
1459 auto &save(blobs[slot]);
1460 std::swap(save, value);
1461 return save;
1462 }
1463
1464 static size_t put(std::streambuf &output, uint32_t magic, const Blobs &blobs) {
1465 size_t total(0);
1466 _foreach (blob, blobs)
1467 total += blob.second.size();
1468
1469 struct SuperBlob super;
1470 super.blob.magic = Swap(magic);
1471 super.blob.length = Swap(uint32_t(sizeof(SuperBlob) + blobs.size() * sizeof(BlobIndex) + total));
1472 super.count = Swap(uint32_t(blobs.size()));
1473 put(output, &super, sizeof(super));
1474
1475 size_t offset(sizeof(SuperBlob) + sizeof(BlobIndex) * blobs.size());
1476
1477 _foreach (blob, blobs) {
1478 BlobIndex index;
1479 index.type = Swap(blob.first);
1480 index.offset = Swap(uint32_t(offset));
1481 put(output, &index, sizeof(index));
1482 offset += blob.second.size();
1483 }
1484
1485 _foreach (blob, blobs)
1486 put(output, blob.second.data(), blob.second.size());
1487
1488 return offset;
1489 }
1490
1491 #ifndef LDID_NOSMIME
1492 class Buffer {
1493 private:
1494 BIO *bio_;
1495
1496 public:
1497 Buffer(BIO *bio) :
1498 bio_(bio)
1499 {
1500 _assert(bio_ != NULL);
1501 }
1502
1503 Buffer() :
1504 bio_(BIO_new(BIO_s_mem()))
1505 {
1506 }
1507
1508 Buffer(const char *data, size_t size) :
1509 Buffer(BIO_new_mem_buf(const_cast<char *>(data), size))
1510 {
1511 }
1512
1513 Buffer(const std::string &data) :
1514 Buffer(data.data(), data.size())
1515 {
1516 }
1517
1518 Buffer(PKCS7 *pkcs) :
1519 Buffer()
1520 {
1521 _assert(i2d_PKCS7_bio(bio_, pkcs) != 0);
1522 }
1523
1524 ~Buffer() {
1525 BIO_free_all(bio_);
1526 }
1527
1528 operator BIO *() const {
1529 return bio_;
1530 }
1531
1532 explicit operator std::string() const {
1533 char *data;
1534 auto size(BIO_get_mem_data(bio_, &data));
1535 return std::string(data, size);
1536 }
1537 };
1538
1539 class Stuff {
1540 private:
1541 PKCS12 *value_;
1542 EVP_PKEY *key_;
1543 X509 *cert_;
1544 STACK_OF(X509) *ca_;
1545
1546 public:
1547 Stuff(BIO *bio) :
1548 value_(d2i_PKCS12_bio(bio, NULL)),
1549 ca_(NULL)
1550 {
1551 _assert(value_ != NULL);
1552 _assert(PKCS12_parse(value_, "", &key_, &cert_, &ca_) != 0);
1553
1554 _assert(key_ != NULL);
1555 _assert(cert_ != NULL);
1556
1557 if (ca_ == NULL)
1558 ca_ = sk_X509_new_null();
1559 _assert(ca_ != NULL);
1560 }
1561
1562 Stuff(const std::string &data) :
1563 Stuff(Buffer(data))
1564 {
1565 }
1566
1567 ~Stuff() {
1568 sk_X509_pop_free(ca_, X509_free);
1569 X509_free(cert_);
1570 EVP_PKEY_free(key_);
1571 PKCS12_free(value_);
1572 }
1573
1574 operator PKCS12 *() const {
1575 return value_;
1576 }
1577
1578 operator EVP_PKEY *() const {
1579 return key_;
1580 }
1581
1582 operator X509 *() const {
1583 return cert_;
1584 }
1585
1586 operator STACK_OF(X509) *() const {
1587 return ca_;
1588 }
1589 };
1590
1591 class Signature {
1592 private:
1593 PKCS7 *value_;
1594
1595 public:
1596 Signature(const Stuff &stuff, const Buffer &data, const std::string &xml) {
1597 value_ = PKCS7_new();
1598 _assert(value_ != NULL);
1599
1600 _assert(PKCS7_set_type(value_, NID_pkcs7_signed));
1601 _assert(PKCS7_content_new(value_, NID_pkcs7_data));
1602
1603 STACK_OF(X509) *certs(stuff);
1604 for (unsigned i(0), e(sk_X509_num(certs)); i != e; i++)
1605 _assert(PKCS7_add_certificate(value_, sk_X509_value(certs, e - i - 1)));
1606
1607 // XXX: this is the same as PKCS7_sign_add_signer(value_, stuff, stuff, NULL, PKCS7_NOSMIMECAP)
1608 _assert(X509_check_private_key(stuff, stuff));
1609 auto info(PKCS7_add_signature(value_, stuff, stuff, EVP_sha1()));
1610 _assert(info != NULL);
1611 _assert(PKCS7_add_certificate(value_, stuff));
1612 _assert(PKCS7_add_signed_attribute(info, NID_pkcs9_contentType, V_ASN1_OBJECT, OBJ_nid2obj(NID_pkcs7_data)));
1613
1614 PKCS7_set_detached(value_, 1);
1615
1616 ASN1_OCTET_STRING *string(ASN1_OCTET_STRING_new());
1617 _assert(string != NULL);
1618 try {
1619 _assert(ASN1_STRING_set(string, xml.data(), xml.size()));
1620
1621 static auto nid(OBJ_create("1.2.840.113635.100.9.1", "", ""));
1622 _assert(PKCS7_add_signed_attribute(info, nid, V_ASN1_OCTET_STRING, string));
1623 } catch (...) {
1624 ASN1_OCTET_STRING_free(string);
1625 throw;
1626 }
1627
1628 // XXX: this is the same as PKCS7_final(value_, data, PKCS7_BINARY)
1629 BIO *bio(PKCS7_dataInit(value_, NULL));
1630 _assert(bio != NULL);
1631 _scope({ BIO_free_all(bio); });
1632 SMIME_crlf_copy(data, bio, PKCS7_BINARY);
1633 BIO_flush(bio);
1634 _assert(PKCS7_dataFinal(value_, bio));
1635 }
1636
1637 ~Signature() {
1638 PKCS7_free(value_);
1639 }
1640
1641 operator PKCS7 *() const {
1642 return value_;
1643 }
1644 };
1645 #endif
1646
1647 class NullBuffer :
1648 public std::streambuf
1649 {
1650 public:
1651 virtual std::streamsize xsputn(const char_type *data, std::streamsize size) {
1652 return size;
1653 }
1654
1655 virtual int_type overflow(int_type next) {
1656 return next;
1657 }
1658 };
1659
1660 class Digest {
1661 public:
1662 uint8_t sha1_[LDID_SHA1_DIGEST_LENGTH];
1663 };
1664
1665 class HashBuffer :
1666 public std::streambuf
1667 {
1668 private:
1669 ldid::Hash &hash_;
1670
1671 LDID_SHA1_CTX sha1_;
1672 LDID_SHA256_CTX sha256_;
1673
1674 public:
1675 HashBuffer(ldid::Hash &hash) :
1676 hash_(hash)
1677 {
1678 LDID_SHA1_Init(&sha1_);
1679 LDID_SHA256_Init(&sha256_);
1680 }
1681
1682 ~HashBuffer() {
1683 LDID_SHA1_Final(reinterpret_cast<uint8_t *>(hash_.sha1_), &sha1_);
1684 LDID_SHA256_Final(reinterpret_cast<uint8_t *>(hash_.sha256_), &sha256_);
1685 }
1686
1687 virtual std::streamsize xsputn(const char_type *data, std::streamsize size) {
1688 LDID_SHA1_Update(&sha1_, data, size);
1689 LDID_SHA256_Update(&sha256_, data, size);
1690 return size;
1691 }
1692
1693 virtual int_type overflow(int_type next) {
1694 if (next == traits_type::eof())
1695 return sync();
1696 char value(next);
1697 xsputn(&value, 1);
1698 return next;
1699 }
1700 };
1701
1702 class HashProxy :
1703 public HashBuffer
1704 {
1705 private:
1706 std::streambuf &buffer_;
1707
1708 public:
1709 HashProxy(ldid::Hash &hash, std::streambuf &buffer) :
1710 HashBuffer(hash),
1711 buffer_(buffer)
1712 {
1713 }
1714
1715 virtual std::streamsize xsputn(const char_type *data, std::streamsize size) {
1716 _assert(HashBuffer::xsputn(data, size) == size);
1717 return buffer_.sputn(data, size);
1718 }
1719 };
1720
1721 #ifndef LDID_NOTOOLS
1722 static bool Starts(const std::string &lhs, const std::string &rhs) {
1723 return lhs.size() >= rhs.size() && lhs.compare(0, rhs.size(), rhs) == 0;
1724 }
1725
1726 class Split {
1727 public:
1728 std::string dir;
1729 std::string base;
1730
1731 Split(const std::string &path) {
1732 size_t slash(path.rfind('/'));
1733 if (slash == std::string::npos)
1734 base = path;
1735 else {
1736 dir = path.substr(0, slash + 1);
1737 base = path.substr(slash + 1);
1738 }
1739 }
1740 };
1741
1742 static void mkdir_p(const std::string &path) {
1743 if (path.empty())
1744 return;
1745 #ifdef __WIN32__
1746 if (_syscall(mkdir(path.c_str()), EEXIST) == -EEXIST)
1747 return;
1748 #else
1749 if (_syscall(mkdir(path.c_str(), 0755), EEXIST) == -EEXIST)
1750 return;
1751 #endif
1752 auto slash(path.rfind('/', path.size() - 1));
1753 if (slash == std::string::npos)
1754 return;
1755 mkdir_p(path.substr(0, slash));
1756 }
1757
1758 static std::string Temporary(std::filebuf &file, const Split &split) {
1759 std::string temp(split.dir + ".ldid." + split.base);
1760 mkdir_p(split.dir);
1761 _assert_(file.open(temp.c_str(), std::ios::out | std::ios::trunc | std::ios::binary) == &file, "open(): %s", temp.c_str());
1762 return temp;
1763 }
1764
1765 static void Commit(const std::string &path, const std::string &temp) {
1766 struct stat info;
1767 if (_syscall(stat(path.c_str(), &info), ENOENT) == 0) {
1768 #ifndef __WIN32__
1769 _syscall(chown(temp.c_str(), info.st_uid, info.st_gid));
1770 #endif
1771 _syscall(chmod(temp.c_str(), info.st_mode));
1772 }
1773
1774 _syscall(rename(temp.c_str(), path.c_str()));
1775 }
1776 #endif
1777
1778 namespace ldid {
1779
1780 #ifndef LDID_NOSMIME
1781 static void get(std::string &value, X509_NAME *name, int nid) {
1782 auto index(X509_NAME_get_index_by_NID(name, nid, -1));
1783 _assert(index >= 0);
1784 auto next(X509_NAME_get_index_by_NID(name, nid, index));
1785 _assert(next == -1);
1786 auto entry(X509_NAME_get_entry(name, index));
1787 _assert(entry != NULL);
1788 auto asn(X509_NAME_ENTRY_get_data(entry));
1789 _assert(asn != NULL);
1790 value.assign(reinterpret_cast<char *>(ASN1_STRING_data(asn)), ASN1_STRING_length(asn));
1791 }
1792 #endif
1793
1794 static void req(std::streambuf &buffer, uint32_t value) {
1795 value = Swap(value);
1796 put(buffer, &value, sizeof(value));
1797 }
1798
1799 static void req(std::streambuf &buffer, const std::string &value) {
1800 req(buffer, value.size());
1801 put(buffer, value.data(), value.size());
1802 static uint8_t zeros[] = {0,0,0,0};
1803 put(buffer, zeros, 3 - (value.size() + 3) % 4);
1804 }
1805
1806 template <size_t Size_>
1807 static void req(std::streambuf &buffer, uint8_t (&&data)[Size_]) {
1808 req(buffer, Size_);
1809 put(buffer, data, Size_);
1810 static uint8_t zeros[] = {0,0,0,0};
1811 put(buffer, zeros, 3 - (Size_ + 3) % 4);
1812 }
1813
1814 Hash Sign(const void *idata, size_t isize, std::streambuf &output, const std::string &identifier, const std::string &entitlements, bool merge, const std::string &requirements, const std::string &key, const Slots &slots, uint32_t flags, bool platform, const Progress &progress) {
1815 Hash hash;
1816
1817
1818 std::string team;
1819 std::string common;
1820
1821 #ifndef LDID_NOSMIME
1822 if (!key.empty()) {
1823 Stuff stuff(key);
1824 auto name(X509_get_subject_name(stuff));
1825 _assert(name != NULL);
1826 get(team, name, NID_organizationalUnitName);
1827 get(common, name, NID_commonName);
1828 }
1829 #endif
1830
1831
1832 std::stringbuf backing;
1833
1834 if (!requirements.empty()) {
1835 put(backing, requirements.data(), requirements.size());
1836 } else {
1837 Blobs blobs;
1838
1839 std::stringbuf requirement;
1840 req(requirement, exprForm);
1841 req(requirement, opAnd);
1842 req(requirement, opIdent);
1843 req(requirement, identifier);
1844 req(requirement, opAnd);
1845 req(requirement, opAppleGenericAnchor);
1846 req(requirement, opAnd);
1847 req(requirement, opCertField);
1848 req(requirement, 0);
1849 req(requirement, "subject.CN");
1850 req(requirement, matchEqual);
1851 req(requirement, common);
1852 req(requirement, opCertGeneric);
1853 req(requirement, 1);
1854 req(requirement, (uint8_t []) {APPLE_EXTENSION_OID, 2, 1});
1855 req(requirement, matchExists);
1856 insert(blobs, 3, CSMAGIC_REQUIREMENT, requirement);
1857
1858 put(backing, CSMAGIC_REQUIREMENTS, blobs);
1859 }
1860
1861
1862 // XXX: this is just a "sufficiently large number"
1863 size_t certificate(0x3000);
1864
1865 Allocate(idata, isize, output, fun([&](const MachHeader &mach_header, Baton &baton, size_t size) -> size_t {
1866 size_t alloc(sizeof(struct SuperBlob));
1867
1868 uint32_t normal((size + PageSize_ - 1) / PageSize_);
1869
1870 uint32_t special(0);
1871
1872 _foreach (slot, slots)
1873 special = std::max(special, slot.first);
1874
1875 mach_header.ForSection(fun([&](const char *segment, const char *section, void *data, size_t size) {
1876 if (strcmp(segment, "__TEXT") == 0 && section != NULL && strcmp(section, "__info_plist") == 0)
1877 special = std::max(special, CSSLOT_INFOSLOT);
1878 }));
1879
1880 special = std::max(special, CSSLOT_REQUIREMENTS);
1881 alloc += sizeof(struct BlobIndex);
1882 alloc += backing.str().size();
1883
1884 if (!merge)
1885 baton.entitlements_ = entitlements;
1886 else {
1887 #ifndef LDID_NOPLIST
1888 Analyze(mach_header, fun([&](const char *data, size_t size) {
1889 baton.entitlements_.assign(data, size);
1890 }));
1891
1892 if (baton.entitlements_.empty())
1893 baton.entitlements_ = entitlements;
1894 else if (!entitlements.empty()) {
1895 auto combined(plist(baton.entitlements_));
1896 _scope({ plist_free(combined); });
1897 _assert(plist_get_node_type(combined) == PLIST_DICT);
1898
1899 auto merging(plist(entitlements));
1900 _scope({ plist_free(merging); });
1901 _assert(plist_get_node_type(merging) == PLIST_DICT);
1902
1903 plist_dict_iter iterator(NULL);
1904 plist_dict_new_iter(merging, &iterator);
1905 _scope({ free(iterator); });
1906
1907 for (;;) {
1908 char *key(NULL);
1909 plist_t value(NULL);
1910 plist_dict_next_item(merging, iterator, &key, &value);
1911 if (key == NULL)
1912 break;
1913 _scope({ free(key); });
1914 plist_dict_set_item(combined, key, plist_copy(value));
1915 }
1916
1917 char *xml(NULL);
1918 uint32_t size;
1919 plist_to_xml(combined, &xml, &size);
1920 _scope({ free(xml); });
1921
1922 baton.entitlements_.assign(xml, size);
1923 }
1924 #else
1925 _assert(false);
1926 #endif
1927 }
1928
1929 if (!baton.entitlements_.empty()) {
1930 special = std::max(special, CSSLOT_ENTITLEMENTS);
1931 alloc += sizeof(struct BlobIndex);
1932 alloc += sizeof(struct Blob);
1933 alloc += baton.entitlements_.size();
1934 }
1935
1936 size_t directory(0);
1937
1938 directory += sizeof(struct BlobIndex);
1939 directory += sizeof(struct Blob);
1940 directory += sizeof(struct CodeDirectory);
1941 directory += identifier.size() + 1;
1942
1943 if (!team.empty())
1944 directory += team.size() + 1;
1945
1946 for (Algorithm *algorithm : GetAlgorithms())
1947 alloc = Align(alloc + directory + (special + normal) * algorithm->size_, 16);
1948
1949 #ifndef LDID_NOSMIME
1950 if (!key.empty()) {
1951 alloc += sizeof(struct BlobIndex);
1952 alloc += sizeof(struct Blob);
1953 alloc += certificate;
1954 }
1955 #endif
1956
1957 return alloc;
1958 }), fun([&](const MachHeader &mach_header, const Baton &baton, std::streambuf &output, size_t limit, const std::string &overlap, const char *top, const Progress &progress) -> size_t {
1959 Blobs blobs;
1960
1961 if (true) {
1962 insert(blobs, CSSLOT_REQUIREMENTS, backing);
1963 }
1964
1965 if (!baton.entitlements_.empty()) {
1966 std::stringbuf data;
1967 put(data, baton.entitlements_.data(), baton.entitlements_.size());
1968 insert(blobs, CSSLOT_ENTITLEMENTS, CSMAGIC_EMBEDDED_ENTITLEMENTS, data);
1969 }
1970
1971 Slots posts(slots);
1972
1973 mach_header.ForSection(fun([&](const char *segment, const char *section, void *data, size_t size) {
1974 if (strcmp(segment, "__TEXT") == 0 && section != NULL && strcmp(section, "__info_plist") == 0) {
1975 auto &slot(posts[CSSLOT_INFOSLOT]);
1976 for (Algorithm *algorithm : GetAlgorithms())
1977 (*algorithm)(slot, data, size);
1978 }
1979 }));
1980
1981 unsigned total(0);
1982 for (Algorithm *pointer : GetAlgorithms()) {
1983 Algorithm &algorithm(*pointer);
1984
1985 std::stringbuf data;
1986
1987 uint32_t special(0);
1988 _foreach (blob, blobs)
1989 special = std::max(special, blob.first);
1990 _foreach (slot, posts)
1991 special = std::max(special, slot.first);
1992 uint32_t normal((limit + PageSize_ - 1) / PageSize_);
1993
1994 CodeDirectory directory;
1995 directory.version = Swap(uint32_t(0x00020200));
1996 directory.flags = Swap(uint32_t(flags));
1997 directory.nSpecialSlots = Swap(special);
1998 directory.codeLimit = Swap(uint32_t(limit));
1999 directory.nCodeSlots = Swap(normal);
2000 directory.hashSize = algorithm.size_;
2001 directory.hashType = algorithm.type_;
2002 directory.platform = platform ? 0x01 : 0x00;
2003 directory.pageSize = PageShift_;
2004 directory.spare2 = Swap(uint32_t(0));
2005 directory.scatterOffset = Swap(uint32_t(0));
2006 //directory.spare3 = Swap(uint32_t(0));
2007 //directory.codeLimit64 = Swap(uint64_t(0));
2008
2009 uint32_t offset(sizeof(Blob) + sizeof(CodeDirectory));
2010
2011 directory.identOffset = Swap(uint32_t(offset));
2012 offset += identifier.size() + 1;
2013
2014 if (team.empty())
2015 directory.teamIDOffset = Swap(uint32_t(0));
2016 else {
2017 directory.teamIDOffset = Swap(uint32_t(offset));
2018 offset += team.size() + 1;
2019 }
2020
2021 offset += special * algorithm.size_;
2022 directory.hashOffset = Swap(uint32_t(offset));
2023 offset += normal * algorithm.size_;
2024
2025 put(data, &directory, sizeof(directory));
2026
2027 put(data, identifier.c_str(), identifier.size() + 1);
2028 if (!team.empty())
2029 put(data, team.c_str(), team.size() + 1);
2030
2031 std::vector<uint8_t> storage((special + normal) * algorithm.size_);
2032 auto *hashes(&storage[special * algorithm.size_]);
2033
2034 memset(storage.data(), 0, special * algorithm.size_);
2035
2036 _foreach (blob, blobs) {
2037 auto local(reinterpret_cast<const Blob *>(&blob.second[0]));
2038 algorithm(hashes - blob.first * algorithm.size_, local, Swap(local->length));
2039 }
2040
2041 _foreach (slot, posts)
2042 memcpy(hashes - slot.first * algorithm.size_, algorithm[slot.second], algorithm.size_);
2043
2044 progress(0);
2045 if (normal != 1)
2046 for (size_t i = 0; i != normal - 1; ++i) {
2047 algorithm(hashes + i * algorithm.size_, (PageSize_ * i < overlap.size() ? overlap.data() : top) + PageSize_ * i, PageSize_);
2048 progress(double(i) / normal);
2049 }
2050 if (normal != 0)
2051 algorithm(hashes + (normal - 1) * algorithm.size_, top + PageSize_ * (normal - 1), ((limit - 1) % PageSize_) + 1);
2052 progress(1);
2053
2054 put(data, storage.data(), storage.size());
2055
2056 const auto &save(insert(blobs, total == 0 ? CSSLOT_CODEDIRECTORY : CSSLOT_ALTERNATE + total - 1, CSMAGIC_CODEDIRECTORY, data));
2057 algorithm(hash, save.data(), save.size());
2058
2059 ++total;
2060 }
2061
2062 #ifndef LDID_NOSMIME
2063 if (!key.empty()) {
2064 #ifdef LDID_NOPLIST
2065 auto plist(CFDictionaryCreateMutable(kCFAllocatorDefault, 0, &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks));
2066 _scope({ CFRelease(plist); });
2067
2068 auto cdhashes(CFArrayCreateMutable(kCFAllocatorDefault, 0, &kCFTypeArrayCallBacks));
2069 _scope({ CFRelease(cdhashes); });
2070
2071 CFDictionarySetValue(plist, CFSTR("cdhashes"), cdhashes);
2072 #else
2073 auto plist(plist_new_dict());
2074 _scope({ plist_free(plist); });
2075
2076 auto cdhashes(plist_new_array());
2077 plist_dict_set_item(plist, "cdhashes", cdhashes);
2078 #endif
2079
2080 unsigned total(0);
2081 for (Algorithm *pointer : GetAlgorithms()) {
2082 Algorithm &algorithm(*pointer);
2083 (void) algorithm;
2084
2085 const auto &blob(blobs[total == 0 ? CSSLOT_CODEDIRECTORY : CSSLOT_ALTERNATE + total - 1]);
2086 ++total;
2087
2088 std::vector<char> hash;
2089 algorithm(hash, blob.data(), blob.size());
2090 hash.resize(20);
2091
2092 #ifdef LDID_NOPLIST
2093 auto value(CFDataCreate(kCFAllocatorDefault, reinterpret_cast<const UInt8 *>(hash.data()), hash.size()));
2094 _scope({ CFRelease(value); });
2095 CFArrayAppendValue(cdhashes, value);
2096 #else
2097 plist_array_append_item(cdhashes, plist_new_data(hash.data(), hash.size()));
2098 #endif
2099 }
2100
2101 #ifdef LDID_NOPLIST
2102 auto created(CFPropertyListCreateXMLData(kCFAllocatorDefault, plist));
2103 _scope({ CFRelease(created); });
2104 auto xml(reinterpret_cast<const char *>(CFDataGetBytePtr(created)));
2105 auto size(CFDataGetLength(created));
2106 #else
2107 char *xml(NULL);
2108 uint32_t size;
2109 plist_to_xml(plist, &xml, &size);
2110 _scope({ free(xml); });
2111 #endif
2112
2113 std::stringbuf data;
2114 const std::string &sign(blobs[CSSLOT_CODEDIRECTORY]);
2115
2116 Stuff stuff(key);
2117 Buffer bio(sign);
2118
2119 Signature signature(stuff, sign, std::string(xml, size));
2120 Buffer result(signature);
2121 std::string value(result);
2122 put(data, value.data(), value.size());
2123
2124 const auto &save(insert(blobs, CSSLOT_SIGNATURESLOT, CSMAGIC_BLOBWRAPPER, data));
2125 _assert(save.size() <= certificate);
2126 }
2127 #endif
2128
2129 return put(output, CSMAGIC_EMBEDDED_SIGNATURE, blobs);
2130 }), progress);
2131
2132 return hash;
2133 }
2134
2135 #ifndef LDID_NOTOOLS
2136 static void Unsign(void *idata, size_t isize, std::streambuf &output, const Progress &progress) {
2137 Allocate(idata, isize, output, fun([](const MachHeader &mach_header, Baton &baton, size_t size) -> size_t {
2138 return 0;
2139 }), fun([](const MachHeader &mach_header, const Baton &baton, std::streambuf &output, size_t limit, const std::string &overlap, const char *top, const Progress &progress) -> size_t {
2140 return 0;
2141 }), progress);
2142 }
2143
2144 std::string DiskFolder::Path(const std::string &path) const {
2145 return path_ + "/" + path;
2146 }
2147
2148 DiskFolder::DiskFolder(const std::string &path) :
2149 path_(path)
2150 {
2151 }
2152
2153 DiskFolder::~DiskFolder() {
2154 if (!std::uncaught_exception())
2155 for (const auto &commit : commit_)
2156 Commit(commit.first, commit.second);
2157 }
2158
2159 #ifndef __WIN32__
2160 std::string readlink(const std::string &path) {
2161 for (size_t size(1024); ; size *= 2) {
2162 std::string data;
2163 data.resize(size);
2164
2165 int writ(_syscall(::readlink(path.c_str(), &data[0], data.size())));
2166 if (size_t(writ) >= size)
2167 continue;
2168
2169 data.resize(writ);
2170 return data;
2171 }
2172 }
2173 #endif
2174
2175 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 {
2176 std::string path(Path(root) + base);
2177
2178 DIR *dir(opendir(path.c_str()));
2179 _assert(dir != NULL);
2180 _scope({ _syscall(closedir(dir)); });
2181
2182 while (auto child = readdir(dir)) {
2183 std::string name(child->d_name);
2184 if (name == "." || name == "..")
2185 continue;
2186 if (Starts(name, ".ldid."))
2187 continue;
2188
2189 bool directory;
2190
2191 #ifdef __WIN32__
2192 struct stat info;
2193 _syscall(stat((path + name).c_str(), &info));
2194 if (false);
2195 else if (S_ISDIR(info.st_mode))
2196 directory = true;
2197 else if (S_ISREG(info.st_mode))
2198 directory = false;
2199 else
2200 _assert_(false, "st_mode=%x", info.st_mode);
2201 #else
2202 switch (child->d_type) {
2203 case DT_DIR:
2204 directory = true;
2205 break;
2206 case DT_REG:
2207 directory = false;
2208 break;
2209 case DT_LNK:
2210 link(base + name, fun([&]() { return readlink(path + name); }));
2211 continue;
2212 default:
2213 _assert_(false, "d_type=%u", child->d_type);
2214 }
2215 #endif
2216
2217 if (directory)
2218 Find(root, base + name + "/", code, link);
2219 else
2220 code(base + name);
2221 }
2222 }
2223
2224 void DiskFolder::Save(const std::string &path, bool edit, const void *flag, const Functor<void (std::streambuf &)> &code) {
2225 if (!edit) {
2226 // XXX: use nullbuf
2227 std::stringbuf save;
2228 code(save);
2229 } else {
2230 std::filebuf save;
2231 auto from(Path(path));
2232 commit_[from] = Temporary(save, from);
2233 code(save);
2234 }
2235 }
2236
2237 bool DiskFolder::Look(const std::string &path) const {
2238 return _syscall(access(Path(path).c_str(), R_OK), ENOENT) == 0;
2239 }
2240
2241 void DiskFolder::Open(const std::string &path, const Functor<void (std::streambuf &, size_t, const void *)> &code) const {
2242 std::filebuf data;
2243 auto result(data.open(Path(path).c_str(), std::ios::binary | std::ios::in));
2244 _assert_(result == &data, "DiskFolder::Open(%s)", path.c_str());
2245
2246 auto length(data.pubseekoff(0, std::ios::end, std::ios::in));
2247 data.pubseekpos(0, std::ios::in);
2248 code(data, length, NULL);
2249 }
2250
2251 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 {
2252 Find(path, "", code, link);
2253 }
2254 #endif
2255
2256 SubFolder::SubFolder(Folder &parent, const std::string &path) :
2257 parent_(parent),
2258 path_(path)
2259 {
2260 }
2261
2262 void SubFolder::Save(const std::string &path, bool edit, const void *flag, const Functor<void (std::streambuf &)> &code) {
2263 return parent_.Save(path_ + path, edit, flag, code);
2264 }
2265
2266 bool SubFolder::Look(const std::string &path) const {
2267 return parent_.Look(path_ + path);
2268 }
2269
2270 void SubFolder::Open(const std::string &path, const Functor<void (std::streambuf &, size_t, const void *)> &code) const {
2271 return parent_.Open(path_ + path, code);
2272 }
2273
2274 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 {
2275 return parent_.Find(path_ + path, code, link);
2276 }
2277
2278 std::string UnionFolder::Map(const std::string &path) const {
2279 auto remap(remaps_.find(path));
2280 if (remap == remaps_.end())
2281 return path;
2282 return remap->second;
2283 }
2284
2285 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 {
2286 if (file.size() >= path.size() && file.substr(0, path.size()) == path)
2287 code(file.substr(path.size()));
2288 }
2289
2290 UnionFolder::UnionFolder(Folder &parent) :
2291 parent_(parent)
2292 {
2293 }
2294
2295 void UnionFolder::Save(const std::string &path, bool edit, const void *flag, const Functor<void (std::streambuf &)> &code) {
2296 return parent_.Save(Map(path), edit, flag, code);
2297 }
2298
2299 bool UnionFolder::Look(const std::string &path) const {
2300 auto file(resets_.find(path));
2301 if (file != resets_.end())
2302 return true;
2303 return parent_.Look(Map(path));
2304 }
2305
2306 void UnionFolder::Open(const std::string &path, const Functor<void (std::streambuf &, size_t, const void *)> &code) const {
2307 auto file(resets_.find(path));
2308 if (file == resets_.end())
2309 return parent_.Open(Map(path), code);
2310 auto &entry(file->second);
2311
2312 auto &data(*entry.data_);
2313 auto length(data.pubseekoff(0, std::ios::end, std::ios::in));
2314 data.pubseekpos(0, std::ios::in);
2315 code(data, length, entry.flag_);
2316 }
2317
2318 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 {
2319 for (auto &reset : resets_)
2320 Map(path, code, reset.first, fun([&](const Functor<void (std::streambuf &, size_t, const void *)> &code) {
2321 auto &entry(reset.second);
2322 auto &data(*entry.data_);
2323 auto length(data.pubseekoff(0, std::ios::end, std::ios::in));
2324 data.pubseekpos(0, std::ios::in);
2325 code(data, length, entry.flag_);
2326 }));
2327
2328 for (auto &remap : remaps_)
2329 Map(path, code, remap.first, fun([&](const Functor<void (std::streambuf &, size_t, const void *)> &code) {
2330 parent_.Open(remap.second, fun([&](std::streambuf &data, size_t length, const void *flag) {
2331 code(data, length, flag);
2332 }));
2333 }));
2334
2335 parent_.Find(path, fun([&](const std::string &name) {
2336 if (deletes_.find(path + name) == deletes_.end())
2337 code(name);
2338 }), fun([&](const std::string &name, const Functor<std::string ()> &read) {
2339 if (deletes_.find(path + name) == deletes_.end())
2340 link(name, read);
2341 }));
2342 }
2343
2344 #ifndef LDID_NOTOOLS
2345 static void copy(std::streambuf &source, std::streambuf &target, size_t length, const Progress &progress) {
2346 progress(0);
2347 size_t total(0);
2348 for (;;) {
2349 char data[4096 * 4];
2350 size_t writ(source.sgetn(data, sizeof(data)));
2351 if (writ == 0)
2352 break;
2353 _assert(target.sputn(data, writ) == writ);
2354 total += writ;
2355 progress(double(total) / length);
2356 }
2357 }
2358
2359 #ifndef LDID_NOPLIST
2360 static plist_t plist(const std::string &data) {
2361 plist_t plist(NULL);
2362 if (Starts(data, "bplist00"))
2363 plist_from_bin(data.data(), data.size(), &plist);
2364 else
2365 plist_from_xml(data.data(), data.size(), &plist);
2366 _assert(plist != NULL);
2367 return plist;
2368 }
2369
2370 static void plist_d(std::streambuf &buffer, size_t length, const Functor<void (plist_t)> &code) {
2371 std::stringbuf data;
2372 copy(buffer, data, length, dummy_);
2373 auto node(plist(data.str()));
2374 _scope({ plist_free(node); });
2375 _assert(plist_get_node_type(node) == PLIST_DICT);
2376 code(node);
2377 }
2378
2379 static std::string plist_s(plist_t node) {
2380 _assert(node != NULL);
2381 _assert(plist_get_node_type(node) == PLIST_STRING);
2382 char *data;
2383 plist_get_string_val(node, &data);
2384 _scope({ free(data); });
2385 return data;
2386 }
2387 #endif
2388
2389 enum Mode {
2390 NoMode,
2391 OptionalMode,
2392 OmitMode,
2393 NestedMode,
2394 TopMode,
2395 };
2396
2397 class Expression {
2398 private:
2399 regex_t regex_;
2400 std::vector<std::string> matches_;
2401
2402 public:
2403 Expression(const std::string &code) {
2404 _assert_(regcomp(&regex_, code.c_str(), REG_EXTENDED) == 0, "regcomp()");
2405 matches_.resize(regex_.re_nsub + 1);
2406 }
2407
2408 ~Expression() {
2409 regfree(&regex_);
2410 }
2411
2412 bool operator ()(const std::string &data) {
2413 regmatch_t matches[matches_.size()];
2414 auto value(regexec(&regex_, data.c_str(), matches_.size(), matches, 0));
2415 if (value == REG_NOMATCH)
2416 return false;
2417 _assert_(value == 0, "regexec()");
2418 for (size_t i(0); i != matches_.size(); ++i)
2419 matches_[i].assign(data.data() + matches[i].rm_so, matches[i].rm_eo - matches[i].rm_so);
2420 return true;
2421 }
2422
2423 const std::string &operator [](size_t index) const {
2424 return matches_[index];
2425 }
2426 };
2427
2428 struct Rule {
2429 unsigned weight_;
2430 Mode mode_;
2431 std::string code_;
2432
2433 mutable std::auto_ptr<Expression> regex_;
2434
2435 Rule(unsigned weight, Mode mode, const std::string &code) :
2436 weight_(weight),
2437 mode_(mode),
2438 code_(code)
2439 {
2440 }
2441
2442 Rule(const Rule &rhs) :
2443 weight_(rhs.weight_),
2444 mode_(rhs.mode_),
2445 code_(rhs.code_)
2446 {
2447 }
2448
2449 void Compile() const {
2450 regex_.reset(new Expression(code_));
2451 }
2452
2453 bool operator ()(const std::string &data) const {
2454 _assert(regex_.get() != NULL);
2455 return (*regex_)(data);
2456 }
2457
2458 bool operator <(const Rule &rhs) const {
2459 if (weight_ > rhs.weight_)
2460 return true;
2461 if (weight_ < rhs.weight_)
2462 return false;
2463 return mode_ > rhs.mode_;
2464 }
2465 };
2466
2467 struct RuleCode {
2468 bool operator ()(const Rule *lhs, const Rule *rhs) const {
2469 return lhs->code_ < rhs->code_;
2470 }
2471 };
2472
2473 #ifndef LDID_NOPLIST
2474 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, bool merge, const std::string &requirements, const std::string &key, const Slots &slots, size_t length, uint32_t flags, bool platform, const Progress &progress) {
2475 // XXX: this is a miserable fail
2476 std::stringbuf temp;
2477 put(temp, prefix, size);
2478 copy(buffer, temp, length - size, progress);
2479 // XXX: this is a stupid hack
2480 pad(temp, 0x10 - (length & 0xf));
2481 auto data(temp.str());
2482
2483 HashProxy proxy(hash, save);
2484 return Sign(data.data(), data.size(), proxy, identifier, entitlements, merge, requirements, key, slots, flags, platform, progress);
2485 }
2486
2487 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) {
2488 std::string executable;
2489 std::string identifier;
2490
2491 bool mac(false);
2492
2493 std::string info("Info.plist");
2494 if (!folder.Look(info) && folder.Look("Resources/" + info)) {
2495 mac = true;
2496 info = "Resources/" + info;
2497 }
2498
2499 folder.Open(info, fun([&](std::streambuf &buffer, size_t length, const void *flag) {
2500 plist_d(buffer, length, fun([&](plist_t node) {
2501 executable = plist_s(plist_dict_get_item(node, "CFBundleExecutable"));
2502 identifier = plist_s(plist_dict_get_item(node, "CFBundleIdentifier"));
2503 }));
2504 }));
2505
2506 if (!mac && folder.Look("MacOS/" + executable)) {
2507 executable = "MacOS/" + executable;
2508 mac = true;
2509 }
2510
2511 progress(root + "*");
2512
2513 std::string entitlements;
2514 folder.Open(executable, fun([&](std::streambuf &buffer, size_t length, const void *flag) {
2515 // XXX: this is a miserable fail
2516 std::stringbuf temp;
2517 copy(buffer, temp, length, progress);
2518 // XXX: this is a stupid hack
2519 pad(temp, 0x10 - (length & 0xf));
2520 auto data(temp.str());
2521 entitlements = alter(root, Analyze(data.data(), data.size()));
2522 }));
2523
2524 static const std::string directory("_CodeSignature/");
2525 static const std::string signature(directory + "CodeResources");
2526
2527 std::map<std::string, std::multiset<Rule>> versions;
2528
2529 auto &rules1(versions[""]);
2530 auto &rules2(versions["2"]);
2531
2532 const std::string resources(mac ? "Resources/" : "");
2533
2534 if (true) {
2535 rules1.insert(Rule{1, NoMode, "^" + resources});
2536 rules1.insert(Rule{1000, OptionalMode, "^" + resources + ".*\\.lproj/"});
2537 rules1.insert(Rule{1100, OmitMode, "^" + resources + ".*\\.lproj/locversion.plist$"});
2538 rules1.insert(Rule{1010, NoMode, "^Base\\.lproj/"});
2539 rules1.insert(Rule{1, NoMode, "^version.plist$"});
2540 }
2541
2542 if (true) {
2543 rules2.insert(Rule{11, NoMode, ".*\\.dSYM($|/)"});
2544 rules2.insert(Rule{20, NoMode, "^" + resources});
2545 rules2.insert(Rule{2000, OmitMode, "^(.*/)?\\.DS_Store$"});
2546 rules2.insert(Rule{10, NestedMode, "^(Frameworks|SharedFrameworks|PlugIns|Plug-ins|XPCServices|Helpers|MacOS|Library/(Automator|Spotlight|LoginItems))/"});
2547 rules2.insert(Rule{1, NoMode, "^.*"});
2548 rules2.insert(Rule{1000, OptionalMode, "^" + resources + ".*\\.lproj/"});
2549 rules2.insert(Rule{1100, OmitMode, "^" + resources + ".*\\.lproj/locversion.plist$"});
2550 rules2.insert(Rule{1010, NoMode, "^Base\\.lproj/"});
2551 rules2.insert(Rule{20, OmitMode, "^Info\\.plist$"});
2552 rules2.insert(Rule{20, OmitMode, "^PkgInfo$"});
2553 rules2.insert(Rule{10, NestedMode, "^[^/]+$"});
2554 rules2.insert(Rule{20, NoMode, "^embedded\\.provisionprofile$"});
2555 rules2.insert(Rule{20, NoMode, "^version\\.plist$"});
2556 }
2557
2558 std::map<std::string, Hash> local;
2559
2560 std::string failure(mac ? "Contents/|Versions/[^/]*/Resources/" : "");
2561 Expression nested("^(Frameworks/[^/]*\\.framework|PlugIns/[^/]*\\.appex(()|/[^/]*.app))/(" + failure + ")Info\\.plist$");
2562 std::map<std::string, Bundle> bundles;
2563
2564 folder.Find("", fun([&](const std::string &name) {
2565 if (!nested(name))
2566 return;
2567 auto bundle(root + Split(name).dir);
2568 bundle.resize(bundle.size() - resources.size());
2569 SubFolder subfolder(folder, bundle);
2570
2571 bundles[nested[1]] = Sign(bundle, subfolder, key, local, "", Starts(name, "PlugIns/") ? alter :
2572 static_cast<const Functor<std::string (const std::string &, const std::string &)> &>(fun([&](const std::string &, const std::string &) -> std::string { return entitlements; }))
2573 , progress);
2574 }), fun([&](const std::string &name, const Functor<std::string ()> &read) {
2575 }));
2576
2577 std::set<std::string> excludes;
2578
2579 auto exclude([&](const std::string &name) {
2580 // BundleDiskRep::adjustResources -> builder.addExclusion
2581 if (name == executable || Starts(name, directory) || Starts(name, "_MASReceipt/") || name == "CodeResources")
2582 return true;
2583
2584 for (const auto &bundle : bundles)
2585 if (Starts(name, bundle.first + "/")) {
2586 excludes.insert(name);
2587 return true;
2588 }
2589
2590 return false;
2591 });
2592
2593 std::map<std::string, std::string> links;
2594
2595 folder.Find("", fun([&](const std::string &name) {
2596 if (exclude(name))
2597 return;
2598
2599 if (local.find(name) != local.end())
2600 return;
2601 auto &hash(local[name]);
2602
2603 folder.Open(name, fun([&](std::streambuf &data, size_t length, const void *flag) {
2604 progress(root + name);
2605
2606 union {
2607 struct {
2608 uint32_t magic;
2609 uint32_t count;
2610 };
2611
2612 uint8_t bytes[8];
2613 } header;
2614
2615 auto size(most(data, &header.bytes, sizeof(header.bytes)));
2616
2617 if (name != "_WatchKitStub/WK" && size == sizeof(header.bytes))
2618 switch (Swap(header.magic)) {
2619 case FAT_MAGIC:
2620 // Java class file format
2621 if (Swap(header.count) >= 40)
2622 break;
2623 case FAT_CIGAM:
2624 case MH_MAGIC: case MH_MAGIC_64:
2625 case MH_CIGAM: case MH_CIGAM_64:
2626 folder.Save(name, true, flag, fun([&](std::streambuf &save) {
2627 Slots slots;
2628 Sign(header.bytes, size, data, hash, save, identifier, "", false, "", key, slots, length, 0, false, Progression(progress, root + name));
2629 }));
2630 return;
2631 }
2632
2633 folder.Save(name, false, flag, fun([&](std::streambuf &save) {
2634 HashProxy proxy(hash, save);
2635 put(proxy, header.bytes, size);
2636 copy(data, proxy, length - size, progress);
2637 }));
2638 }));
2639 }), fun([&](const std::string &name, const Functor<std::string ()> &read) {
2640 if (exclude(name))
2641 return;
2642
2643 links[name] = read();
2644 }));
2645
2646 auto plist(plist_new_dict());
2647 _scope({ plist_free(plist); });
2648
2649 for (const auto &version : versions) {
2650 auto files(plist_new_dict());
2651 plist_dict_set_item(plist, ("files" + version.first).c_str(), files);
2652
2653 for (const auto &rule : version.second)
2654 rule.Compile();
2655
2656 bool old(&version.second == &rules1);
2657
2658 for (const auto &hash : local)
2659 for (const auto &rule : version.second)
2660 if (rule(hash.first)) {
2661 if (!old && mac && excludes.find(hash.first) != excludes.end());
2662 else if (old && rule.mode_ == NoMode)
2663 plist_dict_set_item(files, hash.first.c_str(), plist_new_data(reinterpret_cast<const char *>(hash.second.sha1_), sizeof(hash.second.sha1_)));
2664 else if (rule.mode_ != OmitMode) {
2665 auto entry(plist_new_dict());
2666 plist_dict_set_item(entry, "hash", plist_new_data(reinterpret_cast<const char *>(hash.second.sha1_), sizeof(hash.second.sha1_)));
2667 if (!old)
2668 plist_dict_set_item(entry, "hash2", plist_new_data(reinterpret_cast<const char *>(hash.second.sha256_), sizeof(hash.second.sha256_)));
2669 if (rule.mode_ == OptionalMode)
2670 plist_dict_set_item(entry, "optional", plist_new_bool(true));
2671 plist_dict_set_item(files, hash.first.c_str(), entry);
2672 }
2673
2674 break;
2675 }
2676
2677 for (const auto &link : links)
2678 for (const auto &rule : version.second)
2679 if (rule(link.first)) {
2680 if (rule.mode_ != OmitMode) {
2681 auto entry(plist_new_dict());
2682 plist_dict_set_item(entry, "symlink", plist_new_string(link.second.c_str()));
2683 if (rule.mode_ == OptionalMode)
2684 plist_dict_set_item(entry, "optional", plist_new_bool(true));
2685 plist_dict_set_item(files, link.first.c_str(), entry);
2686 }
2687
2688 break;
2689 }
2690
2691 if (!old && mac)
2692 for (const auto &bundle : bundles) {
2693 auto entry(plist_new_dict());
2694 plist_dict_set_item(entry, "cdhash", plist_new_data(reinterpret_cast<const char *>(bundle.second.hash.sha256_), sizeof(bundle.second.hash.sha256_)));
2695 plist_dict_set_item(entry, "requirement", plist_new_string("anchor apple generic"));
2696 plist_dict_set_item(files, bundle.first.c_str(), entry);
2697 }
2698 }
2699
2700 for (const auto &version : versions) {
2701 auto rules(plist_new_dict());
2702 plist_dict_set_item(plist, ("rules" + version.first).c_str(), rules);
2703
2704 std::multiset<const Rule *, RuleCode> ordered;
2705 for (const auto &rule : version.second)
2706 ordered.insert(&rule);
2707
2708 for (const auto &rule : ordered)
2709 if (rule->weight_ == 1 && rule->mode_ == NoMode)
2710 plist_dict_set_item(rules, rule->code_.c_str(), plist_new_bool(true));
2711 else {
2712 auto entry(plist_new_dict());
2713 plist_dict_set_item(rules, rule->code_.c_str(), entry);
2714
2715 switch (rule->mode_) {
2716 case NoMode:
2717 break;
2718 case OmitMode:
2719 plist_dict_set_item(entry, "omit", plist_new_bool(true));
2720 break;
2721 case OptionalMode:
2722 plist_dict_set_item(entry, "optional", plist_new_bool(true));
2723 break;
2724 case NestedMode:
2725 plist_dict_set_item(entry, "nested", plist_new_bool(true));
2726 break;
2727 case TopMode:
2728 plist_dict_set_item(entry, "top", plist_new_bool(true));
2729 break;
2730 }
2731
2732 if (rule->weight_ >= 10000)
2733 plist_dict_set_item(entry, "weight", plist_new_uint(rule->weight_));
2734 else if (rule->weight_ != 1)
2735 plist_dict_set_item(entry, "weight", plist_new_real(rule->weight_));
2736 }
2737 }
2738
2739 folder.Save(signature, true, NULL, fun([&](std::streambuf &save) {
2740 HashProxy proxy(local[signature], save);
2741 char *xml(NULL);
2742 uint32_t size;
2743 plist_to_xml(plist, &xml, &size);
2744 _scope({ free(xml); });
2745 put(proxy, xml, size);
2746 }));
2747
2748 Bundle bundle;
2749 bundle.path = executable;
2750
2751 folder.Open(executable, fun([&](std::streambuf &buffer, size_t length, const void *flag) {
2752 progress(root + executable);
2753 folder.Save(executable, true, flag, fun([&](std::streambuf &save) {
2754 Slots slots;
2755 slots[1] = local.at(info);
2756 slots[3] = local.at(signature);
2757 bundle.hash = Sign(NULL, 0, buffer, local[executable], save, identifier, entitlements, false, requirements, key, slots, length, 0, false, Progression(progress, root + executable));
2758 }));
2759 }));
2760
2761 for (const auto &entry : local)
2762 remote[root + entry.first] = entry.second;
2763
2764 return bundle;
2765 }
2766
2767 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) {
2768 std::map<std::string, Hash> local;
2769 return Sign(root, folder, key, local, requirements, alter, progress);
2770 }
2771 #endif
2772
2773 #endif
2774 }
2775
2776 static void usage(const char *argv0) {
2777 fprintf(stderr, "usage: %s -S[entitlements.xml] <binary>\n", argv0);
2778 fprintf(stderr, " %s -e MobileSafari\n", argv0);
2779 fprintf(stderr, " %s -S cat\n", argv0);
2780 fprintf(stderr, " %s -Stfp.xml gdb\n", argv0);
2781 }
2782
2783 #ifndef LDID_NOTOOLS
2784 int main(int argc, char *argv[]) {
2785 #ifndef LDID_NOSMIME
2786 OpenSSL_add_all_algorithms();
2787 #endif
2788
2789 union {
2790 uint16_t word;
2791 uint8_t byte[2];
2792 } endian = {1};
2793
2794 little_ = endian.byte[0];
2795
2796 bool flag_r(false);
2797 bool flag_e(false);
2798 bool flag_q(false);
2799
2800 bool flag_H(false);
2801
2802 #ifndef LDID_NOFLAGT
2803 bool flag_T(false);
2804 #endif
2805
2806 bool flag_S(false);
2807 bool flag_s(false);
2808
2809 bool flag_D(false);
2810
2811 bool flag_A(false);
2812 bool flag_a(false);
2813
2814 bool flag_u(false);
2815
2816 bool flag_M(false);
2817
2818 uint32_t flags(0);
2819 bool platform(false);
2820
2821 uint32_t flag_CPUType(_not(uint32_t));
2822 uint32_t flag_CPUSubtype(_not(uint32_t));
2823
2824 const char *flag_I(NULL);
2825
2826 #ifndef LDID_NOFLAGT
2827 bool timeh(false);
2828 uint32_t timev(0);
2829 #endif
2830
2831 Map entitlements;
2832 Map requirements;
2833 Map key;
2834 ldid::Slots slots;
2835
2836 std::vector<std::string> files;
2837
2838 if (argc == 1) {
2839 usage(argv[0]);
2840 return 0;
2841 }
2842
2843 for (int argi(1); argi != argc; ++argi)
2844 if (argv[argi][0] != '-')
2845 files.push_back(argv[argi]);
2846 else switch (argv[argi][1]) {
2847 case 'r':
2848 _assert(!flag_s);
2849 _assert(!flag_S);
2850 flag_r = true;
2851 break;
2852
2853 case 'e': flag_e = true; break;
2854
2855 case 'E': {
2856 const char *string = argv[argi] + 2;
2857 const char *colon = strchr(string, ':');
2858 _assert(colon != NULL);
2859 Map file(colon + 1, O_RDONLY, PROT_READ, MAP_PRIVATE);
2860 char *arge;
2861 unsigned number(strtoul(string, &arge, 0));
2862 _assert(arge == colon);
2863 auto &slot(slots[number]);
2864 for (Algorithm *algorithm : GetAlgorithms())
2865 (*algorithm)(slot, file.data(), file.size());
2866 } break;
2867
2868 case 'q': flag_q = true; break;
2869
2870 case 'H': {
2871 const char *hash = argv[argi] + 2;
2872
2873 if (!flag_H) {
2874 flag_H = true;
2875
2876 do_sha1 = false;
2877 do_sha256 = false;
2878
2879 fprintf(stderr, "WARNING: -H is only present for compatibility with a fork of ldid\n");
2880 fprintf(stderr, " you should NOT be manually specifying the hash algorithm\n");
2881 }
2882
2883 if (false);
2884 else if (strcmp(hash, "sha1") == 0)
2885 do_sha1 = true;
2886 else if (strcmp(hash, "sha256") == 0)
2887 do_sha256 = true;
2888 else _assert(false);
2889 } break;
2890
2891 case 'Q': {
2892 const char *xml = argv[argi] + 2;
2893 requirements.open(xml, O_RDONLY, PROT_READ, MAP_PRIVATE);
2894 } break;
2895
2896 case 'D': flag_D = true; break;
2897
2898 case 'a': flag_a = true; break;
2899
2900 case 'A':
2901 _assert(!flag_A);
2902 flag_A = true;
2903 if (argv[argi][2] != '\0') {
2904 const char *cpu = argv[argi] + 2;
2905 const char *colon = strchr(cpu, ':');
2906 _assert(colon != NULL);
2907 char *arge;
2908 flag_CPUType = strtoul(cpu, &arge, 0);
2909 _assert(arge == colon);
2910 flag_CPUSubtype = strtoul(colon + 1, &arge, 0);
2911 _assert(arge == argv[argi] + strlen(argv[argi]));
2912 }
2913 break;
2914
2915 case 'C': {
2916 const char *name = argv[argi] + 2;
2917 if (false);
2918 else if (strcmp(name, "host") == 0)
2919 flags |= kSecCodeSignatureHost;
2920 else if (strcmp(name, "adhoc") == 0)
2921 flags |= kSecCodeSignatureAdhoc;
2922 else if (strcmp(name, "hard") == 0)
2923 flags |= kSecCodeSignatureForceHard;
2924 else if (strcmp(name, "kill") == 0)
2925 flags |= kSecCodeSignatureForceKill;
2926 else if (strcmp(name, "expires") == 0)
2927 flags |= kSecCodeSignatureForceExpiration;
2928 else if (strcmp(name, "restrict") == 0)
2929 flags |= kSecCodeSignatureRestrict;
2930 else if (strcmp(name, "enforcement") == 0)
2931 flags |= kSecCodeSignatureEnforcement;
2932 else if (strcmp(name, "library-validation") == 0)
2933 flags |= kSecCodeSignatureLibraryValidation;
2934 else _assert(false);
2935 } break;
2936
2937 case 'P':
2938 platform = true;
2939 break;
2940
2941 case 's':
2942 _assert(!flag_r);
2943 _assert(!flag_S);
2944 flag_s = true;
2945 break;
2946
2947 case 'S':
2948 _assert(!flag_r);
2949 _assert(!flag_s);
2950 flag_S = true;
2951 if (argv[argi][2] != '\0') {
2952 const char *xml = argv[argi] + 2;
2953 entitlements.open(xml, O_RDONLY, PROT_READ, MAP_PRIVATE);
2954 }
2955 break;
2956
2957 case 'M':
2958 flag_M = true;
2959 break;
2960
2961 case 'K':
2962 if (argv[argi][2] != '\0')
2963 key.open(argv[argi] + 2, O_RDONLY, PROT_READ, MAP_PRIVATE);
2964 break;
2965
2966 #ifndef LDID_NOFLAGT
2967 case 'T': {
2968 flag_T = true;
2969 if (argv[argi][2] == '-')
2970 timeh = true;
2971 else {
2972 char *arge;
2973 timev = strtoul(argv[argi] + 2, &arge, 0);
2974 _assert(arge == argv[argi] + strlen(argv[argi]));
2975 }
2976 } break;
2977 #endif
2978
2979 case 'u': {
2980 flag_u = true;
2981 } break;
2982
2983 case 'I': {
2984 flag_I = argv[argi] + 2;
2985 } break;
2986
2987 default:
2988 usage(argv[0]);
2989 return 1;
2990 break;
2991 }
2992
2993 _assert(flag_S || key.empty());
2994 _assert(flag_S || flag_I == NULL);
2995
2996 if (files.empty())
2997 return 0;
2998
2999 size_t filei(0), filee(0);
3000 _foreach (file, files) try {
3001 std::string path(file);
3002
3003 struct stat info;
3004 _syscall(stat(path.c_str(), &info));
3005
3006 if (S_ISDIR(info.st_mode)) {
3007 #ifndef LDID_NOPLIST
3008 _assert(!flag_r);
3009 ldid::DiskFolder folder(path);
3010 path += "/" + Sign("", folder, key, requirements, ldid::fun([&](const std::string &, const std::string &) -> std::string { return entitlements; }), dummy_).path;
3011 #else
3012 _assert(false);
3013 #endif
3014 } else if (flag_S || flag_r) {
3015 Map input(path, O_RDONLY, PROT_READ, MAP_PRIVATE);
3016
3017 std::filebuf output;
3018 Split split(path);
3019 auto temp(Temporary(output, split));
3020
3021 if (flag_r)
3022 ldid::Unsign(input.data(), input.size(), output, dummy_);
3023 else {
3024 std::string identifier(flag_I ?: split.base.c_str());
3025 ldid::Sign(input.data(), input.size(), output, identifier, entitlements, flag_M, requirements, key, slots, flags, platform, dummy_);
3026 }
3027
3028 Commit(path, temp);
3029 }
3030
3031 bool modify(false);
3032 #ifndef LDID_NOFLAGT
3033 if (flag_T)
3034 modify = true;
3035 #endif
3036 if (flag_s)
3037 modify = true;
3038
3039 Map mapping(path, modify);
3040 FatHeader fat_header(mapping.data(), mapping.size());
3041
3042 _foreach (mach_header, fat_header.GetMachHeaders()) {
3043 struct linkedit_data_command *signature(NULL);
3044 struct encryption_info_command *encryption(NULL);
3045
3046 if (flag_A) {
3047 if (mach_header.GetCPUType() != flag_CPUType)
3048 continue;
3049 if (mach_header.GetCPUSubtype() != flag_CPUSubtype)
3050 continue;
3051 }
3052
3053 if (flag_a)
3054 printf("cpu=0x%x:0x%x\n", mach_header.GetCPUType(), mach_header.GetCPUSubtype());
3055
3056 _foreach (load_command, mach_header.GetLoadCommands()) {
3057 uint32_t cmd(mach_header.Swap(load_command->cmd));
3058
3059 if (false);
3060 else if (cmd == LC_CODE_SIGNATURE)
3061 signature = reinterpret_cast<struct linkedit_data_command *>(load_command);
3062 else if (cmd == LC_ENCRYPTION_INFO || cmd == LC_ENCRYPTION_INFO_64)
3063 encryption = reinterpret_cast<struct encryption_info_command *>(load_command);
3064 else if (cmd == LC_LOAD_DYLIB) {
3065 volatile struct dylib_command *dylib_command(reinterpret_cast<struct dylib_command *>(load_command));
3066 const char *name(reinterpret_cast<const char *>(load_command) + mach_header.Swap(dylib_command->dylib.name));
3067
3068 if (strcmp(name, "/System/Library/Frameworks/UIKit.framework/UIKit") == 0) {
3069 if (flag_u) {
3070 Version version;
3071 version.value = mach_header.Swap(dylib_command->dylib.current_version);
3072 printf("uikit=%u.%u.%u\n", version.major, version.minor, version.patch);
3073 }
3074 }
3075 }
3076 #ifndef LDID_NOFLAGT
3077 else if (cmd == LC_ID_DYLIB) {
3078 volatile struct dylib_command *dylib_command(reinterpret_cast<struct dylib_command *>(load_command));
3079
3080 if (flag_T) {
3081 uint32_t timed;
3082
3083 if (!timeh)
3084 timed = timev;
3085 else {
3086 dylib_command->dylib.timestamp = 0;
3087 timed = hash(reinterpret_cast<uint8_t *>(mach_header.GetBase()), mach_header.GetSize(), timev);
3088 }
3089
3090 dylib_command->dylib.timestamp = mach_header.Swap(timed);
3091 }
3092 }
3093 #endif
3094 }
3095
3096 if (flag_D) {
3097 _assert(encryption != NULL);
3098 encryption->cryptid = mach_header.Swap(0);
3099 }
3100
3101 if (flag_e) {
3102 _assert(signature != NULL);
3103
3104 uint32_t data = mach_header.Swap(signature->dataoff);
3105
3106 uint8_t *top = reinterpret_cast<uint8_t *>(mach_header.GetBase());
3107 uint8_t *blob = top + data;
3108 struct SuperBlob *super = reinterpret_cast<struct SuperBlob *>(blob);
3109
3110 for (size_t index(0); index != Swap(super->count); ++index)
3111 if (Swap(super->index[index].type) == CSSLOT_ENTITLEMENTS) {
3112 uint32_t begin = Swap(super->index[index].offset);
3113 struct Blob *entitlements = reinterpret_cast<struct Blob *>(blob + begin);
3114 fwrite(entitlements + 1, 1, Swap(entitlements->length) - sizeof(*entitlements), stdout);
3115 }
3116 }
3117
3118 if (flag_q) {
3119 _assert(signature != NULL);
3120
3121 uint32_t data = mach_header.Swap(signature->dataoff);
3122
3123 uint8_t *top = reinterpret_cast<uint8_t *>(mach_header.GetBase());
3124 uint8_t *blob = top + data;
3125 struct SuperBlob *super = reinterpret_cast<struct SuperBlob *>(blob);
3126
3127 for (size_t index(0); index != Swap(super->count); ++index)
3128 if (Swap(super->index[index].type) == CSSLOT_REQUIREMENTS) {
3129 uint32_t begin = Swap(super->index[index].offset);
3130 struct Blob *requirement = reinterpret_cast<struct Blob *>(blob + begin);
3131 fwrite(requirement, 1, Swap(requirement->length), stdout);
3132 }
3133 }
3134
3135 if (flag_s) {
3136 _assert(signature != NULL);
3137
3138 uint32_t data = mach_header.Swap(signature->dataoff);
3139
3140 uint8_t *top = reinterpret_cast<uint8_t *>(mach_header.GetBase());
3141 uint8_t *blob = top + data;
3142 struct SuperBlob *super = reinterpret_cast<struct SuperBlob *>(blob);
3143
3144 for (size_t index(0); index != Swap(super->count); ++index)
3145 if (Swap(super->index[index].type) == CSSLOT_CODEDIRECTORY) {
3146 uint32_t begin = Swap(super->index[index].offset);
3147 struct CodeDirectory *directory = reinterpret_cast<struct CodeDirectory *>(blob + begin + sizeof(Blob));
3148
3149 uint8_t (*hashes)[LDID_SHA1_DIGEST_LENGTH] = reinterpret_cast<uint8_t (*)[LDID_SHA1_DIGEST_LENGTH]>(blob + begin + Swap(directory->hashOffset));
3150 uint32_t pages = Swap(directory->nCodeSlots);
3151
3152 if (pages != 1)
3153 for (size_t i = 0; i != pages - 1; ++i)
3154 LDID_SHA1(top + PageSize_ * i, PageSize_, hashes[i]);
3155 if (pages != 0)
3156 LDID_SHA1(top + PageSize_ * (pages - 1), ((data - 1) % PageSize_) + 1, hashes[pages - 1]);
3157 }
3158 }
3159 }
3160
3161 ++filei;
3162 } catch (const char *) {
3163 ++filee;
3164 ++filei;
3165 }
3166
3167 return filee;
3168 }
3169 #endif