2 * Copyright (c) 2012-2014 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 #ifndef _SECCFERROR_H_
26 #define _SECCFERROR_H_
28 #include <CoreFoundation/CoreFoundation.h>
29 #include "utilities/SecCFRelease.h"
32 // Leaf error creation from other systems
35 // kern_return_t errors
36 #define kSecKernDomain kCFErrorDomainMach
37 bool SecKernError(kern_return_t result
, CFErrorRef
*error
, CFStringRef format
, ...)
38 CF_FORMAT_FUNCTION(3, 4);
41 #define kSecErrnoDomain kCFErrorDomainPOSIX
42 bool SecCheckErrno(int result
, CFErrorRef
*error
, CFStringRef format
, ...)
43 CF_FORMAT_FUNCTION(3, 4);
46 #define kSecErrorDomain kCFErrorDomainOSStatus
47 bool SecError(OSStatus status
, CFErrorRef
*error
, CFStringRef format
, ...)
48 CF_FORMAT_FUNCTION(3, 4);
50 // Direct checking of POSIX errors.
51 bool SecPOSIXError(int error
, CFErrorRef
*cferror
, CFStringRef format
, ...)
52 CF_FORMAT_FUNCTION(3, 4);
55 #define kSecCoreCryptoDomain CFSTR("kSecCoreCryptoDomain")
56 bool SecCoreCryptoError(int error
, CFErrorRef
*cferror
, CFStringRef format
, ...)
57 CF_FORMAT_FUNCTION(3, 4);
60 #define kSecNotifyDomain CFSTR("kSecNotifyDomain")
61 bool SecNotifyError(uint32_t result
, CFErrorRef
*error
, CFStringRef format
, ...)
62 CF_FORMAT_FUNCTION(3, 4);
65 // requirement error, typically parameters
66 bool SecRequirementError(bool requirement
, CFErrorRef
*error
, CFStringRef format
, ...)
67 CF_FORMAT_FUNCTION(3, 4);
70 bool SecAllocationError(const void *allocated
, CFErrorRef
*error
, CFStringRef format
, ...)
71 CF_FORMAT_FUNCTION(3, 4);
74 // Create and chain, all return false to make the analyzer happy.
76 #define SecCFCreateError(errorCode, domain, descriptionString, previousError, newError) \
77 SecCFCreateErrorWithFormat(errorCode, domain, previousError, newError, NULL, descriptionString)
79 bool SecCFCreateErrorWithFormat(CFIndex errorCode
, CFStringRef domain
, CFErrorRef previousError
, CFErrorRef
*newError
,
80 CFDictionaryRef formatoptions
, CFStringRef descriptionString
, ...)
81 CF_FORMAT_FUNCTION(6,7);
84 bool SecCFCreateErrorWithFormatAndArguments(CFIndex errorCode
, CFStringRef domain
,
85 CFErrorRef previousError
, CFErrorRef
*newError
,
86 CFDictionaryRef formatoptions
, CFStringRef descriptionString
, va_list args
)
87 CF_FORMAT_FUNCTION(6, 0);
90 // MARK: Optional value type casting
93 static inline bool asArrayOptional(CFTypeRef cfType
, CFArrayRef
*array
, CFErrorRef
*error
) {
94 if (!cfType
|| CFGetTypeID(cfType
) == CFArrayGetTypeID()) {
95 if (array
) *array
= (CFArrayRef
)cfType
;
98 SecError(-50, error
, CFSTR("object %@ is not an array"), cfType
);
102 static inline bool asDataOptional(CFTypeRef cfType
, CFDataRef
*data
, CFErrorRef
*error
) {
103 if (!cfType
|| CFGetTypeID(cfType
) == CFDataGetTypeID()) {
104 if (data
) *data
= (CFDataRef
)cfType
;
107 SecError(-50, error
, CFSTR("object %@ is not an data"), cfType
);
111 static inline bool asSetOptional(CFTypeRef cfType
, CFSetRef
*set
, CFErrorRef
*error
) {
112 if (!cfType
|| CFGetTypeID(cfType
) == CFSetGetTypeID()) {
113 if (set
) *set
= (CFSetRef
)cfType
;
116 SecError(-50, error
, CFSTR("object %@ is not a set"), cfType
);
121 // MARK: Required value type casting
125 // MARK: Required value type casting
128 static inline CFArrayRef
copyIfArray(CFTypeRef cfType
, CFErrorRef
*error
) {
129 if (cfType
&& CFGetTypeID(cfType
) == CFArrayGetTypeID())
130 return (CFArrayRef
)CFRetainSafe(cfType
);
131 SecError(-50, error
, CFSTR("object %@ is not an array"), cfType
);
135 static inline CFBooleanRef
copyIfBoolean(CFTypeRef cfType
, CFErrorRef
*error
) {
136 if (cfType
&& CFGetTypeID(cfType
) == CFBooleanGetTypeID())
137 return (CFBooleanRef
)CFRetainSafe(cfType
);
138 SecError(-50, error
, CFSTR("object %@ is not an boolean"), cfType
);
142 static inline CFDataRef
copyIfData(CFTypeRef cfType
, CFErrorRef
*error
) {
143 if (cfType
&& CFGetTypeID(cfType
) == CFDataGetTypeID())
144 return (CFDataRef
)CFRetainSafe(cfType
);
145 SecError(-50, error
, CFSTR("object %@ is not a data"), cfType
);
149 static inline CFDateRef
copyIfDate(CFTypeRef cfType
, CFErrorRef
*error
) {
150 if (cfType
&& CFGetTypeID(cfType
) == CFDateGetTypeID())
151 return (CFDateRef
)CFRetainSafe(cfType
);
152 SecError(-50, error
, CFSTR("object %@ is not a date"), cfType
);
156 static inline CFDictionaryRef
copyIfDictionary(CFTypeRef cfType
, CFErrorRef
*error
) {
157 if (cfType
&& CFGetTypeID(cfType
) == CFDictionaryGetTypeID())
158 return (CFDictionaryRef
)CFRetainSafe(cfType
);
159 SecError(-50, error
, CFSTR("object %@ is not a dictionary"), cfType
);
163 static inline CFSetRef
copyIfSet(CFTypeRef cfType
, CFErrorRef
*error
) {
164 if (cfType
&& CFGetTypeID(cfType
) == CFSetGetTypeID())
165 return (CFSetRef
)CFRetainSafe(cfType
);
166 SecError(-50, error
, CFSTR("object %@ is not a set"), cfType
);
170 static inline CFStringRef
copyIfString(CFTypeRef cfType
, CFErrorRef
*error
) {
171 if (cfType
&& CFGetTypeID(cfType
) == CFStringGetTypeID())
172 return (CFStringRef
)CFRetainSafe(cfType
);
173 SecError(-50, error
, CFSTR("object %@ is not a string"), cfType
);
177 static inline CFUUIDRef
copyIfUUID(CFTypeRef cfType
, CFErrorRef
*error
) {
178 if (cfType
&& CFGetTypeID(cfType
) == CFUUIDGetTypeID())
179 return (CFUUIDRef
)CFRetainSafe(cfType
);
180 SecError(-50, error
, CFSTR("object %@ is not a UUID"), cfType
);
185 // MARK: Analyzer confusing asXxx casting
187 static inline CFArrayRef
asArray(CFTypeRef cfType
, CFErrorRef
*error
) {
188 if (cfType
&& CFGetTypeID(cfType
) == CFArrayGetTypeID())
189 return (CFArrayRef
)cfType
;
190 SecError(-50, error
, CFSTR("object %@ is not an array"), cfType
);
194 static inline CFBooleanRef
asBoolean(CFTypeRef cfType
, CFErrorRef
*error
) {
195 if (cfType
&& CFGetTypeID(cfType
) == CFBooleanGetTypeID())
196 return (CFBooleanRef
)cfType
;
197 SecError(-50, error
, CFSTR("object %@ is not an boolean"), cfType
);
201 static inline CFDataRef
asData(CFTypeRef cfType
, CFErrorRef
*error
) {
202 if (cfType
&& CFGetTypeID(cfType
) == CFDataGetTypeID())
203 return (CFDataRef
)cfType
;
204 SecError(-50, error
, CFSTR("object %@ is not a data"), cfType
);
208 static inline CFDateRef
asDate(CFTypeRef cfType
, CFErrorRef
*error
) {
209 if (cfType
&& CFGetTypeID(cfType
) == CFDateGetTypeID())
210 return (CFDateRef
)cfType
;
211 SecError(-50, error
, CFSTR("object %@ is not a date"), cfType
);
215 static inline CFDictionaryRef
asDictionary(CFTypeRef cfType
, CFErrorRef
*error
) {
216 if (cfType
&& CFGetTypeID(cfType
) == CFDictionaryGetTypeID())
217 return (CFDictionaryRef
)cfType
;
218 SecError(-50, error
, CFSTR("object %@ is not a dictionary"), cfType
);
222 static inline CFSetRef
asSet(CFTypeRef cfType
, CFErrorRef
*error
) {
223 if (cfType
&& CFGetTypeID(cfType
) == CFSetGetTypeID())
224 return (CFSetRef
)cfType
;
225 SecError(-50, error
, CFSTR("object %@ is not a set"), cfType
);
229 static inline CFStringRef
asString(CFTypeRef cfType
, CFErrorRef
*error
) {
230 if (cfType
&& CFGetTypeID(cfType
) == CFStringGetTypeID())
231 return (CFStringRef
)cfType
;
232 SecError(-50, error
, CFSTR("object %@ is not a string"), cfType
);
236 static inline CFUUIDRef
asUUID(CFTypeRef cfType
, CFErrorRef
*error
) {
237 if (cfType
&& CFGetTypeID(cfType
) == CFUUIDGetTypeID())
238 return (CFUUIDRef
)cfType
;
239 SecError(-50, error
, CFSTR("object %@ is not a UUID"), cfType
);
243 #endif /* _SECCFERROR_H_ */