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 <utilities/SecADWrapper.h>
7 #include <SOSInternal.h>
8 #include <AssertMacros.h>
9 #include <SOSCloudKeychainClient.h>
11 @implementation SOSMessageKVS
13 @synthesize pending_changes = pending_changes;
15 -(id) initWithAccount:(SOSAccount*)acct andName:(NSString*)name
21 circleName = [[NSString alloc]initWithString:name];
22 SOSEngineRef e = SOSDataSourceFactoryGetEngineForDataSourceName(acct.factory, (__bridge CFStringRef)(circleName), NULL);
24 pending_changes = CFDictionaryCreateMutableForCFTypes(kCFAllocatorDefault);
25 SOSRegisterTransportMessage((SOSMessage*)self);
31 -(CFIndex) SOSTransportMessageGetTransportType
35 -(CFStringRef) SOSTransportMessageGetCircleName
37 return (__bridge CFStringRef)circleName;
39 -(CFTypeRef) SOSTransportMessageGetEngine
43 -(SOSAccount*) SOSTransportMessageGetAccount
48 -(bool) SOSTransportMessageKVSAppendKeyInterest:(SOSMessageKVS*) transport ak:(CFMutableArrayRef) alwaysKeys firstUnlock:(CFMutableArrayRef) afterFirstUnlockKeys
49 unlocked:(CFMutableArrayRef) unlockedKeys err:(CFErrorRef *)localError
51 require_quiet(engine, fail);
53 CFArrayRef peerInfos = SOSAccountCopyPeersToListenTo( [self SOSTransportMessageGetAccount], localError);
56 NSString* myID = self.account.peerID;
58 CFArrayForEach(peerInfos, ^(const void *value) {
59 CFStringRef peerID = SOSPeerInfoGetPeerID((SOSPeerInfoRef)value);
60 CFStringRef peerMessage = SOSMessageKeyCreateFromPeerToTransport(transport,(__bridge CFStringRef) myID, peerID);
61 if(peerMessage != NULL)
62 CFArrayAppendValue(unlockedKeys, peerMessage);
63 CFReleaseNull(peerMessage);
65 CFReleaseNull(peerInfos);
73 -(CFIndex) SOSTransportMessageGetTransportType:(SOSMessage*) transport err:(CFErrorRef *)error
78 static bool SOSTransportMessageKVSUpdateKVS(SOSMessageKVS* transport, CFDictionaryRef changes, CFErrorRef *error){
80 SecADAddValueForScalarKey(CFSTR("com.apple.security.sos.sendkvs"), 1);
82 CloudKeychainReplyBlock log_error = ^(CFDictionaryRef returnedValues __unused, CFErrorRef block_error) {
84 secerror("Error putting: %@", block_error);
88 SOSCloudKeychainPutObjectsInCloud(changes, dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), log_error);
92 static bool SOSTransportMessageKVSSendPendingChanges(SOSMessageKVS* transport, CFErrorRef *error) {
93 CFErrorRef changeError = NULL;
95 if (transport->pending_changes == NULL || CFDictionaryGetCount(transport->pending_changes) == 0) {
96 CFReleaseNull(transport->pending_changes);
99 SOSAccount* acct = [transport SOSTransportMessageGetAccount];
100 CFTypeRef dsid = SOSAccountGetValue(acct, kSOSDSIDKey, error);
105 CFDictionaryAddValue(transport->pending_changes, kSOSKVSRequiredKey, dsid);
107 bool success = SOSTransportMessageKVSUpdateKVS(transport, transport->pending_changes, &changeError);
109 CFDictionaryRemoveAllValues(transport->pending_changes);
111 SOSCreateErrorWithFormat(kSOSErrorSendFailure, changeError, error, NULL,
112 CFSTR("Send changes block failed [%@]"), transport->pending_changes);
118 static void SOSTransportMessageKVSAddToPendingChanges(SOSMessageKVS* transport, CFStringRef message_key, CFDataRef message_data){
119 if (transport.pending_changes == NULL) {
120 transport.pending_changes = CFDictionaryCreateMutableForCFTypes(kCFAllocatorDefault);
122 if (message_data == NULL) {
123 CFDictionarySetValue(transport.pending_changes, message_key, kCFNull);
125 CFDictionarySetValue(transport.pending_changes, message_key, message_data);
129 static bool SOSTransportMessageKVSCleanupAfterPeerMessages(SOSMessageKVS* transport, CFDictionaryRef circle_to_peer_ids, CFErrorRef *error)
131 CFArrayRef enginePeers = SOSEngineGetPeerIDs((SOSEngineRef)[transport SOSTransportMessageGetEngine]);
133 CFDictionaryForEach(circle_to_peer_ids, ^(const void *key, const void *value) {
134 if (isString(key) && isArray(value)) {
135 CFStringRef circle_name = (CFStringRef) key;
136 CFArrayRef peers_to_cleanup_after = (CFArrayRef) value;
138 CFArrayForEach(peers_to_cleanup_after, ^(const void *value) {
139 if (isString(value)) {
140 CFStringRef cleanup_id = (CFStringRef) value;
141 // TODO: Since the enginePeers list is not authorative (the Account is) this could inadvertently clean up active peers or leave behind stale peers
142 if (enginePeers) CFArrayForEach(enginePeers, ^(const void *value) {
143 if (isString(value)) {
144 CFStringRef in_circle_id = (CFStringRef) value;
146 CFStringRef kvsKey = SOSMessageKeyCreateWithCircleNameAndPeerNames(circle_name, cleanup_id, in_circle_id);
147 SOSTransportMessageKVSAddToPendingChanges(transport, kvsKey, NULL);
148 CFReleaseSafe(kvsKey);
150 kvsKey = SOSMessageKeyCreateWithCircleNameAndPeerNames(circle_name, in_circle_id, cleanup_id);
151 SOSTransportMessageKVSAddToPendingChanges(transport, kvsKey, NULL);
152 CFReleaseSafe(kvsKey);
161 return [transport SOSTransportMessageFlushChanges:(SOSMessage*)transport err:error];
164 -(bool) SOSTransportMessageCleanupAfterPeerMessages:(SOSMessage*) transport peers:(CFDictionaryRef) peers err:(CFErrorRef*) error
166 return SOSTransportMessageKVSCleanupAfterPeerMessages((SOSMessageKVS*) transport, peers, error);
169 -(CFDictionaryRef)CF_RETURNS_RETAINED SOSTransportMessageHandlePeerMessageReturnsHandledCopy:(SOSMessage*) transport peerMessages:(CFMutableDictionaryRef) circle_peer_messages_table err:(CFErrorRef *)error
171 CFMutableDictionaryRef handled = CFDictionaryCreateMutableForCFTypes(kCFAllocatorDefault);
172 CFDictionaryRef peerToMessage = CFDictionaryGetValue(circle_peer_messages_table, (__bridge CFStringRef)(transport.circleName));
173 CFMutableArrayRef handled_peers = CFArrayCreateMutableForCFTypes(kCFAllocatorDefault);
176 CFDictionaryForEach(peerToMessage, ^(const void *key, const void *value) {
177 CFStringRef peer_id = asString(key, NULL);
178 CFDataRef peer_message = asData(value, NULL);
179 CFErrorRef localError = NULL;
181 if (peer_id && peer_message && [transport SOSTransportMessageHandlePeerMessage:transport id:peer_id cm:peer_message err:&localError ]) {
182 CFArrayAppendValue(handled_peers, key);
184 secnotice("transport", "%@ KVSTransport handle message failed: %@", peer_id, localError);
186 CFReleaseNull(localError);
189 CFDictionaryAddValue(handled, (__bridge const void *)(transport.circleName), handled_peers);
190 CFReleaseNull(handled_peers);
196 static bool sendToPeer(SOSMessage* transport, CFStringRef circleName, CFStringRef peerID, CFDataRef message, CFErrorRef *error)
198 SOSMessageKVS* kvsTransport = (SOSMessageKVS*) transport;
200 SOSAccount* account = [transport SOSTransportMessageGetAccount];
201 CFTypeRef dsid = SOSAccountGetValue(account, kSOSDSIDKey, error);
205 NSString* myID = account.peerID;
206 CFStringRef message_to_peer_key = SOSMessageKeyCreateFromTransportToPeer(kvsTransport, (__bridge CFStringRef) myID, peerID);
208 CFTypeRef messageToSend = message != NULL ? (CFTypeRef) message : (CFTypeRef) kCFNull;
209 CFDictionaryRef a_message_to_a_peer = CFDictionaryCreateForCFTypes(NULL,
210 message_to_peer_key, messageToSend,
211 kSOSKVSRequiredKey, dsid,
214 if (!SOSTransportMessageKVSUpdateKVS(kvsTransport, a_message_to_a_peer, error)) {
215 secerror("Sync with peers failed to send to %@ [%@], %@", peerID, a_message_to_a_peer, *error);
218 CFReleaseNull(a_message_to_a_peer);
219 CFReleaseNull(message_to_peer_key);
224 -(bool) SOSTransportMessageSyncWithPeers:(SOSMessage*) transport p:(CFSetRef) peers err:(CFErrorRef *)error
226 // Each entry is keyed by circle name and contains a list of peerIDs
227 __block bool result = true;
229 CFSetForEach(peers, ^(const void *value) {
230 CFStringRef peerID = asString(value, NULL);
232 result &= [ transport SOSTransportMessageSendMessageIfNeeded:transport id:(__bridge CFStringRef)(transport.circleName) pID:peerID err:error];
238 -(bool) SOSTransportMessageSendMessages:(SOSMessage*) transport pm:(CFDictionaryRef) peer_messages err:(CFErrorRef *)error
240 __block bool result = true;
242 CFDictionaryForEach(peer_messages, ^(const void *key, const void *value) {
243 CFStringRef peerID = asString(key, NULL);
244 CFDataRef message = asData(value,NULL);
245 if (peerID && message) {
246 bool rx = sendToPeer(transport, (__bridge CFStringRef)(transport.circleName), peerID, message, error);
254 -(bool) SOSTransportMssageFlushChanges:(SOSMessage*) transport err:(CFErrorRef *)error
256 return SOSTransportMessageKVSSendPendingChanges((SOSMessageKVS*) transport, error);