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@
30 #import <Foundation/NSArray.h>
31 #import <Foundation/Foundation.h>
33 #import <Security/SecBasePriv.h>
34 #import <Security/SecItemPriv.h>
35 #import <utilities/debugging.h>
38 #include <Security/CKBridge/SOSCloudKeychainConstants.h>
39 #include <Security/SecureObjectSync/SOSARCDefines.h>
40 #include <Security/SecureObjectSync/SOSCloudCircle.h>
41 #include <Security/SecureObjectSync/SOSCloudCircleInternal.h>
44 #import <os/activity.h>
46 #include <utilities/SecAKSWrappers.h>
47 #include <utilities/SecCFWrappers.h>
48 #include <utilities/SecCFRelease.h>
49 #include <AssertMacros.h>
52 #import "IDSKeychainSyncingProxy+IDSProxyReceiveMessage.h"
53 #import "IDSKeychainSyncingProxy+IDSProxySendMessage.h"
54 #import "IDSKeychainSyncingProxy+IDSProxyThrottle.h"
55 #import "IDSPersistentState.h"
57 #define kSecServerKeychainChangedNotification "com.apple.security.keychainchanged"
58 #define kSecServerPeerInfoAvailable "com.apple.security.fpiAvailable"
60 #define IDSServiceNameKeychainSync "com.apple.private.alloy.keychainsync"
61 static NSString *kMonitorState = @"MonitorState";
62 static NSString *kExportUnhandledMessages = @"UnhandledMessages";
63 static const char *kStreamName = "com.apple.notifyd.matching";
65 NSString *const IDSSendMessageOptionForceEncryptionOffKey = @"IDSSendMessageOptionForceEncryptionOff";
66 static const int64_t kRetryTimerLeeway = (NSEC_PER_MSEC * 250); // 250ms leeway for handling unhandled messages.
67 static const int64_t kMinMessageRetryDelay = (NSEC_PER_SEC * 8);
69 CFStringRef kSOSErrorDomain = CFSTR("com.apple.security.sos.error");
71 CFIndex kSOSErrorPeerNotFound = 1032;
72 CFIndex SECD_RUN_AS_ROOT_ERROR = 1041;
74 #define IDSPROXYSCOPE "IDSProxy"
76 @implementation IDSKeychainSyncingProxy
78 + (IDSKeychainSyncingProxy *) idsProxy
80 static IDSKeychainSyncingProxy *idsProxy;
82 static dispatch_once_t onceToken;
83 dispatch_once(&onceToken, ^{
84 idsProxy = [[self alloc] init];
90 -(NSDictionary*) exportState
92 return @{ kMonitorState:_monitor,
93 kExportUnhandledMessages:_unhandledMessageBuffer
99 if([_unhandledMessageBuffer count] > 0){
100 [IDSKeychainSyncingProxyPersistentState setUnhandledMessages:[self exportState]];
104 - (void) importIDSState: (NSMutableDictionary*) state
106 _unhandledMessageBuffer = state[kExportUnhandledMessages];
107 if(!_unhandledMessageBuffer)
108 _unhandledMessageBuffer = [NSMutableDictionary dictionary];
110 _monitor = state[kMonitorState];
112 _monitor = [NSMutableDictionary dictionary];
117 if (self = [super init])
119 secnotice("event", "%@ start", self);
121 _isIDSInitDone = false;
123 _calloutQueue = dispatch_queue_create("IDSCallout", DISPATCH_QUEUE_SERIAL);
124 _unhandledMessageBuffer = [ [NSMutableDictionary alloc] initWithCapacity: 0];
125 _pingTimers = [ [NSMutableDictionary alloc] initWithCapacity: 0];
127 _isSecDRunningAsRoot = false;
128 _doesSecDHavePeer = true;
130 secdebug(IDSPROXYSCOPE, "%@ done", self);
132 [self doIDSInitialization];
134 [self doSetIDSDeviceID];
137 // Register for lock state changes
138 xpc_set_event_stream_handler(kStreamName, dispatch_get_main_queue(),
139 ^(xpc_object_t notification){
140 [self streamEvent:notification];
143 _retryTimer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, dispatch_get_main_queue());
144 dispatch_source_set_timer(_retryTimer, DISPATCH_TIME_FOREVER, DISPATCH_TIME_FOREVER, kRetryTimerLeeway);
145 dispatch_source_set_event_handler(_retryTimer, ^{
148 dispatch_resume(_retryTimer);
149 [self importIDSState: [IDSKeychainSyncingProxyPersistentState idsState]];
151 int notificationToken;
152 notify_register_dispatch(kSecServerKeychainChangedNotification, ¬ificationToken, dispatch_get_main_queue(),
153 ^ (int token __unused)
155 secinfo("backoff", "keychain changed, wiping backoff monitor state");
156 _monitor = [NSMutableDictionary dictionary];
159 notify_register_dispatch(kSecServerPeerInfoAvailable, &peerInfo, dispatch_get_main_queue(),
160 ^ (int token __unused)
162 secinfo("IDS Transport", "secd has a peer info");
163 if(_doesSecDHavePeer == false){
164 _doesSecDHavePeer = true;
165 [self doSetIDSDeviceID];
169 [self updateUnlockedSinceBoot];
170 [self updateIsLocked];
172 [self keybagDidUnlock];
179 - (void)streamEvent:(xpc_object_t)notification
181 #if (!TARGET_IPHONE_SIMULATOR)
182 const char *notificationName = xpc_dictionary_get_string(notification, "Notification");
183 if (!notificationName) {
184 } else if (strcmp(notificationName, kUserKeybagStateChangeNotification)==0) {
185 return [self keybagStateChange];
187 const char *eventName = xpc_dictionary_get_string(notification, "XPCEventName");
188 char *desc = xpc_copy_description(notification);
189 secnotice("event", "%@ event: %s name: %s desc: %s", self, eventName, notificationName, desc);
195 - (void) keybagDidLock
197 secnotice("IDS Transport", "%@ locking!", self);
200 - (void) keybagDidUnlock
202 secnotice("IDS Transport", "%@ unlocking!", self);
203 [self handleAllPendingMessage];
206 - (BOOL) updateUnlockedSinceBoot
208 CFErrorRef aksError = NULL;
209 if (!SecAKSGetHasBeenUnlocked(&_unlockedSinceBoot, &aksError)) {
210 secerror("%@ Got error from SecAKSGetHasBeenUnlocked: %@", self, aksError);
211 CFReleaseSafe(aksError);
217 - (BOOL) updateIsLocked
219 CFErrorRef aksError = NULL;
220 if (!SecAKSGetIsLocked(&_isLocked, &aksError)) {
221 secerror("%@ Got error querying lock state: %@", self, aksError);
222 CFReleaseSafe(aksError);
225 secerror("updateIsLocked: %d", _isLocked);
227 _unlockedSinceBoot = YES;
231 - (void) keybagStateChange
233 os_activity_initiate("keybagStateChanged", OS_ACTIVITY_FLAG_DEFAULT, ^{
234 secerror("keybagStateChange! was locked: %d", _isLocked);
235 BOOL wasLocked = _isLocked;
236 if ([self updateIsLocked]) {
237 if (wasLocked == _isLocked)
238 secdebug("IDS Transport", "%@ still %s ignoring", self, _isLocked ? "locked" : "unlocked");
240 [self keybagDidLock];
242 [self keybagDidUnlock];
250 if(_unhandledMessageBuffer)
251 secnotice("IDS Transport", "%@ attempting to hand unhandled messages to securityd, here is our message queue: %@", self, _unhandledMessageBuffer);
254 _retryTimerScheduled = NO;
255 else if([_unhandledMessageBuffer count] == 0)
256 _retryTimerScheduled = NO;
257 else if (_retryTimerScheduled && !_isLocked)
258 [self handleAllPendingMessage];
260 [[IDSKeychainSyncingProxy idsProxy] scheduleRetryRequestTimer];
264 - (void)scheduleRetryRequestTimer
266 secnotice("IDS Transport", "scheduling unhandled messages timer");
267 dispatch_source_set_timer(_retryTimer, dispatch_time(DISPATCH_TIME_NOW, kMinMessageRetryDelay), DISPATCH_TIME_FOREVER, kRetryTimerLeeway);
268 _retryTimerScheduled = YES;
271 - (void)doIDSInitialization
274 secnotice("IDS Transport", "doIDSInitialization!");
276 _service = [[IDSService alloc] initWithService: @IDSServiceNameKeychainSync];
278 if( _service == nil ){
279 _isIDSInitDone = false;
280 secerror("Could not create ids service");
283 secnotice("IDS Transport", "IDS Transport Successfully set up IDS!");
284 [_service addDelegate:self queue: dispatch_get_main_queue()];
286 _isIDSInitDone = true;
287 if(_isSecDRunningAsRoot == false)
288 [self doSetIDSDeviceID];
292 - (void) doSetIDSDeviceID
295 NSString *errorMessage = nil;
296 __block NSString* deviceID;
299 [self doIDSInitialization];
301 require_action_quiet(_isSecDRunningAsRoot == false, fail, errorMessage = @"cannot set IDS device ID, secd is running as root"; code = SECD_RUN_AS_ROOT_ERROR;);
302 require_action_quiet(_doesSecDHavePeer == true, fail, errorMessage = @"cannot set IDS deviceID, secd does not have a full peer info for account"; code = kSOSErrorPeerNotFound);
303 require_action_quiet(_isIDSInitDone, fail, errorMessage = @"IDSKeychainSyncingProxy can't set up the IDS service"; code = kSecIDSErrorNotRegistered);
304 require_action_quiet(!_isLocked, fail, errorMessage = @"IDSKeychainSyncingProxy can't set device ID, device is locked"; code = kSecIDSErrorDeviceIsLocked);
306 deviceID = IDSCopyLocalDeviceUniqueID();
307 secdebug("IDS Transport", "This is our IDS device ID: %@", deviceID);
309 require_action_quiet(deviceID != nil, fail, errorMessage = @"IDSKeychainSyncingProxy could not retrieve device ID from keychain"; code = kSecIDSErrorNoDeviceID);
311 if(_inCallout && _isSecDRunningAsRoot == false){
312 _shadowDoSetIDSDeviceID = YES;
315 _setIDSDeviceID = YES;
316 [self calloutWith:^(NSMutableDictionary *pending, bool handlePendingMesssages, bool doSetDeviceID, dispatch_queue_t queue, void(^done)(NSMutableDictionary *, bool, bool)) {
317 CFErrorRef localError = NULL;
318 bool handledSettingID = false;
319 handledSettingID = SOSCCSetDeviceID((__bridge CFStringRef) deviceID, &localError);
320 if(!handledSettingID && localError != NULL){
321 if(CFErrorGetCode(localError) == SECD_RUN_AS_ROOT_ERROR){
322 secerror("SETTING RUN AS ROOT ERROR: %@", localError);
323 _isSecDRunningAsRoot = true;
325 else if (CFErrorGetCode(localError) == -536870174 && CFErrorGetDomain(localError) == kSecKernDomain) {
326 secnotice("IDS Transport", "system is locked, cannot set device ID, error: %@", localError);
329 else if (CFErrorGetCode(localError) == kSOSErrorPeerNotFound && CFStringCompare(CFErrorGetDomain(localError), kSOSErrorDomain, 0) == 0){
330 secnotice("IDS Transport","securityd does not have a peer yet , error: %@", localError);
331 _doesSecDHavePeer = false;
335 _setIDSDeviceID = NO;
337 CFReleaseNull(localError);
338 dispatch_async(queue, ^{
344 if(errorMessage != nil){
345 secerror("Setting device ID error: %@, code: %ld", errorMessage, (long)code);
349 - (void) calloutWith: (void(^)(NSMutableDictionary *pending, bool handlePendingMesssages, bool doSetDeviceID, dispatch_queue_t queue, void(^done)(NSMutableDictionary *handledMessages, bool handledPendingMessage, bool handledSettingDeviceID))) callout
351 // In IDSKeychainSyncingProxy serial queue
352 dispatch_queue_t idsproxy_queue = dispatch_get_main_queue();
354 // dispatch_get_global_queue - well-known global concurrent queue
355 // dispatch_get_main_queue - default queue that is bound to the main thread
356 xpc_transaction_begin();
357 dispatch_async(_calloutQueue, ^{
358 __block NSMutableDictionary *myPending;
359 __block bool myHandlePendingMessage;
360 __block bool myDoSetDeviceID;
361 __block bool wasLocked;
362 dispatch_sync(idsproxy_queue, ^{
363 myPending = [_unhandledMessageBuffer copy];
364 myHandlePendingMessage = _handleAllPendingMessages;
365 myDoSetDeviceID = _setIDSDeviceID;
366 wasLocked = _isLocked;
370 _shadowHandleAllPendingMessages = NO;
373 callout(myPending, myHandlePendingMessage, myDoSetDeviceID, idsproxy_queue, ^(NSMutableDictionary *handledMessages, bool handledPendingMessage, bool handledSetDeviceID) {
374 secdebug("event", "%@ %s%s before callout handled: %s%s", self, myHandlePendingMessage ? "P" : "p", myDoSetDeviceID ? "D" : "d", handledPendingMessage ? "H" : "h", handledSetDeviceID ? "I" : "i");
376 // In IDSKeychainSyncingProxy's serial queue
379 // Update setting device id
380 _setIDSDeviceID = ((myDoSetDeviceID && !handledSetDeviceID));
382 _shadowDoSetIDSDeviceID = NO;
384 xpc_transaction_end();
389 - (void) sendKeysCallout: (NSMutableDictionary*(^)(NSMutableDictionary* pending, NSError** error)) handleMessages {
390 [self calloutWith: ^(NSMutableDictionary *pending, bool handlePendingMesssages, bool doSetDeviceID, dispatch_queue_t queue, void(^done)(NSMutableDictionary *, bool, bool)) {
391 NSError* error = NULL;
393 NSMutableDictionary* handled = handleMessages(pending, &error);
395 dispatch_async(queue, ^{
396 if (!handled && error) {
397 secerror("%@ did not handle message: %@", self, error);
400 done(handled, NO, NO);
405 NSString* createErrorString(NSString* format, ...)
408 va_start(va, format);
409 NSString* errorString = ([[NSString alloc] initWithFormat:format arguments:va]);