2 * Copyright (c) 2012-2017 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@
25 #import <Foundation/Foundation.h>
26 #import <Foundation/NSArray.h>
27 #import <Foundation/Foundation.h>
29 #import <Security/SecBasePriv.h>
30 #import <Security/SecItemPriv.h>
31 #import <utilities/debugging.h>
34 #include <Security/CKBridge/SOSCloudKeychainConstants.h>
35 #include <Security/SecureObjectSync/SOSARCDefines.h>
36 #include <Security/SecureObjectSync/SOSCloudCircle.h>
37 #include <Security/SecureObjectSync/SOSCloudCircleInternal.h>
38 #include <utilities/SecCFWrappers.h>
41 #import <os/activity.h>
43 #include <utilities/SecAKSWrappers.h>
44 #include <utilities/SecCFRelease.h>
45 #include <AssertMacros.h>
47 #import "IDSPersistentState.h"
48 #import "KeychainSyncingOverIDSProxy+ReceiveMessage.h"
49 #import "KeychainSyncingOverIDSProxy+SendMessage.h"
53 static NSString *const kIDSNumberOfFragments = @"NumberOfIDSMessageFragments";
54 static NSString *const kIDSFragmentIndex = @"kFragmentIndex";
55 static NSString *const kIDSMessageRecipientID = @"RecipientPeerID";
56 static NSString *const kIDSMessageUseACKModel = @"UsesAckModel";
57 static NSString *const kIDSMessageSendersDeviceID = @"SendersDeviceID";
59 @implementation KeychainSyncingOverIDSProxy (ReceiveMessage)
61 -(int) countNumberOfValidObjects:(NSMutableArray*)fragmentsForDeviceID
63 __block int count = 0;
64 [fragmentsForDeviceID enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL * _Nonnull stop) {
65 if(obj != [NSNull null]){
72 -(BOOL) checkForFragmentation:(NSDictionary*)message id:(NSString*)fromID data:(NSData*)messageData
74 BOOL handOffMessage = false;
76 if([message valueForKey:kIDSNumberOfFragments] != nil){
77 NSNumber *idsNumberOfFragments = [message objectForKey:kIDSNumberOfFragments];
78 NSNumber *index = [message objectForKey:kIDSFragmentIndex];
79 NSString *uuidString = [message objectForKey:(__bridge NSString*)kIDSMessageUniqueID];
81 if([KeychainSyncingOverIDSProxy idsProxy].allFragmentedMessages == nil)
82 [KeychainSyncingOverIDSProxy idsProxy].allFragmentedMessages = [NSMutableDictionary dictionary];
84 NSMutableDictionary *uniqueMessages = [[KeychainSyncingOverIDSProxy idsProxy].allFragmentedMessages objectForKey: fromID];
85 if(uniqueMessages == nil)
86 uniqueMessages = [NSMutableDictionary dictionary];
88 NSMutableArray *fragmentsForDeviceID = [uniqueMessages objectForKey: uuidString];
89 if(fragmentsForDeviceID == nil){
90 fragmentsForDeviceID = [ [NSMutableArray alloc] initWithCapacity: [idsNumberOfFragments longValue]];
91 for (int i = 0; i <[idsNumberOfFragments longValue] ; i++) {
92 [fragmentsForDeviceID addObject:[NSNull null]];
96 [fragmentsForDeviceID replaceObjectAtIndex: [index intValue] withObject:messageData ];
97 [uniqueMessages setObject: fragmentsForDeviceID forKey:uuidString];
98 [[KeychainSyncingOverIDSProxy idsProxy].allFragmentedMessages setObject:uniqueMessages forKey: fromID];
100 if([self countNumberOfValidObjects:fragmentsForDeviceID] == [idsNumberOfFragments longValue])
101 handOffMessage = true;
103 handOffMessage = false;
106 else //no fragmentation in the message, ready to hand off to securityd
107 handOffMessage = true;
109 return handOffMessage;
113 -(NSMutableDictionary*) combineMessage:(NSString*)ID peerID:(NSString*)peerID uuid:(NSString*)uuid
115 NSString *dataKey = [ NSString stringWithUTF8String: kMessageKeyIDSDataMessage ];
116 NSString *deviceIDKey = [ NSString stringWithUTF8String: kMessageKeyDeviceID ];
117 NSString *peerIDKey = [ NSString stringWithUTF8String: kMessageKeyPeerID ];
119 NSMutableDictionary *arrayOfFragmentedMessagesByUUID = [[KeychainSyncingOverIDSProxy idsProxy].allFragmentedMessages objectForKey:ID];
120 NSMutableArray *messagesForUUID = [arrayOfFragmentedMessagesByUUID objectForKey:uuid];
121 NSMutableData* completeMessage = [NSMutableData data];
123 [messagesForUUID enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
124 NSData *messageFragment = (NSData*)obj;
126 [completeMessage appendData: messageFragment];
128 //we've combined the message, now remove it from the fragmented messages dictionary
129 [arrayOfFragmentedMessagesByUUID removeObjectForKey:uuid];
131 return [NSMutableDictionary dictionaryWithObjectsAndKeys: completeMessage, dataKey, ID, deviceIDKey, peerID, peerIDKey, nil];
134 -(void) handleTestMessage:(NSString*)operation id:(NSString*)ID messageID:(NSString*)uniqueID senderPeerID:(NSString*)senderPeerID
136 int operationType = [operation intValue];
137 switch(operationType){
138 case kIDSPeerAvailabilityDone:
140 //set current timestamp to indicate success!
141 [self.peerNextSendCache setObject:[NSDate date] forKey:ID];
143 secnotice("IDS Transport","!received availability response!: %@", ID);
144 notify_post(kSOSCCPeerAvailable);
147 case kIDSEndPingTestMessage:
148 secnotice("IDS Transport","received pong message from other device: %@, ping test PASSED", ID);
150 case kIDSSendOneMessage:
151 secnotice("IDS Transport","received ping test message, dropping on the floor now");
154 case kIDSPeerAvailability:
155 case kIDSStartPingTestMessage:
158 if(operationType == kIDSPeerAvailability){
159 secnotice("IDS Transport","Received Availability Message from:%@!", ID);
160 asprintf(&messageCharS, "%d",kIDSPeerAvailabilityDone);
163 secnotice("IDS Transport","Received PingTest Message from: %@!", ID);
164 asprintf(&messageCharS, "%d", kIDSEndPingTestMessage);
167 NSString *operationString = [[NSString alloc] initWithUTF8String:messageCharS];
168 NSString* messageString = @"peer availability check finished";
169 NSDictionary* messsageDictionary = @{(__bridge NSString*)kIDSOperationType:operationString, (__bridge NSString*)kIDSMessageToSendKey:messageString};
171 // We can always hold on to a message and our remote peers would bother everyone
172 [self sendIDSMessage:messsageDictionary name:ID peer:@"me" senderDeviceID:NULL];
178 case kIDSPeerReceivedACK:
180 //set current timestamp to indicate success!
181 [self.peerNextSendCache setObject:[[NSDate alloc] init] forKey:ID];
184 secnotice("IDS Transport", "received ack for: %@", uniqueID);
185 dispatch_async(self.pingQueue, ^{
186 //remove timer for message id
187 dispatch_source_t timer = [[KeychainSyncingOverIDSProxy idsProxy].pingTimers objectForKey:uniqueID];
189 dispatch_cancel(timer);
190 [[KeychainSyncingOverIDSProxy idsProxy].pingTimers removeObjectForKey:uniqueID];
191 dispatch_sync(self.dataQueue, ^{
192 [[KeychainSyncingOverIDSProxy idsProxy].messagesInFlight removeObjectForKey:uniqueID];
194 [[KeychainSyncingOverIDSProxy idsProxy] persistState];
197 //call out to securityd to set a NULL
198 [self sendKeysCallout:^NSMutableDictionary *(NSMutableDictionary *pending, NSError** error) {
200 CFErrorRef localError = NULL;
201 SOSCCClearPeerMessageKeyInKVS((__bridge CFStringRef)senderPeerID, &localError);
211 - (void)sendACK:(NSString*)ID peerID:(NSString*)sendersPeerID uniqueID:(NSString*)uniqueID senderDeviceID:(NSString*)senderDeviceID
214 NSString* messageString = @"ACK";
216 asprintf(&messageCharS, "%d",kIDSPeerReceivedACK);
217 NSString *operationString = [[NSString alloc] initWithUTF8String:messageCharS];
219 NSDictionary* messageDictionary = @{(__bridge NSString*)kIDSOperationType:operationString, (__bridge NSString*)kIDSMessageToSendKey:messageString, (__bridge NSString*)kIDSMessageUniqueID:uniqueID};
221 [self sendIDSMessage:messageDictionary name:ID peer:sendersPeerID senderDeviceID:senderDeviceID];
227 - (void)updateDeviceList
229 self.deviceIDFromAuthToken = nil;
230 self.deviceIDFromAuthToken = [NSMutableDictionary dictionary];
231 [self calloutWith:^(NSMutableDictionary *pending, bool handlePendingMesssages, bool doSetDeviceID, dispatch_queue_t queue, void(^done)(NSMutableDictionary *, bool, bool)) {
234 - (void)service:(IDSService *)service account:(IDSAccount *)account incomingMessage:(NSDictionary *)message fromID:(NSString *)fromID context:(IDSMessageContext *)context
236 NSString *dataKey = [ NSString stringWithUTF8String: kMessageKeyIDSDataMessage ];
237 NSString *deviceIDKey = [ NSString stringWithUTF8String: kMessageKeyDeviceID ];
238 NSString *peerIDKey = [ NSString stringWithUTF8String: kMessageKeyPeerID ];
239 NSString *sendersPeerIDKey = [NSString stringWithUTF8String: kMessageKeySendersPeerID];
241 [KeychainSyncingOverIDSProxy idsProxy].incomingMessages++;
243 dispatch_async(self.calloutQueue, ^{
244 NSString* messageID = nil;
245 uint32_t operationType;
246 bool hadError = false;
247 CFStringRef errorMessage = NULL;
248 __block NSString* myPeerID = @"";
249 __block NSData *messageData = nil;
250 NSString* operationTypeAsString = nil;
251 NSMutableDictionary *messageDictionary = nil;
252 NSString *useAck = nil;
253 NSString *senderDeviceID = nil;
254 NSArray *devices = [self->_service devices];
255 for(NSUInteger i = 0; i < [ devices count ]; i++){
256 IDSDevice *device = devices[i];
257 if( [(IDSCopyIDForDevice(device)) containsString: fromID] == YES){
258 senderDeviceID = device.uniqueID;
262 [[KeychainSyncingOverIDSProxy idsProxy] printMessage:message state:[NSString stringWithFormat:@"received message from: %@", senderDeviceID]];
263 NSString *sendersPeerID = [message objectForKey: sendersPeerIDKey];
265 if(sendersPeerID == nil)
266 sendersPeerID = [NSString string];
269 senderDeviceID = message[kIDSMessageSendersDeviceID];
270 secnotice("IDS Transport", "Their device ID!: %@", senderDeviceID);
272 require_action_quiet(senderDeviceID, fail, hadError = true; errorMessage = CFSTR("require the sender's device ID"));
274 operationTypeAsString = [message objectForKey: (__bridge NSString*)kIDSOperationType];
275 messageDictionary = [message objectForKey: (__bridge NSString*)kIDSMessageToSendKey];
277 messageID = [message objectForKey:(__bridge NSString*)kIDSMessageUniqueID];
278 useAck = [message objectForKey:kIDSMessageUseACKModel];
280 if(useAck != nil && [useAck compare:@"YES"] == NSOrderedSame)
281 require_quiet(messageID != nil, fail);
283 secnotice("IDS Transport","from peer %@, operation: %@", senderDeviceID, operationTypeAsString);
284 operationType = [operationTypeAsString intValue];
286 if(operationType != kIDSKeychainSyncIDSFragmentation)
288 [self handleTestMessage:operationTypeAsString id:senderDeviceID messageID:messageID senderPeerID:sendersPeerID];
292 [messageDictionary enumerateKeysAndObjectsUsingBlock:^(id key, id obj, BOOL *stop) {
293 myPeerID = (NSString*)key;
294 messageData = (NSData*)obj;
297 BOOL readyToHandOffToSecD = [self checkForFragmentation:message id:senderDeviceID data:messageData];
299 NSMutableDictionary *messageAndFromID = nil;
301 if(readyToHandOffToSecD && ([message objectForKey:kIDSFragmentIndex])!= nil){
302 secnotice("IDS Transport", "combing message");
303 NSString* uuid = [message objectForKey:(__bridge NSString*)kIDSMessageUniqueID];
304 messageAndFromID = [self combineMessage:senderDeviceID peerID:myPeerID uuid:uuid];
305 //update next sequence number
307 else if(readyToHandOffToSecD){
308 messageAndFromID = [NSMutableDictionary dictionaryWithObjectsAndKeys: messageData, dataKey, senderDeviceID, deviceIDKey, myPeerID, peerIDKey, nil];
313 //set the sender's peer id so we can check it in securityd
314 [messageAndFromID setObject:sendersPeerID forKey:sendersPeerIDKey];
316 if([KeychainSyncingOverIDSProxy idsProxy].isLocked){
317 //hang on to the message and set the retry deadline
318 dispatch_sync(self.dataQueue, ^{
319 [self.unhandledMessageBuffer setObject: messageAndFromID forKey: fromID];
323 [self sendMessageToSecurity:messageAndFromID fromID:fromID shouldSendAck:useAck peerID:myPeerID messageID:messageID deviceID:senderDeviceID];
330 secerror("error:%@", errorMessage);
335 - (void) handleAllPendingMessage
337 secnotice("IDS Transport", "Attempting to handle pending messsages");
339 NSMutableDictionary * __block copyOfUnhandled = nil;
340 dispatch_sync(self.dataQueue, ^{
341 if ([self.unhandledMessageBuffer count] > 0) {
342 secnotice("IDS Transport", "handling messages: %@", self.unhandledMessageBuffer);
343 copyOfUnhandled = [NSMutableDictionary dictionaryWithDictionary:self.unhandledMessageBuffer];
347 [copyOfUnhandled enumerateKeysAndObjectsUsingBlock: ^(id key, id obj, BOOL *stop)
349 NSMutableDictionary *messageAndFromID = (NSMutableDictionary*)obj;
350 NSString *fromID = (NSString*)key;
351 //remove the message from the official message buffer (if it fails to get handled it'll be reset again in sendMessageToSecurity)
352 dispatch_sync(self.dataQueue, ^{
353 [self.unhandledMessageBuffer removeObjectForKey: fromID];
355 [self sendMessageToSecurity:messageAndFromID fromID:fromID shouldSendAck:nil peerID:nil messageID:nil deviceID:nil];
359 - (bool) shouldPersistMessage:(NSDictionary*) newMessageAndFromID id:(NSString*)fromID
361 //get the dictionary of messages for a particular device id
362 NSDictionary* __block messagesFromBuffer;
363 dispatch_sync(self.dataQueue, ^{
364 messagesFromBuffer = [self.unhandledMessageBuffer valueForKey:fromID];
367 if([messagesFromBuffer isEqual:newMessageAndFromID])
373 -(void)sendMessageToSecurity:(NSMutableDictionary*)messageAndFromID fromID:(NSString*)fromID shouldSendAck:(NSString *)useAck peerID:(NSString*)peerID messageID:(NSString*)messageID deviceID:(NSString*)senderDeviceID
375 __block CFErrorRef cf_error = NULL;
376 __block HandleIDSMessageReason success = kHandleIDSMessageSuccess;
378 [self sendKeysCallout:^NSMutableDictionary *(NSMutableDictionary *pending, NSError** error) {
380 success = SOSCCHandleIDSMessage(((__bridge CFDictionaryRef)messageAndFromID), &cf_error);
381 //turns out the error needs to be evaluated as sync_and_do returns bools
384 if(CFErrorIsMalfunctioningKeybagError(cf_error)){
385 success = kHandleIDSMessageLocked;
389 if(success == kHandleIDSMessageLocked){
390 secnotice("IDS Transport","cannot handle messages from: %@ when locked, error:%@", fromID, cf_error);
391 // I don't think this is ever nil but it was like this when I got here
392 dispatch_sync(self.dataQueue, ^{
393 if(!self.unhandledMessageBuffer) {
394 self.unhandledMessageBuffer = [NSMutableDictionary dictionary];
398 //write message to disk if message is new to the unhandled queue
399 if([self shouldPersistMessage:messageAndFromID id:fromID]) {
403 dispatch_sync(self.dataQueue, ^{
404 [self.unhandledMessageBuffer setObject: messageAndFromID forKey: fromID];
405 secnotice("IDS Transport", "unhandledMessageBuffer: %@", self.unhandledMessageBuffer);
410 else if(success == kHandleIDSMessageNotReady){
411 secnotice("IDS Transport","not ready to handle message from: %@, error:%@", fromID, cf_error);
412 dispatch_sync(self.dataQueue, ^{
413 if(!self.unhandledMessageBuffer) {
414 self.unhandledMessageBuffer = [NSMutableDictionary dictionary];
416 [self.unhandledMessageBuffer setObject: messageAndFromID forKey: fromID];
417 secnotice("IDS Transport","unhandledMessageBuffer: %@", self.unhandledMessageBuffer);
419 //write message to disk if message is new to the unhandled queue
420 if([self shouldPersistMessage:messageAndFromID id:fromID])
423 [[KeychainSyncingOverIDSProxy idsProxy] scheduleRetryRequestTimer];
426 else if(success == kHandleIDSmessageDeviceIDMismatch){
427 secnotice("IDS Transport","message for a ghost! dropping message. error:%@", cf_error);
430 else if(success == kHandleIDSMessageDontHandle){
431 secnotice("IDS Transport","error in message, dropping message. error:%@", cf_error);
435 secnotice("IDS Transport","IDSProxy handled this message %@, from: %@", messageAndFromID, fromID);
437 if(useAck != nil && [useAck compare:@"YES"] == NSOrderedSame)
438 [self sendACK:senderDeviceID peerID:peerID uniqueID:messageID senderDeviceID:senderDeviceID];
439 return (NSMutableDictionary*)messageAndFromID;
442 CFReleaseNull(cf_error);