2 * Copyright (c) 2012-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@
24 #include <Security/SecureObjectSync/SOSPeer.h>
25 #include <Security/SecureObjectSync/SOSPeerCoder.h>
26 #include <Security/SecureObjectSync/SOSTransportMessage.h>
27 #include <Security/SecureObjectSync/SOSAccount.h>
28 #include <Security/SecureObjectSync/SOSCoder.h>
29 #include <Security/SecureObjectSync/SOSEngine.h>
30 #include <Security/SecureObjectSync/SOSDataSource.h>
31 #include <Security/SecureObjectSync/SOSAccountTransaction.h>
32 #include <Security/SecureObjectSync/SOSKVSKeys.h>
33 #include <Security/SecureObjectSync/SOSPeerOTRTimer.h>
35 #include <Security/CKBridge/SOSCloudKeychainClient.h>
37 #include <utilities/debugging.h>
38 #include <utilities/SecCFWrappers.h>
40 #include <AssertMacros.h>
41 #include "SOSInternal.h"
43 enum SOSCoderUnwrapStatus SOSPeerHandleCoderMessage(SOSPeerRef peer, SOSCoderRef coder, CFStringRef peer_id, CFDataRef codedMessage, CFDataRef *decodedMessage, bool *forceSave, CFErrorRef *error) {
45 enum SOSCoderUnwrapStatus result = SOSCoderUnwrapError;
46 CFMutableDataRef localDecodedMessage = NULL;
48 SOSCoderStatus coderStatus = kSOSCoderDataReturned;
49 require_action_quiet(coder, xit, secerror("%@ getCoder: %@", peer_id, error ? *error : NULL));
50 CFErrorRef localError = NULL;
52 coderStatus = SOSCoderUnwrap(coder, codedMessage, &localDecodedMessage, peer_id, error);
53 dispatch_source_t timer = SOSPeerGetOTRTimer(peer);
55 secnotice("otrtimer","removing timer for peer: %@", peer);
56 SOSPeerRemoveOTRTimerEntry(peer);
57 dispatch_cancel(timer);
60 case kSOSCoderDataReturned: {
61 //logRawMessage(localDecodedMessage, false, 0);
62 result = SOSCoderUnwrapDecoded;
65 case kSOSCoderNegotiating: // Sent message already in Unwrap.
66 result = SOSCoderUnwrapHandled;
67 secnotice("engine", "%@ engine negotiating", peer_id);
69 case kSOSCoderNegotiationCompleted:
70 SOSPeerDidConnect(peer);
71 result = SOSCoderUnwrapHandled;
73 secnotice("engine", "%@ engine negotiation complete", peer_id);
75 case kSOSCoderFailure: // Probably restart coder
76 secnotice("engine", "%@ engine failed handling message %@", peer_id, error ? *error : NULL);
78 if(SOSCoderStart(coder, &localError) == kSOSCoderFailure){
79 secerror("Attempt to recover coder failed to restart: %@", localError);
82 case kSOSCoderStaleEvent: // We received an event we have already processed in the past.
83 secinfo("engine", "%@ engine stale event ignored", peer_id);
84 result = SOSCoderUnwrapHandled;
86 case kSOSCoderForceMessage:
87 SOSPeerSetMustSendMessage(peer, true);
88 result = SOSCoderUnwrapHandled;
90 case kSOSCoderTooNew: // We received an event from the future!
91 secnotice("engine", "%@ engine received a message too soon, time to restart", peer_id);
93 if(SOSCoderStart(coder, &localError) == kSOSCoderFailure){
94 secerror("Attempt to recover coder failed to restart: %@", localError);
102 *decodedMessage = CFRetainSafe(localDecodedMessage);
103 CFReleaseNull(localDecodedMessage);
106 CFReleaseNull(localError);
110 bool SOSPeerCoderSendMessageIfNeeded(SOSAccount* account, SOSEngineRef engine, SOSTransactionRef txn, SOSPeerRef peer, SOSCoderRef coder, CFDataRef *message_to_send, CFStringRef peer_id, CFMutableArrayRef *attributeList, SOSEnginePeerMessageSentCallback **sentCallback, CFErrorRef *error) {
112 secnotice("transport", "coder state: %@", coder);
113 require_action_quiet(coder, xit, secerror("%@ getCoder: %@", peer_id, error ? *error : NULL));
115 if (SOSCoderCanWrap(coder)) {
116 secinfo("transport", "%@ Coder can wrap, getting message from engine", peer_id);
117 CFMutableDataRef codedMessage = NULL;
118 CFDataRef message = SOSEngineCreateMessage_locked(engine, txn, peer, attributeList, error, sentCallback);
120 secnotice("transport", "%@ SOSEngineCreateMessage_locked failed: %@", peer_id, *error);
121 } else if (CFDataGetLength(message) || SOSPeerMustSendMessage(peer)) {
122 // TODO: Remove SOSPeerMustSendMessage from peer and move into coder/transport instead
123 ok = message && (SOSCoderWrap(coder, message, &codedMessage, peer_id, error) == kSOSCoderDataReturned);
125 secnotice("transport", "%@ SOSCoderWrap failed: %@", peer_id, *error);
127 CFRetainAssign(*message_to_send, codedMessage);
128 SOSEngineSetCodersNeedSaving(engine, true);
130 CFReleaseNull(codedMessage);
132 // Zero length message means we have no work to do.
135 CFReleaseNull(message);
138 *message_to_send = SOSCoderCopyPendingResponse(coder);
139 SOSEngineSetCodersNeedSaving(engine, true);
140 secinfo("transport", "%@ negotiating, %@", peer_id, (message_to_send && *message_to_send) ? CFSTR("sending negotiation message.") : CFSTR("waiting for negotiation message."));
142 SOSEnginePeerMessageSentCallback* pmsc = malloc(sizeof(SOSEnginePeerMessageSentCallback));
143 memset(pmsc, 0, sizeof(SOSEnginePeerMessageSentCallback));
145 pmsc->coder = CFRetainSafe(coder);
146 SOSEngineMessageCallbackSetCallback(pmsc, ^(bool wasSent){
148 SOSCoderConsumeResponse(pmsc->coder);
152 *sentCallback = pmsc;
155 /*if coder state is in awaiting for message, then set a timer and restart if failure*/
156 if(*message_to_send != NULL && account.isInitialSyncing && !SOSPeerOTRTimerHaveReachedMaxRetryAllowance(account, (__bridge NSString*)peer_id)){
157 if(SOSCoderIsCoderInAwaitingState(coder) && !SOSPeerTimerForPeerExist(peer) && SOSPeerOTRTimerHaveAnRTTAvailable(account, (__bridge NSString*)peer_id)){
158 secnotice("otrtimer", "coder is in awaiting state");
159 SOSPeerOTRTimerSetupAwaitingTimer(account, peer, engine, coder);
161 else if(!SOSCoderIsCoderInAwaitingState(coder)){
162 secnotice("otrtimer", "coder not in awaiting state: %@", coder);
164 else if (SOSPeerTimerForPeerExist(peer)){
165 secnotice("otrtimer", "timer for coder already set: %@", coder);