]> git.saurik.com Git - apple/security.git/blob - OSX/sec/SOSCircle/SecureObjectSync/SOSPeerCoder.m
Security-58286.41.2.tar.gz
[apple/security.git] / OSX / sec / SOSCircle / SecureObjectSync / SOSPeerCoder.m
1 /*
2 * Copyright (c) 2012-2016 Apple Inc. All Rights Reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
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
11 * file.
12 *
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.
20 *
21 * @APPLE_LICENSE_HEADER_END@
22 */
23
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>
34
35 #include <Security/CKBridge/SOSCloudKeychainClient.h>
36
37 #include <utilities/debugging.h>
38 #include <utilities/SecCFWrappers.h>
39
40 #include <AssertMacros.h>
41 #include "SOSInternal.h"
42
43 void SOSPeerCoderConsume(SOSEnginePeerMessageSentBlock *sent, bool ok) {
44 if (*sent)
45 (*sent)(ok);
46 }
47
48 enum SOSCoderUnwrapStatus SOSPeerHandleCoderMessage(SOSPeerRef peer, SOSCoderRef coder, CFStringRef peer_id, CFDataRef codedMessage, CFDataRef *decodedMessage, bool *forceSave, CFErrorRef *error) {
49
50 enum SOSCoderUnwrapStatus result = SOSCoderUnwrapError;
51 CFMutableDataRef localDecodedMessage = NULL;
52
53 SOSCoderStatus coderStatus = kSOSCoderDataReturned;
54 require_action_quiet(coder, xit, secerror("%@ getCoder: %@", peer_id, error ? *error : NULL));
55 CFErrorRef localError = NULL;
56 if (coder) {
57 coderStatus = SOSCoderUnwrap(coder, codedMessage, &localDecodedMessage, peer_id, error);
58 dispatch_source_t timer = SOSPeerGetOTRTimer(peer);
59 if(timer){
60 secnotice("otrtimer","removing timer for peer: %@", peer);
61 SOSPeerRemoveOTRTimerEntry(peer);
62 dispatch_cancel(timer);
63 }
64 switch(coderStatus) {
65 case kSOSCoderDataReturned: {
66 //logRawMessage(localDecodedMessage, false, 0);
67 result = SOSCoderUnwrapDecoded;
68 break;
69 }
70 case kSOSCoderNegotiating: // Sent message already in Unwrap.
71 result = SOSCoderUnwrapHandled;
72 secnotice("engine", "%@ engine negotiating", peer_id);
73 break;
74 case kSOSCoderNegotiationCompleted:
75 SOSPeerDidConnect(peer);
76 result = SOSCoderUnwrapHandled;
77 *forceSave = true;
78 secnotice("engine", "%@ engine negotiation complete", peer_id);
79 break;
80 case kSOSCoderFailure: // Probably restart coder
81 secnotice("engine", "%@ engine failed handling message %@", peer_id, error ? *error : NULL);
82 SOSCoderReset(coder);
83 if(SOSCoderStart(coder, &localError) == kSOSCoderFailure){
84 secerror("Attempt to recover coder failed to restart: %@", localError);
85 }
86 break;
87 case kSOSCoderStaleEvent: // We received an event we have already processed in the past.
88 secinfo("engine", "%@ engine stale event ignored", peer_id);
89 result = SOSCoderUnwrapHandled;
90 break;
91 case kSOSCoderForceMessage:
92 SOSPeerSetMustSendMessage(peer, true);
93 result = SOSCoderUnwrapHandled;
94 break;
95 case kSOSCoderTooNew: // We received an event from the future!
96 secnotice("engine", "%@ engine received a message too soon, time to restart", peer_id);
97 SOSCoderReset(coder);
98 if(SOSCoderStart(coder, &localError) == kSOSCoderFailure){
99 secerror("Attempt to recover coder failed to restart: %@", localError);
100 }
101 break;
102 default:
103 assert(false);
104 break;
105 }
106 if(decodedMessage)
107 *decodedMessage = CFRetainSafe(localDecodedMessage);
108 CFReleaseNull(localDecodedMessage);
109 }
110
111 CFReleaseNull(localError);
112 xit:
113 return result;
114 }
115 bool SOSPeerCoderSendMessageIfNeeded(SOSAccount* account, SOSEngineRef engine, SOSTransactionRef txn, SOSPeerRef peer, SOSCoderRef coder, CFDataRef *message_to_send, CFStringRef peer_id, CFMutableArrayRef *attributeList, SOSEnginePeerMessageSentBlock *sent, CFErrorRef *error) {
116 bool ok = false;
117 secnotice("transport", "coder state: %@", coder);
118 require_action_quiet(coder, xit, secerror("%@ getCoder: %@", peer_id, error ? *error : NULL));
119
120 if (SOSCoderCanWrap(coder)) {
121 secinfo("transport", "%@ Coder can wrap, getting message from engine", peer_id);
122 CFMutableDataRef codedMessage = NULL;
123 CFDataRef message = SOSEngineCreateMessage_locked(engine, txn, peer, attributeList, error, sent);
124 if (!message) {
125 secnotice("transport", "%@ SOSEngineCreateMessage_locked failed: %@", peer_id, *error);
126 } else if (CFDataGetLength(message) || SOSPeerMustSendMessage(peer)) {
127 // TODO: Remove SOSPeerMustSendMessage from peer and move into coder/transport instead
128 ok = message && (SOSCoderWrap(coder, message, &codedMessage, peer_id, error) == kSOSCoderDataReturned);
129 if (!ok) {
130 secnotice("transport", "%@ SOSCoderWrap failed: %@", peer_id, *error);
131 } else {
132 CFRetainAssign(*message_to_send, codedMessage);
133 SOSEngineSetCodersNeedSaving(engine, true);
134 }
135 CFReleaseNull(codedMessage);
136 } else {
137 // Zero length message means we have no work to do.
138 ok = true;
139 }
140 CFReleaseNull(message);
141
142 } else {
143 *message_to_send = SOSCoderCopyPendingResponse(coder);
144 SOSEngineSetCodersNeedSaving(engine, true);
145 secinfo("transport", "%@ negotiating, %@", peer_id, (message_to_send && *message_to_send) ? CFSTR("sending negotiation message.") : CFSTR("waiting for negotiation message."));
146 *sent = ^(bool wasSent){
147 if (wasSent)
148 SOSCoderConsumeResponse(coder);
149 };
150 ok = true;
151 }
152 /*if coder state is in awaiting for message, then set a timer and restart if failure*/
153 if(*message_to_send != NULL && account.isInitialSyncing && !SOSPeerOTRTimerHaveReachedMaxRetryAllowance(account, (__bridge NSString*)peer_id)){
154 if(SOSCoderIsCoderInAwaitingState(coder) && !SOSPeerTimerForPeerExist(peer) && SOSPeerOTRTimerHaveAnRTTAvailable(account, (__bridge NSString*)peer_id)){
155 secnotice("otrtimer", "coder is in awaiting state");
156 SOSPeerOTRTimerSetupAwaitingTimer(account, peer, engine, coder);
157 }
158 else if(!SOSCoderIsCoderInAwaitingState(coder)){
159 secnotice("otrtimer", "coder not in awaiting state: %@", coder);
160 }
161 else if (SOSPeerTimerForPeerExist(peer)){
162 secnotice("otrtimer", "timer for coder already set: %@", coder);
163 }
164 }
165 xit:
166 return ok;
167 }