2 // SOSTransportCircleKVS.c
5 #include <Security/SecureObjectSync/SOSTransport.h>
6 #include <Security/SecureObjectSync/SOSTransportCircle.h>
7 #include <Security/SecureObjectSync/SOSTransportCircleKVS.h>
8 #include <Security/SecureObjectSync/SOSKVSKeys.h>
9 #include <Security/SecureObjectSync/SOSInternal.h>
10 #include <SOSCloudKeychainClient.h>
11 #include <utilities/SecCFWrappers.h>
12 #import "Security/SecureObjectSync/SOSAccountTrustClassic.h"
15 @implementation SOSKVSCircleStorageTransport
17 @synthesize pending_changes = pending_changes;
18 @synthesize circleName = circleName;
19 extern CFStringRef kSOSAccountDebugScope;
26 -(id)initWithAccount:(SOSAccount*)acct andCircleName:(NSString*)name
30 self.pending_changes = [NSMutableDictionary dictionary];
31 self.circleName = [[NSString alloc] initWithString:name];
33 SOSRegisterTransportCircle(self);
38 -(NSString*) getCircleName
40 return self.circleName;
43 -(CFIndex) getTransportType{
47 static bool SOSTransportCircleKVSUpdateKVS(NSDictionary *changes, CFErrorRef *error)
49 CloudKeychainReplyBlock log_error = ^(CFDictionaryRef returnedValues __unused, CFErrorRef block_error) {
51 secerror("Error putting: %@", block_error);
55 SOSCloudKeychainPutObjectsInCloud((__bridge CFDictionaryRef)(changes), dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), log_error);
59 -(bool)kvsSendPendingChanges:(CFErrorRef *)error
61 CFErrorRef changeError = NULL;
63 if (self.pending_changes == NULL || [self.pending_changes count] == 0) {
67 CFTypeRef dsid = SOSAccountGetValue(account, kSOSDSIDKey, error);
71 [self.pending_changes setObject:(__bridge id _Nonnull)((void*)(dsid)) forKey:(__bridge NSString*)kSOSKVSRequiredKey];
73 bool success = SOSTransportCircleKVSUpdateKVS(self.pending_changes, &changeError);
75 [self.pending_changes removeAllObjects];
77 SOSCreateErrorWithFormat(kSOSErrorSendFailure, changeError, error, NULL,
78 CFSTR("Send changes block failed [%@]"), self.pending_changes);
84 -(void)kvsAddToPendingChanges:(CFStringRef) message_key data:(CFDataRef)message_data
86 if (self.pending_changes == NULL) {
87 self.pending_changes = [NSMutableDictionary dictionary];
89 if (message_data == NULL) {
90 [self.pending_changes setObject:[NSNull null] forKey:(__bridge NSString*)message_key];
92 [self.pending_changes setObject:(__bridge NSData*)message_data forKey:(__bridge NSString*)message_key];
96 static bool SOSTransportCircleKVSUpdateRetirementRecords(CFDictionaryRef updates, CFErrorRef* error){
97 CFErrorRef updateError = NULL;
99 if (SOSTransportCircleKVSUpdateKVS((__bridge NSDictionary*)updates, &updateError)){
102 SOSCreateErrorWithFormat(kSOSErrorSendFailure, updateError, error, NULL,
103 CFSTR("update parameters key failed [%@]"), updates);
107 -(bool) expireRetirementRecords:(CFDictionaryRef) retirements err:(CFErrorRef *)error
110 CFMutableDictionaryRef keysToWrite = CFDictionaryCreateMutableForCFTypes(kCFAllocatorDefault);
112 CFDictionaryForEach(retirements, ^(const void *key, const void *value) {
113 if (isString(key) && isArray(value)) {
114 CFStringRef circle_name = (CFStringRef) key;
115 CFArrayRef retirees = (CFArrayRef) value;
117 CFArrayForEach(retirees, ^(const void *value) {
118 if (isString(value)) {
119 CFStringRef retiree_id = (CFStringRef) value;
121 CFStringRef kvsKey = SOSRetirementKeyCreateWithCircleNameAndPeer(circle_name, retiree_id);
123 CFDictionaryAddValue(keysToWrite, kvsKey, kCFNull);
125 CFReleaseSafe(kvsKey);
131 if(CFDictionaryGetCount(keysToWrite)) {
132 success = SOSTransportCircleKVSUpdateRetirementRecords(keysToWrite, error);
134 CFReleaseNull(keysToWrite);
139 -(bool) flushChanges:(CFErrorRef *)error
141 return [self kvsSendPendingChanges:error];
145 -(bool) postCircle:(CFStringRef)name circleData:(CFDataRef)circle_data err:(CFErrorRef *)error
147 CFStringRef circle_key = SOSCircleKeyCreateWithName(name, error);
149 [self kvsAddToPendingChanges:circle_key data:circle_data];
151 CFReleaseNull(circle_key);
156 -(CFDictionaryRef)CF_RETURNS_RETAINED handleRetirementMessages:(CFMutableDictionaryRef) circle_retirement_messages_table err:(CFErrorRef *)error
158 return SOSAccountHandleRetirementMessages(self.account, circle_retirement_messages_table, error);
161 -(CFArrayRef)CF_RETURNS_RETAINED handleCircleMessagesAndReturnHandledCopy:(CFMutableDictionaryRef) circle_circle_messages_table err:(CFErrorRef *)error
163 CFMutableArrayRef handledKeys = CFArrayCreateMutableForCFTypes(kCFAllocatorDefault);
164 CFDictionaryForEach(circle_circle_messages_table, ^(const void *key, const void *value) {
165 CFErrorRef circleMessageError = NULL;
166 if(!isString(key) || !isData(value)) {
167 secerror("Error, Key-Value for CircleMessage was not CFString/CFData");
168 } if (!SOSAccountHandleCircleMessage(self.account, key, value, &circleMessageError)) {
169 secerror("Error handling circle message %@ (%@): %@", key, value, circleMessageError);
171 CFStringRef circle_id = (CFStringRef) key;
172 CFArrayAppendValue(handledKeys, circle_id);
174 CFReleaseNull(circleMessageError);
180 -(bool)kvsAppendKeyInterest:(CFMutableArrayRef) alwaysKeys firstUnlock:(CFMutableArrayRef) afterFirstUnlockKeys unlocked:(CFMutableArrayRef)unlockedKeys err:(CFErrorRef *)error
182 CFStringRef circle_name = NULL;
183 CFStringRef circle_key = NULL;
185 if(SOSAccountHasPublicKey(self.account, NULL)){
186 require_quiet(circle_name = (__bridge CFStringRef)self.circleName, fail);
187 require_quiet(circle_key = SOSCircleKeyCreateWithName(circle_name, error), fail);
189 require_quiet(account, fail);
190 SOSAccountTrustClassic *trust = account.trust;
191 SOSCircleRef circle = trust.trustedCircle;
192 require_quiet(circle && CFEqualSafe(circle_name, SOSCircleGetName(circle)), fail);
194 SOSCircleForEachActivePeer(circle, ^(SOSPeerInfoRef peer) {
195 CFStringRef retirement_key = SOSRetirementKeyCreateWithCircleNameAndPeer(circle_name, SOSPeerInfoGetPeerID(peer));
196 CFArrayAppendValue(alwaysKeys, retirement_key);
197 CFReleaseNull(retirement_key);
200 CFArrayAppendValue(alwaysKeys, circle_key);
202 CFReleaseNull(circle_key);
207 CFReleaseNull(circle_key);
212 -(bool)kvsAppendRingKeyInterest:(CFMutableArrayRef) alwaysKeys firstUnlock:(CFMutableArrayRef)afterFirstUnlockKeys unlocked:(CFMutableArrayRef) unlockedKeys err:(CFErrorRef *)error
214 if(SOSAccountHasPublicKey(self.account, NULL)){
215 require_quiet(account, fail);
216 SOSAccountTrustClassic *trust = account.trust;
217 SOSCircleRef circle = trust.trustedCircle;
218 require_quiet(circle, fail);
220 // Always interested in backup rings:
221 SOSAccountForEachRingName(account, ^(CFStringRef ringName) {
222 CFStringRef ring_key = SOSRingKeyCreateWithRingName(ringName);
223 CFArrayAppendValue(unlockedKeys, ring_key);
224 CFReleaseNull(ring_key);
232 //register debug scope key
233 -(bool)kvsAppendDebugKeyInterest:(CFMutableArrayRef) alwaysKeys firstUnlock:(CFMutableArrayRef)afterFirstUnlockKeys unlocked:(CFMutableArrayRef) unlockedKeys err:(CFErrorRef *)error
235 CFStringRef key = SOSDebugInfoKeyCreateWithTypeName(kSOSAccountDebugScope);
236 CFArrayAppendValue(alwaysKeys, key);
241 //send debug info over KVS
242 -(bool) kvssendDebugInfo:(CFStringRef) type debug:(CFTypeRef) debugInfo err:(CFErrorRef *)error
244 CFStringRef key = SOSDebugInfoKeyCreateWithTypeName(type);
245 NSDictionary *changes = @{(__bridge NSString*)key:(__bridge id _Nonnull)debugInfo};
248 bool success = SOSTransportCircleKVSUpdateKVS(changes, error);
253 -(bool) kvsSendAccountChangedWithDSID:(CFStringRef) dsid err:(CFErrorRef *)error
255 NSDictionary *changes = @{(__bridge NSString*)kSOSKVSAccountChangedKey:(__bridge NSString*)dsid};
257 bool success = SOSTransportCircleKVSUpdateKVS(changes, error);
263 //send the Ring over KVS
264 -(bool) kvsRingPostRing:(CFStringRef) ringName ring:(CFDataRef) ring err:(CFErrorRef *)error
266 CFStringRef ringKey = SOSRingKeyCreateWithName(ringName, error);
269 [self kvsAddToPendingChanges:ringKey data:ring];
271 CFReleaseNull(ringKey);
276 -(bool) kvsRingFlushChanges:(CFErrorRef*) error
278 return [self kvsSendPendingChanges:error];
281 -(bool) kvsSendOfficialDSID:(CFStringRef) dsid err:(CFErrorRef *)error
283 NSDictionary *changes = @{(__bridge NSString*)kSOSKVSOfficialDSIDKey : (__bridge NSString*)dsid};
284 bool success = SOSTransportCircleKVSUpdateKVS(changes, error);
288 -(bool) postRetirement:(CFStringRef)circleName peer:(SOSPeerInfoRef)peer err:(CFErrorRef *)error
290 CFDataRef retirement_data = SOSPeerInfoCopyEncodedData(peer, kCFAllocatorDefault, error);
292 require_quiet(retirement_data, fail);
294 CFStringRef retirement_key = SOSRetirementKeyCreateWithCircleNameAndPeer((__bridge CFStringRef)(self.circleName), SOSPeerInfoGetPeerID(peer));
296 [self kvsAddToPendingChanges:retirement_key data:retirement_data];
298 CFReleaseNull(retirement_key);
299 CFReleaseNull(retirement_data);
302 CFReleaseNull(retirement_data);