2 * Copyright (c) 2006-2012,2014 Apple Inc. All Rights Reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
13 * The Original Code and all software distributed under the License are
14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18 * Please see the License for the specific language governing rights and
19 * limitations under the License.
21 * @APPLE_LICENSE_HEADER_END@
25 // cdbuilder - constructor for CodeDirectories
30 #include "codedirectory.h"
34 namespace CodeSigning
{
38 // Builder can construct CodeDirectories from pieces:
39 // Builder builder(...);
40 // builder.variousSetters(withSuitableData);
41 // CodeDirectory *result = builder.build();
42 // Builder is not reusable.
44 class CodeDirectory::Builder
: public RefCount
{
47 Builder(HashAlgorithm digestAlgorithm
);
50 void executable(string path
, size_t pagesize
, size_t offset
, size_t length
);
51 void reopen(string path
, size_t offset
, size_t length
);
53 void specialSlot(SpecialSlot slot
, CFDataRef data
);
54 void identifier(const std::string
&code
) { mIdentifier
= code
; }
55 void teamID(const std::string
&team
) { mTeamID
= team
; }
56 void flags(uint32_t f
) { mFlags
= f
; }
57 void platform(uint8_t p
) { mPlatform
= p
; }
58 std::set
<Slot
> filledSpecialSlots() const { return mFilledSpecialSlots
; }
60 Scatter
*scatter(unsigned count
); // allocate that many scatter elements (w/o sentinel)
61 Scatter
*scatter() { return mScatter
; } // return already allocated scatter vector
63 size_t size(const uint32_t version
); // calculate size
64 CodeDirectory
*build(); // build CodeDirectory and return it
65 const size_t fixedSize(const uint32_t version
); // calculate fixed size of the CodeDirectory
67 uint32_t hashType() const { return mHashType
; }
69 DynamicHash
*getHash() const { return CodeDirectory::hashFor(this->mHashType
); }
72 Hashing::Byte
*specialSlot(SpecialSlot slot
)
73 { assert(slot
> 0 && slot
<= cdSlotMax
); return mSpecial
+ (slot
- 1) * mDigestLength
; }
74 Hashing::Byte
*specialSlot(SpecialSlot slot
) const
75 { assert(slot
> 0 && slot
<= cdSlotMax
); return mSpecial
+ (slot
- 1) * mDigestLength
; }
78 Hashing::Byte
*mSpecial
; // array of special slot hashes
79 std::set
<Slot
> mFilledSpecialSlots
; // special slots filled with values
80 UnixPlusPlus::AutoFileDesc mExec
; // main executable file
81 size_t mExecOffset
; // starting offset in mExec
82 size_t mExecLength
; // total bytes of file to sign
83 size_t mPageSize
; // page size of executable (bytes)
84 uint32_t mFlags
; // CodeDirectory flags
85 uint32_t mHashType
; // digest algorithm code
86 uint8_t mPlatform
; // platform identifier
87 uint32_t mDigestLength
; // number of bytes in a single glue digest
88 std::string mIdentifier
; // canonical identifier
89 std::string mTeamID
; // team identifier
91 size_t mSpecialSlots
; // highest special slot set
92 size_t mCodeSlots
; // number of code pages (slots)
94 Scatter
*mScatter
; // scatter vector
95 size_t mScatterSize
; // number of scatter elements allocated (incl. sentinel)
97 CodeDirectory
*mDir
; // what we're building
105 #endif //_H_CDBUILDER