5 // Created by John Hurley on 5/6/13.
6 // Copyright (c) 2013 Apple Inc. All rights reserved.
11 #include <utilities/SecXPCError.h>
12 #include <utilities/SecCFError.h>
13 #include <utilities/SecCFWrappers.h>
15 CFStringRef sSecXPCErrorDomain
= CFSTR("com.apple.security.xpc");
17 static const char* kDomainKey
= "domain";
18 static const char* kDescriptionKey
= "description";
19 static const char* kCodeKey
= "code";
21 CFErrorRef
SecCreateCFErrorWithXPCObject(xpc_object_t xpc_error
)
23 CFErrorRef result
= NULL
;
25 if (xpc_get_type(xpc_error
) == XPC_TYPE_DICTIONARY
) {
26 CFStringRef domain
= NULL
;
28 const char * domain_string
= xpc_dictionary_get_string(xpc_error
, kDomainKey
);
29 if (domain_string
!= NULL
) {
30 domain
= CFStringCreateWithCString(kCFAllocatorDefault
, domain_string
, kCFStringEncodingUTF8
);
32 domain
= sSecXPCErrorDomain
;
35 CFIndex code
= (CFIndex
) xpc_dictionary_get_int64(xpc_error
, kCodeKey
);
37 const char *description
= xpc_dictionary_get_string(xpc_error
, kDescriptionKey
);
39 SecCFCreateErrorWithFormat(code
, domain
, NULL
, &result
, NULL
, CFSTR("Remote error : %s"), description
);
41 CFReleaseSafe(domain
);
43 SecCFCreateErrorWithFormat(kSecXPCErrorUnexpectedType
, sSecXPCErrorDomain
, NULL
, &result
, NULL
, CFSTR("Remote error not dictionary!: %@"), xpc_error
);
48 static void SecXPCDictionarySetCFString(xpc_object_t dict
, const char *key
, CFStringRef string
)
50 CFStringPerformWithCString(string
, ^(const char *utf8Str
) {
51 xpc_dictionary_set_string(dict
, key
, utf8Str
);
55 xpc_object_t
SecCreateXPCObjectWithCFError(CFErrorRef error
)
57 xpc_object_t error_xpc
= xpc_dictionary_create(NULL
, NULL
, 0);
59 SecXPCDictionarySetCFString(error_xpc
, kDomainKey
, CFErrorGetDomain(error
));
60 xpc_dictionary_set_int64(error_xpc
, kCodeKey
, CFErrorGetCode(error
));
62 CFStringRef description
= CFErrorCopyDescription(error
);
63 SecXPCDictionarySetCFString(error_xpc
, kDescriptionKey
, description
);
64 CFReleaseNull(description
);