]> git.saurik.com Git - apple/security.git/blob - keychain/SecureObjectSync/SOSAccountPeers.m
Security-59306.101.1.tar.gz
[apple/security.git] / keychain / SecureObjectSync / SOSAccountPeers.m
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 #include "SOSAccountPriv.h"
25 #include "SOSAccount.h"
26 #include "keychain/SecureObjectSync/SOSPeerInfoCollections.h"
27 #include "keychain/SecureObjectSync/SOSTransportMessage.h"
28 #include "keychain/SecureObjectSync/SOSPeerInfoV2.h"
29 #import "keychain/SecureObjectSync/SOSAccountTrust.h"
30 #include "keychain/SecureObjectSync/SOSAccountTrustClassic+Circle.h"
31
32 bool SOSAccountIsMyPeerActive(SOSAccount* account, CFErrorRef* error) {
33 SOSFullPeerInfoRef identity = NULL;
34 SOSCircleRef circle = NULL;
35
36 SOSAccountTrustClassic *trust = account.trust;
37 identity = trust.fullPeerInfo;
38 circle = trust.trustedCircle;
39
40 SOSPeerInfoRef me = SOSFullPeerInfoGetPeerInfo(identity);
41 return me ? SOSCircleHasActivePeer(circle, me, error) : false;
42 }
43
44 //
45 // MARK: Peer Querying
46 //
47
48
49 static void sosArrayAppendPeerCopy(CFMutableArrayRef appendPeersTo, SOSPeerInfoRef peer) {
50 SOSPeerInfoRef peerInfo = SOSPeerInfoCreateCopy(kCFAllocatorDefault, peer, NULL);
51 CFArrayAppendValue(appendPeersTo, peerInfo);
52 CFRelease(peerInfo);
53 }
54
55 static CFArrayRef SOSAccountCopySortedPeerArray(SOSAccount* account,
56 CFErrorRef *error,
57 void (^action)(SOSCircleRef circle, CFMutableArrayRef appendPeersTo)) {
58 if (!SOSAccountHasPublicKey(account, error))
59 return NULL;
60
61 CFMutableArrayRef peers = CFArrayCreateMutableForCFTypes(kCFAllocatorDefault);
62 SOSCircleRef circle = NULL;
63
64 SOSAccountTrustClassic *trust = account.trust;
65 circle = trust.trustedCircle;
66 action(circle, peers);
67
68 CFArrayOfSOSPeerInfosSortByID(peers);
69
70 return peers;
71 }
72
73
74 CFArrayRef SOSAccountCopyNotValidPeers(SOSAccount* account, CFErrorRef *error) {
75 return SOSAccountCopySortedPeerArray(account, error, ^(SOSCircleRef circle, CFMutableArrayRef appendPeersTo) {
76 SOSCircleForEachPeer(circle, ^(SOSPeerInfoRef peer) {
77 if(!SOSPeerInfoApplicationVerify(peer, account.accountKey, NULL)) {
78 sosArrayAppendPeerCopy(appendPeersTo, peer);
79 }
80 });
81 });
82 }
83
84
85 CFArrayRef SOSAccountCopyValidPeers(SOSAccount* account, CFErrorRef *error) {
86 return SOSAccountCopySortedPeerArray(account, error, ^(SOSCircleRef circle, CFMutableArrayRef appendPeersTo) {
87 SOSCircleForEachPeer(circle, ^(SOSPeerInfoRef peer) {
88 if(SOSPeerInfoApplicationVerify(peer, account.accountKey, NULL)) {
89 sosArrayAppendPeerCopy(appendPeersTo, peer);
90 }
91 });
92 });
93 }
94
95
96
97 CFArrayRef SOSAccountCopyPeersToListenTo(SOSAccount* account, CFErrorRef *error) {
98 SOSFullPeerInfoRef identity = NULL;
99
100 SOSAccountTrustClassic *trust = account.trust;
101 identity = trust.fullPeerInfo;
102 SOSPeerInfoRef myPeerInfo = SOSFullPeerInfoGetPeerInfo(identity);
103 CFStringRef myID = myPeerInfo ? SOSPeerInfoGetPeerID(myPeerInfo) : NULL;
104 return SOSAccountCopySortedPeerArray(account, error, ^(SOSCircleRef circle, CFMutableArrayRef appendPeersTo) {
105 SOSCircleForEachPeer(circle, ^(SOSPeerInfoRef peer) {
106 if(!CFEqualSafe(myID, SOSPeerInfoGetPeerID(peer)) &&
107 SOSPeerInfoApplicationVerify(peer, account.accountKey, NULL) &&
108 !SOSPeerInfoIsRetirementTicket(peer)) {
109 CFArrayAppendValue(appendPeersTo, peer);
110 }
111 });
112 });
113 }
114
115 CFArrayRef SOSAccountCopyRetired(SOSAccount* account, CFErrorRef *error) {
116 return SOSAccountCopySortedPeerArray(account, error, ^(SOSCircleRef circle, CFMutableArrayRef appendPeersTo) {
117 SOSCircleForEachRetiredPeer(circle, ^(SOSPeerInfoRef peer) {
118 sosArrayAppendPeerCopy(appendPeersTo, peer);
119 });
120 });
121 }
122
123 CFArrayRef SOSAccountCopyViewUnaware(SOSAccount* account, CFErrorRef *error) {
124 return SOSAccountCopySortedPeerArray(account, error, ^(SOSCircleRef circle, CFMutableArrayRef appendPeersTo) {
125 SOSCircleForEachPeer(circle, ^(SOSPeerInfoRef peer) {
126 if (!SOSPeerInfoVersionHasV2Data(peer) ) {
127 sosArrayAppendPeerCopy(appendPeersTo, peer);
128 } else {
129 CFSetRef peerEnabledViews = SOSPeerInfoCopyEnabledViews(peer);
130 CFSetRef enabledV0Views = CFSetCreateIntersection(kCFAllocatorDefault, peerEnabledViews, SOSViewsGetV0ViewSet());
131 if(CFSetGetCount(enabledV0Views) != 0) {
132 sosArrayAppendPeerCopy(appendPeersTo, peer);
133 }
134 CFReleaseNull(peerEnabledViews);
135 CFReleaseNull(enabledV0Views);
136 }
137 });
138 });
139 }
140
141 CFArrayRef SOSAccountCopyApplicants(SOSAccount* account, CFErrorRef *error) {
142 return SOSAccountCopySortedPeerArray(account, error, ^(SOSCircleRef circle, CFMutableArrayRef appendPeersTo) {
143 SOSCircleForEachApplicant(circle, ^(SOSPeerInfoRef peer) {
144 sosArrayAppendPeerCopy(appendPeersTo, peer);
145 });
146 });
147 }
148
149 CFArrayRef SOSAccountCopyPeers(SOSAccount* account, CFErrorRef *error) {
150 return SOSAccountCopySortedPeerArray(account, error, ^(SOSCircleRef circle, CFMutableArrayRef appendPeersTo) {
151 SOSCircleForEachPeer(circle, ^(SOSPeerInfoRef peer) {
152 sosArrayAppendPeerCopy(appendPeersTo, peer);
153 });
154 });
155 }
156
157 CFArrayRef SOSAccountCopyActivePeers(SOSAccount* account, CFErrorRef *error) {
158 return SOSAccountCopySortedPeerArray(account, error, ^(SOSCircleRef circle, CFMutableArrayRef appendPeersTo) {
159 SOSCircleForEachActivePeer(circle, ^(SOSPeerInfoRef peer) {
160 sosArrayAppendPeerCopy(appendPeersTo, peer);
161 });
162 });
163 }
164
165 CFArrayRef CF_RETURNS_RETAINED SOSAccountCopyActiveValidPeers(SOSAccount* account, CFErrorRef *error) {
166 return SOSAccountCopySortedPeerArray(account, error, ^(SOSCircleRef circle, CFMutableArrayRef appendPeersTo) {
167 SOSCircleForEachActiveValidPeer(circle, account.accountKey, ^(SOSPeerInfoRef peer) {
168 sosArrayAppendPeerCopy(appendPeersTo, peer);
169 });
170 });
171 }
172
173 CFArrayRef SOSAccountCopyConcurringPeers(SOSAccount* account, CFErrorRef *error)
174 {
175 return SOSAccountCopySortedPeerArray(account, error, ^(SOSCircleRef circle, CFMutableArrayRef appendPeersTo) {
176 SOSCircleAppendConcurringPeers(circle, appendPeersTo, NULL);
177 });
178 }
179
180 SOSPeerInfoRef SOSAccountCopyPeerWithID(SOSAccount* account, CFStringRef peerid, CFErrorRef *error) {
181 SOSCircleRef circle = NULL;
182
183 SOSAccountTrustClassic *trust = account.trust;
184 circle = trust.trustedCircle;
185 if(!circle) return NULL;
186 return SOSCircleCopyPeerWithID(circle, peerid, error);
187 }
188
189 CFBooleanRef SOSAccountPeersHaveViewsEnabled(SOSAccount* account, CFArrayRef viewNames, CFErrorRef *error) {
190 CFBooleanRef result = NULL;
191 CFMutableSetRef viewsRemaining = NULL;
192 CFSetRef viewsToLookFor = NULL;
193
194 if(![account isInCircle:error]) {
195 CFReleaseNull(viewsToLookFor);
196 CFReleaseNull(viewsRemaining);
197 return result;
198 }
199
200 viewsToLookFor = CFSetCreateCopyOfArrayForCFTypes(viewNames);
201 viewsRemaining = CFSetCreateMutableCopy(kCFAllocatorDefault, 0, viewsToLookFor);
202 CFReleaseNull(viewsToLookFor);
203
204 SOSAccountForEachCirclePeerExceptMe(account, ^(SOSPeerInfoRef peer) {
205 if (SOSPeerInfoApplicationVerify(peer, account.accountKey, NULL)) {
206 CFSetRef peerViews = SOSPeerInfoCopyEnabledViews(peer);
207 CFSetSubtract(viewsRemaining, peerViews);
208 CFReleaseNull(peerViews);
209 }
210 });
211
212 result = CFSetIsEmpty(viewsRemaining) ? kCFBooleanTrue : kCFBooleanFalse;
213
214 CFReleaseNull(viewsToLookFor);
215 CFReleaseNull(viewsRemaining);
216
217 return result;
218 }
219