]> git.saurik.com Git - apple/security.git/blame - OSX/libsecurity_keychain/lib/SecBridge.h
Security-59754.80.3.tar.gz
[apple/security.git] / OSX / libsecurity_keychain / lib / SecBridge.h
CommitLineData
b1ab9ed8 1/*
fa7225c8 2 * Copyright (c) 2000-2004,2011,2013-2016 Apple Inc. All Rights Reserved.
5c19dc3a 3 *
b1ab9ed8 4 * @APPLE_LICENSE_HEADER_START@
5c19dc3a 5 *
b1ab9ed8
A
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.
5c19dc3a 12 *
b1ab9ed8
A
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.
5c19dc3a 20 *
b1ab9ed8
A
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>
b54c578e 29#include <Security/SecBasePriv.h>
b1ab9ed8
A
30#include <Security/SecKeychainPriv.h>
31#include <security_keychain/KCUtilities.h>
32#include <security_cdsa_utilities/cssmbridge.h>
d64be36e 33#include "LegacyAPICounts.h"
b1ab9ed8
A
34
35using namespace KeychainCore;
36
d64be36e
A
37#define COUNTLEGACYAPI static dispatch_once_t countToken; \
38 countLegacyAPI(&countToken, __FUNCTION__);
39
b1ab9ed8
A
40//
41// API boilerplate macros. These provide a frame for C++ code that is impermeable to exceptions.
42// Usage:
43// BEGIN_API
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
50//
51#define BEGIN_SECAPI \
fa7225c8 52 OSStatus __secapiresult = errSecSuccess; \
d64be36e
A
53 static dispatch_once_t countToken; \
54 countLegacyAPI(&countToken, __FUNCTION__); \
b1ab9ed8
A
55 try {
56#define END_SECAPI }\
57 catch (const MacOSError &err) { __secapiresult=err.osStatus(); } \
58 catch (const CommonError &err) { __secapiresult=SecKeychainErrFromOSStatus(err.osStatus()); } \
427c49bc
A
59 catch (const std::bad_alloc &) { __secapiresult=errSecAllocate; } \
60 catch (...) { __secapiresult=errSecInternalComponent; } \
fa7225c8 61 return __secapiresult;
b1ab9ed8
A
62#define END_SECAPI1(BAD_RETURN_VAL) \
63 } \
64 catch (...) \
65 { \
66 __secapiresult=BAD_RETURN_VAL; \
67 } \
68 return __secapiresult;
69#define END_SECAPI1(BAD_RETURN_VAL) }\
fa7225c8
A
70 catch (...) { __secapiresult=BAD_RETURN_VAL; } \
71 return __secapiresult;
b1ab9ed8 72#define END_SECAPI0 }\
fa7225c8
A
73 catch (...) { return; }
74
75
76//
77// BEGIN_SECKCITEMAPI
78// Note: this macro assumes an input parameter named "itemRef"
79//
fa7225c8
A
80#define BEGIN_SECKCITEMAPI \
81 OSStatus __secapiresult=errSecSuccess; \
d64be36e
A
82 static dispatch_once_t countToken; \
83 countLegacyAPI(&countToken, __FUNCTION__); \
fa7225c8
A
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); \
89 } else { \
90 __itemImplRef=(SecKeychainItemRef)SecCertificateCopyKeychainItem((SecCertificateRef)itemRef); \
91 if (!__itemImplRef) { \
92 __itemImplRef=(SecKeychainItemRef)SecCertificateCreateItemImplInstance((SecCertificateRef)itemRef); \
93 (void)SecCertificateSetKeychainItem((SecCertificateRef)itemRef,__itemImplRef); \
94 } \
95 } \
96 } else { \
97 __itemImplRef=(SecKeychainItemRef)((itemRef) ? CFRetain(itemRef) : NULL); \
98 } \
99 try {
6b200bc3 100
fa7225c8
A
101//
102// END_SECKCITEMAPI
103//
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;
111
b1ab9ed8 112
fa7225c8
A
113//
114// BEGIN_SECCERTAPI
115// Note: this macro assumes an input parameter named "certificate"
116//
5c19dc3a 117#define BEGIN_SECCERTAPI \
fa7225c8 118 OSStatus __secapiresult=errSecSuccess; \
d64be36e
A
119 static dispatch_once_t countToken; \
120 countLegacyAPI(&countToken, __FUNCTION__); \
fa7225c8
A
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); } \
126 try {
6b200bc3 127
fa7225c8
A
128//
129// END_SECCERTAPI
130//
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;
138
139
140//
141// BEGIN_SECKEYAPI
142//
143#define BEGIN_SECKEYAPI(resultType, resultInit) \
144resultType result = resultInit; try {
145
146extern "C" bool SecError(OSStatus status, CFErrorRef *error, CFStringRef format, ...);
5c19dc3a 147
fa7225c8
A
148#define END_SECKEYAPI }\
149catch (const MacOSError &err) { SecError(err.osStatus(), error, CFSTR("%s"), err.what()); result = NULL; } \
b04fe171
A
150catch (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; } \
154 } \
fa7225c8
A
155catch (const std::bad_alloc &) { SecError(errSecAllocate, error, CFSTR("allocation failed")); result = NULL; } \
156catch (...) { SecError(errSecInternalComponent, error, CFSTR("internal error")); result = NULL; } \
157return result;
5c19dc3a 158
b1ab9ed8 159#endif /* !_SECURITY_SECBRIDGE_H_ */