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@
28 #import <Foundation/Foundation.h>
29 #import <Foundation/NSXPCConnection_Private.h>
32 #include <AssertMacros.h>
33 #include "keychain/SecureObjectSync/SOSCloudCircle.h"
34 #include "keychain/SecureObjectSync/SOSCloudCircleInternal.h"
35 #include "keychain/SecureObjectSync/SOSCircle.h"
36 #include "keychain/SecureObjectSync/SOSAccount.h"
37 #include "keychain/SecureObjectSync/SOSFullPeerInfo.h"
38 #include "keychain/SecureObjectSync/SOSPeerInfoCollections.h"
39 #include "keychain/SecureObjectSync/SOSInternal.h"
40 #include "keychain/SecureObjectSync/SOSRing.h"
41 #include <Security/SecureObjectSync/SOSBackupSliceKeyBag.h>
42 #include <Security/SecureObjectSync/SOSViews.h>
43 #include "keychain/SecureObjectSync/SOSControlHelper.h"
44 #include "keychain/SecureObjectSync/SOSPeerInfoDER.h"
46 #include <Security/SecKeyPriv.h>
47 #include <Security/SecFramework.h>
48 #include <CoreFoundation/CFXPCBridge.h>
50 #include "keychain/securityd/SecItemServer.h"
52 #include <utilities/SecDispatchRelease.h>
53 #include <utilities/SecCFRelease.h>
54 #include <utilities/SecCFWrappers.h>
55 #include <utilities/SecXPCError.h>
57 #include <corecrypto/ccsha2.h>
59 #include <utilities/debugging.h>
61 #include <CoreFoundation/CoreFoundation.h>
64 #define MINIMIZE_INCLUDES MINIMIZE_INCLUDES
65 #include <ipc/securityd_client.h>
66 #include "keychain/securityd/spi.h"
68 #include <Security/SecuritydXPC.h>
70 const char * kSOSCCCircleChangedNotification = "com.apple.security.secureobjectsync.circlechanged";
71 const char * kSOSCCViewMembershipChangedNotification = "com.apple.security.secureobjectsync.viewschanged";
72 const char * kSOSCCInitialSyncChangedNotification = "com.apple.security.secureobjectsync.initialsyncchanged";
73 const char * kSOSCCHoldLockForInitialSync = "com.apple.security.secureobjectsync.holdlock";
74 const char * kSOSCCPeerAvailable = "com.apple.security.secureobjectsync.peeravailable";
75 const char * kSOSCCRecoveryKeyChanged = "com.apple.security.secureobjectsync.recoverykeychanged";
76 const char * kSOSCCCircleOctagonKeysChangedNotification = "com.apple.security.sosoctagonbitschanged";
78 #define do_if_registered(sdp, ...) if (gSecurityd && gSecurityd->sdp) { return gSecurityd->sdp(__VA_ARGS__); }
80 static bool xpc_dictionary_entry_is_type(xpc_object_t dictionary, const char *key, xpc_type_t type)
82 xpc_object_t value = xpc_dictionary_get_value(dictionary, key);
84 return value && (xpc_get_type(value) == type);
87 static void setSOSDisabledError(CFErrorRef *error) {
88 SecCFCreateErrorWithFormat(0, kSOSErrorDomain, NULL, error, NULL, CFSTR("SOS Disabled for this platform"));
91 SOSCCStatus SOSCCThisDeviceIsInCircle(CFErrorRef *error)
94 secdebug("circleOps", "SOS disabled for this platform");
95 setSOSDisabledError(error);
99 SOSCCStatus retval = SOSGetCachedCircleStatus(error);
100 if(retval != kSOSNoCachedValue) {
101 secdebug("circleOps", "Retrieved cached circle value %d", retval);
104 return SOSCCThisDeviceIsInCircleNonCached(error);
108 SOSCCStatus SOSCCThisDeviceIsInCircleNonCached(CFErrorRef *error)
111 secdebug("circleOps", "SOS disabled for this platform");
112 setSOSDisabledError(error);
116 sec_trace_enter_api(NULL);
117 sec_trace_return_api(SOSCCStatus, ^{
118 SOSCCStatus result = kSOSCCError;
120 do_if_registered(soscc_ThisDeviceIsInCircle, error);
122 xpc_object_t message = securityd_create_message(kSecXPCOpDeviceInCircle, error);
124 xpc_object_t response = securityd_message_with_reply_sync(message, error);
126 if (response && xpc_dictionary_entry_is_type(response, kSecXPCKeyResult, XPC_TYPE_INT64)) {
127 result = (SOSCCStatus) xpc_dictionary_get_int64(response, kSecXPCKeyResult);
129 result = kSOSCCError;
133 if (response && securityd_message_no_error(response, error))
135 char *desc = xpc_copy_description(response);
136 SecCFCreateErrorWithFormat(0, sSecXPCErrorDomain, NULL, error, NULL, CFSTR("Remote error occurred/no info: %s"), desc);
141 secnotice("circleOps", "Retrieved non-cached circle value %d", result);
144 }, CFSTR("SOSCCStatus=%d"))
147 static bool simple_bool_error_request(enum SecXPCOperation op, CFErrorRef* error)
149 __block bool result = false;
151 secdebug("sosops","enter - operation: %d", op);
152 securityd_send_sync_and_do(op, error, NULL, ^bool(xpc_object_t response, __unused CFErrorRef *error) {
153 result = xpc_dictionary_get_bool(response, kSecXPCKeyResult);
159 static bool SecXPCDictionarySetPeerInfoData(xpc_object_t message, const char *key, SOSPeerInfoRef peerInfo, CFErrorRef *error) {
160 bool success = false;
161 CFDataRef peerData = SOSPeerInfoCopyEncodedData(peerInfo, kCFAllocatorDefault, error);
163 success = SecXPCDictionarySetData(message, key, peerData, error);
165 CFReleaseNull(peerData);
169 static bool peer_info_to_bool_error_request(enum SecXPCOperation op, SOSPeerInfoRef peerInfo, CFErrorRef* error)
171 __block bool result = false;
173 secdebug("sosops","enter - operation: %d", op);
174 securityd_send_sync_and_do(op, error, ^bool(xpc_object_t message, CFErrorRef *error) {
175 return SecXPCDictionarySetPeerInfoData(message, kSecXPCKeyPeerInfo, peerInfo, error);
176 }, ^bool(xpc_object_t response, __unused CFErrorRef *error) {
177 result = xpc_dictionary_get_bool(response, kSecXPCKeyResult);
183 static CFBooleanRef cfarray_to_cfboolean_error_request(enum SecXPCOperation op, CFArrayRef views, CFErrorRef* error)
185 __block bool result = false;
187 secdebug("sosops","enter - operation: %d", op);
188 bool noError = securityd_send_sync_and_do(op, error, ^bool(xpc_object_t message, CFErrorRef *error) {
189 return SecXPCDictionarySetPList(message, kSecXPCKeyArray, views, error);
190 }, ^bool(xpc_object_t response, __unused CFErrorRef *error) {
191 result = xpc_dictionary_get_bool(response, kSecXPCKeyResult);
194 return noError ? (result ? kCFBooleanTrue : kCFBooleanFalse) : NULL;
198 static CFSetRef cfset_cfset_to_cfset_error_request(enum SecXPCOperation op, CFSetRef set1, CFSetRef set2, CFErrorRef* error)
200 __block CFSetRef result = NULL;
202 secdebug("sosops","enter - operation: %d", op);
203 bool noError = securityd_send_sync_and_do(op, error, ^bool(xpc_object_t message, CFErrorRef *error) {
204 return SecXPCDictionarySetPList(message, kSecXPCKeySet, set1, error) && SecXPCDictionarySetPList(message, kSecXPCKeySet2, set2, error);
205 }, ^bool(xpc_object_t response, __unused CFErrorRef *error) {
206 result = SecXPCDictionaryCopySet(response, kSecXPCKeyResult, error);
211 CFReleaseNull(result);
216 static CF_RETURNS_RETAINED CFArrayRef simple_array_error_request(enum SecXPCOperation op, CFErrorRef* error)
218 __block CFArrayRef result = NULL;
220 secdebug("sosops","enter - operation: %d", op);
221 if (securityd_send_sync_and_do(op, error, NULL, ^bool(xpc_object_t response, CFErrorRef *error) {
222 xpc_object_t temp_result = xpc_dictionary_get_value(response, kSecXPCKeyResult);
223 result = _CFXPCCreateCFObjectFromXPCObject(temp_result);
224 return result != NULL;
226 if (!isArray(result)) {
227 SOSErrorCreate(kSOSErrorUnexpectedType, error, NULL, CFSTR("Expected array, got: %@"), result);
228 CFReleaseNull(result);
234 static CF_RETURNS_RETAINED CFArrayRef der_array_error_request(enum SecXPCOperation op, CFErrorRef* error)
236 __block CFArrayRef result = NULL;
238 secdebug("sosops","enter - operation: %d", op);
239 if (securityd_send_sync_and_do(op, error, NULL, ^bool(xpc_object_t response, CFErrorRef *error) {
241 const uint8_t* bytes = xpc_dictionary_get_data(response, kSecXPCKeyResult, &length);
242 der_decode_plist(kCFAllocatorDefault, (CFPropertyListRef*) &result, error, bytes, bytes + length);
244 return result != NULL;
246 if (!isArray(result)) {
247 SOSErrorCreate(kSOSErrorUnexpectedType, error, NULL, CFSTR("Expected array, got: %@"), result);
248 CFReleaseNull(result);
254 static int simple_int_error_request(enum SecXPCOperation op, CFErrorRef* error)
256 __block int result = 0;
258 secdebug("sosops","enter - operation: %d", op);
259 securityd_send_sync_and_do(op, error, NULL, ^bool(xpc_object_t response, __unused CFErrorRef *error) {
260 int64_t temp_result = xpc_dictionary_get_int64(response, kSecXPCKeyResult);
261 if ((temp_result >= INT32_MIN) && (temp_result <= INT32_MAX)) {
262 result = (int)temp_result;
269 static CF_RETURNS_RETAINED SOSPeerInfoRef peer_info_error_request(enum SecXPCOperation op, CFErrorRef* error)
271 SOSPeerInfoRef result = NULL;
272 __block CFDataRef data = NULL;
274 secdebug("sosops","enter - operation: %d", op);
275 securityd_send_sync_and_do(op, error, NULL, ^bool(xpc_object_t response, CFErrorRef *error) {
276 xpc_object_t temp_result = xpc_dictionary_get_value(response, kSecXPCKeyResult);
277 if (response && (NULL != temp_result)) {
278 data = _CFXPCCreateCFObjectFromXPCObject(temp_result);
284 result = SOSPeerInfoCreateFromData(kCFAllocatorDefault, error, data);
286 SOSErrorCreate(kSOSErrorUnexpectedType, error, NULL, CFSTR("Expected CFData, got: %@"), data);
293 static CFDataRef flags_to_data_error_request(enum SecXPCOperation op, uint32_t flags, CFErrorRef *error)
295 __block CFDataRef result = NULL;
297 secdebug("sosops", "enter -- operation: %d", op);
298 securityd_send_sync_and_do(op, error, ^bool(xpc_object_t message, CFErrorRef *error) {
299 xpc_dictionary_set_uint64(message, kSecXPCKeyFlags, flags);
301 }, ^bool(xpc_object_t response, CFErrorRef *error) {
302 xpc_object_t temp_result = xpc_dictionary_get_value(response, kSecXPCKeyResult);
303 if (response && (NULL != temp_result)) {
304 result = _CFXPCCreateCFObjectFromXPCObject(temp_result);
306 return result != NULL;
309 if (!isData(result)) {
310 SOSErrorCreate(kSOSErrorUnexpectedType, error, NULL, CFSTR("Expected CFData, got: %@"), result);
318 static CFDataRef data_to_error_request(enum SecXPCOperation op, CFErrorRef *error)
320 __block CFDataRef result = NULL;
322 secdebug("sosops", "enter -- operation: %d", op);
323 securityd_send_sync_and_do(op, error, NULL, ^bool(xpc_object_t response, CFErrorRef *error) {
324 xpc_object_t temp_result = xpc_dictionary_get_value(response, kSecXPCKeyResult);
325 if (response && (NULL != temp_result)) {
326 result = _CFXPCCreateCFObjectFromXPCObject(temp_result);
328 return result != NULL;
331 if (!isData(result)) {
332 SOSErrorCreate(kSOSErrorUnexpectedType, error, NULL, CFSTR("Expected CFData, got: %@"), result);
339 static CFArrayRef array_of_info_error_request(enum SecXPCOperation op, CFErrorRef* error)
341 __block CFArrayRef result = NULL;
343 secdebug("sosops","enter - operation: %d", op);
344 securityd_send_sync_and_do(op, error, NULL, ^bool(xpc_object_t response, CFErrorRef *error) {
345 xpc_object_t encoded_array = xpc_dictionary_get_value(response, kSecXPCKeyResult);
346 if (response && (NULL != encoded_array)) {
347 result = CreateArrayOfPeerInfoWithXPCObject(encoded_array, error);
349 return result != NULL;
352 if (!isArray(result)) {
353 SOSErrorCreate(kSOSErrorUnexpectedType, error, NULL, CFSTR("Expected array, got: %@"), result);
354 CFReleaseNull(result);
359 static CF_RETURNS_RETAINED SOSPeerInfoRef data_to_peer_info_error_request(enum SecXPCOperation op, CFDataRef secret, CFErrorRef* error)
361 __block SOSPeerInfoRef result = false;
362 __block CFDataRef data = NULL;
364 secdebug("sosops", "enter - operation: %d", op);
365 securityd_send_sync_and_do(op, error, ^bool(xpc_object_t message, CFErrorRef *error) {
366 xpc_object_t xsecretData = _CFXPCCreateXPCObjectFromCFObject(secret);
367 bool success = false;
369 xpc_dictionary_set_value(message, kSecXPCKeyNewPublicBackupKey, xsecretData);
374 }, ^bool(xpc_object_t response, __unused CFErrorRef *error) {
375 xpc_object_t temp_result = xpc_dictionary_get_value(response, kSecXPCKeyResult);
376 if (response && (NULL != temp_result)) {
377 data = _CFXPCCreateCFObjectFromXPCObject(temp_result);
383 result = SOSPeerInfoCreateFromData(kCFAllocatorDefault, error, data);
385 SOSErrorCreate(kSOSErrorUnexpectedType, error, NULL, CFSTR("Expected CFData, got: %@"), data);
392 static bool keybag_and_bool_to_bool_error_request(enum SecXPCOperation op, CFDataRef data, bool include, CFErrorRef* error)
394 secdebug("sosops", "enter - operation: %d", op);
395 return securityd_send_sync_and_do(op, error, ^bool(xpc_object_t message, CFErrorRef *error) {
396 xpc_object_t xData = _CFXPCCreateXPCObjectFromCFObject(data);
397 bool success = false;
399 xpc_dictionary_set_value(message, kSecXPCKeyKeybag, xData);
403 xpc_dictionary_set_bool(message, kSecXPCKeyIncludeV0, include);
405 }, ^bool(xpc_object_t response, __unused CFErrorRef *error) {
406 return xpc_dictionary_get_bool(response, kSecXPCKeyResult);
410 static bool recovery_and_bool_to_bool_error_request(enum SecXPCOperation op, CFDataRef data, CFErrorRef* error)
412 secdebug("sosops", "enter - operation: %d", op);
413 return securityd_send_sync_and_do(op, error, ^bool(xpc_object_t message, CFErrorRef *error) {
414 xpc_object_t xData = NULL;
416 xData = _CFXPCCreateXPCObjectFromCFObject(data);
419 CFDataRef nullData = CFDataCreate(kCFAllocatorDefault, &zero, 1);
420 xData = _CFXPCCreateXPCObjectFromCFObject(nullData);
421 CFReleaseNull(nullData);
423 bool success = false;
425 xpc_dictionary_set_value(message, kSecXPCKeyRecoveryPublicKey, xData);
430 }, ^bool(xpc_object_t response, __unused CFErrorRef *error) {
431 return xpc_dictionary_get_bool(response, kSecXPCKeyResult);
435 static bool info_array_to_bool_error_request(enum SecXPCOperation op, CFArrayRef peer_infos, CFErrorRef* error)
437 __block bool result = false;
439 secdebug("sosops", "enter - operation: %d", op);
440 securityd_send_sync_and_do(op, error, ^bool(xpc_object_t message, CFErrorRef *error) {
441 xpc_object_t encoded_peers = CreateXPCObjectWithArrayOfPeerInfo(peer_infos, error);
443 xpc_dictionary_set_value(message, kSecXPCKeyPeerInfoArray, encoded_peers);
444 return encoded_peers != NULL;
445 }, ^bool(xpc_object_t response, __unused CFErrorRef *error) {
446 result = xpc_dictionary_get_bool(response, kSecXPCKeyResult);
452 static bool uint64_t_to_bool_error_request(enum SecXPCOperation op,
456 __block bool result = false;
458 securityd_send_sync_and_do(op, error, ^bool(xpc_object_t message, CFErrorRef *error) {
459 xpc_dictionary_set_uint64(message, kSecXPCLimitInMinutes, number);
461 }, ^bool(xpc_object_t response, __unused CFErrorRef *error) {
462 result = xpc_dictionary_get_bool(response, kSecXPCKeyResult);
469 static bool cfdata_and_int_error_request_returns_bool(enum SecXPCOperation op, CFDataRef thedata,
470 PiggyBackProtocolVersion version, CFErrorRef *error) {
471 __block bool result = false;
473 sec_trace_enter_api(NULL);
474 securityd_send_sync_and_do(op, error, ^(xpc_object_t message, CFErrorRef *error) {
475 xpc_object_t xdata = _CFXPCCreateXPCObjectFromCFObject(thedata);
476 bool success = false;
478 xpc_dictionary_set_value(message, kSecXPCData, xdata);
479 xpc_dictionary_set_uint64(message, kSecXPCVersion, version);
485 }, ^(xpc_object_t response, __unused CFErrorRef *error) {
486 result = xpc_dictionary_get_bool(response, kSecXPCKeyResult);
493 static CFDataRef cfdata_error_request_returns_cfdata(enum SecXPCOperation op, CFDataRef thedata, CFErrorRef *error) {
494 __block CFDataRef result = NULL;
496 sec_trace_enter_api(NULL);
497 securityd_send_sync_and_do(op, error, ^(xpc_object_t message, CFErrorRef *error) {
498 xpc_object_t xdata = _CFXPCCreateXPCObjectFromCFObject(thedata);
499 bool success = false;
501 xpc_dictionary_set_value(message, kSecXPCData, xdata);
506 }, ^(xpc_object_t response, __unused CFErrorRef *error) {
507 xpc_object_t temp_result = xpc_dictionary_get_value(response, kSecXPCKeyResult);
508 if (response && (NULL != temp_result)) {
509 CFTypeRef object = _CFXPCCreateCFObjectFromXPCObject(temp_result);
510 result = copyIfData(object, error);
511 CFReleaseNull(object);
513 return (bool) (result != NULL);
519 bool SOSCCRequestToJoinCircle(CFErrorRef* error)
522 secdebug("circleOps", "SOS disabled for this platform");
523 setSOSDisabledError(error);
527 sec_trace_enter_api(NULL);
528 sec_trace_return_bool_api(^{
529 do_if_registered(soscc_RequestToJoinCircle, error);
531 return simple_bool_error_request(kSecXPCOpRequestToJoin, error);
535 bool SOSCCRequestToJoinCircleAfterRestore(CFErrorRef* error)
538 secdebug("circleOps", "SOS disabled for this platform");
539 setSOSDisabledError(error);
543 sec_trace_enter_api(NULL);
544 sec_trace_return_bool_api(^{
545 do_if_registered(soscc_RequestToJoinCircleAfterRestore, error);
547 return simple_bool_error_request(kSecXPCOpRequestToJoinAfterRestore, error);
551 bool SOSCCAccountHasPublicKey(CFErrorRef *error)
554 secdebug("circleOps", "SOS disabled for this platform");
555 setSOSDisabledError(error);
559 sec_trace_enter_api(NULL);
560 sec_trace_return_bool_api(^{
561 do_if_registered(soscc_AccountHasPublicKey, error);
563 return simple_bool_error_request(kSecXPCOpAccountHasPublicKey, error);
568 bool SOSCCWaitForInitialSync(CFErrorRef* error)
571 secdebug("circleOps", "SOS disabled for this platform");
572 setSOSDisabledError(error);
576 sec_trace_enter_api(NULL);
577 sec_trace_return_bool_api(^{
578 do_if_registered(soscc_WaitForInitialSync, error);
580 return simple_bool_error_request(kSecXPCOpWaitForInitialSync, error);
584 bool SOSCCAccountSetToNew(CFErrorRef *error)
587 secdebug("circleOps", "SOS disabled for this platform");
588 setSOSDisabledError(error);
592 secwarning("SOSCCAccountSetToNew called");
593 sec_trace_enter_api(NULL);
594 sec_trace_return_bool_api(^{
595 do_if_registered(soscc_SetToNew, error);
596 return simple_bool_error_request(kSecXPCOpAccountSetToNew, error);
600 bool SOSCCResetToOffering(CFErrorRef* error)
603 secdebug("circleOps", "SOS disabled for this platform");
604 setSOSDisabledError(error);
608 secwarning("SOSCCResetToOffering called");
609 sec_trace_enter_api(NULL);
610 sec_trace_return_bool_api(^{
611 do_if_registered(soscc_ResetToOffering, error);
613 return simple_bool_error_request(kSecXPCOpResetToOffering, error);
617 bool SOSCCResetToEmpty(CFErrorRef* error)
620 secdebug("circleOps", "SOS disabled for this platform");
621 setSOSDisabledError(error);
625 secwarning("SOSCCResetToEmpty called");
626 sec_trace_enter_api(NULL);
627 sec_trace_return_bool_api(^{
628 do_if_registered(soscc_ResetToEmpty, error);
630 return simple_bool_error_request(kSecXPCOpResetToEmpty, error);
634 bool SOSCCRemovePeersFromCircle(CFArrayRef peers, CFErrorRef* error)
637 secdebug("circleOps", "SOS disabled for this platform");
638 setSOSDisabledError(error);
642 sec_trace_enter_api(NULL);
643 sec_trace_return_bool_api(^{
644 do_if_registered(soscc_RemovePeersFromCircle, peers, error);
646 return info_array_to_bool_error_request(kSecXPCOpRemovePeersFromCircle, peers, error);
650 bool SOSCCRemoveThisDeviceFromCircle(CFErrorRef* error)
653 secdebug("circleOps", "SOS disabled for this platform");
654 setSOSDisabledError(error);
658 sec_trace_enter_api(NULL);
659 sec_trace_return_bool_api(^{
660 do_if_registered(soscc_RemoveThisDeviceFromCircle, error);
662 return simple_bool_error_request(kSecXPCOpRemoveThisDeviceFromCircle, error);
666 bool SOSCCLoggedIntoAccount(CFErrorRef* error) {
667 sec_trace_enter_api(NULL);
668 sec_trace_return_bool_api(^{
669 do_if_registered(soscc_LoggedIntoAccount, error);
671 return simple_bool_error_request(kSecXPCOpLoggedIntoAccount, error);
675 bool SOSCCLoggedOutOfAccount(CFErrorRef* error)
678 secdebug("circleOps", "SOS disabled for this platform");
679 setSOSDisabledError(error);
683 sec_trace_enter_api(NULL);
684 sec_trace_return_bool_api(^{
685 do_if_registered(soscc_LoggedOutOfAccount, error);
687 return simple_bool_error_request(kSecXPCOpLoggedOutOfAccount, error);
691 bool SOSCCBailFromCircle_BestEffort(uint64_t limit_in_seconds, CFErrorRef* error)
694 secdebug("circleOps", "SOS disabled for this platform");
695 setSOSDisabledError(error);
699 sec_trace_enter_api(NULL);
700 sec_trace_return_bool_api(^{
701 do_if_registered(soscc_BailFromCircle, limit_in_seconds, error);
703 return uint64_t_to_bool_error_request(kSecXPCOpBailFromCircle, limit_in_seconds, error);
707 CFArrayRef SOSCCCopyPeerPeerInfo(CFErrorRef* error)
710 secdebug("circleOps", "SOS disabled for this platform");
711 setSOSDisabledError(error);
715 sec_trace_enter_api(NULL);
716 sec_trace_return_api(CFArrayRef, ^{
717 do_if_registered(soscc_CopyPeerInfo, error);
719 return array_of_info_error_request(kSecXPCOpCopyPeerPeerInfo, error);
720 }, CFSTR("return=%@"));
723 CFArrayRef SOSCCCopyConcurringPeerPeerInfo(CFErrorRef* error)
726 secdebug("circleOps", "SOS disabled for this platform");
727 setSOSDisabledError(error);
731 sec_trace_enter_api(NULL);
732 sec_trace_return_api(CFArrayRef, ^{
733 do_if_registered(soscc_CopyConcurringPeerInfo, error);
735 return array_of_info_error_request(kSecXPCOpCopyConcurringPeerPeerInfo, error);
736 }, CFSTR("return=%@"));
739 CFArrayRef SOSCCCopyGenerationPeerInfo(CFErrorRef* error)
742 secdebug("circleOps", "SOS disabled for this platform");
743 setSOSDisabledError(error);
747 sec_trace_enter_api(NULL);
748 sec_trace_return_api(CFArrayRef, ^{
749 do_if_registered(soscc_CopyGenerationPeerInfo, error);
751 return simple_array_error_request(kSecXPCOpCopyGenerationPeerInfo, error);
752 }, CFSTR("return=%@"));
755 CFArrayRef SOSCCCopyApplicantPeerInfo(CFErrorRef* error)
758 secdebug("circleOps", "SOS disabled for this platform");
759 setSOSDisabledError(error);
763 sec_trace_enter_api(NULL);
764 sec_trace_return_api(CFArrayRef, ^{
765 do_if_registered(soscc_CopyApplicantPeerInfo, error);
767 return array_of_info_error_request(kSecXPCOpCopyApplicantPeerInfo, error);
768 }, CFSTR("return=%@"));
771 bool SOSCCValidateUserPublic(CFErrorRef* error){
773 secdebug("circleOps", "SOS disabled for this platform");
774 setSOSDisabledError(error);
778 sec_trace_enter_api(NULL);
779 sec_trace_return_api(bool, ^{
780 do_if_registered(soscc_ValidateUserPublic, error);
782 return simple_bool_error_request(kSecXPCOpValidateUserPublic, error);
786 CFArrayRef SOSCCCopyValidPeerPeerInfo(CFErrorRef* error)
789 secdebug("circleOps", "SOS disabled for this platform");
790 setSOSDisabledError(error);
794 sec_trace_enter_api(NULL);
795 sec_trace_return_api(CFArrayRef, ^{
796 do_if_registered(soscc_CopyValidPeerPeerInfo, error);
798 return array_of_info_error_request(kSecXPCOpCopyValidPeerPeerInfo, error);
799 }, CFSTR("return=%@"));
802 CFArrayRef SOSCCCopyNotValidPeerPeerInfo(CFErrorRef* error)
805 secdebug("circleOps", "SOS disabled for this platform");
806 setSOSDisabledError(error);
810 sec_trace_enter_api(NULL);
811 sec_trace_return_api(CFArrayRef, ^{
812 do_if_registered(soscc_CopyNotValidPeerPeerInfo, error);
814 return array_of_info_error_request(kSecXPCOpCopyNotValidPeerPeerInfo, error);
815 }, CFSTR("return=%@"));
818 CFArrayRef SOSCCCopyRetirementPeerInfo(CFErrorRef* error)
821 secdebug("circleOps", "SOS disabled for this platform");
822 setSOSDisabledError(error);
826 sec_trace_enter_api(NULL);
827 sec_trace_return_api(CFArrayRef, ^{
828 do_if_registered(soscc_CopyRetirementPeerInfo, error);
830 return array_of_info_error_request(kSecXPCOpCopyRetirementPeerInfo, error);
831 }, CFSTR("return=%@"));
834 CFArrayRef SOSCCCopyViewUnawarePeerInfo(CFErrorRef* error)
837 secdebug("circleOps", "SOS disabled for this platform");
838 setSOSDisabledError(error);
842 sec_trace_enter_api(NULL);
843 sec_trace_return_api(CFArrayRef, ^{
844 do_if_registered(soscc_CopyViewUnawarePeerInfo, error);
846 return array_of_info_error_request(kSecXPCOpCopyViewUnawarePeerInfo, error);
847 }, CFSTR("return=%@"));
850 SOSPeerInfoRef SOSCCCopyMyPeerInfo(CFErrorRef *error)
853 secdebug("circleOps", "SOS disabled for this platform");
854 setSOSDisabledError(error);
858 sec_trace_enter_api(NULL);
859 sec_trace_return_api(SOSPeerInfoRef, ^{
860 do_if_registered(soscc_CopyMyPeerInfo, error);
862 return peer_info_error_request(kSecXPCOpCopyMyPeerInfo, error);
863 }, CFSTR("return=%@"));
866 static CFArrayRef SOSCCCopyEngineState(CFErrorRef* error)
869 secdebug("circleOps", "SOS disabled for this platform");
870 setSOSDisabledError(error);
874 sec_trace_enter_api(NULL);
875 sec_trace_return_api(CFArrayRef, ^{
876 do_if_registered(soscc_CopyEngineState, error);
878 return der_array_error_request(kSecXPCOpCopyEngineState, error);
879 }, CFSTR("return=%@"));
882 CFStringRef kSOSCCEngineStatePeerIDKey = CFSTR("PeerID");
883 CFStringRef kSOSCCEngineStateManifestCountKey = CFSTR("ManifestCount");
884 CFStringRef kSOSCCEngineStateSyncSetKey = CFSTR("SyncSet");
885 CFStringRef kSOSCCEngineStateCoderKey = CFSTR("CoderDump");
886 CFStringRef kSOSCCEngineStateManifestHashKey = CFSTR("ManifestHash");
888 void SOSCCForEachEngineStateAsStringFromArray(CFArrayRef states, void (^block)(CFStringRef oneStateString)) {
890 secdebug("circleOps", "SOS disabled for this platform");
895 CFArrayForEach(states, ^(const void *value) {
896 CFDictionaryRef dict = asDictionary(value, NULL);
898 CFMutableStringRef description = CFStringCreateMutable(kCFAllocatorDefault, 0);
900 CFStringRef id = asString(CFDictionaryGetValue(dict, kSOSCCEngineStatePeerIDKey), NULL);
903 CFStringAppendFormat(description, NULL, CFSTR("remote %@ "), id);
905 CFStringAppendFormat(description, NULL, CFSTR("local "));
908 CFSetRef viewSet = asSet(CFDictionaryGetValue(dict, kSOSCCEngineStateSyncSetKey), NULL);
910 CFStringSetPerformWithDescription(viewSet, ^(CFStringRef setDescription) {
911 CFStringAppend(description, setDescription);
914 CFStringAppendFormat(description, NULL, CFSTR("<Missing view set!>"));
917 CFStringAppendFormat(description, NULL, CFSTR(" [%@]"),
918 CFDictionaryGetValue(dict, kSOSCCEngineStateManifestCountKey));
920 CFDataRef mainfestHash = asData(CFDictionaryGetValue(dict, kSOSCCEngineStateManifestHashKey), NULL);
923 CFDataPerformWithHexString(mainfestHash, ^(CFStringRef dataString) {
924 CFStringAppendFormat(description, NULL, CFSTR(" %@"), dataString);
928 CFStringRef coderDescription = asString(CFDictionaryGetValue(dict, kSOSCCEngineStateCoderKey), NULL);
929 if (coderDescription) {
930 CFStringAppendFormat(description, NULL, CFSTR(" %@"), coderDescription);
935 CFReleaseNull(description);
940 bool SOSCCForEachEngineStateAsString(CFErrorRef* error, void (^block)(CFStringRef oneStateString)) {
942 secdebug("circleOps", "SOS disabled for this platform");
943 setSOSDisabledError(error);
947 CFArrayRef states = SOSCCCopyEngineState(error);
951 SOSCCForEachEngineStateAsStringFromArray(states, block);
953 CFReleaseNull(states);
959 bool SOSCCAcceptApplicants(CFArrayRef applicants, CFErrorRef* error)
962 secdebug("circleOps", "SOS disabled for this platform");
963 setSOSDisabledError(error);
967 sec_trace_enter_api(NULL);
968 sec_trace_return_bool_api(^{
969 do_if_registered(soscc_AcceptApplicants, applicants, error);
971 return info_array_to_bool_error_request(kSecXPCOpAcceptApplicants, applicants, error);
975 bool SOSCCRejectApplicants(CFArrayRef applicants, CFErrorRef *error)
978 secdebug("circleOps", "SOS disabled for this platform");
979 setSOSDisabledError(error);
983 sec_trace_enter_api(CFSTR("applicants=%@"), applicants);
984 sec_trace_return_bool_api(^{
985 do_if_registered(soscc_RejectApplicants, applicants, error);
987 return info_array_to_bool_error_request(kSecXPCOpRejectApplicants, applicants, error);
991 static CF_RETURNS_RETAINED SOSPeerInfoRef SOSSetNewPublicBackupKey(CFDataRef pubKey, CFErrorRef *error)
993 sec_trace_enter_api(NULL);
994 sec_trace_return_api(SOSPeerInfoRef, ^{
995 do_if_registered(soscc_SetNewPublicBackupKey, pubKey, error);
997 return data_to_peer_info_error_request(kSecXPCOpSetNewPublicBackupKey, pubKey, error);
998 }, CFSTR("return=%@"));
1001 SOSPeerInfoRef SOSCCCopyMyPeerWithNewDeviceRecoverySecret(CFDataRef secret, CFErrorRef *error){
1003 secdebug("circleOps", "SOS disabled for this platform");
1004 setSOSDisabledError(error);
1008 secnotice("devRecovery", "Enter SOSCCCopyMyPeerWithNewDeviceRecoverySecret()");
1009 CFDataRef publicKeyData = SOSCopyDeviceBackupPublicKey(secret, error);
1010 secnotice("devRecovery", "SOSCopyDeviceBackupPublicKey (%@)", publicKeyData);
1011 SOSPeerInfoRef copiedPeer = publicKeyData ? SOSSetNewPublicBackupKey(publicKeyData, error) : NULL;
1012 secnotice("devRecovery", "SOSSetNewPublicBackupKey (%@)", copiedPeer);
1013 CFReleaseNull(publicKeyData);
1017 bool SOSCCRegisterSingleRecoverySecret(CFDataRef aks_bag, bool forV0Only, CFErrorRef *error){
1019 secdebug("circleOps", "SOS disabled for this platform");
1020 setSOSDisabledError(error);
1024 sec_trace_enter_api(NULL);
1025 sec_trace_return_bool_api(^{
1026 do_if_registered(soscc_RegisterSingleRecoverySecret, aks_bag, forV0Only, error);
1027 return keybag_and_bool_to_bool_error_request(kSecXPCOpSetBagForAllSlices, aks_bag, forV0Only, error);
1032 bool SOSCCRegisterRecoveryPublicKey(CFDataRef recovery_key, CFErrorRef *error){
1034 secdebug("circleOps", "SOS disabled for this platform");
1035 setSOSDisabledError(error);
1039 sec_trace_enter_api(NULL);
1040 sec_trace_return_bool_api(^{
1041 bool retval = false;
1042 do_if_registered(soscc_RegisterRecoveryPublicKey, recovery_key, error);
1043 // NULL recovery_key is handled in recovery_and_bool_to_bool_error_request now.
1044 retval = recovery_and_bool_to_bool_error_request(kSecXPCOpRegisterRecoveryPublicKey, recovery_key, error);
1049 CFDataRef SOSCCCopyRecoveryPublicKey(CFErrorRef *error){
1051 secdebug("circleOps", "SOS disabled for this platform");
1052 setSOSDisabledError(error);
1056 sec_trace_enter_api(NULL);
1057 sec_trace_return_api(CFDataRef, ^{
1058 do_if_registered(soscc_CopyRecoveryPublicKey, error);
1059 return data_to_error_request(kSecXPCOpGetRecoveryPublicKey, error);
1060 }, CFSTR("return=%@"));
1062 static bool label_and_password_to_bool_error_request(enum SecXPCOperation op,
1063 CFStringRef user_label, CFDataRef user_password,
1066 __block bool result = false;
1068 securityd_send_sync_and_do(op, error, ^bool(xpc_object_t message, CFErrorRef *error) {
1069 CFStringPerformWithCString(user_label, ^(const char *utf8Str) {
1070 xpc_dictionary_set_string(message, kSecXPCKeyUserLabel, utf8Str);
1072 xpc_dictionary_set_data(message, kSecXPCKeyUserPassword, CFDataGetBytePtr(user_password), CFDataGetLength(user_password));
1074 }, ^bool(xpc_object_t response, __unused CFErrorRef *error) {
1075 result = xpc_dictionary_get_bool(response, kSecXPCKeyResult);
1082 static bool label_and_password_and_dsid_to_bool_error_request(enum SecXPCOperation op,
1083 CFStringRef user_label, CFDataRef user_password,
1084 CFStringRef dsid, CFErrorRef* error)
1086 __block bool result = false;
1088 securityd_send_sync_and_do(op, error, ^bool(xpc_object_t message, CFErrorRef *error) {
1089 CFStringPerformWithCString(user_label, ^(const char *utf8Str) {
1090 xpc_dictionary_set_string(message, kSecXPCKeyUserLabel, utf8Str);
1092 CFStringPerformWithCString(dsid, ^(const char *utr8StrDSID) {
1093 xpc_dictionary_set_string(message, kSecXPCKeyDSID, utr8StrDSID);
1095 xpc_dictionary_set_data(message, kSecXPCKeyUserPassword, CFDataGetBytePtr(user_password), CFDataGetLength(user_password));
1097 }, ^bool(xpc_object_t response, __unused CFErrorRef *error) {
1098 result = xpc_dictionary_get_bool(response, kSecXPCKeyResult);
1105 bool SOSCCRegisterUserCredentials(CFStringRef user_label, CFDataRef user_password, CFErrorRef* error)
1108 secdebug("circleOps", "SOS disabled for this platform");
1109 setSOSDisabledError(error);
1113 secnotice("circleOps", "SOSCCRegisterUserCredentials - calling SOSCCSetUserCredentials for %@\n", user_label);
1114 return SOSCCSetUserCredentials(user_label, user_password, error);
1117 bool SOSCCSetUserCredentials(CFStringRef user_label, CFDataRef user_password, CFErrorRef* error)
1120 secdebug("circleOps", "SOS disabled for this platform");
1121 setSOSDisabledError(error);
1125 secnotice("circleOps", "SOSCCSetUserCredentials for %@\n", user_label);
1126 sec_trace_enter_api(CFSTR("user_label=%@"), user_label);
1127 sec_trace_return_bool_api(^{
1128 do_if_registered(soscc_SetUserCredentials, user_label, user_password, error);
1130 return label_and_password_to_bool_error_request(kSecXPCOpSetUserCredentials, user_label, user_password, error);
1134 bool SOSCCSetUserCredentialsAndDSID(CFStringRef user_label, CFDataRef user_password, CFStringRef dsid, CFErrorRef *error)
1137 secdebug("circleOps", "SOS disabled for this platform");
1138 setSOSDisabledError(error);
1142 secnotice("circleOps", "SOSCCSetUserCredentialsAndDSID for %@\n", user_label);
1143 sec_trace_enter_api(CFSTR("user_label=%@"), user_label);
1144 sec_trace_return_bool_api(^{
1145 do_if_registered(soscc_SetUserCredentialsAndDSID, user_label, user_password, dsid, error);
1147 bool result = false;
1148 __block CFStringRef account_dsid = dsid;
1150 require_action_quiet(user_label, out, SOSErrorCreate(kSOSErrorParam, error, NULL, CFSTR("user_label is nil")));
1151 require_action_quiet(user_password, out, SOSErrorCreate(kSOSErrorParam, error, NULL, CFSTR("user_password is nil")));
1153 if(account_dsid == NULL){
1154 account_dsid = CFSTR("");
1156 return label_and_password_and_dsid_to_bool_error_request(kSecXPCOpSetUserCredentialsAndDSID, user_label, user_password, account_dsid, error);
1163 static bool SOSCCTryUserCredentialsAndDSID_internal(CFStringRef user_label, CFDataRef user_password, CFStringRef dsid, CFErrorRef *error) {
1164 sec_trace_return_bool_api(^{
1165 do_if_registered(soscc_TryUserCredentials, user_label, user_password, dsid, error);
1167 bool result = false;
1168 __block CFStringRef account_dsid = dsid;
1170 require_action_quiet(user_label, out, SOSErrorCreate(kSOSErrorParam, error, NULL, CFSTR("user_label is nil")));
1171 require_action_quiet(user_password, out, SOSErrorCreate(kSOSErrorParam, error, NULL, CFSTR("user_password is nil")));
1173 if(account_dsid == NULL){
1174 account_dsid = CFSTR("");
1177 return label_and_password_and_dsid_to_bool_error_request(kSecXPCOpTryUserCredentials, user_label, user_password, account_dsid, error);
1185 bool SOSCCTryUserCredentialsAndDSID(CFStringRef user_label, CFDataRef user_password, CFStringRef dsid, CFErrorRef *error)
1188 secdebug("circleOps", "SOS disabled for this platform");
1189 setSOSDisabledError(error);
1193 secnotice("sosops", "SOSCCTryUserCredentialsAndDSID!! %@\n", user_label);
1194 require_action_quiet(user_label, out, SOSErrorCreate(kSOSErrorParam, error, NULL, CFSTR("user_label is nil")));
1195 require_action_quiet(user_password, out, SOSErrorCreate(kSOSErrorParam, error, NULL, CFSTR("user_password is nil")));
1196 CFStringRef account_dsid = (dsid != NULL) ? dsid: CFSTR("");
1197 return SOSCCTryUserCredentialsAndDSID_internal(user_label, user_password, account_dsid, error);
1202 bool SOSCCTryUserCredentials(CFStringRef user_label, CFDataRef user_password, CFErrorRef* error) {
1204 secdebug("circleOps", "SOS disabled for this platform");
1205 setSOSDisabledError(error);
1209 return SOSCCTryUserCredentialsAndDSID_internal(user_label, user_password, NULL, error);
1213 bool SOSCCCanAuthenticate(CFErrorRef* error) {
1215 secdebug("circleOps", "SOS disabled for this platform");
1216 setSOSDisabledError(error);
1220 sec_trace_enter_api(NULL);
1221 sec_trace_return_bool_api(^{
1222 do_if_registered(soscc_CanAuthenticate, error);
1224 return simple_bool_error_request(kSecXPCOpCanAuthenticate, error);
1228 bool SOSCCPurgeUserCredentials(CFErrorRef* error) {
1230 secdebug("circleOps", "SOS disabled for this platform");
1231 setSOSDisabledError(error);
1235 sec_trace_enter_api(NULL);
1236 sec_trace_return_bool_api(^{
1237 do_if_registered(soscc_PurgeUserCredentials, error);
1239 return simple_bool_error_request(kSecXPCOpPurgeUserCredentials, error);
1243 enum DepartureReason SOSCCGetLastDepartureReason(CFErrorRef *error) {
1245 secdebug("circleOps", "SOS disabled for this platform");
1246 setSOSDisabledError(error);
1247 return kSOSDepartureReasonError;
1250 sec_trace_enter_api(NULL);
1251 sec_trace_return_api(enum DepartureReason, ^{
1252 do_if_registered(soscc_GetLastDepartureReason, error);
1254 return (enum DepartureReason) simple_int_error_request(kSecXPCOpGetLastDepartureReason, error);
1258 bool SOSCCSetLastDepartureReason(enum DepartureReason reason, CFErrorRef *error) {
1260 secdebug("circleOps", "SOS disabled for this platform");
1261 setSOSDisabledError(error);
1265 sec_trace_enter_api(NULL);
1266 sec_trace_return_api(bool, ^{
1267 do_if_registered(soscc_SetLastDepartureReason, reason, error);
1268 return securityd_send_sync_and_do(kSecXPCOpSetLastDepartureReason, error,
1269 ^bool(xpc_object_t message, CFErrorRef *error) {
1270 xpc_dictionary_set_int64(message, kSecXPCKeyReason, reason);
1273 ^bool(xpc_object_t response, __unused CFErrorRef *error) {
1274 return xpc_dictionary_get_bool(response, kSecXPCKeyResult);
1280 bool SOSCCProcessEnsurePeerRegistration(CFErrorRef* error){
1282 secdebug("circleOps", "SOS disabled for this platform");
1283 setSOSDisabledError(error);
1287 secnotice("updates", "enter SOSCCProcessEnsurePeerRegistration");
1288 sec_trace_enter_api(NULL);
1289 sec_trace_return_bool_api(^{
1290 do_if_registered(soscc_EnsurePeerRegistration, error);
1292 return simple_bool_error_request(soscc_EnsurePeerRegistration_id, error);
1297 CFSetRef /* CFString */ SOSCCProcessSyncWithPeers(CFSetRef peers, CFSetRef backupPeers, CFErrorRef* error)
1300 secdebug("circleOps", "SOS disabled for this platform");
1301 setSOSDisabledError(error);
1305 sec_trace_enter_api(NULL);
1306 sec_trace_return_api(CFSetRef, ^{
1307 do_if_registered(soscc_ProcessSyncWithPeers, peers, backupPeers, error);
1309 return cfset_cfset_to_cfset_error_request(kSecXPCOpProcessSyncWithPeers, peers, backupPeers, error);
1314 SyncWithAllPeersReason SOSCCProcessSyncWithAllPeers(CFErrorRef* error)
1317 secdebug("circleOps", "SOS disabled for this platform");
1318 setSOSDisabledError(error);
1319 return kSyncWithAllPeersOtherFail;
1322 sec_trace_enter_api(NULL);
1323 sec_trace_return_api(SyncWithAllPeersReason, ^{
1324 do_if_registered(soscc_ProcessSyncWithAllPeers, error);
1326 return (SyncWithAllPeersReason) simple_int_error_request(kSecXPCOpProcessSyncWithAllPeers, error);
1330 CFStringRef SOSCCGetStatusDescription(SOSCCStatus status)
1333 case kSOSCCInCircle:
1334 return CFSTR("InCircle");
1335 case kSOSCCNotInCircle:
1336 return CFSTR("NotInCircle");
1337 case kSOSCCRequestPending:
1338 return CFSTR("RequestPending");
1339 case kSOSCCCircleAbsent:
1340 return CFSTR("CircleAbsent");
1342 return CFSTR("InternalError");
1344 return CFSTR("Unknown Status");
1349 CFStringRef SOSCCGetViewResultDescription(SOSViewResultCode vrc)
1352 case kSOSCCGeneralViewError:
1353 return CFSTR("GeneralViewError");
1354 case kSOSCCViewMember:
1355 return CFSTR("ViewMember");
1356 case kSOSCCViewNotMember:
1357 return CFSTR("ViewNotMember");
1358 case kSOSCCViewNotQualified:
1359 return CFSTR("ViewNotQualified");
1360 case kSOSCCNoSuchView:
1361 return CFSTR("ViewUndefined");
1363 return CFSTR("Unknown View Status");
1368 static int64_t name_action_to_code_request(enum SecXPCOperation op, uint16_t error_result,
1369 CFStringRef name, uint64_t action, CFErrorRef *error) {
1370 __block int64_t result = error_result;
1372 securityd_send_sync_and_do(op, error, ^bool(xpc_object_t message, CFErrorRef *error) {
1373 CFStringPerformWithCString(name, ^(const char *utf8Str) {
1374 xpc_dictionary_set_string(message, kSecXPCKeyViewName, utf8Str);
1376 xpc_dictionary_set_int64(message, kSecXPCKeyViewActionCode, action);
1378 }, ^bool(xpc_object_t response, __unused CFErrorRef *error) {
1379 if (response && xpc_dictionary_entry_is_type(response, kSecXPCKeyResult, XPC_TYPE_INT64)) {
1380 result = xpc_dictionary_get_int64(response, kSecXPCKeyResult);
1382 return result != error_result;
1388 SOSViewResultCode SOSCCView(CFStringRef view, SOSViewActionCode actionCode, CFErrorRef *error) {
1390 secdebug("circleOps", "SOS disabled for this platform");
1391 setSOSDisabledError(error);
1392 return kSOSCCGeneralViewError;
1395 if(actionCode == kSOSCCViewQuery) {
1396 uint64_t circleStat = SOSGetCachedCircleBitmask();
1397 if(circleStat & CC_STATISVALID) {
1398 SOSViewResultCode retval = kSOSCCViewNotMember;
1399 CFSetRef enabledViews = SOSCreateCachedViewStatus();
1401 if(CFSetContainsValue(enabledViews, view)) {
1402 retval = kSOSCCViewMember;
1404 retval = kSOSCCViewNotMember;
1406 CFReleaseNull(enabledViews);
1411 sec_trace_enter_api(NULL);
1412 sec_trace_return_api(SOSViewResultCode, ^{
1413 do_if_registered(soscc_View, view, actionCode, error);
1415 return (SOSViewResultCode) name_action_to_code_request(kSecXPCOpView, kSOSCCGeneralViewError, view, actionCode, error);
1416 }, CFSTR("SOSViewResultCode=%d"))
1420 bool SOSCCViewSet(CFSetRef enabledViews, CFSetRef disabledViews) {
1422 secdebug("circleOps", "SOS disabled for this platform");
1426 CFErrorRef *error = NULL;
1427 __block bool result = false;
1429 sec_trace_enter_api(NULL);
1430 sec_trace_return_bool_api(^{
1431 do_if_registered(soscc_ViewSet, enabledViews, disabledViews);
1432 return securityd_send_sync_and_do(kSecXPCOpViewSet, error, ^bool(xpc_object_t message, CFErrorRef *error) {
1433 xpc_object_t enabledSetXpc = CreateXPCObjectWithCFSetRef(enabledViews, error);
1434 xpc_object_t disabledSetXpc = CreateXPCObjectWithCFSetRef(disabledViews, error);
1435 if (enabledSetXpc) xpc_dictionary_set_value(message, kSecXPCKeyEnabledViewsKey, enabledSetXpc);
1436 if (disabledSetXpc) xpc_dictionary_set_value(message, kSecXPCKeyDisabledViewsKey, disabledSetXpc);
1437 return (enabledSetXpc != NULL) || (disabledSetXpc != NULL) ;
1438 }, ^bool(xpc_object_t response, __unused CFErrorRef *error) {
1439 result = xpc_dictionary_get_bool(response, kSecXPCKeyResult);
1445 static CFStringRef copyViewNames(size_t n, CFStringRef *views) {
1446 CFMutableStringRef retval = CFStringCreateMutable(kCFAllocatorDefault, 0);
1447 CFStringAppend(retval, CFSTR("|"));
1448 for(size_t i = 0; i < n; i++) {
1449 CFStringAppend(retval, views[i]);
1450 CFStringAppend(retval, CFSTR("|"));
1455 static bool sosIsViewSetSyncing(size_t n, CFStringRef *views) {
1456 __block bool retval = true;
1457 CFErrorRef error = NULL;
1459 if(n == 0 || views == NULL) return false;
1460 CFStringRef viewString = copyViewNames(n, views);
1462 SOSCCStatus cstatus = SOSCCThisDeviceIsInCircle(&error);
1463 if(cstatus != kSOSCCInCircle) {
1464 secnotice("viewCheck", "Checking view / circle status for %@: SOSCCStatus: (%@) Error: (%@)", viewString, SOSCCGetStatusDescription(cstatus), error);
1468 if(retval == true) {
1469 // use cached values if valid
1470 uint64_t circleStat = SOSGetCachedCircleBitmask();
1471 if(circleStat & CC_STATISVALID) {
1472 CFSetRef enabledViews = SOSCreateCachedViewStatus();
1474 for(size_t i = 0; i < n; i++) {
1475 if(!CFSetContainsValue(enabledViews, views[i])) {
1479 CFReleaseNull(enabledViews);
1480 CFReleaseNull(viewString);
1485 // make the individual calls otherwise.
1486 for(size_t i = 0; i < n; i++) {
1487 SOSViewResultCode vstatus = SOSCCView(views[i], kSOSCCViewQuery, &error);
1488 if(vstatus != kSOSCCViewMember) {
1489 secnotice("viewCheck", "Checking view / circle status for %@: SOSCCStatus: (%@) SOSViewResultCode(%@) Error: (%@)", views[i],
1490 SOSCCGetStatusDescription(cstatus), SOSCCGetViewResultDescription(vstatus), error);
1496 if(retval == true) {
1497 secnotice("viewCheck", "Checking view / circle status for %@: ENABLED", viewString);
1499 CFReleaseNull(error);
1500 CFReleaseNull(viewString);
1504 bool SOSCCIsIcloudKeychainSyncing(void) {
1506 secdebug("circleOps", "SOS disabled for this platform");
1507 return false; // should we lie since CKKS is syncing - or forward?
1510 CFStringRef views[] = { kSOSViewWiFi, kSOSViewAutofillPasswords, kSOSViewSafariCreditCards, kSOSViewOtherSyncable };
1511 return sosIsViewSetSyncing(sizeof(views)/sizeof(views[0]), views);
1514 bool SOSCCIsSafariSyncing(void) {
1516 secdebug("circleOps", "SOS disabled for this platform");
1520 CFStringRef views[] = { kSOSViewAutofillPasswords, kSOSViewSafariCreditCards };
1521 return sosIsViewSetSyncing(sizeof(views)/sizeof(views[0]), views);
1524 bool SOSCCIsAppleTVSyncing(void) {
1526 secdebug("circleOps", "SOS disabled for this platform");
1530 CFStringRef views[] = { kSOSViewAppleTV };
1531 return sosIsViewSetSyncing(sizeof(views)/sizeof(views[0]), views);
1534 bool SOSCCIsHomeKitSyncing(void) {
1536 secdebug("circleOps", "SOS disabled for this platform");
1540 CFStringRef views[] = { kSOSViewHomeKit };
1541 return sosIsViewSetSyncing(sizeof(views)/sizeof(views[0]), views);
1544 bool SOSCCIsWiFiSyncing(void) {
1546 secdebug("circleOps", "SOS disabled for this platform");
1550 CFStringRef views[] = { kSOSViewWiFi };
1551 return sosIsViewSetSyncing(sizeof(views)/sizeof(views[0]), views);
1554 bool SOSCCIsContinuityUnlockSyncing(void) {
1556 secdebug("circleOps", "SOS disabled for this platform");
1560 CFStringRef views[] = { kSOSViewContinuityUnlock };
1561 return sosIsViewSetSyncing(sizeof(views)/sizeof(views[0]), views);
1564 SOSPeerInfoRef SOSCCCopyApplication(CFErrorRef *error) {
1566 secdebug("circleOps", "SOS disabled for this platform");
1567 setSOSDisabledError(error);
1571 secnotice("hsa2PB", "enter SOSCCCopyApplication applicant");
1572 sec_trace_enter_api(NULL);
1574 sec_trace_return_api(SOSPeerInfoRef, ^{
1575 do_if_registered(soscc_CopyApplicant, error);
1576 return peer_info_error_request(kSecXPCOpCopyApplication, error);
1577 }, CFSTR("return=%@"));
1580 bool SOSCCCleanupKVSKeys(CFErrorRef *error) {
1582 secdebug("circleOps", "SOS disabled for this platform");
1583 setSOSDisabledError(error);
1587 secnotice("cleanup-keys", "enter SOSCCCleanupKVSKeys");
1588 sec_trace_enter_api(NULL);
1589 sec_trace_return_bool_api(^{
1590 do_if_registered(soscc_SOSCCCleanupKVSKeys, error);
1592 return simple_bool_error_request(kSecXPCOpKVSKeyCleanup, error);
1598 CFDataRef SOSCCCopyCircleJoiningBlob(SOSPeerInfoRef applicant, CFErrorRef *error) {
1600 secdebug("circleOps", "SOS disabled for this platform");
1601 setSOSDisabledError(error);
1605 secnotice("hsa2PB", "enter SOSCCCopyCircleJoiningBlob approver");
1606 sec_trace_enter_api(NULL);
1608 sec_trace_return_api(CFDataRef, ^{
1609 CFDataRef result = NULL;
1610 do_if_registered(soscc_CopyCircleJoiningBlob, applicant, error);
1611 CFDataRef piData = SOSPeerInfoCopyEncodedData(applicant, kCFAllocatorDefault, error);
1612 result = cfdata_error_request_returns_cfdata(kSecXPCOpCopyCircleJoiningBlob, piData, error);
1613 CFReleaseNull(piData);
1615 }, CFSTR("return=%@"));
1618 CFDataRef SOSCCCopyInitialSyncData(SOSInitialSyncFlags flags, CFErrorRef *error) {
1620 secdebug("circleOps", "SOS disabled for this platform");
1621 setSOSDisabledError(error);
1625 secnotice("circleJoin", "enter SOSCCCopyInitialSyncData approver");
1626 sec_trace_enter_api(NULL);
1628 sec_trace_return_api(CFDataRef, ^{
1629 do_if_registered(soscc_CopyInitialSyncData, flags, error);
1630 return flags_to_data_error_request(kSecXPCOpCopyInitialSyncBlob, flags, error);
1631 }, CFSTR("return=%@"));
1634 bool SOSCCJoinWithCircleJoiningBlob(CFDataRef joiningBlob, PiggyBackProtocolVersion version, CFErrorRef *error) {
1636 secdebug("circleOps", "SOS disabled for this platform");
1637 setSOSDisabledError(error);
1641 secnotice("hsa2PB", "enter SOSCCJoinWithCircleJoiningBlob applicant");
1642 sec_trace_enter_api(NULL);
1643 sec_trace_return_bool_api(^{
1644 do_if_registered(soscc_JoinWithCircleJoiningBlob, joiningBlob, version, error);
1646 return cfdata_and_int_error_request_returns_bool(kSecXPCOpJoinWithCircleJoiningBlob, joiningBlob, version, error);
1650 CFBooleanRef SOSCCPeersHaveViewsEnabled(CFArrayRef viewNames, CFErrorRef *error) {
1652 secdebug("circleOps", "SOS disabled for this platform");
1653 setSOSDisabledError(error);
1657 secnotice("view-enabled", "enter SOSCCPeersHaveViewsEnabled");
1658 sec_trace_enter_api(NULL);
1659 sec_trace_return_api(CFBooleanRef, ^{
1660 do_if_registered(soscc_SOSCCPeersHaveViewsEnabled, viewNames, error);
1662 return cfarray_to_cfboolean_error_request(kSecXPCOpPeersHaveViewsEnabled, viewNames, error);
1663 }, CFSTR("return=%@"))
1666 bool SOSCCMessageFromPeerIsPending(SOSPeerInfoRef peer, CFErrorRef *error) {
1668 secdebug("circleOps", "SOS disabled for this platform");
1669 setSOSDisabledError(error);
1673 secnotice("pending-check", "enter SOSCCMessageFromPeerIsPending");
1675 sec_trace_return_bool_api(^{
1676 do_if_registered(soscc_SOSCCMessageFromPeerIsPending, peer, error);
1678 return peer_info_to_bool_error_request(kSecXPCOpMessageFromPeerIsPending, peer, error);
1683 bool SOSCCSendToPeerIsPending(SOSPeerInfoRef peer, CFErrorRef *error) {
1685 secdebug("circleOps", "SOS disabled for this platform");
1686 setSOSDisabledError(error);
1690 sec_trace_return_bool_api(^{
1691 do_if_registered(soscc_SOSCCSendToPeerIsPending, peer, error);
1693 return peer_info_to_bool_error_request(kSecXPCOpSendToPeerIsPending, peer, error);
1698 * SecSOSStatus interfaces
1701 @interface SecSOSStatus : NSObject {
1702 NSXPCConnection* _connection;
1704 @property NSXPCConnection *connection;
1707 @implementation SecSOSStatus
1708 @synthesize connection = _connection;
1710 - (instancetype) init
1712 if ((self = [super init]) == NULL)
1715 NSXPCInterface *interface = [NSXPCInterface interfaceWithProtocol:@protocol(SOSControlProtocol)];
1716 _SOSControlSetupInterface(interface);
1718 self.connection = [[NSXPCConnection alloc] initWithMachServiceName:@(kSecuritydSOSServiceName) options:0];
1719 if (self.connection == NULL){
1722 self.connection.remoteObjectInterface = interface;
1724 [self.connection resume];
1731 static id<SOSControlProtocol>
1732 SOSCCGetStatusObject(CFErrorRef *error)
1735 secdebug("circleOps", "SOS disabled for this platform");
1736 setSOSDisabledError(error);
1740 if (gSecurityd && gSecurityd->soscc_status)
1741 return (__bridge id<SOSControlProtocol>)gSecurityd->soscc_status();
1743 static SecSOSStatus *control;
1744 static dispatch_once_t onceToken;
1745 dispatch_once(&onceToken, ^{
1746 control = [[SecSOSStatus alloc] init];
1748 return control.connection.remoteObjectProxy;
1752 static id<SOSControlProtocol>
1753 SOSCCGetSynchronousStatusObject(CFErrorRef *cferror) {
1755 secdebug("circleOps", "SOS disabled for this platform");
1756 setSOSDisabledError(cferror);
1759 if (gSecurityd && gSecurityd->soscc_status)
1760 return (__bridge id<SOSControlProtocol>)gSecurityd->soscc_status();
1762 static SecSOSStatus *control;
1763 static dispatch_once_t onceToken;
1764 dispatch_once(&onceToken, ^{
1765 control = [[SecSOSStatus alloc] init];
1768 __block NSError *localError = nil;
1769 id <SOSControlProtocol> synchronousRemoteObject = [control.connection synchronousRemoteObjectProxyWithErrorHandler:^(NSError *error) {
1770 secnotice("ghostbust", "An error occurred on the xpc connection to setup the background session: %@", error );
1775 if(cferror && localError) {
1776 *cferror = (__bridge CFErrorRef)(localError);
1778 return synchronousRemoteObject;
1784 SOSCCAccountGetPublicKey(void (^reply)(BOOL trusted, NSData *data, NSError *error))
1786 CFErrorRef error = NULL;
1787 id<SOSControlProtocol> status = SOSCCGetStatusObject(&error);
1788 if (status == NULL) {
1789 reply(false, NULL, (__bridge NSError *)error);
1790 CFReleaseNull(error);
1794 [status userPublicKey:reply];
1798 SOSCCAccountGetAccountPrivateCredential(void (^complete)(NSData *data, NSError *error))
1800 CFErrorRef error = NULL;
1801 id<SOSControlProtocol> status = SOSCCGetStatusObject(&error);
1802 if (status == NULL) {
1803 complete(NULL, (__bridge NSError *)error);
1804 CFReleaseNull(error);
1808 [status validatedStashedAccountCredential:complete];
1812 SOSCCAccountGetKeyCircleGeneration(void (^reply)(NSData *data, NSError *error))
1814 SOSCCAccountGetPublicKey(^(BOOL __unused trusted, NSData *data, NSError *error){
1818 NSMutableData *digest = [NSMutableData dataWithLength:CCSHA256_OUTPUT_SIZE];
1819 ccdigest(ccsha256_di(), [data length], [data bytes], [digest mutableBytes]);
1820 reply(digest, error);
1826 SOSCCCircleHash(NSError **error)
1828 CFErrorRef cferror = NULL;
1829 id<SOSControlProtocol> status = SOSCCGetSynchronousStatusObject(&cferror);
1830 if (status == NULL) {
1832 *error = (__bridge NSError *)cferror;
1834 CFReleaseNull(cferror);
1837 __block NSString *hash = NULL;
1838 [status circleHash:^(NSString *circleHash, NSError *error) {
1847 SOSCCGhostBust(SOSAccountGhostBustingOptions options, void (^complete)(bool ghostsBusted, NSError *error)) {
1848 CFErrorRef error = NULL;
1849 id<SOSControlProtocol> status = SOSCCGetSynchronousStatusObject(&error);
1850 if (status == NULL) {
1851 complete(false, (__bridge NSError *)error);
1852 CFReleaseNull(error);
1855 secnotice("ghostbust", "API Called");
1856 [status ghostBust:options complete:^(bool ghostBusted, NSError *error) {
1857 secnotice("ghostbust", "API returned: %d", ghostBusted);
1858 complete(ghostBusted, error);
1862 void SOSCCGhostBustTriggerTimed(SOSAccountGhostBustingOptions options, void (^complete)(bool ghostsBusted, NSError *error)) {
1863 CFErrorRef error = NULL;
1864 id<SOSControlProtocol> status = SOSCCGetSynchronousStatusObject(&error);
1865 if (status == NULL) {
1866 complete(false, (__bridge NSError *)error);
1867 CFReleaseNull(error);
1870 secnotice("ghostbust", "API Trigger Timed Called");
1871 [status ghostBustTriggerTimed:options complete:^(bool ghostBusted, NSError *error) {
1872 secnotice("ghostbust", "API Trigger Timed returned: %d", ghostBusted);
1873 complete(ghostBusted, error);
1877 void SOSCCGhostBustInfo(void (^complete)(NSData *json, NSError *error)) {
1878 CFErrorRef error = NULL;
1879 id<SOSControlProtocol> status = SOSCCGetSynchronousStatusObject(&error);
1880 if (status == NULL) {
1881 complete(false, (__bridge NSError *)error);
1882 CFReleaseNull(error);
1885 secnotice("ghostbust", "API Info Called");
1886 [status ghostBustInfo:^(NSData *json, NSError *error) {
1887 secnotice("ghostbust", "API Info returned");
1888 complete(json, error);