]>
Commit | Line | Data |
---|---|---|
7d31e928 A |
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 | // reqmaker - Requirement assembler | |
26 | // | |
27 | #include "reqmaker.h" | |
28 | ||
29 | namespace Security { | |
30 | namespace CodeSigning { | |
31 | ||
32 | ||
33 | // | |
34 | // Requirement::Makers | |
35 | // | |
36 | Requirement::Maker::Maker(Kind k) | |
37 | : mSize(1024) | |
38 | { | |
39 | mBuffer = (Requirement *)malloc(mSize); | |
40 | mBuffer->initialize(); | |
41 | mBuffer->kind(k); | |
42 | mPC = sizeof(Requirement); | |
43 | } | |
44 | ||
45 | // need at least (size) bytes in the creation buffer | |
46 | void Requirement::Maker::require(size_t size) | |
47 | { | |
48 | if (mPC + size > mSize) { | |
49 | mSize *= 2; | |
50 | if (mPC + size > mSize) | |
51 | mSize = mPC + size; | |
52 | if (!(mBuffer = (Requirement *)realloc(mBuffer, mSize))) | |
53 | UnixError::throwMe(ENOMEM); | |
54 | } | |
55 | } | |
56 | ||
57 | // allocate (size) bytes at end of buffer and return pointer to that | |
58 | void *Requirement::Maker::alloc(size_t size) | |
59 | { | |
60 | // round size up to preserve alignment | |
61 | size_t usedSize = LowLevelMemoryUtilities::alignUp(size, baseAlignment); | |
62 | require(usedSize); | |
63 | void *data = mBuffer->at<void>(mPC); | |
64 | mPC += usedSize; | |
65 | ||
66 | // clear any padding (avoid random bytes in code image) | |
67 | const uint32_t zero = 0; | |
68 | memcpy(mBuffer->at<void>(mPC - usedSize + size), &zero, usedSize - size); | |
69 | ||
70 | // all done | |
71 | return data; | |
72 | } | |
73 | ||
74 | // put contiguous data blob | |
75 | void Requirement::Maker::putData(const void *data, size_t length) | |
76 | { | |
77 | put(uint32_t(length)); | |
78 | memcpy(alloc(length), data, length); | |
79 | } | |
80 | ||
81 | // Specialized Maker put operations | |
82 | void Requirement::Maker::anchor() | |
83 | { | |
84 | put(opAppleAnchor); | |
85 | } | |
86 | ||
87 | void Requirement::Maker::anchor(int slot, SHA1::Digest digest) | |
88 | { | |
89 | put(opAnchorHash); | |
90 | put(slot); | |
91 | putData(digest, SHA1::digestLength); | |
92 | } | |
93 | ||
94 | void Requirement::Maker::anchor(int slot, const void *cert, size_t length) | |
95 | { | |
96 | SHA1 hasher; | |
97 | hasher(cert, length); | |
98 | SHA1::Digest digest; | |
99 | hasher.finish(digest); | |
100 | anchor(slot, digest); | |
101 | } | |
102 | ||
103 | void Requirement::Maker::trustedAnchor() | |
104 | { | |
105 | put(opTrustedCerts); | |
106 | } | |
107 | ||
108 | void Requirement::Maker::trustedAnchor(int slot) | |
109 | { | |
110 | put(opTrustedCert); | |
111 | put(slot); | |
112 | } | |
113 | ||
114 | void Requirement::Maker::infoKey(const string &key, const string &value) | |
115 | { | |
116 | put(opInfoKeyValue); | |
117 | put(key); | |
118 | put(value); | |
119 | } | |
120 | ||
121 | void Requirement::Maker::ident(const string &identifier) | |
122 | { | |
123 | put(opIdent); | |
124 | put(identifier); | |
125 | } | |
126 | ||
127 | void Requirement::Maker::cdhash(SHA1::Digest digest) | |
128 | { | |
129 | put(opCDHash); | |
130 | putData(digest, SHA1::digestLength); | |
131 | } | |
132 | ||
133 | ||
134 | void *Requirement::Maker::insert(const Label &label, size_t length) | |
135 | { | |
136 | require(length); | |
137 | memmove(mBuffer->at<void>(label.pos + length), | |
138 | mBuffer->at<void>(label.pos), mPC - label.pos); | |
139 | mPC += length; | |
140 | return mBuffer->at<void>(label.pos); | |
141 | } | |
142 | ||
143 | ||
144 | Requirement *Requirement::Maker::make() | |
145 | { | |
146 | mBuffer->length(mPC); | |
147 | Requirement *result = mBuffer; | |
148 | mBuffer = NULL; | |
149 | return result; | |
150 | } | |
151 | ||
152 | ||
153 | } // CodeSigning | |
154 | } // Security |