2 * Copyright (c) 2015 Apple Inc. All Rights Reserved.
4 * @APPLE_LICENSE_HEADER_START@
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
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.
21 * @APPLE_LICENSE_HEADER_END@
25 * SOSViews.c - Implementation of views
28 #include <AssertMacros.h>
29 #include <TargetConditionals.h>
32 #include <utilities/SecCFWrappers.h>
33 #include <utilities/SecCFRelease.h>
34 #include <utilities/SecXPCError.h>
36 #include <utilities/SecCFError.h>
37 #include <utilities/der_set.h>
38 #include <Security/SecureObjectSync/SOSInternal.h>
40 #include <Security/SecureObjectSync/SOSPeerInfo.h>
41 #include <Security/SecureObjectSync/SOSPeerInfoV2.h>
42 #include <Security/SecureObjectSync/SOSPeerInfoPriv.h>
43 #include <Security/SecureObjectSync/SOSCloudCircle.h>
45 #include <utilities/array_size.h>
46 #include <Security/SecureObjectSync/SOSAccount.h>
47 #include <Security/SecureObjectSync/SOSAccountPriv.h>
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)")
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");
63 const CFStringRef kSOSViewKeychainV0 = CFSTR("KeychainV0"); // iCloud Keychain syncing for v0 peers
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"
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");
85 const CFStringRef kSOSViewHintAppleTV = CFSTR("AppleTV");
86 const CFStringRef kSOSViewHintHomeKit = CFSTR("HomeKit");
88 CFMutableSetRef SOSViewCopyViewSet(ViewSetKind setKind) {
89 CFMutableSetRef result = CFSetCreateMutableForCFTypes(kCFAllocatorDefault);
91 // Only return views in the SOS system, unless they asked for kViewSetCKKS
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
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); \
112 #define DOVIEWMACRO_CKKS(VIEWNAME, DEFSTRING, CMDSTRING, SYSTEM, DEFAULT, INITIAL, ALWAYSON, BACKUP, V0) \
113 if(setKind == kViewSetCKKS) { \
114 CFSetAddValue(result, k##SYSTEM##View##VIEWNAME); \
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"
124 bool SOSViewInSOSSystem(CFStringRef view) {
126 if(CFEqualSafe(view, kSOSViewKeychainV0)) {
131 #define DOVIEWMACRO(VIEWNAME, DEFSTRING, CMDSTRING, SYSTEM, DEFAULT, INITIAL, ALWAYSON, BACKUP, V0) \
132 if(CFEqualSafe(view, k##SYSTEM##View##VIEWNAME)) { \
133 return __SYSTEM_##SYSTEM; \
135 #include "Security/SecureObjectSync/ViewList.list"
140 bool SOSViewHintInSOSSystem(CFStringRef viewHint) {
142 #define CHECK_VIEWHINT_(VIEWNAME, SYSTEM) \
143 if(CFEqualSafe(viewHint, kSecAttrViewHint##VIEWNAME)) { \
144 return __SYSTEM_##SYSTEM; \
146 #define CHECK_VIEWHINT_V(VIEWNAME, SYSTEM)
148 #define DOVIEWMACRO(VIEWNAME, DEFSTRING, CMDSTRING, SYSTEM, DEFAULT, INITIAL, ALWAYSON, BACKUP, V0) \
149 CHECK_VIEWHINT_##V0(VIEWNAME, SYSTEM)
150 #include "Security/SecureObjectSync/ViewList.list"
155 bool SOSViewHintInCKKSSystem(CFStringRef viewHint) {
157 #undef DOVIEWMACRO_SOS
158 #undef DOVIEWMACRO_CKKS
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)) { \
166 #define DOVIEWMACRO(VIEWNAME, DEFSTRING, CMDSTRING, SYSTEM, DEFAULT, INITIAL, ALWAYSON, BACKUP, V0) \
167 DOVIEWMACRO_##SYSTEM(VIEWNAME, DEFSTRING, CMDSTRING, SYSTEM, DEFAULT, INITIAL, ALWAYSON, BACKUP, V0)
169 #include "Security/SecureObjectSync/ViewList.list"
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);
181 CFGiblisGetSingleton(CFSetRef, SOSViewsGetV0SubviewSet, subViewSet, (^{
182 // Since peer->views must never be NULL, fill in with a default
183 *subViewSet = SOSViewCopyViewSet(kViewSetV0);
186 CFGiblisGetSingleton(CFSetRef, SOSViewsGetV0BackupViewSet, defaultViewSet, ^{
187 const void *values[] = { kSOSViewKeychainV0_tomb };
188 *defaultViewSet = CFSetCreate(kCFAllocatorDefault, values, array_size(values), &kCFTypeSetCallBacks);
191 CFGiblisGetSingleton(CFSetRef, SOSViewsGetV0BackupBagViewSet, defaultViewSet, ^{
192 const void *values[] = { kSOSViewBackupBagV0_tomb };
193 *defaultViewSet = CFSetCreate(kCFAllocatorDefault, values, array_size(values), &kCFTypeSetCallBacks);
197 CFGiblisGetSingleton(CFSetRef, SOSViewsGetInitialSyncSubviewSet, subViewSet, (^{
198 *subViewSet = SOSViewCopyViewSet(kViewSetInitial);
202 bool SOSViewsIsV0Subview(CFStringRef viewName) {
203 return CFSetContainsValue(SOSViewsGetV0SubviewSet(), viewName);
206 CFSetRef sTestViewSet = NULL;
207 void SOSViewsSetTestViewsSet(CFSetRef testViewNames) {
208 CFRetainAssign(sTestViewSet, testViewNames);
211 CFSetRef SOSViewsGetAllCurrent(void) {
212 static dispatch_once_t dot;
213 static CFMutableSetRef allViews = NULL;
214 dispatch_once(&dot, ^{
215 allViews = SOSViewCopyViewSet(kViewSetAll);
217 CFSetAddValue(allViews, kSOSViewKeychainV0);
218 if(sTestViewSet) CFSetUnion(allViews, sTestViewSet);
223 const char *SOSViewsXlateAction(SOSViewActionCode action) {
225 case kSOSCCViewEnable: return "kSOSCCViewEnable";
226 case kSOSCCViewDisable: return "kSOSCCViewDisable";
227 case kSOSCCViewQuery: return "kSOSCCViewQuery";
228 default: return "unknownViewAction";
233 // Eventually this will want to know the gestalt or security properties...
234 void SOSViewsForEachDefaultEnabledViewName(void (^operation)(CFStringRef viewName)) {
235 CFMutableSetRef defaultViews = SOSViewCopyViewSet(kViewSetDefault);
237 CFSetForEach(defaultViews, ^(const void *value) {
238 CFStringRef name = asString(value, NULL);
245 CFReleaseNull(defaultViews);
248 static bool SOSViewsIsKnownView(CFStringRef viewname) {
249 CFSetRef allViews = SOSViewsGetAllCurrent();
250 if(CFSetContainsValue(allViews, viewname)) return true;
251 secnotice("views","Not a known view");
255 static bool SOSViewsRequireIsKnownView(CFStringRef viewname, CFErrorRef* error) {
256 return SOSViewsIsKnownView(viewname) || SOSCreateErrorWithFormat(kSOSErrorNameMismatch, NULL, error, NULL, viewUnknownError, viewname, kSOSCCNoSuchView);
259 bool SOSPeerInfoIsEnabledView(SOSPeerInfoRef pi, CFStringRef viewName) {
260 if (pi->version < kSOSPeerV2BaseVersion) {
261 return CFSetContainsValue(SOSViewsGetV0ViewSet(), viewName);
263 return SOSPeerInfoV2DictionaryHasSetContaining(pi, sViewsKey, viewName);
267 void SOSPeerInfoWithEnabledViewSet(SOSPeerInfoRef pi, void (^operation)(CFSetRef enabled)) {
268 if (pi->version < kSOSPeerV2BaseVersion) {
269 operation(SOSViewsGetV0ViewSet());
271 SOSPeerInfoV2DictionaryWithSet(pi, sViewsKey, operation);
275 CFMutableSetRef SOSPeerInfoCopyEnabledViews(SOSPeerInfoRef pi) {
276 if (pi->version < kSOSPeerV2BaseVersion) {
277 return CFSetCreateMutableCopy(kCFAllocatorDefault, CFSetGetCount(SOSViewsGetV0ViewSet()), SOSViewsGetV0ViewSet());
279 CFMutableSetRef views = SOSPeerInfoV2DictionaryCopySet(pi, sViewsKey);
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);
289 CFSetRef SOSPeerInfoGetPermittedViews(SOSPeerInfoRef pi) {
290 return SOSViewsGetAllCurrent();
293 static void SOSPeerInfoSetViews(SOSPeerInfoRef pi, CFSetRef newviews) {
295 secnotice("views","Asked to swap to NULL views");
298 SOSPeerInfoV2DictionarySetValue(pi, sViewsKey, newviews);
301 static bool SOSPeerInfoViewIsValid(SOSPeerInfoRef pi, CFStringRef viewname) {
305 SOSViewResultCode SOSViewsEnable(SOSPeerInfoRef pi, CFStringRef viewname, CFErrorRef *error) {
306 SOSViewResultCode retval = kSOSCCGeneralViewError;
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;
321 CFReleaseNull(newviews);
322 secnotice("views","Failed to enable view(%@): %@", viewname, error ? *error : NULL);
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"));
331 CFSetForEach(viewSet, ^(const void *value) {
332 CFStringRef viewName = (CFStringRef) value;
333 if(SOSViewsIsKnownView(viewName) && SOSPeerInfoViewIsValid(pi, viewName)) {
334 if (!CFSetContainsValue(newviews, viewName)) {
336 CFSetAddValue(newviews, viewName);
339 secnotice("views", "couldn't add view %@", viewName);
342 require_quiet(addedView, errOut);
344 SOSPeerInfoSetViews(pi, newviews);
347 CFReleaseNull(newviews);
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);
359 CFSetRemoveValue(newviews, viewname);
360 SOSPeerInfoSetViews(pi, newviews);
361 CFReleaseSafe(newviews);
362 return kSOSCCViewNotMember;
365 CFReleaseNull(newviews);
366 secnotice("views","Failed to disable view(%@): %@", viewname, error ? *error : NULL);
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"));
376 CFSetForEach(viewSet, ^(const void *value) {
377 CFStringRef viewName = (CFStringRef) value;
378 if(SOSViewsIsKnownView(viewName) && CFSetContainsValue(newviews, viewName)) {
380 CFSetRemoveValue(newviews, viewName);
382 secnotice("views", "couldn't delete view %@", viewName);
386 require_quiet(removed, errOut);
388 SOSPeerInfoSetViews(pi, newviews);
391 CFReleaseNull(newviews);
396 SOSViewResultCode SOSViewsQuery(SOSPeerInfoRef pi, CFStringRef viewname, CFErrorRef *error) {
397 SOSViewResultCode retval = kSOSCCNoSuchView;
398 CFSetRef views = NULL;
399 require_quiet(SOSViewsRequireIsKnownView(viewname, error), fail);
401 views = SOSPeerInfoCopyEnabledViews(pi);
403 retval = kSOSCCViewNotMember;
404 CFReleaseNull(views);
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;
414 retval = (CFSetContainsValue(views, viewname)) ? kSOSCCViewMember: kSOSCCViewNotMember;
417 CFReleaseNull(views);
421 secnotice("views","Failed to query view(%@): %@", viewname, error ? *error : NULL);
422 CFReleaseNull(views);
427 /* Need XPC way to carry CFSets of views */
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);
439 der_encode_set(setref, error, data, data + data_size);
440 result = xpc_data_create(data, data_size);