]> git.saurik.com Git - apple/security.git/blob - KVSKeychainSyncingProxy/cloudkeychainproxy.m
Security-57740.60.18.tar.gz
[apple/security.git] / KVSKeychainSyncingProxy / cloudkeychainproxy.m
1 /*
2 * Copyright (c) 2012-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 // main.m
26 // ckd-xpc
27 //
28 //
29
30 /*
31 This XPC service is essentially just a proxy to iCloud KVS, which exists since
32 the main security code cannot link against Foundation.
33
34 See sendTSARequestWithXPC in tsaSupport.c for how to call the service
35
36 send message to app with xpc_connection_send_message
37
38 For now, build this with:
39
40 ~rc/bin/buildit . --rootsDirectory=/var/tmp -noverify -offline -target CloudKeychainProxy
41
42 and install or upgrade with:
43
44 darwinup install /var/tmp/sec.roots/sec~dst
45 darwinup upgrade /var/tmp/sec.roots/sec~dst
46
47 You must use darwinup during development to update system caches
48 */
49
50 //------------------------------------------------------------------------------------------------
51
52 #include <AssertMacros.h>
53 #include <TargetConditionals.h>
54
55 #import <Foundation/Foundation.h>
56 #import <Security/Security.h>
57 #import <utilities/SecCFRelease.h>
58 #import <xpc/xpc.h>
59 #import <xpc/private.h>
60 #import <CoreFoundation/CFXPCBridge.h>
61 #import <sysexits.h>
62 #import <syslog.h>
63 #import <CommonCrypto/CommonDigest.h>
64 #include <utilities/SecXPCError.h>
65 #include <utilities/SecCFError.h>
66
67 #include <utilities/SecFileLocations.h>
68
69 #import "CKDKVSProxy.h"
70 #import "CKDSecuritydAccount.h"
71 #import "CKDKVSStore.h"
72 #import "CKDAKSLockMonitor.h"
73
74
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);
78
79 static bool operation_put_dictionary(xpc_object_t event);
80 static bool operation_get_v2(xpc_connection_t peer, xpc_object_t event);
81
82 int ckdproxymain(int argc, const char *argv[]);
83
84 #define PROXYXPCSCOPE "xpcproxy"
85
86 static void describeXPCObject(char *prefix, xpc_object_t object)
87 {
88 //#ifndef NDEBUG
89 // This is useful for debugging.
90 if (object)
91 {
92 char *desc = xpc_copy_description(object);
93 secdebug(PROXYXPCSCOPE, "%s%s\n", prefix, desc);
94 free(desc);
95 }
96 else
97 secdebug(PROXYXPCSCOPE, "%s<NULL>\n", prefix);
98
99 //#endif
100 }
101
102 static NSObject *CreateNSObjectForCFXPCObjectFromKey(xpc_object_t xdict, const char * _Nonnull key)
103 {
104 xpc_object_t xObj = xpc_dictionary_get_value(xdict, key);
105
106 if (!xObj) {
107 return nil;
108 }
109
110 return (__bridge_transfer NSObject *)(_CFXPCCreateCFObjectFromXPCObject(xObj));
111 }
112
113 static NSArray<NSString*> *CreateArrayOfStringsForCFXPCObjectFromKey(xpc_object_t xdict, const char * _Nonnull key) {
114 NSObject * possibleArray = CreateNSObjectForCFXPCObjectFromKey(xdict, key);
115
116 if (![possibleArray isNSArray__])
117 return nil;
118
119 __block bool onlyStrings = true;
120 [(NSArray*) possibleArray enumerateObjectsUsingBlock:^(id _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
121 if (![obj isNSString__]) {
122 *stop = true;
123 onlyStrings = false;
124 }
125 }];
126
127 return onlyStrings ? (NSArray<NSString*>*) possibleArray : nil;
128 }
129
130 static CFStringRef kRegistrationFileName = CFSTR("com.apple.security.cloudkeychainproxy3.keysToRegister.plist");
131
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))];
140 });
141
142 return sProxy;
143 }
144
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
148 {
149 xpc_dictionary_set_string(replyMessage, kMessageKeyValue, "ACK");
150 xpc_connection_send_message(peer, replyMessage);
151 }
152 }
153
154 static void cloudkeychainproxy_peer_dictionary_handler(const xpc_connection_t peer, xpc_object_t event)
155 {
156 bool result = false;
157 int err = 0;
158
159 require_action_string(xpc_get_type(event) == XPC_TYPE_DICTIONARY, xit, err = -51, "expected XPC_TYPE_DICTIONARY");
160
161 const char *operation = xpc_dictionary_get_string(event, kMessageKeyOperation);
162 require_action(operation, xit, result = false);
163
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);
168
169 // Operations
170 secdebug(PROXYXPCSCOPE, "Handling %s operation", operation);
171
172
173 if (operation && !strcmp(operation, kOperationPUTDictionary))
174 {
175 operation_put_dictionary(event);
176 sendAckResponse(peer, event);
177 }
178 else if (operation && !strcmp(operation, kOperationGETv2))
179 {
180 operation_get_v2(peer, event);
181 // operationg_get_v2 sends the response
182 }
183 else if (operation && !strcmp(operation, kOperationClearStore))
184 {
185 [SharedProxy() clearStore];
186 sendAckResponse(peer, event);
187 }
188 else if (operation && !strcmp(operation, kOperationSynchronize))
189 {
190 [SharedProxy() synchronizeStore];
191 sendAckResponse(peer, event);
192 }
193 else if (operation && !strcmp(operation, kOperationSynchronizeAndWait))
194 {
195 xpc_object_t replyMessage = xpc_dictionary_create_reply(event);
196 secnotice(XPROXYSCOPE, "%s XPC request: %s", kWAIT2MINID, kOperationSynchronizeAndWait);
197
198 [SharedProxy() waitForSynchronization:^(__unused NSDictionary *values, NSError *error)
199 {
200 secnotice(PROXYXPCSCOPE, "%s Result from [Proxy waitForSynchronization:]: %@", kWAIT2MINID, error);
201
202 if (replyMessage) // Caller wanted an ACK, so give one
203 {
204 if (error)
205 {
206 xpc_object_t xerrobj = SecCreateXPCObjectWithCFError((__bridge CFErrorRef)(error));
207 xpc_dictionary_set_value(replyMessage, kMessageKeyError, xerrobj);
208 } else {
209 xpc_dictionary_set_string(replyMessage, kMessageKeyValue, "ACK");
210 }
211 xpc_connection_send_message(peer, replyMessage);
212 }
213 }];
214 }
215 else if (operation && !strcmp(operation, kOperationRegisterKeys))
216 {
217 xpc_object_t xkeysToRegisterDict = xpc_dictionary_get_value(event, kMessageKeyValue);
218
219 xpc_object_t xKTRallkeys = xpc_dictionary_get_value(xkeysToRegisterDict, kMessageAllKeys);
220
221 NSString* accountUUID = (NSString*) CreateNSObjectForCFXPCObjectFromKey(event, kMessageKeyAccountUUID);
222
223 if (![accountUUID isKindOfClass:[NSString class]]) {
224 accountUUID = nil;
225 }
226
227 NSDictionary *KTRallkeys = (__bridge_transfer NSDictionary *)(_CFXPCCreateCFObjectFromXPCObject(xKTRallkeys));
228
229 [SharedProxy() registerKeys: KTRallkeys forAccount: accountUUID];
230 sendAckResponse(peer, event);
231
232 secdebug(PROXYXPCSCOPE, "RegisterKeys message sent");
233 }
234 else if (operation && !strcmp(operation, kOperationRequestSyncWithPeers))
235 {
236
237 NSArray<NSString*> * peerIDs = CreateArrayOfStringsForCFXPCObjectFromKey(event, kMessageKeyPeerIDList);
238 NSArray<NSString*> * backupPeerIDs = CreateArrayOfStringsForCFXPCObjectFromKey(event, kMesssgeKeyBackupPeerIDList);
239
240 require_action(peerIDs && backupPeerIDs, xit, (secnotice(XPROXYSCOPE, "Bad call to sync with peers"), result = false));
241
242 [SharedProxy() requestSyncWithPeerIDs: peerIDs backupPeerIDs: backupPeerIDs];
243 sendAckResponse(peer, event);
244
245 secdebug(PROXYXPCSCOPE, "RequestSyncWithAllPeers reply sent");
246 }
247 else if (operation && !strcmp(operation, kOperationHasPendingSyncWithPeer)) {
248 NSString *peerID = (NSString*) CreateNSObjectForCFXPCObjectFromKey(event, kMessageKeyPeerID);
249
250 BOOL hasPending = [SharedProxy() hasSyncPendingFor: peerID];
251
252 xpc_object_t replyMessage = xpc_dictionary_create_reply(event);
253 if (replyMessage)
254 {
255 xpc_dictionary_set_bool(replyMessage, kMessageKeyValue, hasPending);
256 xpc_connection_send_message(peer, replyMessage);
257 secdebug(PROXYXPCSCOPE, "HasPendingSyncWithPeer reply sent");
258 }
259 }
260 else if (operation && !strcmp(operation, kOperationHasPendingKey)) {
261 NSString *peerID = (NSString*) CreateNSObjectForCFXPCObjectFromKey(event, kMessageKeyPeerID);
262
263 BOOL hasPending = [SharedProxy() hasPendingKey: peerID];
264
265 xpc_object_t replyMessage = xpc_dictionary_create_reply(event);
266 if (replyMessage)
267 {
268 xpc_dictionary_set_bool(replyMessage, kMessageKeyValue, hasPending);
269 xpc_connection_send_message(peer, replyMessage);
270 secdebug(PROXYXPCSCOPE, "HasIncomingMessageFromPeer reply sent");
271 }
272 }
273 else if (operation && !strcmp(operation, kOperationRequestEnsurePeerRegistration))
274 {
275 [SharedProxy() requestEnsurePeerRegistration];
276 sendAckResponse(peer, event);
277 secdebug(PROXYXPCSCOPE, "RequestEnsurePeerRegistration reply sent");
278 }
279 else if (operation && !strcmp(operation, kOperationFlush))
280 {
281 [SharedProxy() doAfterFlush:^{
282 sendAckResponse(peer, event);
283 secdebug(PROXYXPCSCOPE, "flush reply sent");
284 }];
285 }
286 else
287 {
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);
290 free(description);
291 }
292 result = true;
293 xit:
294 if (!result)
295 describeXPCObject("handle_operation fail: ", event);
296 }
297
298 void finalize_connection(void *not_used)
299 {
300 secdebug(PROXYXPCSCOPE, "finalize_connection");
301 [SharedProxy() synchronizeStore];
302 xpc_transaction_end();
303 }
304
305 static bool operation_put_dictionary(xpc_object_t event)
306 {
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);
310 if (!xvalue) {
311 return false;
312 }
313
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);
317 return false;
318 }
319
320 [SharedProxy() setObjectsFromDictionary: (NSDictionary<NSString*, NSObject*> *)object];
321
322 return true;
323 }
324
325 static bool operation_get_v2(xpc_connection_t peer, xpc_object_t event)
326 {
327 // GET a set of objects from the KVS store. Return false if error
328 describeXPCObject("operation_get_v2 event: ", event);
329
330 xpc_object_t replyMessage = xpc_dictionary_create_reply(event);
331 if (!replyMessage)
332 {
333 secdebug(PROXYXPCSCOPE, "can't create replyMessage");
334 assert(true); //must have a reply handler
335 return false;
336 }
337 xpc_object_t returnedValues = xpc_dictionary_create(NULL, NULL, 0);
338 if (!returnedValues)
339 {
340 secdebug(PROXYXPCSCOPE, "can't create returnedValues");
341 assert(true); // must have a spot for the returned values
342 return false;
343 }
344
345 xpc_object_t xvalue = xpc_dictionary_get_value(event, kMessageKeyValue);
346 if (!xvalue)
347 {
348 secdebug(PROXYXPCSCOPE, "missing \"value\" key");
349 return false;
350 }
351
352 xpc_object_t xkeystoget = xpc_dictionary_get_value(xvalue, kMessageKeyKeysToGet);
353 if (xkeystoget)
354 {
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
358 {
359 secdebug(PROXYXPCSCOPE, "can't convert keystoget or is not an array");
360 CFReleaseSafe(keystoget);
361 return false;
362 }
363
364 [(__bridge NSArray *)keystoget enumerateObjectsUsingBlock: ^ (id obj, NSUInteger idx, BOOL *stop)
365 {
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);
372 }];
373 }
374 else // get all values from kvs
375 {
376 secdebug(PROXYXPCSCOPE, "get all values from kvs");
377 NSDictionary *all = [SharedProxy() copyAsDictionary];
378 [all enumerateKeysAndObjectsUsingBlock: ^ (id key, id obj, BOOL *stop)
379 {
380 xpc_object_t xobject = obj ? _CFXPCCreateXPCObjectFromCFObject((__bridge CFTypeRef)obj) : xpc_null_create();
381 xpc_dictionary_set_value(returnedValues, [(NSString *)key UTF8String], xobject);
382 }];
383 }
384
385 xpc_dictionary_set_uint64(replyMessage, kMessageKeyVersion, kCKDXPCVersion);
386 xpc_dictionary_set_value(replyMessage, kMessageKeyValue, returnedValues);
387 xpc_connection_send_message(peer, replyMessage);
388
389 return true;
390 }
391
392 static void cloudkeychainproxy_event_handler(xpc_connection_t peer)
393 {
394 if (xpc_get_type(peer) != XPC_TYPE_CONNECTION)
395 {
396 secdebug(PROXYXPCSCOPE, "expected XPC_TYPE_CONNECTION");
397 return;
398 }
399
400 xpc_connection_set_target_queue(peer, [SharedProxy() ckdkvsproxy_queue]);
401 xpc_connection_set_event_handler(peer, ^(xpc_object_t event)
402 {
403 describeXPCObject("peer: ", peer); // Only describes under debug
404
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);
409 }
410 });
411
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);
416 }
417
418 static void diagnostics(int argc, const char *argv[])
419 {
420 @autoreleasepool
421 {
422 NSDictionary *all = [SharedProxy() copyAsDictionary];
423 NSLog(@"All: %@",all);
424 }
425 }
426
427 int ckdproxymain(int argc, const char *argv[])
428 {
429 secdebug(PROXYXPCSCOPE, "Starting CloudKeychainProxy");
430 char *wait4debugger = getenv("WAIT4DEBUGGER");
431
432 if (wait4debugger && !strcasecmp("YES", wait4debugger))
433 {
434 syslog(LOG_ERR, "Waiting for debugger");
435 kill(getpid(), SIGTSTP);
436 }
437
438 if (argc > 1) {
439 diagnostics(argc, argv);
440 return 0;
441 }
442
443 UbiqitousKVSProxy* proxyID = SharedProxy();
444
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); });
448
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);
452
453 @autoreleasepool
454 {
455 secdebug(PROXYXPCSCOPE, "Starting mainRunLoop");
456 NSRunLoop *runLoop = [NSRunLoop mainRunLoop];
457 [runLoop run];
458 }
459 }
460
461 secdebug(PROXYXPCSCOPE, "Exiting CloudKeychainProxy");
462
463 return EXIT_FAILURE;
464 }
465
466 int main(int argc, const char *argv[])
467 {
468 return ckdproxymain(argc, argv);
469 }