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 "keychain/SecureObjectSync/SOSInternal.h"
40 #include <Security/SecureObjectSync/SOSPeerInfo.h>
41 #include "keychain/SecureObjectSync/SOSPeerInfoV2.h"
42 #include "keychain/SecureObjectSync/SOSPeerInfoPriv.h"
43 #include <Security/SecureObjectSync/SOSCloudCircle.h>
45 #include <utilities/array_size.h>
46 #include "keychain/SecureObjectSync/SOSAccount.h"
47 #include "keychain/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 "keychain/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 CFSetRef sTestViewSet = NULL;
91 CFMutableSetRef SOSViewCopyViewSet(ViewSetKind setKind) {
92 CFMutableSetRef result = CFSetCreateMutableForCFTypes(kCFAllocatorDefault);
94 // Only return views in the SOS system, unless they asked for kViewSetCKKS
96 #define __TYPE_MEMBER_ false
97 #define __TYPE_MEMBER_D true
98 #define __TYPE_MEMBER_I true
99 #define __TYPE_MEMBER_A true
100 #define __TYPE_MEMBER_V true
101 #define __TYPE_MEMBER_B true
102 #define __SYSTEM_SOS true
103 #define __SYSTEM_CKKS false
105 #define DOVIEWMACRO_SOS(VIEWNAME, DEFSTRING, CMDSTRING, SYSTEM, DEFAULT, INITIAL, ALWAYSON, BACKUP, V0) \
106 if(((setKind == kViewSetAll) || \
107 ((setKind == kViewSetDefault) && __TYPE_MEMBER_##DEFAULT) || \
108 ((setKind == kViewSetInitial) && __TYPE_MEMBER_##INITIAL) || \
109 ((setKind == kViewSetAlwaysOn) && __TYPE_MEMBER_##ALWAYSON) || \
110 ((setKind == kViewSetRequiredForBackup) && __TYPE_MEMBER_##BACKUP) || \
111 ((setKind == kViewSetV0) && __TYPE_MEMBER_##V0) )) { \
112 CFSetAddValue(result, k##SYSTEM##View##VIEWNAME); \
115 #define DOVIEWMACRO_CKKS(VIEWNAME, DEFSTRING, CMDSTRING, SYSTEM, DEFAULT, INITIAL, ALWAYSON, BACKUP, V0) \
116 if(setKind == kViewSetCKKS) { \
117 CFSetAddValue(result, k##SYSTEM##View##VIEWNAME); \
120 #define DOVIEWMACRO(VIEWNAME, DEFSTRING, CMDSTRING, SYSTEM, DEFAULT, INITIAL, ALWAYSON, BACKUP, V0) \
121 DOVIEWMACRO_##SYSTEM(VIEWNAME, DEFSTRING, CMDSTRING, SYSTEM, DEFAULT, INITIAL, ALWAYSON, BACKUP, V0)
122 #include "keychain/SecureObjectSync/ViewList.list"
123 if(setKind == kViewSetAlwaysOn && sTestViewSet != NULL) {
124 CFReleaseNull(result);
125 result = CFSetCreateMutableForCFTypes(kCFAllocatorDefault);
127 if(setKind == kViewSetAll && sTestViewSet != NULL) {
128 CFSetUnion(result, sTestViewSet);
133 bool SOSViewInSOSSystem(CFStringRef view) {
135 if(CFEqualSafe(view, kSOSViewKeychainV0)) {
140 #define DOVIEWMACRO(VIEWNAME, DEFSTRING, CMDSTRING, SYSTEM, DEFAULT, INITIAL, ALWAYSON, BACKUP, V0) \
141 if(CFEqualSafe(view, k##SYSTEM##View##VIEWNAME)) { \
142 return __SYSTEM_##SYSTEM; \
144 #include "keychain/SecureObjectSync/ViewList.list"
149 bool SOSViewHintInSOSSystem(CFStringRef viewHint) {
151 #define CHECK_VIEWHINT_(VIEWNAME, SYSTEM) \
152 if(CFEqualSafe(viewHint, kSecAttrViewHint##VIEWNAME)) { \
153 return __SYSTEM_##SYSTEM; \
155 #define CHECK_VIEWHINT_V(VIEWNAME, SYSTEM)
157 #define DOVIEWMACRO(VIEWNAME, DEFSTRING, CMDSTRING, SYSTEM, DEFAULT, INITIAL, ALWAYSON, BACKUP, V0) \
158 CHECK_VIEWHINT_##V0(VIEWNAME, SYSTEM)
159 #include "keychain/SecureObjectSync/ViewList.list"
164 bool SOSViewHintInCKKSSystem(CFStringRef viewHint) {
166 #undef DOVIEWMACRO_SOS
167 #undef DOVIEWMACRO_CKKS
170 #define DOVIEWMACRO_SOS(VIEWNAME, DEFSTRING, CMDSTRING, SYSTEM, DEFAULT, INITIAL, ALWAYSON, BACKUP, V0)
171 #define DOVIEWMACRO_CKKS(VIEWNAME, DEFSTRING, CMDSTRING, SYSTEM, DEFAULT, INITIAL, ALWAYSON, BACKUP, V0) \
172 if(CFEqualSafe(viewHint, kSecAttrViewHint##VIEWNAME)) { \
175 #define DOVIEWMACRO(VIEWNAME, DEFSTRING, CMDSTRING, SYSTEM, DEFAULT, INITIAL, ALWAYSON, BACKUP, V0) \
176 DOVIEWMACRO_##SYSTEM(VIEWNAME, DEFSTRING, CMDSTRING, SYSTEM, DEFAULT, INITIAL, ALWAYSON, BACKUP, V0)
178 #include "keychain/SecureObjectSync/ViewList.list"
184 CFGiblisGetSingleton(CFSetRef, SOSViewsGetV0ViewSet, defaultViewSet, ^{
185 // Since peer->views must never be NULL, fill in with a default
186 const void *values[] = { kSOSViewKeychainV0 };
187 *defaultViewSet = CFSetCreate(kCFAllocatorDefault, values, array_size(values), &kCFTypeSetCallBacks);
190 CFGiblisGetSingleton(CFSetRef, SOSViewsGetV0SubviewSet, subViewSet, (^{
191 // Since peer->views must never be NULL, fill in with a default
192 *subViewSet = SOSViewCopyViewSet(kViewSetV0);
195 CFGiblisGetSingleton(CFSetRef, SOSViewsGetV0BackupViewSet, defaultViewSet, ^{
196 const void *values[] = { kSOSViewKeychainV0_tomb };
197 *defaultViewSet = CFSetCreate(kCFAllocatorDefault, values, array_size(values), &kCFTypeSetCallBacks);
200 CFGiblisGetSingleton(CFSetRef, SOSViewsGetV0BackupBagViewSet, defaultViewSet, ^{
201 const void *values[] = { kSOSViewBackupBagV0_tomb };
202 *defaultViewSet = CFSetCreate(kCFAllocatorDefault, values, array_size(values), &kCFTypeSetCallBacks);
206 CFGiblisGetSingleton(CFSetRef, SOSViewsGetInitialSyncSubviewSet, subViewSet, (^{
207 *subViewSet = SOSViewCopyViewSet(kViewSetInitial);
211 bool SOSViewsIsV0Subview(CFStringRef viewName) {
212 return CFSetContainsValue(SOSViewsGetV0SubviewSet(), viewName);
215 static CFMutableSetRef sosAllViews = NULL;
217 void SOSViewsSetTestViewsSet(CFSetRef testViewNames) {
218 CFReleaseNull(sTestViewSet);
220 CFRetainAssign(sTestViewSet, testViewNames);
223 CFReleaseNull(sosAllViews);
224 sosAllViews = SOSViewCopyViewSet(kViewSetAll);
225 CFSetAddValue(sosAllViews, kSOSViewKeychainV0);
226 if(sTestViewSet) CFSetUnion(sosAllViews, sTestViewSet);
230 CFSetRef SOSViewsGetAllCurrent(void) {
231 static dispatch_once_t dot;
232 dispatch_once(&dot, ^{
233 sosAllViews = SOSViewCopyViewSet(kViewSetAll);
235 CFSetAddValue(sosAllViews, kSOSViewKeychainV0);
236 if(sTestViewSet) CFSetUnion(sosAllViews, sTestViewSet);
241 static CFDictionaryRef SOSViewsGetBitmasks(void) {
242 static dispatch_once_t once;
243 static CFMutableDictionaryRef masks = NULL;
245 dispatch_once(&once, ^{
246 CFSetRef views = SOSViewsGetAllCurrent();
247 CFMutableArrayRef viewArray = CFArrayCreateMutableForCFTypes(kCFAllocatorDefault);
248 CFSetForEach(views, ^(const void *value) {
249 CFStringRef viewName = (CFStringRef) value;
250 CFArrayAppendValue(viewArray, viewName);
252 CFIndex viewCount = CFArrayGetCount(viewArray);
254 secnotice("views", "Too many views defined, can't make bitmask (%d)", (int) viewCount);
256 __block uint32_t maskValue = 1;
257 CFRange all = CFRangeMake(0, viewCount);
258 CFArraySortValues(viewArray, all, (CFComparatorFunction)CFStringCompare, NULL);
259 masks = CFDictionaryCreateMutable(kCFAllocatorDefault, 0, &kCFCopyStringDictionaryKeyCallBacks, NULL);
260 CFArrayForEach(viewArray, ^(const void *value) {
261 CFDictionaryAddValue(masks, value, (const void *) (uintptr_t) maskValue);
265 CFReleaseNull(viewArray);
270 uint64_t SOSViewBitmaskFromSet(CFSetRef views) {
271 __block uint64_t retval = 0;
272 if(!views) return retval;
273 CFDictionaryRef masks = SOSViewsGetBitmasks();
275 CFSetForEach(views, ^(const void *viewName) {
276 uint64_t viewMask = (uint64_t) CFDictionaryGetValue(masks, viewName);
283 uint64_t SOSPeerInfoViewBitMask(SOSPeerInfoRef pi) {
284 __block uint64_t retval = 0;
285 CFSetRef views = SOSPeerInfoCopyEnabledViews(pi);
287 retval = SOSViewBitmaskFromSet(views);
288 CFReleaseNull(views);
293 CFSetRef SOSViewCreateSetFromBitmask(uint64_t bitmask) {
294 CFMutableSetRef retval = NULL;
295 CFDictionaryRef masks = SOSViewsGetBitmasks();
297 retval = CFSetCreateMutableForCFTypes(kCFAllocatorDefault);
298 CFDictionaryForEach(masks, ^(const void *key, const void *value) {
299 CFStringRef viewName = (CFStringRef) key;
300 uint64_t viewMask = (uint64_t) value;
301 if(bitmask & viewMask) {
302 CFSetAddValue(retval, viewName);
309 const char *SOSViewsXlateAction(SOSViewActionCode action) {
311 case kSOSCCViewEnable: return "kSOSCCViewEnable";
312 case kSOSCCViewDisable: return "kSOSCCViewDisable";
313 case kSOSCCViewQuery: return "kSOSCCViewQuery";
314 default: return "unknownViewAction";
319 // Eventually this will want to know the gestalt or security properties...
320 void SOSViewsForEachDefaultEnabledViewName(void (^operation)(CFStringRef viewName)) {
321 CFMutableSetRef defaultViews = SOSViewCopyViewSet(kViewSetDefault);
323 CFSetForEach(defaultViews, ^(const void *value) {
324 CFStringRef name = asString(value, NULL);
331 CFReleaseNull(defaultViews);
334 static bool SOSViewsIsKnownView(CFStringRef viewname) {
335 if(!viewname) return false;
336 CFSetRef allViews = SOSViewsGetAllCurrent();
337 if(CFSetContainsValue(allViews, viewname)) return true;
338 secnotice("views","Not a known view");
342 static bool SOSViewsRequireIsKnownView(CFStringRef viewname, CFErrorRef* error) {
343 return SOSViewsIsKnownView(viewname) || SOSCreateErrorWithFormat(kSOSErrorNameMismatch, NULL, error, NULL, viewUnknownError, viewname, kSOSCCNoSuchView);
346 bool SOSPeerInfoIsEnabledView(SOSPeerInfoRef pi, CFStringRef viewName) {
347 if (pi->version < kSOSPeerV2BaseVersion) {
348 return CFSetContainsValue(SOSViewsGetV0ViewSet(), viewName);
350 return SOSPeerInfoV2DictionaryHasSetContaining(pi, sViewsKey, viewName);
354 void SOSPeerInfoWithEnabledViewSet(SOSPeerInfoRef pi, void (^operation)(CFSetRef enabled)) {
355 if (pi->version < kSOSPeerV2BaseVersion) {
356 operation(SOSViewsGetV0ViewSet());
358 SOSPeerInfoV2DictionaryWithSet(pi, sViewsKey, operation);
362 CFMutableSetRef SOSPeerInfoCopyEnabledViews(SOSPeerInfoRef pi) {
363 if (pi->version < kSOSPeerV2BaseVersion) {
364 return CFSetCreateMutableCopy(kCFAllocatorDefault, CFSetGetCount(SOSViewsGetV0ViewSet()), SOSViewsGetV0ViewSet());
366 CFMutableSetRef views = SOSPeerInfoV2DictionaryCopySet(pi, sViewsKey);
368 // This is unexpected: log and return an empty set to prevent <rdar://problem/21938868>
369 secerror("%@ v2 peer has no views", SOSPeerInfoGetPeerID(pi));
370 views = CFSetCreateMutableForCFTypes(kCFAllocatorDefault);
376 CFSetRef SOSPeerInfoGetPermittedViews(SOSPeerInfoRef pi) {
377 return SOSViewsGetAllCurrent();
380 static void SOSPeerInfoSetViews(SOSPeerInfoRef pi, CFSetRef newviews) {
382 secnotice("views","Asked to swap to NULL views");
385 SOSPeerInfoV2DictionarySetValue(pi, sViewsKey, newviews);
388 static bool SOSPeerInfoViewIsValid(SOSPeerInfoRef pi, CFStringRef viewname) {
392 SOSViewResultCode SOSViewsEnable(SOSPeerInfoRef pi, CFStringRef viewname, CFErrorRef *error) {
393 SOSViewResultCode retval = kSOSCCGeneralViewError;
395 CFMutableSetRef newviews = SOSPeerInfoCopyEnabledViews(pi);
396 require_action_quiet(newviews, fail,
397 SOSCreateError(kSOSErrorAllocationFailure, viewMemError, NULL, error));
398 require_action_quiet(SOSViewsRequireIsKnownView(viewname, error), fail,
399 retval = kSOSCCNoSuchView);
400 require_action_quiet(SOSPeerInfoViewIsValid(pi, viewname), fail,
401 SOSCreateErrorWithFormat(kSOSErrorNameMismatch, NULL, error, NULL, viewInvalidError, viewname, retval = kSOSCCViewNotQualified));
402 CFSetAddValue(newviews, viewname);
403 SOSPeerInfoSetViews(pi, newviews);
404 CFReleaseSafe(newviews);
405 return kSOSCCViewMember;
408 CFReleaseNull(newviews);
409 secnotice("views","Failed to enable view(%@): %@", viewname, error ? *error : NULL);
413 bool SOSViewSetEnable(SOSPeerInfoRef pi, CFSetRef viewSet) {
414 __block bool addedView = false;
415 CFMutableSetRef newviews = SOSPeerInfoCopyEnabledViews(pi);
416 require_action_quiet(newviews, errOut, secnotice("views", "failed to copy enabled views"));
418 CFSetForEach(viewSet, ^(const void *value) {
419 CFStringRef viewName = (CFStringRef) value;
420 if(SOSViewsIsKnownView(viewName) && SOSPeerInfoViewIsValid(pi, viewName)) {
421 if (!CFSetContainsValue(newviews, viewName)) {
423 CFSetAddValue(newviews, viewName);
426 secnotice("views", "couldn't add view %@", viewName);
429 require_quiet(addedView, errOut);
431 SOSPeerInfoSetViews(pi, newviews);
434 CFReleaseNull(newviews);
439 SOSViewResultCode SOSViewsDisable(SOSPeerInfoRef pi, CFStringRef viewname, CFErrorRef *error) {
440 SOSViewResultCode retval = kSOSCCGeneralViewError;
441 CFMutableSetRef newviews = SOSPeerInfoCopyEnabledViews(pi);
442 require_action_quiet(newviews, fail,
443 SOSCreateError(kSOSErrorAllocationFailure, viewMemError, NULL, error));
444 require_action_quiet(SOSViewsRequireIsKnownView(viewname, error), fail, retval = kSOSCCNoSuchView);
446 CFSetRemoveValue(newviews, viewname);
447 SOSPeerInfoSetViews(pi, newviews);
448 CFReleaseSafe(newviews);
449 return kSOSCCViewNotMember;
452 CFReleaseNull(newviews);
453 secnotice("views","Failed to disable view(%@): %@", viewname, error ? *error : NULL);
458 bool SOSViewSetDisable(SOSPeerInfoRef pi, CFSetRef viewSet) {
459 __block bool removed = false;
460 CFMutableSetRef newviews = SOSPeerInfoCopyEnabledViews(pi);
461 require_action_quiet(newviews, errOut, secnotice("views", "failed to copy enabled views"));
463 CFSetForEach(viewSet, ^(const void *value) {
464 CFStringRef viewName = (CFStringRef) value;
465 if(SOSViewsIsKnownView(viewName) && CFSetContainsValue(newviews, viewName)) {
467 CFSetRemoveValue(newviews, viewName);
469 secnotice("views", "couldn't delete view %@", viewName);
473 require_quiet(removed, errOut);
475 SOSPeerInfoSetViews(pi, newviews);
478 CFReleaseNull(newviews);
483 SOSViewResultCode SOSViewsQuery(SOSPeerInfoRef pi, CFStringRef viewname, CFErrorRef *error) {
484 SOSViewResultCode retval = kSOSCCNoSuchView;
485 CFSetRef views = NULL;
486 require_quiet(SOSViewsRequireIsKnownView(viewname, error), fail);
488 views = SOSPeerInfoCopyEnabledViews(pi);
490 retval = kSOSCCViewNotMember;
491 CFReleaseNull(views);
495 // kSOSViewKeychainV0 is set if there is a V0 PeerInfo in the circle. It represents all of the subviews in
496 // SOSViewsGetV0SubviewSet() so we return kSOSCCViewMember for that case. kSOSViewKeychainV0 and the subviews
497 // are mutually exclusive.
498 else if(CFSetContainsValue(views, kSOSViewKeychainV0) && CFSetContainsValue(SOSViewsGetV0SubviewSet(), viewname)) {
499 retval = kSOSCCViewMember;
501 retval = (CFSetContainsValue(views, viewname)) ? kSOSCCViewMember: kSOSCCViewNotMember;
504 CFReleaseNull(views);
508 secnotice("views","Failed to query view(%@): %@", viewname, error ? *error : NULL);
509 CFReleaseNull(views);
514 /* Need XPC way to carry CFSets of views */
518 xpc_object_t CreateXPCObjectWithCFSetRef(CFSetRef setref, CFErrorRef *error) {
519 xpc_object_t result = NULL;
520 size_t data_size = 0;
521 uint8_t *data = NULL;
522 require_action_quiet(setref, errOut, SecCFCreateErrorWithFormat(kSecXPCErrorUnexpectedNull, sSecXPCErrorDomain, NULL, error, NULL, CFSTR("Unexpected Null Set to encode")));
523 require_quiet((data_size = der_sizeof_set(setref, error)) != 0, errOut);
524 require_quiet((data = (uint8_t *)malloc(data_size)) != NULL, errOut);
526 der_encode_set(setref, error, data, data + data_size);
527 result = xpc_data_create(data, data_size);