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@
31 This XPC service is essentially just a proxy to iCloud KVS, which exists since
32 the main security code cannot link against Foundation.
34 See sendTSARequestWithXPC in tsaSupport.c for how to call the service
36 send message to app with xpc_connection_send_message
38 For now, build this with:
40 ~rc/bin/buildit . --rootsDirectory=/var/tmp -noverify -offline -target CloudKeychainProxy
42 and install or upgrade with:
44 darwinup install /var/tmp/sec.roots/sec~dst
45 darwinup upgrade /var/tmp/sec.roots/sec~dst
47 You must use darwinup during development to update system caches
50 //------------------------------------------------------------------------------------------------
52 #include <AssertMacros.h>
54 #import <Foundation/Foundation.h>
55 #import <Security/Security.h>
56 #import <utilities/SecCFRelease.h>
58 #import <xpc/private.h>
59 #import <CoreFoundation/CFXPCBridge.h>
62 #import <CommonCrypto/CommonDigest.h>
63 #include <utilities/SecXPCError.h>
64 #include <TargetConditionals.h>
66 #import "SOSCloudKeychainConstants.h"
67 #import "CKDKVSProxy.h"
69 void finalize_connection(void *not_used);
70 void handle_connection_event(const xpc_connection_t peer);
71 static void cloudkeychainproxy_peer_dictionary_handler(const xpc_connection_t peer, xpc_object_t event);
73 static bool operation_put_dictionary(xpc_object_t event);
74 static bool operation_get_v2(xpc_object_t event);
76 int ckdproxymain(int argc, const char *argv[]);
78 #define PROXYXPCSCOPE "xpcproxy"
80 static void describeXPCObject(char *prefix, xpc_object_t object)
83 // This is useful for debugging.
86 char *desc = xpc_copy_description(object);
87 secdebug(PROXYXPCSCOPE, "%s%s\n", prefix, desc);
91 secdebug(PROXYXPCSCOPE, "%s<NULL>\n", prefix);
96 static void cloudkeychainproxy_peer_dictionary_handler(const xpc_connection_t peer, xpc_object_t event)
101 require_action_string(xpc_get_type(event) == XPC_TYPE_DICTIONARY, xit, err = -51, "expected XPC_TYPE_DICTIONARY");
103 const char *operation = xpc_dictionary_get_string(event, kMessageKeyOperation);
104 require_action(operation, xit, result = false);
106 // Check protocol version
107 uint64_t version = xpc_dictionary_get_uint64(event, kMessageKeyVersion);
108 secdebug(PROXYXPCSCOPE, "Reply version: %lld\n", version);
109 require_action(version == kCKDXPCVersion, xit, result = false);
112 secdebug(PROXYXPCSCOPE, "Handling %s operation", operation);
115 if (operation && !strcmp(operation, kOperationPUTDictionary))
117 operation_put_dictionary(event);
119 else if (operation && !strcmp(operation, kOperationGETv2))
121 operation_get_v2(event);
123 else if (operation && !strcmp(operation, kOperationClearStore))
125 [[UbiqitousKVSProxy sharedKVSProxy] clearStore];
126 xpc_object_t replyMessage = xpc_dictionary_create_reply(event);
127 if (replyMessage) // Caller wanted an ACK, so give one
129 xpc_dictionary_set_string(replyMessage, kMessageKeyValue, "ACK");
130 xpc_connection_send_message(peer, replyMessage);
133 else if (operation && !strcmp(operation, kOperationRemoveObjectForKey))
135 const char *keyToRemove = xpc_dictionary_get_string(event, kMessageKeyKey);
136 [[UbiqitousKVSProxy sharedKVSProxy] removeObjectForKey:[NSString stringWithCString:keyToRemove encoding:NSUTF8StringEncoding]];
137 xpc_object_t replyMessage = xpc_dictionary_create_reply(event);
138 if (replyMessage) // Caller wanted an ACK, so give one
140 xpc_dictionary_set_string(replyMessage, kMessageKeyValue, "ACK");
141 xpc_connection_send_message(peer, replyMessage);
144 else if (operation && !strcmp(operation, kOperationSynchronize))
146 [[UbiqitousKVSProxy sharedKVSProxy] requestSynchronization:YES];
147 xpc_object_t replyMessage = xpc_dictionary_create_reply(event);
148 if (replyMessage) // Caller wanted an ACK, so give one
150 xpc_dictionary_set_string(replyMessage, kMessageKeyValue, "ACK");
151 xpc_connection_send_message(peer, replyMessage);
154 else if (operation && !strcmp(operation, kOperationSynchronizeAndWait))
156 xpc_object_t xkeysToGetDict = xpc_dictionary_get_value(event, kMessageKeyValue);
157 xpc_object_t replyMessage = xpc_dictionary_create_reply(event);
158 xpc_object_t xkeys = xpc_dictionary_get_value(xkeysToGetDict, kMessageKeyKeysToGet);
159 NSArray *keysToGet = (__bridge_transfer NSArray *)(_CFXPCCreateCFObjectFromXPCObject(xkeys));
160 secnotice(XPROXYSCOPE, "%s XPC request: %s", kWAIT2MINID, kOperationSynchronizeAndWait);
162 [[UbiqitousKVSProxy sharedKVSProxy] waitForSynchronization:keysToGet
163 handler:^(NSDictionary *values, NSError *err) {
164 if (replyMessage) // Caller wanted an ACK, so give one
166 secnotice(PROXYXPCSCOPE, "%s Result from [[UbiqitousKVSProxy sharedKVSProxy] waitForSynchronization:]: %@", kWAIT2MINID, err);
167 xpc_object_t xobject = values ? _CFXPCCreateXPCObjectFromCFObject((__bridge CFTypeRef)(values)) : xpc_null_create();
168 xpc_dictionary_set_value(replyMessage, kMessageKeyValue, xobject);
171 xpc_object_t xerrobj = SecCreateXPCObjectWithCFError((__bridge CFErrorRef)(err));
172 xpc_dictionary_set_value(replyMessage, kMessageKeyError, xerrobj);
174 xpc_connection_send_message(peer, replyMessage);
177 secerror("%s XPC request: %s - No replyMessage: %@", kWAIT2MINID, kOperationSynchronizeAndWait, err);
181 else if (operation && !strcmp(operation, kOperationRegisterKeys))
183 xpc_object_t xkeysToRegisterDict = xpc_dictionary_get_value(event, kMessageKeyValue);
184 // describeXPCObject("xkeysToRegister: ", xkeysToRegisterDict);
186 // KTR = keysToRegister
187 xpc_object_t xKTRallkeys = xpc_dictionary_get_value(xkeysToRegisterDict, kMessageAllKeys);
189 NSDictionary *KTRallkeys = (__bridge_transfer NSDictionary *)(_CFXPCCreateCFObjectFromXPCObject(xKTRallkeys));
191 [[UbiqitousKVSProxy sharedKVSProxy] registerKeys: KTRallkeys];
193 xpc_object_t replyMessage = xpc_dictionary_create_reply(event);
194 xpc_dictionary_set_string(replyMessage, kMessageKeyValue, "ACK");
195 xpc_connection_send_message(peer, replyMessage);
197 secdebug(PROXYXPCSCOPE, "RegisterKeys message sent");
199 else if (operation && !strcmp(operation, kOperationUILocalNotification))
201 xpc_object_t xLocalNotificationDict = xpc_dictionary_get_value(event, kMessageKeyValue);
202 // describeXPCObject("xLocalNotificationDict: ", xLocalNotificationDict);
203 NSDictionary *localNotificationDict = (__bridge_transfer NSDictionary *)(_CFXPCCreateCFObjectFromXPCObject(xLocalNotificationDict));
204 int64_t outFlags = 0;
205 id object = [[UbiqitousKVSProxy sharedKVSProxy] localNotification:localNotificationDict outFlags:&outFlags];
206 secdebug(PROXYXPCSCOPE, "Result from [[UbiqitousKVSProxy sharedKVSProxy] localNotification:]: %@", object);
207 xpc_object_t xobject = object ? _CFXPCCreateXPCObjectFromCFObject((__bridge CFTypeRef)(object)) : xpc_null_create();
208 xpc_object_t replyMessage = xpc_dictionary_create_reply(event);
209 xpc_dictionary_set_int64(xobject, kMessageKeyNotificationFlags, outFlags);
210 xpc_dictionary_set_value(replyMessage, kMessageKeyValue, xobject);
211 xpc_connection_send_message(peer, replyMessage);
212 secdebug(PROXYXPCSCOPE, "localNotification reply sent");
214 else if (operation && !strcmp(operation, kOperationRequestSyncWithAllPeers))
216 [[UbiqitousKVSProxy sharedKVSProxy] requestSyncWithAllPeers];
217 xpc_object_t replyMessage = xpc_dictionary_create_reply(event);
218 if (replyMessage) // Caller wanted an ACK, so give one
220 xpc_dictionary_set_string(replyMessage, kMessageKeyValue, "ACK");
221 xpc_connection_send_message(peer, replyMessage);
223 secdebug(PROXYXPCSCOPE, "RequestSyncWithAllPeers reply sent");
225 else if (operation && !strcmp(operation, kOperationRequestEnsurePeerRegistration))
227 [[UbiqitousKVSProxy sharedKVSProxy] requestEnsurePeerRegistration];
228 xpc_object_t replyMessage = xpc_dictionary_create_reply(event);
229 if (replyMessage) // Caller wanted an ACK, so give one
231 xpc_dictionary_set_string(replyMessage, kMessageKeyValue, "ACK");
232 xpc_connection_send_message(peer, replyMessage);
234 secdebug(PROXYXPCSCOPE, "RequestEnsurePeerRegistration reply sent");
236 else if (operation && !strcmp(operation, kOperationFlush))
238 [[UbiqitousKVSProxy sharedKVSProxy] doAfterFlush:^{
239 xpc_object_t replyMessage = xpc_dictionary_create_reply(event);
240 if (replyMessage) // Caller wanted an ACK, so give one
242 xpc_dictionary_set_string(replyMessage, kMessageKeyValue, "ACK");
243 xpc_connection_send_message(peer, replyMessage);
245 secdebug(PROXYXPCSCOPE, "flush reply sent");
250 char *description = xpc_copy_description(event);
251 secdebug(PROXYXPCSCOPE, "Unknown op=%s request from pid %d: %s", operation, xpc_connection_get_pid(peer), description);
257 describeXPCObject("handle_operation fail: ", event);
260 void finalize_connection(void *not_used)
262 secdebug(PROXYXPCSCOPE, "finalize_connection");
263 [[UbiqitousKVSProxy sharedKVSProxy] requestSynchronization:YES];
264 xpc_transaction_end();
267 static bool operation_put_dictionary(xpc_object_t event)
269 // PUT a set of objects into the KVS store. Return false if error
271 describeXPCObject("operation_put_dictionary event: ", event);
272 xpc_object_t xvalue = xpc_dictionary_get_value(event, kMessageKeyValue);
276 CFTypeRef cfvalue = _CFXPCCreateCFObjectFromXPCObject(xvalue);
277 if (cfvalue && (CFGetTypeID(cfvalue)==CFDictionaryGetTypeID()))
279 [[UbiqitousKVSProxy sharedKVSProxy] setObjectsFromDictionary:(__bridge NSDictionary *)cfvalue];
280 CFReleaseSafe(cfvalue);
284 describeXPCObject("operation_put_dictionary unable to convert to CF: ", xvalue);
285 CFReleaseSafe(cfvalue);
290 static bool operation_get_v2(xpc_object_t event)
292 // GET a set of objects from the KVS store. Return false if error
293 describeXPCObject("operation_get_v2 event: ", event);
294 xpc_connection_t peer = xpc_dictionary_get_remote_connection(event);
295 describeXPCObject("operation_get_v2: peer: ", peer);
297 xpc_object_t replyMessage = xpc_dictionary_create_reply(event);
300 secdebug(PROXYXPCSCOPE, "can't create replyMessage");
301 assert(true); //must have a reply handler
304 xpc_object_t returnedValues = xpc_dictionary_create(NULL, NULL, 0);
307 secdebug(PROXYXPCSCOPE, "can't create returnedValues");
308 assert(true); // must have a spot for the returned values
312 xpc_object_t xvalue = xpc_dictionary_get_value(event, kMessageKeyValue);
315 secdebug(PROXYXPCSCOPE, "missing \"value\" key");
319 xpc_object_t xkeystoget = xpc_dictionary_get_value(xvalue, kMessageKeyKeysToGet);
322 secdebug(PROXYXPCSCOPE, "got xkeystoget");
323 CFTypeRef keystoget = _CFXPCCreateCFObjectFromXPCObject(xkeystoget);
324 if (!keystoget || (CFGetTypeID(keystoget)!=CFArrayGetTypeID())) // not "getAll", this is an error of some kind
326 secdebug(PROXYXPCSCOPE, "can't convert keystoget or is not an array");
327 CFReleaseSafe(keystoget);
331 [(__bridge NSArray *)keystoget enumerateObjectsUsingBlock: ^ (id obj, NSUInteger idx, BOOL *stop)
333 NSString *key = (NSString *)obj;
334 id object = [[UbiqitousKVSProxy sharedKVSProxy] get:key];
335 secdebug(PROXYXPCSCOPE, "[UbiqitousKVSProxy sharedKVSProxy] get: key: %@, object: %@", key, object);
336 xpc_object_t xobject = object ? _CFXPCCreateXPCObjectFromCFObject((__bridge CFTypeRef)object) : xpc_null_create();
337 xpc_dictionary_set_value(returnedValues, [key UTF8String], xobject);
338 describeXPCObject("operation_get_v2: value from kvs: ", xobject);
341 else // get all values from kvs
343 secdebug(PROXYXPCSCOPE, "get all values from kvs");
344 NSDictionary *all = [[UbiqitousKVSProxy sharedKVSProxy] getAll];
345 [all enumerateKeysAndObjectsUsingBlock: ^ (id key, id obj, BOOL *stop)
347 xpc_object_t xobject = obj ? _CFXPCCreateXPCObjectFromCFObject((__bridge CFTypeRef)obj) : xpc_null_create();
348 xpc_dictionary_set_value(returnedValues, [(NSString *)key UTF8String], xobject);
352 xpc_dictionary_set_uint64(replyMessage, kMessageKeyVersion, kCKDXPCVersion);
353 xpc_dictionary_set_value(replyMessage, kMessageKeyValue, returnedValues);
354 xpc_connection_send_message(peer, replyMessage);
359 static void initializeProxyObjectWithConnection(const xpc_connection_t connection)
361 [[UbiqitousKVSProxy sharedKVSProxy] setItemsChangedBlock:^CFArrayRef(CFDictionaryRef values)
363 secdebug(PROXYXPCSCOPE, "UbiqitousKVSProxy called back");
364 xpc_object_t xobj = _CFXPCCreateXPCObjectFromCFObject(values);
365 xpc_object_t message = xpc_dictionary_create(NULL, NULL, 0);
366 xpc_dictionary_set_uint64(message, kMessageKeyVersion, kCKDXPCVersion);
367 xpc_dictionary_set_string(message, kMessageKeyOperation, kMessageOperationItemChanged);
368 xpc_dictionary_set_value(message, kMessageKeyValue, xobj?xobj:xpc_null_create());
369 xpc_connection_send_message(connection, message); // Send message; don't wait for a reply
374 static void cloudkeychainproxy_peer_event_handler(xpc_connection_t peer, xpc_object_t event)
376 describeXPCObject("peer: ", peer);
377 xpc_type_t type = xpc_get_type(event);
378 if (type == XPC_TYPE_ERROR) {
379 if (event == XPC_ERROR_CONNECTION_INVALID) {
380 // The client process on the other end of the connection has either
381 // crashed or cancelled the connection. After receiving this error,
382 // the connection is in an invalid state, and you do not need to
383 // call xpc_connection_cancel(). Just tear down any associated state
385 } else if (event == XPC_ERROR_TERMINATION_IMMINENT) {
386 // Handle per-connection termination cleanup.
389 assert(type == XPC_TYPE_DICTIONARY);
390 // Handle the message.
391 // describeXPCObject("dictionary:", event);
392 dispatch_async(dispatch_get_main_queue(), ^{
393 cloudkeychainproxy_peer_dictionary_handler(peer, event);
398 static void cloudkeychainproxy_event_handler(xpc_connection_t peer)
400 // By defaults, new connections will target the default dispatch
403 if (xpc_get_type(peer) != XPC_TYPE_CONNECTION)
405 secdebug(PROXYXPCSCOPE, "expected XPC_TYPE_CONNECTION");
408 initializeProxyObjectWithConnection(peer);
409 xpc_connection_set_event_handler(peer, ^(xpc_object_t event)
411 cloudkeychainproxy_peer_event_handler(peer, event);
414 // This will tell the connection to begin listening for events. If you
415 // have some other initialization that must be done asynchronously, then
416 // you can defer this call until after that initialization is done.
417 xpc_connection_resume(peer);
420 static void diagnostics(int argc, const char *argv[])
424 NSDictionary *all = [[UbiqitousKVSProxy sharedKVSProxy] getAll];
425 NSLog(@"All: %@",all);
429 int ckdproxymain(int argc, const char *argv[])
431 secdebug(PROXYXPCSCOPE, "Starting CloudKeychainProxy");
432 char *wait4debugger = getenv("WAIT4DEBUGGER");
434 if (wait4debugger && !strcasecmp("YES", wait4debugger))
436 syslog(LOG_ERR, "Waiting for debugger");
437 kill(getpid(), SIGTSTP);
441 diagnostics(argc, argv);
445 // DISPATCH_TARGET_QUEUE_DEFAULT
446 xpc_connection_t listener = xpc_connection_create_mach_service(xpcServiceName, NULL, XPC_CONNECTION_MACH_SERVICE_LISTENER);
447 xpc_connection_set_event_handler(listener, ^(xpc_object_t object){ cloudkeychainproxy_event_handler(object); });
449 [UbiqitousKVSProxy sharedKVSProxy];
451 // It looks to me like there is insufficient locking to allow a request to come in on the XPC connection while doing the initial all items.
452 // Therefore I'm leaving the XPC connection suspended until that has time to process.
453 xpc_connection_resume(listener);
457 secdebug(PROXYXPCSCOPE, "Starting mainRunLoop");
458 NSRunLoop *runLoop = [NSRunLoop mainRunLoop];
462 secdebug(PROXYXPCSCOPE, "Exiting CloudKeychainProxy");