2 * Copyright (c) 2002-2008, 2010, 2011 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 * Modification History
27 * October 31, 2000 Allan Nathanson <ajn@apple.com>
32 #include <sys/types.h>
33 #include <sys/param.h>
37 #include "dy_framework.h"
42 #pragma mark IOKit.framework APIs
46 static void *image
= NULL
;
48 const char *framework
= "/System/Library/Frameworks/IOKit.framework/IOKit";
50 const char *suffix
= getenv("DYLD_IMAGE_SUFFIX");
51 char path
[MAXPATHLEN
];
53 strlcpy(path
, framework
, sizeof(path
));
54 if (suffix
) strlcat(path
, suffix
, sizeof(path
));
55 if (0 <= stat(path
, &statbuf
)) {
56 image
= dlopen(path
, RTLD_LAZY
| RTLD_LOCAL
);
58 image
= dlopen(framework
, RTLD_LAZY
| RTLD_LOCAL
);
65 __private_extern__ CFMutableDictionaryRef
66 _IOBSDNameMatching(mach_port_t masterPort
, uint32_t options
, const char *bsdName
)
68 #undef IOBSDNameMatching
69 static typeof (IOBSDNameMatching
) *dyfunc
= NULL
;
71 void *image
= __loadIOKit();
72 if (image
) dyfunc
= dlsym(image
, "IOBSDNameMatching");
74 return dyfunc
? dyfunc(masterPort
, options
, bsdName
) : NULL
;
78 __private_extern__ io_object_t
79 _IOIteratorNext(io_iterator_t iterator
)
82 static typeof (IOIteratorNext
) *dyfunc
= NULL
;
84 void *image
= __loadIOKit();
85 if (image
) dyfunc
= dlsym(image
, "IOIteratorNext");
87 return dyfunc
? dyfunc(iterator
) : 0;
91 __private_extern__ kern_return_t
92 _IOMasterPort(mach_port_t bootstrapPort
, mach_port_t
*masterPort
)
95 static typeof (IOMasterPort
) *dyfunc
= NULL
;
97 void *image
= __loadIOKit();
98 if (image
) dyfunc
= dlsym(image
, "IOMasterPort");
100 return dyfunc
? dyfunc(bootstrapPort
, masterPort
) : KERN_FAILURE
;
104 __private_extern__ boolean_t
105 _IOObjectConformsTo(io_object_t object
, const io_name_t className
)
107 #undef IOObjectConformsTo
108 static typeof (IOObjectConformsTo
) *dyfunc
= NULL
;
110 void *image
= __loadIOKit();
111 if (image
) dyfunc
= dlsym(image
, "IOObjectConformsTo");
113 return dyfunc
? dyfunc(object
, className
) : FALSE
;
117 __private_extern__ boolean_t
118 _IOObjectGetClass(io_object_t object
, io_name_t className
)
120 #undef IOObjectGetClass
121 static typeof (IOObjectGetClass
) *dyfunc
= NULL
;
123 void *image
= __loadIOKit();
124 if (image
) dyfunc
= dlsym(image
, "IOObjectGetClass");
126 return dyfunc
? dyfunc(object
, className
) : FALSE
;
130 __private_extern__ kern_return_t
131 _IOObjectRelease(io_object_t object
)
133 #undef IOObjectRelease
134 static typeof (IOObjectRelease
) *dyfunc
= NULL
;
136 void *image
= __loadIOKit();
137 if (image
) dyfunc
= dlsym(image
, "IOObjectRelease");
139 return dyfunc
? dyfunc(object
) : KERN_FAILURE
;
143 __private_extern__ CFTypeRef
144 _IORegistryEntryCreateCFProperty(io_registry_entry_t entry
, CFStringRef key
, CFAllocatorRef allocator
, IOOptionBits options
)
146 #undef IORegistryEntryCreateCFProperty
147 static typeof (IORegistryEntryCreateCFProperty
) *dyfunc
= NULL
;
149 void *image
= __loadIOKit();
150 if (image
) dyfunc
= dlsym(image
, "IORegistryEntryCreateCFProperty");
152 return dyfunc
? dyfunc(entry
, key
, allocator
, options
) : NULL
;
156 __private_extern__ kern_return_t
157 _IORegistryEntryCreateCFProperties(io_registry_entry_t entry
, CFMutableDictionaryRef
*properties
, CFAllocatorRef allocator
, IOOptionBits options
)
159 #undef IORegistryEntryCreateCFProperties
160 static typeof (IORegistryEntryCreateCFProperties
) *dyfunc
= NULL
;
162 void *image
= __loadIOKit();
163 if (image
) dyfunc
= dlsym(image
, "IORegistryEntryCreateCFProperties");
165 return dyfunc
? dyfunc(entry
, properties
, allocator
, options
) : KERN_FAILURE
;
169 __private_extern__ kern_return_t
170 _IORegistryEntryCreateIterator(mach_port_t masterPort
, const io_name_t plane
, IOOptionBits options
, io_iterator_t
*iterator
)
172 #undef IORegistryEntryCreateIterator
173 static typeof (IORegistryEntryCreateIterator
) *dyfunc
= NULL
;
175 void *image
= __loadIOKit();
176 if (image
) dyfunc
= dlsym(image
, "IORegistryEntryCreateIterator");
178 return dyfunc
? dyfunc(masterPort
, plane
, options
, iterator
) : KERN_FAILURE
;
182 __private_extern__ kern_return_t
183 _IORegistryEntryGetName(io_registry_entry_t entry
, io_name_t name
)
185 #undef IORegistryEntryGetName
186 static typeof (IORegistryEntryGetName
) *dyfunc
= NULL
;
188 void *image
= __loadIOKit();
189 if (image
) dyfunc
= dlsym(image
, "IORegistryEntryGetName");
191 return dyfunc
? dyfunc(entry
, name
) : KERN_FAILURE
;
195 __private_extern__ kern_return_t
196 _IORegistryEntryGetParentEntry(io_registry_entry_t entry
, const io_name_t plane
, io_registry_entry_t
*parent
)
198 #undef IORegistryEntryGetParentEntry
199 static typeof (IORegistryEntryGetParentEntry
) *dyfunc
= NULL
;
201 void *image
= __loadIOKit();
202 if (image
) dyfunc
= dlsym(image
, "IORegistryEntryGetParentEntry");
204 return dyfunc
? dyfunc(entry
, plane
, parent
) : KERN_FAILURE
;
208 __private_extern__ kern_return_t
209 _IORegistryEntryGetPath(io_registry_entry_t entry
, const io_name_t plane
, io_string_t path
)
211 #undef IORegistryEntryGetPath
212 static typeof (IORegistryEntryGetPath
) *dyfunc
= NULL
;
214 void *image
= __loadIOKit();
215 if (image
) dyfunc
= dlsym(image
, "IORegistryEntryGetPath");
217 return dyfunc
? dyfunc(entry
, plane
, path
) : KERN_FAILURE
;
221 __private_extern__ kern_return_t
222 _IORegistryEntryGetRegistryEntryID(io_registry_entry_t entry
, uint64_t *entryID
)
224 #undef IORegistryEntryGetRegistryEntryID
225 static typeof (IORegistryEntryGetRegistryEntryID
) *dyfunc
= NULL
;
227 void *image
= __loadIOKit();
228 if (image
) dyfunc
= dlsym(image
, "IORegistryEntryGetRegistryEntryID");
230 return dyfunc
? dyfunc(entry
, entryID
) : KERN_FAILURE
;
234 __private_extern__ CFTypeRef
235 _IORegistryEntrySearchCFProperty(io_registry_entry_t entry
, const io_name_t plane
, CFStringRef key
, CFAllocatorRef allocator
, IOOptionBits options
)
237 #undef IORegistryEntrySearchCFProperty
238 static typeof (IORegistryEntrySearchCFProperty
) *dyfunc
= NULL
;
240 void *image
= __loadIOKit();
241 if (image
) dyfunc
= dlsym(image
, "IORegistryEntrySearchCFProperty");
243 return dyfunc
? dyfunc(entry
, plane
, key
, allocator
, options
) : NULL
;
247 __private_extern__ kern_return_t
248 _IOServiceGetMatchingServices(mach_port_t masterPort
, CFDictionaryRef matching
, io_iterator_t
*existing
)
250 #undef IOServiceGetMatchingServices
251 static typeof (IOServiceGetMatchingServices
) *dyfunc
= NULL
;
253 void *image
= __loadIOKit();
254 if (image
) dyfunc
= dlsym(image
, "IOServiceGetMatchingServices");
256 return dyfunc
? dyfunc(masterPort
, matching
, existing
) : KERN_FAILURE
;
260 __private_extern__ CFMutableDictionaryRef
261 _IOServiceMatching(const char *name
)
263 #undef IOServiceMatching
264 static typeof (IOServiceMatching
) *dyfunc
= NULL
;
266 void *image
= __loadIOKit();
267 if (image
) dyfunc
= dlsym(image
, "IOServiceMatching");
269 return dyfunc
? dyfunc(name
) : NULL
;
273 #pragma mark Security.framework APIs
276 __loadSecurity(void) {
277 static void *image
= NULL
;
279 const char *framework
= "/System/Library/Frameworks/Security.framework/Security";
281 const char *suffix
= getenv("DYLD_IMAGE_SUFFIX");
282 char path
[MAXPATHLEN
];
284 strlcpy(path
, framework
, sizeof(path
));
285 if (suffix
) strlcat(path
, suffix
, sizeof(path
));
286 if (0 <= stat(path
, &statbuf
)) {
287 image
= dlopen(path
, RTLD_LAZY
| RTLD_LOCAL
);
289 image
= dlopen(framework
, RTLD_LAZY
| RTLD_LOCAL
);
292 return (void *)image
;
295 #define SECURITY_FRAMEWORK_EXTERN(t, s) \
296 __private_extern__ t \
299 static t *dysym = NULL; \
301 void *image = __loadSecurity(); \
302 if (image) dysym = dlsym(image, #s ); \
304 return (dysym != NULL) ? *dysym : NULL; \
307 #if !TARGET_OS_IPHONE
308 SECURITY_FRAMEWORK_EXTERN(CFTypeRef
, kSecAttrService
)
309 SECURITY_FRAMEWORK_EXTERN(CFTypeRef
, kSecClass
)
310 SECURITY_FRAMEWORK_EXTERN(CFTypeRef
, kSecClassGenericPassword
)
311 SECURITY_FRAMEWORK_EXTERN(CFTypeRef
, kSecMatchLimit
)
312 SECURITY_FRAMEWORK_EXTERN(CFTypeRef
, kSecMatchLimitAll
)
313 SECURITY_FRAMEWORK_EXTERN(CFTypeRef
, kSecMatchSearchList
)
314 SECURITY_FRAMEWORK_EXTERN(CFTypeRef
, kSecReturnRef
)
316 __private_extern__ OSStatus
317 _AuthorizationMakeExternalForm(AuthorizationRef authorization
, AuthorizationExternalForm
*extForm
)
319 #undef AuthorizationMakeExternalForm
320 static typeof (AuthorizationMakeExternalForm
) *dyfunc
= NULL
;
322 void *image
= __loadSecurity();
323 if (image
) dyfunc
= dlsym(image
, "AuthorizationMakeExternalForm");
325 return dyfunc
? dyfunc(authorization
, extForm
) : -1;
328 __private_extern__ OSStatus
329 _SecAccessCreate(CFStringRef descriptor
, CFArrayRef trustedlist
, SecAccessRef
*accessRef
)
331 #undef SecAccessCreate
332 static typeof (SecAccessCreate
) *dyfunc
= NULL
;
334 void *image
= __loadSecurity();
335 if (image
) dyfunc
= dlsym(image
, "SecAccessCreate");
337 return dyfunc
? dyfunc(descriptor
, trustedlist
, accessRef
) : -1;
340 #if (__MAC_OS_X_VERSION_MIN_REQUIRED < 1070)
341 __private_extern__ OSStatus
342 _SecAccessCreateFromOwnerAndACL(const CSSM_ACL_OWNER_PROTOTYPE
*owner
, uint32 aclCount
, const CSSM_ACL_ENTRY_INFO
*acls
, SecAccessRef
*accessRef
)
344 #undef SecAccessCreateFromOwnerAndACL
345 static typeof (SecAccessCreateFromOwnerAndACL
) *dyfunc
= NULL
;
347 void *image
= __loadSecurity();
348 if (image
) dyfunc
= dlsym(image
, "SecAccessCreateFromOwnerAndACL");
350 return dyfunc
? dyfunc(owner
, aclCount
, acls
, accessRef
) : -1;
352 #else // (__MAC_OS_X_VERSION_MIN_REQUIRED < 1070)
353 __private_extern__ SecAccessRef
354 _SecAccessCreateWithOwnerAndACL(uid_t userId
, gid_t groupId
, SecAccessOwnerType ownerType
, CFArrayRef acls
, CFErrorRef
*error
)
356 #undef SecAccessCreateWithOwnerAndACL
357 static typeof (SecAccessCreateWithOwnerAndACL
) *dyfunc
= NULL
;
359 void *image
= __loadSecurity();
360 if (image
) dyfunc
= dlsym(image
, "SecAccessCreateWithOwnerAndACL");
362 return dyfunc
? dyfunc(userId
, groupId
, ownerType
, acls
, error
) : NULL
;
364 #endif // (__MAC_OS_X_VERSION_MIN_REQUIRED < 1070)
366 __private_extern__ OSStatus
367 _SecItemCopyMatching(CFDictionaryRef query
, CFTypeRef
*result
)
369 #undef SecItemCopyMatching
370 static typeof (SecItemCopyMatching
) *dyfunc
= NULL
;
372 void *image
= __loadSecurity();
373 if (image
) dyfunc
= dlsym(image
, "SecItemCopyMatching");
375 return dyfunc
? dyfunc(query
, result
) : -1;
378 __private_extern__ OSStatus
379 _SecKeychainCopyDomainDefault(SecPreferencesDomain domain
, SecKeychainRef
*keychain
)
381 #undef SecKeychainCopyDomainDefault
382 static typeof (SecKeychainCopyDomainDefault
) *dyfunc
= NULL
;
384 void *image
= __loadSecurity();
385 if (image
) dyfunc
= dlsym(image
, "SecKeychainCopyDomainDefault");
387 return dyfunc
? dyfunc(domain
, keychain
) : -1;
390 __private_extern__ OSStatus
391 _SecKeychainGetPreferenceDomain(SecPreferencesDomain
*domain
)
393 #undef SecKeychainGetPreferenceDomain
394 static typeof (SecKeychainGetPreferenceDomain
) *dyfunc
= NULL
;
396 void *image
= __loadSecurity();
397 if (image
) dyfunc
= dlsym(image
, "SecKeychainGetPreferenceDomain");
399 return dyfunc
? dyfunc(domain
) : -1;
402 __private_extern__ OSStatus
403 _SecKeychainOpen(const char *pathName
, SecKeychainRef
*keychain
)
405 #undef SecKeychainOpen
406 static typeof (SecKeychainOpen
) *dyfunc
= NULL
;
408 void *image
= __loadSecurity();
409 if (image
) dyfunc
= dlsym(image
, "SecKeychainOpen");
411 return dyfunc
? dyfunc(pathName
, keychain
) : -1;
414 __private_extern__ OSStatus
415 _SecKeychainSetDomainDefault(SecPreferencesDomain domain
, SecKeychainRef keychain
)
417 #undef SecKeychainSetDomainDefault
418 static typeof (SecKeychainSetDomainDefault
) *dyfunc
= NULL
;
420 void *image
= __loadSecurity();
421 if (image
) dyfunc
= dlsym(image
, "SecKeychainSetDomainDefault");
423 return dyfunc
? dyfunc(domain
, keychain
) : -1;
426 __private_extern__ OSStatus
427 _SecKeychainSetPreferenceDomain(SecPreferencesDomain domain
)
429 #undef SecKeychainSetPreferenceDomain
430 static typeof (SecKeychainSetPreferenceDomain
) *dyfunc
= NULL
;
432 void *image
= __loadSecurity();
433 if (image
) dyfunc
= dlsym(image
, "SecKeychainSetPreferenceDomain");
435 return dyfunc
? dyfunc(domain
) : -1;
438 __private_extern__ OSStatus
439 _SecKeychainItemCopyContent(SecKeychainItemRef itemRef
, SecItemClass
*itemClass
, SecKeychainAttributeList
*attrList
, UInt32
*length
, void **outData
)
441 #undef SecKeychainItemCopyContent
442 static typeof (SecKeychainItemCopyContent
) *dyfunc
= NULL
;
444 void *image
= __loadSecurity();
445 if (image
) dyfunc
= dlsym(image
, "SecKeychainItemCopyContent");
447 return dyfunc
? dyfunc(itemRef
, itemClass
, attrList
, length
, outData
) : -1;
450 __private_extern__ OSStatus
451 _SecKeychainItemCreateFromContent(SecItemClass itemClass
, SecKeychainAttributeList
*attrList
, UInt32 length
, const void *data
, SecKeychainRef keychainRef
, SecAccessRef initialAccess
, SecKeychainItemRef
*itemRef
)
453 #undef SecKeychainItemCreateFromContent
454 static typeof (SecKeychainItemCreateFromContent
) *dyfunc
= NULL
;
456 void *image
= __loadSecurity();
457 if (image
) dyfunc
= dlsym(image
, "SecKeychainItemCreateFromContent");
459 return dyfunc
? dyfunc(itemClass
, attrList
, length
, data
, keychainRef
, initialAccess
, itemRef
) : -1;
462 __private_extern__ OSStatus
463 _SecKeychainItemDelete(SecKeychainItemRef itemRef
)
465 #undef SecKeychainItemDelete
466 static typeof (SecKeychainItemDelete
) *dyfunc
= NULL
;
468 void *image
= __loadSecurity();
469 if (image
) dyfunc
= dlsym(image
, "SecKeychainItemDelete");
471 return dyfunc
? dyfunc(itemRef
) : -1;
474 __private_extern__ OSStatus
475 _SecKeychainItemFreeContent(SecKeychainAttributeList
*attrList
, void *data
)
477 #undef SecKeychainItemFreeContent
478 static typeof (SecKeychainItemFreeContent
) *dyfunc
= NULL
;
480 void *image
= __loadSecurity();
481 if (image
) dyfunc
= dlsym(image
, "SecKeychainItemFreeContent");
483 return dyfunc
? dyfunc(attrList
, data
) : -1;
486 __private_extern__ OSStatus
487 _SecKeychainItemModifyContent(SecKeychainItemRef itemRef
, const SecKeychainAttributeList
*attrList
, UInt32 length
, const void *data
)
489 #undef SecKeychainItemModifyContent
490 static typeof (SecKeychainItemModifyContent
) *dyfunc
= NULL
;
492 void *image
= __loadSecurity();
493 if (image
) dyfunc
= dlsym(image
, "SecKeychainItemModifyContent");
495 return dyfunc
? dyfunc(itemRef
, attrList
, length
, data
) : -1;
499 __private_extern__ OSStatus
500 _SecTrustedApplicationCreateFromPath(const char *path
, SecTrustedApplicationRef
*app
)
502 #undef SecTrustedApplicationCreateFromPath
503 static typeof (SecTrustedApplicationCreateFromPath
) *dyfunc
= NULL
;
505 void *image
= __loadSecurity();
506 if (image
) dyfunc
= dlsym(image
, "SecTrustedApplicationCreateFromPath");
508 return dyfunc
? dyfunc(path
, app
) : -1;
511 #else // TARGET_OS_IPHONE
513 SECURITY_FRAMEWORK_EXTERN(CFStringRef
, kSecPropertyKeyValue
)
514 SECURITY_FRAMEWORK_EXTERN(CFStringRef
, kSecPropertyKeyLabel
)
516 __private_extern__ CFArrayRef
517 _SecCertificateCopyProperties(SecCertificateRef certRef
)
519 #undef SecCertificateCopyProperties
520 static typeof (SecCertificateCopyProperties
) *dyfunc
= NULL
;
522 void *image
= __loadSecurity();
523 if (image
) dyfunc
= dlsym(image
, "SecCertificateCopyProperties");
525 return dyfunc
? dyfunc(certRef
) : NULL
;
528 #endif // TARGET_OS_IPHONE
530 __private_extern__ SecCertificateRef
531 _SecCertificateCreateWithData(CFAllocatorRef allocator
, CFDataRef data
)
533 #undef SecCertificateCreateWithData
534 static typeof (SecCertificateCreateWithData
) *dyfunc
= NULL
;
536 void *image
= __loadSecurity();
537 if (image
) dyfunc
= dlsym(image
, "SecCertificateCreateWithData");
539 return dyfunc
? dyfunc(allocator
, data
) : NULL
;