2 * Copyright (c) 2013-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@
27 #include <utilities/SecXPCError.h>
28 #include <utilities/SecCFError.h>
29 #include <utilities/SecCFWrappers.h>
31 CFStringRef sSecXPCErrorDomain
= CFSTR("com.apple.security.xpc");
33 static const char* kDomainKey
= "domain";
34 static const char* kDescriptionKey
= "description";
35 static const char* kCodeKey
= "code";
37 CFErrorRef
SecCreateCFErrorWithXPCObject(xpc_object_t xpc_error
)
39 CFErrorRef result
= NULL
;
41 if (xpc_get_type(xpc_error
) == XPC_TYPE_DICTIONARY
) {
42 CFStringRef domain
= NULL
;
44 const char * domain_string
= xpc_dictionary_get_string(xpc_error
, kDomainKey
);
45 if (domain_string
!= NULL
) {
46 domain
= CFStringCreateWithCString(kCFAllocatorDefault
, domain_string
, kCFStringEncodingUTF8
);
48 domain
= sSecXPCErrorDomain
;
51 CFIndex code
= (CFIndex
) xpc_dictionary_get_int64(xpc_error
, kCodeKey
);
53 const char *description
= xpc_dictionary_get_string(xpc_error
, kDescriptionKey
);
55 SecCFCreateErrorWithFormat(code
, domain
, NULL
, &result
, NULL
, CFSTR("Remote error : %s"), description
);
57 CFReleaseSafe(domain
);
59 SecCFCreateErrorWithFormat(kSecXPCErrorUnexpectedType
, sSecXPCErrorDomain
, NULL
, &result
, NULL
, CFSTR("Remote error not dictionary!: %@"), xpc_error
);
64 static void SecXPCDictionarySetCFString(xpc_object_t dict
, const char *key
, CFStringRef string
)
66 CFStringPerformWithCString(string
, ^(const char *utf8Str
) {
67 xpc_dictionary_set_string(dict
, key
, utf8Str
);
71 xpc_object_t
SecCreateXPCObjectWithCFError(CFErrorRef error
)
73 xpc_object_t error_xpc
= xpc_dictionary_create(NULL
, NULL
, 0);
75 SecXPCDictionarySetCFString(error_xpc
, kDomainKey
, CFErrorGetDomain(error
));
76 xpc_dictionary_set_int64(error_xpc
, kCodeKey
, CFErrorGetCode(error
));
78 CFStringRef description
= CFErrorCopyDescription(error
);
79 SecXPCDictionarySetCFString(error_xpc
, kDescriptionKey
, description
);
80 CFReleaseNull(description
);