]> git.saurik.com Git - apple/security.git/blob - OSX/sec/SOSCircle/SecureObjectSync/SOSViews.m
Security-58286.51.6.tar.gz
[apple/security.git] / OSX / sec / SOSCircle / SecureObjectSync / SOSViews.m
1 /*
2 * Copyright (c) 2015 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 * SOSViews.c - Implementation of views
26 */
27
28 #include <AssertMacros.h>
29 #include <TargetConditionals.h>
30
31 #include "SOSViews.h"
32 #include <utilities/SecCFWrappers.h>
33 #include <utilities/SecCFRelease.h>
34 #include <utilities/SecXPCError.h>
35
36 #include <utilities/SecCFError.h>
37 #include <utilities/der_set.h>
38 #include <Security/SecureObjectSync/SOSInternal.h>
39
40 #include <Security/SecureObjectSync/SOSPeerInfo.h>
41 #include <Security/SecureObjectSync/SOSPeerInfoV2.h>
42 #include <Security/SecureObjectSync/SOSPeerInfoPriv.h>
43 #include <Security/SecureObjectSync/SOSCloudCircle.h>
44
45 #include <utilities/array_size.h>
46 #include <Security/SecureObjectSync/SOSAccount.h>
47 #include <Security/SecureObjectSync/SOSAccountPriv.h>
48
49 #define viewMemError CFSTR("Failed to get memory for views in PeerInfo")
50 #define viewUnknownError CFSTR("Unknown view(%@) (ViewResultCode=%d)")
51 #define viewInvalidError CFSTR("Peer is invalid for this view(%@) (ViewResultCode=%d)")
52
53 // Internal Views:
54 const CFStringRef kSOSViewKeychainV0_tomb = CFSTR("KeychainV0-tomb"); // iCloud Keychain backup for v0 peers (no tombstones)
55 const CFStringRef kSOSViewBackupBagV0_tomb = CFSTR("BackupBagV0-tomb"); // iCloud Keychain backup bag for v0 peers (no tombstones)
56 const CFStringRef kSOSViewWiFi_tomb = CFSTR("WiFi-tomb");
57 const CFStringRef kSOSViewAutofillPasswords_tomb = CFSTR("Passwords-tomb");
58 const CFStringRef kSOSViewSafariCreditCards_tomb = CFSTR("CreditCards-tomb");
59 const CFStringRef kSOSViewiCloudIdentity_tomb = CFSTR("iCloudIdentity-tomb");
60 const CFStringRef kSOSViewOtherSyncable_tomb = CFSTR("OtherSyncable-tomb");
61
62 // Views
63 const CFStringRef kSOSViewKeychainV0 = CFSTR("KeychainV0"); // iCloud Keychain syncing for v0 peers
64
65 #undef DOVIEWMACRO
66 #define DOVIEWMACRO(VIEWNAME, DEFSTRING, CMDSTRING, SYSTEM, DEFAULTSETTING, INITIALSYNCSETTING, ALWAYSONSETTING, BACKUPSETTING, V0SETTING) \
67 const CFStringRef k##SYSTEM##View##VIEWNAME = CFSTR(DEFSTRING);
68 #include "Security/SecureObjectSync/ViewList.list"
69
70 // View Hints
71 // Note that by definition, there cannot be a V0 view hint
72 // These will be deprecated for new constants found in SecItemPriv.h
73 const CFStringRef kSOSViewHintPCSMasterKey = CFSTR("PCS-MasterKey");
74 const CFStringRef kSOSViewHintPCSiCloudDrive = CFSTR("PCS-iCloudDrive");
75 const CFStringRef kSOSViewHintPCSPhotos = CFSTR("PCS-Photos");
76 const CFStringRef kSOSViewHintPCSCloudKit = CFSTR("PCS-CloudKit");
77 const CFStringRef kSOSViewHintPCSEscrow = CFSTR("PCS-Escrow");
78 const CFStringRef kSOSViewHintPCSFDE = CFSTR("PCS-FDE");
79 const CFStringRef kSOSViewHintPCSMailDrop = CFSTR("PCS-Maildrop");
80 const CFStringRef kSOSViewHintPCSiCloudBackup = CFSTR("PCS-Backup");
81 const CFStringRef kSOSViewHintPCSNotes = CFSTR("PCS-Notes");
82 const CFStringRef kSOSViewHintPCSiMessage = CFSTR("PCS-iMessage");
83 const CFStringRef kSOSViewHintPCSFeldspar = CFSTR("PCS-Feldspar");
84
85 const CFStringRef kSOSViewHintAppleTV = CFSTR("AppleTV");
86 const CFStringRef kSOSViewHintHomeKit = CFSTR("HomeKit");
87
88 CFMutableSetRef SOSViewCopyViewSet(ViewSetKind setKind) {
89 CFMutableSetRef result = CFSetCreateMutableForCFTypes(kCFAllocatorDefault);
90
91 // Only return views in the SOS system, unless they asked for kViewSetCKKS
92 #undef DOVIEWMACRO
93 #define __TYPE_MEMBER_ false
94 #define __TYPE_MEMBER_D true
95 #define __TYPE_MEMBER_I true
96 #define __TYPE_MEMBER_A true
97 #define __TYPE_MEMBER_V true
98 #define __TYPE_MEMBER_B true
99 #define __SYSTEM_SOS true
100 #define __SYSTEM_CKKS false
101
102 #define DOVIEWMACRO_SOS(VIEWNAME, DEFSTRING, CMDSTRING, SYSTEM, DEFAULT, INITIAL, ALWAYSON, BACKUP, V0) \
103 if(((setKind == kViewSetAll) || \
104 ((setKind == kViewSetDefault) && __TYPE_MEMBER_##DEFAULT) || \
105 ((setKind == kViewSetInitial) && __TYPE_MEMBER_##INITIAL) || \
106 ((setKind == kViewSetAlwaysOn) && __TYPE_MEMBER_##ALWAYSON) || \
107 ((setKind == kViewSetRequiredForBackup) && __TYPE_MEMBER_##BACKUP) || \
108 ((setKind == kViewSetV0) && __TYPE_MEMBER_##V0) )) { \
109 CFSetAddValue(result, k##SYSTEM##View##VIEWNAME); \
110 }
111
112 #define DOVIEWMACRO_CKKS(VIEWNAME, DEFSTRING, CMDSTRING, SYSTEM, DEFAULT, INITIAL, ALWAYSON, BACKUP, V0) \
113 if(setKind == kViewSetCKKS) { \
114 CFSetAddValue(result, k##SYSTEM##View##VIEWNAME); \
115 }
116
117 #define DOVIEWMACRO(VIEWNAME, DEFSTRING, CMDSTRING, SYSTEM, DEFAULT, INITIAL, ALWAYSON, BACKUP, V0) \
118 DOVIEWMACRO_##SYSTEM(VIEWNAME, DEFSTRING, CMDSTRING, SYSTEM, DEFAULT, INITIAL, ALWAYSON, BACKUP, V0)
119 #include "Security/SecureObjectSync/ViewList.list"
120
121 return result;
122 }
123
124 bool SOSViewInSOSSystem(CFStringRef view) {
125
126 if(CFEqualSafe(view, kSOSViewKeychainV0)) {
127 return true;
128 }
129
130 #undef DOVIEWMACRO
131 #define DOVIEWMACRO(VIEWNAME, DEFSTRING, CMDSTRING, SYSTEM, DEFAULT, INITIAL, ALWAYSON, BACKUP, V0) \
132 if(CFEqualSafe(view, k##SYSTEM##View##VIEWNAME)) { \
133 return __SYSTEM_##SYSTEM; \
134 }
135 #include "Security/SecureObjectSync/ViewList.list"
136
137 return false;
138 }
139
140 bool SOSViewHintInSOSSystem(CFStringRef viewHint) {
141 #undef DOVIEWMACRO
142 #define CHECK_VIEWHINT_(VIEWNAME, SYSTEM) \
143 if(CFEqualSafe(viewHint, kSecAttrViewHint##VIEWNAME)) { \
144 return __SYSTEM_##SYSTEM; \
145 }
146 #define CHECK_VIEWHINT_V(VIEWNAME, SYSTEM)
147
148 #define DOVIEWMACRO(VIEWNAME, DEFSTRING, CMDSTRING, SYSTEM, DEFAULT, INITIAL, ALWAYSON, BACKUP, V0) \
149 CHECK_VIEWHINT_##V0(VIEWNAME, SYSTEM)
150 #include "Security/SecureObjectSync/ViewList.list"
151
152 return false;
153 }
154
155 bool SOSViewHintInCKKSSystem(CFStringRef viewHint) {
156
157 #undef DOVIEWMACRO_SOS
158 #undef DOVIEWMACRO_CKKS
159 #undef DOVIEWMACRO
160
161 #define DOVIEWMACRO_SOS(VIEWNAME, DEFSTRING, CMDSTRING, SYSTEM, DEFAULT, INITIAL, ALWAYSON, BACKUP, V0)
162 #define DOVIEWMACRO_CKKS(VIEWNAME, DEFSTRING, CMDSTRING, SYSTEM, DEFAULT, INITIAL, ALWAYSON, BACKUP, V0) \
163 if(CFEqualSafe(viewHint, kSecAttrViewHint##VIEWNAME)) { \
164 return true; \
165 }
166 #define DOVIEWMACRO(VIEWNAME, DEFSTRING, CMDSTRING, SYSTEM, DEFAULT, INITIAL, ALWAYSON, BACKUP, V0) \
167 DOVIEWMACRO_##SYSTEM(VIEWNAME, DEFSTRING, CMDSTRING, SYSTEM, DEFAULT, INITIAL, ALWAYSON, BACKUP, V0)
168
169 #include "Security/SecureObjectSync/ViewList.list"
170
171 return false;
172 }
173
174
175 CFGiblisGetSingleton(CFSetRef, SOSViewsGetV0ViewSet, defaultViewSet, ^{
176 // Since peer->views must never be NULL, fill in with a default
177 const void *values[] = { kSOSViewKeychainV0 };
178 *defaultViewSet = CFSetCreate(kCFAllocatorDefault, values, array_size(values), &kCFTypeSetCallBacks);
179 });
180
181 CFGiblisGetSingleton(CFSetRef, SOSViewsGetV0SubviewSet, subViewSet, (^{
182 // Since peer->views must never be NULL, fill in with a default
183 *subViewSet = SOSViewCopyViewSet(kViewSetV0);
184 }));
185
186 CFGiblisGetSingleton(CFSetRef, SOSViewsGetV0BackupViewSet, defaultViewSet, ^{
187 const void *values[] = { kSOSViewKeychainV0_tomb };
188 *defaultViewSet = CFSetCreate(kCFAllocatorDefault, values, array_size(values), &kCFTypeSetCallBacks);
189 });
190
191 CFGiblisGetSingleton(CFSetRef, SOSViewsGetV0BackupBagViewSet, defaultViewSet, ^{
192 const void *values[] = { kSOSViewBackupBagV0_tomb };
193 *defaultViewSet = CFSetCreate(kCFAllocatorDefault, values, array_size(values), &kCFTypeSetCallBacks);
194 });
195
196
197 CFGiblisGetSingleton(CFSetRef, SOSViewsGetInitialSyncSubviewSet, subViewSet, (^{
198 *subViewSet = SOSViewCopyViewSet(kViewSetInitial);
199 }));
200
201
202 bool SOSViewsIsV0Subview(CFStringRef viewName) {
203 return CFSetContainsValue(SOSViewsGetV0SubviewSet(), viewName);
204 }
205
206 CFSetRef sTestViewSet = NULL;
207 void SOSViewsSetTestViewsSet(CFSetRef testViewNames) {
208 CFRetainAssign(sTestViewSet, testViewNames);
209 }
210
211 CFSetRef SOSViewsGetAllCurrent(void) {
212 static dispatch_once_t dot;
213 static CFMutableSetRef allViews = NULL;
214 dispatch_once(&dot, ^{
215 allViews = SOSViewCopyViewSet(kViewSetAll);
216
217 CFSetAddValue(allViews, kSOSViewKeychainV0);
218 if(sTestViewSet) CFSetUnion(allViews, sTestViewSet);
219 });
220 return allViews;
221 }
222
223 const char *SOSViewsXlateAction(SOSViewActionCode action) {
224 switch(action) {
225 case kSOSCCViewEnable: return "kSOSCCViewEnable";
226 case kSOSCCViewDisable: return "kSOSCCViewDisable";
227 case kSOSCCViewQuery: return "kSOSCCViewQuery";
228 default: return "unknownViewAction";
229 }
230 }
231
232
233 // Eventually this will want to know the gestalt or security properties...
234 void SOSViewsForEachDefaultEnabledViewName(void (^operation)(CFStringRef viewName)) {
235 CFMutableSetRef defaultViews = SOSViewCopyViewSet(kViewSetDefault);
236
237 CFSetForEach(defaultViews, ^(const void *value) {
238 CFStringRef name = asString(value, NULL);
239
240 if (name) {
241 operation(name);
242 }
243 });
244
245 CFReleaseNull(defaultViews);
246 }
247
248 static bool SOSViewsIsKnownView(CFStringRef viewname) {
249 CFSetRef allViews = SOSViewsGetAllCurrent();
250 if(CFSetContainsValue(allViews, viewname)) return true;
251 secnotice("views","Not a known view");
252 return false;
253 }
254
255 static bool SOSViewsRequireIsKnownView(CFStringRef viewname, CFErrorRef* error) {
256 return SOSViewsIsKnownView(viewname) || SOSCreateErrorWithFormat(kSOSErrorNameMismatch, NULL, error, NULL, viewUnknownError, viewname, kSOSCCNoSuchView);
257 }
258
259 bool SOSPeerInfoIsEnabledView(SOSPeerInfoRef pi, CFStringRef viewName) {
260 if (pi->version < kSOSPeerV2BaseVersion) {
261 return CFSetContainsValue(SOSViewsGetV0ViewSet(), viewName);
262 } else {
263 return SOSPeerInfoV2DictionaryHasSetContaining(pi, sViewsKey, viewName);
264 }
265 }
266
267 void SOSPeerInfoWithEnabledViewSet(SOSPeerInfoRef pi, void (^operation)(CFSetRef enabled)) {
268 if (pi->version < kSOSPeerV2BaseVersion) {
269 operation(SOSViewsGetV0ViewSet());
270 } else {
271 SOSPeerInfoV2DictionaryWithSet(pi, sViewsKey, operation);
272 }
273 }
274
275 CFMutableSetRef SOSPeerInfoCopyEnabledViews(SOSPeerInfoRef pi) {
276 if (pi->version < kSOSPeerV2BaseVersion) {
277 return CFSetCreateMutableCopy(kCFAllocatorDefault, CFSetGetCount(SOSViewsGetV0ViewSet()), SOSViewsGetV0ViewSet());
278 } else {
279 CFMutableSetRef views = SOSPeerInfoV2DictionaryCopySet(pi, sViewsKey);
280 if (!views) {
281 // This is unexpected: log and return an empty set to prevent <rdar://problem/21938868>
282 secerror("%@ v2 peer has no views", SOSPeerInfoGetPeerID(pi));
283 views = CFSetCreateMutableForCFTypes(kCFAllocatorDefault);
284 }
285 return views;
286 }
287 }
288
289 CFSetRef SOSPeerInfoGetPermittedViews(SOSPeerInfoRef pi) {
290 return SOSViewsGetAllCurrent();
291 }
292
293 static void SOSPeerInfoSetViews(SOSPeerInfoRef pi, CFSetRef newviews) {
294 if(!newviews) {
295 secnotice("views","Asked to swap to NULL views");
296 return;
297 }
298 SOSPeerInfoV2DictionarySetValue(pi, sViewsKey, newviews);
299 }
300
301 static bool SOSPeerInfoViewIsValid(SOSPeerInfoRef pi, CFStringRef viewname) {
302 return true;
303 }
304
305 SOSViewResultCode SOSViewsEnable(SOSPeerInfoRef pi, CFStringRef viewname, CFErrorRef *error) {
306 SOSViewResultCode retval = kSOSCCGeneralViewError;
307
308 CFMutableSetRef newviews = SOSPeerInfoCopyEnabledViews(pi);
309 require_action_quiet(newviews, fail,
310 SOSCreateError(kSOSErrorAllocationFailure, viewMemError, NULL, error));
311 require_action_quiet(SOSViewsRequireIsKnownView(viewname, error), fail,
312 retval = kSOSCCNoSuchView);
313 require_action_quiet(SOSPeerInfoViewIsValid(pi, viewname), fail,
314 SOSCreateErrorWithFormat(kSOSErrorNameMismatch, NULL, error, NULL, viewInvalidError, viewname, retval = kSOSCCViewNotQualified));
315 CFSetAddValue(newviews, viewname);
316 SOSPeerInfoSetViews(pi, newviews);
317 CFReleaseSafe(newviews);
318 return kSOSCCViewMember;
319
320 fail:
321 CFReleaseNull(newviews);
322 secnotice("views","Failed to enable view(%@): %@", viewname, error ? *error : NULL);
323 return retval;
324 }
325
326 bool SOSViewSetEnable(SOSPeerInfoRef pi, CFSetRef viewSet) {
327 __block bool addedView = false;
328 CFMutableSetRef newviews = SOSPeerInfoCopyEnabledViews(pi);
329 require_action_quiet(newviews, errOut, secnotice("views", "failed to copy enabled views"));
330
331 CFSetForEach(viewSet, ^(const void *value) {
332 CFStringRef viewName = (CFStringRef) value;
333 if(SOSViewsIsKnownView(viewName) && SOSPeerInfoViewIsValid(pi, viewName)) {
334 if (!CFSetContainsValue(newviews, viewName)) {
335 addedView = true;
336 CFSetAddValue(newviews, viewName);
337 }
338 } else {
339 secnotice("views", "couldn't add view %@", viewName);
340 }
341 });
342 require_quiet(addedView, errOut);
343
344 SOSPeerInfoSetViews(pi, newviews);
345
346 errOut:
347 CFReleaseNull(newviews);
348 return addedView;
349 }
350
351
352 SOSViewResultCode SOSViewsDisable(SOSPeerInfoRef pi, CFStringRef viewname, CFErrorRef *error) {
353 SOSViewResultCode retval = kSOSCCGeneralViewError;
354 CFMutableSetRef newviews = SOSPeerInfoCopyEnabledViews(pi);
355 require_action_quiet(newviews, fail,
356 SOSCreateError(kSOSErrorAllocationFailure, viewMemError, NULL, error));
357 require_action_quiet(SOSViewsRequireIsKnownView(viewname, error), fail, retval = kSOSCCNoSuchView);
358
359 CFSetRemoveValue(newviews, viewname);
360 SOSPeerInfoSetViews(pi, newviews);
361 CFReleaseSafe(newviews);
362 return kSOSCCViewNotMember;
363
364 fail:
365 CFReleaseNull(newviews);
366 secnotice("views","Failed to disable view(%@): %@", viewname, error ? *error : NULL);
367 return retval;
368 }
369
370
371 bool SOSViewSetDisable(SOSPeerInfoRef pi, CFSetRef viewSet) {
372 __block bool removed = false;
373 CFMutableSetRef newviews = SOSPeerInfoCopyEnabledViews(pi);
374 require_action_quiet(newviews, errOut, secnotice("views", "failed to copy enabled views"));
375
376 CFSetForEach(viewSet, ^(const void *value) {
377 CFStringRef viewName = (CFStringRef) value;
378 if(SOSViewsIsKnownView(viewName) && CFSetContainsValue(newviews, viewName)) {
379 removed = true;
380 CFSetRemoveValue(newviews, viewName);
381 } else {
382 secnotice("views", "couldn't delete view %@", viewName);
383 }
384 });
385
386 require_quiet(removed, errOut);
387
388 SOSPeerInfoSetViews(pi, newviews);
389
390 errOut:
391 CFReleaseNull(newviews);
392 return removed;
393 }
394
395
396 SOSViewResultCode SOSViewsQuery(SOSPeerInfoRef pi, CFStringRef viewname, CFErrorRef *error) {
397 SOSViewResultCode retval = kSOSCCNoSuchView;
398 CFSetRef views = NULL;
399 require_quiet(SOSViewsRequireIsKnownView(viewname, error), fail);
400
401 views = SOSPeerInfoCopyEnabledViews(pi);
402 if(!views){
403 retval = kSOSCCViewNotMember;
404 CFReleaseNull(views);
405 return retval;
406 }
407
408 // kSOSViewKeychainV0 is set if there is a V0 PeerInfo in the circle. It represents all of the subviews in
409 // SOSViewsGetV0SubviewSet() so we return kSOSCCViewMember for that case. kSOSViewKeychainV0 and the subviews
410 // are mutually exclusive.
411 else if(CFSetContainsValue(views, kSOSViewKeychainV0) && CFSetContainsValue(SOSViewsGetV0SubviewSet(), viewname)) {
412 retval = kSOSCCViewMember;
413 } else {
414 retval = (CFSetContainsValue(views, viewname)) ? kSOSCCViewMember: kSOSCCViewNotMember;
415 }
416
417 CFReleaseNull(views);
418 return retval;
419
420 fail:
421 secnotice("views","Failed to query view(%@): %@", viewname, error ? *error : NULL);
422 CFReleaseNull(views);
423 return retval;
424 }
425
426
427 /* Need XPC way to carry CFSets of views */
428
429
430
431 xpc_object_t CreateXPCObjectWithCFSetRef(CFSetRef setref, CFErrorRef *error) {
432 xpc_object_t result = NULL;
433 size_t data_size = 0;
434 uint8_t *data = NULL;
435 require_action_quiet(setref, errOut, SecCFCreateErrorWithFormat(kSecXPCErrorUnexpectedNull, sSecXPCErrorDomain, NULL, error, NULL, CFSTR("Unexpected Null Set to encode")));
436 require_quiet((data_size = der_sizeof_set(setref, error)) != 0, errOut);
437 require_quiet((data = (uint8_t *)malloc(data_size)) != NULL, errOut);
438
439 der_encode_set(setref, error, data, data + data_size);
440 result = xpc_data_create(data, data_size);
441 free(data);
442 errOut:
443 return result;
444 }
445