]> git.saurik.com Git - apple/libsecurity_codesigning.git/blob - lib/codedirectory.h
135f2aa81ec016e09b6c4c4723b5e573686c0b75
[apple/libsecurity_codesigning.git] / lib / codedirectory.h
1 /*
2 * Copyright (c) 2006 Apple Computer, Inc. All Rights Reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
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
11 * file.
12 *
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.
20 *
21 * @APPLE_LICENSE_HEADER_END@
22 */
23
24 //
25 // codedirectory - format and operations for code signing "code directory" structures
26 //
27 // A CodeDirectory is a contiguous binary blob containing a two-tiered
28 // hash of (much of) the contents of a real StaticCode object.
29 // It consists of a header followed by an array of hash vector elements.
30 //
31 // This structure is meant to be self-describing, binary stable, and endian independent.
32 //
33 #ifndef _H_CODEDIRECTORY
34 #define _H_CODEDIRECTORY
35
36 #include <security_utilities/unix++.h>
37 #include <security_utilities/blob.h>
38 #include <security_utilities/cfutilities.h>
39 #include <security_utilities/hashing.h>
40
41
42 namespace Security {
43 namespace CodeSigning {
44
45
46 //
47 // Types of hashes supported.
48 // Actually, right now, only SHA1 is really supported.
49 //
50 enum {
51 cdHashTypeSHA1 = 1,
52 cdHashTypeSHA256 = 2,
53
54 cdHashTypeDefault = cdHashTypeSHA1
55 };
56
57
58 //
59 // Conventional string names for various code signature components.
60 // Depending on storage, these may end up as filenames, extended attribute names, etc.
61 //
62 #define kSecCS_CODEDIRECTORYFILE "CodeDirectory" // CodeDirectory
63 #define kSecCS_SIGNATUREFILE "CodeSignature" // CMS Signature
64 #define kSecCS_REQUIREMENTSFILE "CodeRequirements" // internal requirements
65 #define kSecCS_RESOURCEDIRFILE "CodeResources" // resource directory
66 #define kSecCS_APPLICATIONFILE "CodeApplication" // application-specific resource
67 #define kSecCS_ENTITLEMENTFILE "CodeEntitlements" // entitlement configuration (just in case)
68
69
70 //
71 // Special hash slot values. In a CodeDirectory, these show up at negative slot
72 // indices. This enumeration is also used widely in various internal APIs, and as
73 // type values in embedded SuperBlobs.
74 //
75 enum {
76 //
77 // Primary slot numbers.
78 // These values are potentially present in the CodeDirectory hash array
79 // under their negative values. They are also used in APIs and SuperBlobs.
80 // Note that zero must not be used for these (it's page 0 of the main code array),
81 // and it's good to assign contiguous (very) small values for them.
82 //
83 cdInfoSlot = 1, // Info.plist
84 cdRequirementsSlot = 2, // internal requirements
85 cdResourceDirSlot = 3, // resource directory
86 cdApplicationSlot = 4, // Application specific slot
87 cdEntitlementSlot = 5, // embedded entitlement configuration
88 // (add further primary slot numbers here)
89
90 cdSlotCount, // total number of special slots (+1 for slot 0)
91 cdSlotMax = cdSlotCount - 1, // highest special slot number
92
93 //
94 // Virtual slot numbers.
95 // These values are NOT used in the CodeDirectory hash array. The are used as
96 // internal API identifiers and as types in in-image SuperBlobs.
97 // Zero is okay to use here; and we assign that to the CodeDirectory itself so
98 // it shows up first in (properly sorted) SuperBlob indices. The rest of the
99 // numbers is set Far Away so the primary slot set can expand safely.
100 // It's okay to have gaps in these assignments.
101 //
102 cdCodeDirectorySlot = 0, // CodeDirectory
103 cdSignatureSlot = 0x10000, // CMS signature
104 // (add further virtual slot numbers here)
105 };
106
107
108 //
109 // Special hash slot attributes.
110 // This is a central description of attributes of each slot.
111 // Various places in Code Signing pick up those attributes and act accordingly.
112 //
113 enum {
114 cdComponentPerArchitecture = 1, // slot value differs for each Mach-O architecture
115 cdComponentIsBlob = 2, // slot value is a Blob (need not be BlobWrapped)
116 };
117
118
119 //
120 // A CodeDirectory is a typed Blob describing the secured pieces of a program.
121 // This structure describes the common header and provides access to the variable-size
122 // elements packed after it. For help in constructing a CodeDirectory, use the nested
123 // Builder class.
124 //
125 // The hashes are stored as an array of digests. The array covers the range
126 // [-nSpecialSlots .. nCodeSlots-1]. Non-negative indices denote pages of the main
127 // executable. Negative indices indicate "special" hashes, each of a different thing
128 // (see cd*Slot constants above). Special slots that are in range but not present
129 // are zeroed out. Unallocated special slots are also presumed absent; this is not
130 // an error. (Thus the range of special slots can be extended at will.)
131 //
132 // HOW TO MANAGE COMPATIBILITY:
133 // Each CodeDirectory has a format (compatibility) version. Three constants control
134 // versioning:
135 // * currentVersion is the version used for newly created CodeDirectories.
136 // * compatibilityLimit is the highest version the code will accept as compatible.
137 // Test for version < currentVersion to detect old formats that may need special
138 // handling. The current code rejects those; add backward cases to checkVersion().
139 // Break backward compatibility by rejecting versions that are unsuitable.
140 // Accept currentVersion < version <= compatibilityLimit as versions newer than
141 // those understood by this code but engineered (by newer code) to be backward
142 // compatible. Reject version > compatibilityLimit as incomprehensible gibberish.
143 //
144 // When creating a new version, increment currentVersion. When adding new fixed fields,
145 // just append them; the flex fields will shift to make room. To add new flex fields,
146 // add a fixed field containing the new field's offset and add suitable computations
147 // to the Builder to place the new data (right) before the hash array. Older code will
148 // then simply ignore your new fields on load/read.
149 // Add flag bits to the existing flags field to add features that step outside
150 // of the linear versioning stream. Leave the 'spare' fields alone unless you need
151 // something extraordinarily weird - they're meant to be the final escape when everything
152 // else fails.
153 // As you create new versions, consider moving the compatibilityLimit out to open up
154 // new room for backward compatibility.
155 // To break backward compatibility intentionally, move currentVersion beyond the
156 // old compatibilityLimit (and move compatibilityLimit further out).
157 //
158 class CodeDirectory: public Blob<CodeDirectory, 0xfade0c02> {
159 typedef SHA1 Hash;
160 public:
161 Endian<uint32_t> version; // compatibility version
162 Endian<uint32_t> flags; // setup and mode flags
163 Endian<uint32_t> hashOffset; // offset of hash slot element at index zero
164 Endian<uint32_t> identOffset; // offset of identifier string
165 Endian<uint32_t> nSpecialSlots; // number of special hash slots
166 Endian<uint32_t> nCodeSlots; // number of ordinary (code) hash slots
167 Endian<uint32_t> codeLimit; // limit to main image signature range
168 uint8_t hashSize; // size of each hash in bytes
169 uint8_t hashType; // type of hash (cdHashType* constants)
170 uint8_t spare1; // unused (must be zero)
171 uint8_t pageSize; // log2(page size in bytes); 0 => infinite
172 Endian<uint32_t> spare2; // unused (must be zero)
173
174 // works with the version field; see comments above
175 static const uint32_t currentVersion = 0x20001; // "version 2"
176 static const uint32_t compatibilityLimit = 0x2F000; // "version 3 with wiggle room"
177
178 void checkVersion() const; // throws if not compatible with this code
179
180 typedef int Slot; // slot index (negative for special slots)
181 typedef unsigned int SpecialSlot; // positive special slot index (not for code slots)
182
183 const char *identifier() const { return at<const char>(identOffset); }
184 char *identifier() { return at<char>(identOffset); }
185
186 unsigned char *operator [] (Slot slot)
187 {
188 assert(slot >= int(-nSpecialSlots) && slot < int(nCodeSlots));
189 return at<unsigned char>(hashOffset) + hashSize * slot;
190 }
191
192 const unsigned char *operator [] (Slot slot) const
193 {
194 assert(slot >= int(-nSpecialSlots) && slot < int(nCodeSlots));
195 return at<unsigned char>(hashOffset) + hashSize * slot;
196 }
197
198 bool validateSlot(const void *data, size_t size, Slot slot) const;
199 bool validateSlot(UnixPlusPlus::FileDesc fd, size_t size, Slot slot) const;
200 bool slotIsPresent(Slot slot) const;
201
202 class Builder;
203
204 protected:
205 static size_t hash(UnixPlusPlus::FileDesc fd, Hash::Byte *digest, size_t limit = 0);
206 static size_t hash(const void *data, size_t length, Hash::Byte *digest);
207
208 public:
209 //
210 // Information about SpecialSlots
211 //
212 static const char *canonicalSlotName(SpecialSlot slot);
213 static unsigned slotAttributes(SpecialSlot slot);
214 IFDEBUG(static const char * const debugSlotName[]);
215
216 public:
217 //
218 // Canonical text forms for (only) the user-settable flags
219 //
220 struct FlagItem {
221 const char *name;
222 uint32_t value;
223 bool external;
224 };
225 static const FlagItem flagItems[]; // terminated with NULL item
226
227 static uint32_t textFlags(std::string text);
228 };
229
230
231 } // CodeSigning
232 } // Security
233
234
235 #endif //_H_CODEDIRECTORY