5 #import <Foundation/Foundation.h>
6 #include <Security/SecureObjectSync/SOSPeer.h>
7 #include <Security/SecureObjectSync/SOSPeerCoder.h>
8 #include <Security/SecureObjectSync/SOSTransportMessage.h>
9 #include <Security/SecureObjectSync/SOSAccount.h>
10 #include <Security/SecureObjectSync/SOSCoder.h>
11 #include <Security/SecureObjectSync/SOSEngine.h>
12 #include <Security/SecureObjectSync/SOSDataSource.h>
13 #include <Security/SecureObjectSync/SOSAccountTransaction.h>
14 #include <Security/SecureObjectSync/SOSKVSKeys.h>
15 #include <Security/SecureObjectSync/SOSPeerOTRTimer.h>
16 #include <Security/CKBridge/SOSCloudKeychainClient.h>
18 #include <utilities/SecADWrapper.h>
19 #include <utilities/debugging.h>
20 #include <utilities/SecCFWrappers.h>
22 #include <AssertMacros.h>
23 #include "SOSInternal.h"
26 NSString* const SecSOSAggdMaxRenegotiation = @"com.apple.security.sos.otrrenegotiationmaxretries";
28 __unused static int initialOTRTimeoutValue = 60; //best round trip time in KVS plus extra for good measure
29 static int maxRetryCount = 7; //max number of times to attempt restarting OTR negotiation
31 bool SOSPeerOTRTimerHaveReachedMaxRetryAllowance(SOSAccount* account, NSString* peerid){
32 bool reachedMax = false;
33 NSMutableDictionary* attemptsPerPeer = (__bridge NSMutableDictionary*)SOSAccountGetValue(account, kSOSAccountRenegotiationRetryCount, NULL);
35 attemptsPerPeer = [NSMutableDictionary dictionary];
37 NSNumber* attempt = [attemptsPerPeer objectForKey:peerid];
38 if(attempt && [attempt intValue] >= maxRetryCount)
41 SecADAddValueForScalarKey((__bridge CFStringRef) SecSOSAggdMaxRenegotiation,1);
46 //used when evaluating whether or not securityd should start a timer for negotiation
47 bool SOSPeerOTRTimerHaveAnRTTAvailable(SOSAccount* account, NSString* peerid)
49 CFErrorRef error = NULL;
50 CFNumberRef timeout = NULL;
51 bool doesRTTExist = false;
53 CFMutableDictionaryRef timeouts = (CFMutableDictionaryRef)asDictionary(SOSAccountGetValue(account, kSOSAccountPeerNegotiationTimeouts, &error), NULL);
54 require_action_quiet(timeouts, exit, secnotice("otrtimer", "do not have an rtt yet"));
56 timeout = CFDictionaryGetValue(timeouts, (__bridge CFStringRef)peerid);
57 require_action_quiet(timeout, exit, secnotice("otrtimer", "do not have an rtt yet"));
64 //call when a timer has fired, remove the current rtt entry as the existing one isn't working
65 void SOSPeerOTRTimerRemoveRTTTimeoutForPeer(SOSAccount* account, NSString* peerid)
67 CFNumberRef timeout = NULL;
68 CFErrorRef error = NULL;
70 CFMutableDictionaryRef timeouts = (CFMutableDictionaryRef)asDictionary(SOSAccountGetValue(account, kSOSAccountPeerNegotiationTimeouts, &error), NULL);
71 require_action_quiet(timeouts && (error == NULL), exit, secnotice("otrtimer","timeout dictionary doesn't exist"));
73 timeout = CFDictionaryGetValue(timeouts, (__bridge CFStringRef)peerid);
74 require_action_quiet(timeout, exit, secnotice("otrtimer","timeout for peerid: %@, doesn't exist", (__bridge CFStringRef)peerid));
76 CFDictionaryRemoveValue(timeouts, (__bridge CFStringRef)peerid);
77 SOSAccountSetValue(account, kSOSAccountPeerNegotiationTimeouts, timeouts, &error);
79 secnotice("otrtimer","SOSAccountSetValue threw an error for key kSOSAccountPeerNegotiationTimeouts: %@", error);
85 void SOSPeerOTRTimerRemoveLastSentMessageTimestamp(SOSAccount* account, NSString* peerid)
87 NSMutableDictionary *peerToTimeLastSentDict = (__bridge NSMutableDictionary*)SOSAccountGetValue(account, kSOSAccountPeerLastSentTimestamp, NULL);
89 if(peerToTimeLastSentDict){
90 NSDate* storedDate = [peerToTimeLastSentDict objectForKey:peerid];
92 [peerToTimeLastSentDict removeObjectForKey:peerid];
93 SOSAccountSetValue(account, kSOSAccountPeerLastSentTimestamp, (__bridge CFMutableDictionaryRef)peerToTimeLastSentDict, NULL);
98 void SOSPeerOTRTimerIncreaseOTRNegotiationRetryCount(SOSAccount* account, NSString* peerid)
100 NSMutableDictionary* attemptsPerPeer = (__bridge NSMutableDictionary*)SOSAccountGetValue(account, kSOSAccountRenegotiationRetryCount, NULL);
101 if(!attemptsPerPeer){
102 attemptsPerPeer = [NSMutableDictionary dictionary];
104 NSNumber* attempt = [attemptsPerPeer objectForKey:peerid];
106 attempt = [[NSNumber alloc] initWithInt:1];
107 [attemptsPerPeer setObject:attempt forKey:peerid];
110 NSNumber* newCount = [[NSNumber alloc]initWithInt:([attempt intValue]+1)];
111 [attemptsPerPeer setObject:newCount forKey:peerid];
112 secnotice("otr","OTR negotiation retry count: %d", [newCount intValue]);
114 SOSAccountSetValue(account, kSOSAccountRenegotiationRetryCount, (__bridge CFMutableDictionaryRef)attemptsPerPeer, NULL);
117 int SOSPeerOTRTimerTimeoutValue(SOSAccount* account, SOSPeerRef peer)
119 CFErrorRef error = NULL;
120 int timeoutIntValue = 0;
122 CFMutableDictionaryRef timeouts = (CFMutableDictionaryRef)asDictionary(SOSAccountGetValue(account, kSOSAccountPeerNegotiationTimeouts, &error), NULL);
123 require_action_quiet(timeouts, xit, secnotice("otrtimer","deadline value not available yet"));
125 CFNumberRef timeout = CFDictionaryGetValue(timeouts, SOSPeerGetID(peer));
126 require_action_quiet(timeout, xit, secnotice("otrtimer","deadline value not available yet"));
128 secnotice("otrtimer", "decided to wait %d before restarting negotiation", [(__bridge NSNumber*)timeout intValue]);
129 timeoutIntValue = [(__bridge NSNumber*)timeout intValue];
132 return timeoutIntValue;
135 void SOSPeerOTRTimerSetupAwaitingTimer(SOSAccount* account, SOSPeerRef peer, SOSEngineRef engine, SOSCoderRef coder)
137 //check which timeout value to use
138 int timeoutValue = SOSPeerOTRTimerTimeoutValue(account, peer);
139 CFStringRef peerid = CFRetainSafe(SOSPeerGetID(peer));
141 secnotice("otrtimer", "setting timer for peer: %@", peer);
142 __block dispatch_source_t timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, dispatch_get_main_queue());
143 dispatch_source_set_timer(timer, dispatch_time(DISPATCH_TIME_NOW, timeoutValue * NSEC_PER_SEC), DISPATCH_TIME_FOREVER, 0);
144 dispatch_source_set_event_handler(timer, ^{
145 secnotice("otrtimer","otrTimerFired fired");
146 SOSCCResetOTRNegotiation_Server(peerid);
150 dispatch_source_set_cancel_handler(timer, ^{
151 CFReleaseSafe(peerid);
154 dispatch_resume(timer);
156 SOSPeerSetOTRTimer(peer, timer);
159 //clear the max retry counter in the account object
160 void SOSPeerOTRTimerClearMaxRetryCount(SOSAccount* account, NSString* peerid)
162 secnotice("otrtimer", "negotiation finished! clearing max retry counter");
163 NSMutableDictionary* attemptsPerPeer = (__bridge NSMutableDictionary*)SOSAccountGetValue(account, kSOSAccountRenegotiationRetryCount, NULL);
164 if(!attemptsPerPeer){
165 attemptsPerPeer = [NSMutableDictionary dictionary];
167 [attemptsPerPeer removeObjectForKey:peerid];
168 SOSAccountSetValue(account, kSOSAccountRenegotiationRetryCount, (__bridge CFMutableDictionaryRef)attemptsPerPeer, NULL);