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>
31 // Leaf error creation from other systems
34 // kern_return_t errors
35 #define kSecKernDomain kCFErrorDomainMach
36 bool SecKernError(kern_return_t result
, CFErrorRef
*error
, CFStringRef format
, ...);
39 #define kSecErrnoDomain kCFErrorDomainPOSIX
40 bool SecCheckErrno(int result
, CFErrorRef
*error
, CFStringRef format
, ...);
43 #define kSecErrorDomain kCFErrorDomainOSStatus
44 bool SecError(OSStatus status
, CFErrorRef
*error
, CFStringRef format
, ...);
46 // Direct checking of POSIX errors.
47 bool SecPOSIXError(int error
, CFErrorRef
*cferror
, CFStringRef format
, ...);
50 #define kSecCoreCryptoDomain CFSTR("kSecCoreCryptoDomain")
51 bool SecCoreCryptoError(int error
, CFErrorRef
*cferror
, CFStringRef format
, ...);
54 #define kSecNotifyDomain CFSTR("kSecNotifyDomain")
55 bool SecNotifyError(uint32_t result
, CFErrorRef
*error
, CFStringRef format
, ...);
57 // requirement error, typically parameters
58 bool SecRequirementError(bool requirement
, CFErrorRef
*error
, CFStringRef format
, ...);
61 bool SecAllocationError(const void *allocated
, CFErrorRef
*error
, CFStringRef format
, ...);
65 // Create and chain, all return false to make the analyzer happy.
67 bool SecCFCreateError(CFIndex errorCode
, CFStringRef domain
, CFStringRef descriptionString
,
68 CFErrorRef previousError
, CFErrorRef
*newError
);
70 bool SecCFCreateErrorWithFormat(CFIndex errorCode
, CFStringRef domain
, CFErrorRef previousError
, CFErrorRef
*newError
,
71 CFDictionaryRef formatoptions
, CFStringRef descriptionString
, ...)
72 CF_FORMAT_FUNCTION(6,7);
75 bool SecCFCreateErrorWithFormatAndArguments(CFIndex errorCode
, CFStringRef domain
,
76 CFErrorRef previousError
, CFErrorRef
*newError
,
77 CFDictionaryRef formatoptions
, CFStringRef descriptionString
, va_list args
)
78 CF_FORMAT_FUNCTION(6, 0);
81 // MARK: Optional value type casting
84 static inline bool asArrayOptional(CFTypeRef cfType
, CFArrayRef
*array
, CFErrorRef
*error
) {
85 if (!cfType
|| CFGetTypeID(cfType
) == CFArrayGetTypeID()) {
86 if (array
) *array
= (CFArrayRef
)cfType
;
89 SecError(-50, error
, CFSTR("object %@ is not an array"), cfType
);
93 static inline bool asDataOptional(CFTypeRef cfType
, CFDataRef
*data
, CFErrorRef
*error
) {
94 if (!cfType
|| CFGetTypeID(cfType
) == CFDataGetTypeID()) {
95 if (data
) *data
= (CFDataRef
)cfType
;
98 SecError(-50, error
, CFSTR("object %@ is not an data"), cfType
);
102 static inline bool asSetOptional(CFTypeRef cfType
, CFSetRef
*set
, CFErrorRef
*error
) {
103 if (!cfType
|| CFGetTypeID(cfType
) == CFSetGetTypeID()) {
104 if (set
) *set
= (CFSetRef
)cfType
;
107 SecError(-50, error
, CFSTR("object %@ is not a set"), cfType
);
112 // MARK: Required value type casting
115 static inline CFArrayRef
asArray(CFTypeRef cfType
, CFErrorRef
*error
) {
116 if (cfType
&& CFGetTypeID(cfType
) == CFArrayGetTypeID())
117 return (CFArrayRef
)cfType
;
118 SecError(-50, error
, CFSTR("object %@ is not an array"), cfType
);
122 static inline CFBooleanRef
asBoolean(CFTypeRef cfType
, CFErrorRef
*error
) {
123 if (cfType
&& CFGetTypeID(cfType
) == CFBooleanGetTypeID())
124 return (CFBooleanRef
)cfType
;
125 SecError(-50, error
, CFSTR("object %@ is not an boolean"), cfType
);
129 static inline CFDataRef
asData(CFTypeRef cfType
, CFErrorRef
*error
) {
130 if (cfType
&& CFGetTypeID(cfType
) == CFDataGetTypeID())
131 return (CFDataRef
)cfType
;
132 SecError(-50, error
, CFSTR("object %@ is not a data"), cfType
);
136 static inline CFDateRef
asDate(CFTypeRef cfType
, CFErrorRef
*error
) {
137 if (cfType
&& CFGetTypeID(cfType
) == CFDateGetTypeID())
138 return (CFDateRef
)cfType
;
139 SecError(-50, error
, CFSTR("object %@ is not a date"), cfType
);
143 static inline CFDictionaryRef
asDictionary(CFTypeRef cfType
, CFErrorRef
*error
) {
144 if (cfType
&& CFGetTypeID(cfType
) == CFDictionaryGetTypeID())
145 return (CFDictionaryRef
)cfType
;
146 SecError(-50, error
, CFSTR("object %@ is not a dictionary"), cfType
);
150 static inline CFSetRef
asSet(CFTypeRef cfType
, CFErrorRef
*error
) {
151 if (cfType
&& CFGetTypeID(cfType
) == CFSetGetTypeID())
152 return (CFSetRef
)cfType
;
153 SecError(-50, error
, CFSTR("object %@ is not a set"), cfType
);
157 static inline CFStringRef
asString(CFTypeRef cfType
, CFErrorRef
*error
) {
158 if (cfType
&& CFGetTypeID(cfType
) == CFStringGetTypeID())
159 return (CFStringRef
)cfType
;
160 SecError(-50, error
, CFSTR("object %@ is not a string"), cfType
);
164 #endif /* _SECCFERROR_H_ */