2 * Copyright (c) 2002-2008, 2010-2013 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"
41 #pragma mark IOKit.framework APIs
45 static void *image
= NULL
;
47 const char *framework
= "/System/Library/Frameworks/IOKit.framework/IOKit";
49 const char *suffix
= getenv("DYLD_IMAGE_SUFFIX");
50 char path
[MAXPATHLEN
];
52 strlcpy(path
, framework
, sizeof(path
));
53 if (suffix
) strlcat(path
, suffix
, sizeof(path
));
54 if (0 <= stat(path
, &statbuf
)) {
55 image
= dlopen(path
, RTLD_LAZY
| RTLD_LOCAL
);
57 image
= dlopen(framework
, RTLD_LAZY
| RTLD_LOCAL
);
64 __private_extern__ CFMutableDictionaryRef
65 _IOBSDNameMatching(mach_port_t masterPort
, uint32_t options
, const char *bsdName
)
67 #undef IOBSDNameMatching
68 static typeof (IOBSDNameMatching
) *dyfunc
= NULL
;
70 void *image
= __loadIOKit();
71 if (image
) dyfunc
= dlsym(image
, "IOBSDNameMatching");
73 return dyfunc
? dyfunc(masterPort
, options
, bsdName
) : NULL
;
77 __private_extern__ io_object_t
78 _IOIteratorNext(io_iterator_t iterator
)
81 static typeof (IOIteratorNext
) *dyfunc
= NULL
;
83 void *image
= __loadIOKit();
84 if (image
) dyfunc
= dlsym(image
, "IOIteratorNext");
86 return dyfunc
? dyfunc(iterator
) : 0;
90 __private_extern__ kern_return_t
91 _IOMasterPort(mach_port_t bootstrapPort
, mach_port_t
*masterPort
)
94 static typeof (IOMasterPort
) *dyfunc
= NULL
;
96 void *image
= __loadIOKit();
97 if (image
) dyfunc
= dlsym(image
, "IOMasterPort");
99 return dyfunc
? dyfunc(bootstrapPort
, masterPort
) : KERN_FAILURE
;
103 __private_extern__ boolean_t
104 _IOObjectConformsTo(io_object_t object
, const io_name_t className
)
106 #undef IOObjectConformsTo
107 static typeof (IOObjectConformsTo
) *dyfunc
= NULL
;
109 void *image
= __loadIOKit();
110 if (image
) dyfunc
= dlsym(image
, "IOObjectConformsTo");
112 return dyfunc
? dyfunc(object
, className
) : FALSE
;
116 __private_extern__ boolean_t
117 _IOObjectGetClass(io_object_t object
, io_name_t className
)
119 #undef IOObjectGetClass
120 static typeof (IOObjectGetClass
) *dyfunc
= NULL
;
122 void *image
= __loadIOKit();
123 if (image
) dyfunc
= dlsym(image
, "IOObjectGetClass");
125 return dyfunc
? dyfunc(object
, className
) : FALSE
;
129 __private_extern__ kern_return_t
130 _IOObjectRelease(io_object_t object
)
132 #undef IOObjectRelease
133 static typeof (IOObjectRelease
) *dyfunc
= NULL
;
135 void *image
= __loadIOKit();
136 if (image
) dyfunc
= dlsym(image
, "IOObjectRelease");
138 return dyfunc
? dyfunc(object
) : KERN_FAILURE
;
142 __private_extern__ CFTypeRef
143 _IORegistryEntryCreateCFProperty(io_registry_entry_t entry
, CFStringRef key
, CFAllocatorRef allocator
, IOOptionBits options
)
145 #undef IORegistryEntryCreateCFProperty
146 static typeof (IORegistryEntryCreateCFProperty
) *dyfunc
= NULL
;
148 void *image
= __loadIOKit();
149 if (image
) dyfunc
= dlsym(image
, "IORegistryEntryCreateCFProperty");
151 return dyfunc
? dyfunc(entry
, key
, allocator
, options
) : NULL
;
155 __private_extern__ kern_return_t
156 _IORegistryEntryCreateCFProperties(io_registry_entry_t entry
, CFMutableDictionaryRef
*properties
, CFAllocatorRef allocator
, IOOptionBits options
)
158 #undef IORegistryEntryCreateCFProperties
159 static typeof (IORegistryEntryCreateCFProperties
) *dyfunc
= NULL
;
161 void *image
= __loadIOKit();
162 if (image
) dyfunc
= dlsym(image
, "IORegistryEntryCreateCFProperties");
164 return dyfunc
? dyfunc(entry
, properties
, allocator
, options
) : KERN_FAILURE
;
168 __private_extern__ kern_return_t
169 _IORegistryEntryCreateIterator(mach_port_t masterPort
, const io_name_t plane
, IOOptionBits options
, io_iterator_t
*iterator
)
171 #undef IORegistryEntryCreateIterator
172 static typeof (IORegistryEntryCreateIterator
) *dyfunc
= NULL
;
174 void *image
= __loadIOKit();
175 if (image
) dyfunc
= dlsym(image
, "IORegistryEntryCreateIterator");
177 return dyfunc
? dyfunc(masterPort
, plane
, options
, iterator
) : KERN_FAILURE
;
181 __private_extern__ kern_return_t
182 _IORegistryEntryGetLocationInPlane(io_registry_entry_t entry
, const io_name_t plane
, io_name_t location
)
184 #undef IORegistryEntryGetLocationInPlane
185 static typeof (IORegistryEntryGetLocationInPlane
) *dyfunc
= NULL
;
187 void *image
= __loadIOKit();
188 if (image
) dyfunc
= dlsym(image
, "IORegistryEntryGetLocationInPlane");
190 return dyfunc
? dyfunc(entry
, plane
, location
) : KERN_FAILURE
;
194 __private_extern__ kern_return_t
195 _IORegistryEntryGetName(io_registry_entry_t entry
, io_name_t name
)
197 #undef IORegistryEntryGetName
198 static typeof (IORegistryEntryGetName
) *dyfunc
= NULL
;
200 void *image
= __loadIOKit();
201 if (image
) dyfunc
= dlsym(image
, "IORegistryEntryGetName");
203 return dyfunc
? dyfunc(entry
, name
) : KERN_FAILURE
;
207 __private_extern__ kern_return_t
208 _IORegistryEntryGetNameInPlane(io_registry_entry_t entry
, const io_name_t plane
, io_name_t name
)
210 #undef IORegistryEntryGetNameInPlane
211 static typeof (IORegistryEntryGetNameInPlane
) *dyfunc
= NULL
;
213 void *image
= __loadIOKit();
214 if (image
) dyfunc
= dlsym(image
, "IORegistryEntryGetNameInPlane");
216 return dyfunc
? dyfunc(entry
, plane
, name
) : KERN_FAILURE
;
220 __private_extern__ kern_return_t
221 _IORegistryEntryGetParentEntry(io_registry_entry_t entry
, const io_name_t plane
, io_registry_entry_t
*parent
)
223 #undef IORegistryEntryGetParentEntry
224 static typeof (IORegistryEntryGetParentEntry
) *dyfunc
= NULL
;
226 void *image
= __loadIOKit();
227 if (image
) dyfunc
= dlsym(image
, "IORegistryEntryGetParentEntry");
229 return dyfunc
? dyfunc(entry
, plane
, parent
) : KERN_FAILURE
;
233 __private_extern__ kern_return_t
234 _IORegistryEntryGetPath(io_registry_entry_t entry
, const io_name_t plane
, io_string_t path
)
236 #undef IORegistryEntryGetPath
237 static typeof (IORegistryEntryGetPath
) *dyfunc
= NULL
;
239 void *image
= __loadIOKit();
240 if (image
) dyfunc
= dlsym(image
, "IORegistryEntryGetPath");
242 return dyfunc
? dyfunc(entry
, plane
, path
) : KERN_FAILURE
;
246 __private_extern__ kern_return_t
247 _IORegistryEntryGetRegistryEntryID(io_registry_entry_t entry
, uint64_t *entryID
)
249 #undef IORegistryEntryGetRegistryEntryID
250 static typeof (IORegistryEntryGetRegistryEntryID
) *dyfunc
= NULL
;
252 void *image
= __loadIOKit();
253 if (image
) dyfunc
= dlsym(image
, "IORegistryEntryGetRegistryEntryID");
255 return dyfunc
? dyfunc(entry
, entryID
) : KERN_FAILURE
;
259 __private_extern__ CFTypeRef
260 _IORegistryEntrySearchCFProperty(io_registry_entry_t entry
, const io_name_t plane
, CFStringRef key
, CFAllocatorRef allocator
, IOOptionBits options
)
262 #undef IORegistryEntrySearchCFProperty
263 static typeof (IORegistryEntrySearchCFProperty
) *dyfunc
= NULL
;
265 void *image
= __loadIOKit();
266 if (image
) dyfunc
= dlsym(image
, "IORegistryEntrySearchCFProperty");
268 return dyfunc
? dyfunc(entry
, plane
, key
, allocator
, options
) : NULL
;
272 __private_extern__ kern_return_t
273 _IOServiceGetMatchingServices(mach_port_t masterPort
, CFDictionaryRef matching
, io_iterator_t
*existing
)
275 #undef IOServiceGetMatchingServices
276 static typeof (IOServiceGetMatchingServices
) *dyfunc
= NULL
;
278 void *image
= __loadIOKit();
279 if (image
) dyfunc
= dlsym(image
, "IOServiceGetMatchingServices");
281 return dyfunc
? dyfunc(masterPort
, matching
, existing
) : KERN_FAILURE
;
285 __private_extern__ CFMutableDictionaryRef
286 _IOServiceMatching(const char *name
)
288 #undef IOServiceMatching
289 static typeof (IOServiceMatching
) *dyfunc
= NULL
;
291 void *image
= __loadIOKit();
292 if (image
) dyfunc
= dlsym(image
, "IOServiceMatching");
294 return dyfunc
? dyfunc(name
) : NULL
;
298 #pragma mark Security.framework APIs
301 __loadSecurity(void) {
302 static void *image
= NULL
;
304 const char *framework
= "/System/Library/Frameworks/Security.framework/Security";
306 const char *suffix
= getenv("DYLD_IMAGE_SUFFIX");
307 char path
[MAXPATHLEN
];
309 strlcpy(path
, framework
, sizeof(path
));
310 if (suffix
) strlcat(path
, suffix
, sizeof(path
));
311 if (0 <= stat(path
, &statbuf
)) {
312 image
= dlopen(path
, RTLD_LAZY
| RTLD_LOCAL
);
314 image
= dlopen(framework
, RTLD_LAZY
| RTLD_LOCAL
);
317 return (void *)image
;
320 #define SECURITY_FRAMEWORK_EXTERN(t, s) \
321 __private_extern__ t \
324 static t *dysym = NULL; \
326 void *image = __loadSecurity(); \
327 if (image) dysym = dlsym(image, #s ); \
329 return (dysym != NULL) ? *dysym : NULL; \
332 #if !TARGET_OS_IPHONE
333 SECURITY_FRAMEWORK_EXTERN(CFTypeRef
, kSecAttrService
)
334 SECURITY_FRAMEWORK_EXTERN(CFTypeRef
, kSecClass
)
335 SECURITY_FRAMEWORK_EXTERN(CFTypeRef
, kSecClassGenericPassword
)
336 SECURITY_FRAMEWORK_EXTERN(CFTypeRef
, kSecMatchLimit
)
337 SECURITY_FRAMEWORK_EXTERN(CFTypeRef
, kSecMatchLimitAll
)
338 SECURITY_FRAMEWORK_EXTERN(CFTypeRef
, kSecMatchSearchList
)
339 SECURITY_FRAMEWORK_EXTERN(CFTypeRef
, kSecReturnRef
)
340 SECURITY_FRAMEWORK_EXTERN(CFTypeRef
, kSecGuestAttributePid
)
341 SECURITY_FRAMEWORK_EXTERN(CFTypeRef
, kSecCodeInfoIdentifier
)
342 SECURITY_FRAMEWORK_EXTERN(CFTypeRef
, kSecCodeInfoUnique
)
344 __private_extern__ OSStatus
345 _AuthorizationMakeExternalForm(AuthorizationRef authorization
, AuthorizationExternalForm
*extForm
)
347 #undef AuthorizationMakeExternalForm
348 static typeof (AuthorizationMakeExternalForm
) *dyfunc
= NULL
;
350 void *image
= __loadSecurity();
351 if (image
) dyfunc
= dlsym(image
, "AuthorizationMakeExternalForm");
353 return dyfunc
? dyfunc(authorization
, extForm
) : -1;
356 __private_extern__ OSStatus
357 _SecAccessCreate(CFStringRef descriptor
, CFArrayRef trustedlist
, SecAccessRef
*accessRef
)
359 #undef SecAccessCreate
360 static typeof (SecAccessCreate
) *dyfunc
= NULL
;
362 void *image
= __loadSecurity();
363 if (image
) dyfunc
= dlsym(image
, "SecAccessCreate");
365 return dyfunc
? dyfunc(descriptor
, trustedlist
, accessRef
) : -1;
368 #if (__MAC_OS_X_VERSION_MIN_REQUIRED < 1070)
369 __private_extern__ OSStatus
370 _SecAccessCreateFromOwnerAndACL(const CSSM_ACL_OWNER_PROTOTYPE
*owner
, uint32 aclCount
, const CSSM_ACL_ENTRY_INFO
*acls
, SecAccessRef
*accessRef
)
372 #undef SecAccessCreateFromOwnerAndACL
373 static typeof (SecAccessCreateFromOwnerAndACL
) *dyfunc
= NULL
;
375 void *image
= __loadSecurity();
376 if (image
) dyfunc
= dlsym(image
, "SecAccessCreateFromOwnerAndACL");
378 return dyfunc
? dyfunc(owner
, aclCount
, acls
, accessRef
) : -1;
380 #else // (__MAC_OS_X_VERSION_MIN_REQUIRED < 1070)
381 __private_extern__ SecAccessRef
382 _SecAccessCreateWithOwnerAndACL(uid_t userId
, gid_t groupId
, SecAccessOwnerType ownerType
, CFArrayRef acls
, CFErrorRef
*error
)
384 #undef SecAccessCreateWithOwnerAndACL
385 static typeof (SecAccessCreateWithOwnerAndACL
) *dyfunc
= NULL
;
387 void *image
= __loadSecurity();
388 if (image
) dyfunc
= dlsym(image
, "SecAccessCreateWithOwnerAndACL");
390 return dyfunc
? dyfunc(userId
, groupId
, ownerType
, acls
, error
) : NULL
;
392 #endif // (__MAC_OS_X_VERSION_MIN_REQUIRED < 1070)
394 __private_extern__ OSStatus
395 _SecItemCopyMatching(CFDictionaryRef query
, CFTypeRef
*result
)
397 #undef SecItemCopyMatching
398 static typeof (SecItemCopyMatching
) *dyfunc
= NULL
;
400 void *image
= __loadSecurity();
401 if (image
) dyfunc
= dlsym(image
, "SecItemCopyMatching");
403 return dyfunc
? dyfunc(query
, result
) : -1;
406 __private_extern__ OSStatus
407 _SecKeychainCopyDomainDefault(SecPreferencesDomain domain
, SecKeychainRef
*keychain
)
409 #undef SecKeychainCopyDomainDefault
410 static typeof (SecKeychainCopyDomainDefault
) *dyfunc
= NULL
;
412 void *image
= __loadSecurity();
413 if (image
) dyfunc
= dlsym(image
, "SecKeychainCopyDomainDefault");
415 return dyfunc
? dyfunc(domain
, keychain
) : -1;
418 __private_extern__ OSStatus
419 _SecKeychainGetPreferenceDomain(SecPreferencesDomain
*domain
)
421 #undef SecKeychainGetPreferenceDomain
422 static typeof (SecKeychainGetPreferenceDomain
) *dyfunc
= NULL
;
424 void *image
= __loadSecurity();
425 if (image
) dyfunc
= dlsym(image
, "SecKeychainGetPreferenceDomain");
427 return dyfunc
? dyfunc(domain
) : -1;
430 __private_extern__ OSStatus
431 _SecKeychainOpen(const char *pathName
, SecKeychainRef
*keychain
)
433 #undef SecKeychainOpen
434 static typeof (SecKeychainOpen
) *dyfunc
= NULL
;
436 void *image
= __loadSecurity();
437 if (image
) dyfunc
= dlsym(image
, "SecKeychainOpen");
439 return dyfunc
? dyfunc(pathName
, keychain
) : -1;
442 __private_extern__ OSStatus
443 _SecKeychainSetDomainDefault(SecPreferencesDomain domain
, SecKeychainRef keychain
)
445 #undef SecKeychainSetDomainDefault
446 static typeof (SecKeychainSetDomainDefault
) *dyfunc
= NULL
;
448 void *image
= __loadSecurity();
449 if (image
) dyfunc
= dlsym(image
, "SecKeychainSetDomainDefault");
451 return dyfunc
? dyfunc(domain
, keychain
) : -1;
454 __private_extern__ OSStatus
455 _SecKeychainSetPreferenceDomain(SecPreferencesDomain domain
)
457 #undef SecKeychainSetPreferenceDomain
458 static typeof (SecKeychainSetPreferenceDomain
) *dyfunc
= NULL
;
460 void *image
= __loadSecurity();
461 if (image
) dyfunc
= dlsym(image
, "SecKeychainSetPreferenceDomain");
463 return dyfunc
? dyfunc(domain
) : -1;
466 __private_extern__ OSStatus
467 _SecKeychainItemCopyContent(SecKeychainItemRef itemRef
, SecItemClass
*itemClass
, SecKeychainAttributeList
*attrList
, UInt32
*length
, void **outData
)
469 #undef SecKeychainItemCopyContent
470 static typeof (SecKeychainItemCopyContent
) *dyfunc
= NULL
;
472 void *image
= __loadSecurity();
473 if (image
) dyfunc
= dlsym(image
, "SecKeychainItemCopyContent");
475 return dyfunc
? dyfunc(itemRef
, itemClass
, attrList
, length
, outData
) : -1;
478 __private_extern__ OSStatus
479 _SecKeychainItemCreateFromContent(SecItemClass itemClass
, SecKeychainAttributeList
*attrList
, UInt32 length
, const void *data
, SecKeychainRef keychainRef
, SecAccessRef initialAccess
, SecKeychainItemRef
*itemRef
)
481 #undef SecKeychainItemCreateFromContent
482 static typeof (SecKeychainItemCreateFromContent
) *dyfunc
= NULL
;
484 void *image
= __loadSecurity();
485 if (image
) dyfunc
= dlsym(image
, "SecKeychainItemCreateFromContent");
487 return dyfunc
? dyfunc(itemClass
, attrList
, length
, data
, keychainRef
, initialAccess
, itemRef
) : -1;
490 __private_extern__ OSStatus
491 _SecKeychainItemDelete(SecKeychainItemRef itemRef
)
493 #undef SecKeychainItemDelete
494 static typeof (SecKeychainItemDelete
) *dyfunc
= NULL
;
496 void *image
= __loadSecurity();
497 if (image
) dyfunc
= dlsym(image
, "SecKeychainItemDelete");
499 return dyfunc
? dyfunc(itemRef
) : -1;
502 __private_extern__ OSStatus
503 _SecKeychainItemFreeContent(SecKeychainAttributeList
*attrList
, void *data
)
505 #undef SecKeychainItemFreeContent
506 static typeof (SecKeychainItemFreeContent
) *dyfunc
= NULL
;
508 void *image
= __loadSecurity();
509 if (image
) dyfunc
= dlsym(image
, "SecKeychainItemFreeContent");
511 return dyfunc
? dyfunc(attrList
, data
) : -1;
514 __private_extern__ OSStatus
515 _SecKeychainItemModifyContent(SecKeychainItemRef itemRef
, const SecKeychainAttributeList
*attrList
, UInt32 length
, const void *data
)
517 #undef SecKeychainItemModifyContent
518 static typeof (SecKeychainItemModifyContent
) *dyfunc
= NULL
;
520 void *image
= __loadSecurity();
521 if (image
) dyfunc
= dlsym(image
, "SecKeychainItemModifyContent");
523 return dyfunc
? dyfunc(itemRef
, attrList
, length
, data
) : -1;
527 __private_extern__ OSStatus
528 _SecTrustedApplicationCreateFromPath(const char *path
, SecTrustedApplicationRef
*app
)
530 #undef SecTrustedApplicationCreateFromPath
531 static typeof (SecTrustedApplicationCreateFromPath
) *dyfunc
= NULL
;
533 void *image
= __loadSecurity();
534 if (image
) dyfunc
= dlsym(image
, "SecTrustedApplicationCreateFromPath");
536 return dyfunc
? dyfunc(path
, app
) : -1;
539 #else // TARGET_OS_IPHONE
541 SECURITY_FRAMEWORK_EXTERN(CFStringRef
, kSecPropertyKeyValue
)
542 SECURITY_FRAMEWORK_EXTERN(CFStringRef
, kSecPropertyKeyLabel
)
544 __private_extern__ CFArrayRef
545 _SecCertificateCopyProperties(SecCertificateRef certRef
)
547 #undef SecCertificateCopyProperties
548 static typeof (SecCertificateCopyProperties
) *dyfunc
= NULL
;
550 void *image
= __loadSecurity();
551 if (image
) dyfunc
= dlsym(image
, "SecCertificateCopyProperties");
553 return dyfunc
? dyfunc(certRef
) : NULL
;
556 #endif // TARGET_OS_IPHONE
558 __private_extern__ SecCertificateRef
559 _SecCertificateCreateWithData(CFAllocatorRef allocator
, CFDataRef data
)
561 #undef SecCertificateCreateWithData
562 static typeof (SecCertificateCreateWithData
) *dyfunc
= NULL
;
564 void *image
= __loadSecurity();
565 if (image
) dyfunc
= dlsym(image
, "SecCertificateCreateWithData");
567 return dyfunc
? dyfunc(allocator
, data
) : NULL
;