]> git.saurik.com Git - apple/libsecurity_codesigning.git/blob - lib/reqmaker.cpp
libsecurity_codesigning-55037.15.tar.gz
[apple/libsecurity_codesigning.git] / lib / reqmaker.cpp
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::anchorGeneric()
88 {
89 put(opAppleGenericAnchor);
90 }
91
92 void Requirement::Maker::anchor(int slot, SHA1::Digest digest)
93 {
94 put(opAnchorHash);
95 put(slot);
96 putData(digest, SHA1::digestLength);
97 }
98
99 void Requirement::Maker::anchor(int slot, const void *cert, size_t length)
100 {
101 SHA1 hasher;
102 hasher(cert, length);
103 SHA1::Digest digest;
104 hasher.finish(digest);
105 anchor(slot, digest);
106 }
107
108 void Requirement::Maker::trustedAnchor()
109 {
110 put(opTrustedCerts);
111 }
112
113 void Requirement::Maker::trustedAnchor(int slot)
114 {
115 put(opTrustedCert);
116 put(slot);
117 }
118
119 void Requirement::Maker::infoKey(const string &key, const string &value)
120 {
121 put(opInfoKeyValue);
122 put(key);
123 put(value);
124 }
125
126 void Requirement::Maker::ident(const string &identifier)
127 {
128 put(opIdent);
129 put(identifier);
130 }
131
132 void Requirement::Maker::cdhash(SHA1::Digest digest)
133 {
134 put(opCDHash);
135 putData(digest, SHA1::digestLength);
136 }
137
138
139
140 void Requirement::Maker::copy(const Requirement *req)
141 {
142 assert(req);
143 if (req->kind() != exprForm) // don't know how to embed this
144 MacOSError::throwMe(errSecCSReqUnsupported);
145 this->copy(req->at<const void>(sizeof(Requirement)), req->length() - sizeof(Requirement));
146 }
147
148
149 void *Requirement::Maker::insert(const Label &label, size_t length)
150 {
151 require(length);
152 memmove(mBuffer->at<void>(label.pos + length),
153 mBuffer->at<void>(label.pos), mPC - label.pos);
154 mPC += length;
155 return mBuffer->at<void>(label.pos);
156 }
157
158
159 Requirement *Requirement::Maker::make()
160 {
161 mBuffer->length(mPC);
162 Requirement *result = mBuffer;
163 mBuffer = NULL;
164 return result;
165 }
166
167
168 } // CodeSigning
169 } // Security