Changeset 360 for trunk/util

Show
Ignore:
Timestamp:
07/18/08 08:19:34 (6 months ago)
Author:
saurik
Message:

Added codehash update-only and entitlement hashing.

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • trunk/util/ldid.cpp

    r354 r360  
    305305 
    306306    bool flag_T(false); 
     307 
    307308    bool flag_S(false); 
     309    bool flag_s(false); 
    308310 
    309311    bool timeh(false); 
     
    325327            case 'p': flag_p = true; break; 
    326328 
     329            case 's': 
     330                _assert(!flag_S); 
     331                flag_s = true; 
     332            break; 
     333 
    327334            case 'S': 
     335                _assert(!flag_s); 
    328336                flag_S = true; 
    329337                if (argv[argi][2] != '\0') { 
     
    406414            if (pid == 0) { 
    407415                char *ssize; 
    408                 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); 
    409                 //printf("codesign_allocate -i %s -a %s %s -o %s\n", path, arch, ssize, temp); 
     416                asprintf(&ssize, "%u", (sizeof(struct SuperBlob) + 2 * sizeof(struct BlobIndex) + sizeof(struct CodeDirectory) + strlen(base) + 1 + ((xmld == NULL ? CSSLOT_REQUIREMENTS : CSSLOT_ENTITLEMENTS) + (size + 0x1000 - 1) / 0x1000) * 0x14 + 0xc + (xmld == NULL ? 0 : 0x10 + xmls) + 15) / 16 * 16); 
     417                //printf("%s -i %s -a %s %s -o %s\n", allocate, path, arch, ssize, temp); 
    410418                execlp(allocate, allocate, "-i", path, "-a", arch, ssize, "-o", temp, NULL); 
    411419                _assert(false); 
     
    461469                } 
    462470            } 
     471        } 
     472 
     473        if (flag_s) { 
     474            _assert(signature != NULL); 
     475 
     476            uint32_t data = framework.Swap(signature->dataoff); 
     477            uint32_t size = framework.Swap(signature->datasize); 
     478 
     479            uint8_t *top = reinterpret_cast<uint8_t *>(framework.GetBase()); 
     480            uint8_t *blob = top + data; 
     481            struct SuperBlob *super = reinterpret_cast<struct SuperBlob *>(blob); 
     482 
     483            for (size_t index(0); index != Swap(super->count); ++index) 
     484                if (Swap(super->index[index].type) == CSSLOT_CODEDIRECTORY) { 
     485                    uint32_t begin = Swap(super->index[index].offset); 
     486                    struct CodeDirectory *directory = reinterpret_cast<struct CodeDirectory *>(blob + begin); 
     487 
     488                    uint8_t (*hashes)[20] = reinterpret_cast<uint8_t (*)[20]>(blob + begin + Swap(directory->hashOffset)); 
     489                    uint32_t pages = Swap(directory->nCodeSlots); 
     490 
     491                    if (pages != 1) 
     492                        for (size_t i = 0; i != pages - 1; ++i) 
     493                            sha1(hashes[i], top + 0x1000 * i, 0x1000); 
     494                    if (pages != 0) 
     495                        sha1(hashes[pages - 1], top + 0x1000 * (pages - 1), data % 0x1000); 
     496                } 
    463497        } 
    464498 
     
    498532            offset += strlen(base) + 1; 
    499533 
     534            uint32_t special = xmld == NULL ? CSSLOT_REQUIREMENTS : CSSLOT_ENTITLEMENTS; 
     535            directory->nSpecialSlots = Swap(special); 
     536 
    500537            uint8_t (*hashes)[20] = reinterpret_cast<uint8_t (*)[20]>(blob + offset); 
    501             uint32_t special = 0; 
     538            memset(hashes, 0, sizeof(*hashes) * special); 
     539 
     540            offset += sizeof(*hashes) * special; 
     541            hashes += special; 
    502542 
    503543            uint32_t pages = (data + 0x1000 - 1) / 0x1000; 
    504             directory->nSpecialSlots = Swap(special); 
    505544            directory->nCodeSlots = Swap(pages); 
    506545 
    507546            if (pages != 1) 
    508547                for (size_t i = 0; i != pages - 1; ++i) 
    509                     sha1(hashes[special + i], top + 0x1000 * i, 0x1000); 
     548                    sha1(hashes[i], top + 0x1000 * i, 0x1000); 
    510549            if (pages != 0) 
    511                 sha1(hashes[special + pages - 1], top + 0x1000 * (pages - 1), data % 0x1000); 
     550                sha1(hashes[pages - 1], top + 0x1000 * (pages - 1), data % 0x1000); 
    512551 
    513552            directory->hashOffset = Swap(offset - begin); 
    514             offset += sizeof(*hashes) * (special + pages); 
     553            offset += sizeof(*hashes) * pages; 
    515554            directory->blob.length = Swap(offset - begin); 
    516555 
     
    534573                entitlements->magic = Swap(CSMAGIC_ENTITLEMENTS); 
    535574                entitlements->length = Swap(offset - begin); 
     575            } 
     576 
     577            for (size_t index(0); index != count; ++index) { 
     578                uint32_t type = Swap(super->index[index].type); 
     579                if (type != 0 && type <= special) { 
     580                    uint32_t offset = Swap(super->index[index].offset); 
     581                    struct Blob *local = (struct Blob *) (blob + offset); 
     582                    sha1((uint8_t *) (hashes - type), (uint8_t *) local, Swap(local->length)); 
     583                } 
    536584            } 
    537585