]>
git.saurik.com Git - apple/security.git/blob - OSX/libsecurity_codesigning/lib/reqmaker.cpp
2 * Copyright (c) 2006,2011-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 // reqmaker - Requirement assembler
30 namespace CodeSigning
{
34 // Requirement::Makers
36 Requirement::Maker::Maker(Kind k
)
39 mBuffer
= (Requirement
*)malloc(mSize
);
40 mBuffer
->initialize();
42 mPC
= sizeof(Requirement
);
45 // need at least (size) bytes in the creation buffer
46 void Requirement::Maker::require(size_t size
)
48 if (mPC
+ size
> mSize
) {
50 if (mPC
+ size
> mSize
)
51 mSize
= (Offset
)(mPC
+ size
);
52 if (!(mBuffer
= (Requirement
*)realloc(mBuffer
, mSize
)))
53 UnixError::throwMe(ENOMEM
);
57 // allocate (size) bytes at end of buffer and return pointer to that
58 void *Requirement::Maker::alloc(size_t size
)
60 // round size up to preserve alignment
61 size_t usedSize
= LowLevelMemoryUtilities::alignUp(size
, baseAlignment
);
63 void *data
= mBuffer
->at
<void>(mPC
);
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
);
74 // put contiguous data blob
75 void Requirement::Maker::putData(const void *data
, size_t length
)
77 put(uint32_t(length
));
78 memcpy(alloc(length
), data
, length
);
81 // Specialized Maker put operations
82 void Requirement::Maker::anchor()
87 void Requirement::Maker::anchorGeneric()
89 put(opAppleGenericAnchor
);
92 void Requirement::Maker::anchor(int slot
, SHA1::Digest digest
)
96 putData(digest
, SHA1::digestLength
);
99 void Requirement::Maker::anchor(int slot
, const void *cert
, size_t length
)
102 hasher(cert
, length
);
104 hasher
.finish(digest
);
105 anchor(slot
, digest
);
108 void Requirement::Maker::trustedAnchor()
113 void Requirement::Maker::trustedAnchor(int slot
)
119 void Requirement::Maker::infoKey(const string
&key
, const string
&value
)
126 void Requirement::Maker::ident(const string
&identifier
)
132 void Requirement::Maker::cdhash(SHA1::Digest digest
)
135 putData(digest
, SHA1::digestLength
);
138 void Requirement::Maker::cdhash(CFDataRef digest
)
141 putData(CFDataGetBytePtr(digest
), CFDataGetLength(digest
));
144 void Requirement::Maker::platform(int platformIdentifier
)
147 put(platformIdentifier
);
151 void Requirement::Maker::copy(const Requirement
*req
)
154 if (req
->kind() != exprForm
) // don't know how to embed this
155 MacOSError::throwMe(errSecCSReqUnsupported
);
156 this->copy(req
->at
<const void>(sizeof(Requirement
)), req
->length() - sizeof(Requirement
));
160 void *Requirement::Maker::insert(const Label
&label
, size_t length
)
163 memmove(mBuffer
->at
<void>(label
.pos
+ length
),
164 mBuffer
->at
<void>(label
.pos
), mPC
- label
.pos
);
166 return mBuffer
->at
<void>(label
.pos
);
170 Requirement
*Requirement::Maker::make()
172 mBuffer
->length(mPC
);
173 Requirement
*result
= mBuffer
;