Changeset 353 for trunk/util/ldid.cpp

Show
Ignore:
Timestamp:
07/06/08 20:56:57 (6 months ago)
Author:
saurik
Message:

Added support for entitlement XML files.

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • trunk/util/ldid.cpp

    r316 r353  
    5252    uint32_t magic; 
    5353    uint32_t nfat_arch; 
    54 }; 
     54} _packed; 
    5555 
    5656#define FAT_MAGIC 0xcafebabe 
     
    6363    uint32_t size; 
    6464    uint32_t align; 
    65 }; 
     65} _packed; 
    6666 
    6767struct mach_header { 
     
    7373    uint32_t sizeofcmds; 
    7474    uint32_t flags; 
    75 }; 
     75} _packed; 
    7676 
    7777#define MH_MAGIC 0xfeedface 
     
    8686    uint32_t cmd; 
    8787    uint32_t cmdsize; 
    88 }; 
     88} _packed; 
    8989 
    9090#define LC_REQ_DYLD  0x80000000 
     
    101101    uint32_t current_version; 
    102102    uint32_t compatibility_version; 
    103 }; 
     103} _packed; 
    104104 
    105105struct dylib_command { 
     
    107107    uint32_t cmdsize; 
    108108    struct dylib dylib; 
    109 }; 
     109} _packed; 
    110110 
    111111struct uuid_command { 
     
    113113    uint32_t cmdsize; 
    114114    uint8_t uuid[16]; 
    115 }; 
     115} _packed; 
    116116 
    117117struct linkedit_data_command { 
     
    120120    uint32_t dataoff; 
    121121    uint32_t datasize; 
    122 }; 
     122} _packed; 
    123123 
    124124uint16_t Swap_(uint16_t value) { 
     
    246246}; 
    247247 
    248 #define CSMAGIC_CODEDIRECTORY 0xfade0c02 
     248#define CSMAGIC_CODEDIRECTORY      0xfade0c02 
    249249#define CSMAGIC_EMBEDDED_SIGNATURE 0xfade0cc0 
     250#define CSMAGIC_ENTITLEMENTS       0xfade7171 
     251 
    250252#define CSSLOT_CODEDIRECTORY 0 
    251 #define CSSLOT_REQUIREMENTS 2 
     253#define CSSLOT_REQUIREMENTS  2 
     254#define CSSLOT_ENTITLEMENTS  5 
    252255 
    253256struct BlobIndex { 
    254257    uint32_t type; 
    255258    uint32_t offset; 
    256 }; 
    257  
    258 struct SuperBlob { 
     259} _packed; 
     260 
     261struct Blob { 
    259262    uint32_t magic; 
    260263    uint32_t length; 
     264} _packed; 
     265 
     266struct SuperBlob { 
     267    struct Blob blob; 
    261268    uint32_t count; 
    262269    struct BlobIndex index[]; 
    263 }; 
     270} _packed; 
    264271 
    265272struct CodeDirectory { 
    266     uint32_t magic; 
    267     uint32_t length; 
     273    struct Blob blob; 
    268274    uint32_t version; 
    269275    uint32_t flags; 
     
    278284    uint8_t pageSize; 
    279285    uint32_t spare2; 
    280 }; 
     286} _packed; 
    281287 
    282288extern "C" uint32_t hash(uint8_t *k, uint32_t length, uint32_t initval); 
     
    302308    bool timeh(false); 
    303309    uint32_t timev(0); 
     310 
     311    const void *xmld(NULL); 
     312    size_t xmls(0); 
    304313 
    305314    std::vector<std::string> files; 
     
    314323            case 'u': flag_u = true; break; 
    315324            case 'p': flag_p = true; break; 
    316             case 'S': flag_S = true; break; 
     325 
     326            case 'S': 
     327                flag_S = true; 
     328                if (argv[argi][2] != '\0') { 
     329                    const char *xml = argv[argi] + 2; 
     330                    xmld = map(xml, 0, _not(size_t), &xmls, true); 
     331                } 
     332            break; 
    317333 
    318334            case 'T': { 
     
    389405            if (pid == 0) { 
    390406                char *ssize; 
    391                 asprintf(&ssize, "%u", (sizeof(struct SuperBlob) + 2 * sizeof(struct BlobIndex) + sizeof(struct CodeDirectory) + strlen(base) + 1 + (size + 0x1000 - 1) / 0x1000 * 0x14 + 0xc + 15) / 16 * 16); 
     407                asprintf(&ssize, "%u", (sizeof(struct SuperBlob) + 2 * sizeof(struct BlobIndex) + sizeof(struct CodeDirectory) + strlen(base) + 1 + (size + 0x1000 - 1) / 0x1000 * 0x14 + 0xc + (xmld == NULL ? 0 : 0x10 + xmls) + 15) / 16 * 16); 
     408                //printf("codesign_allocate -i %s -a %s %s -o %s\n", path, arch, ssize, temp); 
    392409                execlp(allocate, allocate, "-i", path, "-a", arch, ssize, "-o", temp, NULL); 
    393410                _assert(false); 
     
    454471            uint8_t *blob = top + data; 
    455472            struct SuperBlob *super = reinterpret_cast<struct SuperBlob *>(blob); 
    456             super->magic = Swap(CSMAGIC_EMBEDDED_SIGNATURE); 
    457  
    458             uint32_t count = 2; 
     473            super->blob.magic = Swap(CSMAGIC_EMBEDDED_SIGNATURE); 
     474 
     475            uint32_t count = xmld == NULL ? 2 : 3; 
    459476            uint32_t offset = sizeof(struct SuperBlob) + count * sizeof(struct BlobIndex); 
    460477 
     
    466483            offset += sizeof(struct CodeDirectory); 
    467484 
    468             directory->magic = Swap(CSMAGIC_CODEDIRECTORY); 
     485            directory->blob.magic = Swap(CSMAGIC_CODEDIRECTORY); 
    469486            directory->version = Swap(0x00020001); 
    470487            directory->flags = Swap(0); 
     
    495512            directory->hashOffset = Swap(offset - begin); 
    496513            offset += sizeof(*hashes) * (special + pages); 
    497             directory->length = Swap(offset - begin); 
     514            directory->blob.length = Swap(offset - begin); 
    498515 
    499516            super->index[1].type = Swap(CSSLOT_REQUIREMENTS); 
     
    503520            offset += 0xc; 
    504521 
     522            if (xmld != NULL) { 
     523                super->index[2].type = Swap(CSSLOT_ENTITLEMENTS); 
     524                super->index[2].offset = Swap(offset); 
     525 
     526                uint32_t begin = offset; 
     527                struct Blob *entitlements = reinterpret_cast<struct Blob *>(blob + begin); 
     528                offset += sizeof(struct Blob); 
     529 
     530                memcpy(blob + offset, xmld, xmls); 
     531                offset += xmls; 
     532 
     533                entitlements->magic = Swap(CSMAGIC_ENTITLEMENTS); 
     534                entitlements->length = Swap(offset - begin); 
     535            } 
     536 
    505537            super->count = Swap(count); 
    506             super->length = Swap(offset); 
    507  
    508             _assert(offset <= size); 
     538            super->blob.length = Swap(offset); 
     539 
     540            if (offset > size) { 
     541                fprintf(stderr, "offset (%zu) > size (%zu)\n", offset, size); 
     542                _assert(false); 
     543            } //else fprintf(stderr, "offset (%zu) <= size (%zu)\n", offset, size); 
     544 
    509545            memset(blob + offset, 0, size - offset); 
    510546        }