]>
Commit | Line | Data |
---|---|---|
427c49bc | 1 | /* |
d8f41ccd | 2 | * Copyright (c) 2012-2014 Apple Inc. All Rights Reserved. |
427c49bc A |
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 | SOSCloudTransport.c - Implementation of the transport layer from CKBridge to SOSAccount/SOSCircle | |
26 | These are the exported functions from CloudKeychainProxy | |
27 | */ | |
28 | ||
29 | /* | |
30 | This XPC service is essentially just a proxy to iCloud KVS, which exists since | |
31 | the main security code cannot link against Foundation. | |
32 | ||
33 | See sendTSARequestWithXPC in tsaSupport.c for how to call the service | |
34 | ||
35 | The client of an XPC service does not get connection events, nor does it | |
36 | need to deal with transactions. | |
37 | */ | |
38 | ||
39 | #include <AssertMacros.h> | |
40 | ||
41 | #include <xpc/xpc.h> | |
42 | #include <CoreFoundation/CoreFoundation.h> | |
43 | #include <CoreFoundation/CFXPCBridge.h> | |
44 | #include <sysexits.h> | |
45 | #include <syslog.h> | |
5c19dc3a | 46 | #include <os/activity.h> |
427c49bc A |
47 | #include <CoreFoundation/CFUserNotification.h> |
48 | ||
49 | #include <utilities/debugging.h> | |
50 | #include <utilities/SecCFWrappers.h> | |
51 | #include <utilities/SecXPCError.h> | |
52 | ||
53 | #include "SOSCloudKeychainConstants.h" | |
54 | #include "SOSCloudKeychainClient.h" | |
55 | ||
56 | static CFStringRef sErrorDomain = CFSTR("com.apple.security.sos.transport.error"); | |
57 | ||
58 | #define SOSCKCSCOPE "sync" | |
59 | ||
60 | // MARK: ---------- SOSCloudTransport ---------- | |
61 | ||
62 | /* SOSCloudTransport, a statically initialized transport singleton. */ | |
63 | static SOSCloudTransportRef sTransport = NULL; | |
64 | ||
65 | static SOSCloudTransportRef SOSCloudTransportCreateXPCTransport(void); | |
66 | ||
67 | void SOSCloudKeychainSetTransport(SOSCloudTransportRef transport) { | |
68 | sTransport = transport; | |
69 | } | |
70 | ||
71 | /* Return the singleton cloud transport instance. */ | |
72 | static SOSCloudTransportRef SOSCloudTransportDefaultTransport(void) | |
73 | { | |
74 | static dispatch_once_t sTransportOnce; | |
75 | dispatch_once(&sTransportOnce, ^{ | |
76 | if (!sTransport) | |
77 | SOSCloudKeychainSetTransport(SOSCloudTransportCreateXPCTransport()); | |
78 | }); | |
79 | return sTransport; | |
80 | } | |
81 | ||
82 | ||
83 | // MARK: ----- utilities ----- | |
84 | ||
85 | static CFErrorRef makeError(CFIndex which) | |
86 | { | |
87 | CFDictionaryRef userInfo = NULL; | |
88 | return CFErrorCreate(kCFAllocatorDefault, sErrorDomain, which, userInfo); | |
89 | } | |
90 | ||
91 | // MARK: ----- DEBUG Utilities ----- | |
92 | ||
93 | //------------------------------------------------------------------------------------------------ | |
94 | // DEBUG only | |
95 | //------------------------------------------------------------------------------------------------ | |
96 | ||
97 | static void describeXPCObject(char *prefix, xpc_object_t object) | |
98 | { | |
99 | //#ifndef NDEBUG | |
100 | // This is useful for debugging. | |
101 | if (object) | |
102 | { | |
103 | char *desc = xpc_copy_description(object); | |
104 | secdebug(SOSCKCSCOPE, "%s%s\n", prefix, desc); | |
105 | free(desc); | |
106 | } | |
107 | else | |
108 | secdebug(SOSCKCSCOPE, "%s<NULL>\n", prefix); | |
109 | //#endif | |
110 | } | |
111 | ||
112 | static void describeXPCType(char *prefix, xpc_type_t xtype) | |
113 | { | |
114 | // Add others as necessary, e.g. XPC_TYPE_DOUBLE | |
115 | #ifndef NDEBUG | |
116 | // This is useful for debugging. | |
117 | char msg[256]={0,}; | |
118 | if (XPC_TYPE_CONNECTION == xtype) | |
119 | strcpy(msg, "XPC_TYPE_CONNECTION"); | |
120 | else if (XPC_TYPE_ERROR == xtype) | |
121 | strcpy(msg, "XPC_TYPE_ERROR"); | |
122 | else if (XPC_TYPE_DICTIONARY == xtype) | |
123 | strcpy(msg, "XPC_TYPE_DICTIONARY"); | |
124 | else | |
125 | strcpy(msg, "<unknown>"); | |
126 | ||
127 | secdebug(SOSCKCSCOPE, "%s type:%s\n", prefix, msg); | |
128 | #endif | |
129 | } | |
130 | ||
131 | // MARK: ---------- SOSXPCCloudTransport ---------- | |
132 | ||
133 | typedef struct SOSXPCCloudTransport *SOSXPCCloudTransportRef; | |
134 | struct SOSXPCCloudTransport | |
135 | { | |
136 | struct SOSCloudTransport transport; | |
137 | xpc_connection_t serviceConnection; | |
5c19dc3a | 138 | xpc_connection_t idsProxyServiceConnection; |
427c49bc A |
139 | dispatch_queue_t xpc_queue; |
140 | }; | |
141 | ||
142 | static bool xpc_event_filter(const xpc_connection_t peer, xpc_object_t event, CFErrorRef *error) | |
143 | { | |
144 | // return true if the type is XPC_TYPE_DICTIONARY (and therefore something more to process) | |
145 | secdebug(SOSCKCSCOPE, "handle_connection_event\n"); | |
146 | xpc_type_t xtype = xpc_get_type(event); | |
147 | describeXPCType("handle_xpc_event", xtype); | |
148 | if (XPC_TYPE_CONNECTION == xtype) | |
149 | { | |
150 | secdebug(SOSCKCSCOPE, "handle_xpc_event: XPC_TYPE_CONNECTION (unexpected)"); | |
151 | // The client of an XPC service does not get connection events | |
152 | // For now, we log this and keep going | |
153 | describeXPCObject("handle_xpc_event: XPC_TYPE_CONNECTION, obj : ", event); | |
154 | #if 0 | |
155 | if (error) | |
156 | *error = makeError(kSOSOUnexpectedConnectionEvent); // FIX | |
157 | assert(true); | |
158 | #endif | |
159 | } | |
160 | else | |
161 | if (XPC_TYPE_ERROR == xtype) | |
162 | { | |
163 | #ifndef NDEBUG | |
164 | const char *estr = xpc_dictionary_get_string(event, XPC_ERROR_KEY_DESCRIPTION); | |
165 | #endif | |
166 | secdebug(SOSCKCSCOPE, "default: xpc error: %s\n", estr); | |
167 | #if 0 // just log for now | |
168 | CFStringRef errStr = CFStringCreateWithCString(kCFAllocatorDefault, estr, kCFStringEncodingUTF8); | |
169 | CFMutableDictionaryRef userInfo = CFDictionaryCreateMutable(kCFAllocatorDefault, 0, &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks); | |
170 | if (errStr) | |
171 | CFDictionaryAddValue(userInfo, kCFErrorLocalizedDescriptionKey, errStr); | |
172 | if (error) | |
173 | *error = CFErrorCreate(kCFAllocatorDefault, sErrorDomain, kSOSOXPCErrorEvent, userInfo); | |
174 | CFReleaseSafe(errStr); | |
175 | CFReleaseSafe(userInfo); | |
176 | #endif | |
177 | } | |
178 | else | |
179 | if (XPC_TYPE_DICTIONARY == xtype) | |
180 | { | |
181 | secdebug(SOSCKCSCOPE, "received dictionary event %p\n", event); | |
182 | return true; | |
183 | } | |
184 | else | |
185 | { | |
186 | secdebug(SOSCKCSCOPE, "default: unexpected connection event %p\n", event); | |
187 | describeXPCObject("handle_xpc_event: obj : ", event); | |
188 | if (error) | |
189 | *error = makeError(kSOSOUnexpectedXPCEvent); | |
190 | } | |
191 | return false; | |
192 | } | |
193 | ||
427c49bc A |
194 | static void SOSXPCCloudTransportInit(SOSXPCCloudTransportRef transport) |
195 | { | |
196 | secdebug(SOSCKCSCOPE, "initXPCConnection\n"); | |
d8f41ccd | 197 | |
427c49bc | 198 | transport->xpc_queue = dispatch_queue_create(xpcServiceName, DISPATCH_QUEUE_SERIAL); |
d8f41ccd | 199 | |
427c49bc | 200 | transport->serviceConnection = xpc_connection_create_mach_service(xpcServiceName, transport->xpc_queue, 0); |
d8f41ccd | 201 | |
5c19dc3a | 202 | |
427c49bc | 203 | secdebug(SOSCKCSCOPE, "serviceConnection: %p\n", transport->serviceConnection); |
d8f41ccd | 204 | |
5c19dc3a | 205 | |
427c49bc | 206 | xpc_connection_set_event_handler(transport->serviceConnection, ^(xpc_object_t event) |
d8f41ccd A |
207 | { |
208 | secdebug(SOSCKCSCOPE, "xpc_connection_set_event_handler\n"); | |
209 | }); | |
210 | ||
427c49bc A |
211 | xpc_connection_resume(transport->serviceConnection); |
212 | xpc_retain(transport->serviceConnection); | |
5c19dc3a A |
213 | |
214 | transport->idsProxyServiceConnection = xpc_connection_create_mach_service(xpcIDSServiceName, transport->xpc_queue, 0); | |
215 | ||
216 | secdebug(SOSCKCSCOPE, "ids service connection: %p\n", transport->idsProxyServiceConnection); | |
217 | ||
218 | xpc_connection_set_event_handler(transport->idsProxyServiceConnection, ^(xpc_object_t object) { | |
219 | secdebug(SOSCKCSCOPE, "IDS Transport, xpc_connection_set_event_handler\n"); | |
220 | }); | |
221 | xpc_connection_resume(transport->idsProxyServiceConnection); | |
222 | xpc_retain(transport->idsProxyServiceConnection); | |
427c49bc A |
223 | } |
224 | ||
5c19dc3a A |
225 | static void talkWithIDS(SOSXPCCloudTransportRef transport, xpc_object_t message, dispatch_queue_t processQueue, CloudKeychainReplyBlock replyBlock) |
226 | { | |
227 | CFErrorRef connectionError = NULL; | |
228 | ||
229 | os_activity_t trace_activity = os_activity_start("talkWithIDS", OS_ACTIVITY_FLAG_DEFAULT); | |
230 | require_action(transport->idsProxyServiceConnection, xit, connectionError = makeError(kSOSConnectionNotOpen)); | |
231 | require_action(message, xit, connectionError = makeError(kSOSObjectNotFoundError)); | |
232 | dispatch_retain(processQueue); | |
233 | ||
234 | xpc_connection_send_message_with_reply(transport->idsProxyServiceConnection, message, transport->xpc_queue, ^(xpc_object_t reply) | |
235 | { | |
236 | CFErrorRef serverError = NULL; | |
237 | CFTypeRef object = NULL; | |
238 | if (xpc_event_filter(transport->idsProxyServiceConnection, reply, &serverError) && reply) | |
239 | { | |
240 | describeXPCObject("IDS Proxy: reply : ", reply); | |
241 | if (serverError) | |
242 | secerror("Error from xpc_event_filter: %@", serverError); | |
243 | xpc_object_t xrv = xpc_dictionary_get_value(reply, kMessageKeyValue); | |
244 | if (xrv) | |
245 | { | |
246 | describeXPCObject("talkwithIDS: xrv: ", xrv); | |
247 | /* | |
248 | * The given XPC object must be one that was previously returned by | |
249 | * _CFXPCCreateXPCMessageWithCFObject(). | |
250 | */ | |
251 | object = _CFXPCCreateCFObjectFromXPCObject(xrv); // CF object is retained; release in callback | |
252 | secnotice("talkwithIDS", "converted CF object: %@", object); | |
253 | } | |
254 | else | |
255 | secerror("missing value reply"); | |
256 | ||
257 | xpc_object_t xerror = xpc_dictionary_get_value(reply, kMessageKeyError); | |
258 | if (xerror) | |
259 | serverError = SecCreateCFErrorWithXPCObject(xerror); // use SecCFCreateErrorWithFormat? | |
260 | } | |
261 | dispatch_async(processQueue, ^{ | |
262 | if (replyBlock) | |
263 | replyBlock(object, serverError); | |
264 | CFReleaseSafe(object); | |
265 | if (serverError) | |
266 | { | |
267 | secerror("talkwithIDS callback error: %@", serverError); | |
268 | CFReleaseSafe(serverError); | |
269 | } | |
270 | dispatch_release(processQueue); | |
271 | }); | |
272 | }); | |
273 | return; | |
274 | ||
275 | xit: | |
276 | secerror("talkWithIDS error: %@", connectionError); | |
277 | dispatch_async(processQueue, ^{ | |
278 | if (replyBlock) | |
279 | replyBlock(NULL, connectionError); | |
280 | CFReleaseSafe(connectionError); | |
281 | dispatch_release(processQueue); | |
282 | }); | |
283 | ||
284 | os_activity_end(trace_activity); | |
285 | } | |
427c49bc A |
286 | static void talkWithKVS(SOSXPCCloudTransportRef transport, xpc_object_t message, dispatch_queue_t processQueue, CloudKeychainReplyBlock replyBlock) |
287 | { | |
d8f41ccd | 288 | CFErrorRef connectionError = NULL; |
5c19dc3a A |
289 | |
290 | os_activity_t trace_activity = os_activity_start("talkWithKVS", OS_ACTIVITY_FLAG_DEFAULT); | |
d8f41ccd A |
291 | require_action(transport->serviceConnection, xit, connectionError = makeError(kSOSConnectionNotOpen)); |
292 | require_action(message, xit, connectionError = makeError(kSOSObjectNotFoundError)); | |
427c49bc | 293 | dispatch_retain(processQueue); |
d8f41ccd | 294 | |
427c49bc A |
295 | xpc_connection_send_message_with_reply(transport->serviceConnection, message, transport->xpc_queue, ^(xpc_object_t reply) |
296 | { | |
d8f41ccd A |
297 | CFErrorRef serverError = NULL; |
298 | CFTypeRef object = NULL; | |
299 | if (xpc_event_filter(transport->serviceConnection, reply, &serverError) && reply) | |
427c49bc A |
300 | { |
301 | describeXPCObject("getValuesFromKVS: reply : ", reply); | |
d8f41ccd A |
302 | if (serverError) |
303 | secerror("Error from xpc_event_filter: %@", serverError); | |
427c49bc A |
304 | xpc_object_t xrv = xpc_dictionary_get_value(reply, kMessageKeyValue); |
305 | if (xrv) | |
306 | { | |
307 | describeXPCObject("talkWithKVS: xrv: ", xrv); | |
308 | /* | |
309 | * The given XPC object must be one that was previously returned by | |
310 | * _CFXPCCreateXPCMessageWithCFObject(). | |
311 | */ | |
312 | object = _CFXPCCreateCFObjectFromXPCObject(xrv); // CF object is retained; release in callback | |
313 | secnotice("talkwithkvs", "converted CF object: %@", object); | |
314 | } | |
315 | else | |
316 | secerror("missing value reply"); | |
317 | ||
318 | xpc_object_t xerror = xpc_dictionary_get_value(reply, kMessageKeyError); | |
319 | if (xerror) | |
d8f41ccd | 320 | serverError = SecCreateCFErrorWithXPCObject(xerror); // use SecCFCreateErrorWithFormat? |
427c49bc | 321 | } |
d8f41ccd A |
322 | dispatch_async(processQueue, ^{ |
323 | if (replyBlock) | |
324 | replyBlock(object, serverError); | |
325 | CFReleaseSafe(object); | |
326 | if (serverError) | |
327 | { | |
328 | secerror("callback error: %@", serverError); | |
329 | CFReleaseSafe(serverError); | |
330 | } | |
331 | dispatch_release(processQueue); | |
332 | }); | |
427c49bc A |
333 | }); |
334 | return; | |
d8f41ccd | 335 | |
427c49bc | 336 | xit: |
d8f41ccd A |
337 | secerror("talkWithKVS error: %@", connectionError); |
338 | dispatch_async(processQueue, ^{ | |
339 | if (replyBlock) | |
340 | replyBlock(NULL, connectionError); | |
341 | CFReleaseSafe(connectionError); | |
342 | dispatch_release(processQueue); | |
343 | }); | |
5c19dc3a A |
344 | |
345 | os_activity_end(trace_activity); | |
427c49bc A |
346 | } |
347 | ||
348 | // MARK: ---------- SOSXPCCloudTransport Client Calls ---------- | |
349 | ||
350 | /* Concrete function backend implementations. */ | |
351 | static void SOSCloudTransportSetItemsChangedBlock(SOSCloudTransportRef transport, | |
352 | CloudItemsChangedBlock itemsChangedBlock) { | |
353 | if (transport->itemsChangedBlock != itemsChangedBlock) | |
354 | { | |
d8f41ccd | 355 | secnotice(SOSCKCSCOPE, "Changing itemsChangedBlock"); |
427c49bc A |
356 | if (transport->itemsChangedBlock) |
357 | Block_release(transport->itemsChangedBlock); | |
358 | transport->itemsChangedBlock = Block_copy(itemsChangedBlock); | |
359 | } | |
360 | } | |
361 | ||
362 | /* Virtual function backend implementations. */ | |
363 | static void SOSCloudTransportPut(SOSCloudTransportRef transport, CFDictionaryRef values, dispatch_queue_t processQueue, CloudKeychainReplyBlock replyBlock) | |
364 | { | |
365 | SOSXPCCloudTransportRef xpcTransport = (SOSXPCCloudTransportRef)transport; | |
366 | secdebug(SOSCKCSCOPE, "%@", values); | |
367 | CFErrorRef error = NULL; | |
368 | xpc_object_t message = NULL; | |
369 | xpc_object_t xobject = NULL; | |
370 | require_action(values, xit, error = makeError(kSOSObjectNotFoundError)); | |
371 | ||
372 | message = xpc_dictionary_create(NULL, NULL, 0); | |
373 | xpc_dictionary_set_uint64(message, kMessageKeyVersion, kCKDXPCVersion); | |
374 | xpc_dictionary_set_string(message, kMessageKeyOperation, kOperationPUTDictionary); | |
375 | ||
376 | xobject = _CFXPCCreateXPCObjectFromCFObject(values); | |
377 | require_action(xobject, xit, error = makeError(kSOSObjectCantBeConvertedToXPCObject)); | |
378 | xpc_dictionary_set_value(message, kMessageKeyValue, xobject); | |
379 | xpc_release(xobject); | |
380 | ||
381 | talkWithKVS(xpcTransport, message, processQueue, replyBlock); | |
382 | xpc_release(message); | |
383 | return; | |
384 | ||
385 | xit: | |
386 | if (replyBlock) | |
387 | replyBlock(NULL, error); | |
388 | CFReleaseSafe(error); | |
389 | } | |
390 | ||
391 | /* Get from KVS */ | |
392 | static void SOSCloudTransportGet(SOSCloudTransportRef transport, CFArrayRef keysToGet, dispatch_queue_t processQueue, CloudKeychainReplyBlock replyBlock) | |
393 | { | |
394 | SOSXPCCloudTransportRef xpcTransport = (SOSXPCCloudTransportRef)transport; | |
395 | secdebug(SOSCKCSCOPE, "%@", keysToGet); | |
396 | CFErrorRef error = NULL; | |
397 | xpc_object_t xkeysOfInterest = xpc_dictionary_create(NULL, NULL, 0); | |
398 | xpc_object_t xkeysToGet = keysToGet ? _CFXPCCreateXPCObjectFromCFObject(keysToGet) : xpc_null_create(); | |
399 | ||
400 | require_action(xkeysToGet, xit, error = makeError(kSOSObjectNotFoundError)); | |
401 | ||
402 | if (keysToGet) // don't add if nulll; will call getall | |
403 | xpc_dictionary_set_value(xkeysOfInterest, kMessageKeyKeysToGet, xkeysToGet); | |
404 | ||
405 | xpc_object_t message = xpc_dictionary_create(NULL, NULL, 0); | |
406 | xpc_dictionary_set_uint64(message, kMessageKeyVersion, kCKDXPCVersion); | |
407 | xpc_dictionary_set_string(message, kMessageKeyOperation, kOperationGETv2); | |
408 | xpc_dictionary_set_value(message, kMessageKeyValue, xkeysOfInterest); | |
409 | ||
410 | talkWithKVS(xpcTransport, message, processQueue, replyBlock); | |
411 | xpc_release(xkeysToGet); | |
412 | xpc_release(xkeysOfInterest); | |
413 | xpc_release(message); | |
414 | return; | |
415 | ||
416 | xit: | |
417 | if(xkeysOfInterest) | |
418 | xpc_release(xkeysOfInterest); | |
419 | if(xkeysToGet) | |
420 | xpc_release(xkeysToGet); | |
421 | if (replyBlock) | |
422 | replyBlock(NULL, error); | |
423 | CFReleaseSafe(error); | |
424 | } | |
425 | ||
427c49bc A |
426 | // |
427 | // Handles NULL by seting xpc_null. | |
428 | static void SecXPCDictionarySetCFObject(xpc_object_t xdict, const char *key, CFTypeRef object) | |
429 | { | |
430 | xpc_object_t xpc_obj = object ? _CFXPCCreateXPCObjectFromCFObject(object) : xpc_null_create(); | |
431 | xpc_dictionary_set_value(xdict, key, xpc_obj); | |
432 | xpc_release(xpc_obj); | |
433 | } | |
434 | ||
5c19dc3a | 435 | static void SOSCloudTransportGetIDSDeviceID(SOSCloudTransportRef transport, CloudKeychainReplyBlock replyBlock) |
427c49bc | 436 | { |
427c49bc | 437 | dispatch_queue_t processQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0); |
5c19dc3a A |
438 | |
439 | SOSXPCCloudTransportRef xpcTransport = (SOSXPCCloudTransportRef)transport; | |
440 | ||
441 | xpc_object_t message = xpc_dictionary_create(NULL, NULL, 0); | |
442 | xpc_dictionary_set_uint64(message, kMessageKeyVersion, kCKDXPCVersion); | |
443 | xpc_dictionary_set_string(message, kMessageKeyOperation, kOperationGetDeviceID); | |
444 | ||
445 | talkWithIDS(xpcTransport, message, processQueue, replyBlock); | |
446 | xpc_release(message); | |
447 | } | |
427c49bc | 448 | |
e0e0d90e | 449 | static void SOSCloudTransportSendIDSMessage(SOSCloudTransportRef transport, CFDictionaryRef messageData, CFStringRef deviceName, CFStringRef peerID, dispatch_queue_t processQueue, CloudKeychainReplyBlock replyBlock){ |
5c19dc3a A |
450 | |
451 | SOSXPCCloudTransportRef xpcTransport = (SOSXPCCloudTransportRef)transport; | |
452 | xpc_object_t xmessageData = _CFXPCCreateXPCObjectFromCFObject(messageData); | |
453 | ||
454 | xpc_object_t message = xpc_dictionary_create(NULL, NULL, 0); | |
455 | xpc_dictionary_set_uint64(message, kMessageKeyVersion, kCKDXPCVersion); | |
456 | xpc_dictionary_set_string(message, kMessageKeyOperation, kOperationSendIDSMessage); | |
457 | xpc_dictionary_set_value(message, kMessageKeyValue, xmessageData); | |
458 | SecXPCDictionarySetCFObject(message, kMessageKeyDeviceName, deviceName); | |
459 | SecXPCDictionarySetCFObject(message, kMessageKeyPeerID, peerID); | |
460 | talkWithIDS(xpcTransport, message, processQueue, replyBlock); | |
461 | ||
462 | xpc_release(xmessageData); | |
463 | xpc_release(message); | |
464 | } | |
427c49bc | 465 | |
5c19dc3a A |
466 | static void SOSCloudTransportUpdateKeys(SOSCloudTransportRef transport, |
467 | CFDictionaryRef keys, | |
468 | dispatch_queue_t processQueue, | |
469 | CloudKeychainReplyBlock replyBlock) | |
470 | { | |
427c49bc A |
471 | SOSXPCCloudTransportRef xpcTransport = (SOSXPCCloudTransportRef)transport; |
472 | ||
473 | xpc_object_t xkeysOfInterest = xpc_dictionary_create(NULL, NULL, 0); | |
d8f41ccd | 474 | SecXPCDictionarySetCFObject(xkeysOfInterest, kMessageAllKeys, keys); |
427c49bc A |
475 | |
476 | xpc_object_t message = xpc_dictionary_create(NULL, NULL, 0); | |
477 | xpc_dictionary_set_uint64(message, kMessageKeyVersion, kCKDXPCVersion); | |
d8f41ccd | 478 | xpc_dictionary_set_string(message, kMessageKeyOperation, kOperationRegisterKeys); |
427c49bc A |
479 | xpc_dictionary_set_value(message, kMessageKeyValue, xkeysOfInterest); |
480 | ||
481 | talkWithKVS(xpcTransport, message, processQueue, replyBlock); | |
482 | xpc_release(message); | |
483 | xpc_release(xkeysOfInterest); | |
427c49bc A |
484 | } |
485 | ||
427c49bc A |
486 | static void SOSCloudTransportGetAll(SOSCloudTransportRef transport, dispatch_queue_t processQueue, CloudKeychainReplyBlock replyBlock) |
487 | { | |
488 | secdebug(SOSCKCSCOPE, "start"); | |
489 | SOSCloudTransportGet(transport, NULL, processQueue, replyBlock); | |
490 | } | |
491 | ||
492 | static void SOSCloudTransportSync(SOSCloudTransportRef transport, dispatch_queue_t processQueue, CloudKeychainReplyBlock replyBlock) | |
493 | { | |
494 | SOSXPCCloudTransportRef xpcTransport = (SOSXPCCloudTransportRef)transport; | |
495 | secdebug(SOSCKCSCOPE, "start"); | |
496 | xpc_object_t message = xpc_dictionary_create(NULL, NULL, 0); | |
497 | xpc_dictionary_set_uint64(message, kMessageKeyVersion, kCKDXPCVersion); | |
498 | xpc_dictionary_set_string(message, kMessageKeyOperation, kOperationSynchronize); | |
499 | talkWithKVS(xpcTransport, message, processQueue, replyBlock); | |
500 | xpc_release(message); | |
501 | } | |
502 | ||
503 | static void SOSCloudTransportSyncAndWait(SOSCloudTransportRef transport, CFArrayRef keysToGet, dispatch_queue_t processQueue, CloudKeychainReplyBlock replyBlock) | |
504 | { | |
505 | SOSXPCCloudTransportRef xpcTransport = (SOSXPCCloudTransportRef)transport; | |
506 | secdebug(SOSCKCSCOPE, "%@", keysToGet); | |
d8f41ccd | 507 | secnotice(SOSCKCSCOPE, "%s XPC request to CKD: %s", kWAIT2MINID, kOperationSynchronizeAndWait); |
427c49bc A |
508 | xpc_object_t xkeysOfInterest = xpc_dictionary_create(NULL, NULL, 0); |
509 | ||
510 | xpc_object_t xkeysToRegister = keysToGet ? _CFXPCCreateXPCObjectFromCFObject(keysToGet) : xpc_null_create(); | |
511 | xpc_dictionary_set_value(xkeysOfInterest, kMessageKeyKeysToGet, xkeysToRegister); | |
512 | xpc_release(xkeysToRegister); | |
513 | xkeysToRegister = NULL; | |
514 | ||
515 | xpc_object_t message = xpc_dictionary_create(NULL, NULL, 0); | |
516 | xpc_dictionary_set_uint64(message, kMessageKeyVersion, kCKDXPCVersion); | |
517 | xpc_dictionary_set_string(message, kMessageKeyOperation, kOperationSynchronizeAndWait); | |
518 | xpc_dictionary_set_value(message, kMessageKeyValue, xkeysOfInterest); | |
519 | xpc_release(xkeysOfInterest); | |
520 | xkeysOfInterest = NULL; | |
521 | ||
522 | talkWithKVS(xpcTransport, message, processQueue, replyBlock); | |
523 | xpc_release(message); | |
524 | } | |
525 | ||
526 | static void SOSCloudTransportClearAll(SOSCloudTransportRef transport, dispatch_queue_t processQueue, CloudKeychainReplyBlock replyBlock) | |
527 | { | |
528 | SOSXPCCloudTransportRef xpcTransport = (SOSXPCCloudTransportRef)transport; | |
529 | secdebug(SOSCKCSCOPE, "start"); | |
530 | xpc_object_t message = xpc_dictionary_create(NULL, NULL, 0); | |
531 | xpc_dictionary_set_uint64(message, kMessageKeyVersion, kCKDXPCVersion); | |
532 | xpc_dictionary_set_string(message, kMessageKeyOperation, kOperationClearStore); | |
533 | talkWithKVS(xpcTransport, message, processQueue, replyBlock); | |
534 | xpc_release(message); | |
535 | } | |
536 | ||
537 | static void SOSCloudTransportRemoveObjectForKey(SOSCloudTransportRef transport, CFStringRef keyToRemove, dispatch_queue_t processQueue, CloudKeychainReplyBlock replyBlock) | |
538 | { | |
539 | SOSXPCCloudTransportRef xpcTransport = (SOSXPCCloudTransportRef)transport; | |
540 | secdebug(SOSCKCSCOPE, "start"); | |
541 | CFErrorRef error = NULL; | |
542 | xpc_object_t message = NULL; | |
543 | xpc_object_t xkeytoremove = NULL; | |
544 | ||
545 | require_action(keyToRemove, xit, error = makeError(kSOSObjectNotFoundError)); | |
546 | ||
547 | message = xpc_dictionary_create(NULL, NULL, 0); | |
548 | xpc_dictionary_set_uint64(message, kMessageKeyVersion, kCKDXPCVersion); | |
549 | xpc_dictionary_set_string(message, kMessageKeyOperation, kOperationRemoveObjectForKey); | |
550 | ||
551 | xkeytoremove = _CFXPCCreateXPCObjectFromCFObject(keyToRemove); | |
552 | require_action(xkeytoremove, xit, error = makeError(kSOSObjectCantBeConvertedToXPCObject)); | |
553 | xpc_dictionary_set_value(message, kMessageKeyKey, xkeytoremove); | |
554 | xpc_release(xkeytoremove); | |
555 | ||
556 | talkWithKVS(xpcTransport, message, processQueue, replyBlock); | |
557 | xpc_release(message); | |
558 | return; | |
559 | ||
560 | xit: | |
561 | if(xkeytoremove) | |
562 | xpc_release(xkeytoremove); | |
563 | if(message) | |
564 | xpc_release(message); | |
565 | if (replyBlock) | |
566 | replyBlock(NULL, error); | |
567 | CFReleaseSafe(error); | |
568 | } | |
427c49bc A |
569 | static void SOSCloudTransportLocalNotification(SOSCloudTransportRef transport, CFStringRef messageToUser, dispatch_queue_t processQueue, CloudKeychainReplyBlock replyBlock) |
570 | { | |
571 | SOSXPCCloudTransportRef xpcTransport = (SOSXPCCloudTransportRef)transport; | |
572 | secdebug(SOSCKCSCOPE, "start"); | |
573 | xpc_object_t xLocalNotificationDict = xpc_dictionary_create(NULL, NULL, 0); | |
574 | char *headerKey = CFStringToCString(kCFUserNotificationAlertHeaderKey); | |
575 | char *message = CFStringToCString(messageToUser); | |
576 | xpc_dictionary_set_string(xLocalNotificationDict, headerKey, message); | |
d8f41ccd | 577 | |
427c49bc A |
578 | xpc_object_t xpcmessage = xpc_dictionary_create(NULL, NULL, 0); |
579 | xpc_dictionary_set_uint64(xpcmessage, kMessageKeyVersion, kCKDXPCVersion); | |
580 | xpc_dictionary_set_string(xpcmessage, kMessageKeyOperation, kOperationUILocalNotification); | |
581 | xpc_dictionary_set_value (xpcmessage, kMessageKeyValue, xLocalNotificationDict); | |
582 | xpc_release(xLocalNotificationDict); | |
d8f41ccd | 583 | |
427c49bc | 584 | talkWithKVS(xpcTransport, xpcmessage, processQueue, replyBlock); |
d8f41ccd | 585 | |
427c49bc A |
586 | free(headerKey); |
587 | free(message); | |
588 | xpc_release(xpcmessage); | |
589 | } | |
590 | ||
d8f41ccd | 591 | static void SOSCloudTransportRequestSyncWithAllPeers(SOSCloudTransportRef transport, dispatch_queue_t processQueue, CloudKeychainReplyBlock replyBlock) |
427c49bc A |
592 | { |
593 | secdebug(SOSCKCSCOPE, "start"); | |
594 | SOSXPCCloudTransportRef xpcTransport = (SOSXPCCloudTransportRef)transport; | |
595 | ||
427c49bc A |
596 | xpc_object_t xpcmessage = xpc_dictionary_create(NULL, NULL, 0); |
597 | xpc_dictionary_set_uint64(xpcmessage, kMessageKeyVersion, kCKDXPCVersion); | |
d8f41ccd | 598 | xpc_dictionary_set_string(xpcmessage, kMessageKeyOperation, kOperationRequestSyncWithAllPeers); |
427c49bc A |
599 | |
600 | talkWithKVS(xpcTransport, xpcmessage, processQueue, replyBlock); | |
601 | ||
602 | xpc_release(xpcmessage); | |
603 | } | |
604 | ||
d8f41ccd | 605 | static void SOSCloudTransportRequestEnsurePeerRegistration(SOSCloudTransportRef transport, dispatch_queue_t processQueue, CloudKeychainReplyBlock replyBlock) |
427c49bc A |
606 | { |
607 | secdebug(SOSCKCSCOPE, "start"); | |
608 | SOSXPCCloudTransportRef xpcTransport = (SOSXPCCloudTransportRef)transport; | |
d8f41ccd | 609 | |
427c49bc A |
610 | xpc_object_t xpcmessage = xpc_dictionary_create(NULL, NULL, 0); |
611 | xpc_dictionary_set_uint64(xpcmessage, kMessageKeyVersion, kCKDXPCVersion); | |
d8f41ccd A |
612 | xpc_dictionary_set_string(xpcmessage, kMessageKeyOperation, kOperationRequestEnsurePeerRegistration); |
613 | ||
427c49bc | 614 | talkWithKVS(xpcTransport, xpcmessage, processQueue, replyBlock); |
d8f41ccd A |
615 | |
616 | xpc_release(xpcmessage); | |
617 | } | |
618 | ||
619 | static void SOSCloudTransportFlush(SOSCloudTransportRef transport, dispatch_queue_t processQueue, CloudKeychainReplyBlock replyBlock) | |
620 | { | |
621 | secdebug(SOSCKCSCOPE, "start"); | |
622 | SOSXPCCloudTransportRef xpcTransport = (SOSXPCCloudTransportRef)transport; | |
623 | ||
624 | xpc_object_t xpcmessage = xpc_dictionary_create(NULL, NULL, 0); | |
625 | xpc_dictionary_set_uint64(xpcmessage, kMessageKeyVersion, kCKDXPCVersion); | |
626 | xpc_dictionary_set_string(xpcmessage, kMessageKeyOperation, kOperationFlush); | |
627 | ||
628 | talkWithKVS(xpcTransport, xpcmessage, processQueue, replyBlock); | |
629 | ||
427c49bc A |
630 | xpc_release(xpcmessage); |
631 | } | |
632 | ||
633 | static SOSCloudTransportRef SOSCloudTransportCreateXPCTransport(void) | |
634 | { | |
635 | SOSXPCCloudTransportRef st; | |
636 | st = calloc(1, sizeof(*st)); | |
637 | st->transport.put = SOSCloudTransportPut; | |
427c49bc | 638 | st->transport.updateKeys = SOSCloudTransportUpdateKeys; |
5c19dc3a A |
639 | st->transport.sendIDSMessage = SOSCloudTransportSendIDSMessage; |
640 | st->transport.getDeviceID = SOSCloudTransportGetIDSDeviceID; | |
427c49bc A |
641 | st->transport.get = SOSCloudTransportGet; |
642 | st->transport.getAll = SOSCloudTransportGetAll; | |
643 | st->transport.synchronize = SOSCloudTransportSync; | |
644 | st->transport.synchronizeAndWait = SOSCloudTransportSyncAndWait; | |
645 | st->transport.clearAll = SOSCloudTransportClearAll; | |
646 | st->transport.removeObjectForKey = SOSCloudTransportRemoveObjectForKey; | |
d8f41ccd | 647 | st->transport.localNotification = SOSCloudTransportLocalNotification; |
427c49bc | 648 | st->transport.requestSyncWithAllPeers = SOSCloudTransportRequestSyncWithAllPeers; |
d8f41ccd A |
649 | st->transport.requestEnsurePeerRegistration = SOSCloudTransportRequestEnsurePeerRegistration; |
650 | st->transport.flush = SOSCloudTransportFlush; | |
651 | st->transport.itemsChangedBlock = Block_copy(^CFArrayRef(CFDictionaryRef changes) { | |
652 | secerror("Calling default itemsChangedBlock - fatal: %@", changes); | |
653 | assert(false); | |
654 | return NULL; | |
655 | }); | |
427c49bc A |
656 | SOSXPCCloudTransportInit(st); |
657 | return &st->transport; | |
658 | } | |
659 | ||
660 | // MARK: ---------- SOSCloudKeychain concrete client APIs ---------- | |
661 | void SOSCloudKeychainSetItemsChangedBlock(CloudItemsChangedBlock itemsChangedBlock) | |
662 | { | |
663 | secdebug(SOSCKCSCOPE, "start"); | |
664 | SOSCloudTransportSetItemsChangedBlock(SOSCloudTransportDefaultTransport(), | |
665 | itemsChangedBlock); | |
666 | } | |
667 | ||
668 | // MARK: ---------- SOSCloudKeychain virtual client APIs ---------- | |
669 | ||
670 | void SOSCloudKeychainPutObjectsInCloud(CFDictionaryRef objects, dispatch_queue_t processQueue, CloudKeychainReplyBlock replyBlock) | |
671 | { | |
672 | SOSCloudTransportRef cTransportRef = SOSCloudTransportDefaultTransport(); | |
673 | if (cTransportRef) | |
674 | cTransportRef->put(cTransportRef, objects, processQueue, replyBlock); | |
675 | } | |
676 | ||
5c19dc3a | 677 | void SOSCloudKeychainUpdateKeys(CFDictionaryRef keys, dispatch_queue_t processQueue, CloudKeychainReplyBlock replyBlock) |
427c49bc A |
678 | { |
679 | SOSCloudTransportRef cTransportRef = SOSCloudTransportDefaultTransport(); | |
680 | if (cTransportRef) | |
5c19dc3a | 681 | cTransportRef->updateKeys(cTransportRef, keys, processQueue, replyBlock); |
427c49bc A |
682 | } |
683 | ||
e0e0d90e | 684 | void SOSCloudKeychainSendIDSMessage(CFDictionaryRef message, CFStringRef deviceName, CFStringRef peerID, dispatch_queue_t processQueue, CloudKeychainReplyBlock replyBlock) |
427c49bc A |
685 | { |
686 | SOSCloudTransportRef cTransportRef = SOSCloudTransportDefaultTransport(); | |
5c19dc3a A |
687 | if(cTransportRef) |
688 | cTransportRef->sendIDSMessage(cTransportRef, message, deviceName, peerID, processQueue, replyBlock); | |
427c49bc | 689 | |
427c49bc | 690 | } |
5c19dc3a | 691 | void SOSCloudKeychainGetIDSDeviceID(CloudKeychainReplyBlock replyBlock) |
427c49bc A |
692 | { |
693 | SOSCloudTransportRef cTransportRef = SOSCloudTransportDefaultTransport(); | |
5c19dc3a A |
694 | if (cTransportRef) |
695 | cTransportRef->getDeviceID(cTransportRef, replyBlock); | |
696 | ||
427c49bc A |
697 | } |
698 | ||
d8f41ccd | 699 | CF_RETURNS_RETAINED CFArrayRef SOSCloudKeychainHandleUpdateMessage(CFDictionaryRef updates) |
427c49bc | 700 | { |
d8f41ccd | 701 | CFArrayRef result = NULL; |
427c49bc A |
702 | SOSCloudTransportRef cTransportRef = SOSCloudTransportDefaultTransport(); |
703 | if (cTransportRef->itemsChangedBlock) | |
d8f41ccd A |
704 | result = ((CloudItemsChangedBlock)cTransportRef->itemsChangedBlock)(updates); |
705 | return result; | |
427c49bc A |
706 | } |
707 | ||
708 | void SOSCloudKeychainGetObjectsFromCloud(CFArrayRef keysToGet, dispatch_queue_t processQueue, CloudKeychainReplyBlock replyBlock) | |
709 | { | |
710 | SOSCloudTransportRef cTransportRef = SOSCloudTransportDefaultTransport(); | |
711 | if (cTransportRef) | |
712 | cTransportRef->get(cTransportRef, keysToGet, processQueue, replyBlock); | |
713 | } | |
714 | ||
715 | void SOSCloudKeychainGetAllObjectsFromCloud(dispatch_queue_t processQueue, CloudKeychainReplyBlock replyBlock) | |
716 | { | |
717 | SOSCloudTransportRef cTransportRef = SOSCloudTransportDefaultTransport(); | |
718 | if (cTransportRef) | |
719 | cTransportRef->getAll(cTransportRef, processQueue, replyBlock); | |
720 | } | |
721 | ||
722 | void SOSCloudKeychainSynchronizeAndWait(CFArrayRef keysToGet, dispatch_queue_t processQueue, CloudKeychainReplyBlock replyBlock) | |
723 | { | |
724 | SOSCloudTransportRef cTransportRef = SOSCloudTransportDefaultTransport(); | |
725 | if (cTransportRef) | |
726 | cTransportRef->synchronizeAndWait(cTransportRef, keysToGet, processQueue, replyBlock); | |
727 | } | |
728 | ||
729 | //DEBUG ONLY | |
730 | void SOSCloudKeychainSynchronize(dispatch_queue_t processQueue, CloudKeychainReplyBlock replyBlock) | |
731 | { | |
732 | SOSCloudTransportRef cTransportRef = SOSCloudTransportDefaultTransport(); | |
733 | if (cTransportRef) | |
734 | cTransportRef->synchronize(cTransportRef, processQueue, replyBlock); | |
735 | } | |
736 | ||
737 | //DEBUG ONLY | |
738 | void SOSCloudKeychainClearAll(dispatch_queue_t processQueue, CloudKeychainReplyBlock replyBlock) | |
739 | { | |
740 | SOSCloudTransportRef cTransportRef = SOSCloudTransportDefaultTransport(); | |
741 | if (cTransportRef) | |
742 | cTransportRef->clearAll(cTransportRef, processQueue, replyBlock); | |
743 | } | |
744 | ||
745 | void SOSCloudKeychainRemoveObjectForKey(CFStringRef keyToRemove, dispatch_queue_t processQueue, CloudKeychainReplyBlock replyBlock) | |
746 | { | |
747 | SOSCloudTransportRef cTransportRef = SOSCloudTransportDefaultTransport(); | |
748 | if (cTransportRef) | |
749 | cTransportRef->removeObjectForKey(cTransportRef, keyToRemove, processQueue, replyBlock); | |
750 | } | |
751 | ||
d8f41ccd | 752 | void SOSCloudKeychainRequestSyncWithAllPeers(dispatch_queue_t processQueue, CloudKeychainReplyBlock replyBlock) |
427c49bc A |
753 | { |
754 | SOSCloudTransportRef cTransportRef = SOSCloudTransportDefaultTransport(); | |
755 | if (cTransportRef) | |
d8f41ccd | 756 | cTransportRef->requestSyncWithAllPeers(cTransportRef, processQueue, replyBlock); |
427c49bc A |
757 | } |
758 | ||
d8f41ccd | 759 | void SOSCloudKeychainRequestEnsurePeerRegistration(dispatch_queue_t processQueue, CloudKeychainReplyBlock replyBlock) |
427c49bc A |
760 | { |
761 | SOSCloudTransportRef cTransportRef = SOSCloudTransportDefaultTransport(); | |
762 | if (cTransportRef) | |
d8f41ccd | 763 | cTransportRef->requestEnsurePeerRegistration(cTransportRef, processQueue, replyBlock); |
427c49bc A |
764 | } |
765 | ||
d8f41ccd | 766 | void SOSCloudKeychainFlush(dispatch_queue_t processQueue, CloudKeychainReplyBlock replyBlock) |
427c49bc A |
767 | { |
768 | SOSCloudTransportRef cTransportRef = SOSCloudTransportDefaultTransport(); | |
769 | if (cTransportRef) | |
d8f41ccd | 770 | cTransportRef->flush(cTransportRef, processQueue, replyBlock); |
427c49bc | 771 | } |
e0e0d90e | 772 |