1// 2// SecCFXPCWrappers.c 3// utilities 4// 5// Created by John Hurley on 5/6/13. 6// Copyright (c) 2013 Apple Inc. All rights reserved. 7// 8 9#include <stdio.h> 10 11#include <utilities/SecXPCError.h> 12#include <utilities/SecCFError.h> 13#include <utilities/SecCFWrappers.h> 14 15CFStringRef sSecXPCErrorDomain = CFSTR("com.apple.security.xpc"); 16 17static const char* kDomainKey = "domain"; 18static const char* kDescriptionKey = "description"; 19static const char* kCodeKey = "code"; 20 21CFErrorRef SecCreateCFErrorWithXPCObject(xpc_object_t xpc_error) 22{ 23 CFErrorRef result = NULL; 24 25 if (xpc_get_type(xpc_error) == XPC_TYPE_DICTIONARY) { 26 CFStringRef domain = NULL; 27 28 const char * domain_string = xpc_dictionary_get_string(xpc_error, kDomainKey); 29 if (domain_string != NULL) { 30 domain = CFStringCreateWithCString(kCFAllocatorDefault, domain_string, kCFStringEncodingUTF8); 31 } else { 32 domain = sSecXPCErrorDomain; 33 CFRetain(domain); 34 } 35 CFIndex code = (CFIndex) xpc_dictionary_get_int64(xpc_error, kCodeKey); 36 37 const char *description = xpc_dictionary_get_string(xpc_error, kDescriptionKey); 38 39 SecCFCreateErrorWithFormat(code, domain, NULL, &result, NULL, CFSTR("Remote error : %s"), description); 40 41 CFReleaseSafe(domain); 42 } else { 43 SecCFCreateErrorWithFormat(kSecXPCErrorUnexpectedType, sSecXPCErrorDomain, NULL, &result, NULL, CFSTR("Remote error not dictionary!: %@"), xpc_error); 44 } 45 return result; 46} 47 48static void SecXPCDictionarySetCFString(xpc_object_t dict, const char *key, CFStringRef string) 49{ 50 CFStringPerformWithCString(string, ^(const char *utf8Str) { 51 xpc_dictionary_set_string(dict, key, utf8Str); 52 }); 53} 54 55xpc_object_t SecCreateXPCObjectWithCFError(CFErrorRef error) 56{ 57 xpc_object_t error_xpc = xpc_dictionary_create(NULL, NULL, 0); 58 59 SecXPCDictionarySetCFString(error_xpc, kDomainKey, CFErrorGetDomain(error)); 60 xpc_dictionary_set_int64(error_xpc, kCodeKey, CFErrorGetCode(error)); 61 62 CFStringRef description = CFErrorCopyDescription(error); 63 SecXPCDictionarySetCFString(error_xpc, kDescriptionKey, description); 64 CFReleaseNull(description); 65 66 return error_xpc; 67} 68