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
, ...);
40 #define kSecErrnoDomain kCFErrorDomainPOSIX
41 bool SecCheckErrno(int result
, CFErrorRef
*error
, CFStringRef format
, ...);
44 #define kSecErrorDomain kCFErrorDomainOSStatus
45 bool SecError(OSStatus status
, CFErrorRef
*error
, CFStringRef format
, ...);
47 // Direct checking of POSIX errors.
48 bool SecPOSIXError(int error
, CFErrorRef
*cferror
, CFStringRef format
, ...);
51 #define kSecCoreCryptoDomain CFSTR("kSecCoreCryptoDomain")
52 bool SecCoreCryptoError(int error
, CFErrorRef
*cferror
, CFStringRef format
, ...);
55 #define kSecNotifyDomain CFSTR("kSecNotifyDomain")
56 bool SecNotifyError(uint32_t result
, CFErrorRef
*error
, CFStringRef format
, ...);
58 // requirement error, typically parameters
59 bool SecRequirementError(bool requirement
, CFErrorRef
*error
, CFStringRef format
, ...);
62 bool SecAllocationError(const void *allocated
, CFErrorRef
*error
, CFStringRef format
, ...);
66 // Create and chain, all return false to make the analyzer happy.
68 bool SecCFCreateError(CFIndex errorCode
, CFStringRef domain
, CFStringRef descriptionString
,
69 CFErrorRef previousError
, CFErrorRef
*newError
);
71 bool SecCFCreateErrorWithFormat(CFIndex errorCode
, CFStringRef domain
, CFErrorRef previousError
, CFErrorRef
*newError
,
72 CFDictionaryRef formatoptions
, CFStringRef descriptionString
, ...)
73 CF_FORMAT_FUNCTION(6,7);
76 bool SecCFCreateErrorWithFormatAndArguments(CFIndex errorCode
, CFStringRef domain
,
77 CFErrorRef previousError
, CFErrorRef
*newError
,
78 CFDictionaryRef formatoptions
, CFStringRef descriptionString
, va_list args
)
79 CF_FORMAT_FUNCTION(6, 0);
82 // MARK: Optional value type casting
85 static inline bool asArrayOptional(CFTypeRef cfType
, CFArrayRef
*array
, CFErrorRef
*error
) {
86 if (!cfType
|| CFGetTypeID(cfType
) == CFArrayGetTypeID()) {
87 if (array
) *array
= (CFArrayRef
)cfType
;
90 SecError(-50, error
, CFSTR("object %@ is not an array"), cfType
);
94 static inline bool asDataOptional(CFTypeRef cfType
, CFDataRef
*data
, CFErrorRef
*error
) {
95 if (!cfType
|| CFGetTypeID(cfType
) == CFDataGetTypeID()) {
96 if (data
) *data
= (CFDataRef
)cfType
;
99 SecError(-50, error
, CFSTR("object %@ is not an data"), cfType
);
103 static inline bool asSetOptional(CFTypeRef cfType
, CFSetRef
*set
, CFErrorRef
*error
) {
104 if (!cfType
|| CFGetTypeID(cfType
) == CFSetGetTypeID()) {
105 if (set
) *set
= (CFSetRef
)cfType
;
108 SecError(-50, error
, CFSTR("object %@ is not a set"), cfType
);
113 // MARK: Required value type casting
117 // MARK: Required value type casting
120 static inline CFArrayRef
copyIfArray(CFTypeRef cfType
, CFErrorRef
*error
) {
121 if (cfType
&& CFGetTypeID(cfType
) == CFArrayGetTypeID())
122 return (CFArrayRef
)CFRetainSafe(cfType
);
123 SecError(-50, error
, CFSTR("object %@ is not an array"), cfType
);
127 static inline CFBooleanRef
copyIfBoolean(CFTypeRef cfType
, CFErrorRef
*error
) {
128 if (cfType
&& CFGetTypeID(cfType
) == CFBooleanGetTypeID())
129 return (CFBooleanRef
)CFRetainSafe(cfType
);
130 SecError(-50, error
, CFSTR("object %@ is not an boolean"), cfType
);
134 static inline CFDataRef
copyIfData(CFTypeRef cfType
, CFErrorRef
*error
) {
135 if (cfType
&& CFGetTypeID(cfType
) == CFDataGetTypeID())
136 return (CFDataRef
)CFRetainSafe(cfType
);
137 SecError(-50, error
, CFSTR("object %@ is not a data"), cfType
);
141 static inline CFDateRef
copyIfDate(CFTypeRef cfType
, CFErrorRef
*error
) {
142 if (cfType
&& CFGetTypeID(cfType
) == CFDateGetTypeID())
143 return (CFDateRef
)CFRetainSafe(cfType
);
144 SecError(-50, error
, CFSTR("object %@ is not a date"), cfType
);
148 static inline CFDictionaryRef
copyIfDictionary(CFTypeRef cfType
, CFErrorRef
*error
) {
149 if (cfType
&& CFGetTypeID(cfType
) == CFDictionaryGetTypeID())
150 return (CFDictionaryRef
)CFRetainSafe(cfType
);
151 SecError(-50, error
, CFSTR("object %@ is not a dictionary"), cfType
);
155 static inline CFSetRef
copyIfSet(CFTypeRef cfType
, CFErrorRef
*error
) {
156 if (cfType
&& CFGetTypeID(cfType
) == CFSetGetTypeID())
157 return (CFSetRef
)CFRetainSafe(cfType
);
158 SecError(-50, error
, CFSTR("object %@ is not a set"), cfType
);
162 static inline CFStringRef
copyIfString(CFTypeRef cfType
, CFErrorRef
*error
) {
163 if (cfType
&& CFGetTypeID(cfType
) == CFStringGetTypeID())
164 return (CFStringRef
)CFRetainSafe(cfType
);
165 SecError(-50, error
, CFSTR("object %@ is not a string"), cfType
);
169 static inline CFUUIDRef
copyIfUUID(CFTypeRef cfType
, CFErrorRef
*error
) {
170 if (cfType
&& CFGetTypeID(cfType
) == CFUUIDGetTypeID())
171 return (CFUUIDRef
)CFRetainSafe(cfType
);
172 SecError(-50, error
, CFSTR("object %@ is not a UUID"), cfType
);
177 // MARK: Analyzer confusing asXxx casting
179 static inline CFArrayRef
asArray(CFTypeRef cfType
, CFErrorRef
*error
) {
180 if (cfType
&& CFGetTypeID(cfType
) == CFArrayGetTypeID())
181 return (CFArrayRef
)cfType
;
182 SecError(-50, error
, CFSTR("object %@ is not an array"), cfType
);
186 static inline CFBooleanRef
asBoolean(CFTypeRef cfType
, CFErrorRef
*error
) {
187 if (cfType
&& CFGetTypeID(cfType
) == CFBooleanGetTypeID())
188 return (CFBooleanRef
)cfType
;
189 SecError(-50, error
, CFSTR("object %@ is not an boolean"), cfType
);
193 static inline CFDataRef
asData(CFTypeRef cfType
, CFErrorRef
*error
) {
194 if (cfType
&& CFGetTypeID(cfType
) == CFDataGetTypeID())
195 return (CFDataRef
)cfType
;
196 SecError(-50, error
, CFSTR("object %@ is not a data"), cfType
);
200 static inline CFDateRef
asDate(CFTypeRef cfType
, CFErrorRef
*error
) {
201 if (cfType
&& CFGetTypeID(cfType
) == CFDateGetTypeID())
202 return (CFDateRef
)cfType
;
203 SecError(-50, error
, CFSTR("object %@ is not a date"), cfType
);
207 static inline CFDictionaryRef
asDictionary(CFTypeRef cfType
, CFErrorRef
*error
) {
208 if (cfType
&& CFGetTypeID(cfType
) == CFDictionaryGetTypeID())
209 return (CFDictionaryRef
)cfType
;
210 SecError(-50, error
, CFSTR("object %@ is not a dictionary"), cfType
);
214 static inline CFSetRef
asSet(CFTypeRef cfType
, CFErrorRef
*error
) {
215 if (cfType
&& CFGetTypeID(cfType
) == CFSetGetTypeID())
216 return (CFSetRef
)cfType
;
217 SecError(-50, error
, CFSTR("object %@ is not a set"), cfType
);
221 static inline CFStringRef
asString(CFTypeRef cfType
, CFErrorRef
*error
) {
222 if (cfType
&& CFGetTypeID(cfType
) == CFStringGetTypeID())
223 return (CFStringRef
)cfType
;
224 SecError(-50, error
, CFSTR("object %@ is not a string"), cfType
);
228 static inline CFUUIDRef
asUUID(CFTypeRef cfType
, CFErrorRef
*error
) {
229 if (cfType
&& CFGetTypeID(cfType
) == CFUUIDGetTypeID())
230 return (CFUUIDRef
)cfType
;
231 SecError(-50, error
, CFSTR("object %@ is not a UUID"), cfType
);
235 #endif /* _SECCFERROR_H_ */