2 * Copyright (c) 2012-2014,2016 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@
29 #import <Foundation/Foundation.h>
31 #import <utilities/debugging.h>
32 #import <os/activity.h>
34 #import "CKDKVSProxy.h"
35 #import "CKDKVSStore.h"
36 #import "CKDAKSLockMonitor.h"
37 #import "CKDSecuritydAccount.h"
38 #import "NSURL+SOSPlistStore.h"
40 #include <Security/SecureObjectSync/SOSARCDefines.h>
41 #include <Security/SecureObjectSync/SOSKVSKeys.h>
42 #include <utilities/SecCFWrappers.h>
43 #include <utilities/SecPLWrappers.h>
45 #include "SOSCloudKeychainConstants.h"
47 #include <utilities/SecAKSWrappers.h>
48 #include <utilities/SecADWrapper.h>
49 #include <utilities/SecNSAdditions.h>
50 #import "XPCNotificationDispatcher.h"
53 @interface NSSet (CKDLogging)
54 - (NSString*) logKeys;
58 @implementation NSSet (CKDLogging)
59 - (NSString*) logKeys {
60 return [self sortedElementsJoinedByString:@" "];
63 - (NSString*) logIDs {
64 return [self sortedElementsTruncated:8 JoinedByString:@" "];
70 The total space available in your app’s iCloud key-value storage is 1 MB.
71 The maximum number of keys you can specify is 1024, and the size limit for
72 each value associated with a key is 1 MB. So, for example, if you store a
73 single large value of 1 MB for a single key, that consumes your total
74 available storage. If you store 1 KB of data for each key, you can use
75 1,000 key-value pairs.
78 static NSString *kKeyKeyParameterKeys = @"KeyParameterKeys";
79 static NSString *kKeyCircleKeys = @"CircleKeys";
80 static NSString *kKeyMessageKeys = @"MessageKeys";
82 static NSString *kKeyAlwaysKeys = @"AlwaysKeys";
83 static NSString *kKeyFirstUnlockKeys = @"FirstUnlockKeys";
84 static NSString *kKeyUnlockedKeys = @"UnlockedKeys";
85 static NSString *kKeyPendingKeys = @"PendingKeys";
86 static NSString *kKeyUnsentChangedKeys = @"unsentChangedKeys";
87 static NSString *kKeyUnlockNotificationRequested = @"unlockNotificationRequested";
88 static NSString *kKeySyncWithPeersPending = @"SyncWithPeersPending";
90 static NSString *kKeyPendingSyncPeerIDs = @"SyncPeerIDs";
91 static NSString *kKeyPendingSyncBackupPeerIDs = @"SyncBackupPeerIDs";
93 static NSString *kKeyEnsurePeerRegistration = @"EnsurePeerRegistration";
94 static NSString *kKeyDSID = @"DSID";
95 static NSString *kMonitorState = @"MonitorState";
96 static NSString *kKeyAccountUUID = @"MonitorState";
98 static NSString *kMonitorPenaltyBoxKey = @"Penalty";
99 static NSString *kMonitorMessageKey = @"Message";
100 static NSString *kMonitorConsecutiveWrites = @"ConsecutiveWrites";
101 static NSString *kMonitorLastWriteTimestamp = @"LastWriteTimestamp";
102 static NSString *kMonitorMessageQueue = @"MessageQueue";
103 static NSString *kMonitorPenaltyTimer = @"PenaltyTimer";
104 static NSString *kMonitorDidWriteDuringPenalty = @"DidWriteDuringPenalty";
106 static NSString *kMonitorTimeTable = @"TimeTable";
107 static NSString *kMonitorFirstMinute = @"AFirstMinute";
108 static NSString *kMonitorSecondMinute = @"BSecondMinute";
109 static NSString *kMonitorThirdMinute = @"CThirdMinute";
110 static NSString *kMonitorFourthMinute = @"DFourthMinute";
111 static NSString *kMonitorFifthMinute = @"EFifthMinute";
112 static NSString *kMonitorWroteInTimeSlice = @"TimeSlice";
114 #define kSecServerKeychainChangedNotification "com.apple.security.keychainchanged"
116 @interface UbiqitousKVSProxy ()
117 @property (nonatomic) NSDictionary* persistentData;
118 - (void) doSyncWithAllPeers;
119 - (void) persistState;
122 @implementation UbiqitousKVSProxy
124 + (instancetype)withAccount:(NSObject<CKDAccount>*) account
125 store:(NSObject<CKDStore>*) store
126 lockMonitor:(NSObject<CKDLockMonitor>*) lockMonitor
127 persistence:(NSURL*) localPersistence
129 return [[self alloc] initWithAccount:account
131 lockMonitor:lockMonitor
132 persistence:localPersistence];
135 - (instancetype)initWithAccount:(NSObject<CKDAccount>*) account
136 store:(NSObject<CKDStore>*) store
137 lockMonitor:(NSObject<CKDLockMonitor>*) lockMonitor
138 persistence:(NSURL*) localPersistence
140 if (self = [super init])
142 secnotice("event", "%@ start", self);
144 #if !(TARGET_OS_EMBEDDED)
145 // rdar://problem/26247270
146 if (geteuid() == 0) {
147 secerror("Cannot run CloudKeychainProxy as root");
151 _ensurePeerRegistration = NO;
153 _pendingSyncPeerIDs = [NSMutableSet set];
154 _pendingSyncBackupPeerIDs = [NSMutableSet set];
155 _shadowPendingSyncPeerIDs = nil;
156 _shadowPendingSyncBackupPeerIDs = nil;
158 _persistenceURL = localPersistence;
162 _lockMonitor = lockMonitor;
165 _calloutQueue = dispatch_queue_create("CKDCallout", DISPATCH_QUEUE_SERIAL);
166 _ckdkvsproxy_queue = dispatch_queue_create("CKDKVSProxy", DISPATCH_QUEUE_SERIAL);
168 _freshnessCompletions = [NSMutableArray<FreshnessResponseBlock> array];
170 _monitor = [NSMutableDictionary dictionary];
172 [[XPCNotificationDispatcher dispatcher] addListener: self];
174 int notificationToken;
175 notify_register_dispatch(kSecServerKeychainChangedNotification, ¬ificationToken, _ckdkvsproxy_queue,
176 ^ (int token __unused)
178 secinfo("backoff", "keychain changed, wiping backoff monitor state");
179 self->_monitor = [NSMutableDictionary dictionary];
182 [self setPersistentData: [self.persistenceURL readPlist]];
187 [[self store] connectToProxy: self];
188 [[self lockMonitor] connectTo:self];
190 secdebug(XPROXYSCOPE, "%@ done", self);
195 - (NSString *)description
197 return [NSString stringWithFormat:@"<%s%s%s%s%s%s%s%s%s%s>",
198 [[self lockMonitor] locked] ? "L" : "U",
199 [[self lockMonitor] unlockedSinceBoot] ? "B" : "-",
200 _seenKVSStoreChange ? "K" : "-",
201 [self hasPendingNonShadowSyncIDs] ? "s" : "-",
202 _ensurePeerRegistration ? "e" : "-",
203 [_pendingKeys count] ? "p" : "-",
204 _inCallout ? "C" : "-",
205 [self hasPendingShadowSyncIDs] ? "S" : "-",
206 _shadowEnsurePeerRegistration ? "E" : "-",
207 [_shadowPendingKeys count] ? "P" : "-"];
211 // MARK: XPC Function commands
213 - (void) clearStore {
214 [self.store removeAllObjects];
217 - (void)synchronizeStore {
218 [self.store pushWrites];
221 - (id) objectForKey: (NSString*) key {
222 return [self.store objectForKey: key];
224 - (NSDictionary<NSString *, id>*) copyAsDictionary {
225 return [self.store copyAsDictionary];
228 - (void)_queue_processAllItems
230 dispatch_assert_queue(_ckdkvsproxy_queue);
232 NSDictionary *allItems = [self.store copyAsDictionary];
235 secnotice("event", "%@ sending: %@", self, [[allItems allKeys] componentsJoinedByString: @" "]);
236 [self processKeyChangedEvent:allItems];
239 secdebug(XPROXYSCOPE, "%@ No items in KVS", self);
244 secdebug(XPROXYSCOPE, "%@", self);
245 [[NSNotificationCenter defaultCenter] removeObserver:self
246 name:NSUbiquitousKeyValueStoreDidChangeExternallyNotification
249 [[NSNotificationCenter defaultCenter] removeObserver:self
250 name:NSUbiquityIdentityDidChangeNotification
256 - (NSDictionary*) persistentData
258 return @{ kKeyAlwaysKeys:[_alwaysKeys allObjects],
259 kKeyFirstUnlockKeys:[_firstUnlockKeys allObjects],
260 kKeyUnlockedKeys:[_unlockedKeys allObjects],
261 kKeyPendingKeys:[_pendingKeys allObjects],
262 kKeyPendingSyncPeerIDs:[_pendingSyncPeerIDs allObjects],
263 kKeyPendingSyncBackupPeerIDs:[_pendingSyncBackupPeerIDs allObjects],
264 kMonitorState:_monitor,
265 kKeyEnsurePeerRegistration:[NSNumber numberWithBool:_ensurePeerRegistration],
267 kKeyAccountUUID:_accountUUID
271 - (void) setPersistentData: (NSDictionary*) interests
273 _alwaysKeys = [NSMutableSet setWithArray: interests[kKeyAlwaysKeys]];
274 _firstUnlockKeys = [NSMutableSet setWithArray: interests[kKeyFirstUnlockKeys]];
275 _unlockedKeys = [NSMutableSet setWithArray: interests[kKeyUnlockedKeys]];
277 _pendingKeys = [NSMutableSet setWithArray: interests[kKeyPendingKeys]];
279 _pendingSyncPeerIDs = [NSMutableSet setWithArray: interests[kKeyPendingSyncPeerIDs]];
280 _pendingSyncBackupPeerIDs = [NSMutableSet setWithArray: interests[kKeyPendingSyncBackupPeerIDs]];
282 _monitor = interests[kMonitorState];
284 _monitor = [NSMutableDictionary dictionary];
286 _ensurePeerRegistration = [interests[kKeyEnsurePeerRegistration] boolValue];
288 _dsid = interests[kKeyDSID];
289 _accountUUID = interests[kKeyAccountUUID];
291 // If we had a sync pending, we kick it off and migrate to sync with these peers
292 if ([interests[kKeySyncWithPeersPending] boolValue]) {
293 [self doSyncWithAllPeers];
299 NSDictionary* dataToSave = self.persistentData;
301 secdebug("persistence", "Writing registeredKeys: %@", [dataToSave compactDescription]);
302 if (![self.persistenceURL writePlist:dataToSave]) {
303 secerror("Failed to write persistence data to %@", self.persistenceURL);
307 - (void)perfCounters:(void(^)(NSDictionary *counters))callback
309 /* Collect and merge perf counters from other layers here too */
310 [self.store perfCounters:callback];
314 // MARK: Object setting
317 - (void)setStoreObjectsFromDictionary:(NSDictionary *)values
320 secdebug(XPROXYSCOPE, "%@ NULL? values: %@", self, values);
324 NSMutableDictionary<NSString*, NSObject*> *mutableValues = [values mutableCopy];
325 NSString* newDSID = asNSString([mutableValues extractObjectForKey:(__bridge NSString*) kSOSKVSOfficialDSIDKey]);
330 NSString* requiredDSID = asNSString([mutableValues extractObjectForKey:(__bridge NSString*) kSOSKVSRequiredKey]);
332 if (_dsid == nil || [_dsid isEqualToString: @""]) {
333 secdebug("dsid", "CloudKeychainProxy setting dsid to :%@ from securityd", requiredDSID);
334 _dsid = requiredDSID;
335 } else if (![_dsid isEqual: requiredDSID]) {
336 secerror("Account DSIDs do not match, cloud keychain proxy: %@, securityd: %@", _dsid, requiredDSID);
337 secerror("Not going to write these: %@ into KVS!", values);
340 secnoticeq("dsid", "DSIDs match, writing");
344 secnoticeq("keytrace", "%@ sending: %@", self, [[mutableValues allKeys] componentsJoinedByString: @" "]);
345 [mutableValues enumerateKeysAndObjectsUsingBlock: ^(id key, id obj, BOOL *stop)
347 if (obj == NULL || obj == [NSNull null]) {
348 [self.store removeObjectForKey:key];
350 if ([key hasPrefix:@"ak|"]) { // TODO: somewhat of a hack
351 id oldObj = [self.store objectForKey:key];
352 if ([oldObj isEqual: obj]) {
353 // Fix KVS repeated message undelivery by sending a NULL first (deafness)
354 secnoticeq("keytrace", "forcing resend of key write: %@", key);
355 [self.store removeObjectForKey:key];
358 [[self store] addOneToOutGoing];
359 [self.store setObject:obj forKey:key];
363 [self.store pushWrites];
366 - (void)setObjectsFromDictionary:(NSDictionary<NSString*, NSObject*> *)values
368 [self setStoreObjectsFromDictionary:values];
371 - (void)waitForSynchronization:(void (^)(NSDictionary<NSString*, NSObject*> *results, NSError *err))handler
373 secnoticeq("fresh", "%s Requesting WFS", kWAIT2MINID);
375 [_freshnessCompletions addObject: ^(bool success, NSError *error){
376 secnoticeq("fresh", "%s WFS Done", kWAIT2MINID);
380 if ([self.freshnessCompletions count] == 1) {
381 // We can't talk to synchronize on the _ckdkvsproxy_queue or we deadlock,
382 // bounce to a global concurrent queue
383 dispatch_after(_nextFreshnessTime, dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
384 NSError *error = nil;
385 bool success = [self.store pullUpdates:&error];
387 dispatch_async(self->_ckdkvsproxy_queue, ^{
388 [self waitForSyncDone: success error: error];
394 - (void) waitForSyncDone: (bool) success error: (NSError*) error{
396 const uint64_t delayBeforeCallingAgainInSeconds = 5ull * NSEC_PER_SEC;
397 _nextFreshnessTime = dispatch_time(DISPATCH_TIME_NOW, delayBeforeCallingAgainInSeconds);
400 secnoticeq("fresh", "%s Completing WFS", kWAIT2MINID);
401 [_freshnessCompletions enumerateObjectsUsingBlock:^(FreshnessResponseBlock _Nonnull block,
403 BOOL * _Nonnull stop) {
404 block(success, error);
406 [_freshnessCompletions removeAllObjects];
411 // MARK: ----- KVS key lists -----
414 - (NSMutableSet *)copyAllKeyInterests
416 NSMutableSet *allKeys = [NSMutableSet setWithSet: _alwaysKeys];
417 [allKeys unionSet: _firstUnlockKeys];
418 [allKeys unionSet: _unlockedKeys];
422 -(void)registerAtTimeKeys:(NSDictionary*)keyparms
427 NSArray *alwaysArray = [keyparms valueForKey: kKeyAlwaysKeys];
428 NSArray *firstUnlockedKeysArray = [keyparms valueForKey: kKeyFirstUnlockKeys];
429 NSArray *whenUnlockedKeysArray = [keyparms valueForKey: kKeyUnlockedKeys];
432 [_alwaysKeys unionSet: [NSMutableSet setWithArray: alwaysArray]];
433 if(firstUnlockedKeysArray)
434 [_firstUnlockKeys unionSet: [NSMutableSet setWithArray: firstUnlockedKeysArray]];
435 if(whenUnlockedKeysArray)
436 [_unlockedKeys unionSet: [NSMutableSet setWithArray: whenUnlockedKeysArray]];
439 - (void)removeKeys: (NSArray*)keys forAccount: (NSString*) accountUUID
441 secdebug(XPROXYSCOPE, "removeKeys: keys: %@", keys);
443 // We only reset when we know the ID and they send the ID and it changes.
444 bool newAccount = accountUUID != nil && self.accountUUID != nil && ![accountUUID isEqualToString: self.accountUUID];
447 secnotice(XPROXYSCOPE, "not removing keys, account UUID is for a new account");
450 [keys enumerateObjectsUsingBlock:^(NSString* _Nonnull key, NSUInteger idx, BOOL * _Nonnull stop) {
451 secnotice(XPROXYSCOPE, "removing from KVS store: %@", key);
452 [self.store removeObjectForKey:key];
456 - (void)registerKeys: (NSDictionary*)keys forAccount: (NSString*) accountUUID
458 secdebug(XPROXYSCOPE, "registerKeys: keys: %@", keys);
460 // We only reset when we know the ID and they send the ID and it changes.
461 bool newAccount = accountUUID != nil && self.accountUUID != nil && ![accountUUID isEqualToString: self.accountUUID];
464 self.accountUUID = accountUUID;
467 // If we're a new account we don't exclude the old keys
468 NSMutableSet *allOldKeys = newAccount ? [NSMutableSet set] : [self copyAllKeyInterests];
471 NSDictionary *keyparms = [keys valueForKey: [NSString stringWithUTF8String: kMessageKeyParameter]];
472 NSDictionary *circles = [keys valueForKey: [NSString stringWithUTF8String: kMessageCircle]];
473 NSDictionary *messages = [keys valueForKey: [NSString stringWithUTF8String: kMessageMessage]];
475 _alwaysKeys = [NSMutableSet set];
476 _firstUnlockKeys = [NSMutableSet set];
477 _unlockedKeys = [NSMutableSet set];
479 [self registerAtTimeKeys: keyparms];
480 [self registerAtTimeKeys: circles];
481 [self registerAtTimeKeys: messages];
483 NSMutableSet *allNewKeys = [self copyAllKeyInterests];
485 // Make sure keys we no longer care about are not pending
486 [_pendingKeys intersectSet:allNewKeys];
487 if (_shadowPendingKeys) {
488 [_shadowPendingKeys intersectSet:allNewKeys];
491 // All new keys only is new keys (remove old keys)
492 [allNewKeys minusSet:allOldKeys];
494 // Mark new keys pending, they're new!
495 NSMutableSet *newKeysForCurrentLockState = [self pendKeysAndGetNewlyPended:allNewKeys];
497 [self persistState]; // Before we might call out, save our state so we recover if we crash
499 [self intersectWithCurrentLockState: newKeysForCurrentLockState];
500 // TODO: Don't processPendingKeysForCurrentLockState if none of the new keys have values.
501 if ([newKeysForCurrentLockState count] != 0) {
502 [self processPendingKeysForCurrentLockState];
506 // MARK: ----- Event Handling -----
508 - (void)_queue_handleNotification:(const char *) name
510 dispatch_assert_queue(_ckdkvsproxy_queue);
512 if (strcmp(name, kNotifyTokenForceUpdate)==0) {
513 // DEBUG -- Possibly remove in future
514 [self _queue_processAllItems];
515 } else if (strcmp(name, kCloudKeychainStorechangeChangeNotification)==0) {
516 // DEBUG -- Possibly remove in future
517 [self _queue_kvsStoreChange];
521 - (void)_queue_storeKeysChanged: (NSSet<NSString*>*) changedKeys initial: (bool) initial
523 dispatch_assert_queue(_ckdkvsproxy_queue);
525 // Mark that our store is talking to us, so we don't have to make up for missing anything previous.
526 _seenKVSStoreChange = YES;
528 // Unmark them as pending as they have just changed and we'll process them.
529 [_pendingKeys minusSet:changedKeys];
531 // Only send values that we're currently interested in.
532 NSSet *keysOfInterestThatChanged = [self pendKeysAndGetPendingForCurrentLockState:changedKeys];
533 NSMutableDictionary *changedValues = [self copyValues:keysOfInterestThatChanged];
535 changedValues[(__bridge NSString*)kSOSKVSInitialSyncKey] = @"true";
537 secnotice("event", "%@ keysChangedInCloud: %@ keysOfInterest: %@ initial: %@",
539 [[changedKeys allObjects] componentsJoinedByString: @" "],
540 [[changedValues allKeys] componentsJoinedByString: @" "],
541 initial ? @"YES" : @"NO");
543 if ([changedValues count])
544 [self processKeyChangedEvent:changedValues];
547 - (void)_queue_storeAccountChanged
549 dispatch_assert_queue(_ckdkvsproxy_queue);
551 secnotice("event", "%@", self);
553 NSDictionary *changedValues = nil;
555 changedValues = @{ (__bridge NSString*)kSOSKVSAccountChangedKey: _dsid };
557 changedValues = @{ (__bridge NSString*)kSOSKVSAccountChangedKey: @"true" };
559 [self processKeyChangedEvent:changedValues];
562 - (void) doAfterFlush: (dispatch_block_t) block
564 //Flush any pending communication to Securityd.
566 dispatch_async(_calloutQueue, block);
568 _shadowFlushBlock = block;
571 - (void) calloutWith: (void(^)(NSSet *pending, NSSet* pendingSyncIDs, NSSet* pendingBackupSyncIDs, bool ensurePeerRegistration, dispatch_queue_t queue, void(^done)(NSSet *handledKeys, NSSet *handledSyncs, bool handledEnsurePeerRegistration, NSError* error))) callout
573 // In CKDKVSProxy's serial queue
575 // dispatch_get_global_queue - well-known global concurrent queue
576 // dispatch_get_main_queue - default queue that is bound to the main thread
577 xpc_transaction_begin();
578 dispatch_async(_calloutQueue, ^{
579 __block NSSet *myPending;
580 __block NSSet *mySyncPeerIDs;
581 __block NSSet *mySyncBackupPeerIDs;
582 __block bool myEnsurePeerRegistration;
583 __block bool wasLocked;
584 dispatch_sync(self->_ckdkvsproxy_queue, ^{
585 myPending = [self->_pendingKeys copy];
586 mySyncPeerIDs = [self->_pendingSyncPeerIDs copy];
587 mySyncBackupPeerIDs = [self->_pendingSyncBackupPeerIDs copy];
589 myEnsurePeerRegistration = self->_ensurePeerRegistration;
590 wasLocked = [self.lockMonitor locked];
592 self->_inCallout = YES;
594 self->_shadowPendingKeys = [NSMutableSet set];
595 self->_shadowPendingSyncPeerIDs = [NSMutableSet set];
596 self->_shadowPendingSyncBackupPeerIDs = [NSMutableSet set];
599 callout(myPending, mySyncPeerIDs, mySyncBackupPeerIDs, myEnsurePeerRegistration, self->_ckdkvsproxy_queue, ^(NSSet *handledKeys, NSSet *handledSyncs, bool handledEnsurePeerRegistration, NSError* failure) {
600 secdebug("event", "%@ %s%s before callout handled: %s%s", self,
601 ![mySyncPeerIDs isEmpty] || ![mySyncBackupPeerIDs isEmpty] ? "S" : "s",
602 myEnsurePeerRegistration ? "E" : "e",
603 ![handledKeys isEmpty] ? "S" : "s",
604 handledEnsurePeerRegistration ? "E" : "e");
606 // In CKDKVSProxy's serial queue
607 self->_inCallout = NO;
609 // Update ensurePeerRegistration
610 self->_ensurePeerRegistration = ((self->_ensurePeerRegistration && !handledEnsurePeerRegistration) || self->_shadowEnsurePeerRegistration);
612 self->_shadowEnsurePeerRegistration = NO;
614 [self handlePendingEnsurePeerRegistrationRequests:true];
616 bool hadShadowPeerIDs = ![self->_shadowPendingSyncPeerIDs isEmpty] || ![self->_shadowPendingSyncBackupPeerIDs isEmpty];
618 // Update SyncWithPeers stuff.
620 [self->_pendingSyncPeerIDs minusSet: handledSyncs];
621 [self->_pendingSyncBackupPeerIDs minusSet: handledSyncs];
623 if (![handledSyncs isEmpty]) {
624 secnotice("sync-ids", "handled syncIDs: %@", [handledSyncs logIDs]);
625 secnotice("sync-ids", "remaining peerIDs: %@", [self->_pendingSyncPeerIDs logIDs]);
626 secnotice("sync-ids", "remaining backupIDs: %@", [self->_pendingSyncBackupPeerIDs logIDs]);
628 if (hadShadowPeerIDs) {
629 secnotice("sync-ids", "signaled peerIDs: %@", [self->_shadowPendingSyncPeerIDs logIDs]);
630 secnotice("sync-ids", "signaled backupIDs: %@", [self->_shadowPendingSyncBackupPeerIDs logIDs]);
634 self->_shadowPendingSyncPeerIDs = nil;
635 self->_shadowPendingSyncBackupPeerIDs = nil;
639 // Update pendingKeys and handle them
640 [self->_pendingKeys removeObject: [NSNull null]]; // Don't let NULL hang around
642 [self->_pendingKeys minusSet: handledKeys];
643 bool hadShadowPendingKeys = [self->_shadowPendingKeys count];
644 // Move away shadownPendingKeys first, because pendKeysAndGetPendingForCurrentLockState
645 // will look at them. See rdar://problem/20733166.
646 NSSet *oldShadowPendingKeys = self->_shadowPendingKeys;
647 self->_shadowPendingKeys = nil;
649 NSSet *filteredKeys = [self pendKeysAndGetPendingForCurrentLockState:oldShadowPendingKeys];
651 secnoticeq("keytrace", "%@ account handled: %@ pending: %@", self,
652 [[handledKeys allObjects] componentsJoinedByString: @" "],
653 [[filteredKeys allObjects] componentsJoinedByString: @" "]);
655 // Write state to disk
658 // Handle shadow pended stuff
660 // We only kick off another sync if we got new stuff during handling
661 if (hadShadowPeerIDs && ![self.lockMonitor locked]) {
662 secnotice("event", "%@ syncWithPeersPending: %d inCallout: %d isLocked: %d", self, [self hasPendingSyncIDs], self->_inCallout, [self.lockMonitor locked]);
663 if ([self hasPendingSyncIDs] && !self->_inCallout && ![self.lockMonitor locked]){
664 [self doSyncWithPendingPeers];
668 /* We don't want to call processKeyChangedEvent if we failed to
669 handle pending keys and the device didn't unlock nor receive
670 any kvs changes while we were in our callout.
671 Doing so will lead to securityd and CloudKeychainProxy
672 talking to each other forever in a tight loop if securityd
673 repeatedly returns an error processing the same message.
674 Instead we leave any old pending keys until the next event. */
675 if (hadShadowPendingKeys || (![self.lockMonitor locked] && wasLocked)){
676 [self processKeyChangedEvent:[self copyValues:filteredKeys]];
677 if(self->_shadowFlushBlock != NULL)
678 secerror("Flush block is not null and sending new keys");
681 if(self->_shadowFlushBlock != NULL){
682 dispatch_async(self->_calloutQueue, self->_shadowFlushBlock);
683 self->_shadowFlushBlock = NULL;
687 [self.lockMonitor recheck];
690 xpc_transaction_end();
695 - (void) sendKeysCallout: (NSSet *(^)(NSSet* pending, NSError** error)) handleKeys {
696 [self calloutWith: ^(NSSet *pending, NSSet* pendingSyncIDs, NSSet* pendingBackupSyncIDs, bool ensurePeerRegistration, dispatch_queue_t queue, void(^done)(NSSet *handledKeys, NSSet *handledSyncs, bool handledEnsurePeerRegistration, NSError* error)) {
697 NSError* error = NULL;
699 secnotice("CloudKeychainProxy", "send keys: %@", pending);
700 NSSet * handled = handleKeys(pending, &error);
702 dispatch_async(queue, ^{
704 secerror("%@ ensurePeerRegistration failed: %@", self, error);
707 done(handled, nil, NO, error);
712 - (void)handlePendingEnsurePeerRegistrationRequests:(bool)onlyIfUnlocked
714 // doEnsurePeerRegistration's callback will be run on _calloutQueue, so we should check the 'are we running yet' flags on that queue
715 dispatch_async(_calloutQueue, ^{
716 if(self.ensurePeerRegistration && (!onlyIfUnlocked || ![self.lockMonitor locked])) {
717 if(self.ensurePeerRegistrationEnqueuedButNotStarted) {
718 secnotice("EnsurePeerRegistration", "%@ ensurePeerRegistration block already enqueued, not starting a new one", self);
722 [self doEnsurePeerRegistration];
727 - (void) doEnsurePeerRegistration
729 NSObject<CKDAccount>* accountDelegate = [self account];
730 self.ensurePeerRegistrationEnqueuedButNotStarted = true;
731 [self calloutWith:^(NSSet *pending, NSSet* pendingSyncIDs, NSSet* pendingBackupSyncIDs, bool ensurePeerRegistration, dispatch_queue_t queue, void(^done)(NSSet *handledKeys, NSSet *handledSyncs, bool handledEnsurePeerRegistration, NSError* error)) {
732 NSError* error = nil;
733 self.ensurePeerRegistrationEnqueuedButNotStarted = false;
734 bool handledEnsurePeerRegistration = [accountDelegate ensurePeerRegistration:&error];
735 secnotice("EnsurePeerRegistration", "%@ ensurePeerRegistration called, %@ (%@)", self, handledEnsurePeerRegistration ? @"success" : @"failure", error);
736 if (!handledEnsurePeerRegistration) {
737 [self.lockMonitor recheck];
738 handledEnsurePeerRegistration = ![self.lockMonitor locked]; // If we're unlocked we handled it, if we're locked we didn't.
739 // This means we get to fail once per unlock and then cut that spinning out.
741 dispatch_async(queue, ^{
742 done(nil, nil, handledEnsurePeerRegistration, error);
747 - (void) doSyncWithPendingPeers
749 NSObject<CKDAccount>* accountDelegate = [self account];
750 [self calloutWith:^(NSSet *pending, NSSet* pendingSyncIDs, NSSet* pendingBackupSyncIDs, bool ensurePeerRegistration, dispatch_queue_t queue, void(^done)(NSSet *handledKeys, NSSet *handledSyncs, bool handledEnsurePeerRegistration, NSError* error)) {
751 NSError* error = NULL;
752 secnotice("syncwith", "%@ syncwith peers: %@", self, [[pendingSyncIDs allObjects] componentsJoinedByString:@" "]);
753 secnotice("syncwith", "%@ syncwith backups: %@", self, [[pendingBackupSyncIDs allObjects] componentsJoinedByString:@" "]);
754 NSSet<NSString*>* handled = [accountDelegate syncWithPeers:pendingSyncIDs backups:pendingBackupSyncIDs error:&error];
755 secnotice("syncwith", "%@ syncwith handled: %@", self, [[handled allObjects] componentsJoinedByString:@" "]);
756 dispatch_async(queue, ^{
758 // We might be confused about lock state
759 [self.lockMonitor recheck];
762 done(nil, handled, false, error);
767 - (void) doSyncWithAllPeers
769 NSObject<CKDAccount>* accountDelegate = [self account];
770 [self calloutWith:^(NSSet *pending, NSSet* pendingSyncIDs, NSSet* pendingBackupSyncIDs, bool ensurePeerRegistration, dispatch_queue_t queue, void(^done)(NSSet *handledKeys, NSSet *handledSyncs, bool handledEnsurePeerRegistration, NSError*error)) {
771 NSError* error = NULL;
772 bool handled = [accountDelegate syncWithAllPeers:&error];
774 secerror("Failed to syncWithAllPeers: %@", error);
776 dispatch_async(queue, ^{
777 done(nil, nil, false, error);
782 - (bool)hasPendingNonShadowSyncIDs {
783 return ![_pendingSyncPeerIDs isEmpty] || ![_pendingSyncBackupPeerIDs isEmpty];
786 - (bool)hasPendingShadowSyncIDs {
787 return (_shadowPendingSyncPeerIDs && ![_shadowPendingSyncPeerIDs isEmpty]) ||
788 (_shadowPendingSyncBackupPeerIDs && ![_shadowPendingSyncBackupPeerIDs isEmpty]);
791 - (bool)hasPendingSyncIDs
793 bool pendingIDs = [self hasPendingNonShadowSyncIDs];
796 pendingIDs |= [self hasPendingShadowSyncIDs];
802 - (void)requestSyncWithPeerIDs: (NSArray<NSString*>*) peerIDs backupPeerIDs: (NSArray<NSString*>*) backupPeerIDs
804 if ([peerIDs count] == 0 && [backupPeerIDs count] == 0)
805 return; // Nothing to do;
807 NSSet<NSString*>* peerIDsSet = [NSSet setWithArray: peerIDs];
808 NSSet<NSString*>* backupPeerIDsSet = [NSSet setWithArray: backupPeerIDs];
810 [_pendingSyncPeerIDs unionSet: peerIDsSet];
811 [_pendingSyncBackupPeerIDs unionSet: backupPeerIDsSet];
814 [_shadowPendingSyncPeerIDs unionSet: peerIDsSet];
815 [_shadowPendingSyncBackupPeerIDs unionSet: backupPeerIDsSet];
820 [self handlePendingEnsurePeerRegistrationRequests:true];
822 if ([self hasPendingSyncIDs] && !_inCallout && ![self.lockMonitor locked]){
823 [self doSyncWithPendingPeers];
827 - (BOOL)hasSyncPendingFor: (NSString*) peerID {
828 return [_pendingSyncPeerIDs containsObject: peerID] ||
829 (_shadowPendingSyncPeerIDs && [_shadowPendingSyncPeerIDs containsObject: peerID]);
832 - (BOOL)hasPendingKey: (NSString*) keyName {
833 return [self.pendingKeys containsObject: keyName]
834 || (_shadowPendingKeys && [self.shadowPendingKeys containsObject: keyName]);
837 - (void)requestEnsurePeerRegistration
840 NSString *desc = [self description];
844 _shadowEnsurePeerRegistration = YES;
846 _ensurePeerRegistration = YES;
847 [self handlePendingEnsurePeerRegistrationRequests:true];
851 secdebug("event", "%@ %@", desc, self);
854 - (void)_queue_locked
856 dispatch_assert_queue(_ckdkvsproxy_queue);
858 secnotice("event", "%@ Locked", self);
861 - (void)_queue_unlocked
863 dispatch_assert_queue(_ckdkvsproxy_queue);
865 secnotice("event", "%@ Unlocked", self);
866 [self handlePendingEnsurePeerRegistrationRequests:false];
868 // First send changed keys to securityd so it can proccess updates
869 [self processPendingKeysForCurrentLockState];
871 // Then, tickle securityd to perform a sync if needed.
872 if ([self hasPendingSyncIDs]) {
873 [self doSyncWithPendingPeers];
877 - (void) _queue_kvsStoreChange {
878 dispatch_assert_queue(_ckdkvsproxy_queue);
880 os_activity_initiate("kvsStoreChange", OS_ACTIVITY_FLAG_DEFAULT, ^{
881 if (!self->_seenKVSStoreChange) {
882 secnotice("event", "%@ received darwin notification before first NSNotification", self);
883 // TODO This might not be needed if we always get the NSNotification
884 // deleived even if we were launched due to a kvsStoreChange
885 // Send all keys for current lock state to securityd so it can proccess them
886 [self pendKeysAndGetNewlyPended: [self copyAllKeyInterests]];
887 [self processPendingKeysForCurrentLockState];
889 secdebug("event", "%@ ignored, waiting for NSNotification", self);
895 #pragma mark XPCNotificationListener
897 - (void)handleNotification:(const char *) name
899 // sync because we cannot ensure the lifetime of name
900 dispatch_sync(_ckdkvsproxy_queue, ^{
901 [self _queue_handleNotification:name];
906 #pragma mark Calls from -[CKDKVSStore kvsStoreChanged:]
908 - (void)storeKeysChanged: (NSSet<NSString*>*) changedKeys initial: (bool) initial
910 // sync, caller must wait to ensure correct state
911 dispatch_sync(_ckdkvsproxy_queue, ^{
912 [self _queue_storeKeysChanged:changedKeys initial:initial];
916 - (void)storeAccountChanged
918 // sync, caller must wait to ensure correct state
919 dispatch_sync(_ckdkvsproxy_queue, ^{
920 [self _queue_storeAccountChanged];
925 #pragma mark CKDLockListener
929 // sync, otherwise tests fail
930 dispatch_sync(_ckdkvsproxy_queue, ^{
931 [self _queue_locked];
937 // sync, otherwise tests fail
938 dispatch_sync(_ckdkvsproxy_queue, ^{
939 [self _queue_unlocked];
944 // MARK: ----- Key Filtering -----
947 - (NSSet*) keysForCurrentLockState
949 secdebug("filtering", "%@ Filtering: unlockedSinceBoot: %d\n unlocked: %d\n, keysOfInterest: <%@>", self, (int) [self.lockMonitor unlockedSinceBoot], (int) ![self.lockMonitor locked], [self.persistentData compactDescription]);
951 NSMutableSet *currentStateKeys = [NSMutableSet setWithSet: _alwaysKeys];
952 if ([self.lockMonitor unlockedSinceBoot])
953 [currentStateKeys unionSet: _firstUnlockKeys];
955 if (![self.lockMonitor locked])
956 [currentStateKeys unionSet: _unlockedKeys];
958 return currentStateKeys;
962 - (NSMutableSet*) pendKeysAndGetNewlyPended: (NSSet*) keysToPend
964 NSMutableSet *filteredKeysToPend = [self copyAllKeyInterests];
965 [filteredKeysToPend intersectSet: keysToPend];
967 NSMutableSet *newlyPendedKeys = [filteredKeysToPend mutableCopy];
968 [newlyPendedKeys minusSet: _pendingKeys];
969 if (_shadowPendingKeys) {
970 [newlyPendedKeys minusSet: _shadowPendingKeys];
973 if (_shadowPendingKeys) {
974 [_shadowPendingKeys unionSet:filteredKeysToPend];
977 [_pendingKeys unionSet:filteredKeysToPend];
980 return newlyPendedKeys;
983 - (void) intersectWithCurrentLockState: (NSMutableSet*) set
985 [set intersectSet: [self keysForCurrentLockState]];
988 - (NSMutableSet*) pendingKeysForCurrentLockState
990 NSMutableSet * result = [_pendingKeys mutableCopy];
991 [self intersectWithCurrentLockState:result];
995 - (NSMutableSet*) pendKeysAndGetPendingForCurrentLockState: (NSSet*) startingSet
997 [self pendKeysAndGetNewlyPended: startingSet];
999 return [self pendingKeysForCurrentLockState];
1002 - (NSMutableDictionary *)copyValues:(NSSet*)keysOfInterest
1004 // Grab values from store.
1005 NSObject<CKDStore> *store = [self store];
1006 NSMutableDictionary *changedValues = [NSMutableDictionary dictionaryWithCapacity:0];
1007 [keysOfInterest enumerateObjectsUsingBlock:^(id obj, BOOL *stop)
1009 NSString* key = (NSString*) obj;
1010 id objval = [store objectForKey:key];
1011 if (!objval) objval = [NSNull null];
1013 [changedValues setObject:objval forKey:key];
1014 secdebug(XPROXYSCOPE, "%@ storeChanged updated value for %@", self, key);
1016 return changedValues;
1020 During RegisterKeys, separate keys-of-interest into three disjoint sets:
1021 - keys that we always want to be notified about; this means we can get the
1023 - keys that require the device to have been unlocked at least once
1024 - keys that require the device to be unlocked now
1026 Typically, the sets of keys will be:
1032 The caller is responsible for making sure that the keys in e.g. alwaysKeys are
1033 values that can be handled at any time (that is, not when unlocked)
1035 Each time we get a notification from ubiquity that keys have changed, we need to
1036 see if anything of interest changed. If we don't care, then done.
1038 For each key-of-interest that changed, we either notify the client that things
1039 changed, or add it to a pendingNotifications list. If the notification to the
1040 client fails, also add it to the pendingNotifications list. This pending list
1041 should be written to persistent storage and consulted any time we either get an
1042 item changed notification, or get a stream event signalling a change in lock state.
1044 We can notify the client either through XPC if a connection is set up, or call a
1045 routine in securityd to launch it.
1049 - (void)processKeyChangedEvent:(NSDictionary *)changedValues
1051 NSMutableDictionary* filtered = [NSMutableDictionary dictionary];
1053 secnotice("processKeyChangedEvent", "changedValues:%@", changedValues);
1054 NSMutableArray* nullKeys = [NSMutableArray array];
1055 // Remove nulls because we don't want them in securityd.
1056 [changedValues enumerateKeysAndObjectsUsingBlock:^(id key, id obj, BOOL *stop) {
1057 if (obj == [NSNull null]){
1058 [nullKeys addObject:key];
1060 filtered[key] = obj;
1063 if ([nullKeys count])
1064 [_pendingKeys minusSet: [NSSet setWithArray: nullKeys]];
1066 if([filtered count] != 0 ) {
1067 [self sendKeysCallout:^NSSet *(NSSet *pending, NSError** error) {
1068 secnotice("processing keys", "pending:%@", pending);
1069 NSError *updateError = nil;
1070 return [[self account] keysChanged: filtered error: &updateError];
1073 secnoticeq("keytrace", "%@ null: %@ pending: %@", self,
1074 [nullKeys componentsJoinedByString: @" "],
1075 [[_pendingKeys allObjects] componentsJoinedByString: @" "]);
1079 - (void) processPendingKeysForCurrentLockState
1081 [self processKeyChangedEvent: [self copyValues: [self pendingKeysForCurrentLockState]]];