]> git.saurik.com Git - apple/security.git/blob - OSX/sec/ProjectHeaders/Security/SecureObjectSync/SOSAccountPeers.c
Security-57336.1.9.tar.gz
[apple/security.git] / OSX / sec / ProjectHeaders / Security / 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 SOSAccountCopyApplicants(SOSAccountRef account, CFErrorRef *error) {
126 return SOSAccountCopySortedPeerArray(account, error, ^(SOSCircleRef circle, CFMutableArrayRef appendPeersTo) {
127 SOSCircleForEachApplicant(circle, ^(SOSPeerInfoRef peer) {
128 sosArrayAppendPeerCopy(appendPeersTo, peer);
129 });
130 });
131 }
132
133 CFArrayRef SOSAccountCopyPeers(SOSAccountRef account, CFErrorRef *error) {
134 return SOSAccountCopySortedPeerArray(account, error, ^(SOSCircleRef circle, CFMutableArrayRef appendPeersTo) {
135 SOSCircleForEachPeer(circle, ^(SOSPeerInfoRef peer) {
136 sosArrayAppendPeerCopy(appendPeersTo, peer);
137 });
138 });
139 }
140
141 CFArrayRef SOSAccountCopyActivePeers(SOSAccountRef account, CFErrorRef *error) {
142 return SOSAccountCopySortedPeerArray(account, error, ^(SOSCircleRef circle, CFMutableArrayRef appendPeersTo) {
143 SOSCircleForEachActivePeer(circle, ^(SOSPeerInfoRef peer) {
144 sosArrayAppendPeerCopy(appendPeersTo, peer);
145 });
146 });
147 }
148
149 CFArrayRef SOSAccountCopyActiveValidPeers(SOSAccountRef account, CFErrorRef *error) {
150 return SOSAccountCopySortedPeerArray(account, error, ^(SOSCircleRef circle, CFMutableArrayRef appendPeersTo) {
151 SOSCircleForEachActiveValidPeer(circle, account->user_public, ^(SOSPeerInfoRef peer) {
152 sosArrayAppendPeerCopy(appendPeersTo, peer);
153 });
154 });
155 }
156
157 CFArrayRef SOSAccountCopyConcurringPeers(SOSAccountRef account, CFErrorRef *error)
158 {
159 return SOSAccountCopySortedPeerArray(account, error, ^(SOSCircleRef circle, CFMutableArrayRef appendPeersTo) {
160 SOSCircleAppendConcurringPeers(circle, appendPeersTo, NULL);
161 });
162 }
163
164 SOSPeerInfoRef SOSAccountCopyPeerWithID(SOSAccountRef account, CFStringRef peerid, CFErrorRef *error) {
165 if(!account->trusted_circle) return NULL;
166 return SOSCircleCopyPeerWithID(account->trusted_circle, peerid, error);
167 }