]> git.saurik.com Git - apple/security.git/blob - OSX/sec/SOSCircle/SecureObjectSync/SOSAccountPeers.c
Security-57336.10.29.tar.gz
[apple/security.git] / OSX / sec / SOSCircle / SecureObjectSync / SOSAccountPeers.c
1 /*
2 * Copyright (c) 2013-2014 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
25 #include <stdio.h>
26 #include "SOSAccountPriv.h"
27 #include <Security/SecureObjectSync/SOSPeerInfoCollections.h>
28 #include <Security/SecureObjectSync/SOSTransportMessage.h>
29 #include <Security/SecureObjectSync/SOSPeerInfoV2.h>
30
31
32 bool SOSAccountIsMyPeerActive(SOSAccountRef account, CFErrorRef* error) {
33 SOSPeerInfoRef me = SOSFullPeerInfoGetPeerInfo(account->my_identity);
34 return me ? SOSCircleHasActivePeer(account->trusted_circle, me, error) : false;
35 }
36
37 SOSTransportMessageRef SOSAccountGetMessageTransportFor(SOSAccountRef account, SOSPeerInfoRef peerInfo) {
38 // Returns the transport to use to talk to that peer. if he's a member of the circle.
39 return (SOSPeerInfoHasDeviceID(peerInfo) && (false)) ? account->ids_message_transport : account->kvs_message_transport;
40 }
41
42 //
43 // MARK: Peer Querying
44 //
45
46
47 static void sosArrayAppendPeerCopy(CFMutableArrayRef appendPeersTo, SOSPeerInfoRef peer) {
48 SOSPeerInfoRef peerInfo = SOSPeerInfoCreateCopy(kCFAllocatorDefault, peer, NULL);
49 CFArrayAppendValue(appendPeersTo, peerInfo);
50 CFRelease(peerInfo);
51 }
52
53 static CFArrayRef SOSAccountCopySortedPeerArray(SOSAccountRef account,
54 CFErrorRef *error,
55 void (^action)(SOSCircleRef circle, CFMutableArrayRef appendPeersTo)) {
56 if (!SOSAccountHasPublicKey(account, error))
57 return NULL;
58
59 CFMutableArrayRef peers = CFArrayCreateMutableForCFTypes(kCFAllocatorDefault);
60
61 action(account->trusted_circle, peers);
62
63 CFArrayOfSOSPeerInfosSortByID(peers);
64
65 return peers;
66 }
67
68
69 CFArrayRef SOSAccountCopyNotValidPeers(SOSAccountRef account, CFErrorRef *error) {
70 return SOSAccountCopySortedPeerArray(account, error, ^(SOSCircleRef circle, CFMutableArrayRef appendPeersTo) {
71 SOSCircleForEachPeer(circle, ^(SOSPeerInfoRef peer) {
72 if(!SOSPeerInfoApplicationVerify(peer, account->user_public, NULL)) {
73 sosArrayAppendPeerCopy(appendPeersTo, peer);
74 }
75 });
76 });
77 }
78
79
80 CFArrayRef SOSAccountCopyValidPeers(SOSAccountRef account, CFErrorRef *error) {
81 return SOSAccountCopySortedPeerArray(account, error, ^(SOSCircleRef circle, CFMutableArrayRef appendPeersTo) {
82 SOSCircleForEachPeer(circle, ^(SOSPeerInfoRef peer) {
83 if(SOSPeerInfoApplicationVerify(peer, account->user_public, NULL)) {
84 sosArrayAppendPeerCopy(appendPeersTo, peer);
85 }
86 });
87 });
88 }
89
90 void SOSAccountForEachCirclePeerExceptMe(SOSAccountRef account, void (^action)(SOSPeerInfoRef peer)) {
91 SOSPeerInfoRef myPi = SOSAccountGetMyPeerInfo(account);
92 if (account->trusted_circle && myPi) {
93 CFStringRef myPi_id = SOSPeerInfoGetPeerID(myPi);
94 SOSCircleForEachPeer(account->trusted_circle, ^(SOSPeerInfoRef peer) {
95 CFStringRef peerID = SOSPeerInfoGetPeerID(peer);
96 if (peerID && !CFEqual(peerID, myPi_id)) {
97 action(peer);
98 }
99 });
100 }
101 }
102
103 CFArrayRef SOSAccountCopyPeersToListenTo(SOSAccountRef account, CFErrorRef *error) {
104 SOSPeerInfoRef myPeerInfo = SOSFullPeerInfoGetPeerInfo(account->my_identity);
105 CFStringRef myID = myPeerInfo ? SOSPeerInfoGetPeerID(myPeerInfo) : NULL;
106 return SOSAccountCopySortedPeerArray(account, error, ^(SOSCircleRef circle, CFMutableArrayRef appendPeersTo) {
107 SOSCircleForEachPeer(circle, ^(SOSPeerInfoRef peer) {
108 if(!CFEqualSafe(myID, SOSPeerInfoGetPeerID(peer)) &&
109 SOSPeerInfoApplicationVerify(peer, account->user_public, NULL) &&
110 !SOSPeerInfoIsRetirementTicket(peer)) {
111 CFArrayAppendValue(appendPeersTo, peer);
112 }
113 });
114 });
115 }
116
117 CFArrayRef SOSAccountCopyRetired(SOSAccountRef account, CFErrorRef *error) {
118 return SOSAccountCopySortedPeerArray(account, error, ^(SOSCircleRef circle, CFMutableArrayRef appendPeersTo) {
119 SOSCircleForEachRetiredPeer(circle, ^(SOSPeerInfoRef peer) {
120 sosArrayAppendPeerCopy(appendPeersTo, peer);
121 });
122 });
123 }
124
125 CFArrayRef SOSAccountCopyViewUnaware(SOSAccountRef account, CFErrorRef *error) {
126 return SOSAccountCopySortedPeerArray(account, error, ^(SOSCircleRef circle, CFMutableArrayRef appendPeersTo) {
127 SOSCircleForEachPeer(circle, ^(SOSPeerInfoRef peer) {
128 if (!SOSPeerInfoVersionHasV2Data(peer)) {
129 sosArrayAppendPeerCopy(appendPeersTo, peer);
130 }
131 });
132 });
133 }
134
135 CFArrayRef SOSAccountCopyApplicants(SOSAccountRef account, CFErrorRef *error) {
136 return SOSAccountCopySortedPeerArray(account, error, ^(SOSCircleRef circle, CFMutableArrayRef appendPeersTo) {
137 SOSCircleForEachApplicant(circle, ^(SOSPeerInfoRef peer) {
138 sosArrayAppendPeerCopy(appendPeersTo, peer);
139 });
140 });
141 }
142
143 CFArrayRef SOSAccountCopyPeers(SOSAccountRef account, CFErrorRef *error) {
144 return SOSAccountCopySortedPeerArray(account, error, ^(SOSCircleRef circle, CFMutableArrayRef appendPeersTo) {
145 SOSCircleForEachPeer(circle, ^(SOSPeerInfoRef peer) {
146 sosArrayAppendPeerCopy(appendPeersTo, peer);
147 });
148 });
149 }
150
151 CFArrayRef SOSAccountCopyActivePeers(SOSAccountRef account, CFErrorRef *error) {
152 return SOSAccountCopySortedPeerArray(account, error, ^(SOSCircleRef circle, CFMutableArrayRef appendPeersTo) {
153 SOSCircleForEachActivePeer(circle, ^(SOSPeerInfoRef peer) {
154 sosArrayAppendPeerCopy(appendPeersTo, peer);
155 });
156 });
157 }
158
159 CFArrayRef SOSAccountCopyActiveValidPeers(SOSAccountRef account, CFErrorRef *error) {
160 return SOSAccountCopySortedPeerArray(account, error, ^(SOSCircleRef circle, CFMutableArrayRef appendPeersTo) {
161 SOSCircleForEachActiveValidPeer(circle, account->user_public, ^(SOSPeerInfoRef peer) {
162 sosArrayAppendPeerCopy(appendPeersTo, peer);
163 });
164 });
165 }
166
167 CFArrayRef SOSAccountCopyConcurringPeers(SOSAccountRef account, CFErrorRef *error)
168 {
169 return SOSAccountCopySortedPeerArray(account, error, ^(SOSCircleRef circle, CFMutableArrayRef appendPeersTo) {
170 SOSCircleAppendConcurringPeers(circle, appendPeersTo, NULL);
171 });
172 }
173
174 SOSPeerInfoRef SOSAccountCopyPeerWithID(SOSAccountRef account, CFStringRef peerid, CFErrorRef *error) {
175 if(!account->trusted_circle) return NULL;
176 return SOSCircleCopyPeerWithID(account->trusted_circle, peerid, error);
177 }