]>
git.saurik.com Git - apple/security.git/blob - OSX/libsecurity_keychain/lib/SecBridge.h
2 * Copyright (c) 2000-2004,2011,2013-2016 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@
24 #ifndef _SECURITY_SECBRIDGE_H_
25 #define _SECURITY_SECBRIDGE_H_
27 #include <security_keychain/Globals.h>
28 #include <security_keychain/SecCFTypes.h>
29 #include <Security/SecBasePriv.h>
30 #include <Security/SecKeychainPriv.h>
31 #include <security_keychain/KCUtilities.h>
32 #include <security_cdsa_utilities/cssmbridge.h>
33 #include "LegacyAPICounts.h"
35 using namespace KeychainCore
;
37 #define COUNTLEGACYAPI static dispatch_once_t countToken; \
38 countLegacyAPI(&countToken, __FUNCTION__);
41 // API boilerplate macros. These provide a frame for C++ code that is impermeable to exceptions.
44 // ... your C++ code here ...
45 // END_API // returns CSSM_RETURN on exception
46 // END_API0 // returns nothing (void) on exception
47 // END_API1(bad) // return (bad) on exception
48 // END_API2(name) // like END_API, with API name as debug scope for printing function result
49 // END_API3(name, bad) // like END_API1, with API name as debug scope for printing function result
51 #define BEGIN_SECAPI \
52 OSStatus __secapiresult = errSecSuccess; \
53 static dispatch_once_t countToken; \
54 countLegacyAPI(&countToken, __FUNCTION__); \
57 catch (const MacOSError &err) { __secapiresult=err.osStatus(); } \
58 catch (const CommonError &err) { __secapiresult=SecKeychainErrFromOSStatus(err.osStatus()); } \
59 catch (const std::bad_alloc &) { __secapiresult=errSecAllocate; } \
60 catch (...) { __secapiresult=errSecInternalComponent; } \
61 return __secapiresult;
62 #define END_SECAPI1(BAD_RETURN_VAL) \
66 __secapiresult=BAD_RETURN_VAL; \
68 return __secapiresult;
69 #define END_SECAPI1(BAD_RETURN_VAL) }\
70 catch (...) { __secapiresult=BAD_RETURN_VAL; } \
71 return __secapiresult;
72 #define END_SECAPI0 }\
73 catch (...) { return; }
78 // Note: this macro assumes an input parameter named "itemRef"
80 #define BEGIN_SECKCITEMAPI \
81 OSStatus __secapiresult=errSecSuccess; \
82 static dispatch_once_t countToken; \
83 countLegacyAPI(&countToken, __FUNCTION__); \
84 SecKeychainItemRef __itemImplRef=NULL; \
85 bool __is_certificate=(itemRef && (CFGetTypeID(itemRef) == SecCertificateGetTypeID())); \
86 if (__is_certificate) { \
87 if (SecCertificateIsItemImplInstance((SecCertificateRef)itemRef)) { \
88 __itemImplRef=(SecKeychainItemRef)CFRetain(itemRef); \
90 __itemImplRef=(SecKeychainItemRef)SecCertificateCopyKeychainItem((SecCertificateRef)itemRef); \
91 if (!__itemImplRef) { \
92 __itemImplRef=(SecKeychainItemRef)SecCertificateCreateItemImplInstance((SecCertificateRef)itemRef); \
93 (void)SecCertificateSetKeychainItem((SecCertificateRef)itemRef,__itemImplRef); \
97 __itemImplRef=(SecKeychainItemRef)((itemRef) ? CFRetain(itemRef) : NULL); \
104 #define END_SECKCITEMAPI } \
105 catch (const MacOSError &err) { __secapiresult=err.osStatus(); } \
106 catch (const CommonError &err) { __secapiresult=SecKeychainErrFromOSStatus(err.osStatus()); } \
107 catch (const std::bad_alloc &) { __secapiresult=errSecAllocate; } \
108 catch (...) { __secapiresult=errSecInternalComponent; } \
109 if (__itemImplRef) { CFRelease(__itemImplRef); } \
110 return __secapiresult;
115 // Note: this macro assumes an input parameter named "certificate"
117 #define BEGIN_SECCERTAPI \
118 OSStatus __secapiresult=errSecSuccess; \
119 static dispatch_once_t countToken; \
120 countLegacyAPI(&countToken, __FUNCTION__); \
121 SecCertificateRef __itemImplRef=NULL; \
122 if (SecCertificateIsItemImplInstance(certificate)) { __itemImplRef=(SecCertificateRef)CFRetain(certificate); } \
123 if (!__itemImplRef && certificate) { __itemImplRef=(SecCertificateRef)SecCertificateCopyKeychainItem(certificate); } \
124 if (!__itemImplRef && certificate) { __itemImplRef=SecCertificateCreateItemImplInstance(certificate); \
125 (void)SecCertificateSetKeychainItem(certificate,__itemImplRef); } \
131 #define END_SECCERTAPI } \
132 catch (const MacOSError &err) { __secapiresult=err.osStatus(); } \
133 catch (const CommonError &err) { __secapiresult=SecKeychainErrFromOSStatus(err.osStatus()); } \
134 catch (const std::bad_alloc &) { __secapiresult=errSecAllocate; } \
135 catch (...) { __secapiresult=errSecInternalComponent; } \
136 if (__itemImplRef) { CFRelease(__itemImplRef); } \
137 return __secapiresult;
143 #define BEGIN_SECKEYAPI(resultType, resultInit) \
144 resultType result = resultInit; try {
146 extern "C" bool SecError(OSStatus status
, CFErrorRef
*error
, CFStringRef format
, ...);
148 #define END_SECKEYAPI }\
149 catch (const MacOSError &err) { SecError(err.osStatus(), error, CFSTR("%s"), err.what()); result = NULL; } \
150 catch (const CommonError &err) { \
151 if (err.osStatus() != CSSMERR_CSP_INVALID_DIGEST_ALGORITHM) { \
152 OSStatus status = SecKeychainErrFromOSStatus(err.osStatus()); if (status == errSecInputLengthError) status = errSecParam; \
153 SecError(status, error, CFSTR("%s"), err.what()); result = NULL; } \
155 catch (const std::bad_alloc &) { SecError(errSecAllocate, error, CFSTR("allocation failed")); result = NULL; } \
156 catch (...) { SecError(errSecInternalComponent, error, CFSTR("internal error")); result = NULL; } \
159 #endif /* !_SECURITY_SECBRIDGE_H_ */