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 #import <Foundation/Foundation.h>
26 #include <ipc/securityd_client.h>
27 #include <ipc/server_security_helpers.h>
28 #include <ipc/server_endpoint.h>
30 #include <Security/SecEntitlements.h>
31 #include <Security/SecItemPriv.h>
32 #include <securityd/SecItemServer.h>
33 #include <securityd/SecItemSchema.h>
34 #include <securityd/SecItemDb.h>
36 #include "keychain/ckks/CKKSViewManager.h"
38 @implementation SecuritydXPCServer (SecuritydXPCProtocol)
40 - (void) SecItemAddAndNotifyOnSync:(NSDictionary*) attributes
41 syncCallback:(id<SecuritydXPCCallbackProtocol>) callback
42 complete:(void (^) (NSDictionary* opDictResult, NSArray* opArrayResult, NSError* operror)) complete
44 CFErrorRef cferror = NULL;
45 if([self clientHasBooleanEntitlement: (__bridge NSString*) kSecEntitlementKeychainDeny]) {
46 SecError(errSecNotAvailable, &cferror, CFSTR("SecItemAddAndNotifyOnSync: %@ has entitlement %@"), _client.task, kSecEntitlementKeychainDeny);
47 //TODO: ensure cferror can transit xpc
48 complete(NULL, NULL, (__bridge NSError*) cferror);
49 CFReleaseNull(cferror);
53 if(attributes[(id)kSecAttrDeriveSyncIDFromItemAttributes] ||
54 attributes[(id)kSecAttrPCSPlaintextServiceIdentifier] ||
55 attributes[(id)kSecAttrPCSPlaintextPublicKey] ||
56 attributes[(id)kSecAttrPCSPlaintextPublicIdentity]) {
58 if(![self clientHasBooleanEntitlement: (__bridge NSString*) kSecEntitlementPrivateCKKSPlaintextFields]) {
59 SecError(errSecMissingEntitlement, &cferror, CFSTR("SecItemAddAndNotifyOnSync: %@ does not have entitlement %@, but is using SPI anyway"), _client.task, kSecEntitlementPrivateCKKSPlaintextFields);
60 complete(NULL, NULL, (__bridge NSError*) cferror);
61 CFReleaseNull(cferror);
66 CFTypeRef cfresult = NULL;
68 NSMutableDictionary* callbackQuery = [attributes mutableCopy];
69 callbackQuery[@"f_ckkscallback"] = ^void (bool didSync, CFErrorRef syncerror) {
70 [callback callCallback: didSync error: (__bridge NSError*)syncerror];
73 _SecItemAdd((__bridge CFDictionaryRef) callbackQuery, &_client, &cfresult, &cferror);
75 //TODO: ensure cferror can transit xpc
77 // SecItemAdd returns Some CF Object, but NSXPC is pretty adamant that everything be a specific NS type. Split it up here:
79 complete(NULL, NULL, (__bridge NSError *)(cferror));
80 } else if( CFGetTypeID(cfresult) == CFDictionaryGetTypeID()) {
81 complete((__bridge NSDictionary *)(cfresult), NULL, (__bridge NSError *)(cferror));
82 } else if( CFGetTypeID(cfresult) == CFArrayGetTypeID()) {
83 complete(NULL, (__bridge NSArray *)cfresult, (__bridge NSError *)(cferror));
85 // TODO: actually error here
86 complete(NULL, NULL, NULL);
88 CFReleaseNull(cfresult);
89 CFReleaseNull(cferror);
92 - (void)secItemSetCurrentItemAcrossAllDevices:(NSData* _Nonnull)newItemPersistentRef
93 newCurrentItemHash:(NSData* _Nonnull)newItemSHA1
94 accessGroup:(NSString* _Nonnull)accessGroup
95 identifier:(NSString* _Nonnull)identifier
96 viewHint:(NSString* _Nonnull)viewHint
97 oldCurrentItemReference:(NSData* _Nullable)oldCurrentItemPersistentRef
98 oldCurrentItemHash:(NSData* _Nullable)oldItemSHA1
99 complete:(void (^) (NSError* _Nullable operror)) complete
102 __block CFErrorRef cferror = NULL;
103 if([self clientHasBooleanEntitlement: (__bridge NSString*) kSecEntitlementKeychainDeny]) {
104 SecError(errSecNotAvailable, &cferror, CFSTR("SecItemSetCurrentItemAcrossAllDevices: %@ has entitlement %@"), _client.task, kSecEntitlementKeychainDeny);
105 complete((__bridge NSError*) cferror);
106 CFReleaseNull(cferror);
110 if(![self clientHasBooleanEntitlement: (__bridge NSString*) kSecEntitlementPrivateCKKSWriteCurrentItemPointers]) {
111 SecError(errSecMissingEntitlement, &cferror, CFSTR("SecItemSetCurrentItemAcrossAllDevices: %@ does not have entitlement %@"), _client.task, kSecEntitlementPrivateCKKSWriteCurrentItemPointers);
112 complete((__bridge NSError*) cferror);
113 CFReleaseNull(cferror);
117 if (!accessGroupsAllows(self->_client.accessGroups, (__bridge CFStringRef)accessGroup, &_client)) {
118 SecError(errSecMissingEntitlement, &cferror, CFSTR("SecItemSetCurrentItemAcrossAllDevices: client is missing access-group %@: %@"), accessGroup, _client.task);
119 complete((__bridge NSError*)cferror);
120 CFReleaseNull(cferror);
124 __block SecDbItemRef newItem = NULL;
125 __block SecDbItemRef oldItem = NULL;
127 bool ok = kc_with_dbt(false, &cferror, ^bool (SecDbConnectionRef dbt) {
128 Query *q = query_create_with_limit( (__bridge CFDictionaryRef) @{
129 (__bridge NSString *)kSecValuePersistentRef : newItemPersistentRef,
130 (__bridge NSString *)kSecAttrAccessGroup : accessGroup,
136 secerror("couldn't create query: %@", cferror);
140 if(!SecDbItemQuery(q, NULL, dbt, &cferror, ^(SecDbItemRef item, bool *stop) {
141 newItem = CFRetainSafe(item);
143 query_destroy(q, NULL);
147 if(!query_destroy(q, &cferror)) {
151 if(oldCurrentItemPersistentRef) {
152 q = query_create_with_limit( (__bridge CFDictionaryRef) @{
153 (__bridge NSString *)kSecValuePersistentRef : oldCurrentItemPersistentRef,
154 (__bridge NSString *)kSecAttrAccessGroup : accessGroup,
160 secerror("couldn't create query: %@", cferror);
164 if(!SecDbItemQuery(q, NULL, dbt, &cferror, ^(SecDbItemRef item, bool *stop) {
165 oldItem = CFRetainSafe(item);
167 query_destroy(q, NULL);
171 if(!query_destroy(q, &cferror)) {
176 CKKSViewManager* manager = [CKKSViewManager manager];
178 secerror("SecItemSetCurrentItemAcrossAllDevices: no view manager?");
179 cferror = (CFErrorRef) CFBridgingRetain([NSError errorWithDomain:@"securityd" code:errSecInternalError userInfo:@{NSLocalizedDescriptionKey: @"No view manager, cannot forward request"}]);
182 [manager setCurrentItemForAccessGroup:newItem
184 accessGroup:accessGroup
185 identifier:identifier
193 CFReleaseNull(newItem);
194 CFReleaseNull(oldItem);
197 secnotice("ckks", "SecItemSetCurrentItemAcrossAllDevices failed due to: %@", cferror);
198 complete((__bridge NSError*) cferror);
200 CFReleaseNull(cferror);
202 complete([NSError errorWithDomain:@"securityd" code:errSecParam userInfo:@{NSLocalizedDescriptionKey: @"SecItemSetCurrentItemAcrossAllDevices not implemented on this platform"}]);
206 -(void)secItemFetchCurrentItemAcrossAllDevices:(NSString*)accessGroup
207 identifier:(NSString*)identifier
208 viewHint:(NSString*)viewHint
209 fetchCloudValue:(bool)fetchCloudValue
210 complete:(void (^) (NSData* persistentref, NSError* operror)) complete
213 CFErrorRef cferror = NULL;
214 if([self clientHasBooleanEntitlement: (__bridge NSString*) kSecEntitlementKeychainDeny]) {
215 SecError(errSecNotAvailable, &cferror, CFSTR("SecItemFetchCurrentItemAcrossAllDevices: %@ has entitlement %@"), _client.task, kSecEntitlementKeychainDeny);
216 complete(NULL, (__bridge NSError*) cferror);
217 CFReleaseNull(cferror);
221 if(![self clientHasBooleanEntitlement: (__bridge NSString*) kSecEntitlementPrivateCKKSReadCurrentItemPointers]) {
222 SecError(errSecNotAvailable, &cferror, CFSTR("SecItemFetchCurrentItemAcrossAllDevices: %@ does not have entitlement %@"), _client.task, kSecEntitlementPrivateCKKSReadCurrentItemPointers);
223 complete(NULL, (__bridge NSError*) cferror);
224 CFReleaseNull(cferror);
228 if (!accessGroupsAllows(self->_client.accessGroups, (__bridge CFStringRef)accessGroup, &_client)) {
229 SecError(errSecMissingEntitlement, &cferror, CFSTR("SecItemFetchCurrentItemAcrossAllDevices: client is missing access-group %@: %@"), accessGroup, _client.task);
230 complete(NULL, (__bridge NSError*)cferror);
231 CFReleaseNull(cferror);
235 [[CKKSViewManager manager] getCurrentItemForAccessGroup:accessGroup
236 identifier:identifier
238 fetchCloudValue:fetchCloudValue
239 complete:^(NSString* uuid, NSError* error) {
241 complete(NULL, error);
245 // Find the persisent ref and return it.
246 [self findItemPersistentRefByUUID:uuid complete:complete];
249 complete(NULL, [NSError errorWithDomain:@"securityd" code:errSecParam userInfo:@{NSLocalizedDescriptionKey: @"SecItemFetchCurrentItemAcrossAllDevices not implemented on this platform"}]);
253 -(void)findItemPersistentRefByUUID:(NSString*)uuid
254 complete:(void (^) (NSData* persistentref, NSError* operror)) complete
256 CFErrorRef cferror = NULL;
257 CFTypeRef result = NULL;
259 // Must query per-class, so:
260 const SecDbSchema *newSchema = current_schema();
261 for (const SecDbClass *const *class = newSchema->classes; *class != NULL; class++) {
262 CFReleaseNull(result);
263 CFReleaseNull(cferror);
265 if(!((*class)->itemclass)) {
266 //Don't try to search non-item 'classes'
270 _SecItemCopyMatching((__bridge CFDictionaryRef) @{
271 (__bridge NSString*) kSecClass: (__bridge NSString*) (*class)->name,
272 (id)kSecAttrSynchronizable: (id)kSecAttrSynchronizableAny,
273 (id)kSecMatchLimit : (id)kSecMatchLimitOne,
274 (id)kSecAttrUUID: uuid,
275 (id)kSecReturnPersistentRef: @YES,
281 if(cferror && CFErrorGetCode(cferror) != errSecItemNotFound) {
286 // Found the persistent ref! Quit searching.
291 complete((__bridge NSData*) result, (__bridge NSError*) cferror);
292 CFReleaseNull(result);
293 CFReleaseNull(cferror);
296 - (void) secItemDigest:(NSString *)itemClass
297 accessGroup:(NSString *)accessGroup
298 complete:(void (^)(NSArray *digest, NSError* error))complete
300 CFArrayRef accessGroups = self->_client.accessGroups;
301 __block CFErrorRef cferror = NULL;
302 __block CFArrayRef result = NULL;
304 if (itemClass == NULL || accessGroup == NULL) {
305 SecError(errSecParam, &cferror, CFSTR("parameter missing: %@"), _client.task);
306 complete(NULL, (__bridge NSError*) cferror);
307 CFReleaseNull(cferror);
311 if (![itemClass isEqualToString:@"inet"] && ![itemClass isEqualToString:@"genp"]) {
312 SecError(errSecParam, &cferror, CFSTR("class %@ is not supported: %@"), itemClass, _client.task);
313 complete(NULL, (__bridge NSError*) cferror);
314 CFReleaseNull(cferror);
318 if (!accessGroupsAllows(accessGroups, (__bridge CFStringRef)accessGroup, &_client)) {
319 SecError(errSecMissingEntitlement, &cferror, CFSTR("Client is missing access-group %@: %@"), accessGroup, _client.task);
320 complete(NULL, (__bridge NSError*) cferror);
321 CFReleaseNull(cferror);
325 if (CFArrayContainsValue(accessGroups, CFRangeMake(0, CFArrayGetCount(accessGroups)), CFSTR("*"))) {
326 /* Having the special accessGroup "*" allows access to all accessGroups. */
330 NSDictionary *attributes = @{
331 (__bridge NSString *)kSecClass : itemClass,
332 (__bridge NSString *)kSecAttrAccessGroup : accessGroup,
333 (__bridge NSString *)kSecAttrSynchronizable : (__bridge NSString *)kSecAttrSynchronizableAny,
336 Query *q = query_create_with_limit((__bridge CFDictionaryRef)attributes, _client.musr, 0, &cferror);
338 SecError(errSecParam, &cferror, CFSTR("failed to build query: %@"), _client.task);
339 complete(NULL, (__bridge NSError*) cferror);
340 CFReleaseNull(cferror);
344 bool ok = kc_with_dbt(false, &cferror, ^(SecDbConnectionRef dbt) {
345 return (bool)s3dl_copy_digest(dbt, q, &result, accessGroups, &cferror);
350 complete((__bridge NSArray *)result, (__bridge NSError *)cferror);
352 (void)query_destroy(q, &cferror);
354 CFReleaseNull(result);
355 CFReleaseNull(cferror);