]> git.saurik.com Git - apple/security.git/blob - OSX/sec/Security/SecAccessControl.c
Security-57336.10.29.tar.gz
[apple/security.git] / OSX / sec / Security / SecAccessControl.c
1 /*
2 * Copyright (c) 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 * SecAccessControl.c - CoreFoundation based access control object
26 */
27
28 #include <TargetConditionals.h>
29 #include <AssertMacros.h>
30 #include <Security/SecAccessControl.h>
31 #include <Security/SecAccessControlPriv.h>
32 #include <Security/SecItem.h>
33 #include <utilities/SecCFWrappers.h>
34 #include <utilities/SecCFError.h>
35 #include <utilities/der_plist.h>
36 #include <libaks_acl_cf_keys.h>
37 #include <ACMDefs.h>
38 #include <ACMAclDefs.h>
39
40 static CFTypeRef kSecAccessControlKeyProtection = CFSTR("prot");
41 static CFTypeRef kSecAccessControlKeyBound = CFSTR("bound");
42
43 struct __SecAccessControl {
44 CFRuntimeBase _base;
45 CFMutableDictionaryRef dict;
46 };
47
48 static SecAccessConstraintRef SecAccessConstraintCreateValueOfKofN(CFAllocatorRef allocator, size_t numRequired, CFArrayRef constraints, CFErrorRef *error);
49
50 static CFStringRef SecAccessControlCopyFormatDescription(CFTypeRef cf, CFDictionaryRef formatOptions) {
51 SecAccessControlRef access_control = (SecAccessControlRef)cf;
52 return CFStringCreateWithFormat(kCFAllocatorDefault, NULL, CFSTR("<SecAccessControlRef: %p>"), access_control);
53 }
54
55 static Boolean SecAccessControlCompare(CFTypeRef lhs, CFTypeRef rhs) {
56 SecAccessControlRef laccess_control = (SecAccessControlRef)lhs;
57 SecAccessControlRef raccess_control = (SecAccessControlRef)rhs;
58 return (laccess_control == raccess_control) || CFEqual(laccess_control->dict, raccess_control->dict);
59 }
60
61 static void SecAccessControlDestroy(CFTypeRef cf) {
62 SecAccessControlRef access_control = (SecAccessControlRef)cf;
63 CFReleaseSafe(access_control->dict);
64 }
65
66 CFGiblisWithCompareFor(SecAccessControl);
67
68 static CFMutableDictionaryRef SecAccessControlGetMutableConstraints(SecAccessControlRef access_control) {
69 CFMutableDictionaryRef constraints = (CFMutableDictionaryRef)CFDictionaryGetValue(access_control->dict, kAKSKeyAcl);
70
71 if (!constraints) {
72 CFMutableDictionaryRef newConstraints = CFDictionaryCreateMutableForCFTypes(CFGetAllocator(access_control));
73 CFDictionarySetValue(access_control->dict, kAKSKeyAcl, newConstraints);
74 CFRelease(newConstraints);
75
76 constraints = (CFMutableDictionaryRef)CFDictionaryGetValue(access_control->dict, kAKSKeyAcl);
77 }
78
79 return constraints;
80 }
81
82 SecAccessControlRef SecAccessControlCreate(CFAllocatorRef allocator, CFErrorRef *error) {
83 SecAccessControlRef access_control = CFTypeAllocate(SecAccessControl, struct __SecAccessControl, allocator);
84 if (!access_control) {
85 SecError(errSecAllocate, error, CFSTR("allocate memory for SecAccessControl"));
86 return NULL;
87 }
88
89 access_control->dict = CFDictionaryCreateMutableForCFTypes(allocator);
90 return access_control;
91 }
92
93 #if TARGET_OS_EMBEDDED || TARGET_OS_IPHONE
94 static CFDataRef _getEmptyData() {
95 static CFMutableDataRef emptyData = NULL;
96 static dispatch_once_t onceToken;
97
98 dispatch_once(&onceToken, ^{
99 emptyData = CFDataCreateMutable(kCFAllocatorDefault, 0);
100 });
101
102 return emptyData;
103 }
104 #endif
105
106 SecAccessControlRef SecAccessControlCreateWithFlags(CFAllocatorRef allocator, CFTypeRef protection,
107 SecAccessControlCreateFlags flags, CFErrorRef *error) {
108 SecAccessControlRef access_control = NULL;
109 CFTypeRef constraint = NULL;
110 CFMutableArrayRef constraints = NULL;
111
112 require_quiet(access_control = SecAccessControlCreate(allocator, error), errOut);
113
114 if (!SecAccessControlSetProtection(access_control, protection, error))
115 goto errOut;
116
117 if (flags) {
118 #if TARGET_OS_EMBEDDED || TARGET_OS_IPHONE
119 bool or = (flags & kSecAccessControlOr) ? true : false;
120 bool and = (flags & kSecAccessControlAnd) ? true : false;
121
122 if (or && and) {
123 SecError(errSecParam, error, CFSTR("only one logical operation can be set"));
124 goto errOut;
125 }
126
127 SecAccessControlCreateFlags maskedFlags = flags & (kSecAccessControlTouchIDAny | kSecAccessControlTouchIDCurrentSet);
128 if (maskedFlags && maskedFlags != kSecAccessControlTouchIDAny && maskedFlags != kSecAccessControlTouchIDCurrentSet) {
129 SecError(errSecParam, error, CFSTR("only one bio constraint can be set"));
130 goto errOut;
131 }
132
133 if (flags & kSecAccessControlUserPresence && flags & ~(kSecAccessControlUserPresence | kSecAccessControlApplicationPassword | kSecAccessControlPrivateKeyUsage)) {
134 #else
135 if (flags & kSecAccessControlUserPresence && flags != kSecAccessControlUserPresence) {
136 #endif
137 SecError(errSecParam, error, CFSTR("kSecAccessControlUserPresence can be combined only with kSecAccessControlApplicationPassword and kSecAccessControlPrivateKeyUsage"));
138 goto errOut;
139 }
140
141 constraints = CFArrayCreateMutable(allocator, 0, &kCFTypeArrayCallBacks);
142
143 if (flags & kSecAccessControlUserPresence) {
144 require_quiet(constraint = SecAccessConstraintCreatePolicy(allocator, CFSTR(kACMPolicyDeviceOwnerAuthentication), error), errOut);
145 CFArrayAppendValue(constraints, constraint);
146 CFReleaseNull(constraint);
147 }
148
149 if (flags & kSecAccessControlDevicePasscode) {
150 require_quiet(constraint = SecAccessConstraintCreatePasscode(allocator), errOut);
151 CFArrayAppendValue(constraints, constraint);
152 CFReleaseNull(constraint);
153 }
154
155 #if TARGET_OS_EMBEDDED || TARGET_OS_IPHONE
156 if (flags & kSecAccessControlTouchIDAny) {
157 require_quiet(constraint = SecAccessConstraintCreateTouchIDAny(allocator, _getEmptyData()), errOut);
158 CFArrayAppendValue(constraints, constraint);
159 CFReleaseNull(constraint);
160 }
161
162 if (flags & kSecAccessControlTouchIDCurrentSet) {
163 require_quiet(constraint = SecAccessConstraintCreateTouchIDCurrentSet(allocator, _getEmptyData(), _getEmptyData()), errOut);
164 CFArrayAppendValue(constraints, constraint);
165 CFReleaseNull(constraint);
166 }
167
168 if (flags & kSecAccessControlApplicationPassword) {
169 SecAccessControlSetRequirePassword(access_control, true);
170 }
171 #endif
172 CFIndex constraints_count = CFArrayGetCount(constraints);
173 #if TARGET_OS_EMBEDDED || TARGET_OS_IPHONE
174 if (constraints_count > 1) {
175 require_quiet(constraint = SecAccessConstraintCreateValueOfKofN(allocator, or?1:constraints_count, constraints, error), errOut);
176 if (flags & kSecAccessControlPrivateKeyUsage) {
177 require_quiet(SecAccessControlAddConstraintForOperation(access_control, kAKSKeyOpSign, constraint, error), errOut);
178 }
179 else {
180 require_quiet(SecAccessControlAddConstraintForOperation(access_control, kAKSKeyOpDecrypt, constraint, error), errOut);
181 require_quiet(SecAccessControlAddConstraintForOperation(access_control, kAKSKeyOpEncrypt, kCFBooleanTrue, error), errOut);
182 }
183 require_quiet(SecAccessControlAddConstraintForOperation(access_control, kAKSKeyOpDelete, kCFBooleanTrue, error), errOut);
184 CFReleaseNull(constraint);
185 } else
186 #endif
187 if (constraints_count == 1) {
188 #if TARGET_OS_EMBEDDED || TARGET_OS_IPHONE
189 if (flags & kSecAccessControlPrivateKeyUsage) {
190 require_quiet(SecAccessControlAddConstraintForOperation(access_control, kAKSKeyOpSign, CFArrayGetValueAtIndex(constraints, 0), error), errOut);
191 }
192 else {
193 #endif
194 require_quiet(SecAccessControlAddConstraintForOperation(access_control, kAKSKeyOpDecrypt, CFArrayGetValueAtIndex(constraints, 0), error), errOut);
195 require_quiet(SecAccessControlAddConstraintForOperation(access_control, kAKSKeyOpEncrypt, kCFBooleanTrue, error), errOut);
196 #if TARGET_OS_EMBEDDED || TARGET_OS_IPHONE
197 }
198 #endif
199 require_quiet(SecAccessControlAddConstraintForOperation(access_control, kAKSKeyOpDelete, kCFBooleanTrue, error), errOut);
200 } else {
201 #if TARGET_OS_EMBEDDED || TARGET_OS_IPHONE
202 if (flags & kSecAccessControlPrivateKeyUsage) {
203 require_quiet(SecAccessControlAddConstraintForOperation(access_control, kAKSKeyOpSign, kCFBooleanTrue, error), errOut);
204 require_quiet(SecAccessControlAddConstraintForOperation(access_control, kAKSKeyOpDelete, kCFBooleanTrue, error), errOut);
205 }
206 else {
207 #endif
208 require_quiet(SecAccessControlAddConstraintForOperation(access_control, kAKSKeyOpDefaultAcl, kCFBooleanTrue, error), errOut);
209 #if TARGET_OS_EMBEDDED || TARGET_OS_IPHONE
210 }
211 #endif
212 }
213
214 CFReleaseNull(constraints);
215 }
216 else {
217 require_quiet(SecAccessControlAddConstraintForOperation(access_control, kAKSKeyOpDefaultAcl, kCFBooleanTrue, error), errOut);
218 }
219
220 return access_control;
221
222 errOut:
223 CFReleaseSafe(access_control);
224 CFReleaseSafe(constraints);
225 CFReleaseSafe(constraint);
226 return NULL;
227 }
228
229 CFTypeRef SecAccessControlGetProtection(SecAccessControlRef access_control) {
230 return CFDictionaryGetValue(access_control->dict, kSecAccessControlKeyProtection);
231 }
232
233 static bool checkItemInArray(CFTypeRef item, const CFTypeRef *values, CFIndex count, CFStringRef errMessage, CFErrorRef *error) {
234 for (CFIndex i = 0; i < count; i++) {
235 if (CFEqualSafe(item, values[i])) {
236 return true;
237 }
238 }
239 return SecError(errSecParam, error, errMessage, item);
240 }
241
242 #define CheckItemInArray(item, values, msg) \
243 { \
244 const CFTypeRef vals[] = values; \
245 if (!checkItemInArray(item, vals, sizeof(vals)/sizeof(*vals), CFSTR(msg), error)) { \
246 return false; \
247 } \
248 }
249
250 #define ItemArray(...) { __VA_ARGS__ }
251
252
253 bool SecAccessControlSetProtection(SecAccessControlRef access_control, CFTypeRef protection, CFErrorRef *error) {
254 // Verify protection type.
255 CheckItemInArray(protection, ItemArray(kSecAttrAccessibleAlways, kSecAttrAccessibleAfterFirstUnlock,
256 kSecAttrAccessibleWhenUnlocked, kSecAttrAccessibleAlwaysThisDeviceOnly,
257 kSecAttrAccessibleAfterFirstUnlockThisDeviceOnly,
258 kSecAttrAccessibleWhenUnlockedThisDeviceOnly,
259 kSecAttrAccessibleWhenPasscodeSetThisDeviceOnly),
260 "SecAccessControl: invalid protection %@");
261
262 // Protection valid, use it.
263 CFDictionarySetValue(access_control->dict, kSecAccessControlKeyProtection, protection);
264 return true;
265 }
266
267 SecAccessConstraintRef SecAccessConstraintCreatePolicy(CFAllocatorRef allocator, CFTypeRef policy, CFErrorRef *error) {
268 return CFDictionaryCreateMutableForCFTypesWith(allocator, CFSTR(kACMKeyAclConstraintPolicy), policy, NULL);
269 }
270
271 SecAccessConstraintRef SecAccessConstraintCreatePasscode(CFAllocatorRef allocator) {
272 return CFDictionaryCreateMutableForCFTypesWith(allocator, CFSTR(kACMKeyAclConstraintUserPasscode), kCFBooleanTrue, NULL);
273 }
274
275 SecAccessConstraintRef SecAccessConstraintCreateTouchIDAny(CFAllocatorRef allocator, CFDataRef catacombUUID) {
276 CFMutableDictionaryRef bioDict = CFDictionaryCreateMutableForCFTypesWith(allocator, CFSTR(kACMKeyAclParamBioCatacombUUID), catacombUUID, NULL);
277 SecAccessConstraintRef constraint = CFDictionaryCreateMutableForCFTypesWith(allocator, CFSTR(kACMKeyAclConstraintBio), bioDict, NULL);
278 CFReleaseSafe(bioDict);
279 return constraint;
280 }
281
282 SecAccessConstraintRef SecAccessConstraintCreateTouchIDCurrentSet(CFAllocatorRef allocator, CFDataRef catacombUUID, CFDataRef bioDbHash) {
283 CFMutableDictionaryRef bioDict = CFDictionaryCreateMutableForCFTypesWith(allocator, CFSTR(kACMKeyAclParamBioCatacombUUID), catacombUUID, NULL);
284 CFDictionarySetValue(bioDict, CFSTR(kACMKeyAclParamBioDatabaseHash), bioDbHash);
285 SecAccessConstraintRef constraint = CFDictionaryCreateMutableForCFTypesWith(allocator, CFSTR(kACMKeyAclConstraintBio), bioDict, NULL);
286 CFReleaseSafe(bioDict);
287 return constraint;
288 }
289
290 static SecAccessConstraintRef SecAccessConstraintCreateValueOfKofN(CFAllocatorRef allocator, size_t numRequired, CFArrayRef constraints, CFErrorRef *error) {
291 CFNumberRef k = CFNumberCreateWithCFIndex(allocator, numRequired);
292 CFMutableDictionaryRef kofn = CFDictionaryCreateMutableForCFTypesWith(allocator, CFSTR(kACMKeyAclParamKofN), k, NULL);
293 CFRelease(k);
294
295 /* Populate kofn dictionary with constraint keys from the array. note that for now we just ignore any additional
296 constraint parameters, but we might err-out if some parameter is found, since we cannot propagate parameteres
297 into k-of-n dictionary. */
298 const CFTypeRef keysToCopy[] = { CFSTR(kACMKeyAclConstraintBio), CFSTR(kACMKeyAclConstraintPolicy),
299 CFSTR(kACMKeyAclConstraintUserPasscode) };
300 SecAccessConstraintRef constraint;
301 CFArrayForEachC(constraints, constraint) {
302 require_quiet(isDictionary(constraint), errOut);
303 bool found = false;
304 for (CFIndex i = 0; i < (CFIndex)(sizeof(keysToCopy) / sizeof(keysToCopy[0])); i++) {
305 CFTypeRef value = CFDictionaryGetValue(constraint, keysToCopy[i]);
306 if (value) {
307 CFDictionarySetValue(kofn, keysToCopy[i], value);
308 found = true;
309 break;
310 }
311 }
312 require_quiet(found, errOut);
313 }
314
315 return kofn;
316
317 errOut:
318 SecError(errSecParam, error, CFSTR("SecAccessControl: invalid constraint for k-of-n"));
319 CFReleaseSafe(kofn);
320 return NULL;
321 }
322
323 SecAccessConstraintRef SecAccessConstraintCreateKofN(CFAllocatorRef allocator, size_t numRequired, CFArrayRef constraints, CFErrorRef *error) {
324 SecAccessConstraintRef valueOfKofN = SecAccessConstraintCreateValueOfKofN(allocator, numRequired, constraints, error);
325 require_quiet(valueOfKofN, errOut);
326
327 SecAccessConstraintRef constraint = CFDictionaryCreateMutableForCFTypesWith(allocator, CFSTR(kACMKeyAclConstraintKofN), valueOfKofN, NULL);
328 CFReleaseSafe(valueOfKofN);
329 return constraint;
330
331 errOut:
332 return NULL;
333 }
334
335 bool SecAccessControlAddConstraintForOperation(SecAccessControlRef access_control, CFTypeRef operation, CFTypeRef constraint, CFErrorRef *error) {
336 CheckItemInArray(operation, ItemArray(kAKSKeyOpEncrypt, kAKSKeyOpDecrypt,
337 #if TARGET_OS_EMBEDDED || TARGET_OS_IPHONE
338 kAKSKeyOpSign,
339 #endif
340 kAKSKeyOpSync, kAKSKeyOpDefaultAcl, kAKSKeyOpDelete),
341 "SecAccessControl: invalid operation %@");
342 if (!isDictionary(constraint) && !CFEqual(constraint, kCFBooleanTrue) && !CFEqual(constraint, kCFBooleanFalse) ) {
343 return SecError(errSecParam, error, CFSTR("invalid constraint"));
344 }
345
346 CFMutableDictionaryRef constraints = SecAccessControlGetMutableConstraints(access_control);
347 CFDictionarySetValue(constraints, operation, constraint);
348 return true;
349 }
350
351 SecAccessConstraintRef SecAccessControlGetConstraint(SecAccessControlRef access_control, CFTypeRef operation) {
352 CFMutableDictionaryRef ops = (CFMutableDictionaryRef)CFDictionaryGetValue(access_control->dict, kAKSKeyAcl);
353 if (!ops || CFDictionaryGetCount(ops) == 0)
354 // No ACL is present, this means that everything is allowed.
355 return kCFBooleanTrue;
356
357 SecAccessConstraintRef constraint = CFDictionaryGetValue(ops, operation);
358 if (!constraint) {
359 constraint = CFDictionaryGetValue(ops, kAKSKeyOpDefaultAcl);
360 }
361 return constraint;
362 }
363
364 CFDataRef SecAccessControlCopyConstraintData(SecAccessControlRef access_control, CFTypeRef operation) {
365 SecAccessConstraintRef constraint = SecAccessControlGetConstraint(access_control, operation);
366
367 size_t len = der_sizeof_plist(constraint, NULL);
368 CFMutableDataRef encoded = CFDataCreateMutable(0, len);
369 CFDataSetLength(encoded, len);
370 uint8_t *der_end = CFDataGetMutableBytePtr(encoded);
371 const uint8_t *der = der_end;
372 der_end += len;
373 der_end = der_encode_plist(constraint, NULL, der, der_end);
374 if (!der_end) {
375 CFReleaseNull(encoded);
376 }
377 return encoded;
378 }
379
380 CFDictionaryRef SecAccessControlGetConstraints(SecAccessControlRef access_control) {
381 return CFDictionaryGetValue(access_control->dict, kAKSKeyAcl);
382 }
383
384 void SecAccessControlSetConstraints(SecAccessControlRef access_control, CFDictionaryRef constraints) {
385 CFMutableDictionaryRef mutableConstraints = CFDictionaryCreateMutableCopy(CFGetAllocator(access_control), 0, constraints);
386 CFDictionarySetValue(access_control->dict, kAKSKeyAcl, mutableConstraints);
387 CFReleaseSafe(mutableConstraints);
388 }
389
390 void SecAccessControlSetRequirePassword(SecAccessControlRef access_control, bool require) {
391 CFMutableDictionaryRef constraints = SecAccessControlGetMutableConstraints(access_control);
392 CFDictionarySetValue(constraints, kAKSKeyAclParamRequirePasscode, require?kCFBooleanTrue:kCFBooleanFalse);
393 }
394
395 bool SecAccessControlGetRequirePassword(SecAccessControlRef access_control) {
396 CFMutableDictionaryRef acl = (CFMutableDictionaryRef)CFDictionaryGetValue(access_control->dict, kAKSKeyAcl);
397 if (acl) {
398 return CFEqualSafe(CFDictionaryGetValue(acl, kAKSKeyAclParamRequirePasscode), kCFBooleanTrue);
399 }
400
401 return false;
402 }
403
404 void SecAccessControlSetBound(SecAccessControlRef access_control, bool bound) {
405 CFDictionarySetValue(access_control->dict, kSecAccessControlKeyBound, bound ? kCFBooleanTrue : kCFBooleanFalse);
406 }
407
408 bool SecAccessControlIsBound(SecAccessControlRef access_control) {
409 CFTypeRef bound = CFDictionaryGetValue(access_control->dict, kSecAccessControlKeyBound);
410 return bound != NULL && CFEqualSafe(bound, kCFBooleanTrue);
411 }
412
413 CFDataRef SecAccessControlCopyData(SecAccessControlRef access_control) {
414 size_t len = der_sizeof_plist(access_control->dict, NULL);
415 CFMutableDataRef encoded = CFDataCreateMutable(0, len);
416 CFDataSetLength(encoded, len);
417 uint8_t *der_end = CFDataGetMutableBytePtr(encoded);
418 const uint8_t *der = der_end;
419 der_end += len;
420 der_end = der_encode_plist(access_control->dict, NULL, der, der_end);
421 if (!der_end) {
422 CFReleaseNull(encoded);
423 }
424 return encoded;
425 }
426
427 SecAccessControlRef SecAccessControlCreateFromData(CFAllocatorRef allocator, CFDataRef data, CFErrorRef *error) {
428 SecAccessControlRef access_control;
429 require_quiet(access_control = SecAccessControlCreate(allocator, error), errOut);
430
431 CFPropertyListRef plist;
432 const uint8_t *der = CFDataGetBytePtr(data);
433 const uint8_t *der_end = der + CFDataGetLength(data);
434 require_quiet(der = der_decode_plist(0, kCFPropertyListMutableContainers, &plist, error, der, der_end), errOut);
435 if (der != der_end) {
436 SecError(errSecDecode, error, CFSTR("trailing garbage at end of SecAccessControl data"));
437 goto errOut;
438 }
439
440 CFReleaseSafe(access_control->dict);
441 access_control->dict = (CFMutableDictionaryRef)plist;
442 return access_control;
443
444 errOut:
445 CFReleaseSafe(access_control);
446 return NULL;
447 }