]> git.saurik.com Git - apple/security.git/blob - OSX/libsecurity_keychain/lib/SecBridge.h
Security-57740.1.18.tar.gz
[apple/security.git] / OSX / libsecurity_keychain / lib / SecBridge.h
1 /*
2 * Copyright (c) 2000-2004,2011,2013-2016 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 #ifndef _SECURITY_SECBRIDGE_H_
25 #define _SECURITY_SECBRIDGE_H_
26
27 #include <security_keychain/Globals.h>
28 #include <security_keychain/SecCFTypes.h>
29 #include "SecBasePriv.h"
30 #include <Security/SecKeychainPriv.h>
31 #include <security_keychain/KCUtilities.h>
32 #include <security_cdsa_utilities/cssmbridge.h>
33
34 using namespace KeychainCore;
35
36 //
37 // API boilerplate macros. These provide a frame for C++ code that is impermeable to exceptions.
38 // Usage:
39 // BEGIN_API
40 // ... your C++ code here ...
41 // END_API // returns CSSM_RETURN on exception
42 // END_API0 // returns nothing (void) on exception
43 // END_API1(bad) // return (bad) on exception
44 // END_API2(name) // like END_API, with API name as debug scope for printing function result
45 // END_API3(name, bad) // like END_API1, with API name as debug scope for printing function result
46 //
47 #define BEGIN_SECAPI \
48 OSStatus __secapiresult = errSecSuccess; \
49 try {
50 #define END_SECAPI }\
51 catch (const MacOSError &err) { __secapiresult=err.osStatus(); } \
52 catch (const CommonError &err) { __secapiresult=SecKeychainErrFromOSStatus(err.osStatus()); } \
53 catch (const std::bad_alloc &) { __secapiresult=errSecAllocate; } \
54 catch (...) { __secapiresult=errSecInternalComponent; } \
55 return __secapiresult;
56 #define END_SECAPI1(BAD_RETURN_VAL) \
57 } \
58 catch (...) \
59 { \
60 __secapiresult=BAD_RETURN_VAL; \
61 } \
62 return __secapiresult;
63 #define END_SECAPI1(BAD_RETURN_VAL) }\
64 catch (...) { __secapiresult=BAD_RETURN_VAL; } \
65 return __secapiresult;
66 #define END_SECAPI0 }\
67 catch (...) { return; }
68
69
70 //
71 // BEGIN_SECKCITEMAPI
72 // Note: this macro assumes an input parameter named "itemRef"
73 //
74 #if SECTRUST_OSX
75 #define BEGIN_SECKCITEMAPI \
76 OSStatus __secapiresult=errSecSuccess; \
77 SecKeychainItemRef __itemImplRef=NULL; \
78 bool __is_certificate=(itemRef && (CFGetTypeID(itemRef) == SecCertificateGetTypeID())); \
79 if (__is_certificate) { \
80 if (SecCertificateIsItemImplInstance((SecCertificateRef)itemRef)) { \
81 __itemImplRef=(SecKeychainItemRef)CFRetain(itemRef); \
82 } else { \
83 __itemImplRef=(SecKeychainItemRef)SecCertificateCopyKeychainItem((SecCertificateRef)itemRef); \
84 if (!__itemImplRef) { \
85 __itemImplRef=(SecKeychainItemRef)SecCertificateCreateItemImplInstance((SecCertificateRef)itemRef); \
86 (void)SecCertificateSetKeychainItem((SecCertificateRef)itemRef,__itemImplRef); \
87 } \
88 } \
89 } else { \
90 __itemImplRef=(SecKeychainItemRef)((itemRef) ? CFRetain(itemRef) : NULL); \
91 } \
92 try {
93 #else
94 #define BEGIN_SECKCITEMAPI \
95 OSStatus __secapiresult=errSecSuccess; \
96 SecKeychainItemRef __itemImplRef=(SecKeychainItemRef)((itemRef) ? CFRetain(itemRef) : NULL); \
97 try {
98 #endif
99 //
100 // END_SECKCITEMAPI
101 //
102 #define END_SECKCITEMAPI } \
103 catch (const MacOSError &err) { __secapiresult=err.osStatus(); } \
104 catch (const CommonError &err) { __secapiresult=SecKeychainErrFromOSStatus(err.osStatus()); } \
105 catch (const std::bad_alloc &) { __secapiresult=errSecAllocate; } \
106 catch (...) { __secapiresult=errSecInternalComponent; } \
107 if (__itemImplRef) { CFRelease(__itemImplRef); } \
108 return __secapiresult;
109
110
111 //
112 // BEGIN_SECCERTAPI
113 // Note: this macro assumes an input parameter named "certificate"
114 //
115 #if SECTRUST_OSX
116 #define BEGIN_SECCERTAPI \
117 OSStatus __secapiresult=errSecSuccess; \
118 SecCertificateRef __itemImplRef=NULL; \
119 if (SecCertificateIsItemImplInstance(certificate)) { __itemImplRef=(SecCertificateRef)CFRetain(certificate); } \
120 if (!__itemImplRef && certificate) { __itemImplRef=(SecCertificateRef)SecCertificateCopyKeychainItem(certificate); } \
121 if (!__itemImplRef && certificate) { __itemImplRef=SecCertificateCreateItemImplInstance(certificate); \
122 (void)SecCertificateSetKeychainItem(certificate,__itemImplRef); } \
123 try {
124 #else
125 #define BEGIN_SECCERTAPI \
126 OSStatus __secapiresult=errSecSuccess; \
127 SecCertificateRef __itemImplRef=(SecCertificateRef)((certificate)?CFRetain(certificate):NULL); \
128 try {
129 #endif
130 //
131 // END_SECCERTAPI
132 //
133 #define END_SECCERTAPI } \
134 catch (const MacOSError &err) { __secapiresult=err.osStatus(); } \
135 catch (const CommonError &err) { __secapiresult=SecKeychainErrFromOSStatus(err.osStatus()); } \
136 catch (const std::bad_alloc &) { __secapiresult=errSecAllocate; } \
137 catch (...) { __secapiresult=errSecInternalComponent; } \
138 if (__itemImplRef) { CFRelease(__itemImplRef); } \
139 return __secapiresult;
140
141
142 //
143 // BEGIN_SECKEYAPI
144 //
145 #define BEGIN_SECKEYAPI(resultType, resultInit) \
146 resultType result = resultInit; try {
147
148 extern "C" bool SecError(OSStatus status, CFErrorRef *error, CFStringRef format, ...);
149
150 #define END_SECKEYAPI }\
151 catch (const MacOSError &err) { SecError(err.osStatus(), error, CFSTR("%s"), err.what()); result = NULL; } \
152 catch (const CommonError &err) { SecError(SecKeychainErrFromOSStatus(err.osStatus()), error, CFSTR("%s"), err.what()); result = NULL; } \
153 catch (const std::bad_alloc &) { SecError(errSecAllocate, error, CFSTR("allocation failed")); result = NULL; } \
154 catch (...) { SecError(errSecInternalComponent, error, CFSTR("internal error")); result = NULL; } \
155 return result;
156
157 #endif /* !_SECURITY_SECBRIDGE_H_ */