2 * Copyright (c) 2017 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@
24 #include <pthread/pthread.h>
26 #include "server_security_helpers.h"
27 #include "server_entitlement_helpers.h"
29 #include <Security/SecTask.h>
30 #include <Security/SecTaskPriv.h>
31 #include "ipc/securityd_client.h"
32 #include <Security/SecEntitlements.h>
33 #include <Security/SecItem.h>
34 #include "utilities/SecCFRelease.h"
35 #include "utilities/SecCFWrappers.h"
36 #include "utilities/debugging.h"
37 #include "keychain/securityd/SecDbQuery.h"
39 #if __has_include(<MobileKeyBag/MobileKeyBag.h>) && TARGET_HAS_KEYSTORE
40 #include <MobileKeyBag/MobileKeyBag.h>
41 #define HAVE_MOBILE_KEYBAG_SUPPORT 1
44 #if __has_include(<UserManagement/UserManagement.h>)
45 #include <UserManagement/UserManagement.h>
48 #if TARGET_OS_IOS && HAVE_MOBILE_KEYBAG_SUPPORT
50 device_is_multiuser(void)
52 static dispatch_once_t once;
55 dispatch_once(&once, ^{
56 CFDictionaryRef deviceMode = MKBUserTypeDeviceMode(NULL, NULL);
57 CFTypeRef value = NULL;
59 if (deviceMode && CFDictionaryGetValueIfPresent(deviceMode, kMKBDeviceModeKey, &value) && CFEqual(value, kMKBDeviceModeMultiUser)) {
62 CFReleaseNull(deviceMode);
67 #endif /* HAVE_MOBILE_KEYBAG_SUPPORT && TARGET_OS_IOS */
70 fill_security_client(SecurityClient * client, const uid_t uid, audit_token_t auditToken) {
81 #if HAVE_MOBILE_KEYBAG_SUPPORT
82 if (device_is_multiuser()) {
83 CFErrorRef error = NULL;
85 client->inMultiUser = true;
86 client->activeUser = MKBForegroundUserSessionID(&error);
87 if (client->activeUser == -1 || client->activeUser == 0) {
89 client->activeUser = 0;
93 * If we are a edu mode user, and its not the active user,
94 * then the request is coming from inside the syncbubble.
96 * otherwise we are going to execute the request as the
100 if (client->uid > 501 && (uid_t)client->activeUser != client->uid) {
101 secinfo("serverxpc", "securityd client: sync bubble user");
102 client->musr = SecMUSRCreateSyncBubbleUserUUID(client->uid);
103 client->keybag = KEYBAG_DEVICE;
105 secinfo("serverxpc", "securityd client: active user");
106 client->musr = SecMUSRCreateActiveUserUUID(client->activeUser);
107 client->uid = (uid_t)client->activeUser;
108 client->keybag = KEYBAG_DEVICE;
113 * If the client application is a enterprise app according to usermanager, switch
114 * to the per enterprise slice of keychain.
117 UMUserPersona * persona = [[UMUserManager sharedManager] currentPersona];
118 if (persona && persona.userPersonaType == UMUserPersonaTypeEnterprise) {
119 secinfo("serverxpc", "securityd client: enterprise user");
122 if (uuid_parse([persona.userPersonaUniqueString UTF8String], uuid) != 0) {
125 client->musr = CFDataCreate(NULL, uuid, sizeof(uuid_t));
128 #endif /* TARGET_OS_IOS */
130 client->task = SecTaskCreateWithAuditToken(kCFAllocatorDefault, auditToken);
132 client->accessGroups = SecTaskCopyAccessGroups(client->task);
135 client->allowSystemKeychain = SecTaskGetBooleanValueForEntitlement(client->task, kSecEntitlementPrivateSystemKeychain);
136 client->isNetworkExtension = SecTaskGetBooleanValueForEntitlement(client->task, kSecEntitlementPrivateNetworkExtension);
137 client->canAccessNetworkExtensionAccessGroups = SecTaskGetBooleanValueForEntitlement(client->task, kSecEntitlementNetworkExtensionAccessGroups);
139 #if HAVE_MOBILE_KEYBAG_SUPPORT && TARGET_OS_IOS
140 if (client->inMultiUser) {
141 client->allowSyncBubbleKeychain = SecTaskGetBooleanValueForEntitlement(client->task, kSecEntitlementPrivateKeychainSyncBubble);