1 #include <Security/SecureObjectSync/SOSTransport.h>
2 #import <Security/SecureObjectSync/SOSTransportMessage.h>
3 #import <Security/SecureObjectSync/SOSTransportMessageKVS.h>
4 #include <Security/SecureObjectSync/SOSKVSKeys.h>
5 #include <utilities/SecCFWrappers.h>
6 #include <SOSInternal.h>
7 #include <AssertMacros.h>
8 #include <SOSCloudKeychainClient.h>
10 @implementation SOSMessageKVS
12 @synthesize pending_changes = pending_changes;
14 -(id) initWithAccount:(SOSAccount*)acct andName:(NSString*)name
20 circleName = [[NSString alloc]initWithString:name];
21 SOSEngineRef e = SOSDataSourceFactoryGetEngineForDataSourceName(acct.factory, (__bridge CFStringRef)(circleName), NULL);
23 pending_changes = CFDictionaryCreateMutableForCFTypes(kCFAllocatorDefault);
24 SOSRegisterTransportMessage((SOSMessage*)self);
30 -(CFIndex) SOSTransportMessageGetTransportType
34 -(CFStringRef) SOSTransportMessageGetCircleName
36 return (__bridge CFStringRef)circleName;
38 -(CFTypeRef) SOSTransportMessageGetEngine
42 -(SOSAccount*) SOSTransportMessageGetAccount
47 -(bool) SOSTransportMessageKVSAppendKeyInterest:(SOSMessageKVS*) transport ak:(CFMutableArrayRef) alwaysKeys firstUnlock:(CFMutableArrayRef) afterFirstUnlockKeys
48 unlocked:(CFMutableArrayRef) unlockedKeys err:(CFErrorRef *)localError
50 require_quiet(engine, fail);
52 CFArrayRef peerInfos = SOSAccountCopyPeersToListenTo( [self SOSTransportMessageGetAccount], localError);
55 NSString* myID = self.account.peerID;
57 CFArrayForEach(peerInfos, ^(const void *value) {
58 CFStringRef peerID = SOSPeerInfoGetPeerID((SOSPeerInfoRef)value);
59 CFStringRef peerMessage = SOSMessageKeyCreateFromPeerToTransport(transport,(__bridge CFStringRef) myID, peerID);
60 if(peerMessage != NULL)
61 CFArrayAppendValue(unlockedKeys, peerMessage);
62 CFReleaseNull(peerMessage);
64 CFReleaseNull(peerInfos);
72 -(CFIndex) SOSTransportMessageGetTransportType:(SOSMessage*) transport err:(CFErrorRef *)error
77 static bool SOSTransportMessageKVSUpdateKVS(SOSMessageKVS* transport, CFDictionaryRef changes, CFErrorRef *error){
79 CloudKeychainReplyBlock log_error = ^(CFDictionaryRef returnedValues __unused, CFErrorRef block_error) {
81 secerror("Error putting: %@", block_error);
85 SOSCloudKeychainPutObjectsInCloud(changes, dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), log_error);
89 static bool SOSTransportMessageKVSSendPendingChanges(SOSMessageKVS* transport, CFErrorRef *error) {
90 CFErrorRef changeError = NULL;
92 if (transport->pending_changes == NULL || CFDictionaryGetCount(transport->pending_changes) == 0) {
93 CFReleaseNull(transport->pending_changes);
96 SOSAccount* acct = [transport SOSTransportMessageGetAccount];
97 CFTypeRef dsid = SOSAccountGetValue(acct, kSOSDSIDKey, error);
102 CFDictionaryAddValue(transport->pending_changes, kSOSKVSRequiredKey, dsid);
104 bool success = SOSTransportMessageKVSUpdateKVS(transport, transport->pending_changes, &changeError);
106 CFDictionaryRemoveAllValues(transport->pending_changes);
108 SOSCreateErrorWithFormat(kSOSErrorSendFailure, changeError, error, NULL,
109 CFSTR("Send changes block failed [%@]"), transport->pending_changes);
115 static void SOSTransportMessageKVSAddToPendingChanges(SOSMessageKVS* transport, CFStringRef message_key, CFDataRef message_data){
116 if (transport.pending_changes == NULL) {
117 transport.pending_changes = CFDictionaryCreateMutableForCFTypes(kCFAllocatorDefault);
119 if (message_data == NULL) {
120 CFDictionarySetValue(transport.pending_changes, message_key, kCFNull);
122 CFDictionarySetValue(transport.pending_changes, message_key, message_data);
126 static bool SOSTransportMessageKVSCleanupAfterPeerMessages(SOSMessageKVS* transport, CFDictionaryRef circle_to_peer_ids, CFErrorRef *error)
128 CFArrayRef enginePeers = SOSEngineGetPeerIDs((SOSEngineRef)[transport SOSTransportMessageGetEngine]);
130 CFDictionaryForEach(circle_to_peer_ids, ^(const void *key, const void *value) {
131 if (isString(key) && isArray(value)) {
132 CFStringRef circle_name = (CFStringRef) key;
133 CFArrayRef peers_to_cleanup_after = (CFArrayRef) value;
135 CFArrayForEach(peers_to_cleanup_after, ^(const void *value) {
136 if (isString(value)) {
137 CFStringRef cleanup_id = (CFStringRef) value;
138 // TODO: Since the enginePeers list is not authorative (the Account is) this could inadvertently clean up active peers or leave behind stale peers
139 if (enginePeers) CFArrayForEach(enginePeers, ^(const void *value) {
140 if (isString(value)) {
141 CFStringRef in_circle_id = (CFStringRef) value;
143 CFStringRef kvsKey = SOSMessageKeyCreateWithCircleNameAndPeerNames(circle_name, cleanup_id, in_circle_id);
144 SOSTransportMessageKVSAddToPendingChanges(transport, kvsKey, NULL);
145 CFReleaseSafe(kvsKey);
147 kvsKey = SOSMessageKeyCreateWithCircleNameAndPeerNames(circle_name, in_circle_id, cleanup_id);
148 SOSTransportMessageKVSAddToPendingChanges(transport, kvsKey, NULL);
149 CFReleaseSafe(kvsKey);
158 return [transport SOSTransportMessageFlushChanges:(SOSMessage*)transport err:error];
161 -(bool) SOSTransportMessageCleanupAfterPeerMessages:(SOSMessage*) transport peers:(CFDictionaryRef) peers err:(CFErrorRef*) error
163 return SOSTransportMessageKVSCleanupAfterPeerMessages((SOSMessageKVS*) transport, peers, error);
166 -(CFDictionaryRef)CF_RETURNS_RETAINED SOSTransportMessageHandlePeerMessageReturnsHandledCopy:(SOSMessage*) transport peerMessages:(CFMutableDictionaryRef) circle_peer_messages_table err:(CFErrorRef *)error
168 CFMutableDictionaryRef handled = CFDictionaryCreateMutableForCFTypes(kCFAllocatorDefault);
169 CFDictionaryRef peerToMessage = CFDictionaryGetValue(circle_peer_messages_table, (__bridge CFStringRef)(transport.circleName));
170 CFMutableArrayRef handled_peers = CFArrayCreateMutableForCFTypes(kCFAllocatorDefault);
173 CFDictionaryForEach(peerToMessage, ^(const void *key, const void *value) {
174 CFStringRef peer_id = asString(key, NULL);
175 CFDataRef peer_message = asData(value, NULL);
176 CFErrorRef localError = NULL;
178 if (peer_id && peer_message && [transport SOSTransportMessageHandlePeerMessage:transport id:peer_id cm:peer_message err:&localError ]) {
179 CFArrayAppendValue(handled_peers, key);
181 secnotice("transport", "%@ KVSTransport handle message failed: %@", peer_id, localError);
183 CFReleaseNull(localError);
186 CFDictionaryAddValue(handled, (__bridge const void *)(transport.circleName), handled_peers);
187 CFReleaseNull(handled_peers);
193 static bool sendToPeer(SOSMessage* transport, CFStringRef circleName, CFStringRef peerID, CFDataRef message, CFErrorRef *error)
195 SOSMessageKVS* kvsTransport = (SOSMessageKVS*) transport;
197 SOSAccount* account = [transport SOSTransportMessageGetAccount];
198 CFTypeRef dsid = SOSAccountGetValue(account, kSOSDSIDKey, error);
202 NSString* myID = account.peerID;
203 CFStringRef message_to_peer_key = SOSMessageKeyCreateFromTransportToPeer(kvsTransport, (__bridge CFStringRef) myID, peerID);
205 CFTypeRef messageToSend = message != NULL ? (CFTypeRef) message : (CFTypeRef) kCFNull;
206 CFDictionaryRef a_message_to_a_peer = CFDictionaryCreateForCFTypes(NULL,
207 message_to_peer_key, messageToSend,
208 kSOSKVSRequiredKey, dsid,
211 if (!SOSTransportMessageKVSUpdateKVS(kvsTransport, a_message_to_a_peer, error)) {
212 secerror("Sync with peers failed to send to %@ [%@], %@", peerID, a_message_to_a_peer, *error);
215 CFReleaseNull(a_message_to_a_peer);
216 CFReleaseNull(message_to_peer_key);
221 -(bool) SOSTransportMessageSyncWithPeers:(SOSMessage*) transport p:(CFSetRef) peers err:(CFErrorRef *)error
223 // Each entry is keyed by circle name and contains a list of peerIDs
224 __block bool result = true;
226 CFSetForEach(peers, ^(const void *value) {
227 CFStringRef peerID = asString(value, NULL);
229 result &= [ transport SOSTransportMessageSendMessageIfNeeded:transport id:(__bridge CFStringRef)(transport.circleName) pID:peerID err:error];
235 -(bool) SOSTransportMessageSendMessages:(SOSMessage*) transport pm:(CFDictionaryRef) peer_messages err:(CFErrorRef *)error
237 __block bool result = true;
239 CFDictionaryForEach(peer_messages, ^(const void *key, const void *value) {
240 CFStringRef peerID = asString(key, NULL);
241 CFDataRef message = asData(value,NULL);
242 if (peerID && message) {
243 bool rx = sendToPeer(transport, (__bridge CFStringRef)(transport.circleName), peerID, message, error);
251 -(bool) SOSTransportMssageFlushChanges:(SOSMessage*) transport err:(CFErrorRef *)error
253 return SOSTransportMessageKVSSendPendingChanges((SOSMessageKVS*) transport, error);