]> git.saurik.com Git - apple/security.git/blob - OSX/libsecurity_codesigning/lib/SecRequirement.cpp
Security-58286.200.222.tar.gz
[apple/security.git] / OSX / libsecurity_codesigning / lib / SecRequirement.cpp
1 /*
2 * Copyright (c) 2006,2011-2012,2014 Apple 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 // SecRequirement - API frame for SecRequirement objects
26 //
27 #include "cs.h"
28 #include "Requirements.h"
29 #include "reqparser.h"
30 #include "reqmaker.h"
31 #include "reqdumper.h"
32 #include <Security/SecCertificate.h>
33 #include <security_utilities/cfutilities.h>
34
35 using namespace CodeSigning;
36
37
38 //
39 // CF-standard type code function
40 //
41 CFTypeID SecRequirementGetTypeID(void)
42 {
43 BEGIN_CSAPI
44 return gCFObjects().Requirement.typeID;
45 END_CSAPI1(_kCFRuntimeNotATypeID)
46 }
47
48
49 //
50 // Create a Requirement from data
51 //
52 OSStatus SecRequirementCreateWithData(CFDataRef data, SecCSFlags flags,
53 SecRequirementRef *requirementRef)
54 {
55 BEGIN_CSAPI
56
57 checkFlags(flags);
58 CodeSigning::Required(requirementRef) = (new SecRequirement(CFDataGetBytePtr(data), CFDataGetLength(data)))->handle();
59
60 END_CSAPI
61 }
62
63
64 //
65 // Create a Requirement from data in a file
66 //
67 OSStatus SecRequirementCreateWithResource(CFURLRef resource, SecCSFlags flags,
68 SecRequirementRef *requirementRef)
69 {
70 BEGIN_CSAPI
71
72 checkFlags(flags);
73 CFRef<CFDataRef> data = cfLoadFile(resource);
74 CodeSigning::Required(requirementRef) =
75 (new SecRequirement(CFDataGetBytePtr(data), CFDataGetLength(data)))->handle();
76
77 END_CSAPI
78 }
79
80
81 //
82 // Create a Requirement from source text (compiling it)
83 //
84 OSStatus SecRequirementCreateWithString(CFStringRef text, SecCSFlags flags,
85 SecRequirementRef *requirementRef)
86 {
87 return SecRequirementCreateWithStringAndErrors(text, flags, NULL, requirementRef);
88 }
89
90 OSStatus SecRequirementCreateWithStringAndErrors(CFStringRef text, SecCSFlags flags,
91 CFErrorRef *errors, SecRequirementRef *requirementRef)
92 {
93 BEGIN_CSAPI
94
95 checkFlags(flags);
96 CodeSigning::Required(requirementRef) = (new SecRequirement(parseRequirement(cfString(text)), true))->handle();
97
98 END_CSAPI_ERRORS
99 }
100
101
102 //
103 // Create a Requirement group.
104 // This is the canonical point where "application group" is defined.
105 //
106 OSStatus SecRequirementCreateGroup(CFStringRef groupName, SecCertificateRef anchorRef,
107 SecCSFlags flags, SecRequirementRef *requirementRef)
108 {
109 BEGIN_CSAPI
110
111 checkFlags(flags);
112 Requirement::Maker maker;
113 maker.put(opAnd); // both of...
114 maker.infoKey("Application-Group", cfString(groupName));
115 if (anchorRef) {
116 #if TARGET_OS_OSX
117 CSSM_DATA certData;
118 MacOSError::check(SecCertificateGetData(anchorRef, &certData));
119 maker.anchor(0, certData.Data, certData.Length);
120 #else
121 maker.anchor(0, SecCertificateGetBytePtr(anchorRef), SecCertificateGetLength(anchorRef));
122 #endif
123 } else {
124 maker.anchor(); // canonical Apple anchor
125 }
126 CodeSigning::Required(requirementRef) = (new SecRequirement(maker.make(), true))->handle();
127
128 END_CSAPI
129 }
130
131
132 //
133 // Extract the stable binary from from a SecRequirementRef
134 //
135 OSStatus SecRequirementCopyData(SecRequirementRef requirementRef, SecCSFlags flags,
136 CFDataRef *data)
137 {
138 BEGIN_CSAPI
139
140 const Requirement *req = SecRequirement::required(requirementRef)->requirement();
141 checkFlags(flags);
142 CodeSigning::Required(data);
143 *data = makeCFData(*req);
144
145 END_CSAPI
146 }
147
148
149 //
150 // Generate source form for a SecRequirement (decompile/disassemble)
151 //
152 OSStatus SecRequirementCopyString(SecRequirementRef requirementRef, SecCSFlags flags,
153 CFStringRef *text)
154 {
155 BEGIN_CSAPI
156
157 const Requirement *req = SecRequirement::required(requirementRef)->requirement();
158 checkFlags(flags);
159 CodeSigning::Required(text);
160 *text = makeCFString(Dumper::dump(req));
161
162 END_CSAPI
163 }
164
165
166 //
167 CFStringRef kSecRequirementKeyInfoPlist = CFSTR("requirement:eval:info");
168 CFStringRef kSecRequirementKeyEntitlements = CFSTR("requirement:eval:entitlements");
169 CFStringRef kSecRequirementKeyIdentifier = CFSTR("requirement:eval:identifier");
170 CFStringRef kSecRequirementKeyPackageChecksum = CFSTR("requirement:eval:package_checksum");
171 CFStringRef kSecRequirementKeyChecksumAlgorithm = CFSTR("requirement:eval:package_checksum_algorithm");
172
173 OSStatus SecRequirementEvaluate(SecRequirementRef requirementRef,
174 CFArrayRef certificateChain, CFDictionaryRef context,
175 SecCSFlags flags)
176 {
177 BEGIN_CSAPI
178
179 const Requirement *req = SecRequirement::required(requirementRef)->requirement();
180 checkFlags(flags);
181 CodeSigning::Required(certificateChain);
182
183 SecCSDigestAlgorithm checksumAlgorithm = kSecCodeSignatureNoHash;
184 if (context) {
185 CFRef<CFNumberRef> num = (CFNumberRef)CFDictionaryGetValue(context, kSecRequirementKeyChecksumAlgorithm);
186 if (num) {
187 checksumAlgorithm = (SecCSDigestAlgorithm)cfNumber<uint32_t>(num);
188 }
189 }
190
191 Requirement::Context ctx(certificateChain, // mandatory
192 context ? CFDictionaryRef(CFDictionaryGetValue(context, kSecRequirementKeyInfoPlist)) : NULL,
193 context ? CFDictionaryRef(CFDictionaryGetValue(context, kSecRequirementKeyEntitlements)) : NULL,
194 (context && CFDictionaryGetValue(context, kSecRequirementKeyIdentifier)) ?
195 cfString(CFStringRef(CFDictionaryGetValue(context, kSecRequirementKeyIdentifier))) : "",
196 NULL, // can't specify a CodeDirectory here
197 context ? CFDataRef(CFDictionaryGetValue(context, kSecRequirementKeyPackageChecksum)) : NULL,
198 checksumAlgorithm,
199 false // can't get forced platform this way
200 );
201 req->validate(ctx);
202
203 END_CSAPI
204 }
205
206
207 //
208 // Assemble a requirement set (as a CFData) from a dictionary of requirement objects.
209 // An empty set is allowed.
210 //
211 OSStatus SecRequirementsCreateFromRequirements(CFDictionaryRef requirements, SecCSFlags flags,
212 CFDataRef *requirementSet)
213 {
214 BEGIN_CSAPI
215
216 checkFlags(flags);
217 if (requirements == NULL)
218 return errSecCSObjectRequired;
219 CFIndex count = CFDictionaryGetCount(requirements);
220 CFNumberRef keys[count];
221 SecRequirementRef reqs[count];
222 CFDictionaryGetKeysAndValues(requirements, (const void **)keys, (const void **)reqs);
223 Requirements::Maker maker;
224 for (CFIndex n = 0; n < count; n++) {
225 const Requirement *req = SecRequirement::required(reqs[n])->requirement();
226 maker.add(cfNumber<Requirements::Type>(keys[n]), req->clone());
227 }
228 Requirements *reqset = maker.make(); // malloc'ed
229 CodeSigning::Required(requirementSet) = makeCFDataMalloc(*reqset); // takes ownership of reqs
230
231 END_CSAPI
232 }
233
234
235 //
236 // Break a requirement set (given as a CFData) into its constituent requirements
237 // and return it as a CFDictionary.
238 //
239 OSStatus SecRequirementsCopyRequirements(CFDataRef requirementSet, SecCSFlags flags,
240 CFDictionaryRef *requirements)
241 {
242 BEGIN_CSAPI
243
244 checkFlags(flags);
245 if (requirementSet == NULL)
246 return errSecCSObjectRequired;
247 const Requirements *reqs = (const Requirements *)CFDataGetBytePtr(requirementSet);
248 if (!reqs->validateBlob())
249 MacOSError::throwMe(errSecCSReqInvalid);
250 CFRef<CFMutableDictionaryRef> dict = makeCFMutableDictionary();
251 unsigned count = reqs->count();
252 for (unsigned n = 0; n < count; n++) {
253 CFRef<SecRequirementRef> req = (new SecRequirement(reqs->blob<Requirement>(n)))->handle();
254 CFDictionaryAddValue(dict, CFTempNumber(reqs->type(n)), req);
255 }
256 CodeSigning::Required(requirements) = dict.yield();
257
258 END_CSAPI
259 }
260
261
262 //
263 // Generically parse a string as some kind of requirement-related source form.
264 // If properly recognized, return the result as a CF object:
265 // SecRequirementRef for a single requirement
266 // CFDataRef for a requirement set
267 //
268 OSStatus SecRequirementsCreateWithString(CFStringRef text, SecCSFlags flags,
269 CFTypeRef *result, CFErrorRef *errors)
270 {
271 BEGIN_CSAPI
272
273 checkFlags(flags, kSecCSParseRequirement | kSecCSParseRequirementSet);
274 if (text == NULL || result == NULL)
275 return errSecCSObjectRequired;
276 std::string s = cfString(text);
277 switch (flags & (kSecCSParseRequirement | kSecCSParseRequirementSet)) {
278 case kSecCSParseRequirement: // single only
279 *result = (new SecRequirement(parseRequirement(s), true))->handle();
280 break;
281 case kSecCSParseRequirementSet: // single only
282 {
283 const Requirements *reqs = parseRequirements(s);
284 *result = makeCFDataMalloc(*reqs);
285 break;
286 }
287 case 0:
288 case kSecCSParseRequirement | kSecCSParseRequirementSet:
289 {
290 const BlobCore *any = parseGeneric(s);
291 if (any->is<Requirement>())
292 *result = (new SecRequirement(Requirement::specific(any), true))->handle();
293 else
294 *result = makeCFDataMalloc(*any);
295 break;
296 }
297 }
298
299 END_CSAPI_ERRORS
300 }
301
302
303 //
304 // Convert a SecRequirementRef or a CFDataRef containing a requirement set to text.
305 // Requirement sets will be formatted as multiple lines (one per requirement). They can be empty.
306 // A single requirement will return a single line that is NOT newline-terminated.
307 //
308 OSStatus SecRequirementsCopyString(CFTypeRef input, SecCSFlags flags, CFStringRef *text)
309 {
310 BEGIN_CSAPI
311
312 checkFlags(flags);
313 if (input == NULL)
314 return errSecCSObjectRequired;
315 if (CFGetTypeID(input) == SecRequirementGetTypeID()) {
316 return SecRequirementCopyString(SecRequirementRef(input), flags, text);
317 } else if (CFGetTypeID(input) == CFDataGetTypeID()) {
318 const Requirements *reqs = (const Requirements *)CFDataGetBytePtr(CFDataRef(input));
319 if (!reqs->validateBlob(CFDataGetLength(CFDataRef(input))))
320 return errSecCSReqInvalid;
321 CodeSigning::Required(text) = makeCFString(Dumper::dump(reqs, false));
322 } else
323 return errSecCSInvalidObjectRef;
324
325 END_CSAPI
326 }