X-Git-Url: https://git.saurik.com/apple/libsecurity_codesigning.git/blobdiff_plain/6aae018b5d43c30038cfa4003e5d4bcc81f134cf..935e692843d9c528f9a4c5eee98e00961ca5f4a4:/lib/cdbuilder.cpp diff --git a/lib/cdbuilder.cpp b/lib/cdbuilder.cpp index 6d63525..ac17edb 100644 --- a/lib/cdbuilder.cpp +++ b/lib/cdbuilder.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2006 Apple Computer, Inc. All Rights Reserved. + * Copyright (c) 2006-2010 Apple Inc. All Rights Reserved. * * @APPLE_LICENSE_HEADER_START@ * @@ -39,14 +39,22 @@ namespace CodeSigning { // // Create an (empty) builder // -CodeDirectory::Builder::Builder() - : mFlags(0), mSpecialSlots(0), mCodeSlots(0), mScatter(NULL), mScatterSize(0), mDir(NULL) +CodeDirectory::Builder::Builder(HashAlgorithm digestAlgorithm) + : mFlags(0), + mHashType(digestAlgorithm), + mSpecialSlots(0), + mCodeSlots(0), + mScatter(NULL), + mScatterSize(0), + mDir(NULL) { - memset(mSpecial, 0, sizeof(mSpecial)); + mDigestLength = MakeHash(this)->digestLength(); + mSpecial = (unsigned char *)calloc(cdSlotMax, mDigestLength); } CodeDirectory::Builder::~Builder() { + ::free(mSpecial); ::free(mScatter); } @@ -77,12 +85,12 @@ void CodeDirectory::Builder::reopen(string path, size_t offset, size_t length) // // Set the source for one special slot // -void CodeDirectory::Builder::special(size_t slot, CFDataRef data) +void CodeDirectory::Builder::specialSlot(SpecialSlot slot, CFDataRef data) { assert(slot <= cdSlotMax); - Hash hash; - hash(CFDataGetBytePtr(data), CFDataGetLength(data)); - hash.finish(mSpecial[slot]); + MakeHash hash(this); + hash->update(CFDataGetBytePtr(data), CFDataGetLength(data)); + hash->finish(specialSlot(slot)); if (slot >= mSpecialSlots) mSpecialSlots = slot; } @@ -120,7 +128,7 @@ size_t CodeDirectory::Builder::size() size_t offset = sizeof(CodeDirectory); offset += mScatterSize; // scatter vector offset += mIdentifier.size() + 1; // size of identifier (with null byte) - offset += (mCodeSlots + mSpecialSlots) * Hash::digestLength; // hash vector + offset += (mCodeSlots + mSpecialSlots) * mDigestLength; // hash vector return offset; } @@ -155,8 +163,8 @@ CodeDirectory *CodeDirectory::Builder::build() mDir->nSpecialSlots = mSpecialSlots; mDir->nCodeSlots = mCodeSlots; mDir->codeLimit = mExecLength; - mDir->hashSize = Hash::digestLength; - mDir->hashType = cdHashTypeDefault; + mDir->hashType = mHashType; + mDir->hashSize = mDigestLength; if (mPageSize) { int pglog; assert(frexp(mPageSize, &pglog) == 0.5); // must be power of 2 @@ -181,21 +189,22 @@ CodeDirectory *CodeDirectory::Builder::build() // (add new flexibly-allocated fields here) - mDir->hashOffset = offset + mSpecialSlots * Hash::digestLength; - offset += (mSpecialSlots + mCodeSlots) * Hash::digestLength; + mDir->hashOffset = offset + mSpecialSlots * mDigestLength; + offset += (mSpecialSlots + mCodeSlots) * mDigestLength; assert(offset == total); // matches allocated size // fill special slots - memset((*mDir)[-mSpecialSlots], 0, mDir->hashSize * mSpecialSlots); + memset((*mDir)[-mSpecialSlots], 0, mDigestLength * mSpecialSlots); for (size_t slot = 1; slot <= mSpecialSlots; ++slot) - memcpy((*mDir)[-slot], &mSpecial[slot], Hash::digestLength); + memcpy((*mDir)[-slot], specialSlot(slot), mDigestLength); // fill code slots mExec.seek(mExecOffset); size_t remaining = mExecLength; for (unsigned int slot = 0; slot < mCodeSlots; ++slot) { size_t thisPage = min(mPageSize, remaining); - hash(mExec, (*mDir)[slot], thisPage); + MakeHash hasher(this); + generateHash(hasher, mExec, (*mDir)[slot], thisPage); remaining -= thisPage; }