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 "CKDKVSProxy.h"
68 void finalize_connection(void *not_used);
69 void handle_connection_event(const xpc_connection_t peer);
70 static void cloudkeychainproxy_peer_dictionary_handler(const xpc_connection_t peer, xpc_object_t event);
72 static bool operation_put_dictionary(xpc_object_t event);
73 static bool operation_get_v2(xpc_object_t event);
75 int ckdproxymain(int argc, const char *argv[]);
77 #define PROXYXPCSCOPE "xpcproxy"
79 static void describeXPCObject(char *prefix, xpc_object_t object)
82 // This is useful for debugging.
85 char *desc = xpc_copy_description(object);
86 secdebug(PROXYXPCSCOPE, "%s%s\n", prefix, desc);
90 secdebug(PROXYXPCSCOPE, "%s<NULL>\n", prefix);
95 static void cloudkeychainproxy_peer_dictionary_handler(const xpc_connection_t peer, xpc_object_t event)
100 require_action_string(xpc_get_type(event) == XPC_TYPE_DICTIONARY, xit, err = -51, "expected XPC_TYPE_DICTIONARY");
102 const char *operation = xpc_dictionary_get_string(event, kMessageKeyOperation);
103 require_action(operation, xit, result = false);
105 // Check protocol version
106 uint64_t version = xpc_dictionary_get_uint64(event, kMessageKeyVersion);
107 secdebug(PROXYXPCSCOPE, "Reply version: %lld\n", version);
108 require_action(version == kCKDXPCVersion, xit, result = false);
111 secdebug(PROXYXPCSCOPE, "Handling %s operation", operation);
114 if (operation && !strcmp(operation, kOperationPUTDictionary))
116 operation_put_dictionary(event);
118 else if (operation && !strcmp(operation, kOperationGETv2))
120 operation_get_v2(event);
122 else if (operation && !strcmp(operation, kOperationClearStore))
124 [[UbiqitousKVSProxy sharedKVSProxy] clearStore];
125 xpc_object_t replyMessage = xpc_dictionary_create_reply(event);
126 if (replyMessage) // Caller wanted an ACK, so give one
128 xpc_dictionary_set_string(replyMessage, kMessageKeyValue, "ACK");
129 xpc_connection_send_message(peer, replyMessage);
132 else if (operation && !strcmp(operation, kOperationRemoveObjectForKey))
134 const char *keyToRemove = xpc_dictionary_get_string(event, kMessageKeyKey);
135 [[UbiqitousKVSProxy sharedKVSProxy] removeObjectForKey:[NSString stringWithCString:keyToRemove encoding:NSUTF8StringEncoding]];
136 xpc_object_t replyMessage = xpc_dictionary_create_reply(event);
137 if (replyMessage) // Caller wanted an ACK, so give one
139 xpc_dictionary_set_string(replyMessage, kMessageKeyValue, "ACK");
140 xpc_connection_send_message(peer, replyMessage);
143 else if (operation && !strcmp(operation, kOperationSynchronize))
145 [[UbiqitousKVSProxy sharedKVSProxy] requestSynchronization:YES];
146 xpc_object_t replyMessage = xpc_dictionary_create_reply(event);
147 if (replyMessage) // Caller wanted an ACK, so give one
149 xpc_dictionary_set_string(replyMessage, kMessageKeyValue, "ACK");
150 xpc_connection_send_message(peer, replyMessage);
153 else if (operation && !strcmp(operation, kOperationSynchronizeAndWait))
155 xpc_object_t xkeysToGetDict = xpc_dictionary_get_value(event, kMessageKeyValue);
156 xpc_object_t replyMessage = xpc_dictionary_create_reply(event);
157 xpc_object_t xkeys = xpc_dictionary_get_value(xkeysToGetDict, kMessageKeyKeysToGet);
158 NSArray *keysToGet = (__bridge_transfer NSArray *)(_CFXPCCreateCFObjectFromXPCObject(xkeys));
159 secnotice(XPROXYSCOPE, "%s XPC request: %s", kWAIT2MINID, kOperationSynchronizeAndWait);
161 [[UbiqitousKVSProxy sharedKVSProxy] waitForSynchronization:keysToGet
162 handler:^(NSDictionary *values, NSError *err) {
163 if (replyMessage) // Caller wanted an ACK, so give one
165 secnotice(PROXYXPCSCOPE, "%s Result from [[UbiqitousKVSProxy sharedKVSProxy] waitForSynchronization:]: %@", kWAIT2MINID, err);
166 xpc_object_t xobject = values ? _CFXPCCreateXPCObjectFromCFObject((__bridge CFTypeRef)(values)) : xpc_null_create();
167 xpc_dictionary_set_value(replyMessage, kMessageKeyValue, xobject);
170 xpc_object_t xerrobj = SecCreateXPCObjectWithCFError((__bridge CFErrorRef)(err));
171 xpc_dictionary_set_value(replyMessage, kMessageKeyError, xerrobj);
173 xpc_connection_send_message(peer, replyMessage);
176 secerror("%s XPC request: %s - No replyMessage: %@", kWAIT2MINID, kOperationSynchronizeAndWait, err);
180 else if (operation && !strcmp(operation, kOperationRegisterKeys))
182 xpc_object_t xkeysToRegisterDict = xpc_dictionary_get_value(event, kMessageKeyValue);
183 // describeXPCObject("xkeysToRegister: ", xkeysToRegisterDict);
185 // KTR = keysToRegister
186 xpc_object_t xKTRallkeys = xpc_dictionary_get_value(xkeysToRegisterDict, kMessageAllKeys);
188 NSDictionary *KTRallkeys = (__bridge_transfer NSDictionary *)(_CFXPCCreateCFObjectFromXPCObject(xKTRallkeys));
190 [[UbiqitousKVSProxy sharedKVSProxy] registerKeys: KTRallkeys];
192 xpc_object_t replyMessage = xpc_dictionary_create_reply(event);
193 xpc_dictionary_set_string(replyMessage, kMessageKeyValue, "ACK");
194 xpc_connection_send_message(peer, replyMessage);
196 secdebug(PROXYXPCSCOPE, "RegisterKeys message sent");
198 else if (operation && !strcmp(operation, kOperationRequestSyncWithAllPeers))
200 [[UbiqitousKVSProxy sharedKVSProxy] requestSyncWithAllPeers];
201 xpc_object_t replyMessage = xpc_dictionary_create_reply(event);
202 if (replyMessage) // Caller wanted an ACK, so give one
204 xpc_dictionary_set_string(replyMessage, kMessageKeyValue, "ACK");
205 xpc_connection_send_message(peer, replyMessage);
207 secdebug(PROXYXPCSCOPE, "RequestSyncWithAllPeers reply sent");
209 else if (operation && !strcmp(operation, kOperationRequestEnsurePeerRegistration))
211 [[UbiqitousKVSProxy sharedKVSProxy] requestEnsurePeerRegistration];
212 xpc_object_t replyMessage = xpc_dictionary_create_reply(event);
213 if (replyMessage) // Caller wanted an ACK, so give one
215 xpc_dictionary_set_string(replyMessage, kMessageKeyValue, "ACK");
216 xpc_connection_send_message(peer, replyMessage);
218 secdebug(PROXYXPCSCOPE, "RequestEnsurePeerRegistration reply sent");
220 else if (operation && !strcmp(operation, kOperationFlush))
222 [[UbiqitousKVSProxy sharedKVSProxy] doAfterFlush:^{
223 xpc_object_t replyMessage = xpc_dictionary_create_reply(event);
224 if (replyMessage) // Caller wanted an ACK, so give one
226 xpc_dictionary_set_string(replyMessage, kMessageKeyValue, "ACK");
227 xpc_connection_send_message(peer, replyMessage);
229 secdebug(PROXYXPCSCOPE, "flush reply sent");
234 char *description = xpc_copy_description(event);
235 secdebug(PROXYXPCSCOPE, "Unknown op=%s request from pid %d: %s", operation, xpc_connection_get_pid(peer), description);
241 describeXPCObject("handle_operation fail: ", event);
244 void finalize_connection(void *not_used)
246 secdebug(PROXYXPCSCOPE, "finalize_connection");
247 [[UbiqitousKVSProxy sharedKVSProxy] requestSynchronization:YES];
248 xpc_transaction_end();
251 static bool operation_put_dictionary(xpc_object_t event)
253 // PUT a set of objects into the KVS store. Return false if error
255 describeXPCObject("operation_put_dictionary event: ", event);
256 xpc_object_t xvalue = xpc_dictionary_get_value(event, kMessageKeyValue);
260 CFTypeRef cfvalue = _CFXPCCreateCFObjectFromXPCObject(xvalue);
261 if (cfvalue && (CFGetTypeID(cfvalue)==CFDictionaryGetTypeID()))
263 [[UbiqitousKVSProxy sharedKVSProxy] recordWriteToKVS:(__bridge NSDictionary *)cfvalue];
264 NSDictionary *safeValues = [[UbiqitousKVSProxy sharedKVSProxy] recordHaltedValuesAndReturnValuesToSafelyWrite:(__bridge NSDictionary *)cfvalue];
265 if([safeValues count] !=0){
266 [[UbiqitousKVSProxy sharedKVSProxy] setObjectsFromDictionary:safeValues];
268 CFReleaseSafe(cfvalue);
272 describeXPCObject("operation_put_dictionary unable to convert to CF: ", xvalue);
273 CFReleaseSafe(cfvalue);
278 static bool operation_get_v2(xpc_object_t event)
280 // GET a set of objects from the KVS store. Return false if error
281 describeXPCObject("operation_get_v2 event: ", event);
282 xpc_connection_t peer = xpc_dictionary_get_remote_connection(event);
283 describeXPCObject("operation_get_v2: peer: ", peer);
285 xpc_object_t replyMessage = xpc_dictionary_create_reply(event);
288 secdebug(PROXYXPCSCOPE, "can't create replyMessage");
289 assert(true); //must have a reply handler
292 xpc_object_t returnedValues = xpc_dictionary_create(NULL, NULL, 0);
295 secdebug(PROXYXPCSCOPE, "can't create returnedValues");
296 assert(true); // must have a spot for the returned values
300 xpc_object_t xvalue = xpc_dictionary_get_value(event, kMessageKeyValue);
303 secdebug(PROXYXPCSCOPE, "missing \"value\" key");
307 xpc_object_t xkeystoget = xpc_dictionary_get_value(xvalue, kMessageKeyKeysToGet);
310 secdebug(PROXYXPCSCOPE, "got xkeystoget");
311 CFTypeRef keystoget = _CFXPCCreateCFObjectFromXPCObject(xkeystoget);
312 if (!keystoget || (CFGetTypeID(keystoget)!=CFArrayGetTypeID())) // not "getAll", this is an error of some kind
314 secdebug(PROXYXPCSCOPE, "can't convert keystoget or is not an array");
315 CFReleaseSafe(keystoget);
319 [(__bridge NSArray *)keystoget enumerateObjectsUsingBlock: ^ (id obj, NSUInteger idx, BOOL *stop)
321 NSString *key = (NSString *)obj;
322 id object = [[UbiqitousKVSProxy sharedKVSProxy] get:key];
323 secdebug(PROXYXPCSCOPE, "[UbiqitousKVSProxy sharedKVSProxy] get: key: %@, object: %@", key, object);
324 xpc_object_t xobject = object ? _CFXPCCreateXPCObjectFromCFObject((__bridge CFTypeRef)object) : xpc_null_create();
325 xpc_dictionary_set_value(returnedValues, [key UTF8String], xobject);
326 describeXPCObject("operation_get_v2: value from kvs: ", xobject);
329 else // get all values from kvs
331 secdebug(PROXYXPCSCOPE, "get all values from kvs");
332 NSDictionary *all = [[UbiqitousKVSProxy sharedKVSProxy] getAll];
333 [all enumerateKeysAndObjectsUsingBlock: ^ (id key, id obj, BOOL *stop)
335 xpc_object_t xobject = obj ? _CFXPCCreateXPCObjectFromCFObject((__bridge CFTypeRef)obj) : xpc_null_create();
336 xpc_dictionary_set_value(returnedValues, [(NSString *)key UTF8String], xobject);
340 xpc_dictionary_set_uint64(replyMessage, kMessageKeyVersion, kCKDXPCVersion);
341 xpc_dictionary_set_value(replyMessage, kMessageKeyValue, returnedValues);
342 xpc_connection_send_message(peer, replyMessage);
347 static void initializeProxyObjectWithConnection(const xpc_connection_t connection)
349 [[UbiqitousKVSProxy sharedKVSProxy] setItemsChangedBlock:^CFArrayRef(CFDictionaryRef values)
351 secdebug(PROXYXPCSCOPE, "UbiqitousKVSProxy called back");
352 xpc_object_t xobj = _CFXPCCreateXPCObjectFromCFObject(values);
353 xpc_object_t message = xpc_dictionary_create(NULL, NULL, 0);
354 xpc_dictionary_set_uint64(message, kMessageKeyVersion, kCKDXPCVersion);
355 xpc_dictionary_set_string(message, kMessageKeyOperation, kMessageOperationItemChanged);
356 xpc_dictionary_set_value(message, kMessageKeyValue, xobj?xobj:xpc_null_create());
357 xpc_connection_send_message(connection, message); // Send message; don't wait for a reply
362 static void cloudkeychainproxy_peer_event_handler(xpc_connection_t peer, xpc_object_t event)
364 describeXPCObject("peer: ", peer);
365 xpc_type_t type = xpc_get_type(event);
366 if (type == XPC_TYPE_ERROR) {
367 if (event == XPC_ERROR_CONNECTION_INVALID) {
368 // The client process on the other end of the connection has either
369 // crashed or cancelled the connection. After receiving this error,
370 // the connection is in an invalid state, and you do not need to
371 // call xpc_connection_cancel(). Just tear down any associated state
373 } else if (event == XPC_ERROR_TERMINATION_IMMINENT) {
374 // Handle per-connection termination cleanup.
377 assert(type == XPC_TYPE_DICTIONARY);
378 // Handle the message.
379 // describeXPCObject("dictionary:", event);
380 dispatch_async(dispatch_get_main_queue(), ^{
381 cloudkeychainproxy_peer_dictionary_handler(peer, event);
386 static void cloudkeychainproxy_event_handler(xpc_connection_t peer)
388 // By defaults, new connections will target the default dispatch
391 if (xpc_get_type(peer) != XPC_TYPE_CONNECTION)
393 secdebug(PROXYXPCSCOPE, "expected XPC_TYPE_CONNECTION");
396 initializeProxyObjectWithConnection(peer);
397 xpc_connection_set_event_handler(peer, ^(xpc_object_t event)
399 cloudkeychainproxy_peer_event_handler(peer, event);
402 // This will tell the connection to begin listening for events. If you
403 // have some other initialization that must be done asynchronously, then
404 // you can defer this call until after that initialization is done.
405 xpc_connection_resume(peer);
408 static void diagnostics(int argc, const char *argv[])
412 NSDictionary *all = [[UbiqitousKVSProxy sharedKVSProxy] getAll];
413 NSLog(@"All: %@",all);
417 int ckdproxymain(int argc, const char *argv[])
419 secdebug(PROXYXPCSCOPE, "Starting CloudKeychainProxy");
420 char *wait4debugger = getenv("WAIT4DEBUGGER");
422 if (wait4debugger && !strcasecmp("YES", wait4debugger))
424 syslog(LOG_ERR, "Waiting for debugger");
425 kill(getpid(), SIGTSTP);
429 diagnostics(argc, argv);
433 // DISPATCH_TARGET_QUEUE_DEFAULT
434 xpc_connection_t listener = xpc_connection_create_mach_service(xpcServiceName, NULL, XPC_CONNECTION_MACH_SERVICE_LISTENER);
435 xpc_connection_set_event_handler(listener, ^(xpc_object_t object){ cloudkeychainproxy_event_handler(object); });
437 [UbiqitousKVSProxy sharedKVSProxy];
439 // 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.
440 // Therefore I'm leaving the XPC connection suspended until that has time to process.
441 xpc_connection_resume(listener);
445 secdebug(PROXYXPCSCOPE, "Starting mainRunLoop");
446 NSRunLoop *runLoop = [NSRunLoop mainRunLoop];
450 secdebug(PROXYXPCSCOPE, "Exiting CloudKeychainProxy");