]> git.saurik.com Git - apple/security.git/blob - OSX/libsecurity_keychain/lib/SecAccess.cpp
Security-58286.251.4.tar.gz
[apple/security.git] / OSX / libsecurity_keychain / lib / SecAccess.cpp
1 /*
2 * Copyright (c) 2002-2004,2011-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 #include <SecBase.h>
25 #include <Security/SecAccess.h>
26 #include <Security/SecAccessPriv.h>
27 #include <Security/SecTrustedApplication.h>
28 #include <Security/SecTrustedApplicationPriv.h>
29 #include <security_keychain/Access.h>
30 #include <security_utilities/casts.h>
31 #include <utilities/SecCFRelease.h>
32 #include "SecBridge.h"
33 #include <sys/param.h>
34
35 #include <utilities/SecCFWrappers.h>
36
37
38 /* No restrictions. Permission to perform all operations on
39 the resource or available to an ACL owner. */
40
41
42 const CFStringRef kSecACLAuthorizationAny = CFSTR("ACLAuthorizationAny");
43
44 const CFStringRef kSecACLAuthorizationLogin = CFSTR("ACLAuthorizationLogin");
45 const CFStringRef kSecACLAuthorizationGenKey = CFSTR("ACLAuthorizationGenKey");
46 const CFStringRef kSecACLAuthorizationDelete = CFSTR("ACLAuthorizationDelete");
47 const CFStringRef kSecACLAuthorizationExportWrapped = CFSTR("ACLAuthorizationExportWrapped");
48 const CFStringRef kSecACLAuthorizationExportClear = CFSTR("ACLAuthorizationExportClear");
49 const CFStringRef kSecACLAuthorizationImportWrapped = CFSTR("ACLAuthorizationImportWrapped");
50 const CFStringRef kSecACLAuthorizationImportClear = CFSTR("ACLAuthorizationImportClear");
51 const CFStringRef kSecACLAuthorizationSign = CFSTR("ACLAuthorizationSign");
52 const CFStringRef kSecACLAuthorizationEncrypt = CFSTR("ACLAuthorizationEncrypt");
53 const CFStringRef kSecACLAuthorizationDecrypt = CFSTR("ACLAuthorizationDecrypt");
54 const CFStringRef kSecACLAuthorizationMAC = CFSTR("ACLAuthorizationMAC");
55 const CFStringRef kSecACLAuthorizationDerive = CFSTR("ACLAuthorizationDerive");
56
57 /* Defined authorization tag values for Keychain */
58
59
60
61 const CFStringRef kSecACLAuthorizationKeychainCreate = CFSTR("ACLAuthorizationKeychainCreate");
62 const CFStringRef kSecACLAuthorizationKeychainDelete = CFSTR("ACLAuthorizationKeychainDelete");
63 const CFStringRef kSecACLAuthorizationKeychainItemRead = CFSTR("ACLAuthorizationKeychainItemRead");
64 const CFStringRef kSecACLAuthorizationKeychainItemInsert = CFSTR("ACLAuthorizationKeychainItemInsert");
65 const CFStringRef kSecACLAuthorizationKeychainItemModify = CFSTR("ACLAuthorizationKeychainItemModify");
66 const CFStringRef kSecACLAuthorizationKeychainItemDelete = CFSTR("ACLAuthorizationKeychainItemDelete");
67
68 const CFStringRef kSecACLAuthorizationChangeACL = CFSTR("ACLAuthorizationChangeACL");
69 const CFStringRef kSecACLAuthorizationChangeOwner = CFSTR("ACLAuthorizationChangeOwner");
70 const CFStringRef kSecACLAuthorizationPartitionID = CFSTR("ACLAuthorizationPartitionID");
71 const CFStringRef kSecACLAuthorizationIntegrity = CFSTR("ACLAuthorizationIntegrity");
72
73
74 static CFArrayRef copyTrustedAppListFromBundle(CFStringRef bundlePath, CFStringRef trustedAppListFileName);
75
76 static CFStringRef gKeys[] =
77 {
78 kSecACLAuthorizationAny,
79 kSecACLAuthorizationLogin,
80 kSecACLAuthorizationGenKey,
81 kSecACLAuthorizationDelete,
82 kSecACLAuthorizationExportWrapped,
83 kSecACLAuthorizationExportClear,
84 kSecACLAuthorizationImportWrapped,
85 kSecACLAuthorizationImportClear,
86 kSecACLAuthorizationSign,
87 kSecACLAuthorizationEncrypt,
88 kSecACLAuthorizationDecrypt,
89 kSecACLAuthorizationMAC,
90 kSecACLAuthorizationDerive,
91
92 /* Defined authorization tag values for Keychain */
93 kSecACLAuthorizationKeychainCreate,
94 kSecACLAuthorizationKeychainDelete,
95 kSecACLAuthorizationKeychainItemRead,
96 kSecACLAuthorizationKeychainItemInsert,
97 kSecACLAuthorizationKeychainItemModify,
98 kSecACLAuthorizationKeychainItemDelete,
99
100 kSecACLAuthorizationChangeACL,
101 kSecACLAuthorizationChangeOwner,
102 kSecACLAuthorizationPartitionID,
103 kSecACLAuthorizationIntegrity
104 };
105
106 static sint32 gValues[] =
107 {
108 CSSM_ACL_AUTHORIZATION_ANY,
109 CSSM_ACL_AUTHORIZATION_LOGIN,
110 CSSM_ACL_AUTHORIZATION_GENKEY,
111 CSSM_ACL_AUTHORIZATION_DELETE,
112 CSSM_ACL_AUTHORIZATION_EXPORT_WRAPPED,
113 CSSM_ACL_AUTHORIZATION_EXPORT_CLEAR,
114 CSSM_ACL_AUTHORIZATION_IMPORT_WRAPPED,
115 CSSM_ACL_AUTHORIZATION_IMPORT_CLEAR,
116 CSSM_ACL_AUTHORIZATION_SIGN,
117 CSSM_ACL_AUTHORIZATION_ENCRYPT,
118 CSSM_ACL_AUTHORIZATION_DECRYPT,
119 CSSM_ACL_AUTHORIZATION_MAC,
120 CSSM_ACL_AUTHORIZATION_DERIVE,
121 CSSM_ACL_AUTHORIZATION_DBS_CREATE,
122 CSSM_ACL_AUTHORIZATION_DBS_DELETE,
123 CSSM_ACL_AUTHORIZATION_DB_READ,
124 CSSM_ACL_AUTHORIZATION_DB_INSERT,
125 CSSM_ACL_AUTHORIZATION_DB_MODIFY,
126 CSSM_ACL_AUTHORIZATION_DB_DELETE,
127 CSSM_ACL_AUTHORIZATION_CHANGE_ACL,
128 CSSM_ACL_AUTHORIZATION_CHANGE_OWNER,
129 CSSM_ACL_AUTHORIZATION_PARTITION_ID,
130 CSSM_ACL_AUTHORIZATION_INTEGRITY
131 };
132
133 static
134 CFDictionaryRef CreateStringToNumDictionary()
135 {
136 int numItems = (sizeof(gValues) / sizeof(sint32));
137 CFMutableDictionaryRef tempDict = CFDictionaryCreateMutable(kCFAllocatorDefault, numItems, &kCFCopyStringDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks);
138
139 for (int iCnt = 0; iCnt < numItems; iCnt++)
140 {
141 sint32 aNumber = gValues[iCnt];
142 CFNumberRef aNum = CFNumberCreate(kCFAllocatorDefault, kCFNumberSInt32Type, &aNumber);
143
144 CFStringRef aString = gKeys[iCnt];
145 CFDictionaryAddValue(tempDict, aString, aNum);
146 CFRelease(aNum);
147 }
148
149 CFDictionaryRef result = CFDictionaryCreateCopy(kCFAllocatorDefault, tempDict);
150 CFRelease(tempDict);
151 return result;
152
153 }
154
155 static
156 CFDictionaryRef CreateNumToStringDictionary()
157 {
158 int numItems = (sizeof(gValues) / sizeof(sint32));
159
160 CFMutableDictionaryRef tempDict = CFDictionaryCreateMutable(kCFAllocatorDefault, numItems, &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks);
161
162 for (int iCnt = 0; iCnt < numItems; iCnt++)
163 {
164 sint32 aNumber = gValues[iCnt];
165 CFNumberRef aNum = CFNumberCreate(kCFAllocatorDefault, kCFNumberSInt32Type, &aNumber);
166
167 CFStringRef aString = gKeys[iCnt];
168 CFDictionaryAddValue(tempDict, aNum, aString);
169 CFRelease(aNum);
170
171 }
172
173 CFDictionaryRef result = CFDictionaryCreateCopy(kCFAllocatorDefault, tempDict);
174 CFRelease(tempDict);
175 return result;
176 }
177
178
179 /* TODO: This should be in some header */
180 sint32 GetACLAuthorizationTagFromString(CFStringRef aclStr);
181 sint32 GetACLAuthorizationTagFromString(CFStringRef aclStr)
182 {
183 if (NULL == aclStr)
184 {
185 #ifndef NDEBUG
186 CFShow(CFSTR("GetACLAuthorizationTagFromString aclStr is NULL"));
187 #endif
188 return 0;
189 }
190
191 static CFDictionaryRef gACLMapping = NULL;
192
193 if (NULL == gACLMapping)
194 {
195 gACLMapping = CreateStringToNumDictionary();
196 }
197
198 sint32 result = 0;
199 CFNumberRef valueResult = (CFNumberRef)CFDictionaryGetValue(gACLMapping, aclStr);
200 if (NULL != valueResult)
201 {
202 if (!CFNumberGetValue(valueResult, kCFNumberSInt32Type, &result))
203 {
204 return 0;
205 }
206
207 }
208 else
209 {
210 return 0;
211 }
212
213 return result;
214
215 }
216
217 /* TODO: This should be in some header */
218 CFStringRef GetAuthStringFromACLAuthorizationTag(sint32 tag);
219 CFStringRef GetAuthStringFromACLAuthorizationTag(sint32 tag)
220 {
221 static CFDictionaryRef gTagMapping = NULL;
222 CFNumberRef aNum = CFNumberCreate(kCFAllocatorDefault, kCFNumberSInt32Type, &tag);
223
224 if (NULL == gTagMapping)
225 {
226 gTagMapping = CreateNumToStringDictionary();
227 }
228
229 CFStringRef result = (CFStringRef)kSecACLAuthorizationAny;
230
231 if (NULL != gTagMapping && CFDictionaryContainsKey(gTagMapping, aNum))
232 {
233 result = (CFStringRef)CFDictionaryGetValue(gTagMapping, aNum);
234 }
235 CFReleaseSafe(aNum);
236 return result;
237 }
238
239 //
240 // CF boilerplate
241 //
242 CFTypeID SecAccessGetTypeID(void)
243 {
244 BEGIN_SECAPI
245 return gTypes().Access.typeID;
246 END_SECAPI1(_kCFRuntimeNotATypeID)
247 }
248
249
250 //
251 // API bridge calls
252 //
253 /*!
254 * Create a new SecAccessRef that is set to the default configuration
255 * of a (newly created) security object.
256 */
257 OSStatus SecAccessCreate(CFStringRef descriptor, CFArrayRef trustedList, SecAccessRef *accessRef)
258 {
259 BEGIN_SECAPI
260 Required(descriptor);
261 SecPointer<Access> access;
262 if (trustedList) {
263 CFIndex length = CFArrayGetCount(trustedList);
264 ACL::ApplicationList trusted;
265 for (CFIndex n = 0; n < length; n++)
266 trusted.push_back(TrustedApplication::required(
267 SecTrustedApplicationRef(CFArrayGetValueAtIndex(trustedList, n))));
268 access = new Access(cfString(descriptor), trusted);
269 } else {
270 access = new Access(cfString(descriptor));
271 }
272 Required(accessRef) = access->handle();
273 END_SECAPI
274 }
275
276
277 /*!
278 */
279 OSStatus SecAccessCreateFromOwnerAndACL(const CSSM_ACL_OWNER_PROTOTYPE *owner,
280 uint32 aclCount, const CSSM_ACL_ENTRY_INFO *acls,
281 SecAccessRef *accessRef)
282 {
283 BEGIN_SECAPI
284 Required(accessRef); // preflight
285 SecPointer<Access> access = new Access(Required(owner), aclCount, &Required(acls));
286 *accessRef = access->handle();
287 END_SECAPI
288 }
289
290 SecAccessRef SecAccessCreateWithOwnerAndACL(uid_t userId, gid_t groupId, SecAccessOwnerType ownerType, CFArrayRef acls, CFErrorRef *error)
291 {
292 SecAccessRef result = NULL;
293
294 CSSM_ACL_PROCESS_SUBJECT_SELECTOR selector =
295 {
296 CSSM_ACL_PROCESS_SELECTOR_CURRENT_VERSION, // selector version
297 int_cast<UInt32, uint16>(ownerType),
298 userId,
299 groupId
300 };
301
302 CSSM_LIST_ELEMENT subject2 = { NULL, 0 };
303 subject2.Element.Word.Data = (UInt8 *)&selector;
304 subject2.Element.Word.Length = sizeof(selector);
305 CSSM_LIST_ELEMENT subject1 =
306 {
307 &subject2, CSSM_ACL_SUBJECT_TYPE_PROCESS, CSSM_LIST_ELEMENT_WORDID
308 };
309
310 CFIndex numAcls = 0;
311
312 if (NULL != acls)
313 {
314 numAcls = CFArrayGetCount(acls);
315 }
316
317 #ifndef NDEBUG
318 CFStringRef debugStr = CFStringCreateWithFormat(kCFAllocatorDefault, NULL,
319 CFSTR("SecAccessCreateWithOwnerAndACL: processing %d acls"), (int)numAcls);
320 CFShow(debugStr);
321 CFRelease(debugStr);
322 #endif
323
324 CFIndex rightsSize = numAcls > 0 ? numAcls : 1;
325
326 std::vector<CSSM_ACL_AUTHORIZATION_TAG> rights(numAcls);
327
328 for (CFIndex iCnt = 0; iCnt < numAcls; iCnt++)
329 {
330 CFStringRef aclStr = (CFStringRef)CFArrayGetValueAtIndex(acls, iCnt);
331
332 #ifndef NDEBUG
333 debugStr = CFStringCreateWithFormat(kCFAllocatorDefault, NULL,
334 CFSTR("SecAccessCreateWithOwnerAndACL: acls[%d] = %@"), (int)iCnt, aclStr);
335
336 CFShow(debugStr);
337 CFRelease(debugStr);
338 #endif
339
340 CSSM_ACL_AUTHORIZATION_TAG aTag = GetACLAuthorizationTagFromString(aclStr);
341
342 #ifndef NDEBUG
343 debugStr = CFStringCreateWithFormat(kCFAllocatorDefault, NULL,
344 CFSTR("SecAccessCreateWithOwnerAndACL: rights[%d] = %d"), (int)iCnt, aTag);
345
346 CFShow(debugStr);
347 CFRelease(debugStr);
348 #endif
349
350 rights[iCnt] = aTag;
351 }
352
353
354 for (CFIndex iCnt = 0; iCnt < numAcls; iCnt++)
355 {
356 #ifndef NDEBUG
357 debugStr = CFStringCreateWithFormat(kCFAllocatorDefault, NULL,
358 CFSTR("SecAccessCreateWithOwnerAndACL: rights[%d] = %d"), (int)iCnt, rights[iCnt]);
359
360 CFShow(debugStr);
361 CFRelease(debugStr);
362 #endif
363
364
365 }
366
367 CSSM_ACL_OWNER_PROTOTYPE owner =
368 {
369 // TypedSubject
370 { CSSM_LIST_TYPE_UNKNOWN, &subject1, &subject2 },
371 // Delegate
372 false
373 };
374
375
376 // ACL entries (any number, just one here)
377 CSSM_ACL_ENTRY_INFO acl_rights[] =
378 {
379 {
380 // prototype
381 {
382 // TypedSubject
383 { CSSM_LIST_TYPE_UNKNOWN, &subject1, &subject2 },
384 false, // Delegate
385 // rights for this entry
386 { (uint32)numAcls, rights.data() },
387 // rest is defaulted
388 }
389 }
390 };
391
392 OSStatus err = SecAccessCreateFromOwnerAndACL(&owner,
393 sizeof(acl_rights) / sizeof(acl_rights[0]), acl_rights, &result);
394
395 if (errSecSuccess != err)
396 {
397 result = NULL;
398 if (NULL != error)
399 {
400 *error = CFErrorCreate(kCFAllocatorDefault, CFSTR("FIX ME"), err, NULL);
401 }
402 }
403 return result;
404 }
405
406
407 /*!
408 */
409 OSStatus SecAccessGetOwnerAndACL(SecAccessRef accessRef,
410 CSSM_ACL_OWNER_PROTOTYPE_PTR *owner,
411 uint32 *aclCount, CSSM_ACL_ENTRY_INFO_PTR *acls)
412 {
413 BEGIN_SECAPI
414 Access::required(accessRef)->copyOwnerAndAcl(
415 Required(owner), Required(aclCount), Required(acls));
416 END_SECAPI
417 }
418
419 OSStatus SecAccessCopyOwnerAndACL(SecAccessRef accessRef, uid_t* userId, gid_t* groupId, SecAccessOwnerType* ownerType, CFArrayRef* aclList)
420 {
421 CSSM_ACL_OWNER_PROTOTYPE_PTR owner = NULL;
422 CSSM_ACL_ENTRY_INFO_PTR acls = NULL;
423 uint32 aclCount = 0;
424 OSStatus result = SecAccessGetOwnerAndACL(accessRef, &owner, &aclCount, &acls);
425 if (errSecSuccess != result )
426 {
427 return result;
428 }
429
430 if (NULL != owner)
431 {
432 CSSM_LIST_ELEMENT_PTR listHead = owner->TypedSubject.Head;
433 if (listHead != NULL && listHead->ElementType == CSSM_LIST_ELEMENT_WORDID)
434 {
435 CSSM_LIST_ELEMENT_PTR nextElement = listHead->NextElement;
436 if (listHead->WordID == CSSM_ACL_SUBJECT_TYPE_PROCESS && listHead->ElementType == CSSM_LIST_ELEMENT_WORDID)
437 {
438 // nextElement contains the required data
439 CSSM_ACL_PROCESS_SUBJECT_SELECTOR* selectorPtr = (CSSM_ACL_PROCESS_SUBJECT_SELECTOR*)nextElement->Element.Word.Data;
440 if (NULL != selectorPtr)
441 {
442 if (NULL != userId)
443 {
444 *userId = (uid_t)selectorPtr->uid;
445 }
446
447 if (NULL != groupId)
448 {
449 *groupId = (gid_t)selectorPtr->gid;
450 }
451
452 if (NULL != ownerType)
453 {
454 *ownerType = (SecAccessOwnerType)selectorPtr->mask;
455 }
456 }
457 }
458
459 }
460
461 }
462
463 if (NULL != aclList)
464 {
465 #ifndef NDEBUG
466 CFShow(CFSTR("SecAccessCopyOwnerAndACL: processing the ACL list"));
467 #endif
468
469 CFMutableArrayRef stringArray = CFArrayCreateMutable(kCFAllocatorDefault, 0, &kCFTypeArrayCallBacks);
470 CSSM_ACL_OWNER_PROTOTYPE_PTR protoPtr = NULL;
471 uint32 numAcls = 0L;
472 CSSM_ACL_ENTRY_INFO_PTR aclEntry = NULL;
473
474 result = SecAccessGetOwnerAndACL(accessRef, &protoPtr, &numAcls, &aclEntry);
475 if (errSecSuccess == result)
476 {
477 #ifndef NDEBUG
478 CFStringRef tempStr = CFStringCreateWithFormat(kCFAllocatorDefault, NULL, CFSTR("SecAccessCopyOwnerAndACL: numAcls = %d"), numAcls);
479 CFShow(tempStr);
480 CFRelease(tempStr);
481 #endif
482
483 for (uint32 iCnt = 0; iCnt < numAcls; iCnt++)
484 {
485 CSSM_ACL_ENTRY_PROTOTYPE prototype = aclEntry[iCnt].EntryPublicInfo;
486 CSSM_AUTHORIZATIONGROUP authGroup = prototype.Authorization;
487 int numAuthTags = (int)authGroup.NumberOfAuthTags;
488
489 for (int jCnt = 0; jCnt < numAuthTags; jCnt++)
490 {
491
492 sint32 aTag = authGroup.AuthTags[jCnt];
493 CFStringRef aString = GetAuthStringFromACLAuthorizationTag(aTag);
494
495 CFArrayAppendValue(stringArray, aString);
496 }
497 }
498 }
499
500 if (NULL != stringArray)
501 {
502 if (0 < CFArrayGetCount(stringArray))
503 {
504 *aclList = CFArrayCreateCopy(kCFAllocatorDefault, stringArray);
505 }
506 CFRelease(stringArray);
507 }
508 }
509
510 return result;
511 }
512
513 /*!
514 */
515 OSStatus SecAccessCopyACLList(SecAccessRef accessRef,
516 CFArrayRef *aclList)
517 {
518 BEGIN_SECAPI
519 Required(aclList) = Access::required(accessRef)->copySecACLs();
520 END_SECAPI
521 }
522
523
524 /*!
525 */
526 OSStatus SecAccessCopySelectedACLList(SecAccessRef accessRef,
527 CSSM_ACL_AUTHORIZATION_TAG action,
528 CFArrayRef *aclList)
529 {
530 BEGIN_SECAPI
531 Required(aclList) = Access::required(accessRef)->copySecACLs(action);
532 END_SECAPI
533 }
534
535 CFArrayRef SecAccessCopyMatchingACLList(SecAccessRef accessRef, CFTypeRef authorizationTag)
536 {
537 CFArrayRef result = NULL;
538 CSSM_ACL_AUTHORIZATION_TAG tag = GetACLAuthorizationTagFromString((CFStringRef)authorizationTag);
539 OSStatus err = SecAccessCopySelectedACLList(accessRef, tag, &result);
540 if (errSecSuccess != err)
541 {
542 result = NULL;
543 }
544 return result;
545 }
546
547 CFArrayRef copyTrustedAppListFromBundle(CFStringRef bundlePath, CFStringRef trustedAppListFileName)
548 {
549 CFStringRef errorString = nil;
550 CFURLRef bundleURL,trustedAppsURL = NULL;
551 CFBundleRef secBundle = NULL;
552 CFPropertyListRef trustedAppsPlist = NULL;
553 CFDataRef xmlDataRef = NULL;
554 SInt32 errorCode;
555 CFArrayRef trustedAppList = NULL;
556 CFMutableStringRef trustedAppListFileNameWithoutExtension = NULL;
557
558 // Make a CFURLRef from the CFString representation of the bundleƕs path.
559 bundleURL = CFURLCreateWithFileSystemPath(
560 kCFAllocatorDefault,bundlePath,kCFURLPOSIXPathStyle,true);
561
562 CFRange wholeStrRange;
563
564 if (!bundleURL)
565 goto xit;
566
567 // Make a bundle instance using the URLRef.
568 secBundle = CFBundleCreate(kCFAllocatorDefault,bundleURL);
569 if (!secBundle)
570 goto xit;
571
572 trustedAppListFileNameWithoutExtension =
573 CFStringCreateMutableCopy(NULL,CFStringGetLength(trustedAppListFileName),trustedAppListFileName);
574 wholeStrRange = CFStringFind(trustedAppListFileName,CFSTR(".plist"),0);
575
576 CFStringDelete(trustedAppListFileNameWithoutExtension,wholeStrRange);
577
578 // Look for a resource in the bundle by name and type
579 trustedAppsURL = CFBundleCopyResourceURL(secBundle,trustedAppListFileNameWithoutExtension,CFSTR("plist"),NULL);
580 if (!trustedAppsURL)
581 goto xit;
582
583 if (!CFURLCreateDataAndPropertiesFromResource(kCFAllocatorDefault,trustedAppsURL,&xmlDataRef,NULL,NULL,&errorCode))
584 goto xit;
585
586 trustedAppsPlist = CFPropertyListCreateFromXMLData(kCFAllocatorDefault,xmlDataRef,kCFPropertyListImmutable,&errorString);
587 trustedAppList = (CFArrayRef)trustedAppsPlist;
588
589 xit:
590 CFReleaseNull(trustedAppListFileNameWithoutExtension);
591 if (bundleURL)
592 CFRelease(bundleURL);
593 if (secBundle)
594 CFRelease(secBundle);
595 if (trustedAppsURL)
596 CFRelease(trustedAppsURL);
597 if (xmlDataRef)
598 CFRelease(xmlDataRef);
599 if (errorString)
600 CFRelease(errorString);
601
602 return trustedAppList;
603 }
604
605 OSStatus SecAccessCreateWithTrustedApplications(CFStringRef trustedApplicationsPListPath, CFStringRef accessLabel, Boolean allowAny, SecAccessRef* returnedAccess)
606 {
607 OSStatus err = errSecSuccess;
608 SecAccessRef accessToReturn=nil;
609 CFMutableArrayRef trustedApplications=nil;
610
611 if (!allowAny) // use default access ("confirm access")
612 {
613 // make an exception list of applications you want to trust,
614 // which are allowed to access the item without requiring user confirmation
615 SecTrustedApplicationRef myself=NULL, someOther=NULL;
616 CFArrayRef trustedAppListFromBundle=NULL;
617
618 trustedApplications=CFArrayCreateMutable(kCFAllocatorDefault,0,&kCFTypeArrayCallBacks);
619 err = SecTrustedApplicationCreateFromPath(NULL, &myself);
620 if (!err)
621 CFArrayAppendValue(trustedApplications,myself);
622
623 CFURLRef url = CFURLCreateWithFileSystemPath(NULL, trustedApplicationsPListPath, kCFURLPOSIXPathStyle, 0);
624 CFStringRef leafStr = NULL;
625 leafStr = CFURLCopyLastPathComponent(url);
626
627 CFURLRef bndlPathURL = NULL;
628 bndlPathURL = CFURLCreateCopyDeletingLastPathComponent(NULL, url);
629 CFStringRef bndlPath = NULL;
630 bndlPath = CFURLCopyFileSystemPath(bndlPathURL, kCFURLPOSIXPathStyle);
631 trustedAppListFromBundle=copyTrustedAppListFromBundle(bndlPath, leafStr);
632 if ( leafStr )
633 CFRelease(leafStr);
634 if ( bndlPath )
635 CFRelease(bndlPath);
636 if ( url )
637 CFRelease(url);
638 if ( bndlPathURL )
639 CFRelease(bndlPathURL);
640 if (trustedAppListFromBundle)
641 {
642 CFIndex ix,top;
643 char buffer[MAXPATHLEN];
644 top = CFArrayGetCount(trustedAppListFromBundle);
645 for (ix=0;ix<top;ix++)
646 {
647 CFStringRef filename = (CFStringRef)CFArrayGetValueAtIndex(trustedAppListFromBundle,ix);
648 CFIndex stringLength = CFStringGetLength(filename);
649 CFIndex usedBufLen;
650
651 if (stringLength != CFStringGetBytes(filename,CFRangeMake(0,stringLength),kCFStringEncodingUTF8,0,
652 false,(UInt8 *)&buffer,MAXPATHLEN, &usedBufLen))
653 break;
654 buffer[usedBufLen] = 0;
655 //
656 // Support specification of trusted applications by either
657 // a full pathname or a code requirement string.
658 //
659 if (buffer[0]=='/')
660 {
661 err = SecTrustedApplicationCreateFromPath(buffer,&someOther);
662 }
663 else
664 {
665 char *buf = NULL;
666 CFStringRef reqStr = filename;
667 CFArrayRef descArray = CFStringCreateArrayBySeparatingStrings(NULL, reqStr, CFSTR("\""));
668 if (descArray && (CFArrayGetCount(descArray) > 1))
669 {
670 CFStringRef descStr = (CFStringRef) CFArrayGetValueAtIndex(descArray, 1);
671 if (descStr)
672 buf = CFStringToCString(descStr);
673 }
674 SecRequirementRef reqRef = NULL;
675 err = SecRequirementCreateWithString(reqStr, kSecCSDefaultFlags, &reqRef);
676 if (!err)
677 err = SecTrustedApplicationCreateFromRequirement((const char *)buf, reqRef, &someOther);
678 if (buf)
679 free(buf);
680 CFReleaseSafe(reqRef);
681 CFReleaseSafe(descArray);
682 }
683 if (!err)
684 CFArrayAppendValue(trustedApplications,someOther);
685
686 if (someOther)
687 CFReleaseNull(someOther);
688 }
689 CFRelease(trustedAppListFromBundle);
690 }
691 }
692
693 err = SecAccessCreate((CFStringRef)accessLabel, (CFArrayRef)trustedApplications, &accessToReturn);
694 if (!err)
695 {
696 if (allowAny) // change access to be wide-open for decryption ("always allow access")
697 {
698 // get the access control list for decryption operations
699 CFArrayRef aclList=nil;
700 err = SecAccessCopySelectedACLList(accessToReturn, CSSM_ACL_AUTHORIZATION_DECRYPT, &aclList);
701 if (!err)
702 {
703 // get the first entry in the access control list
704 SecACLRef aclRef=(SecACLRef)CFArrayGetValueAtIndex(aclList, 0);
705 CFArrayRef appList=nil;
706 CFStringRef promptDescription=nil;
707 CSSM_ACL_KEYCHAIN_PROMPT_SELECTOR promptSelector;
708 err = SecACLCopySimpleContents(aclRef, &appList, &promptDescription, &promptSelector);
709
710 // modify the default ACL to not require the passphrase, and have a nil application list
711 promptSelector.flags &= ~CSSM_ACL_KEYCHAIN_PROMPT_REQUIRE_PASSPHRASE;
712 err = SecACLSetSimpleContents(aclRef, NULL, promptDescription, &promptSelector);
713
714 if (appList) CFRelease(appList);
715 if (promptDescription) CFRelease(promptDescription);
716 }
717 }
718 }
719 *returnedAccess = accessToReturn;
720 return err;
721 }