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>
53 #include <TargetConditionals.h>
55 #import <Foundation/Foundation.h>
56 #import <Security/Security.h>
57 #import <utilities/SecCFRelease.h>
59 #import <xpc/private.h>
60 #import <CoreFoundation/CFXPCBridge.h>
63 #import <CommonCrypto/CommonDigest.h>
64 #include <utilities/SecXPCError.h>
65 #include <utilities/SecCFError.h>
67 #include <utilities/SecFileLocations.h>
69 #import "CKDKVSProxy.h"
70 #import "CKDSecuritydAccount.h"
71 #import "CKDKVSStore.h"
72 #import "CKDAKSLockMonitor.h"
75 void finalize_connection(void *not_used);
76 void handle_connection_event(const xpc_connection_t peer);
77 static void cloudkeychainproxy_peer_dictionary_handler(const xpc_connection_t peer, xpc_object_t event);
79 static bool operation_put_dictionary(xpc_object_t event);
80 static bool operation_get_v2(xpc_connection_t peer, xpc_object_t event);
82 int ckdproxymain(int argc, const char *argv[]);
84 #define PROXYXPCSCOPE "xpcproxy"
86 static void describeXPCObject(char *prefix, xpc_object_t object)
89 // This is useful for debugging.
92 char *desc = xpc_copy_description(object);
93 secdebug(PROXYXPCSCOPE, "%s%s\n", prefix, desc);
97 secdebug(PROXYXPCSCOPE, "%s<NULL>\n", prefix);
102 static NSObject *CreateNSObjectForCFXPCObjectFromKey(xpc_object_t xdict, const char * _Nonnull key)
104 xpc_object_t xObj = xpc_dictionary_get_value(xdict, key);
110 return (__bridge_transfer NSObject *)(_CFXPCCreateCFObjectFromXPCObject(xObj));
113 static NSArray<NSString*> *CreateArrayOfStringsForCFXPCObjectFromKey(xpc_object_t xdict, const char * _Nonnull key) {
114 NSObject * possibleArray = CreateNSObjectForCFXPCObjectFromKey(xdict, key);
116 if (![possibleArray isNSArray__])
119 __block bool onlyStrings = true;
120 [(NSArray*) possibleArray enumerateObjectsUsingBlock:^(id _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
121 if (![obj isNSString__]) {
127 return onlyStrings ? (NSArray<NSString*>*) possibleArray : nil;
130 static CFStringRef kRegistrationFileName = CFSTR("com.apple.security.cloudkeychainproxy3.keysToRegister.plist");
132 static UbiqitousKVSProxy *SharedProxy(void) {
133 static UbiqitousKVSProxy *sProxy = NULL;
134 static dispatch_once_t onceToken;
135 dispatch_once(&onceToken, ^{
136 sProxy = [UbiqitousKVSProxy withAccount: [CKDSecuritydAccount securitydAccount]
137 store: [CKDKVSStore kvsInterface]
138 lockMonitor: [CKDAKSLockMonitor monitor]
139 persistence: (NSURL *)CFBridgingRelease(SecCopyURLForFileInPreferencesDirectory(kRegistrationFileName))];
145 static void sendAckResponse(const xpc_connection_t peer, xpc_object_t event) {
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);
154 static void cloudkeychainproxy_peer_dictionary_handler(const xpc_connection_t peer, xpc_object_t event)
159 require_action_string(xpc_get_type(event) == XPC_TYPE_DICTIONARY, xit, err = -51, "expected XPC_TYPE_DICTIONARY");
161 const char *operation = xpc_dictionary_get_string(event, kMessageKeyOperation);
162 require_action(operation, xit, result = false);
164 // Check protocol version
165 uint64_t version = xpc_dictionary_get_uint64(event, kMessageKeyVersion);
166 secdebug(PROXYXPCSCOPE, "Reply version: %lld\n", version);
167 require_action(version == kCKDXPCVersion, xit, result = false);
170 secdebug(PROXYXPCSCOPE, "Handling %s operation", operation);
173 if (operation && !strcmp(operation, kOperationPUTDictionary))
175 operation_put_dictionary(event);
176 sendAckResponse(peer, event);
178 else if (operation && !strcmp(operation, kOperationGETv2))
180 operation_get_v2(peer, event);
181 // operationg_get_v2 sends the response
183 else if (operation && !strcmp(operation, kOperationClearStore))
185 [SharedProxy() clearStore];
186 sendAckResponse(peer, event);
188 else if (operation && !strcmp(operation, kOperationSynchronize))
190 [SharedProxy() synchronizeStore];
191 sendAckResponse(peer, event);
193 else if (operation && !strcmp(operation, kOperationSynchronizeAndWait))
195 xpc_object_t replyMessage = xpc_dictionary_create_reply(event);
196 secnotice(XPROXYSCOPE, "%s XPC request: %s", kWAIT2MINID, kOperationSynchronizeAndWait);
198 [SharedProxy() waitForSynchronization:^(__unused NSDictionary *values, NSError *error)
200 secnotice(PROXYXPCSCOPE, "%s Result from [Proxy waitForSynchronization:]: %@", kWAIT2MINID, error);
202 if (replyMessage) // Caller wanted an ACK, so give one
206 xpc_object_t xerrobj = SecCreateXPCObjectWithCFError((__bridge CFErrorRef)(error));
207 xpc_dictionary_set_value(replyMessage, kMessageKeyError, xerrobj);
209 xpc_dictionary_set_string(replyMessage, kMessageKeyValue, "ACK");
211 xpc_connection_send_message(peer, replyMessage);
215 else if (operation && !strcmp(operation, kOperationRegisterKeys))
217 xpc_object_t xkeysToRegisterDict = xpc_dictionary_get_value(event, kMessageKeyValue);
219 xpc_object_t xKTRallkeys = xpc_dictionary_get_value(xkeysToRegisterDict, kMessageAllKeys);
221 NSString* accountUUID = (NSString*) CreateNSObjectForCFXPCObjectFromKey(event, kMessageKeyAccountUUID);
223 if (![accountUUID isKindOfClass:[NSString class]]) {
227 NSDictionary *KTRallkeys = (__bridge_transfer NSDictionary *)(_CFXPCCreateCFObjectFromXPCObject(xKTRallkeys));
229 [SharedProxy() registerKeys: KTRallkeys forAccount: accountUUID];
230 sendAckResponse(peer, event);
232 secdebug(PROXYXPCSCOPE, "RegisterKeys message sent");
234 else if (operation && !strcmp(operation, kOperationRequestSyncWithPeers))
237 NSArray<NSString*> * peerIDs = CreateArrayOfStringsForCFXPCObjectFromKey(event, kMessageKeyPeerIDList);
238 NSArray<NSString*> * backupPeerIDs = CreateArrayOfStringsForCFXPCObjectFromKey(event, kMesssgeKeyBackupPeerIDList);
240 require_action(peerIDs && backupPeerIDs, xit, (secnotice(XPROXYSCOPE, "Bad call to sync with peers"), result = false));
242 [SharedProxy() requestSyncWithPeerIDs: peerIDs backupPeerIDs: backupPeerIDs];
243 sendAckResponse(peer, event);
245 secdebug(PROXYXPCSCOPE, "RequestSyncWithAllPeers reply sent");
247 else if (operation && !strcmp(operation, kOperationHasPendingSyncWithPeer)) {
248 NSString *peerID = (NSString*) CreateNSObjectForCFXPCObjectFromKey(event, kMessageKeyPeerID);
250 BOOL hasPending = [SharedProxy() hasSyncPendingFor: peerID];
252 xpc_object_t replyMessage = xpc_dictionary_create_reply(event);
255 xpc_dictionary_set_bool(replyMessage, kMessageKeyValue, hasPending);
256 xpc_connection_send_message(peer, replyMessage);
257 secdebug(PROXYXPCSCOPE, "HasPendingSyncWithPeer reply sent");
260 else if (operation && !strcmp(operation, kOperationHasPendingKey)) {
261 NSString *peerID = (NSString*) CreateNSObjectForCFXPCObjectFromKey(event, kMessageKeyPeerID);
263 BOOL hasPending = [SharedProxy() hasPendingKey: peerID];
265 xpc_object_t replyMessage = xpc_dictionary_create_reply(event);
268 xpc_dictionary_set_bool(replyMessage, kMessageKeyValue, hasPending);
269 xpc_connection_send_message(peer, replyMessage);
270 secdebug(PROXYXPCSCOPE, "HasIncomingMessageFromPeer reply sent");
273 else if (operation && !strcmp(operation, kOperationRequestEnsurePeerRegistration))
275 [SharedProxy() requestEnsurePeerRegistration];
276 sendAckResponse(peer, event);
277 secdebug(PROXYXPCSCOPE, "RequestEnsurePeerRegistration reply sent");
279 else if (operation && !strcmp(operation, kOperationFlush))
281 [SharedProxy() doAfterFlush:^{
282 sendAckResponse(peer, event);
283 secdebug(PROXYXPCSCOPE, "flush reply sent");
288 char *description = xpc_copy_description(event);
289 secdebug(PROXYXPCSCOPE, "Unknown op=%s request from pid %d: %s", operation, xpc_connection_get_pid(peer), description);
295 describeXPCObject("handle_operation fail: ", event);
298 void finalize_connection(void *not_used)
300 secdebug(PROXYXPCSCOPE, "finalize_connection");
301 [SharedProxy() synchronizeStore];
302 xpc_transaction_end();
305 static bool operation_put_dictionary(xpc_object_t event)
307 // PUT a set of objects into the KVS store. Return false if error
308 describeXPCObject("operation_put_dictionary event: ", event);
309 xpc_object_t xvalue = xpc_dictionary_get_value(event, kMessageKeyValue);
314 NSObject* object = (__bridge_transfer NSObject*) _CFXPCCreateCFObjectFromXPCObject(xvalue);
315 if (![object isKindOfClass:[NSDictionary<NSString*, NSObject*> class]]) {
316 describeXPCObject("operation_put_dictionary unable to convert to CF: ", xvalue);
320 [SharedProxy() setObjectsFromDictionary: (NSDictionary<NSString*, NSObject*> *)object];
325 static bool operation_get_v2(xpc_connection_t peer, xpc_object_t event)
327 // GET a set of objects from the KVS store. Return false if error
328 describeXPCObject("operation_get_v2 event: ", event);
330 xpc_object_t replyMessage = xpc_dictionary_create_reply(event);
333 secdebug(PROXYXPCSCOPE, "can't create replyMessage");
334 assert(true); //must have a reply handler
337 xpc_object_t returnedValues = xpc_dictionary_create(NULL, NULL, 0);
340 secdebug(PROXYXPCSCOPE, "can't create returnedValues");
341 assert(true); // must have a spot for the returned values
345 xpc_object_t xvalue = xpc_dictionary_get_value(event, kMessageKeyValue);
348 secdebug(PROXYXPCSCOPE, "missing \"value\" key");
352 xpc_object_t xkeystoget = xpc_dictionary_get_value(xvalue, kMessageKeyKeysToGet);
355 secdebug(PROXYXPCSCOPE, "got xkeystoget");
356 CFTypeRef keystoget = _CFXPCCreateCFObjectFromXPCObject(xkeystoget);
357 if (!keystoget || (CFGetTypeID(keystoget)!=CFArrayGetTypeID())) // not "getAll", this is an error of some kind
359 secdebug(PROXYXPCSCOPE, "can't convert keystoget or is not an array");
360 CFReleaseSafe(keystoget);
364 [(__bridge NSArray *)keystoget enumerateObjectsUsingBlock: ^ (id obj, NSUInteger idx, BOOL *stop)
366 NSString *key = (NSString *)obj;
367 id object = [SharedProxy() objectForKey:key];
368 secdebug(PROXYXPCSCOPE, "get: key: %@, object: %@", key, object);
369 xpc_object_t xobject = object ? _CFXPCCreateXPCObjectFromCFObject((__bridge CFTypeRef)object) : xpc_null_create();
370 xpc_dictionary_set_value(returnedValues, [key UTF8String], xobject);
371 describeXPCObject("operation_get_v2: value from kvs: ", xobject);
374 else // get all values from kvs
376 secdebug(PROXYXPCSCOPE, "get all values from kvs");
377 NSDictionary *all = [SharedProxy() copyAsDictionary];
378 [all enumerateKeysAndObjectsUsingBlock: ^ (id key, id obj, BOOL *stop)
380 xpc_object_t xobject = obj ? _CFXPCCreateXPCObjectFromCFObject((__bridge CFTypeRef)obj) : xpc_null_create();
381 xpc_dictionary_set_value(returnedValues, [(NSString *)key UTF8String], xobject);
385 xpc_dictionary_set_uint64(replyMessage, kMessageKeyVersion, kCKDXPCVersion);
386 xpc_dictionary_set_value(replyMessage, kMessageKeyValue, returnedValues);
387 xpc_connection_send_message(peer, replyMessage);
392 static void cloudkeychainproxy_event_handler(xpc_connection_t peer)
394 if (xpc_get_type(peer) != XPC_TYPE_CONNECTION)
396 secdebug(PROXYXPCSCOPE, "expected XPC_TYPE_CONNECTION");
400 xpc_connection_set_target_queue(peer, [SharedProxy() ckdkvsproxy_queue]);
401 xpc_connection_set_event_handler(peer, ^(xpc_object_t event)
403 describeXPCObject("peer: ", peer); // Only describes under debug
405 // We could handle other peer events (e.g.) disconnects,
406 // but we don't keep per-client state so there is no need.
407 if (xpc_get_type(event) == XPC_TYPE_DICTIONARY) {
408 cloudkeychainproxy_peer_dictionary_handler(peer, event);
412 // This will tell the connection to begin listening for events. If you
413 // have some other initialization that must be done asynchronously, then
414 // you can defer this call until after that initialization is done.
415 xpc_connection_resume(peer);
418 static void diagnostics(int argc, const char *argv[])
422 NSDictionary *all = [SharedProxy() copyAsDictionary];
423 NSLog(@"All: %@",all);
427 int ckdproxymain(int argc, const char *argv[])
429 secdebug(PROXYXPCSCOPE, "Starting CloudKeychainProxy");
430 char *wait4debugger = getenv("WAIT4DEBUGGER");
432 if (wait4debugger && !strcasecmp("YES", wait4debugger))
434 syslog(LOG_ERR, "Waiting for debugger");
435 kill(getpid(), SIGTSTP);
439 diagnostics(argc, argv);
443 UbiqitousKVSProxy* proxyID = SharedProxy();
445 if (proxyID) { // nothing bad happened when initializing
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 // 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.
450 // Therefore I'm leaving the XPC connection suspended until that has time to process.
451 xpc_connection_resume(listener);
455 secdebug(PROXYXPCSCOPE, "Starting mainRunLoop");
456 NSRunLoop *runLoop = [NSRunLoop mainRunLoop];
461 secdebug(PROXYXPCSCOPE, "Exiting CloudKeychainProxy");
466 int main(int argc, const char *argv[])
468 return ckdproxymain(argc, argv);