1/* 2 * Copyright (c) 2013-2014 Apple Inc. All Rights Reserved. 3 * 4 * @APPLE_LICENSE_HEADER_START@ 5 * 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 11 * file. 12 * 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. 20 * 21 * @APPLE_LICENSE_HEADER_END@ 22 */ 23 24 25#include <stdio.h> 26 27#include <utilities/SecXPCError.h> 28#include <utilities/SecCFError.h> 29#include <utilities/SecCFWrappers.h> 30 31CFStringRef sSecXPCErrorDomain = CFSTR("com.apple.security.xpc"); 32 33static const char* kDomainKey = "domain"; 34static const char* kDescriptionKey = "description"; 35static const char* kCodeKey = "code"; 36 37CFErrorRef SecCreateCFErrorWithXPCObject(xpc_object_t xpc_error) 38{ 39 CFErrorRef result = NULL; 40 41 if (xpc_get_type(xpc_error) == XPC_TYPE_DICTIONARY) { 42 CFStringRef domain = NULL; 43 44 const char * domain_string = xpc_dictionary_get_string(xpc_error, kDomainKey); 45 if (domain_string != NULL) { 46 domain = CFStringCreateWithCString(kCFAllocatorDefault, domain_string, kCFStringEncodingUTF8); 47 } else { 48 domain = sSecXPCErrorDomain; 49 CFRetain(domain); 50 } 51 CFIndex code = (CFIndex) xpc_dictionary_get_int64(xpc_error, kCodeKey); 52 53 const char *description = xpc_dictionary_get_string(xpc_error, kDescriptionKey); 54 55 SecCFCreateErrorWithFormat(code, domain, NULL, &result, NULL, CFSTR("Remote error : %s"), description); 56 57 CFReleaseSafe(domain); 58 } else { 59 SecCFCreateErrorWithFormat(kSecXPCErrorUnexpectedType, sSecXPCErrorDomain, NULL, &result, NULL, CFSTR("Remote error not dictionary!: %@"), xpc_error); 60 } 61 return result; 62} 63 64static void SecXPCDictionarySetCFString(xpc_object_t dict, const char *key, CFStringRef string) 65{ 66 CFStringPerformWithCString(string, ^(const char *utf8Str) { 67 xpc_dictionary_set_string(dict, key, utf8Str); 68 }); 69} 70 71xpc_object_t SecCreateXPCObjectWithCFError(CFErrorRef error) 72{ 73 xpc_object_t error_xpc = xpc_dictionary_create(NULL, NULL, 0); 74 75 SecXPCDictionarySetCFString(error_xpc, kDomainKey, CFErrorGetDomain(error)); 76 xpc_dictionary_set_int64(error_xpc, kCodeKey, CFErrorGetCode(error)); 77 78 CFStringRef description = CFErrorCopyDescription(error); 79 SecXPCDictionarySetCFString(error_xpc, kDescriptionKey, description); 80 CFReleaseNull(description); 81 82 return error_xpc; 83} 84