1 /* ldid - (Mach-O) Link-Loader Identity Editor
2 * Copyright (C) 2007-2015 Jay Freeman (saurik)
5 /* SPDX-License-Identifier: AGPL-3.0-only */
6 /* GNU Affero General Public License, Version 3 {{{ */
8 * This program is free software: you can redistribute it and/or modify
9 * it under the terms of the GNU Affero General Public License as published by
10 * the Free Software Foundation, either version 3 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU Affero General Public License for more details.
18 * You should have received a copy of the GNU Affero General Public License
19 * along with this program. If not, see <http://www.gnu.org/licenses/>.
45 #include <sys/types.h>
47 #include <openssl/opensslv.h>
48 # if OPENSSL_VERSION_MAJOR >= 3
49 # include <openssl/provider.h>
51 #include <openssl/err.h>
52 #include <openssl/pem.h>
53 #include <openssl/pkcs7.h>
54 #include <openssl/pkcs12.h>
55 #include <openssl/ui.h>
57 #include <openssl/evp.h>
59 #include <plist/plist.h>
65 #define _assert___(line) \
67 #define _assert__(line) \
71 #define $(value) value
75 #define _assert_(expr, format, ...) \
77 fprintf(stderr, $("%s(%u): _assert(): " format "\n"), __FILE__, __LINE__, ## __VA_ARGS__); \
78 throw $(__FILE__ "(" _assert__(__LINE__) "): _assert(" #expr ")"); \
81 // XXX: this is not acceptable
82 #define _assert_(expr, format, ...) \
84 fprintf(stderr, $("%s(%u): _assert(): " format "\n"), __FILE__, __LINE__, ## __VA_ARGS__); \
89 #define _assert(expr) \
90 _assert_(expr, "%s", $(#expr))
92 #define _syscall(expr, ...) [&] { for (;;) { \
94 if ((long) _value != -1) \
99 /* XXX: EINTR is included in this list to fix g++ */ \
100 for (auto success : (long[]) {EINTR, __VA_ARGS__}) \
101 if (error == success) \
102 return (decltype(expr)) -success; \
103 fprintf(stderr, "ldid: %s: %s\n", __func__, strerror(error)); \
108 fprintf(stderr, $("_trace(%s:%u): %s\n"), __FILE__, __LINE__, $(__FUNCTION__))
114 __attribute__((packed))
116 std::string password
;
117 std::vector
<std::string
> cleanup
;
119 template <typename Type_
>
121 typedef typename
Type_::const_iterator Result
;
124 #define _foreach(item, list) \
125 for (bool _stop(true); _stop; ) \
126 for (const __typeof__(list) &_list = (list); _stop; _stop = false) \
127 for (Iterator_<__typeof__(list)>::Result _item = _list.begin(); _item != _list.end(); ++_item) \
128 for (bool _suck(true); _suck; _suck = false) \
129 for (const __typeof__(*_item) &item = *_item; _suck; _suck = false)
134 template <typename Function_
>
142 Scope(const Function_
&function
) :
152 template <typename Function_
>
153 Scope
<Function_
> _scope(const Function_
&function
) {
154 return Scope
<Function_
>(function
);
157 #define _scope__(counter, function) \
158 __attribute__((__unused__)) \
159 const _Scope &_scope ## counter(_scope([&]function))
160 #define _scope_(counter, function) \
161 _scope__(counter, function)
162 #define _scope(function) \
163 _scope_(__COUNTER__, function)
170 #define FAT_MAGIC 0xcafebabe
171 #define FAT_CIGAM 0xbebafeca
191 #define MH_MAGIC 0xfeedface
192 #define MH_CIGAM 0xcefaedfe
194 #define MH_MAGIC_64 0xfeedfacf
195 #define MH_CIGAM_64 0xcffaedfe
197 #define MH_DYLDLINK 0x4
199 #define MH_OBJECT 0x1
200 #define MH_EXECUTE 0x2
202 #define MH_DYLINKER 0x7
203 #define MH_BUNDLE 0x8
204 #define MH_DYLIB_STUB 0x9
206 struct load_command
{
211 #define LC_REQ_DYLD uint32_t(0x80000000)
213 #define LC_SEGMENT uint32_t(0x01)
214 #define LC_SYMTAB uint32_t(0x02)
215 #define LC_DYSYMTAB uint32_t(0x0b)
216 #define LC_LOAD_DYLIB uint32_t(0x0c)
217 #define LC_ID_DYLIB uint32_t(0x0d)
218 #define LC_SEGMENT_64 uint32_t(0x19)
219 #define LC_UUID uint32_t(0x1b)
220 #define LC_CODE_SIGNATURE uint32_t(0x1d)
221 #define LC_SEGMENT_SPLIT_INFO uint32_t(0x1e)
222 #define LC_REEXPORT_DYLIB uint32_t(0x1f | LC_REQ_DYLD)
223 #define LC_ENCRYPTION_INFO uint32_t(0x21)
224 #define LC_DYLD_INFO uint32_t(0x22)
225 #define LC_DYLD_INFO_ONLY uint32_t(0x22 | LC_REQ_DYLD)
226 #define LC_ENCRYPTION_INFO_64 uint32_t(0x2c)
241 uint32_t current_version
;
242 uint32_t compatibility_version
;
245 struct dylib_command
{
251 struct uuid_command
{
257 struct symtab_command
{
266 struct dyld_info_command
{
270 uint32_t rebase_size
;
273 uint32_t weak_bind_off
;
274 uint32_t weak_bind_size
;
275 uint32_t lazy_bind_off
;
276 uint32_t lazy_bind_size
;
278 uint32_t export_size
;
281 struct dysymtab_command
{
294 uint32_t extrefsymoff
;
295 uint32_t nextrefsyms
;
296 uint32_t indirectsymoff
;
297 uint32_t nindirectsyms
;
304 struct dylib_table_of_contents
{
305 uint32_t symbol_index
;
306 uint32_t module_index
;
309 struct dylib_module
{
310 uint32_t module_name
;
319 uint32_t iinit_iterm
;
320 uint32_t ninit_nterm
;
321 uint32_t objc_module_info_addr
;
322 uint32_t objc_module_info_size
;
325 struct dylib_reference
{
330 struct relocation_info
{
332 uint32_t r_symbolnum
:24;
351 struct segment_command
{
365 struct segment_command_64
{
408 struct linkedit_data_command
{
415 struct encryption_info_command
{
423 #define BIND_OPCODE_MASK 0xf0
424 #define BIND_IMMEDIATE_MASK 0x0f
425 #define BIND_OPCODE_DONE 0x00
426 #define BIND_OPCODE_SET_DYLIB_ORDINAL_IMM 0x10
427 #define BIND_OPCODE_SET_DYLIB_ORDINAL_ULEB 0x20
428 #define BIND_OPCODE_SET_DYLIB_SPECIAL_IMM 0x30
429 #define BIND_OPCODE_SET_SYMBOL_TRAILING_FLAGS_IMM 0x40
430 #define BIND_OPCODE_SET_TYPE_IMM 0x50
431 #define BIND_OPCODE_SET_ADDEND_SLEB 0x60
432 #define BIND_OPCODE_SET_SEGMENT_AND_OFFSET_ULEB 0x70
433 #define BIND_OPCODE_ADD_ADDR_ULEB 0x80
434 #define BIND_OPCODE_DO_BIND 0x90
435 #define BIND_OPCODE_DO_BIND_ADD_ADDR_ULEB 0xa0
436 #define BIND_OPCODE_DO_BIND_ADD_ADDR_IMM_SCALED 0xb0
437 #define BIND_OPCODE_DO_BIND_ULEB_TIMES_SKIPPING_ULEB 0xc0
439 struct : ldid::Progress
{
440 virtual void operator()(const std::string
&value
) const {
443 virtual void operator()(double value
) const {
447 struct Progression
: ldid::Progress
{
448 const ldid::Progress
&progress_
;
451 Progression(const ldid::Progress
&progress
, const std::string
&name
) :
457 virtual void operator()(const std::string
&value
) const {
458 return progress_(name_
+ " (" + value
+ ")");
461 virtual void operator()(double value
) const {
462 return progress_(value
);
466 static std::streamsize
read(std::streambuf
&stream
, void *data
, size_t size
) {
467 auto writ(stream
.sgetn(static_cast<char *>(data
), size
));
472 static inline void put(std::streambuf
&stream
, uint8_t value
) {
473 _assert(stream
.sputc(value
) != EOF
);
476 static inline void get(std::streambuf
&stream
, void *data
, size_t size
) {
477 _assert(read(stream
, data
, size
) == size
);
480 static inline void put(std::streambuf
&stream
, const void *data
, size_t size
) {
481 _assert(stream
.sputn(static_cast<const char *>(data
), size
) == size
);
484 static inline void put(std::streambuf
&stream
, const void *data
, size_t size
, const ldid::Progress
&progress
) {
486 for (size_t total(0); total
!= size
;) {
487 auto writ(std::min(size
- total
, size_t(4096 * 4)));
488 _assert(stream
.sputn(static_cast<const char *>(data
) + total
, writ
) == writ
);
490 progress(double(total
) / size
);
494 static inline void put(std::streambuf
&stream
, const std::string
&data
) {
495 return put(stream
, data
.data(), data
.size());
498 static size_t most(std::streambuf
&stream
, void *data
, size_t size
) {
501 if (auto writ
= read(stream
, data
, size
))
507 static inline void pad(std::streambuf
&stream
, size_t size
) {
509 memset(padding
, 0, size
);
510 put(stream
, padding
, size
);
513 template <typename Type_
>
514 Type_
Align(Type_ value
, size_t align
) {
521 static const uint8_t PageShift_(0x0c);
522 static const uint32_t PageSize_(1 << PageShift_
);
524 static inline unsigned bytes(uint64_t value
) {
525 return (64 - __builtin_clzll(value
) + 7) / 8;
528 static void put(std::streambuf
&stream
, uint64_t value
, size_t length
) {
530 do put(stream
, uint8_t(value
>> (length
-= 8)));
534 static void der(std::streambuf
&stream
, uint64_t value
) {
538 unsigned length(bytes(value
));
539 put(stream
, 0x80 | length
);
540 put(stream
, value
, length
);
544 static std::string
der(uint8_t tag
, const char *value
, size_t length
) {
548 put(data
, value
, length
);
552 static std::string
der(uint8_t tag
, const char *value
) {
553 return der(tag
, value
, strlen(value
)); }
554 static std::string
der(uint8_t tag
, const std::string
&value
) {
555 return der(tag
, value
.data(), value
.size()); }
557 template <typename Type_
>
558 static void der_(std::stringbuf
&data
, const Type_
&values
) {
560 for (const auto &value
: values
)
561 size
+= value
.size();
563 for (const auto &value
: values
)
567 static std::string
der(const std::vector
<std::string
> &values
) {
574 static std::string
der(const std::multiset
<std::string
> &values
) {
581 static std::string
der(const std::pair
<std::string
, std::string
> &value
) {
582 const auto key(der(0x0c, value
.first
));
585 der(data
, key
.size() + value
.second
.size());
587 put(data
, value
.second
);
591 static std::string
der(plist_t data
) {
592 switch (const auto type
= plist_get_node_type(data
)) {
593 case PLIST_BOOLEAN
: {
595 plist_get_bool_val(data
, &value
);
600 put(data
, value
!= 0 ? 1 : 0);
606 plist_get_uint_val(data
, &value
);
607 const auto length(bytes(value
));
612 put(data
, value
, length
);
617 fprintf(stderr
, "ldid: Invalid plist entry type\n");
622 fprintf(stderr
, "ldid: Invalid plist entry type\n");
629 plist_get_data_val(data
, &value
, &length
);
630 _scope({ free(value
); });
631 return der(0x04, value
, length
);
636 plist_get_string_val(data
, &value
);
637 _scope({ free(value
); });
638 return der(0x0c, value
);
642 std::vector
<std::string
> values
;
643 for (auto e(plist_array_get_size(data
)), i(decltype(e
)(0)); i
!= e
; ++i
)
644 values
.push_back(der(plist_array_get_item(data
, i
)));
649 std::multiset
<std::string
> values
;
651 plist_dict_iter
iterator(NULL
);
652 plist_dict_new_iter(data
, &iterator
);
653 _scope({ free(iterator
); });
658 plist_dict_next_item(data
, iterator
, &key
, &value
);
661 _scope({ free(key
); });
662 values
.insert(der(std::make_pair(key
, der(value
))));
669 fprintf(stderr
, "ldid: Unsupported plist type %d", type
);
675 static inline uint16_t Swap_(uint16_t value
) {
677 ((value
>> 8) & 0x00ff) |
678 ((value
<< 8) & 0xff00);
681 static inline uint32_t Swap_(uint32_t value
) {
682 value
= ((value
>> 8) & 0x00ff00ff) |
683 ((value
<< 8) & 0xff00ff00);
684 value
= ((value
>> 16) & 0x0000ffff) |
685 ((value
<< 16) & 0xffff0000);
689 static inline uint64_t Swap_(uint64_t value
) {
690 value
= (value
& 0x00000000ffffffff) << 32 | (value
& 0xffffffff00000000) >> 32;
691 value
= (value
& 0x0000ffff0000ffff) << 16 | (value
& 0xffff0000ffff0000) >> 16;
692 value
= (value
& 0x00ff00ff00ff00ff) << 8 | (value
& 0xff00ff00ff00ff00) >> 8;
696 static inline int16_t Swap_(int16_t value
) {
697 return Swap_(static_cast<uint16_t>(value
));
700 static inline int32_t Swap_(int32_t value
) {
701 return Swap_(static_cast<uint32_t>(value
));
704 static inline int64_t Swap_(int64_t value
) {
705 return Swap_(static_cast<uint64_t>(value
));
708 static bool little_(true);
710 static inline uint16_t Swap(uint16_t value
) {
711 return little_
? Swap_(value
) : value
;
714 static inline uint32_t Swap(uint32_t value
) {
715 return little_
? Swap_(value
) : value
;
718 static inline uint64_t Swap(uint64_t value
) {
719 return little_
? Swap_(value
) : value
;
722 static inline int16_t Swap(int16_t value
) {
723 return Swap(static_cast<uint16_t>(value
));
726 static inline int32_t Swap(int32_t value
) {
727 return Swap(static_cast<uint32_t>(value
));
730 static inline int64_t Swap(int64_t value
) {
731 return Swap(static_cast<uint64_t>(value
));
744 Swapped(bool swapped
) :
749 template <typename Type_
>
750 Type_
Swap(Type_ value
) const {
751 return swapped_
? Swap_(value
) : value
;
763 Data(void *base
, size_t size
) :
769 void *GetBase() const {
773 size_t GetSize() const {
784 struct mach_header
*mach_header_
;
785 struct load_command
*load_command_
;
788 MachHeader(void *base
, size_t size
) :
791 mach_header_
= (mach_header
*) base
;
793 switch (Swap(mach_header_
->magic
)) {
795 swapped_
= !swapped_
;
801 swapped_
= !swapped_
;
807 fprintf(stderr
, "ldid: Unknown header magic\nAre you sure that is a Mach-O?\n");
811 void *post
= mach_header_
+ 1;
813 post
= (uint32_t *) post
+ 1;
814 load_command_
= (struct load_command
*) post
;
816 if (Swap(mach_header_
->filetype
) != MH_EXECUTE
&&
817 Swap(mach_header_
->filetype
) != MH_DYLIB
&&
818 Swap(mach_header_
->filetype
) != MH_DYLINKER
&&
819 Swap(mach_header_
->filetype
) != MH_BUNDLE
) {
820 fprintf(stderr
, "ldid: Unsupported Mach-O type\n");
825 bool Bits64() const {
829 struct mach_header
*operator ->() const {
833 operator struct mach_header
*() const {
837 uint32_t GetCPUType() const {
838 return Swap(mach_header_
->cputype
);
841 uint32_t GetCPUSubtype() const {
842 return Swap(mach_header_
->cpusubtype
) & 0xff;
845 struct load_command
*GetLoadCommand() const {
846 return load_command_
;
849 std::vector
<struct load_command
*> GetLoadCommands() const {
850 std::vector
<struct load_command
*> load_commands
;
852 struct load_command
*load_command
= load_command_
;
853 for (uint32_t cmd
= 0; cmd
!= Swap(mach_header_
->ncmds
); ++cmd
) {
854 load_commands
.push_back(load_command
);
855 load_command
= (struct load_command
*) ((uint8_t *) load_command
+ Swap(load_command
->cmdsize
));
858 return load_commands
;
861 void ForSection(const ldid::Functor
<void (const char *, const char *, void *, size_t)> &code
) const {
862 _foreach (load_command
, GetLoadCommands())
863 switch (Swap(load_command
->cmd
)) {
865 auto segment(reinterpret_cast<struct segment_command
*>(load_command
));
866 code(segment
->segname
, NULL
, GetOffset
<void>(segment
->fileoff
), segment
->filesize
);
867 auto section(reinterpret_cast<struct section
*>(segment
+ 1));
868 for (uint32_t i(0), e(Swap(segment
->nsects
)); i
!= e
; ++i
, ++section
)
869 code(segment
->segname
, section
->sectname
, GetOffset
<void>(segment
->fileoff
+ section
->offset
), section
->size
);
872 case LC_SEGMENT_64
: {
873 auto segment(reinterpret_cast<struct segment_command_64
*>(load_command
));
874 code(segment
->segname
, NULL
, GetOffset
<void>(segment
->fileoff
), segment
->filesize
);
875 auto section(reinterpret_cast<struct section_64
*>(segment
+ 1));
876 for (uint32_t i(0), e(Swap(segment
->nsects
)); i
!= e
; ++i
, ++section
)
877 code(segment
->segname
, section
->sectname
, GetOffset
<void>(segment
->fileoff
+ section
->offset
), section
->size
);
882 template <typename Target_
>
883 Target_
*GetOffset(uint32_t offset
) const {
884 return reinterpret_cast<Target_
*>(offset
+ (uint8_t *) mach_header_
);
888 class FatMachHeader
:
895 FatMachHeader(void *base
, size_t size
, fat_arch
*fat_arch
) :
896 MachHeader(base
, size
),
901 fat_arch
*GetFatArch() const {
910 fat_header
*fat_header_
;
911 std::vector
<FatMachHeader
> mach_headers_
;
914 FatHeader(void *base
, size_t size
) :
917 fat_header_
= reinterpret_cast<struct fat_header
*>(base
);
919 if (Swap(fat_header_
->magic
) == FAT_CIGAM
) {
920 swapped_
= !swapped_
;
922 } else if (Swap(fat_header_
->magic
) != FAT_MAGIC
) {
924 mach_headers_
.push_back(FatMachHeader(base
, size
, NULL
));
926 size_t fat_narch
= Swap(fat_header_
->nfat_arch
);
927 fat_arch
*fat_arch
= reinterpret_cast<struct fat_arch
*>(fat_header_
+ 1);
929 for (arch
= 0; arch
!= fat_narch
; ++arch
) {
930 uint32_t arch_offset
= Swap(fat_arch
->offset
);
931 uint32_t arch_size
= Swap(fat_arch
->size
);
932 mach_headers_
.push_back(FatMachHeader((uint8_t *) base
+ arch_offset
, arch_size
, fat_arch
));
938 std::vector
<FatMachHeader
> &GetMachHeaders() {
939 return mach_headers_
;
943 return fat_header_
!= NULL
;
946 struct fat_header
*operator ->() const {
950 operator struct fat_header
*() const {
955 #define CSMAGIC_REQUIREMENT uint32_t(0xfade0c00)
956 #define CSMAGIC_REQUIREMENTS uint32_t(0xfade0c01)
957 #define CSMAGIC_CODEDIRECTORY uint32_t(0xfade0c02)
958 #define CSMAGIC_EMBEDDED_SIGNATURE uint32_t(0xfade0cc0)
959 #define CSMAGIC_EMBEDDED_SIGNATURE_OLD uint32_t(0xfade0b02)
960 #define CSMAGIC_EMBEDDED_ENTITLEMENTS uint32_t(0xfade7171)
961 #define CSMAGIC_EMBEDDED_DERFORMAT uint32_t(0xfade7172) // name?
962 #define CSMAGIC_DETACHED_SIGNATURE uint32_t(0xfade0cc1)
963 #define CSMAGIC_BLOBWRAPPER uint32_t(0xfade0b01)
965 #define CSSLOT_CODEDIRECTORY uint32_t(0x00000)
966 #define CSSLOT_INFOSLOT uint32_t(0x00001)
967 #define CSSLOT_REQUIREMENTS uint32_t(0x00002)
968 #define CSSLOT_RESOURCEDIR uint32_t(0x00003)
969 #define CSSLOT_APPLICATION uint32_t(0x00004)
970 #define CSSLOT_ENTITLEMENTS uint32_t(0x00005)
971 #define CSSLOT_REPSPECIFIC uint32_t(0x00006) // name?
972 #define CSSLOT_DERFORMAT uint32_t(0x00007) // name?
973 #define CSSLOT_ALTERNATE uint32_t(0x01000)
975 #define CSSLOT_SIGNATURESLOT uint32_t(0x10000)
977 #define CS_HASHTYPE_SHA160_160 1
978 #define CS_HASHTYPE_SHA256_256 2
979 #define CS_HASHTYPE_SHA256_160 3
980 #define CS_HASHTYPE_SHA386_386 4
983 #define CS_EXECSEG_MAIN_BINARY 0x001 /* executable segment denotes main binary */
984 #define CS_EXECSEG_ALLOW_UNSIGNED 0x010 /* allow unsigned pages (for debugging) */
985 #define CS_EXECSEG_DEBUGGER 0x020 /* main binary is debugger */
986 #define CS_EXECSEG_JIT 0x040 /* JIT enabled */
987 #define CS_EXECSEG_SKIP_LV 0x080 /* skip library validation */
988 #define CS_EXECSEG_CAN_LOAD_CDHASH 0x100 /* can bless cdhash for execution */
989 #define CS_EXECSEG_CAN_EXEC_CDHASH 0x200 /* can execute blessed cdhash */
991 enum SecCodeExecSegFlags
{
992 kSecCodeExecSegMainBinary
= 0x001,
993 kSecCodeExecSegAllowUnsigned
= 0x010,
994 kSecCodeExecSegDebugger
= 0x020,
995 kSecCodeExecSegJit
= 0x040,
996 kSecCodeExecSegSkipLibraryVal
= 0x080,
997 kSecCodeExecSegCanLoadCdHash
= 0x100,
998 kSecCodeExecSegCanExecCdHash
= 0x100,
1015 struct BlobIndex index
[];
1018 struct CodeDirectory
{
1021 uint32_t hashOffset
;
1022 uint32_t identOffset
;
1023 uint32_t nSpecialSlots
;
1024 uint32_t nCodeSlots
;
1031 uint32_t scatterOffset
;
1032 uint32_t teamIDOffset
;
1034 uint64_t codeLimit64
;
1035 uint64_t execSegBase
;
1036 uint64_t execSegLimit
;
1037 uint64_t execSegFlags
;
1038 #if 0 // version = 0x20500
1040 uint32_t preEncryptOffset
;
1042 #if 0 // version = 0x20600
1043 uint8_t linkageHashType
;
1044 uint8_t linkageTruncated
;
1046 uint32_t linkageOffset
;
1047 uint32_t linkageSize
;
1051 enum CodeSignatureFlags
{
1052 kSecCodeSignatureHost
= 0x0001,
1053 kSecCodeSignatureAdhoc
= 0x0002,
1054 kSecCodeSignatureForceHard
= 0x0100,
1055 kSecCodeSignatureForceKill
= 0x0200,
1056 kSecCodeSignatureForceExpiration
= 0x0400,
1057 kSecCodeSignatureRestrict
= 0x0800,
1058 kSecCodeSignatureEnforcement
= 0x1000,
1059 kSecCodeSignatureLibraryValidation
= 0x2000,
1060 kSecCodeSignatureRuntime
= 0x10000,
1063 enum Kind
: uint32_t {
1064 exprForm
= 1, // prefix expr form
1067 enum ExprOp
: uint32_t {
1068 opFalse
, // unconditionally false
1069 opTrue
, // unconditionally true
1070 opIdent
, // match canonical code [string]
1071 opAppleAnchor
, // signed by Apple as Apple's product
1072 opAnchorHash
, // match anchor [cert hash]
1073 opInfoKeyValue
, // *legacy* - use opInfoKeyField [key; value]
1074 opAnd
, // binary prefix expr AND expr [expr; expr]
1075 opOr
, // binary prefix expr OR expr [expr; expr]
1076 opCDHash
, // match hash of CodeDirectory directly [cd hash]
1077 opNot
, // logical inverse [expr]
1078 opInfoKeyField
, // Info.plist key field [string; match suffix]
1079 opCertField
, // Certificate field [cert index; field name; match suffix]
1080 opTrustedCert
, // require trust settings to approve one particular cert [cert index]
1081 opTrustedCerts
, // require trust settings to approve the cert chain
1082 opCertGeneric
, // Certificate component by OID [cert index; oid; match suffix]
1083 opAppleGenericAnchor
, // signed by Apple in any capacity
1084 opEntitlementField
, // entitlement dictionary field [string; match suffix]
1085 opCertPolicy
, // Certificate policy by OID [cert index; oid; match suffix]
1086 opNamedAnchor
, // named anchor type
1087 opNamedCode
, // named subroutine
1088 opPlatform
, // platform constraint [integer]
1089 exprOpCount
// (total opcode count in use)
1092 enum MatchOperation
{
1093 matchExists
, // anything but explicit "false" - no value stored
1094 matchEqual
, // equal (CFEqual)
1095 matchContains
, // partial match (substring)
1096 matchBeginsWith
, // partial match (initial substring)
1097 matchEndsWith
, // partial match (terminal substring)
1098 matchLessThan
, // less than (string with numeric comparison)
1099 matchGreaterThan
, // greater than (string with numeric comparison)
1100 matchLessEqual
, // less or equal (string with numeric comparison)
1101 matchGreaterEqual
, // greater or equal (string with numeric comparison)
1104 #define OID_ISO_MEMBER 42
1105 #define OID_US OID_ISO_MEMBER, 134, 72
1106 #define APPLE_OID OID_US, 0x86, 0xf7, 0x63
1107 #define APPLE_ADS_OID APPLE_OID, 0x64
1108 #define APPLE_EXTENSION_OID APPLE_ADS_OID, 6
1115 Algorithm(size_t size
, uint8_t type
) :
1121 virtual const uint8_t *operator [](const ldid::Hash
&hash
) const = 0;
1123 virtual void operator ()(uint8_t *hash
, const void *data
, size_t size
) const = 0;
1124 virtual void operator ()(ldid::Hash
&hash
, const void *data
, size_t size
) const = 0;
1125 virtual void operator ()(std::vector
<char> &hash
, const void *data
, size_t size
) const = 0;
1127 virtual const char *name() = 0;
1130 struct AlgorithmSHA1
:
1134 Algorithm(SHA_DIGEST_LENGTH
, CS_HASHTYPE_SHA160_160
)
1138 virtual const uint8_t *operator [](const ldid::Hash
&hash
) const {
1142 void operator ()(uint8_t *hash
, const void *data
, size_t size
) const {
1143 SHA1(static_cast<const uint8_t *>(data
), size
, hash
);
1146 void operator ()(ldid::Hash
&hash
, const void *data
, size_t size
) const {
1147 return operator()(hash
.sha1_
, data
, size
);
1150 void operator ()(std::vector
<char> &hash
, const void *data
, size_t size
) const {
1151 hash
.resize(SHA_DIGEST_LENGTH
);
1152 return operator ()(reinterpret_cast<uint8_t *>(hash
.data()), data
, size
);
1155 virtual const char *name() {
1160 struct AlgorithmSHA256
:
1164 Algorithm(SHA256_DIGEST_LENGTH
, CS_HASHTYPE_SHA256_256
)
1168 virtual const uint8_t *operator [](const ldid::Hash
&hash
) const {
1169 return hash
.sha256_
;
1172 void operator ()(uint8_t *hash
, const void *data
, size_t size
) const {
1173 SHA256(static_cast<const uint8_t *>(data
), size
, hash
);
1176 void operator ()(ldid::Hash
&hash
, const void *data
, size_t size
) const {
1177 return operator()(hash
.sha256_
, data
, size
);
1180 void operator ()(std::vector
<char> &hash
, const void *data
, size_t size
) const {
1181 hash
.resize(SHA256_DIGEST_LENGTH
);
1182 return operator ()(reinterpret_cast<uint8_t *>(hash
.data()), data
, size
);
1185 virtual const char *name() {
1190 static bool do_sha1(true);
1191 static bool do_sha256(true);
1193 static const std::vector
<Algorithm
*> &GetAlgorithms() {
1194 static AlgorithmSHA1 sha1
;
1195 static AlgorithmSHA256 sha256
;
1197 static std::vector
<Algorithm
*> algorithms
;
1198 if (algorithms
.empty()) {
1200 algorithms
.push_back(&sha1
);
1202 algorithms
.push_back(&sha256
);
1209 std::string entitlements_
;
1210 std::string derformat_
;
1213 struct CodesignAllocation
{
1214 FatMachHeader mach_header_
;
1223 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
) :
1224 mach_header_(mach_header
),
1236 #ifndef LDID_NOTOOLS
1249 _syscall(close(file_
));
1252 void open(const char *path
, int flags
) {
1253 file_
= ::open(path
, flags
);
1255 fprintf(stderr
, "ldid: %s: %s\n", path
, strerror(errno
));
1278 Map(const std::string
&path
, int oflag
, int pflag
, int mflag
) :
1281 open(path
, oflag
, pflag
, mflag
);
1284 Map(const std::string
&path
, bool edit
) :
1294 bool empty() const {
1295 return data_
== NULL
;
1298 void open(const std::string
&path
, int oflag
, int pflag
, int mflag
) {
1301 file_
.open(path
.c_str(), oflag
);
1302 int file(file_
.file());
1305 _syscall(fstat(file
, &stat
));
1306 size_
= stat
.st_size
;
1308 data_
= _syscall(mmap(NULL
, size_
, pflag
, mflag
, file
, 0));
1311 void open(const std::string
&path
, bool edit
) {
1313 open(path
, O_RDWR
, PROT_READ
| PROT_WRITE
, MAP_SHARED
);
1315 open(path
, O_RDONLY
, PROT_READ
, MAP_PRIVATE
);
1321 _syscall(munmap(data_
, size_
));
1326 void *data() const {
1330 size_t size() const {
1334 operator std::string() const {
1335 return std::string(static_cast<char *>(data_
), size_
);
1338 #endif // LDID_NOTOOLS
1342 static plist_t
plist(const std::string
&data
);
1344 void Analyze(const MachHeader
&mach_header
, const Functor
<void (const char *data
, size_t size
)> &entitle
) {
1345 _foreach (load_command
, mach_header
.GetLoadCommands())
1346 if (mach_header
.Swap(load_command
->cmd
) == LC_CODE_SIGNATURE
) {
1347 auto signature(reinterpret_cast<struct linkedit_data_command
*>(load_command
));
1348 auto offset(mach_header
.Swap(signature
->dataoff
));
1349 auto pointer(reinterpret_cast<uint8_t *>(mach_header
.GetBase()) + offset
);
1350 auto super(reinterpret_cast<struct SuperBlob
*>(pointer
));
1352 for (size_t index(0); index
!= Swap(super
->count
); ++index
)
1353 if (Swap(super
->index
[index
].type
) == CSSLOT_ENTITLEMENTS
) {
1354 auto begin(Swap(super
->index
[index
].offset
));
1355 auto blob(reinterpret_cast<struct Blob
*>(pointer
+ begin
));
1356 auto writ(Swap(blob
->length
) - sizeof(*blob
));
1357 entitle(reinterpret_cast<char *>(blob
+ 1), writ
);
1362 std::string
Analyze(const void *data
, size_t size
) {
1363 std::string entitlements
;
1365 FatHeader
fat_header(const_cast<void *>(data
), size
);
1366 _foreach (mach_header
, fat_header
.GetMachHeaders())
1367 Analyze(mach_header
, fun([&](const char *data
, size_t size
) {
1368 if (entitlements
.empty())
1369 entitlements
.assign(data
, size
);
1371 if (entitlements
.compare(0, entitlements
.size(), data
, size
) != 0) {
1372 fprintf(stderr
, "ldid: Entitlements do not match\n");
1377 return entitlements
;
1380 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, size_t, size_t, const std::string
&, const char *, const Progress
&)> &save
, const Progress
&progress
) {
1381 FatHeader
source(const_cast<void *>(idata
), isize
);
1385 offset
+= sizeof(fat_header
) + sizeof(fat_arch
) * source
.Swap(source
->nfat_arch
);
1387 std::vector
<CodesignAllocation
> allocations
;
1388 _foreach (mach_header
, source
.GetMachHeaders()) {
1389 struct linkedit_data_command
*signature(NULL
);
1390 struct symtab_command
*symtab(NULL
);
1392 _foreach (load_command
, mach_header
.GetLoadCommands()) {
1393 uint32_t cmd(mach_header
.Swap(load_command
->cmd
));
1394 if (cmd
== LC_CODE_SIGNATURE
)
1395 signature
= reinterpret_cast<struct linkedit_data_command
*>(load_command
);
1396 else if (cmd
== LC_SYMTAB
)
1397 symtab
= reinterpret_cast<struct symtab_command
*>(load_command
);
1401 if (signature
== NULL
)
1402 size
= mach_header
.GetSize();
1404 size
= mach_header
.Swap(signature
->dataoff
);
1405 _assert(size
<= mach_header
.GetSize());
1408 if (symtab
!= NULL
) {
1409 auto end(mach_header
.Swap(symtab
->stroff
) + mach_header
.Swap(symtab
->strsize
));
1410 if (symtab
->stroff
!= 0 || symtab
->strsize
!= 0) {
1411 _assert(end
<= size
);
1412 _assert(end
>= size
- 0x10);
1418 size_t alloc(allocate(mach_header
, baton
, size
));
1420 auto *fat_arch(mach_header
.GetFatArch());
1423 if (fat_arch
!= NULL
)
1424 align
= source
.Swap(fat_arch
->align
);
1425 else switch (mach_header
.GetCPUType()) {
1426 case CPU_TYPE_POWERPC
:
1427 case CPU_TYPE_POWERPC64
:
1429 case CPU_TYPE_X86_64
:
1433 case CPU_TYPE_ARM64
:
1434 case CPU_TYPE_ARM64_32
:
1442 const char *arch(NULL
);
1443 switch (mach_header
.GetCPUType()) {
1444 case CPU_TYPE_POWERPC
:
1447 case CPU_TYPE_POWERPC64
:
1453 case CPU_TYPE_X86_64
:
1459 case CPU_TYPE_ARM64
:
1462 case CPU_TYPE_ARM64_32
:
1467 offset
= Align(offset
, 1 << align
);
1469 uint32_t limit(size
);
1471 limit
= Align(limit
, 0x10);
1473 allocations
.push_back(CodesignAllocation(mach_header
, offset
, size
, limit
, alloc
, align
, arch
, baton
));
1474 offset
+= size
+ alloc
;
1475 offset
= Align(offset
, 0x10);
1480 if (source
.IsFat()) {
1481 fat_header fat_header
;
1482 fat_header
.magic
= Swap(FAT_MAGIC
);
1483 fat_header
.nfat_arch
= Swap(uint32_t(allocations
.size()));
1484 put(output
, &fat_header
, sizeof(fat_header
));
1485 position
+= sizeof(fat_header
);
1487 // XXX: support fat_arch_64 (not in my toolchain)
1488 // probably use C++14 generic lambda (not in my toolchain)
1491 _foreach (allocation
, allocations
) {
1492 const auto offset(allocation
.offset_
);
1493 const auto size(allocation
.limit_
+ allocation
.alloc_
);
1494 if (uint32_t(offset
) != offset
|| uint32_t(size
) != size
)
1498 }(), "FAT slice >=4GiB not currently supported");
1500 _foreach (allocation
, allocations
) {
1501 auto &mach_header(allocation
.mach_header_
);
1504 fat_arch
.cputype
= Swap(mach_header
->cputype
);
1505 fat_arch
.cpusubtype
= Swap(mach_header
->cpusubtype
);
1506 fat_arch
.offset
= Swap(uint32_t(allocation
.offset_
));
1507 fat_arch
.size
= Swap(uint32_t(allocation
.limit_
+ allocation
.alloc_
));
1508 fat_arch
.align
= Swap(allocation
.align_
);
1509 put(output
, &fat_arch
, sizeof(fat_arch
));
1510 position
+= sizeof(fat_arch
);
1514 _foreach (allocation
, allocations
) {
1515 progress(allocation
.arch_
);
1516 auto &mach_header(allocation
.mach_header_
);
1518 pad(output
, allocation
.offset_
- position
);
1519 position
= allocation
.offset_
;
1524 std::vector
<std::string
> commands
;
1526 _foreach (load_command
, mach_header
.GetLoadCommands()) {
1527 std::string
copy(reinterpret_cast<const char *>(load_command
), load_command
->cmdsize
);
1529 switch (mach_header
.Swap(load_command
->cmd
)) {
1530 case LC_CODE_SIGNATURE
:
1534 // XXX: this is getting ridiculous: provide a better abstraction
1537 auto segment_command(reinterpret_cast<struct segment_command
*>(©
[0]));
1539 if ((segment_command
->initprot
& 04) != 0) {
1540 auto begin(mach_header
.Swap(segment_command
->fileoff
));
1541 auto end(begin
+ mach_header
.Swap(segment_command
->filesize
));
1548 if (strncmp(segment_command
->segname
, "__LINKEDIT", 16) == 0) {
1549 size_t size(mach_header
.Swap(allocation
.limit_
+ allocation
.alloc_
- mach_header
.Swap(segment_command
->fileoff
)));
1550 segment_command
->filesize
= size
;
1551 segment_command
->vmsize
= Align(size
, 1 << allocation
.align_
);
1555 case LC_SEGMENT_64
: {
1556 auto segment_command(reinterpret_cast<struct segment_command_64
*>(©
[0]));
1558 if ((segment_command
->initprot
& 04) != 0) {
1559 auto begin(mach_header
.Swap(segment_command
->fileoff
));
1560 auto end(begin
+ mach_header
.Swap(segment_command
->filesize
));
1567 if (strncmp(segment_command
->segname
, "__LINKEDIT", 16) == 0) {
1568 size_t size(mach_header
.Swap(allocation
.limit_
+ allocation
.alloc_
- mach_header
.Swap(segment_command
->fileoff
)));
1569 segment_command
->filesize
= size
;
1570 segment_command
->vmsize
= Align(size
, 1 << allocation
.align_
);
1575 commands
.push_back(copy
);
1578 if (allocation
.alloc_
!= 0) {
1579 linkedit_data_command signature
;
1580 signature
.cmd
= mach_header
.Swap(LC_CODE_SIGNATURE
);
1581 signature
.cmdsize
= mach_header
.Swap(uint32_t(sizeof(signature
)));
1582 signature
.dataoff
= mach_header
.Swap(allocation
.limit_
);
1583 signature
.datasize
= mach_header
.Swap(allocation
.alloc_
);
1584 commands
.push_back(std::string(reinterpret_cast<const char *>(&signature
), sizeof(signature
)));
1587 size_t begin(position
);
1590 _foreach(command
, commands
)
1591 after
+= command
.size();
1593 std::stringbuf altern
;
1595 struct mach_header
header(*mach_header
);
1596 header
.ncmds
= mach_header
.Swap(uint32_t(commands
.size()));
1597 header
.sizeofcmds
= mach_header
.Swap(after
);
1598 put(output
, &header
, sizeof(header
));
1599 put(altern
, &header
, sizeof(header
));
1600 position
+= sizeof(header
);
1602 if (mach_header
.Bits64()) {
1603 auto pad(mach_header
.Swap(uint32_t(0)));
1604 put(output
, &pad
, sizeof(pad
));
1605 put(altern
, &pad
, sizeof(pad
));
1606 position
+= sizeof(pad
);
1609 _foreach(command
, commands
) {
1610 put(output
, command
.data(), command
.size());
1611 put(altern
, command
.data(), command
.size());
1612 position
+= command
.size();
1615 uint32_t before(mach_header
.Swap(mach_header
->sizeofcmds
));
1616 if (before
> after
) {
1617 pad(output
, before
- after
);
1618 pad(altern
, before
- after
);
1619 position
+= before
- after
;
1622 auto top(reinterpret_cast<char *>(mach_header
.GetBase()));
1624 std::string
overlap(altern
.str());
1625 overlap
.append(top
+ overlap
.size(), Align(overlap
.size(), 0x1000) - overlap
.size());
1627 put(output
, top
+ (position
- begin
), allocation
.size_
- (position
- begin
), progress
);
1628 position
= begin
+ allocation
.size_
;
1630 pad(output
, allocation
.limit_
- allocation
.size_
);
1631 position
+= allocation
.limit_
- allocation
.size_
;
1633 size_t saved(save(mach_header
, allocation
.baton_
, output
, allocation
.limit_
, left
, right
, overlap
, top
, progress
));
1634 if (allocation
.alloc_
> saved
)
1635 pad(output
, allocation
.alloc_
- saved
);
1637 _assert(allocation
.alloc_
== saved
);
1638 position
+= allocation
.alloc_
;
1644 typedef std::map
<uint32_t, std::string
> Blobs
;
1646 static void insert(Blobs
&blobs
, uint32_t slot
, const std::stringbuf
&buffer
) {
1647 auto value(buffer
.str());
1648 std::swap(blobs
[slot
], value
);
1651 static const std::string
&insert(Blobs
&blobs
, uint32_t slot
, uint32_t magic
, const std::stringbuf
&buffer
) {
1652 auto value(buffer
.str());
1654 blob
.magic
= Swap(magic
);
1655 blob
.length
= Swap(uint32_t(sizeof(blob
) + value
.size()));
1656 value
.insert(0, reinterpret_cast<char *>(&blob
), sizeof(blob
));
1657 auto &save(blobs
[slot
]);
1658 std::swap(save
, value
);
1662 static size_t put(std::streambuf
&output
, uint32_t magic
, const Blobs
&blobs
) {
1664 _foreach (blob
, blobs
)
1665 total
+= blob
.second
.size();
1667 struct SuperBlob super
;
1668 super
.blob
.magic
= Swap(magic
);
1669 super
.blob
.length
= Swap(uint32_t(sizeof(SuperBlob
) + blobs
.size() * sizeof(BlobIndex
) + total
));
1670 super
.count
= Swap(uint32_t(blobs
.size()));
1671 put(output
, &super
, sizeof(super
));
1673 size_t offset(sizeof(SuperBlob
) + sizeof(BlobIndex
) * blobs
.size());
1675 _foreach (blob
, blobs
) {
1677 index
.type
= Swap(blob
.first
);
1678 index
.offset
= Swap(uint32_t(offset
));
1679 put(output
, &index
, sizeof(index
));
1680 offset
+= blob
.second
.size();
1683 _foreach (blob
, blobs
)
1684 put(output
, blob
.second
.data(), blob
.second
.size());
1697 _assert(bio_
!= NULL
);
1701 bio_(BIO_new(BIO_s_mem()))
1705 Buffer(const char *data
, size_t size
) :
1706 Buffer(BIO_new_mem_buf(const_cast<char *>(data
), size
))
1710 Buffer(const std::string
&data
) :
1711 Buffer(data
.data(), data
.size())
1715 Buffer(PKCS7
*pkcs
) :
1718 if (i2d_PKCS7_bio(bio_
, pkcs
) == 0){
1719 fprintf(stderr
, "ldid: An error occured while getting the PKCS7 file: %s\n", ERR_error_string(ERR_get_error(), NULL
));
1728 operator BIO
*() const {
1732 explicit operator std::string() const {
1734 auto size(BIO_get_mem_data(bio_
, &data
));
1735 return std::string(data
, size
);
1744 STACK_OF(X509
) *ca_
;
1748 value_(d2i_PKCS12_bio(bio
, NULL
)),
1751 if (value_
== NULL
){
1752 fprintf(stderr
, "ldid: An error occured while getting the PKCS12 file: %s\n", ERR_error_string(ERR_get_error(), NULL
));
1756 if (!PKCS12_verify_mac(value_
, "", 0) && password
.empty()) {
1758 UI_UTIL_read_pw_string(passbuf
, 2048, "Enter password: ", 0);
1762 if (PKCS12_parse(value_
, password
.c_str(), &key_
, &cert_
, &ca_
) <= 0){
1763 fprintf(stderr
, "ldid: An error occured while parsing: %s\n", ERR_error_string(ERR_get_error(), NULL
));
1766 if (key_
== NULL
|| cert_
== NULL
){
1767 fprintf(stderr
, "ldid: An error occured while parsing: %s\nYour p12 cert might not be valid\n", ERR_error_string(ERR_get_error(), NULL
));
1772 ca_
= sk_X509_new_null();
1774 fprintf(stderr
, "ldid: An error occured while parsing: %s\n", ERR_error_string(ERR_get_error(), NULL
));
1779 Stuff(const std::string
&data
) :
1785 sk_X509_pop_free(ca_
, X509_free
);
1787 EVP_PKEY_free(key_
);
1788 PKCS12_free(value_
);
1791 operator PKCS12
*() const {
1795 operator EVP_PKEY
*() const {
1799 operator X509
*() const {
1803 operator STACK_OF(X509
) *() const {
1809 struct SEQUENCE_hash_sha1
{
1810 uint8_t SEQUENCE
[2] = {0x30, 0x1d}; // size
1811 uint8_t OBJECT_IDENTIFIER
[7] = {0x06, 0x05, 0x2B, 0x0E, 0x03, 0x02, 0x1A}; // OBJECT IDENTIFIER 1.3.14.3.2.26 sha1 (OIW)
1812 uint8_t hash_size
[2] = {0x04, 0x14};
1816 struct SEQUENCE_hash_sha256
{
1817 uint8_t SEQUENCE
[2] = {0x30, 0x2d}; // size
1818 uint8_t OBJECT_IDENTIFIER
[11] = {0x06 ,0x09 ,0x60, 0x86, 0x48, 0x01 ,0x65, 0x03, 0x04, 0x02, 0x01}; // 2.16.840.1.101.3.4.2.1 sha-256 (NIST Algorithm)
1819 uint8_t hash_size
[2] = {0x04, 0x20}; // hash size
1828 Signature(const Stuff
&stuff
, const Buffer
&data
, const std::string
&xml
, const std::vector
<char>& alternateCDSHA1
, const std::vector
<char>& alternateCDSHA256
) {
1829 value_
= PKCS7_new();
1830 if (value_
== NULL
){
1831 fprintf(stderr
, "ldid: An error occured while getting creating PKCS7 file: %s\n", ERR_error_string(ERR_get_error(), NULL
));
1835 if (PKCS7_set_type(value_
, NID_pkcs7_signed
) == 0 ||
1836 PKCS7_content_new(value_
, NID_pkcs7_data
) == 0) {
1837 fprintf(stderr
, "ldid: An error occured while getting creating PKCS7 file: %s\n", ERR_error_string(ERR_get_error(), NULL
));
1841 STACK_OF(X509
) *certs(stuff
);
1842 for (unsigned i(0), e(sk_X509_num(certs
)); i
!= e
; i
++) {
1843 if (PKCS7_add_certificate(value_
, sk_X509_value(certs
, e
- i
- 1)) == 0) {
1844 fprintf(stderr
, "ldid: An error occured while signing: %s\n", ERR_error_string(ERR_get_error(), NULL
));
1849 auto info(PKCS7_sign_add_signer(value_
, stuff
, stuff
, NULL
, PKCS7_NOSMIMECAP
));
1851 fprintf(stderr
, "ldid: An error occured while signing: %s\n", ERR_error_string(ERR_get_error(), NULL
));
1855 X509_ATTRIBUTE
*attribute
= X509_ATTRIBUTE_new();
1856 ASN1_OBJECT
*obj2
= OBJ_txt2obj("1.2.840.113635.100.9.2", 1);
1857 X509_ATTRIBUTE_set1_object(attribute
, obj2
);
1858 if (alternateCDSHA1
.size() != 0) {
1860 SEQUENCE_hash_sha1 seq1
;
1861 memcpy((void *)seq1
.hash
, (void *)alternateCDSHA1
.data(), alternateCDSHA1
.size());
1862 X509_ATTRIBUTE_set1_data(attribute
, V_ASN1_SEQUENCE
, &seq1
, sizeof(seq1
));
1864 if (alternateCDSHA256
.size() != 0) {
1866 SEQUENCE_hash_sha256 seq256
;
1867 memcpy((void *)seq256
.hash
, (void *)alternateCDSHA256
.data(), alternateCDSHA256
.size());
1868 X509_ATTRIBUTE_set1_data(attribute
, V_ASN1_SEQUENCE
, &seq256
, sizeof(seq256
));
1871 STACK_OF(X509_ATTRIBUTE
) *sk
= PKCS7_get_signed_attributes(info
);
1872 if (!sk_X509_ATTRIBUTE_push(sk
, attribute
)) {
1873 fprintf(stderr
, "ldid: sk_X509_ATTRIBUTE_push failed: %s\n", ERR_error_string(ERR_get_error(), NULL
));
1877 PKCS7_set_detached(value_
, 1);
1879 ASN1_OCTET_STRING
*string(ASN1_OCTET_STRING_new());
1880 if (string
== NULL
) {
1881 fprintf(stderr
, "ldid: %s\n", ERR_error_string(ERR_get_error(), NULL
));
1886 if (ASN1_STRING_set(string
, xml
.data(), xml
.size()) == 0) {
1887 fprintf(stderr
, "ldid: %s\n", ERR_error_string(ERR_get_error(), NULL
));
1891 static auto nid(OBJ_create("1.2.840.113635.100.9.1", "", ""));
1892 if (PKCS7_add_signed_attribute(info
, nid
, V_ASN1_OCTET_STRING
, string
) == 0) {
1893 fprintf(stderr
, "ldid: %s\n", ERR_error_string(ERR_get_error(), NULL
));
1897 ASN1_OCTET_STRING_free(string
);
1901 if (PKCS7_final(value_
, data
, PKCS7_BINARY
) == 0) {
1902 fprintf(stderr
, "ldid: Failed to sign: %s\n", ERR_error_string(ERR_get_error(), NULL
));
1911 operator PKCS7
*() const {
1917 public std::streambuf
1920 virtual std::streamsize
xsputn(const char_type
*data
, std::streamsize size
) {
1924 virtual int_type
overflow(int_type next
) {
1930 public std::streambuf
1936 EVP_MD_CTX
*sha256_
;
1939 HashBuffer(ldid::Hash
&hash
) :
1942 sha1_
= EVP_MD_CTX_new();
1943 sha256_
= EVP_MD_CTX_new();
1945 EVP_DigestInit_ex(sha1_
, EVP_get_digestbyname("sha1"), nullptr);
1946 EVP_DigestInit_ex(sha256_
, EVP_get_digestbyname("sha256"), nullptr);
1950 EVP_DigestFinal_ex(sha1_
, reinterpret_cast<uint8_t *>(hash_
.sha1_
), nullptr);
1951 EVP_DigestFinal_ex(sha256_
, reinterpret_cast<uint8_t *>(hash_
.sha256_
), nullptr);
1953 EVP_MD_CTX_free(sha1_
);
1954 EVP_MD_CTX_free(sha256_
);
1957 virtual std::streamsize
xsputn(const char_type
*data
, std::streamsize size
) {
1958 EVP_DigestUpdate(sha1_
, data
, size
);
1959 EVP_DigestUpdate(sha256_
, data
, size
);
1963 virtual int_type
overflow(int_type next
) {
1964 if (next
== traits_type::eof())
1976 std::streambuf
&buffer_
;
1979 HashProxy(ldid::Hash
&hash
, std::streambuf
&buffer
) :
1985 virtual std::streamsize
xsputn(const char_type
*data
, std::streamsize size
) {
1986 _assert(HashBuffer::xsputn(data
, size
) == size
);
1987 return buffer_
.sputn(data
, size
);
1991 #ifndef LDID_NOTOOLS
1992 static bool Starts(const std::string
&lhs
, const std::string
&rhs
) {
1993 return lhs
.size() >= rhs
.size() && lhs
.compare(0, rhs
.size(), rhs
) == 0;
2001 Split(const std::string
&path
) {
2002 size_t slash(path
.rfind('/'));
2003 if (slash
== std::string::npos
)
2006 dir
= path
.substr(0, slash
+ 1);
2007 base
= path
.substr(slash
+ 1);
2012 static void mkdir_p(const std::string
&path
) {
2016 if (_syscall(mkdir(path
.c_str()), EEXIST
) == -EEXIST
)
2019 if (_syscall(mkdir(path
.c_str(), 0755), EEXIST
) == -EEXIST
)
2022 auto slash(path
.rfind('/', path
.size() - 1));
2023 if (slash
== std::string::npos
)
2025 mkdir_p(path
.substr(0, slash
));
2028 static std::string
Temporary(std::filebuf
&file
, const Split
&split
) {
2029 std::string
temp(split
.dir
+ ".ldid." + split
.base
);
2031 _assert_(file
.open(temp
.c_str(), std::ios::out
| std::ios::trunc
| std::ios::binary
) == &file
, "open(): %s", temp
.c_str());
2032 cleanup
.push_back(temp
);
2036 static void Commit(const std::string
&path
, const std::string
&temp
) {
2038 if (_syscall(stat(path
.c_str(), &info
), ENOENT
) == 0) {
2040 _syscall(chown(temp
.c_str(), info
.st_uid
, info
.st_gid
));
2042 _syscall(chmod(temp
.c_str(), info
.st_mode
));
2045 _syscall(rename(temp
.c_str(), path
.c_str()));
2046 cleanup
.erase(std::remove(cleanup
.begin(), cleanup
.end(), temp
), cleanup
.end());
2048 #endif // LDID_NOTOOLS
2052 static void get(std::string
&value
, X509_NAME
*name
, int nid
) {
2053 auto index(X509_NAME_get_index_by_NID(name
, nid
, -1));
2055 fprintf(stderr
, "ldid: An error occursed while parsing the certificate: %s\n", ERR_error_string(ERR_get_error(), NULL
));
2058 auto next(X509_NAME_get_index_by_NID(name
, nid
, index
));
2060 fprintf(stderr
, "ldid: An error occursed while parsing the certificate: %s\n", ERR_error_string(ERR_get_error(), NULL
));
2063 auto entry(X509_NAME_get_entry(name
, index
));
2064 if (entry
== NULL
) {
2065 fprintf(stderr
, "ldid: An error occursed while parsing the certificate: %s\n", ERR_error_string(ERR_get_error(), NULL
));
2068 auto asn(X509_NAME_ENTRY_get_data(entry
));
2070 fprintf(stderr
, "ldid: An error occursed while parsing the certificate: %s\n", ERR_error_string(ERR_get_error(), NULL
));
2073 value
.assign(reinterpret_cast<const char *>(ASN1_STRING_get0_data(asn
)), ASN1_STRING_length(asn
));
2076 static void req(std::streambuf
&buffer
, uint32_t value
) {
2077 value
= Swap(value
);
2078 put(buffer
, &value
, sizeof(value
));
2081 static void req(std::streambuf
&buffer
, const std::string
&value
) {
2082 req(buffer
, value
.size());
2083 put(buffer
, value
.data(), value
.size());
2084 static uint8_t zeros
[] = {0,0,0,0};
2085 put(buffer
, zeros
, 3 - (value
.size() + 3) % 4);
2088 template <size_t Size_
>
2089 static void req(std::streambuf
&buffer
, uint8_t (&&data
)[Size_
]) {
2091 put(buffer
, data
, Size_
);
2092 static uint8_t zeros
[] = {0,0,0,0};
2093 put(buffer
, zeros
, 3 - (Size_
+ 3) % 4);
2096 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
, uint8_t platform
, const Progress
&progress
) {
2105 auto name(X509_get_subject_name(stuff
));
2107 fprintf(stderr
, "ldid: Your certificate might not be valid: %s\n", ERR_error_string(ERR_get_error(), NULL
));
2110 get(team
, name
, NID_organizationalUnitName
);
2111 get(common
, name
, NID_commonName
);
2115 std::stringbuf backing
;
2117 if (!requirements
.empty()) {
2118 put(backing
, requirements
.data(), requirements
.size());
2122 std::stringbuf requirement
;
2123 req(requirement
, exprForm
);
2124 req(requirement
, opAnd
);
2125 req(requirement
, opIdent
);
2126 req(requirement
, identifier
);
2127 req(requirement
, opAnd
);
2128 req(requirement
, opAppleGenericAnchor
);
2129 req(requirement
, opAnd
);
2130 req(requirement
, opCertField
);
2131 req(requirement
, 0);
2132 req(requirement
, "subject.CN");
2133 req(requirement
, matchEqual
);
2134 req(requirement
, common
);
2135 req(requirement
, opCertGeneric
);
2136 req(requirement
, 1);
2137 req(requirement
, (uint8_t []) {APPLE_EXTENSION_OID
, 2, 1});
2138 req(requirement
, matchExists
);
2139 insert(blobs
, 3, CSMAGIC_REQUIREMENT
, requirement
);
2141 put(backing
, CSMAGIC_REQUIREMENTS
, blobs
);
2145 // XXX: this is just a "sufficiently large number"
2146 size_t certificate(0x3000);
2148 Allocate(idata
, isize
, output
, fun([&](const MachHeader
&mach_header
, Baton
&baton
, size_t size
) -> size_t {
2149 size_t alloc(sizeof(struct SuperBlob
));
2151 uint32_t normal((size
+ PageSize_
- 1) / PageSize_
);
2153 uint32_t special(0);
2155 _foreach (slot
, slots
)
2156 special
= std::max(special
, slot
.first
);
2158 mach_header
.ForSection(fun([&](const char *segment
, const char *section
, void *data
, size_t size
) {
2159 if (strcmp(segment
, "__TEXT") == 0 && section
!= NULL
&& strcmp(section
, "__info_plist") == 0)
2160 special
= std::max(special
, CSSLOT_INFOSLOT
);
2163 special
= std::max(special
, CSSLOT_REQUIREMENTS
);
2164 alloc
+= sizeof(struct BlobIndex
);
2165 alloc
+= backing
.str().size();
2168 Analyze(mach_header
, fun([&](const char *data
, size_t size
) {
2169 baton
.entitlements_
.assign(data
, size
);
2172 if (!baton
.entitlements_
.empty() || !entitlements
.empty()) {
2173 auto combined(plist(baton
.entitlements_
));
2174 _scope({ plist_free(combined
); });
2175 if (plist_get_node_type(combined
) != PLIST_DICT
) {
2176 fprintf(stderr
, "ldid: Existing entitlements are in wrong format\n");
2180 auto merging(plist(entitlements
));
2181 _scope({ plist_free(merging
); });
2182 if (plist_get_node_type(merging
) != PLIST_DICT
) {
2183 fprintf(stderr
, "ldid: Entitlements need a root key of dict\n");
2187 plist_dict_iter
iterator(NULL
);
2188 plist_dict_new_iter(merging
, &iterator
);
2189 _scope({ free(iterator
); });
2193 plist_t
value(NULL
);
2194 plist_dict_next_item(merging
, iterator
, &key
, &value
);
2197 _scope({ free(key
); });
2198 plist_dict_set_item(combined
, key
, plist_copy(value
));
2201 baton
.derformat_
= der(combined
);
2205 plist_to_xml(combined
, &xml
, &size
);
2206 _scope({ free(xml
); });
2208 baton
.entitlements_
.assign(xml
, size
);
2211 if (!baton
.entitlements_
.empty()) {
2212 special
= std::max(special
, CSSLOT_ENTITLEMENTS
);
2213 alloc
+= sizeof(struct BlobIndex
);
2214 alloc
+= sizeof(struct Blob
);
2215 alloc
+= baton
.entitlements_
.size();
2218 if (!baton
.derformat_
.empty()) {
2219 special
= std::max(special
, CSSLOT_DERFORMAT
);
2220 alloc
+= sizeof(struct BlobIndex
);
2221 alloc
+= sizeof(struct Blob
);
2222 alloc
+= baton
.derformat_
.size();
2225 size_t directory(0);
2227 directory
+= sizeof(struct BlobIndex
);
2228 directory
+= sizeof(struct Blob
);
2229 directory
+= sizeof(struct CodeDirectory
);
2230 directory
+= identifier
.size() + 1;
2233 directory
+= team
.size() + 1;
2235 for (Algorithm
*algorithm
: GetAlgorithms())
2236 alloc
= Align(alloc
+ directory
+ (special
+ normal
) * algorithm
->size_
, 16);
2239 alloc
+= sizeof(struct BlobIndex
);
2240 alloc
+= sizeof(struct Blob
);
2241 alloc
+= certificate
;
2245 }), fun([&](const MachHeader
&mach_header
, const Baton
&baton
, std::streambuf
&output
, size_t limit
, size_t left
, size_t right
, const std::string
&overlap
, const char *top
, const Progress
&progress
) -> size_t {
2249 insert(blobs
, CSSLOT_REQUIREMENTS
, backing
);
2253 if (mach_header
.Swap(mach_header
->filetype
) == MH_EXECUTE
)
2254 execs
|= kSecCodeExecSegMainBinary
;
2256 if (!baton
.entitlements_
.empty()) {
2257 std::stringbuf data
;
2258 put(data
, baton
.entitlements_
.data(), baton
.entitlements_
.size());
2259 insert(blobs
, CSSLOT_ENTITLEMENTS
, CSMAGIC_EMBEDDED_ENTITLEMENTS
, data
);
2261 auto entitlements(plist(baton
.entitlements_
));
2262 _scope({ plist_free(entitlements
); });
2263 if (plist_get_node_type(entitlements
) != PLIST_DICT
) {
2264 fprintf(stderr
, "ldid: Entitlements should be a plist dicionary\n");
2268 const auto entitled([&](const char *key
) {
2269 auto item(plist_dict_get_item(entitlements
, key
));
2270 if (plist_get_node_type(item
) != PLIST_BOOLEAN
)
2273 plist_get_bool_val(item
, &value
);
2277 if (entitled("get-task-allow"))
2278 execs
|= kSecCodeExecSegAllowUnsigned
;
2279 if (entitled("run-unsigned-code"))
2280 execs
|= kSecCodeExecSegAllowUnsigned
;
2281 if (entitled("com.apple.private.cs.debugger"))
2282 execs
|= kSecCodeExecSegDebugger
;
2283 if (entitled("dynamic-codesigning"))
2284 execs
|= kSecCodeExecSegJit
;
2285 if (entitled("com.apple.private.skip-library-validation"))
2286 execs
|= kSecCodeExecSegSkipLibraryVal
;
2287 if (entitled("com.apple.private.amfi.can-load-cdhash"))
2288 execs
|= kSecCodeExecSegCanLoadCdHash
;
2289 if (entitled("com.apple.private.amfi.can-execute-cdhash"))
2290 execs
|= kSecCodeExecSegCanExecCdHash
;
2293 if (!baton
.derformat_
.empty()) {
2294 std::stringbuf data
;
2295 put(data
, baton
.derformat_
.data(), baton
.derformat_
.size());
2296 insert(blobs
, CSSLOT_DERFORMAT
, CSMAGIC_EMBEDDED_DERFORMAT
, data
);
2301 mach_header
.ForSection(fun([&](const char *segment
, const char *section
, void *data
, size_t size
) {
2302 if (strcmp(segment
, "__TEXT") == 0 && section
!= NULL
&& strcmp(section
, "__info_plist") == 0) {
2303 auto &slot(posts
[CSSLOT_INFOSLOT
]);
2304 for (Algorithm
*algorithm
: GetAlgorithms())
2305 (*algorithm
)(slot
, data
, size
);
2310 for (Algorithm
*pointer
: GetAlgorithms()) {
2311 Algorithm
&algorithm(*pointer
);
2313 std::stringbuf data
;
2315 uint32_t special(0);
2316 _foreach (blob
, blobs
)
2317 special
= std::max(special
, blob
.first
);
2318 _foreach (slot
, posts
)
2319 special
= std::max(special
, slot
.first
);
2320 uint32_t normal((limit
+ PageSize_
- 1) / PageSize_
);
2322 CodeDirectory directory
;
2323 directory
.version
= Swap(uint32_t(0x00020400));
2324 directory
.flags
= Swap(uint32_t(flags
));
2325 directory
.nSpecialSlots
= Swap(special
);
2326 directory
.codeLimit
= Swap(uint32_t(limit
> UINT32_MAX
? UINT32_MAX
: limit
));
2327 directory
.nCodeSlots
= Swap(normal
);
2328 directory
.hashSize
= algorithm
.size_
;
2329 directory
.hashType
= algorithm
.type_
;
2330 directory
.platform
= platform
;
2331 directory
.pageSize
= PageShift_
;
2332 directory
.spare2
= Swap(uint32_t(0));
2333 directory
.scatterOffset
= Swap(uint32_t(0));
2334 directory
.spare3
= Swap(uint32_t(0));
2335 directory
.codeLimit64
= Swap(uint64_t(limit
> UINT32_MAX
? limit
: 0));
2336 directory
.execSegBase
= Swap(uint64_t(left
));
2337 directory
.execSegLimit
= Swap(uint64_t(right
- left
));
2338 directory
.execSegFlags
= Swap(execs
);
2340 uint32_t offset(sizeof(Blob
) + sizeof(CodeDirectory
));
2342 directory
.identOffset
= Swap(uint32_t(offset
));
2343 offset
+= identifier
.size() + 1;
2346 directory
.teamIDOffset
= Swap(uint32_t(0));
2348 directory
.teamIDOffset
= Swap(uint32_t(offset
));
2349 offset
+= team
.size() + 1;
2352 offset
+= special
* algorithm
.size_
;
2353 directory
.hashOffset
= Swap(uint32_t(offset
));
2354 offset
+= normal
* algorithm
.size_
;
2356 put(data
, &directory
, sizeof(directory
));
2358 put(data
, identifier
.c_str(), identifier
.size() + 1);
2360 put(data
, team
.c_str(), team
.size() + 1);
2362 std::vector
<uint8_t> storage((special
+ normal
) * algorithm
.size_
);
2363 auto *hashes(&storage
[special
* algorithm
.size_
]);
2365 memset(storage
.data(), 0, special
* algorithm
.size_
);
2367 _foreach (blob
, blobs
) {
2368 auto local(reinterpret_cast<const Blob
*>(&blob
.second
[0]));
2369 algorithm(hashes
- blob
.first
* algorithm
.size_
, local
, Swap(local
->length
));
2372 _foreach (slot
, posts
)
2373 memcpy(hashes
- slot
.first
* algorithm
.size_
, algorithm
[slot
.second
], algorithm
.size_
);
2377 for (size_t i
= 0; i
!= normal
- 1; ++i
) {
2378 algorithm(hashes
+ i
* algorithm
.size_
, (PageSize_
* i
< overlap
.size() ? overlap
.data() : top
) + PageSize_
* i
, PageSize_
);
2379 progress(double(i
) / normal
);
2382 algorithm(hashes
+ (normal
- 1) * algorithm
.size_
, top
+ PageSize_
* (normal
- 1), ((limit
- 1) % PageSize_
) + 1);
2385 put(data
, storage
.data(), storage
.size());
2387 const auto &save(insert(blobs
, total
== 0 ? CSSLOT_CODEDIRECTORY
: CSSLOT_ALTERNATE
+ total
- 1, CSMAGIC_CODEDIRECTORY
, data
));
2388 algorithm(hash
, save
.data(), save
.size());
2394 auto plist(plist_new_dict());
2395 _scope({ plist_free(plist
); });
2397 auto cdhashes(plist_new_array());
2398 plist_dict_set_item(plist
, "cdhashes", cdhashes
);
2400 std::vector
<char> alternateCDSHA256
;
2401 std::vector
<char> alternateCDSHA1
;
2404 for (Algorithm
*pointer
: GetAlgorithms()) {
2405 Algorithm
&algorithm(*pointer
);
2408 const auto &blob(blobs
[total
== 0 ? CSSLOT_CODEDIRECTORY
: CSSLOT_ALTERNATE
+ total
- 1]);
2411 std::vector
<char> hash
;
2412 algorithm(hash
, blob
.data(), blob
.size());
2413 if (algorithm
.type_
== CS_HASHTYPE_SHA256_256
)
2414 alternateCDSHA256
= hash
;
2415 else if (algorithm
.type_
== CS_HASHTYPE_SHA160_160
)
2416 alternateCDSHA1
= hash
;
2419 plist_array_append_item(cdhashes
, plist_new_data(hash
.data(), hash
.size()));
2424 plist_to_xml(plist
, &xml
, &size
);
2425 _scope({ free(xml
); });
2427 std::stringbuf data
;
2428 const std::string
&sign(blobs
[CSSLOT_CODEDIRECTORY
]);
2433 Signature
signature(stuff
, sign
, std::string(xml
, size
), alternateCDSHA1
, alternateCDSHA256
);
2434 Buffer
result(signature
);
2435 std::string
value(result
);
2436 put(data
, value
.data(), value
.size());
2438 const auto &save(insert(blobs
, CSSLOT_SIGNATURESLOT
, CSMAGIC_BLOBWRAPPER
, data
));
2439 _assert(save
.size() <= certificate
);
2442 return put(output
, CSMAGIC_EMBEDDED_SIGNATURE
, blobs
);
2448 #ifndef LDID_NOTOOLS
2449 static void Unsign(void *idata
, size_t isize
, std::streambuf
&output
, const Progress
&progress
) {
2450 Allocate(idata
, isize
, output
, fun([](const MachHeader
&mach_header
, Baton
&baton
, size_t size
) -> size_t {
2452 }), fun([](const MachHeader
&mach_header
, const Baton
&baton
, std::streambuf
&output
, size_t limit
, size_t left
, size_t right
, const std::string
&overlap
, const char *top
, const Progress
&progress
) -> size_t {
2457 std::string
DiskFolder::Path(const std::string
&path
) const {
2458 return path_
+ path
;
2461 DiskFolder::DiskFolder(const std::string
&path
) :
2464 _assert_(path_
.size() != 0 && path_
[path_
.size() - 1] == '/', "missing / on %s", path_
.c_str());
2467 DiskFolder::~DiskFolder() {
2468 if (!std::uncaught_exception())
2469 for (const auto &commit
: commit_
)
2470 Commit(commit
.first
, commit
.second
);
2474 std::string
readlink(const std::string
&path
) {
2475 for (size_t size(1024); ; size
*= 2) {
2479 int writ(_syscall(::readlink(path
.c_str(), &data
[0], data
.size())));
2480 if (size_t(writ
) >= size
)
2489 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 {
2490 std::string
path(Path(root
) + base
);
2492 DIR *dir(opendir(path
.c_str()));
2493 _assert(dir
!= NULL
);
2494 _scope({ _syscall(closedir(dir
)); });
2496 while (auto child
= readdir(dir
)) {
2497 std::string
name(child
->d_name
);
2498 if (name
== "." || name
== "..")
2500 if (Starts(name
, ".ldid."))
2507 _syscall(stat((path
+ name
).c_str(), &info
));
2508 if (S_ISDIR(info
.st_mode
))
2510 else if (S_ISREG(info
.st_mode
))
2513 _assert_(false, "st_mode=%x", info
.st_mode
);
2515 switch (child
->d_type
) {
2523 link(base
+ name
, fun([&]() { return readlink(path
+ name
); }));
2526 _assert_(false, "d_type=%u", child
->d_type
);
2531 Find(root
, base
+ name
+ "/", code
, link
);
2537 void DiskFolder::Save(const std::string
&path
, bool edit
, const void *flag
, const Functor
<void (std::streambuf
&)> &code
) {
2543 auto from(Path(path
));
2544 commit_
[from
] = Temporary(save
, from
);
2549 bool DiskFolder::Look(const std::string
&path
) const {
2550 return _syscall(access(Path(path
).c_str(), R_OK
), ENOENT
) == 0;
2553 void DiskFolder::Open(const std::string
&path
, const Functor
<void (std::streambuf
&, size_t, const void *)> &code
) const {
2555 auto result(data
.open(Path(path
).c_str(), std::ios::binary
| std::ios::in
));
2556 _assert_(result
== &data
, "DiskFolder::Open(%s)", Path(path
).c_str());
2558 auto length(data
.pubseekoff(0, std::ios::end
, std::ios::in
));
2559 data
.pubseekpos(0, std::ios::in
);
2560 code(data
, length
, NULL
);
2563 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 {
2564 Find(path
, "", code
, link
);
2566 #endif // LDID_NOTOOLS
2568 SubFolder::SubFolder(Folder
&parent
, const std::string
&path
) :
2572 _assert_(path_
.size() == 0 || path_
[path_
.size() - 1] == '/', "missing / on %s", path_
.c_str());
2575 std::string
SubFolder::Path(const std::string
&path
) const {
2576 return path_
+ path
;
2579 void SubFolder::Save(const std::string
&path
, bool edit
, const void *flag
, const Functor
<void (std::streambuf
&)> &code
) {
2580 return parent_
.Save(Path(path
), edit
, flag
, code
);
2583 bool SubFolder::Look(const std::string
&path
) const {
2584 return parent_
.Look(Path(path
));
2587 void SubFolder::Open(const std::string
&path
, const Functor
<void (std::streambuf
&, size_t, const void *)> &code
) const {
2588 return parent_
.Open(Path(path
), code
);
2591 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 {
2592 return parent_
.Find(Path(path
), code
, link
);
2595 std::string
UnionFolder::Map(const std::string
&path
) const {
2596 auto remap(remaps_
.find(path
));
2597 if (remap
== remaps_
.end())
2599 return remap
->second
;
2602 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 {
2603 if (file
.size() >= path
.size() && file
.substr(0, path
.size()) == path
)
2604 code(file
.substr(path
.size()));
2607 UnionFolder::UnionFolder(Folder
&parent
) :
2612 void UnionFolder::Save(const std::string
&path
, bool edit
, const void *flag
, const Functor
<void (std::streambuf
&)> &code
) {
2613 return parent_
.Save(Map(path
), edit
, flag
, code
);
2616 bool UnionFolder::Look(const std::string
&path
) const {
2617 auto file(resets_
.find(path
));
2618 if (file
!= resets_
.end())
2620 return parent_
.Look(Map(path
));
2623 void UnionFolder::Open(const std::string
&path
, const Functor
<void (std::streambuf
&, size_t, const void *)> &code
) const {
2624 auto file(resets_
.find(path
));
2625 if (file
== resets_
.end())
2626 return parent_
.Open(Map(path
), code
);
2627 auto &entry(file
->second
);
2629 auto &data(*entry
.data_
);
2630 auto length(data
.pubseekoff(0, std::ios::end
, std::ios::in
));
2631 data
.pubseekpos(0, std::ios::in
);
2632 code(data
, length
, entry
.flag_
);
2635 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 {
2636 for (auto &reset
: resets_
)
2637 Map(path
, code
, reset
.first
, fun([&](const Functor
<void (std::streambuf
&, size_t, const void *)> &code
) {
2638 auto &entry(reset
.second
);
2639 auto &data(*entry
.data_
);
2640 auto length(data
.pubseekoff(0, std::ios::end
, std::ios::in
));
2641 data
.pubseekpos(0, std::ios::in
);
2642 code(data
, length
, entry
.flag_
);
2645 for (auto &remap
: remaps_
)
2646 Map(path
, code
, remap
.first
, fun([&](const Functor
<void (std::streambuf
&, size_t, const void *)> &code
) {
2647 parent_
.Open(remap
.second
, fun([&](std::streambuf
&data
, size_t length
, const void *flag
) {
2648 code(data
, length
, flag
);
2652 parent_
.Find(path
, fun([&](const std::string
&name
) {
2653 if (deletes_
.find(path
+ name
) == deletes_
.end())
2655 }), fun([&](const std::string
&name
, const Functor
<std::string ()> &read
) {
2656 if (deletes_
.find(path
+ name
) == deletes_
.end())
2661 #ifndef LDID_NOTOOLS
2662 static void copy(std::streambuf
&source
, std::streambuf
&target
, size_t length
, const Progress
&progress
) {
2666 char data
[4096 * 4];
2667 size_t writ(source
.sgetn(data
, sizeof(data
)));
2670 _assert(target
.sputn(data
, writ
) == writ
);
2672 progress(double(total
) / length
);
2676 static plist_t
plist(const std::string
&data
) {
2678 return plist_new_dict();
2679 plist_t
plist(NULL
);
2680 if (Starts(data
, "bplist00"))
2681 plist_from_bin(data
.data(), data
.size(), &plist
);
2683 plist_from_xml(data
.data(), data
.size(), &plist
);
2684 if (plist
== NULL
) {
2685 fprintf(stderr
, "ldid: Failed to parse plist\n");
2691 static void plist_d(std::streambuf
&buffer
, size_t length
, const Functor
<void (plist_t
)> &code
) {
2692 std::stringbuf data
;
2693 copy(buffer
, data
, length
, dummy_
);
2694 auto node(plist(data
.str()));
2695 _scope({ plist_free(node
); });
2696 if (plist_get_node_type(node
) != PLIST_DICT
) {
2697 fprintf(stderr
, "ldid: Unexpected plist type. Expected <dict>\n");
2703 static std::string
plist_s(plist_t node
) {
2706 if (plist_get_node_type(node
) != PLIST_STRING
) {
2707 fprintf(stderr
, "ldid: Unexpected plist type. Expected <string>\n");
2711 plist_get_string_val(node
, &data
);
2712 _scope({ free(data
); });
2727 std::vector
<std::string
> matches_
;
2730 Expression(const std::string
&code
) {
2731 _assert_(regcomp(®ex_
, code
.c_str(), REG_EXTENDED
) == 0, "regcomp()");
2732 matches_
.resize(regex_
.re_nsub
+ 1);
2739 bool operator ()(const std::string
&data
) {
2740 regmatch_t matches
[matches_
.size()];
2741 auto value(regexec(®ex_
, data
.c_str(), matches_
.size(), matches
, 0));
2742 if (value
== REG_NOMATCH
)
2744 _assert_(value
== 0, "regexec()");
2745 for (size_t i(0); i
!= matches_
.size(); ++i
)
2746 matches_
[i
].assign(data
.data() + matches
[i
].rm_so
, matches
[i
].rm_eo
- matches
[i
].rm_so
);
2750 const std::string
&operator [](size_t index
) const {
2751 return matches_
[index
];
2760 mutable std::unique_ptr
<Expression
> regex_
;
2762 Rule(unsigned weight
, Mode mode
, const std::string
&code
) :
2769 Rule(const Rule
&rhs
) :
2770 weight_(rhs
.weight_
),
2776 void Compile() const {
2777 regex_
.reset(new Expression(code_
));
2780 bool operator ()(const std::string
&data
) const {
2781 _assert(regex_
.get() != NULL
);
2782 return (*regex_
)(data
);
2785 bool operator <(const Rule
&rhs
) const {
2786 if (weight_
> rhs
.weight_
)
2788 if (weight_
< rhs
.weight_
)
2790 return mode_
> rhs
.mode_
;
2795 bool operator ()(const Rule
*lhs
, const Rule
*rhs
) const {
2796 return lhs
->code_
< rhs
->code_
;
2800 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
, uint8_t platform
, const Progress
&progress
) {
2801 // XXX: this is a miserable fail
2802 std::stringbuf temp
;
2803 put(temp
, prefix
, size
);
2804 copy(buffer
, temp
, length
- size
, progress
);
2805 // XXX: this is a stupid hack
2806 pad(temp
, 0x10 - (length
& 0xf));
2807 auto data(temp
.str());
2809 HashProxy
proxy(hash
, save
);
2810 return Sign(data
.data(), data
.size(), proxy
, identifier
, entitlements
, merge
, requirements
, key
, slots
, flags
, platform
, progress
);
2814 std::map
<std::string
, Hash
> files
;
2815 std::map
<std::string
, std::string
> links
;
2817 void Merge(const std::string
&root
, const State
&state
) {
2818 for (const auto &entry
: state
.files
)
2819 files
[root
+ entry
.first
] = entry
.second
;
2820 for (const auto &entry
: state
.links
)
2821 links
[root
+ entry
.first
] = entry
.second
;
2825 Bundle
Sign(const std::string
&root
, Folder
&parent
, const std::string
&key
, State
&local
, const std::string
&requirements
, const Functor
<std::string (const std::string
&, const std::string
&)> &alter
, bool merge
, uint8_t platform
, const Progress
&progress
) {
2826 std::string executable
;
2827 std::string identifier
;
2831 std::string
info("Info.plist");
2833 SubFolder
folder(parent
, [&]() {
2834 if (parent
.Look(info
))
2837 if (parent
.Look("Contents/" + info
))
2839 else if (parent
.Look("Resources/" + info
)) {
2840 info
= "Resources/" + info
;
2843 fprintf(stderr
, "ldid: Could not find Info.plist\n");
2848 folder
.Open(info
, fun([&](std::streambuf
&buffer
, size_t length
, const void *flag
) {
2849 plist_d(buffer
, length
, fun([&](plist_t node
) {
2850 plist_t
nodebuf(plist_dict_get_item(node
, "CFBundleExecutable"));
2851 if (nodebuf
== NULL
) {
2852 fprintf(stderr
, "ldid: Cannot find key CFBundleExecutable\n");
2855 executable
= plist_s(nodebuf
);
2856 nodebuf
= plist_dict_get_item(node
, "CFBundleIdentifier");
2857 if (nodebuf
== NULL
) {
2858 fprintf(stderr
, "ldid: Cannot find key CFBundleIdentifier\n");
2861 identifier
= plist_s(nodebuf
);
2865 if (mac
&& info
== "Info.plist")
2866 executable
= "MacOS/" + executable
;
2868 progress(root
+ "*");
2870 std::string entitlements
;
2871 folder
.Open(executable
, fun([&](std::streambuf
&buffer
, size_t length
, const void *flag
) {
2872 // XXX: this is a miserable fail
2873 std::stringbuf temp
;
2874 copy(buffer
, temp
, length
, progress
);
2875 // XXX: this is a stupid hack
2876 pad(temp
, 0x10 - (length
& 0xf));
2877 auto data(temp
.str());
2878 entitlements
= alter(root
, Analyze(data
.data(), data
.size()));
2881 static const std::string
directory("_CodeSignature/");
2882 static const std::string
signature(directory
+ "CodeResources");
2884 std::map
<std::string
, std::multiset
<Rule
>> versions
;
2886 auto &rules1(versions
[""]);
2887 auto &rules2(versions
["2"]);
2889 const std::string
resources(mac
? "Resources/" : "");
2892 rules1
.insert(Rule
{1, NoMode
, "^" + (resources
== "" ? ".*" : resources
)});
2893 rules1
.insert(Rule
{1000, OptionalMode
, "^" + resources
+ ".*\\.lproj/"});
2894 rules1
.insert(Rule
{1100, OmitMode
, "^" + resources
+ ".*\\.lproj/locversion.plist$"});
2895 rules1
.insert(Rule
{1010, NoMode
, "^" + resources
+ "Base\\.lproj/"});
2896 rules1
.insert(Rule
{1, NoMode
, "^version.plist$"});
2900 rules2
.insert(Rule
{11, NoMode
, ".*\\.dSYM($|/)"});
2901 if (mac
) rules2
.insert(Rule
{20, NoMode
, "^" + resources
});
2902 rules2
.insert(Rule
{2000, OmitMode
, "^(.*/)?\\.DS_Store$"});
2903 if (mac
) rules2
.insert(Rule
{10, NestedMode
, "^(Frameworks|SharedFrameworks|PlugIns|Plug-ins|XPCServices|Helpers|MacOS|Library/(Automator|Spotlight|LoginItems))/"});
2904 rules2
.insert(Rule
{1, NoMode
, "^.*"});
2905 rules2
.insert(Rule
{1000, OptionalMode
, "^" + resources
+ ".*\\.lproj/"});
2906 rules2
.insert(Rule
{1100, OmitMode
, "^" + resources
+ ".*\\.lproj/locversion.plist$"});
2907 if (!mac
) rules2
.insert(Rule
{1010, NoMode
, "^Base\\.lproj/"});
2908 rules2
.insert(Rule
{20, OmitMode
, "^Info\\.plist$"});
2909 rules2
.insert(Rule
{20, OmitMode
, "^PkgInfo$"});
2910 if (mac
) rules2
.insert(Rule
{10, NestedMode
, "^[^/]+$"});
2911 rules2
.insert(Rule
{20, NoMode
, "^embedded\\.provisionprofile$"});
2912 if (mac
) rules2
.insert(Rule
{1010, NoMode
, "^" + resources
+ "Base\\.lproj/"});
2913 rules2
.insert(Rule
{20, NoMode
, "^version\\.plist$"});
2916 std::string
failure(mac
? "Contents/|Versions/[^/]*/Resources/" : "");
2917 Expression
nested("^(Frameworks/[^/]*\\.framework|PlugIns/[^/]*\\.appex(()|/[^/]*.app))/(" + failure
+ ")Info\\.plist$");
2918 std::map
<std::string
, Bundle
> bundles
;
2920 folder
.Find("", fun([&](const std::string
&name
) {
2923 auto bundle(Split(name
).dir
);
2925 _assert(!bundle
.empty());
2926 bundle
= Split(bundle
.substr(0, bundle
.size() - 1)).dir
;
2928 SubFolder
subfolder(folder
, bundle
);
2931 bundles
[nested
[1]] = Sign(root
+ bundle
, subfolder
, key
, remote
, "", Starts(name
, "PlugIns/") ? alter
:
2932 static_cast<const Functor
<std::string (const std::string
&, const std::string
&)> &>(fun([&](const std::string
&, const std::string
&) -> std::string
{ return entitlements
; }))
2933 , merge
, platform
, progress
);
2934 local
.Merge(bundle
, remote
);
2935 }), fun([&](const std::string
&name
, const Functor
<std::string ()> &read
) {
2938 std::set
<std::string
> excludes
;
2940 auto exclude([&](const std::string
&name
) {
2941 // BundleDiskRep::adjustResources -> builder.addExclusion
2942 if (name
== executable
|| Starts(name
, directory
) || Starts(name
, "_MASReceipt/") || name
== "CodeResources")
2945 for (const auto &bundle
: bundles
)
2946 if (Starts(name
, bundle
.first
+ "/")) {
2947 excludes
.insert(name
);
2954 folder
.Find("", fun([&](const std::string
&name
) {
2958 if (local
.files
.find(name
) != local
.files
.end())
2960 auto &hash(local
.files
[name
]);
2962 folder
.Open(name
, fun([&](std::streambuf
&data
, size_t length
, const void *flag
) {
2963 progress(root
+ name
);
2974 auto size(most(data
, &header
.bytes
, sizeof(header
.bytes
)));
2976 if (name
!= "_WatchKitStub/WK" && size
== sizeof(header
.bytes
))
2977 switch (Swap(header
.magic
)) {
2979 // Java class file format
2980 if (Swap(header
.count
) >= 40)
2983 case MH_MAGIC
: case MH_MAGIC_64
:
2984 case MH_CIGAM
: case MH_CIGAM_64
:
2985 folder
.Save(name
, true, flag
, fun([&](std::streambuf
&save
) {
2987 Sign(header
.bytes
, size
, data
, hash
, save
, identifier
, "", false, "", key
, slots
, length
, 0, platform
, Progression(progress
, root
+ name
));
2992 folder
.Save(name
, false, flag
, fun([&](std::streambuf
&save
) {
2993 HashProxy
proxy(hash
, save
);
2994 put(proxy
, header
.bytes
, size
);
2995 copy(data
, proxy
, length
- size
, progress
);
2998 }), fun([&](const std::string
&name
, const Functor
<std::string ()> &read
) {
3002 local
.links
[name
] = read();
3005 auto plist(plist_new_dict());
3006 _scope({ plist_free(plist
); });
3008 for (const auto &version
: versions
) {
3009 auto files(plist_new_dict());
3010 plist_dict_set_item(plist
, ("files" + version
.first
).c_str(), files
);
3012 for (const auto &rule
: version
.second
)
3015 bool old(&version
.second
== &rules1
);
3017 for (const auto &hash
: local
.files
)
3018 for (const auto &rule
: version
.second
)
3019 if (rule(hash
.first
)) {
3020 if (!old
&& mac
&& excludes
.find(hash
.first
) != excludes
.end());
3021 else if (old
&& rule
.mode_
== NoMode
)
3022 plist_dict_set_item(files
, hash
.first
.c_str(), plist_new_data(reinterpret_cast<const char *>(hash
.second
.sha1_
), sizeof(hash
.second
.sha1_
)));
3023 else if (rule
.mode_
!= OmitMode
) {
3024 auto entry(plist_new_dict());
3025 plist_dict_set_item(entry
, "hash", plist_new_data(reinterpret_cast<const char *>(hash
.second
.sha1_
), sizeof(hash
.second
.sha1_
)));
3027 plist_dict_set_item(entry
, "hash2", plist_new_data(reinterpret_cast<const char *>(hash
.second
.sha256_
), sizeof(hash
.second
.sha256_
)));
3028 if (rule
.mode_
== OptionalMode
)
3029 plist_dict_set_item(entry
, "optional", plist_new_bool(true));
3030 plist_dict_set_item(files
, hash
.first
.c_str(), entry
);
3037 for (const auto &link
: local
.links
)
3038 for (const auto &rule
: version
.second
)
3039 if (rule(link
.first
)) {
3040 if (rule
.mode_
!= OmitMode
) {
3041 auto entry(plist_new_dict());
3042 plist_dict_set_item(entry
, "symlink", plist_new_string(link
.second
.c_str()));
3043 if (rule
.mode_
== OptionalMode
)
3044 plist_dict_set_item(entry
, "optional", plist_new_bool(true));
3045 plist_dict_set_item(files
, link
.first
.c_str(), entry
);
3052 for (const auto &bundle
: bundles
) {
3053 auto entry(plist_new_dict());
3054 plist_dict_set_item(entry
, "cdhash", plist_new_data(reinterpret_cast<const char *>(bundle
.second
.hash
.sha256_
), sizeof(bundle
.second
.hash
.sha256_
)));
3055 plist_dict_set_item(entry
, "requirement", plist_new_string("anchor apple generic"));
3056 plist_dict_set_item(files
, bundle
.first
.c_str(), entry
);
3060 for (const auto &version
: versions
) {
3061 auto rules(plist_new_dict());
3062 plist_dict_set_item(plist
, ("rules" + version
.first
).c_str(), rules
);
3064 std::multiset
<const Rule
*, RuleCode
> ordered
;
3065 for (const auto &rule
: version
.second
)
3066 ordered
.insert(&rule
);
3068 for (const auto &rule
: ordered
)
3069 if (rule
->weight_
== 1 && rule
->mode_
== NoMode
)
3070 plist_dict_set_item(rules
, rule
->code_
.c_str(), plist_new_bool(true));
3072 auto entry(plist_new_dict());
3073 plist_dict_set_item(rules
, rule
->code_
.c_str(), entry
);
3075 switch (rule
->mode_
) {
3079 plist_dict_set_item(entry
, "omit", plist_new_bool(true));
3082 plist_dict_set_item(entry
, "optional", plist_new_bool(true));
3085 plist_dict_set_item(entry
, "nested", plist_new_bool(true));
3088 plist_dict_set_item(entry
, "top", plist_new_bool(true));
3092 if (rule
->weight_
>= 10000)
3093 plist_dict_set_item(entry
, "weight", plist_new_uint(rule
->weight_
));
3094 else if (rule
->weight_
!= 1)
3095 plist_dict_set_item(entry
, "weight", plist_new_real(rule
->weight_
));
3099 folder
.Save(signature
, true, NULL
, fun([&](std::streambuf
&save
) {
3100 HashProxy
proxy(local
.files
[signature
], save
);
3103 plist_to_xml(plist
, &xml
, &size
);
3104 _scope({ free(xml
); });
3105 put(proxy
, xml
, size
);
3109 bundle
.path
= folder
.Path(executable
);
3111 folder
.Open(executable
, fun([&](std::streambuf
&buffer
, size_t length
, const void *flag
) {
3112 progress(root
+ executable
);
3113 folder
.Save(executable
, true, flag
, fun([&](std::streambuf
&save
) {
3115 slots
[1] = local
.files
.at(info
);
3116 slots
[3] = local
.files
.at(signature
);
3117 bundle
.hash
= Sign(NULL
, 0, buffer
, local
.files
[executable
], save
, identifier
, entitlements
, merge
, requirements
, key
, slots
, length
, 0, platform
, Progression(progress
, root
+ executable
));
3124 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
, bool merge
, uint8_t platform
, const Progress
&progress
) {
3126 return Sign(root
, folder
, key
, local
, requirements
, alter
, merge
, platform
, progress
);
3132 std::string
Hex(const uint8_t *data
, size_t size
) {
3134 hex
.reserve(size
* 2);
3135 for (size_t i(0); i
!= size
; ++i
) {
3136 hex
+= "0123456789abcdef"[data
[i
] >> 4];
3137 hex
+= "0123456789abcdef"[data
[i
] & 0xf];
3142 static void usage(const char *argv0
) {
3143 fprintf(stderr
, "Link Identity Editor %s\n\n", LDID_VERSION
);
3144 fprintf(stderr
, "Usage: %s [-Acputype:subtype] [-a] [-C[adhoc | enforcement | expires | hard |\n", argv0
);
3145 fprintf(stderr
, " host | kill | library-validation | restrict | runtime]] [-D] [-d]\n");
3146 fprintf(stderr
, " [-Enum:file] [-e] [-H[sha1 | sha256]] [-h] [-Iname]\n");
3147 fprintf(stderr
, " [-Kkey.p12 [-Upassword]] [-M] [-P] [-Qrequirements.xml] [-q]\n");
3148 fprintf(stderr
, " [-r | -Sfile.xml | -s] [-u] [-arch arch_type] file ...\n");
3149 fprintf(stderr
, "Options:\n");
3150 fprintf(stderr
, " -S[file.xml] Pseudo-sign using the entitlements in file.xml\n");
3151 fprintf(stderr
, " -Kkey.p12 Sign using private key in key.p12\n");
3152 fprintf(stderr
, " -Upassword Use password to unlock key.p12\n");
3153 fprintf(stderr
, " -M Merge entitlements with any existing\n");
3154 fprintf(stderr
, " -h Print CDHash of file\n\n");
3155 fprintf(stderr
, "More information: 'man ldid'\n");
3158 void cleanupfunc(void) {
3159 for (const auto &temp
: cleanup
)
3160 remove(temp
.c_str());
3163 #ifndef LDID_NOTOOLS
3164 int main(int argc
, char *argv
[]) {
3165 std::atexit(cleanupfunc
);
3166 OpenSSL_add_all_algorithms();
3167 # if OPENSSL_VERSION_MAJOR >= 3
3168 OSSL_PROVIDER
*legacy
= OSSL_PROVIDER_load(NULL
, "legacy");
3169 OSSL_PROVIDER
*deflt
= OSSL_PROVIDER_load(NULL
, "default");
3177 little_
= endian
.byte
[0];
3201 uint8_t platform(0);
3203 uint32_t flag_CPUType(_not(uint32_t));
3204 uint32_t flag_CPUSubtype(_not(uint32_t));
3206 const char *flag_I(NULL
);
3214 std::vector
<std::string
> files
;
3221 for (int argi(1); argi
!= argc
; ++argi
)
3222 if (argv
[argi
][0] != '-')
3223 files
.push_back(argv
[argi
]);
3224 else if (strcmp(argv
[argi
], "-arch") == 0) {
3225 bool foundarch
= false;
3229 fprintf(stderr
, "ldid: -arch must be followed by an architecture string\n");
3232 for (int i
= 0; archs
[i
].name
!= NULL
; i
++) {
3233 if (strcmp(archs
[i
].name
, argv
[argi
]) == 0) {
3234 flag_CPUType
= archs
[i
].cputype
;
3235 flag_CPUSubtype
= archs
[i
].cpusubtype
;
3243 fprintf(stderr
, "error: unknown architecture specification flag: -arch %s\n", argv
[argi
]);
3246 } else switch (argv
[argi
][1]) {
3248 if (flag_s
|| flag_S
) {
3249 fprintf(stderr
, "ldid: Can only specify one of -r, -S, -s\n");
3255 case 'e': flag_e
= true; break;
3258 const char *string
= argv
[argi
] + 2;
3259 const char *colon
= strchr(string
, ':');
3260 if (colon
== NULL
) {
3264 Map
file(colon
+ 1, O_RDONLY
, PROT_READ
, MAP_PRIVATE
);
3266 unsigned number(strtoul(string
, &arge
, 0));
3267 if (arge
!= colon
|| (number
== 0 && errno
== EINVAL
)) {
3271 auto &slot(slots
[number
]);
3272 for (Algorithm
*algorithm
: GetAlgorithms())
3273 (*algorithm
)(slot
, file
.data(), file
.size());
3276 case 'q': flag_q
= true; break;
3279 const char *hash
= argv
[argi
] + 2;
3288 if (strcmp(hash
, "sha1") == 0)
3290 else if (strcmp(hash
, "sha256") == 0)
3293 fprintf(stderr
, "ldid: only sha1 and sha256 are supported at this time\n");
3298 case 'h': flag_h
= true; break;
3301 const char *xml
= argv
[argi
] + 2;
3302 requirements
.open(xml
, O_RDONLY
, PROT_READ
, MAP_PRIVATE
);
3305 case 'D': flag_D
= true; break;
3306 case 'd': flag_d
= true; break;
3308 case 'a': flag_a
= true; break;
3312 fprintf(stderr
, "ldid: -A can only be specified once\n");
3316 if (argv
[argi
][2] != '\0') {
3317 const char *cpu
= argv
[argi
] + 2;
3318 const char *colon
= strchr(cpu
, ':');
3319 if (colon
== NULL
) {
3324 flag_CPUType
= strtoul(cpu
, &arge
, 0);
3325 if (arge
!= colon
|| (flag_CPUType
== 0 && errno
== EINVAL
)) {
3329 flag_CPUSubtype
= strtoul(colon
+ 1, &arge
, 0);
3330 if (arge
!= argv
[argi
] + strlen(argv
[argi
]) || (flag_CPUSubtype
== 0 && errno
== EINVAL
)) {
3338 const char *name
= argv
[argi
] + 2;
3339 if (strcmp(name
, "host") == 0)
3340 flags
|= kSecCodeSignatureHost
;
3341 else if (strcmp(name
, "adhoc") == 0)
3342 flags
|= kSecCodeSignatureAdhoc
;
3343 else if (strcmp(name
, "hard") == 0)
3344 flags
|= kSecCodeSignatureForceHard
;
3345 else if (strcmp(name
, "kill") == 0)
3346 flags
|= kSecCodeSignatureForceKill
;
3347 else if (strcmp(name
, "expires") == 0)
3348 flags
|= kSecCodeSignatureForceExpiration
;
3349 else if (strcmp(name
, "restrict") == 0)
3350 flags
|= kSecCodeSignatureRestrict
;
3351 else if (strcmp(name
, "enforcement") == 0)
3352 flags
|= kSecCodeSignatureEnforcement
;
3353 else if (strcmp(name
, "library-validation") == 0)
3354 flags
|= kSecCodeSignatureLibraryValidation
;
3355 else if (strcmp(name
, "runtime") == 0)
3356 flags
|= kSecCodeSignatureRuntime
;
3358 fprintf(stderr
, "ldid: -C: Unsupported option\n");
3364 if (argv
[argi
][2] != '\0') {
3365 char *platformchar
= argv
[argi
] + 2;
3367 platform
= strtoul(platformchar
, &arge
, 0);
3374 if (flag_r
|| flag_S
) {
3375 fprintf(stderr
, "ldid: Can only specify one of -r, -S, -s\n");
3379 entitlements
.clear();
3384 if (flag_r
|| flag_s
) {
3385 fprintf(stderr
, "ldid: Can only specify one of -r, -S, -s\n");
3389 if (argv
[argi
][2] != '\0') {
3390 const char *xml
= argv
[argi
] + 2;
3391 entitlements
.open(xml
, O_RDONLY
, PROT_READ
, MAP_PRIVATE
);
3400 password
= argv
[argi
] + 2;
3404 if (argv
[argi
][2] != '\0')
3405 key
.open(argv
[argi
] + 2, O_RDONLY
, PROT_READ
, MAP_PRIVATE
);
3415 flag_I
= argv
[argi
] + 2;
3424 if (flag_I
!= NULL
&& !flag_S
) {
3425 fprintf(stderr
, "ldid: -I requires -S\n");
3432 size_t filei(0), filee(0);
3433 _foreach (file
, files
) try {
3434 std::string
path(file
);
3437 if (stat(path
.c_str(), &info
) == -1) {
3438 fprintf(stderr
, "ldid: %s: %s\n", path
.c_str(), strerror(errno
));
3442 if (S_ISDIR(info
.st_mode
)) {
3443 if (!flag_S
&& !flag_s
) {
3444 fprintf(stderr
, "ldid: Only -S and -s can be used on directories\n");
3447 ldid::DiskFolder
folder(path
+ "/");
3448 path
+= "/" + Sign("", folder
, key
, requirements
, ldid::fun([&](const std::string
&, const std::string
&) -> std::string
{ return entitlements
; }), flag_M
, platform
, dummy_
).path
;
3449 } else if (flag_S
|| flag_r
|| flag_s
) {
3450 Map
input(path
, O_RDONLY
, PROT_READ
, MAP_PRIVATE
);
3452 std::filebuf output
;
3454 auto temp(Temporary(output
, split
));
3457 ldid::Unsign(input
.data(), input
.size(), output
, dummy_
);
3459 std::string
identifier(flag_I
?: split
.base
.c_str());
3460 ldid::Sign(input
.data(), input
.size(), output
, identifier
, entitlements
, flag_M
, requirements
, key
, slots
, flags
, platform
, dummy_
);
3466 Map
mapping(path
, flag_D
? true : false);
3467 FatHeader
fat_header(mapping
.data(), mapping
.size());
3469 _foreach (mach_header
, fat_header
.GetMachHeaders()) {
3470 struct linkedit_data_command
*signature(NULL
);
3471 struct encryption_info_command
*encryption(NULL
);
3474 if (mach_header
.GetCPUType() != flag_CPUType
)
3476 if (mach_header
.GetCPUSubtype() != flag_CPUSubtype
)
3481 printf("cpu=0x%x:0x%x\n", mach_header
.GetCPUType(), mach_header
.GetCPUSubtype());
3483 _foreach (load_command
, mach_header
.GetLoadCommands()) {
3484 uint32_t cmd(mach_header
.Swap(load_command
->cmd
));
3486 if (cmd
== LC_CODE_SIGNATURE
)
3487 signature
= reinterpret_cast<struct linkedit_data_command
*>(load_command
);
3488 else if (cmd
== LC_ENCRYPTION_INFO
|| cmd
== LC_ENCRYPTION_INFO_64
)
3489 encryption
= reinterpret_cast<struct encryption_info_command
*>(load_command
);
3490 else if (cmd
== LC_LOAD_DYLIB
) {
3491 volatile struct dylib_command
*dylib_command(reinterpret_cast<struct dylib_command
*>(load_command
));
3492 const char *name(reinterpret_cast<const char *>(load_command
) + mach_header
.Swap(dylib_command
->dylib
.name
));
3494 if (strcmp(name
, "/System/Library/Frameworks/UIKit.framework/UIKit") == 0) {
3497 version
.value
= mach_header
.Swap(dylib_command
->dylib
.current_version
);
3498 printf("uikit=%u.%u.%u\n", version
.major
, version
.minor
, version
.patch
);
3504 if (flag_d
&& encryption
!= NULL
) {
3505 printf("cryptid=%d\n", mach_header
.Swap(encryption
->cryptid
));
3509 if (encryption
== NULL
) {
3510 fprintf(stderr
, "ldid: -D requires an encrypted binary\n");
3513 encryption
->cryptid
= mach_header
.Swap(0);
3516 if ((flag_e
|| flag_q
|| flag_h
) && signature
== NULL
) {
3517 fprintf(stderr
, "ldid: -e, -q, and -h requre a signed binary\n");
3522 uint32_t data
= mach_header
.Swap(signature
->dataoff
);
3524 uint8_t *top
= reinterpret_cast<uint8_t *>(mach_header
.GetBase());
3525 uint8_t *blob
= top
+ data
;
3526 struct SuperBlob
*super
= reinterpret_cast<struct SuperBlob
*>(blob
);
3528 for (size_t index(0); index
!= Swap(super
->count
); ++index
)
3529 if (Swap(super
->index
[index
].type
) == CSSLOT_ENTITLEMENTS
) {
3530 uint32_t begin
= Swap(super
->index
[index
].offset
);
3531 struct Blob
*entitlements
= reinterpret_cast<struct Blob
*>(blob
+ begin
);
3532 fwrite(entitlements
+ 1, 1, Swap(entitlements
->length
) - sizeof(*entitlements
), stdout
);
3537 uint32_t data
= mach_header
.Swap(signature
->dataoff
);
3539 uint8_t *top
= reinterpret_cast<uint8_t *>(mach_header
.GetBase());
3540 uint8_t *blob
= top
+ data
;
3541 struct SuperBlob
*super
= reinterpret_cast<struct SuperBlob
*>(blob
);
3543 for (size_t index(0); index
!= Swap(super
->count
); ++index
)
3544 if (Swap(super
->index
[index
].type
) == CSSLOT_REQUIREMENTS
) {
3545 uint32_t begin
= Swap(super
->index
[index
].offset
);
3546 struct Blob
*requirement
= reinterpret_cast<struct Blob
*>(blob
+ begin
);
3547 fwrite(requirement
, 1, Swap(requirement
->length
), stdout
);
3552 char *buf
= _syscall(realpath(file
.c_str(), NULL
));
3553 printf("Executable=%s\n", buf
);
3556 auto algorithms(GetAlgorithms());
3558 uint32_t data
= mach_header
.Swap(signature
->dataoff
);
3560 uint8_t *top
= reinterpret_cast<uint8_t *>(mach_header
.GetBase());
3561 uint8_t *blob
= top
+ data
;
3562 struct SuperBlob
*super
= reinterpret_cast<struct SuperBlob
*>(blob
);
3565 CodeDirectory
*directory_
;
3567 Algorithm
&algorithm_
;
3572 std::map
<uint8_t, Candidate
> candidates
;
3573 uint32_t cmsBegin
= 0, cmsEnd
= 0;
3575 for (size_t index(0); index
!= Swap(super
->count
); ++index
) {
3576 auto type(Swap(super
->index
[index
].type
));
3577 if ((type
== CSSLOT_CODEDIRECTORY
|| type
>= CSSLOT_ALTERNATE
) && type
!= CSSLOT_SIGNATURESLOT
) {
3578 uint32_t begin
= Swap(super
->index
[index
].offset
);
3579 uint32_t end
= index
+ 1 == Swap(super
->count
) ? Swap(super
->blob
.length
) : Swap(super
->index
[index
+ 1].offset
);
3580 struct CodeDirectory
*directory
= reinterpret_cast<struct CodeDirectory
*>(blob
+ begin
+ sizeof(Blob
));
3581 auto type(directory
->hashType
);
3582 _assert(type
> 0 && type
<= algorithms
.size());
3583 auto &algorithm(*algorithms
[type
- 1]);
3584 uint8_t hash
[algorithm
.size_
];
3585 algorithm(hash
, blob
+ begin
, end
- begin
);
3586 candidates
.insert({type
, {directory
, end
- begin
, algorithm
, Hex(hash
, algorithm
.size_
), begin
}});
3587 } else if (type
== CSSLOT_SIGNATURESLOT
) {
3588 cmsBegin
= Swap(super
->index
[index
].offset
);
3589 cmsEnd
= index
+ 1 == Swap(super
->count
) ? Swap(super
->blob
.length
) : Swap(super
->index
[index
+ 1].offset
);
3593 _assert(!candidates
.empty());
3594 auto best(candidates
.end());
3597 const auto directory(best
->second
.directory_
);
3598 const auto flags(Swap(directory
->flags
));
3600 printf("Identifier=%s\n", blob
+ best
->second
.offset
+ Swap(directory
->identOffset
));
3603 if (flags
& kSecCodeSignatureHost
)
3605 if (flags
& kSecCodeSignatureAdhoc
)
3607 if (flags
& kSecCodeSignatureForceHard
)
3609 if (flags
& kSecCodeSignatureForceKill
)
3611 if (flags
& kSecCodeSignatureForceExpiration
)
3612 names
+= ",expires";
3613 if (flags
& kSecCodeSignatureRestrict
)
3614 names
+= ",restrict";
3615 if (flags
& kSecCodeSignatureEnforcement
)
3616 names
+= ",enforcement";
3617 if (flags
& kSecCodeSignatureLibraryValidation
)
3618 names
+= ",library-validation";
3619 if (flags
& kSecCodeSignatureRuntime
)
3620 names
+= ",runtime";
3622 printf("CodeDirectory v=%x size=%zd flags=0x%x(%s) hashes=%d+%d location=embedded\n",
3623 Swap(directory
->version
), best
->second
.size_
, flags
, names
.empty() ? "none" : names
.c_str() + 1, Swap(directory
->nCodeSlots
), Swap(directory
->nSpecialSlots
));
3624 printf("Hash type=%s size=%d\n", best
->second
.algorithm_
.name(), directory
->hashSize
);
3626 std::string choices
;
3627 for (const auto &candidate
: candidates
) {
3628 auto choice(candidate
.second
.algorithm_
.name());
3631 printf("CandidateCDHash %s=%.40s\n", choice
, candidate
.second
.hash_
.c_str());
3632 printf("CandidateCDHashFull %s=%s\n", choice
, candidate
.second
.hash_
.c_str());
3634 printf("Hash choices=%s\n", choices
.c_str() + 1);
3636 printf("CDHash=%.40s\n", best
->second
.hash_
.c_str());
3638 if (cmsBegin
!= 0 && cmsEnd
!= 0) {
3639 // This loads the CMS blob and parses each X509 cert in the blob to extract the
3640 // common name and print it as "Authority=%s"
3641 Buffer
bio(reinterpret_cast<const char *>(blob
) + cmsBegin
+ sizeof(Blob
), cmsEnd
- cmsBegin
);
3643 if ((p7
= d2i_PKCS7_bio(bio
, NULL
)) == NULL
) {
3644 // In order to follow codesign, we just ignore errors
3645 printf("Authority=(unavailable)\n");
3647 STACK_OF(X509
) *certs
= NULL
;
3648 switch (OBJ_obj2nid(p7
->type
)) {
3649 case NID_pkcs7_signed
:
3650 if (p7
->d
.sign
!= NULL
)
3651 certs
= p7
->d
.sign
->cert
;
3653 case NID_pkcs7_signedAndEnveloped
:
3654 if (p7
->d
.signed_and_enveloped
!= NULL
)
3655 certs
= p7
->d
.signed_and_enveloped
->cert
;
3660 if (certs
!= NULL
) {
3662 for (int i
= 0; i
< sk_X509_num(certs
); i
++) {
3663 x
= sk_X509_value(certs
, i
);
3665 X509_NAME
*nm
= X509_get_subject_name(x
);
3669 lastpos
= X509_NAME_get_index_by_NID(nm
, NID_commonName
, lastpos
);
3672 e
= X509_NAME_get_entry(nm
, lastpos
);
3673 ASN1_STRING
*s
= X509_NAME_ENTRY_get_data(e
);
3674 printf("Authority=%s\n", reinterpret_cast<const char *>(ASN1_STRING_get0_data(s
)));
3678 printf("Authority=(unavailable)\n");
3684 if (Swap(directory
->teamIDOffset
) > 0)
3685 printf("TeamIdentifier=%s\n", blob
+ best
->second
.offset
+ Swap(directory
->teamIDOffset
));
3687 printf("TeamIdentifier=not set\n");
3692 } catch (const char *) {
3697 # if OPENSSL_VERSION_MAJOR >= 3
3698 OSSL_PROVIDER_unload(legacy
);
3699 OSSL_PROVIDER_unload(deflt
);
3704 #endif // LDID_NOTOOLS