2 #include "SOSAccountPriv.h"
3 #include "SOSAccount.h"
5 #include <Security/SecureObjectSync/SOSKVSKeys.h>
6 #include <Security/SecureObjectSync/SOSTransportCircle.h>
7 #include <Security/SecureObjectSync/SOSTransportCircleKVS.h>
8 #include <Security/SecureObjectSync/SOSTransportMessage.h>
9 #include <Security/SecureObjectSync/SOSTransportMessageIDS.h>
10 #include <Security/SecureObjectSync/SOSKVSKeys.h>
11 #include <Security/SecureObjectSync/SOSTransport.h>
12 #include <Security/SecureObjectSync/SOSTransportKeyParameter.h>
13 #include <SOSCircle/CKBridge/SOSCloudKeychainClient.h>
15 #import <Security/SecureObjectSync/SOSAccountTrust.h>
16 #import <Security/SecureObjectSync/SOSTransport.h>
17 #import <Security/SecureObjectSync/SOSTransportKeyParameter.h>
18 #import <Security/SecureObjectSync/SOSTransportMessage.h>
19 #import "Security/SecureObjectSync/SOSTransportMessageIDS.h"
20 #import <Security/SecureObjectSync/SOSTransportMessageKVS.h>
21 #include <SOSCircle/CKBridge/SOSCloudKeychainClient.h>
22 #include <Security/SecureObjectSync/SOSAccountTrustClassic+Circle.h>
24 #include <CoreFoundation/CoreFoundation.h>
26 #include <utilities/SecCFError.h>
28 // MARK: Engine Logging
29 #define LOG_ENGINE_STATE_INTERVAL 20
31 void SOSAccountConsiderLoggingEngineState(SOSAccountTransaction* txn) {
32 static int engineLogCountDown = 0;
34 if(engineLogCountDown <= 0) {
35 SOSAccount* acct = txn.account;
36 CFTypeRef engine = [acct.kvs_message_transport SOSTransportMessageGetEngine];
38 SOSEngineLogState((SOSEngineRef)engine);
39 engineLogCountDown = LOG_ENGINE_STATE_INTERVAL;
45 bool SOSAccountInflateTransports(SOSAccount* account, CFStringRef circleName, CFErrorRef *error){
48 if(account.key_transport)
49 SOSUnregisterTransportKeyParameter(account.key_transport);
50 if(account.circle_transport)
51 SOSUnregisterTransportCircle(account.circle_transport);
52 if(account.ids_message_transport)
53 SOSUnregisterTransportMessage((SOSMessage*)account.ids_message_transport);
54 if(account.kvs_message_transport)
55 SOSUnregisterTransportMessage((SOSMessage*)account.kvs_message_transport);
57 account.key_transport = [[CKKeyParameter alloc] initWithAccount:account];
58 account.circle_transport = [[SOSKVSCircleStorageTransport alloc]initWithAccount:account andCircleName:(__bridge NSString *)(circleName)];
60 require_quiet(account.key_transport, fail);
61 require_quiet(account.circle_transport, fail);
63 account.ids_message_transport = [[SOSMessageIDS alloc] initWithAccount:account andName:(__bridge NSString *)(circleName)];
64 require_quiet(account.ids_message_transport, fail);
66 account.kvs_message_transport = [[SOSMessageKVS alloc] initWithAccount:account andName:(__bridge NSString*)circleName];
67 require_quiet(account.kvs_message_transport, fail);
75 static bool SOSAccountIsThisPeerIDMe(SOSAccount* account, CFStringRef peerID) {
76 NSString* myPeerID = account.peerID;
78 return myPeerID && [myPeerID isEqualToString: (__bridge NSString*) peerID];
81 bool SOSAccountSendIKSPSyncList(SOSAccount* account, CFErrorRef *error){
83 __block CFErrorRef localError = NULL;
84 __block CFMutableArrayRef ids = NULL;
85 SOSCircleRef circle = NULL;
86 SOSFullPeerInfoRef identity = NULL;
88 if(![account.trust isInCircle:NULL])
90 SOSCreateError(kSOSErrorNoCircle, CFSTR("This device is not in circle"), NULL, &localError);
91 if(error && *error != NULL)
92 secerror("SOSAccountSendIKSPSyncList had an error: %@", *error);
95 secerror("SOSAccountSendIKSPSyncList had an error: %@", localError);
98 CFReleaseNull(localError);
103 circle = account.trust.trustedCircle;
104 identity = account.fullPeerInfo;
105 ids = CFArrayCreateMutableForCFTypes(kCFAllocatorDefault);
107 SOSCircleForEachValidPeer(circle, account.accountKey, ^(SOSPeerInfoRef peer) {
108 if (!SOSAccountIsThisPeerIDMe(account, SOSPeerInfoGetPeerID(peer))) {
109 if(SOSPeerInfoShouldUseIDSTransport(SOSFullPeerInfoGetPeerInfo(identity), peer) &&
110 SOSPeerInfoShouldUseIDSMessageFragmentation(SOSFullPeerInfoGetPeerInfo(identity), peer) &&
111 !SOSPeerInfoShouldUseACKModel(SOSFullPeerInfoGetPeerInfo(identity), peer)){
112 [account.ids_message_transport SOSTransportMessageIDSSetFragmentationPreference:account.ids_message_transport pref:kCFBooleanTrue];
113 CFStringRef deviceID = SOSPeerInfoCopyDeviceID(peer);
114 if(deviceID != NULL){
115 CFArrayAppendValue(ids, deviceID);
117 CFReleaseNull(deviceID);
121 require_quiet(CFArrayGetCount(ids) != 0, xit);
122 secnotice("IDS Transport", "List of IDS Peers to ping: %@", ids);
124 SOSCloudKeychainGetIDSDeviceAvailability(ids, (__bridge CFStringRef)(account.peerID), dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^(CFDictionaryRef returnedValues, CFErrorRef sync_error) {
125 bool success = (sync_error == NULL);
127 secerror("Failed to send list of IDS peers to IDSKSP: %@", sync_error);
130 if(error && *error != NULL)
131 secerror("SOSAccountSendIKSPSyncList had an error: %@", *error);
134 secerror("SOSAccountSendIKSPSyncList had an error: %@", localError);
137 CFReleaseNull(localError);
145 static bool SOSAccountSyncWithKVSPeers(SOSAccountTransaction* txn, CFSetRef peerIDs, CFErrorRef *error) {
146 SOSAccount* account = txn.account;
147 CFErrorRef localError = NULL;
150 require_quiet([account.trust isInCircle:error], xit);
152 result =[account.kvs_message_transport SOSTransportMessageSyncWithPeers:account.kvs_message_transport p:peerIDs err:&localError];
155 SetCloudKeychainTraceValueForKey(kCloudKeychainNumberOfTimesSyncedWithPeers, 1);
159 // Tell account to update SOSEngine with current trusted peers
160 if (isSOSErrorCoded(localError, kSOSErrorPeerNotFound)) {
161 secnotice("Account", "Arming account to update SOSEngine with current trusted peers");
162 account.engine_peer_state_needs_repair = true;
164 CFErrorPropagate(localError, error);
171 bool SOSAccountSyncWithKVSPeerWithMessage(SOSAccountTransaction* txn, CFStringRef peerid, CFDataRef message, CFErrorRef *error) {
172 SOSAccount* account = txn.account;
174 CFErrorRef localError = NULL;
175 CFDictionaryRef encapsulatedMessage = NULL;
177 secnotice("KVS Transport","Syncing with KVS capable peer: %@", peerid);
178 secnotice("KVS Transport", "message: %@", message);
180 require_quiet(message, xit);
181 require_quiet(peerid, xit);
183 encapsulatedMessage = CFDictionaryCreateForCFTypes(kCFAllocatorDefault, peerid, message, NULL);
185 result = [account.kvs_message_transport SOSTransportMessageSendMessages:account.kvs_message_transport pm:encapsulatedMessage err:&localError];
186 secerror("KVS sync %s. (%@)", result ? "succeeded" : "failed", localError);
188 SOSAccountConsiderLoggingEngineState(txn);
191 CFReleaseNull(encapsulatedMessage);
192 CFErrorPropagate(localError, error);
198 static bool SOSAccountSyncWithKVSPeer(SOSAccountTransaction* txn, CFStringRef peerID, CFErrorRef *error)
201 CFErrorRef localError = NULL;
203 secnotice("KVS Transport","Syncing with KVS capable peer: %@", peerID);
205 CFMutableSetRef peerIDs = CFSetCreateMutableForCFTypes(kCFAllocatorDefault);
206 CFSetAddValue(peerIDs, peerID);
208 result = SOSAccountSyncWithKVSPeers(txn, peerIDs, &localError);
209 secerror("KVS sync %s. (%@)", result ? "succeeded" : "failed", localError);
211 CFReleaseNull(peerIDs);
212 CFErrorPropagate(localError, error);
217 static CFMutableArrayRef SOSAccountCopyPeerIDsForDSID(SOSAccount* account, CFStringRef deviceID, CFErrorRef* error) {
218 CFMutableArrayRef peerIDs = CFArrayCreateMutableForCFTypes(kCFAllocatorDefault);
220 SOSCircleForEachValidPeer(account.trust.trustedCircle, account.accountKey, ^(SOSPeerInfoRef peer) {
221 CFStringRef peerDeviceID = SOSPeerInfoCopyDeviceID(peer);
222 if(peerDeviceID != NULL && CFStringCompare(peerDeviceID, deviceID, 0) == 0){
223 CFArrayAppendValue(peerIDs, SOSPeerInfoGetPeerID(peer));
225 CFReleaseNull(peerDeviceID);
228 if (peerIDs == NULL || CFArrayGetCount(peerIDs) == 0) {
229 CFReleaseNull(peerIDs);
230 SOSErrorCreate(kSOSErrorPeerNotFound, error, NULL, CFSTR("No peer with DSID: %@"), deviceID);
236 static bool SOSAccountSyncWithKVSPeerFromPing(SOSAccount* account, CFArrayRef peerIDs, CFErrorRef *error) {
238 CFErrorRef localError = NULL;
241 CFSetRef peerSet = CFSetCreateCopyOfArrayForCFTypes(peerIDs);
242 result = [account.kvs_message_transport SOSTransportMessageSyncWithPeers:account.kvs_message_transport p:peerSet err:&localError];
244 CFReleaseNull(peerSet);
249 bool SOSAccountSyncWithKVSUsingIDSID(SOSAccount* account, CFStringRef deviceID, CFErrorRef *error) {
251 CFErrorRef localError = NULL;
253 secnotice("KVS Transport","Syncing with KVS capable peer via DSID: %@", deviceID);
255 CFArrayRef peerIDs = SOSAccountCopyPeerIDsForDSID(account, deviceID, &localError);
256 require_quiet(peerIDs, xit);
258 CFStringArrayPerfromWithDescription(peerIDs, ^(CFStringRef peerIDList) {
259 secnotice("KVS Transport", "Syncing with KVS capable peers: %@", peerIDList);
262 result = SOSAccountSyncWithKVSPeerFromPing(account, peerIDs, &localError);
263 secerror("KVS sync %s. (%@)", result ? "succeeded" : "failed", localError);
266 CFReleaseNull(peerIDs);
267 CFErrorPropagate(localError, error);
272 CFSetRef SOSAccountSyncWithPeersOverKVS(SOSAccountTransaction* txn, CFSetRef peers) {
273 CFMutableSetRef handled = CFSetCreateMutableForCFTypes(kCFAllocatorDefault);
275 CFSetForEach(peers, ^(const void *value) {
276 CFStringRef peerID = asString(value, NULL);
277 CFErrorRef localError = NULL;
278 if (peerID && SOSAccountSyncWithKVSPeer(txn, peerID, &localError)) {
279 CFSetAddValue(handled, peerID);
280 secnotice("KVS Transport", "synced with peer: %@", peerID);
282 secnotice("KVS Transport", "failed to sync with peer: %@ error: %@", peerID, localError);
289 CF_RETURNS_RETAINED CFSetRef SOSAccountSyncWithPeersOverIDS(SOSAccountTransaction* txn, CFSetRef peers) {
290 CFErrorRef localError = NULL;
291 SOSAccount* account = txn.account;
293 CFStringSetPerformWithDescription(peers, ^(CFStringRef peerDescription) {
294 secnotice("IDS Transport","Syncing with IDS capable peers: %@", peerDescription);
297 // We should change this to return a set of peers we succeeded with, but for now assume they all worked.
298 bool result = [account.ids_message_transport SOSTransportMessageSyncWithPeers:account.ids_message_transport p:peers err:&localError];
299 secnotice("IDS Transport", "IDS Sync result: %d", result);
301 return CFSetCreateCopy(kCFAllocatorDefault, peers);
304 CF_RETURNS_RETAINED CFMutableSetRef SOSAccountSyncWithPeers(SOSAccountTransaction* txn, CFSetRef /* CFStringRef */ peerIDs, CFErrorRef *error) {
305 CFMutableSetRef notMePeers = NULL;
306 CFMutableSetRef handledPeerIDs = NULL;
307 CFMutableSetRef peersForIDS = NULL;
308 CFMutableSetRef peersForKVS = NULL;
310 SOSAccount* account = txn.account;
312 // Kick getting our device ID if we don't have it, and find out if we're setup to use IDS.
313 bool canUseIDS = [account.ids_message_transport SOSTransportMessageIDSGetIDSDeviceID:account];
315 if(![account.trust isInCircle:error])
317 handledPeerIDs = CFSetCreateMutableCopy(kCFAllocatorDefault, 0, peerIDs);
318 CFReleaseNull(notMePeers);
319 CFReleaseNull(peersForIDS);
320 CFReleaseNull(peersForKVS);
321 return handledPeerIDs;
324 handledPeerIDs = CFSetCreateMutableForCFTypes(kCFAllocatorDefault);
325 peersForIDS = CFSetCreateMutableForCFTypes(kCFAllocatorDefault);
326 peersForKVS = CFSetCreateMutableForCFTypes(kCFAllocatorDefault);
328 SOSPeerInfoRef myPeerInfo = account.peerInfo;
331 CFReleaseNull(notMePeers);
332 CFReleaseNull(peersForIDS);
333 CFReleaseNull(peersForKVS);
334 return handledPeerIDs;
337 CFStringRef myPeerID = SOSPeerInfoGetPeerID(myPeerInfo);
339 notMePeers = CFSetCreateMutableCopy(kCFAllocatorDefault, 0, peerIDs);
340 CFSetRemoveValue(notMePeers, myPeerID);
342 if(!SOSAccountSendIKSPSyncList(account, error)){
344 secnotice("IDS Transport", "Did not send list of peers to ping (pre-E): %@", *error);
347 CFSetForEach(notMePeers, ^(const void *value) {
348 CFErrorRef localError = NULL;
349 CFStringRef peerID = asString(value, &localError);
350 SOSPeerInfoRef peerInfo = NULL;
351 SOSCircleRef circle = NULL;
352 SOSAccountTrustClassic *trust = account.trust;
353 circle = trust.trustedCircle;
354 require_quiet(peerID, skip);
356 peerInfo = SOSCircleCopyPeerWithID(circle, peerID, NULL);
357 if (peerInfo && SOSCircleHasValidSyncingPeer(circle, peerInfo, account.accountKey, NULL)) {
358 if (canUseIDS && SOSPeerInfoShouldUseIDSTransport(myPeerInfo, peerInfo) && SOSPeerInfoShouldUseACKModel(myPeerInfo, peerInfo)) {
359 CFSetAddValue(peersForIDS, peerID);
361 CFSetAddValue(peersForKVS, peerID);
364 CFSetAddValue(handledPeerIDs, peerID);
368 CFReleaseNull(peerInfo);
370 secnotice("sync-with-peers", "Skipped peer ID: %@ due to %@", peerID, localError);
372 CFReleaseNull(localError);
375 CFSetRef handledIDSPeerIDs = SOSAccountSyncWithPeersOverIDS(txn, peersForIDS);
376 CFSetUnion(handledPeerIDs, handledIDSPeerIDs);
377 CFReleaseNull(handledIDSPeerIDs);
379 CFSetRef handledKVSPeerIDs = SOSAccountSyncWithPeersOverKVS(txn, peersForKVS);
380 CFSetUnion(handledPeerIDs, handledKVSPeerIDs);
381 CFReleaseNull(handledKVSPeerIDs);
383 SOSAccountConsiderLoggingEngineState(txn);
385 CFReleaseNull(notMePeers);
386 CFReleaseNull(peersForIDS);
387 CFReleaseNull(peersForKVS);
388 return handledPeerIDs;
391 bool SOSAccountClearPeerMessageKey(SOSAccountTransaction* txn, CFStringRef peerID, CFErrorRef *error)
393 SOSAccount* account = txn.account;
395 secnotice("IDS Transport", "clearing peer message for %@", peerID);
396 CFTypeRef dsid = SOSAccountGetValue(account, kSOSDSIDKey, error);
401 CFStringRef myID = (__bridge CFStringRef)(account.peerID);
402 CFStringRef message_to_peer_key = SOSMessageKeyCreateFromTransportToPeer(account.kvs_message_transport, myID, peerID);
403 CFDictionaryRef a_message_to_a_peer = CFDictionaryCreateForCFTypes(NULL, message_to_peer_key, kCFNull, kSOSKVSRequiredKey, dsid, NULL);
405 CloudKeychainReplyBlock log_error = ^(CFDictionaryRef returnedValues __unused, CFErrorRef block_error) {
407 secerror("Error putting: %@", block_error);
411 SOSCloudKeychainPutObjectsInCloud(a_message_to_a_peer, dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), log_error);
413 CFReleaseNull(a_message_to_a_peer);
414 CFReleaseNull(message_to_peer_key);
419 CF_RETURNS_RETAINED CFSetRef SOSAccountProcessSyncWithPeers(SOSAccountTransaction* txn, CFSetRef /* CFStringRef */ peers, CFSetRef /* CFStringRef */ backupPeers, CFErrorRef *error)
421 CFErrorRef localError = NULL;
422 SOSAccount* account = txn.account;
424 CFMutableSetRef handled = SOSAccountSyncWithPeers(txn, peers, &localError);
426 [account.ids_message_transport SOSTransportMessageIDSGetIDSDeviceID:account];
429 secnotice("account-sync", "Peer Sync failed: %@", localError);
430 handled = CFSetCreateMutableForCFTypes(kCFAllocatorDefault);
432 CFReleaseNull(localError);
434 CFTypeRef engine = [account.kvs_message_transport SOSTransportMessageGetEngine];
435 CFSetRef engineHandled = SOSEngineSyncWithBackupPeers((SOSEngineRef)engine, backupPeers, false, error);
438 CFSetUnion(handled, engineHandled);
440 secnotice("account-sync", "Engine Backup Sync failed: %@", localError);
442 CFReleaseNull(localError);
443 CFReleaseNull(engineHandled);
448 CF_RETURNS_RETAINED CFSetRef SOSAccountCopyBackupPeersAndForceSync(SOSAccountTransaction* txn, CFErrorRef *error)
450 SOSEngineRef engine = (SOSEngineRef) [txn.account.kvs_message_transport SOSTransportMessageGetEngine];
452 NSArray* backupPeersArray = (NSArray*) CFBridgingRelease(SOSEngineCopyBackupPeerNames(engine, error));
453 NSSet* backupPeers = [[NSSet alloc] initWithArray: backupPeersArray];
454 return SOSEngineSyncWithBackupPeers(engine, (__bridge CFSetRef) backupPeers, true, error);
457 bool SOSAccountRequestSyncWithAllPeers(SOSAccountTransaction* txn, CFErrorRef *error)
459 SOSAccount* account = txn.account;
460 SOSAccountTrustClassic *trust = account.trust;
462 if (![account.trust isInCircle:error])
465 NSMutableSet<NSString*>* allSyncingPeers = [NSMutableSet set];
466 SOSCircleRef circle = trust.trustedCircle;
468 // Tickle IDS in case we haven't even tried when we're syncing.
469 [account.ids_message_transport SOSTransportMessageIDSGetIDSDeviceID:account];
471 SOSCircleForEachValidSyncingPeer(circle, account.accountKey, ^(SOSPeerInfoRef peer) {
472 [allSyncingPeers addObject: (__bridge NSString*) SOSPeerInfoGetPeerID(peer)];
475 [txn requestSyncWithPeers:allSyncingPeers];
481 // MARK: Syncing status functions
483 bool SOSAccountMessageFromPeerIsPending(SOSAccountTransaction* txn, SOSPeerInfoRef peer, CFErrorRef *error) {
484 bool success = false;
485 SOSAccount* account = txn.account;
486 require_quiet([account.trust isInCircle:error], xit);
488 // This translation belongs inside KVS..way down in CKD, but for now we reach over and do it here.
489 CFStringRef peerMessage = SOSMessageKeyCreateFromPeerToTransport([account kvs_message_transport], (__bridge CFStringRef)(account.peerID), SOSPeerInfoGetPeerID(peer));
491 success = SOSCloudKeychainHasPendingKey(peerMessage, error);
492 CFReleaseNull(peerMessage);
498 bool SOSAccountSendToPeerIsPending(SOSAccountTransaction* txn, SOSPeerInfoRef peer, CFErrorRef *error) {
499 bool success = false;
500 SOSAccount* account = txn.account;
501 require_quiet([account.trust isInCircle:error], xit);
503 success = SOSCCIsSyncPendingFor(SOSPeerInfoGetPeerID(peer), error);