2 * Copyright (c) 2007-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@
26 #undef SECUREOBJECTSYNC
27 #undef SHAREDWEBCREDENTIALS
30 #include <os/transaction_private.h>
31 #include <os/variant_private.h>
33 #include "keychain/SecureObjectSync/SOSPeerInfoDER.h"
34 #include <Security/SecureObjectSync/SOSCloudCircle.h>
35 #include <Security/SecureObjectSync/SOSCloudCircleInternal.h>
36 #include "keychain/SecureObjectSync/SOSInternal.h"
37 #include "keychain/SecureObjectSync/SOSPeerInfoCollections.h"
38 #include "keychain/SecureObjectSync/SOSControlServer.h"
39 #include <Security/SecBase.h>
40 #include <Security/SecBasePriv.h>
41 #include <Security/SecCertificatePriv.h>
42 #include <Security/SecEntitlements.h>
43 #include <Security/SecInternal.h>
44 #include <Security/SecItem.h>
45 #include <Security/SecItemPriv.h>
46 #include <Security/SecPolicy.h>
47 #include <Security/SecPolicyInternal.h>
48 #include <Security/SecTask.h>
49 #include <Security/SecTrustInternal.h>
50 #include <Security/SecuritydXPC.h>
51 #include "trust/trustd/OTATrustUtilities.h"
52 #include "keychain/securityd/SOSCloudCircleServer.h"
53 #include "keychain/securityd/SecItemBackupServer.h"
54 #include "keychain/securityd/SecItemServer.h"
55 #include "keychain/securityd/SecLogSettingsServer.h"
56 #include "keychain/securityd/SecOTRRemote.h"
57 #include "trust/trustd/SecTrustServer.h"
58 #include "trust/trustd/SecTrustStoreServer.h"
59 #include "keychain/securityd/iCloudTrace.h"
60 #include "keychain/securityd/spi.h"
61 #include <utilities/SecCFError.h>
62 #include <utilities/SecCFWrappers.h>
63 #include <utilities/SecDb.h>
64 #include <utilities/SecIOFormat.h>
65 #include <utilities/SecXPCError.h>
66 #include <utilities/debugging.h>
67 #include <utilities/SecInternalReleasePriv.h>
68 #include <utilities/der_plist_internal.h>
69 #include <utilities/der_plist.h>
70 #include "trust/trustd/personalization.h"
71 #include "trust/trustd/SecPinningDb.h"
72 #include "keychain/securityd/SFKeychainControlManager.h"
74 #include <keychain/ckks/CKKS.h>
75 #include <keychain/ckks/CKKSControlServer.h>
76 #include "keychain/ot/OctagonControlServer.h"
78 #include "keychain/securityd/SFKeychainServer.h"
80 #include "keychain/securityd/PolicyReporter.h"
83 #include <AssertMacros.h>
84 #include <CoreFoundation/CFXPCBridge.h>
85 #include <CoreFoundation/CoreFoundation.h>
86 // <rdar://problem/22425706> 13B104+Roots:Device never moved past spinner after using approval to ENABLE icdp
89 #include <Security/SecTaskPriv.h>
90 #include <login/SessionAgentStatusCom.h>
93 #include <bsm/libbsm.h>
94 #include <ipc/securityd_client.h>
95 #include <libkern/OSAtomic.h>
96 #include <mach/mach.h>
97 #include <mach/message.h>
100 #include <sys/queue.h>
101 #include <sys/sysctl.h>
103 #include <xpc/private.h>
106 #include <ipc/server_security_helpers.h>
107 #include <ipc/server_entitlement_helpers.h>
109 #include "keychain/ot/OT.h"
110 #include "keychain/escrowrequest/EscrowRequestXPCServer.h"
111 #include "keychain/escrowrequest/EscrowRequestServerHelpers.h"
125 _xpc_dictionary_copy_CFString(xpc_object_t xdict
, const char *key
)
127 CFStringRef result
= NULL
;
128 const char *str
= xpc_dictionary_get_string(xdict
, key
);
130 result
= CFStringCreateWithCString(kCFAllocatorDefault
, str
, kCFStringEncodingUTF8
);
137 _xpc_dictionary_copy_CFDataNoCopy(xpc_object_t xdict
, const char *key
)
139 CFDataRef result
= NULL
;
141 const void *ptr
= xpc_dictionary_get_data(xdict
, key
, &len
);
143 result
= CFDataCreateWithBytesNoCopy(kCFAllocatorDefault
, ptr
, len
, kCFAllocatorNull
);
148 static void with_label_and_password(xpc_object_t message
, void (^action
)(CFStringRef label
, CFDataRef password
)) {
149 CFStringRef user_label
= _xpc_dictionary_copy_CFString(message
, kSecXPCKeyUserLabel
);
150 CFDataRef user_password
= _xpc_dictionary_copy_CFDataNoCopy(message
, kSecXPCKeyUserPassword
);
152 if (user_label
!= NULL
&& user_password
!= NULL
) {
153 action(user_label
, user_password
);
156 CFReleaseNull(user_label
);
157 CFReleaseNull(user_password
);
160 static void with_label_and_password_and_dsid(xpc_object_t message
, void (^action
)(CFStringRef label
, CFDataRef password
, CFStringRef dsid
)) {
161 CFStringRef user_label
= _xpc_dictionary_copy_CFString(message
, kSecXPCKeyUserLabel
);
162 CFDataRef user_password
= _xpc_dictionary_copy_CFDataNoCopy(message
, kSecXPCKeyUserPassword
);
163 CFStringRef dsid
= _xpc_dictionary_copy_CFString(message
, kSecXPCKeyDSID
);
165 /* dsid is optional */
166 if (user_label
!= NULL
&& user_password
!= NULL
) {
167 action(user_label
, user_password
, dsid
);
170 CFReleaseNull(user_label
);
171 CFReleaseNull(user_password
);
175 static void with_view_and_action(xpc_object_t message
, void (^action
)(CFStringRef view_name
, uint64_t view_action_code
)) {
176 CFStringRef view
= _xpc_dictionary_copy_CFString(message
, kSecXPCKeyViewName
);
177 const int64_t number
= xpc_dictionary_get_int64(message
, kSecXPCKeyViewActionCode
);
180 action(view
, number
);
186 static CFArrayRef
SecXPCDictionaryCopyPeerInfoArray(xpc_object_t dictionary
, const char *key
, CFErrorRef
*error
) {
187 return CreateArrayOfPeerInfoWithXPCObject(xpc_dictionary_get_value(dictionary
, key
), error
);
190 static CFDataRef
SecXPCDictionaryCopyCFDataRef(xpc_object_t message
, const char *key
, CFErrorRef
*error
) {
191 CFDataRef retval
= NULL
;
192 const uint8_t *bytes
= NULL
;
195 bytes
= xpc_dictionary_get_data(message
, key
, &len
);
196 require_action_quiet(bytes
, errOut
, SOSCreateError(kSOSErrorBadKey
, CFSTR("missing CFDataRef info"), NULL
, error
));
197 retval
= CFDataCreate(NULL
, bytes
, len
);
198 require_action_quiet(retval
, errOut
, SOSCreateError(kSOSErrorBadKey
, CFSTR("could not allocate CFDataRef info"), NULL
, error
));
203 static CFSetRef
CreateCFSetRefFromXPCObject(xpc_object_t xpcSetDER
, CFErrorRef
* error
) {
204 CFSetRef retval
= NULL
;
205 require_action_quiet(xpcSetDER
, errOut
, SecCFCreateErrorWithFormat(kSecXPCErrorUnexpectedNull
, sSecXPCErrorDomain
, NULL
, error
, NULL
, CFSTR("Unexpected Null Set to decode")));
207 require_action_quiet(xpc_get_type(xpcSetDER
) == XPC_TYPE_DATA
, errOut
, SecCFCreateErrorWithFormat(kSecXPCErrorUnexpectedType
, sSecXPCErrorDomain
, NULL
, error
, NULL
, CFSTR("xpcSetDER not data, got %@"), xpcSetDER
));
209 const uint8_t* der
= xpc_data_get_bytes_ptr(xpcSetDER
);
210 const uint8_t* der_end
= der
+ xpc_data_get_length(xpcSetDER
);
211 der
= der_decode_set(kCFAllocatorDefault
, kCFPropertyListMutableContainersAndLeaves
, &retval
, error
, der
, der_end
);
212 if (der
!= der_end
) {
213 SecError(errSecDecode
, error
, CFSTR("trailing garbage at end of SecAccessControl data"));
218 CFReleaseNull(retval
);
222 static SOSPeerInfoRef
SecXPCDictionaryCopyPeerInfo(xpc_object_t message
, const char *key
, CFErrorRef
*error
) {
224 const uint8_t *der
= xpc_dictionary_get_data(message
, key
, &length
);
226 return SecRequirementError(der
!= NULL
, error
, CFSTR("No data for key %s"), key
) ? SOSPeerInfoCreateFromDER(kCFAllocatorDefault
, error
, &der
, der
+ length
) : NULL
;
229 static CFSetRef
SecXPCSetCreateFromXPCDictionaryElement(xpc_object_t event
, const char *key
) {
230 CFErrorRef error
= NULL
;
231 xpc_object_t object
= xpc_dictionary_get_value(event
, key
);
232 CFSetRef retval
= NULL
;
233 if(object
) retval
= CreateCFSetRefFromXPCObject(object
, &error
);
234 CFReleaseNull(error
);
239 void xpc_dictionary_set_and_consume_CFArray(xpc_object_t xdict
, const char *key
, CF_CONSUMED CFArrayRef cf_array
) {
241 xpc_object_t xpc_array
= _CFXPCCreateXPCObjectFromCFObject(cf_array
);
242 xpc_dictionary_set_value(xdict
, key
, xpc_array
);
243 xpc_release(xpc_array
);
245 CFReleaseNull(cf_array
);
249 bool xpc_dictionary_set_and_consume_PeerInfoArray(xpc_object_t xdict
, const char *key
, CF_CONSUMED CFArrayRef cf_array
, CFErrorRef
*error
) {
252 xpc_object_t xpc_array
= CreateXPCObjectWithArrayOfPeerInfo(cf_array
, error
);
254 xpc_dictionary_set_value(xdict
, key
, xpc_array
);
255 xpc_release(xpc_array
);
260 CFReleaseNull(cf_array
);
264 #endif /* SECUREOBJECTSYNC */
267 SecDataCopyMmapFileDescriptor(int fd
, void **mem
, size_t *size
, CFErrorRef
*error
)
270 if (fstat(fd
, &sb
) < 0) {
274 *size
= (size_t)sb
.st_size
;
275 if ((off_t
)*size
!= sb
.st_size
) {
279 *mem
= mmap(NULL
, *size
, PROT_READ
, MAP_SHARED
, fd
, 0);
280 if (*mem
== MAP_FAILED
) {
284 return CFDataCreateWithBytesNoCopy(NULL
, *mem
, *size
, kCFAllocatorNull
);
288 SecDataWriteFileDescriptor(int fd
, CFDataRef data
)
290 CFIndex count
= CFDataGetLength(data
);
291 const uint8_t *ptr
= CFDataGetBytePtr(data
);
292 bool writeResult
= false;
295 ssize_t ret
= write(fd
, ptr
, count
);
307 // Returns error if entitlement isn't present.
309 EntitlementPresentAndTrue(uint64_t op
, SecTaskRef clientTask
, CFStringRef entitlement
, CFErrorRef
*error
)
311 if (!SecTaskGetBooleanValueForEntitlement(clientTask
, entitlement
)) {
312 SecError(errSecMissingEntitlement
, error
, CFSTR("%@: %@ lacks entitlement %@"), SOSCCGetOperationDescription((enum SecXPCOperation
)op
), clientTask
, entitlement
);
319 EntitlementAbsentOrFalse(uint64_t op
, SecTaskRef clientTask
, CFStringRef entitlement
, CFErrorRef
*error
)
321 if (SecTaskGetBooleanValueForEntitlement(clientTask
, entitlement
)) {
322 SecError(errSecNotAvailable
, error
, CFSTR("%@: %@ has entitlement %@"), SOSCCGetOperationDescription((enum SecXPCOperation
) op
), clientTask
, entitlement
);
328 static void securityd_xpc_dictionary_handler(const xpc_connection_t connection
, xpc_object_t event
) {
329 xpc_type_t type
= xpc_get_type(event
);
330 __block CFErrorRef error
= NULL
;
331 xpc_object_t xpcError
= NULL
;
332 xpc_object_t replyMessage
= NULL
;
333 CFDataRef clientAuditToken
= NULL
;
334 CFArrayRef domains
= NULL
;
335 SecurityClient client
= {
337 .accessGroups
= NULL
,
339 .uid
= xpc_connection_get_euid(connection
),
340 .allowSystemKeychain
= false,
341 .allowSyncBubbleKeychain
= false,
342 .isNetworkExtension
= false,
343 .canAccessNetworkExtensionAccessGroups
= false,
346 secdebug("serverxpc", "entering");
347 if (type
== XPC_TYPE_DICTIONARY
) {
348 // TODO: Find out what we're dispatching.
349 replyMessage
= xpc_dictionary_create_reply(event
);
351 uint64_t operation
= xpc_dictionary_get_uint64(event
, kSecXPCKeyOperation
);
353 audit_token_t auditToken
= {};
354 xpc_connection_get_audit_token(connection
, &auditToken
);
355 clientAuditToken
= CFDataCreate(kCFAllocatorDefault
, (const UInt8
*)&auditToken
, sizeof(auditToken
));
357 if (!fill_security_client(&client
, xpc_connection_get_euid(connection
), auditToken
)) {
358 CFReleaseNull(clientAuditToken
);
359 xpc_connection_send_message(connection
, replyMessage
);
360 xpc_release(replyMessage
);
365 if (operation
== sec_add_shared_web_credential_id
|| operation
== sec_copy_shared_web_credential_id
) {
366 domains
= SecTaskCopySharedWebCredentialDomains(client
.task
);
369 secinfo("serverxpc", "XPC [%@] operation: %@ (%" PRIu64
")", client
.task
, SOSCCGetOperationDescription((enum SecXPCOperation
)operation
), operation
);
373 case sec_item_add_id
:
375 if (EntitlementAbsentOrFalse(sec_item_add_id
, client
.task
, kSecEntitlementKeychainDeny
, &error
)) {
376 CFDictionaryRef query
= SecXPCDictionaryCopyDictionary(event
, kSecXPCKeyQuery
, &error
);
378 // Check for any entitlement-required attributes
379 bool entitlementsCorrect
= true;
380 if(CFDictionaryGetValue(query
, kSecAttrDeriveSyncIDFromItemAttributes
) ||
381 CFDictionaryGetValue(query
, kSecAttrPCSPlaintextServiceIdentifier
) ||
382 CFDictionaryGetValue(query
, kSecAttrPCSPlaintextPublicKey
) ||
383 CFDictionaryGetValue(query
, kSecAttrPCSPlaintextPublicIdentity
)) {
384 entitlementsCorrect
= EntitlementPresentAndTrue(sec_item_add_id
, client
.task
, kSecEntitlementPrivateCKKSPlaintextFields
, &error
);
386 if (entitlementsCorrect
&& CFDictionaryGetValue(query
, kSecAttrSysBound
)) {
387 entitlementsCorrect
= EntitlementPresentAndTrue(sec_item_add_id
, client
.task
, kSecEntitlementPrivateSysBound
, &error
);
390 CFTypeRef result
= NULL
;
391 if(entitlementsCorrect
) {
392 if (_SecItemAdd(query
, &client
, &result
, &error
) && result
) {
393 SecXPCDictionarySetPList(replyMessage
, kSecXPCKeyResult
, result
, &error
);
394 CFReleaseNull(result
);
397 CFReleaseNull(query
);
402 case sec_item_copy_matching_id
:
404 if (EntitlementAbsentOrFalse(sec_item_add_id
, client
.task
, kSecEntitlementKeychainDeny
, &error
)) {
405 CFDictionaryRef query
= SecXPCDictionaryCopyDictionary(event
, kSecXPCKeyQuery
, &error
);
407 CFTypeRef result
= NULL
;
408 if (_SecItemCopyMatching(query
, &client
, &result
, &error
) && result
) {
409 SecXPCDictionarySetPList(replyMessage
, kSecXPCKeyResult
, result
, &error
);
410 CFReleaseNull(result
);
412 CFReleaseNull(query
);
417 case sec_item_update_id
:
419 if (EntitlementAbsentOrFalse(sec_item_update_id
, client
.task
, kSecEntitlementKeychainDeny
, &error
)) {
420 CFDictionaryRef query
= SecXPCDictionaryCopyDictionary(event
, kSecXPCKeyQuery
, &error
);
422 CFDictionaryRef attributesToUpdate
= SecXPCDictionaryCopyDictionary(event
, kSecXPCKeyAttributesToUpdate
, &error
);
423 if (attributesToUpdate
) {
424 // Check for any entitlement-required attributes
425 bool entitlementsCorrect
= true;
426 if(CFDictionaryGetValue(query
, kSecAttrDeriveSyncIDFromItemAttributes
) ||
427 CFDictionaryGetValue(attributesToUpdate
, kSecAttrPCSPlaintextServiceIdentifier
) ||
428 CFDictionaryGetValue(attributesToUpdate
, kSecAttrPCSPlaintextPublicKey
) ||
429 CFDictionaryGetValue(attributesToUpdate
, kSecAttrPCSPlaintextPublicIdentity
)) {
430 entitlementsCorrect
= EntitlementPresentAndTrue(sec_item_update_id
, client
.task
, kSecEntitlementPrivateCKKSPlaintextFields
, &error
);
432 if (entitlementsCorrect
&& CFDictionaryGetValue(query
, kSecAttrSysBound
)) {
433 entitlementsCorrect
= EntitlementPresentAndTrue(sec_item_update_id
, client
.task
, kSecEntitlementPrivateSysBound
, &error
);
436 if(entitlementsCorrect
) {
437 bool result
= _SecItemUpdate(query
, attributesToUpdate
, &client
, &error
);
438 xpc_dictionary_set_bool(replyMessage
, kSecXPCKeyResult
, result
);
440 CFReleaseNull(attributesToUpdate
);
442 CFReleaseNull(query
);
447 case sec_item_delete_id
:
449 if (EntitlementAbsentOrFalse(sec_item_add_id
, client
.task
, kSecEntitlementKeychainDeny
, &error
)) {
450 CFDictionaryRef query
= SecXPCDictionaryCopyDictionary(event
, kSecXPCKeyQuery
, &error
);
452 bool result
= _SecItemDelete(query
, &client
, &error
);
453 xpc_dictionary_set_bool(replyMessage
, kSecXPCKeyResult
, result
);
454 CFReleaseNull(query
);
459 case sec_item_update_token_items_id
:
461 if (EntitlementAbsentOrFalse(sec_item_add_id
, client
.task
, kSecEntitlementKeychainDeny
, &error
)) {
462 CFStringRef tokenID
= SecXPCDictionaryCopyString(event
, kSecXPCKeyString
, &error
);
463 CFArrayRef attributes
= SecXPCDictionaryCopyArray(event
, kSecXPCKeyQuery
, &error
);
465 bool result
= _SecItemUpdateTokenItems(tokenID
, attributes
, &client
, &error
);
466 xpc_dictionary_set_bool(replyMessage
, kSecXPCKeyResult
, result
);
468 CFReleaseNull(tokenID
);
469 CFReleaseNull(attributes
);
473 case sec_delete_all_id
:
477 /* buddy is temporary allowed to do this */
478 CFStringRef applicationIdentifier
= SecTaskCopyApplicationIdentifier(client
.task
);
479 bool isBuddy
= applicationIdentifier
&&
480 CFEqual(applicationIdentifier
, CFSTR("com.apple.purplebuddy"));
481 CFReleaseNull(applicationIdentifier
);
483 if (isBuddy
|| EntitlementPresentAndTrue(operation
, client
.task
, kSecEntitlementPrivateDeleteAll
, &error
))
485 retval
= _SecItemDeleteAll(&error
);
488 xpc_dictionary_set_bool(replyMessage
, kSecXPCKeyResult
, retval
);
491 case sec_keychain_backup_id
:
493 if (EntitlementPresentAndTrue(operation
, client
.task
, kSecEntitlementRestoreKeychain
, &error
)) {
494 CFDataRef keybag
= NULL
, passcode
= NULL
;
495 if (SecXPCDictionaryCopyDataOptional(event
, kSecXPCKeyKeybag
, &keybag
, &error
)) {
496 if (SecXPCDictionaryCopyDataOptional(event
, kSecXPCKeyUserPassword
, &passcode
, &error
)) {
497 bool emcs
= SecXPCDictionaryGetBool(event
, kSecXPCKeyEMCSBackup
, NULL
);
498 CFDataRef backup
= _SecServerKeychainCreateBackup(&client
, keybag
, passcode
, emcs
, &error
);
500 int fd
= SecXPCDictionaryDupFileDescriptor(event
, kSecXPCKeyFileDescriptor
, NULL
);
502 SecXPCDictionarySetData(replyMessage
, kSecXPCKeyResult
, backup
, &error
);
504 bool writeResult
= SecDataWriteFileDescriptor(fd
, backup
);
508 SecError(errSecIO
, &error
, CFSTR("Failed to write backup file: %d"), errno
);
509 SecXPCDictionarySetBool(replyMessage
, kSecXPCKeyResult
, writeResult
, NULL
);
513 CFReleaseSafe(passcode
);
515 CFReleaseSafe(keybag
);
520 case sec_keychain_restore_id
:
522 if (EntitlementPresentAndTrue(operation
, client
.task
, kSecEntitlementRestoreKeychain
, &error
)) {
523 CFDataRef backup
= NULL
;
527 int fd
= SecXPCDictionaryDupFileDescriptor(event
, kSecXPCKeyFileDescriptor
, NULL
);
529 backup
= SecDataCopyMmapFileDescriptor(fd
, &mem
, &size
, &error
);
531 backup
= SecXPCDictionaryCopyData(event
, kSecXPCKeyBackup
, &error
);
534 CFDataRef keybag
= SecXPCDictionaryCopyData(event
, kSecXPCKeyKeybag
, &error
);
536 CFDataRef passcode
= NULL
;
537 if (SecXPCDictionaryCopyDataOptional(event
, kSecXPCKeyUserPassword
, &passcode
, &error
)) {
538 bool result
= _SecServerKeychainRestore(backup
, &client
, keybag
, passcode
, &error
);
539 xpc_dictionary_set_bool(replyMessage
, kSecXPCKeyResult
, result
);
540 CFReleaseSafe(passcode
);
543 CFReleaseNull(keybag
);
545 CFReleaseNull(backup
);
554 case sec_keychain_backup_keybag_uuid_id
:
556 if (EntitlementPresentAndTrue(operation
, client
.task
, kSecEntitlementRestoreKeychain
, &error
)) {
557 CFDataRef backup
= NULL
;
558 CFStringRef uuid
= NULL
;
562 int fd
= SecXPCDictionaryDupFileDescriptor(event
, kSecXPCKeyFileDescriptor
, NULL
);
564 backup
= SecDataCopyMmapFileDescriptor(fd
, &mem
, &size
, &error
);
566 uuid
= _SecServerBackupCopyUUID(backup
, &error
);
569 SecXPCDictionarySetString(replyMessage
, kSecXPCKeyResult
, uuid
, &error
);
571 CFReleaseNull(backup
);
582 case sec_keychain_sync_update_message_id
:
584 CFDictionaryRef updates
= SecXPCDictionaryCopyDictionary(event
, kSecXPCKeyQuery
, &error
);
586 CFArrayRef result
= _SecServerKeychainSyncUpdateMessage(updates
, &error
);
587 SecXPCDictionarySetPList(replyMessage
, kSecXPCKeyResult
, result
, &error
);
588 CFReleaseNull(result
);
590 CFReleaseNull(updates
);
593 case sec_keychain_backup_syncable_id
:
595 if (EntitlementPresentAndTrue(operation
, client
.task
, kSecEntitlementRestoreKeychain
, &error
)) {
596 CFDictionaryRef oldbackup
= NULL
;
597 if (SecXPCDictionaryCopyDictionaryOptional(event
, kSecXPCKeyBackup
, &oldbackup
, &error
)) {
598 CFDataRef keybag
= SecXPCDictionaryCopyData(event
, kSecXPCKeyKeybag
, &error
);
600 CFDataRef passcode
= NULL
;
601 if (SecXPCDictionaryCopyDataOptional(event
, kSecXPCKeyUserPassword
, &passcode
, &error
)) {
602 CFDictionaryRef newbackup
= _SecServerBackupSyncable(oldbackup
, keybag
, passcode
, &error
);
604 SecXPCDictionarySetPList(replyMessage
, kSecXPCKeyResult
, newbackup
, &error
);
605 CFRelease(newbackup
);
607 CFReleaseSafe(passcode
);
609 CFReleaseNull(keybag
);
611 CFReleaseSafe(oldbackup
);
616 case sec_keychain_restore_syncable_id
:
618 if (EntitlementPresentAndTrue(operation
, client
.task
, kSecEntitlementRestoreKeychain
, &error
)) {
619 CFDictionaryRef backup
= SecXPCDictionaryCopyDictionary(event
, kSecXPCKeyBackup
, &error
);
621 CFDataRef keybag
= SecXPCDictionaryCopyData(event
, kSecXPCKeyKeybag
, &error
);
623 CFDataRef passcode
= NULL
;
624 if (SecXPCDictionaryCopyDataOptional(event
, kSecXPCKeyUserPassword
, &passcode
, &error
)) {
625 bool result
= _SecServerRestoreSyncable(backup
, keybag
, passcode
, &error
);
626 xpc_dictionary_set_bool(replyMessage
, kSecXPCKeyResult
, result
);
627 CFReleaseSafe(passcode
);
629 CFReleaseNull(keybag
);
631 CFReleaseNull(backup
);
636 case sec_item_backup_copy_names_id
:
638 if (EntitlementPresentAndTrue(operation
, client
.task
, kSecEntitlementRestoreKeychain
, &error
)) {
639 CFArrayRef names
= SecServerItemBackupCopyNames(&error
);
640 SecXPCDictionarySetPListOptional(replyMessage
, kSecXPCKeyResult
, names
, &error
);
641 CFReleaseSafe(names
);
645 case sec_item_backup_handoff_fd_id
:
647 if (EntitlementPresentAndTrue(operation
, client
.task
, kSecEntitlementRestoreKeychain
, &error
)) {
648 CFStringRef backupName
= SecXPCDictionaryCopyString(event
, kSecXPCKeyBackup
, &error
);
651 fd
= SecServerItemBackupHandoffFD(backupName
, &error
);
652 CFRelease(backupName
);
654 SecXPCDictionarySetFileDescriptor(replyMessage
, kSecXPCKeyResult
, fd
, &error
);
660 case sec_item_backup_set_confirmed_manifest_id
:
662 if (EntitlementPresentAndTrue(operation
, client
.task
, kSecEntitlementRestoreKeychain
, &error
)) {
663 CFDataRef keybagDigest
= NULL
;
664 if (SecXPCDictionaryCopyDataOptional(event
, kSecXPCKeyKeybag
, &keybagDigest
, &error
)) {
665 CFDataRef manifest
= NULL
;
666 if (SecXPCDictionaryCopyDataOptional(event
, kSecXPCData
, &manifest
, &error
)) {
667 CFStringRef backupName
= SecXPCDictionaryCopyString(event
, kSecXPCKeyBackup
, &error
);
669 bool result
= SecServerItemBackupSetConfirmedManifest(backupName
, keybagDigest
, manifest
, &error
);
670 CFRelease(backupName
);
671 xpc_dictionary_set_bool(replyMessage
, kSecXPCKeyResult
, result
);
673 CFReleaseSafe(manifest
);
676 CFReleaseNull(keybagDigest
);
680 case sec_item_backup_restore_id
:
682 if (EntitlementPresentAndTrue(operation
, client
.task
, kSecEntitlementRestoreKeychain
, &error
)) {
684 CFStringRef backupName
= SecXPCDictionaryCopyString(event
, kSecXPCKeyBackup
, &error
);
686 CFStringRef peerID
= NULL
;
687 if (SecXPCDictionaryCopyStringOptional(event
, kSecXPCKeyDigest
, &peerID
, &error
)) {
688 CFDataRef keybag
= SecXPCDictionaryCopyData(event
, kSecXPCKeyKeybag
, &error
);
690 CFDataRef secret
= SecXPCDictionaryCopyData(event
, kSecXPCKeyUserPassword
, &error
);
692 CFDataRef backup
= SecXPCDictionaryCopyData(event
, kSecXPCData
, &error
);
694 result
= SecServerItemBackupRestore(backupName
, peerID
, keybag
, secret
, backup
, &error
);
701 CFReleaseSafe(peerID
);
703 CFRelease(backupName
);
705 xpc_dictionary_set_bool(replyMessage
, kSecXPCKeyResult
, result
);
709 case sec_add_shared_web_credential_id
:
711 #if SHAREDWEBCREDENTIALS
712 CFDictionaryRef query
= SecXPCDictionaryCopyDictionary(event
, kSecXPCKeyQuery
, &error
);
714 CFTypeRef result
= NULL
;
716 CFStringRef appID
= (client
.task
) ? SecTaskCopyApplicationIdentifier(client
.task
) : NULL
;
717 if (_SecAddSharedWebCredential(query
, &client
, &auditToken
, appID
, domains
, &result
, &error
) && result
) {
718 SecXPCDictionarySetPList(replyMessage
, kSecXPCKeyResult
, result
, &error
);
719 CFReleaseNull(result
);
721 CFReleaseSafe(appID
);
722 CFReleaseNull(query
);
725 SecXPCDictionarySetPList(replyMessage
, kSecXPCKeyResult
, kCFBooleanFalse
, &error
);
729 case sec_copy_shared_web_credential_id
:
731 #if SHAREDWEBCREDENTIALS
732 CFDictionaryRef query
= SecXPCDictionaryCopyDictionary(event
, kSecXPCKeyQuery
, &error
);
734 CFTypeRef result
= NULL
;
735 CFStringRef appID
= (client
.task
) ? SecTaskCopyApplicationIdentifier(client
.task
) : NULL
;
736 if (_SecCopySharedWebCredential(query
, &client
, &auditToken
, appID
, domains
, &result
, &error
) && result
) {
737 SecXPCDictionarySetPList(replyMessage
, kSecXPCKeyResult
, result
, &error
);
738 CFReleaseNull(result
);
740 CFReleaseSafe(appID
);
741 CFReleaseNull(query
);
744 SecXPCDictionarySetPList(replyMessage
, kSecXPCKeyResult
, kCFBooleanFalse
, &error
);
748 case sec_get_log_settings_id
:
750 CFPropertyListRef currentList
= SecCopyLogSettings_Server(&error
);
752 SecXPCDictionarySetPList(replyMessage
, kSecXPCKeyResult
, currentList
, &error
);
754 CFReleaseSafe(currentList
);
757 case sec_set_xpc_log_settings_id
:
759 CFPropertyListRef newSettings
= SecXPCDictionaryCopyPList(event
, kSecXPCKeyQuery
, &error
);
761 SecSetXPCLogSettings_Server(newSettings
, &error
);
763 xpc_dictionary_set_bool(replyMessage
, kSecXPCKeyResult
, true);
764 CFReleaseNull(newSettings
);
767 case sec_set_circle_log_settings_id
:
769 CFPropertyListRef newSettings
= SecXPCDictionaryCopyPList(event
, kSecXPCKeyQuery
, &error
);
771 SecSetCircleLogSettings_Server(newSettings
, &error
);
773 xpc_dictionary_set_bool(replyMessage
, kSecXPCKeyResult
, true);
774 CFReleaseNull(newSettings
);
777 case sec_otr_session_create_remote_id
:
779 CFDataRef publicPeerId
= NULL
;
780 if (SecXPCDictionaryCopyDataOptional(event
, kSecXPCPublicPeerId
, &publicPeerId
, &error
)) {
781 CFDataRef otrSession
= _SecOTRSessionCreateRemote(publicPeerId
, &error
);
783 SecXPCDictionarySetData(replyMessage
, kSecXPCKeyResult
, otrSession
, &error
);
784 CFReleaseNull(otrSession
);
786 CFReleaseSafe(publicPeerId
);
790 case sec_otr_session_process_packet_remote_id
:
792 CFDataRef sessionData
= NULL
, inputPacket
= NULL
, outputSessionData
= NULL
, outputPacket
= NULL
;
793 bool readyForMessages
= false;
794 if (SecXPCDictionaryCopyDataOptional(event
, kSecXPCOTRSession
, &sessionData
, &error
)) {
795 if (SecXPCDictionaryCopyDataOptional(event
, kSecXPCData
, &inputPacket
, &error
)) {
796 bool result
= _SecOTRSessionProcessPacketRemote(sessionData
, inputPacket
, &outputSessionData
, &outputPacket
, &readyForMessages
, &error
);
798 SecXPCDictionarySetData(replyMessage
, kSecXPCOTRSession
, outputSessionData
, &error
);
799 SecXPCDictionarySetData(replyMessage
, kSecXPCData
, outputPacket
, &error
);
800 xpc_dictionary_set_bool(replyMessage
, kSecXPCOTRReady
, readyForMessages
);
801 CFReleaseNull(outputSessionData
);
802 CFReleaseNull(outputPacket
);
804 xpc_dictionary_set_bool(replyMessage
, kSecXPCKeyResult
, result
);
806 CFReleaseSafe(inputPacket
);
808 CFReleaseSafe(sessionData
);
812 case kSecXPCOpTryUserCredentials
:
813 if (EntitlementPresentAndTrue(operation
, client
.task
, kSecEntitlementKeychainCloudCircle
, &error
)) {
814 with_label_and_password_and_dsid(event
, ^(CFStringRef label
, CFDataRef password
, CFStringRef dsid
) {
815 xpc_dictionary_set_bool(replyMessage
, kSecXPCKeyResult
,
816 SOSCCTryUserCredentials_Server(label
, password
, dsid
, &error
));
820 case kSecXPCOpSetUserCredentials
:
821 if (EntitlementPresentAndTrue(operation
, client
.task
, kSecEntitlementKeychainCloudCircle
, &error
)) {
822 with_label_and_password(event
, ^(CFStringRef label
, CFDataRef password
) {
823 xpc_dictionary_set_bool(replyMessage
, kSecXPCKeyResult
,
824 SOSCCSetUserCredentials_Server(label
, password
, &error
));
828 case kSecXPCOpSetUserCredentialsAndDSID
:
829 if (EntitlementPresentAndTrue(operation
, client
.task
, kSecEntitlementKeychainCloudCircle
, &error
)) {
830 with_label_and_password_and_dsid(event
, ^(CFStringRef label
, CFDataRef password
, CFStringRef dsid
) {
831 xpc_dictionary_set_bool(replyMessage
, kSecXPCKeyResult
,
832 SOSCCSetUserCredentialsAndDSID_Server(label
, password
, dsid
, &error
));
836 case kSecXPCOpSetUserCredentialsAndDSIDWithAnalytics
:
837 if (EntitlementPresentAndTrue(operation
, client
.task
, kSecEntitlementKeychainCloudCircle
, &error
)) {
838 with_label_and_password_and_dsid(event
, ^(CFStringRef label
, CFDataRef password
, CFStringRef dsid
) {
839 CFDataRef parentEvent
= NULL
;
840 if(SecXPCDictionaryCopyDataOptional(event
, kSecXPCKeySignInAnalytics
, &parentEvent
, &error
) && parentEvent
!= NULL
){
841 xpc_dictionary_set_bool(replyMessage
, kSecXPCKeyResult
, SOSCCSetUserCredentialsAndDSIDWithAnalytics_Server(label
, password
, dsid
, parentEvent
, &error
));
843 xpc_dictionary_set_bool(replyMessage
, kSecXPCKeyResult
, SOSCCSetUserCredentialsAndDSID_Server(label
, password
, dsid
, &error
));
845 CFReleaseNull(parentEvent
);
850 if (EntitlementPresentAndTrue(operation
, client
.task
, kSecEntitlementKeychainCloudCircle
, &error
)) {
851 with_view_and_action(event
, ^(CFStringRef view
, uint64_t actionCode
) {
852 xpc_dictionary_set_int64(replyMessage
, kSecXPCKeyResult
,
853 SOSCCView_Server(view
, (SOSViewActionCode
)actionCode
, &error
));
857 case kSecXPCOpViewSet
: // FALLTHROUGH
858 case kSecXPCOpViewSetWithAnalytics
:
859 if (EntitlementPresentAndTrue(operation
, client
.task
, kSecEntitlementKeychainCloudCircle
, &error
)) {
860 CFSetRef enabledViews
= SecXPCSetCreateFromXPCDictionaryElement(event
, kSecXPCKeyEnabledViewsKey
);
861 CFSetRef disabledViews
= SecXPCSetCreateFromXPCDictionaryElement(event
, kSecXPCKeyDisabledViewsKey
);
862 CFDataRef parentEvent
= NULL
;
863 if(SecXPCDictionaryCopyDataOptional(event
, kSecXPCKeySignInAnalytics
, &parentEvent
, &error
)){
864 xpc_dictionary_set_bool(replyMessage
, kSecXPCKeyResult
, SOSCCViewSetWithAnalytics_Server(enabledViews
, disabledViews
, parentEvent
));
866 CFReleaseNull(enabledViews
);
867 CFReleaseNull(disabledViews
);
868 CFReleaseNull(parentEvent
);
871 case kSecXPCOpCanAuthenticate
:
872 if (EntitlementPresentAndTrue(operation
, client
.task
, kSecEntitlementKeychainCloudCircle
, &error
)) {
873 xpc_dictionary_set_bool(replyMessage
, kSecXPCKeyResult
,
874 SOSCCCanAuthenticate_Server(&error
));
877 case kSecXPCOpPurgeUserCredentials
:
878 if (EntitlementPresentAndTrue(operation
, client
.task
, kSecEntitlementKeychainCloudCircle
, &error
)) {
879 xpc_dictionary_set_bool(replyMessage
, kSecXPCKeyResult
,
880 SOSCCPurgeUserCredentials_Server(&error
));
883 case kSecXPCOpDeviceInCircle
:
884 if (EntitlementPresentAndTrue(operation
, client
.task
, kSecEntitlementKeychainCloudCircle
, &error
)) {
885 xpc_dictionary_set_int64(replyMessage
, kSecXPCKeyResult
,
886 SOSCCThisDeviceIsInCircle_Server(&error
));
889 case kSecXPCOpRequestToJoin
:
890 if (EntitlementPresentAndTrue(operation
, client
.task
, kSecEntitlementKeychainCloudCircle
, &error
)) {
891 xpc_dictionary_set_bool(replyMessage
, kSecXPCKeyResult
,
892 SOSCCRequestToJoinCircle_Server(&error
));
895 case kSecXPCOpRequestToJoinWithAnalytics
:
896 if (EntitlementPresentAndTrue(operation
, client
.task
, kSecEntitlementKeychainCloudCircle
, &error
)) {
897 CFDataRef parentEvent
= NULL
;
898 if(SecXPCDictionaryCopyDataOptional(event
, kSecXPCKeySignInAnalytics
, &parentEvent
, &error
) && parentEvent
!= NULL
){
899 xpc_dictionary_set_bool(replyMessage
, kSecXPCKeyResult
, SOSCCRequestToJoinCircleWithAnalytics_Server(parentEvent
, &error
));
901 xpc_dictionary_set_bool(replyMessage
, kSecXPCKeyResult
, SOSCCRequestToJoinCircle_Server(&error
));
903 CFReleaseNull(parentEvent
);
906 case kSecXPCOpAccountHasPublicKey
:
907 if (EntitlementPresentAndTrue(operation
, client
.task
, kSecEntitlementKeychainCloudCircle
, &error
)) {
908 xpc_dictionary_set_bool(replyMessage
, kSecXPCKeyResult
,
909 SOSCCAccountHasPublicKey_Server(&error
));
913 case kSecXPCOpRequestToJoinAfterRestore
:
914 if (EntitlementPresentAndTrue(operation
, client
.task
, kSecEntitlementKeychainCloudCircle
, &error
)) {
915 xpc_dictionary_set_bool(replyMessage
, kSecXPCKeyResult
,
916 SOSCCRequestToJoinCircleAfterRestore_Server(&error
));
919 case kSecXPCOpRequestToJoinAfterRestoreWithAnalytics
:
920 if (EntitlementPresentAndTrue(operation
, client
.task
, kSecEntitlementKeychainCloudCircle
, &error
)) {
921 CFDataRef parentEvent
= NULL
;
922 if(SecXPCDictionaryCopyDataOptional(event
, kSecXPCKeySignInAnalytics
, &parentEvent
, &error
) && parentEvent
!= NULL
){
923 xpc_dictionary_set_bool(replyMessage
, kSecXPCKeyResult
, SOSCCRequestToJoinCircleAfterRestoreWithAnalytics_Server(parentEvent
, &error
));
925 xpc_dictionary_set_bool(replyMessage
, kSecXPCKeyResult
, SOSCCRequestToJoinCircleAfterRestore_Server(&error
));
927 CFReleaseNull(parentEvent
);
930 case kSecXPCOpRequestDeviceID
:
931 case kSecXPCOpSetDeviceID
:
932 case kSecXPCOpHandleIDSMessage
:
933 case kSecXPCOpSyncWithIDSPeer
:
934 case kSecXPCOpSendIDSMessage
:
935 case kSecXPCOpPingTest
:
936 case kSecXPCOpIDSDeviceID
:
937 case kSecXPCOpSyncWithKVSPeerIDOnly
:{
938 xpc_dictionary_set_int64(replyMessage
, kSecXPCKeyError
, errSecUnimplemented
);
941 case kSecXPCOpAccountSetToNew
:
942 if (EntitlementPresentAndTrue(operation
, client
.task
, kSecEntitlementKeychainCloudCircle
, &error
)) {
943 xpc_dictionary_set_bool(replyMessage
, kSecXPCKeyResult
, SOSCCAccountSetToNew_Server(&error
));
946 case kSecXPCOpResetToOffering
:
947 if (EntitlementPresentAndTrue(operation
, client
.task
, kSecEntitlementKeychainCloudCircle
, &error
)) {
948 xpc_dictionary_set_bool(replyMessage
, kSecXPCKeyResult
,
949 SOSCCResetToOffering_Server(&error
));
952 case kSecXPCOpResetToEmpty
:
953 if (EntitlementPresentAndTrue(operation
, client
.task
, kSecEntitlementKeychainCloudCircle
, &error
)) {
954 xpc_dictionary_set_bool(replyMessage
, kSecXPCKeyResult
,
955 SOSCCResetToEmpty_Server(&error
));
958 case kSecXPCOpResetToEmptyWithAnalytics
:
959 if (EntitlementPresentAndTrue(operation
, client
.task
, kSecEntitlementKeychainCloudCircle
, &error
)) {
960 CFDataRef parentEvent
= NULL
;
961 if(SecXPCDictionaryCopyDataOptional(event
, kSecXPCKeySignInAnalytics
, &parentEvent
, &error
) && parentEvent
!= NULL
){
962 xpc_dictionary_set_bool(replyMessage
, kSecXPCKeyResult
, SOSCCResetToEmptyWithAnalytics_Server(parentEvent
, &error
));
964 xpc_dictionary_set_bool(replyMessage
, kSecXPCKeyResult
, SOSCCResetToEmpty_Server(&error
));
968 case kSecXPCOpRemoveThisDeviceFromCircle
:
969 if (EntitlementPresentAndTrue(operation
, client
.task
, kSecEntitlementKeychainCloudCircle
, &error
)) {
970 xpc_dictionary_set_bool(replyMessage
, kSecXPCKeyResult
,
971 SOSCCRemoveThisDeviceFromCircle_Server(&error
));
974 case kSecXPCOpRemoveThisDeviceFromCircleWithAnalytics
:
975 if (EntitlementPresentAndTrue(operation
, client
.task
, kSecEntitlementKeychainCloudCircle
, &error
)) {
976 CFDataRef parentEvent
= NULL
;
977 if(SecXPCDictionaryCopyDataOptional(event
, kSecXPCKeySignInAnalytics
, &parentEvent
, &error
) && parentEvent
!= NULL
){
978 xpc_dictionary_set_bool(replyMessage
, kSecXPCKeyResult
, SOSCCRemoveThisDeviceFromCircleWithAnalytics_Server(parentEvent
, &error
));
980 xpc_dictionary_set_bool(replyMessage
, kSecXPCKeyResult
, SOSCCRemoveThisDeviceFromCircle_Server(&error
));
982 CFReleaseNull(parentEvent
);
985 case kSecXPCOpRemovePeersFromCircle
:
986 if (EntitlementPresentAndTrue(operation
, client
.task
, kSecEntitlementKeychainCloudCircle
, &error
)) {
987 CFArrayRef applicants
= SecXPCDictionaryCopyPeerInfoArray(event
, kSecXPCKeyPeerInfoArray
, &error
);
988 xpc_dictionary_set_bool(replyMessage
, kSecXPCKeyResult
,
989 SOSCCRemovePeersFromCircle_Server(applicants
, &error
));
990 CFReleaseNull(applicants
);
993 case kSecXPCOpRemovePeersFromCircleWithAnalytics
:
994 if (EntitlementPresentAndTrue(operation
, client
.task
, kSecEntitlementKeychainCloudCircle
, &error
)) {
995 CFArrayRef applicants
= SecXPCDictionaryCopyPeerInfoArray(event
, kSecXPCKeyPeerInfoArray
, &error
);
996 CFDataRef parentEvent
= NULL
;
997 if(SecXPCDictionaryCopyDataOptional(event
, kSecXPCKeySignInAnalytics
, &parentEvent
, &error
) && parentEvent
!= NULL
){
998 xpc_dictionary_set_bool(replyMessage
, kSecXPCKeyResult
, SOSCCRemovePeersFromCircleWithAnalytics_Server(applicants
, parentEvent
, &error
));
1000 xpc_dictionary_set_bool(replyMessage
, kSecXPCKeyResult
, SOSCCRemovePeersFromCircle_Server(applicants
, &error
));
1002 CFReleaseNull(parentEvent
);
1003 CFReleaseNull(applicants
);
1006 case kSecXPCOpLoggedOutOfAccount
:
1007 if (EntitlementPresentAndTrue(operation
, client
.task
, kSecEntitlementKeychainCloudCircle
, &error
)) {
1008 xpc_dictionary_set_bool(replyMessage
, kSecXPCKeyResult
,
1009 SOSCCLoggedOutOfAccount_Server(&error
));
1012 case kSecXPCOpAcceptApplicants
:
1013 if (EntitlementPresentAndTrue(operation
, client
.task
, kSecEntitlementKeychainCloudCircle
, &error
)) {
1014 xpc_object_t xapplicants
= xpc_dictionary_get_value(event
, kSecXPCKeyPeerInfoArray
);
1015 // CreateArrayOfPeerInfoWithXPCObject enforces that xapplicants is a non-NULL xpc data object
1016 CFArrayRef applicants
= CreateArrayOfPeerInfoWithXPCObject(xapplicants
, &error
); //(CFArrayRef)(_CFXPCCreateCFObjectFromXPCObject(xapplicants));
1017 xpc_dictionary_set_bool(replyMessage
, kSecXPCKeyResult
,
1018 (applicants
&& SOSCCAcceptApplicants_Server(applicants
, &error
)));
1019 CFReleaseSafe(applicants
);
1022 case kSecXPCOpRejectApplicants
:
1023 if (EntitlementPresentAndTrue(operation
, client
.task
, kSecEntitlementKeychainCloudCircle
, &error
)) {
1024 xpc_object_t xapplicants
= xpc_dictionary_get_value(event
, kSecXPCKeyPeerInfoArray
);
1025 // CreateArrayOfPeerInfoWithXPCObject enforces that xapplicants is a non-NULL xpc data object
1026 CFArrayRef applicants
= CreateArrayOfPeerInfoWithXPCObject(xapplicants
, &error
); //(CFArrayRef)(_CFXPCCreateCFObjectFromXPCObject(xapplicants));
1027 xpc_dictionary_set_bool(replyMessage
, kSecXPCKeyResult
,
1028 (applicants
&& SOSCCRejectApplicants_Server(applicants
, &error
)));
1029 CFReleaseSafe(applicants
);
1032 case kSecXPCOpSetNewPublicBackupKey
:
1034 if (EntitlementPresentAndTrue(operation
, client
.task
, kSecEntitlementRestoreKeychain
, &error
)) {
1035 CFDataRef publicBackupKey
= SecXPCDictionaryCopyData(event
, kSecXPCKeyNewPublicBackupKey
, &error
);
1036 if (publicBackupKey
!= NULL
) {
1037 SOSPeerInfoRef peerInfo
= SOSCCSetNewPublicBackupKey_Server(publicBackupKey
, &error
);
1038 CFDataRef peerInfoData
= peerInfo
? SOSPeerInfoCopyEncodedData(peerInfo
, kCFAllocatorDefault
, &error
) : NULL
;
1039 CFReleaseNull(peerInfo
);
1041 xpc_object_t xpc_object
= _CFXPCCreateXPCObjectFromCFObject(peerInfoData
);
1042 xpc_dictionary_set_value(replyMessage
, kSecXPCKeyResult
, xpc_object
);
1043 xpc_release(xpc_object
);
1045 CFReleaseNull(peerInfoData
);
1046 CFReleaseSafe(publicBackupKey
);
1051 case kSecXPCOpRegisterRecoveryPublicKey
:
1053 if (EntitlementPresentAndTrue(operation
, client
.task
, kSecEntitlementRestoreKeychain
, &error
)) {
1054 CFDataRef recovery_key
= SecXPCDictionaryCopyData(event
, kSecXPCKeyRecoveryPublicKey
, &error
);
1055 if (recovery_key
!= NULL
) {
1057 CFDataRef nullData
= CFDataCreate(kCFAllocatorDefault
, &zero
, 1); // token we send if we really wanted to send NULL
1058 if(CFEqual(recovery_key
, nullData
)) {
1059 CFReleaseNull(recovery_key
);
1061 CFReleaseNull(nullData
);
1062 xpc_dictionary_set_bool(replyMessage
, kSecXPCKeyResult
, SOSCCRegisterRecoveryPublicKey_Server(recovery_key
, &error
));
1063 CFReleaseNull(recovery_key
);
1068 case kSecXPCOpGetRecoveryPublicKey
:
1070 if (EntitlementPresentAndTrue(operation
, client
.task
, kSecEntitlementRestoreKeychain
, &error
)) {
1071 xpc_object_t xpc_recovery_object
= NULL
;
1072 CFDataRef recovery
= SOSCCCopyRecoveryPublicKey(&error
);
1074 xpc_recovery_object
= _CFXPCCreateXPCObjectFromCFObject(recovery
);
1076 xpc_dictionary_set_value(replyMessage
, kSecXPCKeyResult
, xpc_recovery_object
);
1077 CFReleaseNull(recovery
);
1081 case kSecXPCOpSetBagForAllSlices
:
1083 if (EntitlementPresentAndTrue(operation
, client
.task
, kSecEntitlementRestoreKeychain
, &error
)) {
1084 CFDataRef backupSlice
= SecXPCDictionaryCopyData(event
, kSecXPCKeyKeybag
, &error
); // NULL checked below
1085 bool includeV0
= xpc_dictionary_get_bool(event
, kSecXPCKeyIncludeV0
); // false is ok, so it's safe for this paramter to be unset or incorrect type
1086 xpc_dictionary_set_bool(replyMessage
, kSecXPCKeyResult
, backupSlice
&& SOSCCRegisterSingleRecoverySecret_Server(backupSlice
, includeV0
, &error
));
1087 CFReleaseSafe(backupSlice
);
1091 case kSecXPCOpCopyApplicantPeerInfo
:
1092 if (EntitlementPresentAndTrue(operation
, client
.task
, kSecEntitlementKeychainCloudCircle
, &error
)) {
1093 xpc_dictionary_set_and_consume_PeerInfoArray(replyMessage
, kSecXPCKeyResult
,
1094 SOSCCCopyApplicantPeerInfo_Server(&error
),
1098 case kSecXPCOpCopyValidPeerPeerInfo
:
1099 if (EntitlementPresentAndTrue(operation
, client
.task
, kSecEntitlementKeychainCloudCircle
, &error
)) {
1100 xpc_dictionary_set_and_consume_PeerInfoArray(replyMessage
, kSecXPCKeyResult
,
1101 SOSCCCopyValidPeerPeerInfo_Server(&error
),
1105 case kSecXPCOpValidateUserPublic
:
1106 if (EntitlementPresentAndTrue(operation
, client
.task
, kSecEntitlementKeychainCloudCircle
, &error
)) {
1107 bool trusted
= SOSCCValidateUserPublic_Server(&error
);
1108 xpc_dictionary_set_bool(replyMessage
, kSecXPCKeyResult
, trusted
);
1111 case kSecXPCOpCopyNotValidPeerPeerInfo
:
1112 if (EntitlementPresentAndTrue(operation
, client
.task
, kSecEntitlementKeychainCloudCircle
, &error
)) {
1113 xpc_dictionary_set_and_consume_PeerInfoArray(replyMessage
, kSecXPCKeyResult
,
1114 SOSCCCopyNotValidPeerPeerInfo_Server(&error
),
1118 case kSecXPCOpCopyGenerationPeerInfo
:
1119 if (EntitlementPresentAndTrue(operation
, client
.task
, kSecEntitlementKeychainCloudCircle
, &error
)) {
1120 xpc_dictionary_set_and_consume_CFArray(replyMessage
, kSecXPCKeyResult
,
1121 SOSCCCopyGenerationPeerInfo_Server(&error
));
1124 case kSecXPCOpCopyRetirementPeerInfo
:
1125 if (EntitlementPresentAndTrue(operation
, client
.task
, kSecEntitlementKeychainCloudCircle
, &error
)) {
1126 xpc_dictionary_set_and_consume_PeerInfoArray(replyMessage
, kSecXPCKeyResult
,
1127 SOSCCCopyRetirementPeerInfo_Server(&error
),
1131 case kSecXPCOpCopyViewUnawarePeerInfo
:
1132 if (EntitlementPresentAndTrue(operation
, client
.task
, kSecEntitlementKeychainCloudCircle
, &error
)) {
1133 xpc_dictionary_set_and_consume_PeerInfoArray(replyMessage
, kSecXPCKeyResult
,
1134 SOSCCCopyViewUnawarePeerInfo_Server(&error
),
1138 case kSecXPCOpCopyEngineState
:
1140 if (EntitlementPresentAndTrue(operation
, client
.task
, kSecEntitlementKeychainCloudCircle
, &error
)) {
1141 CFArrayRef array
= SOSCCCopyEngineState_Server(&error
);
1142 CFDataRef derData
= NULL
;
1144 require_quiet(array
, done
);
1145 derData
= CFPropertyListCreateDERData(kCFAllocatorDefault
, array
, &error
);
1147 require_quiet(derData
, done
);
1148 xpc_dictionary_set_data(replyMessage
, kSecXPCKeyResult
, CFDataGetBytePtr(derData
),CFDataGetLength(derData
));
1150 CFReleaseNull(derData
);
1151 CFReleaseNull(array
);
1155 case kSecXPCOpCopyPeerPeerInfo
:
1156 if (EntitlementPresentAndTrue(operation
, client
.task
, kSecEntitlementKeychainCloudCircle
, &error
)) {
1157 xpc_dictionary_set_and_consume_PeerInfoArray(replyMessage
, kSecXPCKeyResult
,
1158 SOSCCCopyPeerPeerInfo_Server(&error
),
1162 case kSecXPCOpCopyConcurringPeerPeerInfo
:
1163 if (EntitlementPresentAndTrue(operation
, client
.task
, kSecEntitlementKeychainCloudCircle
, &error
)) {
1164 xpc_dictionary_set_and_consume_PeerInfoArray(replyMessage
, kSecXPCKeyResult
,
1165 SOSCCCopyConcurringPeerPeerInfo_Server(&error
),
1169 case kSecXPCOpCopyMyPeerInfo
:
1170 if (EntitlementPresentAndTrue(operation
, client
.task
, kSecEntitlementKeychainCloudCircle
, &error
)) {
1171 SOSPeerInfoRef peerInfo
= SOSCCCopyMyPeerInfo_Server(&error
);
1172 CFDataRef peerInfoData
= peerInfo
? SOSPeerInfoCopyEncodedData(peerInfo
, kCFAllocatorDefault
, &error
) : NULL
;
1173 CFReleaseNull(peerInfo
);
1175 xpc_object_t xpc_object
= _CFXPCCreateXPCObjectFromCFObject(peerInfoData
);
1176 xpc_dictionary_set_value(replyMessage
, kSecXPCKeyResult
, xpc_object
);
1177 xpc_release(xpc_object
);
1179 CFReleaseNull(peerInfoData
);
1182 case kSecXPCOpGetLastDepartureReason
:
1183 if (EntitlementPresentAndTrue(operation
, client
.task
, kSecEntitlementKeychainCloudCircle
, &error
)) {
1184 xpc_dictionary_set_int64(replyMessage
, kSecXPCKeyResult
,
1185 SOSCCGetLastDepartureReason_Server(&error
));
1188 case kSecXPCOpSetLastDepartureReason
:
1189 if (EntitlementPresentAndTrue(operation
, client
.task
, kSecEntitlementKeychainCloudCircle
, &error
)) {
1190 // 0 is a legitimate reason (kSOSDepartureReasonError), so it's safe for this parameter to be unset or incorrect type
1191 int32_t reason
= (int32_t) xpc_dictionary_get_int64(event
, kSecXPCKeyReason
);
1192 xpc_dictionary_set_int64(replyMessage
, kSecXPCKeyResult
,
1193 SOSCCSetLastDepartureReason_Server(reason
, &error
));
1196 case kSecXPCOpProcessSyncWithPeers
:
1197 if (EntitlementPresentAndTrue(operation
, client
.task
, kSecEntitlementKeychainSyncUpdates
, &error
)) {
1198 CFSetRef peers
= SecXPCDictionaryCopySet(event
, kSecXPCKeySet
, &error
);
1199 CFSetRef backupPeers
= SecXPCDictionaryCopySet(event
, kSecXPCKeySet2
, &error
);
1200 if (peers
&& backupPeers
) {
1201 CFSetRef result
= SOSCCProcessSyncWithPeers_Server(peers
, backupPeers
, &error
);
1203 SecXPCDictionarySetPList(replyMessage
, kSecXPCKeyResult
, result
, &error
);
1205 CFReleaseNull(result
);
1207 CFReleaseNull(peers
);
1208 CFReleaseNull(backupPeers
);
1211 case kSecXPCOpProcessSyncWithAllPeers
:
1212 if (EntitlementPresentAndTrue(operation
, client
.task
, kSecEntitlementKeychainSyncUpdates
, &error
)) {
1213 xpc_dictionary_set_int64(replyMessage
, kSecXPCKeyResult
,
1214 SOSCCProcessSyncWithAllPeers_Server(&error
));
1217 case soscc_EnsurePeerRegistration_id
:
1218 if (EntitlementPresentAndTrue(operation
, client
.task
, kSecEntitlementKeychainSyncUpdates
, &error
)) {
1219 xpc_dictionary_set_bool(replyMessage
, kSecXPCKeyResult
,
1220 SOSCCProcessEnsurePeerRegistration_Server(&error
));
1223 case kSecXPCOpRollKeys
:
1225 // false is valid, so it's safe for this parameter to be unset or incorrect type
1226 bool force
= xpc_dictionary_get_bool(event
, "force");
1227 xpc_dictionary_set_bool(replyMessage
, kSecXPCKeyResult
,
1228 _SecServerRollKeys(force
, &client
, &error
));
1231 case kSecXPCOpWaitForInitialSync
:
1232 if (EntitlementPresentAndTrue(operation
, client
.task
, kSecEntitlementKeychainCloudCircle
, &error
)) {
1233 xpc_dictionary_set_bool(replyMessage
, kSecXPCKeyResult
,
1234 SOSCCWaitForInitialSync_Server(&error
));
1238 case kSecXPCOpWaitForInitialSyncWithAnalytics
:
1239 if (EntitlementPresentAndTrue(operation
, client
.task
, kSecEntitlementKeychainCloudCircle
, &error
)) {
1240 CFDataRef parentEvent
= NULL
;
1241 if(SecXPCDictionaryCopyDataOptional(event
, kSecXPCKeySignInAnalytics
, &parentEvent
, &error
) && parentEvent
!= NULL
){
1242 xpc_dictionary_set_bool(replyMessage
, kSecXPCKeyResult
, SOSCCWaitForInitialSyncWithAnalytics_Server(parentEvent
, &error
));
1244 xpc_dictionary_set_bool(replyMessage
, kSecXPCKeyResult
, SOSCCWaitForInitialSync_Server(&error
));
1246 CFReleaseNull(parentEvent
);
1249 case kSecXPCOpPeersHaveViewsEnabled
:
1251 CFArrayRef viewSet
= SecXPCDictionaryCopyArray(event
, kSecXPCKeyArray
, &error
);
1253 CFBooleanRef result
= SOSCCPeersHaveViewsEnabled_Server(viewSet
, &error
);
1254 if (result
!= NULL
) {
1255 xpc_dictionary_set_bool(replyMessage
, kSecXPCKeyResult
, result
!= kCFBooleanFalse
);
1258 CFReleaseNull(viewSet
);
1262 case kSecXPCOpWhoAmI
:
1265 xpc_dictionary_set_data(replyMessage
, "musr", CFDataGetBytePtr(client
.musr
), CFDataGetLength(client
.musr
));
1266 xpc_dictionary_set_bool(replyMessage
, "system-keychain", client
.allowSystemKeychain
);
1267 xpc_dictionary_set_bool(replyMessage
, "syncbubble-keychain", client
.allowSyncBubbleKeychain
);
1268 xpc_dictionary_set_bool(replyMessage
, "network-extension", client
.isNetworkExtension
);
1269 xpc_dictionary_set_bool(replyMessage
, kSecXPCKeyResult
, true);
1272 case kSecXPCOpTransmogrifyToSyncBubble
:
1274 if (EntitlementPresentAndTrue(operation
, client
.task
, kSecEntitlementPrivateKeychainSyncBubble
, &error
)) {
1276 uid_t uid
= (uid_t
)xpc_dictionary_get_int64(event
, "uid");
1277 CFArrayRef services
= SecXPCDictionaryCopyArray(event
, "services", &error
);
1279 if (uid
&& services
) {
1280 res
= _SecServerTransmogrifyToSyncBubble(services
, uid
, &client
, &error
);
1282 xpc_dictionary_set_bool(replyMessage
, kSecXPCKeyResult
, res
);
1283 CFReleaseNull(services
);
1285 xpc_dictionary_set_bool(replyMessage
, kSecXPCKeyResult
, false);
1290 case kSecXPCOpTransmogrifyToSystemKeychain
:
1292 if (EntitlementPresentAndTrue(operation
, client
.task
, kSecEntitlementPrivateKeychainMigrateSystemKeychain
, &error
)) {
1294 bool res
= _SecServerTransmogrifyToSystemKeychain(&client
, &error
);
1295 xpc_dictionary_set_bool(replyMessage
, kSecXPCKeyResult
, res
);
1297 xpc_dictionary_set_bool(replyMessage
, kSecXPCKeyResult
, false);
1303 case kSecXPCOpDeleteUserView
:
1305 if (EntitlementPresentAndTrue(operation
, client
.task
, kSecEntitlementPrivateKeychainMigrateSystemKeychain
, &error
)) {
1308 uid_t uid
= (uid_t
)xpc_dictionary_get_int64(event
, "uid");
1310 res
= _SecServerDeleteMUSERViews(&client
, uid
, &error
);
1313 xpc_dictionary_set_bool(replyMessage
, kSecXPCKeyResult
, res
);
1318 case kSecXPCOpCopyApplication
:
1319 if (EntitlementPresentAndTrue(operation
, client
.task
, kSecEntitlementCircleJoin
, &error
)) {
1320 SOSPeerInfoRef peerInfo
= SOSCCCopyApplication_Server(&error
);
1321 CFDataRef peerInfoData
= peerInfo
? SOSPeerInfoCopyEncodedData(peerInfo
, kCFAllocatorDefault
, &error
) : NULL
;
1322 CFReleaseNull(peerInfo
);
1324 xpc_object_t xpc_object
= _CFXPCCreateXPCObjectFromCFObject(peerInfoData
);
1325 xpc_dictionary_set_value(replyMessage
, kSecXPCKeyResult
, xpc_object
);
1326 xpc_release(xpc_object
);
1328 CFReleaseNull(peerInfoData
);
1331 case kSecXPCOpCopyCircleJoiningBlob
:
1332 if (EntitlementPresentAndTrue(operation
, client
.task
, kSecEntitlementCircleJoin
, &error
)) {
1333 CFDataRef appBlob
= SecXPCDictionaryCopyCFDataRef(event
, kSecXPCData
, &error
);
1334 if (appBlob
!= NULL
) {
1335 SOSPeerInfoRef applicant
= SOSPeerInfoCreateFromData(kCFAllocatorDefault
, &error
, appBlob
);
1336 if (applicant
!= NULL
) {
1337 CFDataRef pbblob
= SOSCCCopyCircleJoiningBlob_Server(applicant
, &error
);
1339 xpc_object_t xpc_object
= _CFXPCCreateXPCObjectFromCFObject(pbblob
);
1340 xpc_dictionary_set_value(replyMessage
, kSecXPCKeyResult
, xpc_object
);
1341 xpc_release(xpc_object
);
1343 CFReleaseNull(pbblob
);
1344 CFReleaseNull(applicant
);
1346 CFReleaseNull(appBlob
);
1350 case kSecXPCOpCopyInitialSyncBlob
:
1351 if (EntitlementPresentAndTrue(operation
, client
.task
, kSecEntitlementCircleJoin
, &error
)) {
1352 uint64_t flags
= xpc_dictionary_get_uint64(event
, kSecXPCKeyFlags
); // 0 is a valid flags, so no error checking
1353 CFDataRef initialblob
= SOSCCCopyInitialSyncData_Server((uint32_t)flags
, &error
);
1355 xpc_object_t xpc_object
= _CFXPCCreateXPCObjectFromCFObject(initialblob
);
1356 xpc_dictionary_set_value(replyMessage
, kSecXPCKeyResult
, xpc_object
);
1357 xpc_release(xpc_object
);
1359 CFReleaseNull(initialblob
);
1362 case kSecXPCOpJoinWithCircleJoiningBlob
:
1363 if (EntitlementPresentAndTrue(operation
, client
.task
, kSecEntitlementCircleJoin
, &error
)) {
1364 CFDataRef joiningBlob
= SecXPCDictionaryCopyCFDataRef(event
, kSecXPCData
, &error
); // NULL checked below
1365 uint64_t version
= xpc_dictionary_get_uint64(event
, kSecXPCVersion
); // 0 is valid, so this parameter can be unset or incorrect type
1366 if (joiningBlob
!= NULL
) {
1367 bool retval
= SOSCCJoinWithCircleJoiningBlob_Server(joiningBlob
, (PiggyBackProtocolVersion
) version
, &error
);
1368 xpc_dictionary_set_bool(replyMessage
, kSecXPCKeyResult
, retval
);
1369 CFReleaseNull(joiningBlob
);
1374 case kSecXPCOpKVSKeyCleanup
:
1375 if (EntitlementPresentAndTrue(operation
, client
.task
, kSecEntitlementKeychainCloudCircle
, &error
)) {
1376 bool retval
= SOSCCCleanupKVSKeys_Server(&error
);
1377 xpc_dictionary_set_bool(replyMessage
, kSecXPCKeyResult
, retval
);
1381 case kSecXPCOpMessageFromPeerIsPending
:
1383 SOSPeerInfoRef peer
= SecXPCDictionaryCopyPeerInfo(event
, kSecXPCKeyPeerInfo
, &error
);
1385 xpc_dictionary_set_bool(replyMessage
, kSecXPCKeyResult
,
1386 SOSCCMessageFromPeerIsPending_Server(peer
, &error
));
1388 CFReleaseNull(peer
);
1391 case kSecXPCOpSendToPeerIsPending
:
1393 SOSPeerInfoRef peer
= SecXPCDictionaryCopyPeerInfo(event
, kSecXPCKeyPeerInfo
, &error
);
1395 xpc_dictionary_set_bool(replyMessage
, kSecXPCKeyResult
,
1396 SOSCCSendToPeerIsPending(peer
, &error
));
1398 CFReleaseNull(peer
);
1401 #endif /* !SECUREOBJECTSYNC */
1402 case sec_delete_items_with_access_groups_id
:
1404 bool retval
= false;
1405 #if TARGET_OS_IPHONE
1406 if (EntitlementPresentAndTrue(operation
, client
.task
, kSecEntitlementPrivateUninstallDeletion
, &error
)) {
1407 CFArrayRef accessGroups
= SecXPCDictionaryCopyArray(event
, kSecXPCKeyAccessGroups
, &error
);
1410 retval
= _SecItemServerDeleteAllWithAccessGroups(accessGroups
, &client
, &error
);
1412 CFReleaseNull(accessGroups
);
1415 xpc_dictionary_set_bool(replyMessage
, kSecXPCKeyResult
, retval
);
1418 case kSecXPCOpBackupKeybagAdd
: {
1419 if (EntitlementPresentAndTrue(operation
, client
.task
, kSecEntitlementBackupTableOperations
, &error
)) {
1420 xpc_dictionary_set_bool(replyMessage
, kSecXPCKeyResult
, false);
1424 case kSecXPCOpBackupKeybagDelete
: {
1425 if (EntitlementPresentAndTrue(operation
, client
.task
, kSecEntitlementBackupTableOperations
, &error
)) {
1426 xpc_dictionary_set_bool(replyMessage
, kSecXPCKeyResult
, false);
1430 case kSecXPCOpKeychainControlEndpoint
: {
1431 if(EntitlementPresentAndTrue(operation
, client
.task
, kSecEntitlementKeychainControl
, &error
)) {
1432 xpc_endpoint_t endpoint
= SecServerCreateKeychainControlEndpoint();
1434 xpc_dictionary_set_value(replyMessage
, kSecXPCKeyEndpoint
, endpoint
);
1435 xpc_dictionary_set_bool(replyMessage
, kSecXPCKeyResult
, true);
1436 xpc_release(endpoint
);
1438 xpc_dictionary_set_bool(replyMessage
, kSecXPCKeyResult
, false);
1443 case sec_item_copy_parent_certificates_id
: {
1444 CFArrayRef results
= NULL
;
1445 if(EntitlementPresentAndTrue(operation
, client
.task
, kSecEntitlementPrivateCertificateAllAccess
, &error
)) {
1446 CFDataRef issuer
= SecXPCDictionaryCopyData(event
, kSecXPCKeyNormalizedIssuer
, &error
);
1447 CFArrayRef accessGroups
= SecXPCDictionaryCopyArray(event
, kSecXPCKeyAccessGroups
, &error
);
1448 if (issuer
&& accessGroups
) {
1449 results
= _SecItemCopyParentCertificates(issuer
, accessGroups
, &error
);
1451 CFReleaseNull(issuer
);
1452 CFReleaseNull(accessGroups
);
1454 SecXPCDictionarySetPListOptional(replyMessage
, kSecXPCKeyResult
, results
, &error
);
1455 CFReleaseNull(results
);
1458 case sec_item_certificate_exists_id
: {
1459 bool result
= false;
1460 if(EntitlementPresentAndTrue(operation
, client
.task
, kSecEntitlementPrivateCertificateAllAccess
, &error
)) {
1461 CFDataRef issuer
= SecXPCDictionaryCopyData(event
, kSecXPCKeyNormalizedIssuer
, &error
);
1462 CFDataRef serialNum
= SecXPCDictionaryCopyData(event
, kSecXPCKeySerialNumber
, &error
);
1463 CFArrayRef accessGroups
= SecXPCDictionaryCopyArray(event
, kSecXPCKeyAccessGroups
, &error
);
1464 if (issuer
&& serialNum
&& accessGroups
) {
1465 result
= _SecItemCertificateExists(issuer
, serialNum
, accessGroups
, &error
);
1467 CFReleaseNull(issuer
);
1468 CFReleaseNull(serialNum
);
1469 CFReleaseNull(accessGroups
);
1471 xpc_dictionary_set_bool(replyMessage
, kSecXPCKeyResult
, result
);
1480 if(SecErrorGetOSStatus(error
) == errSecItemNotFound
|| isSOSErrorCoded(error
, kSOSErrorPublicKeyAbsent
))
1481 secdebug("ipc", "%@ %@ %@", client
.task
, SOSCCGetOperationDescription((enum SecXPCOperation
)operation
), error
);
1482 else if (SecErrorGetOSStatus(error
) == errSecAuthNeeded
)
1483 secwarning("Authentication is needed %@ %@ %@", client
.task
, SOSCCGetOperationDescription((enum SecXPCOperation
)operation
), error
);
1485 secerror("%@ %@ %@", client
.task
, SOSCCGetOperationDescription((enum SecXPCOperation
)operation
), error
);
1487 xpcError
= SecCreateXPCObjectWithCFError(error
);
1489 xpc_dictionary_set_value(replyMessage
, kSecXPCKeyError
, xpcError
);
1491 } else if (replyMessage
) {
1492 secdebug("ipc", "%@ %@ responding %@", client
.task
, SOSCCGetOperationDescription((enum SecXPCOperation
)operation
), replyMessage
);
1495 SecCFCreateErrorWithFormat(kSecXPCErrorUnexpectedType
, sSecXPCErrorDomain
, NULL
, &error
, 0, CFSTR("Messages expect to be xpc dictionary, got: %@"), event
);
1496 secerror("%@: returning error: %@", client
.task
, error
);
1497 xpcError
= SecCreateXPCObjectWithCFError(error
);
1498 replyMessage
= xpc_create_reply_with_format(event
, "{%string: %value}", kSecXPCKeyError
, xpcError
);
1502 xpc_connection_send_message(connection
, replyMessage
);
1503 xpc_release(replyMessage
);
1506 xpc_release(xpcError
);
1507 CFReleaseSafe(error
);
1508 CFReleaseSafe(client
.accessGroups
);
1509 CFReleaseSafe(client
.musr
);
1510 CFReleaseSafe(client
.task
);
1511 CFReleaseSafe(domains
);
1512 CFReleaseSafe(clientAuditToken
);
1515 static void securityd_xpc_init(const char *service_name
)
1517 secdebug("serverxpc", "start");
1518 xpc_connection_t listener
= xpc_connection_create_mach_service(service_name
, NULL
, XPC_CONNECTION_MACH_SERVICE_LISTENER
);
1520 seccritical("security failed to register xpc listener for %s, exiting", service_name
);
1524 xpc_connection_set_event_handler(listener
, ^(xpc_object_t connection
) {
1525 if (xpc_get_type(connection
) == XPC_TYPE_CONNECTION
) {
1526 xpc_connection_set_event_handler(connection
, ^(xpc_object_t event
) {
1527 if (xpc_get_type(event
) == XPC_TYPE_DICTIONARY
) {
1528 // Synchronous. The client has a connection pool so they can be somewhat re-entrant if they need.
1529 securityd_xpc_dictionary_handler(connection
, event
);
1532 xpc_connection_resume(connection
);
1535 xpc_connection_resume(listener
);
1538 xpc_activity_register("com.apple.securityd.daily", XPC_ACTIVITY_CHECK_IN
, ^(xpc_activity_t activity
) {
1539 xpc_activity_state_t activityState
= xpc_activity_get_state(activity
);
1540 if (activityState
== XPC_ACTIVITY_STATE_RUN
) {
1541 SecCKKS24hrNotification();
1542 SecOctagon24hrNotification();
1547 #if OCTAGON && !TARGET_OS_BRIDGE
1548 // Kick off reporting tasks.
1549 if (os_variant_has_internal_diagnostics("com.apple.security") && !os_variant_is_recovery("securityd")) {
1550 InitPolicyReporter();
1556 // <rdar://problem/22425706> 13B104+Roots:Device never moved past spinner after using approval to ENABLE icdp
1558 #if TARGET_OS_IPHONE && !TARGET_OS_SIMULATOR
1559 static void securityd_soscc_lock_hack() {
1560 dispatch_queue_t soscc_lock_queue
= dispatch_queue_create("soscc_lock_queue", DISPATCH_QUEUE_PRIORITY_DEFAULT
);
1563 // <rdar://problem/22500239> Prevent securityd from quitting while holding a keychain assertion
1564 // FIXME: securityd isn't currently registering for any other notifyd events. If/when it does,
1565 // this code will need to be generalized / migrated away from just this specific purpose.
1566 xpc_set_event_stream_handler("com.apple.notifyd.matching", dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH
, 0), ^(xpc_object_t object
) {
1567 char *event_description
= xpc_copy_description(object
);
1568 secnotice("events", "%s", event_description
);
1569 free(event_description
);
1572 secnotice("lockassertion", "notify_register_dispatch(kSOSCCHoldLockForInitialSync)");
1573 notify_register_dispatch(kSOSCCHoldLockForInitialSync
, &soscc_tok
, soscc_lock_queue
, ^(int token __unused
) {
1574 secnotice("lockassertion", "kSOSCCHoldLockForInitialSync: grabbing the lock");
1575 CFErrorRef error
= NULL
;
1577 uint64_t one_minute
= 60ull;
1578 if(SecAKSUserKeybagHoldLockAssertion(one_minute
, &error
)){
1579 // <rdar://problem/22500239> Prevent securityd from quitting while holding a keychain assertion
1580 os_transaction_t transaction
= os_transaction_create("securityd-LockAssertedingHolder");
1582 dispatch_after(dispatch_time(DISPATCH_TIME_NOW
, one_minute
*NSEC_PER_SEC
), soscc_lock_queue
, ^{
1583 CFErrorRef localError
= NULL
;
1584 if(!SecAKSUserKeybagDropLockAssertion(&localError
))
1585 secerror("failed to unlock: %@", localError
);
1586 CFReleaseNull(localError
);
1587 os_release(transaction
);
1590 secerror("Failed to take device lock assertion: %@", error
);
1592 CFReleaseNull(error
);
1593 secnotice("lockassertion", "kSOSCCHoldLockForInitialSync => done");
1603 static char homeDir
[PATH_MAX
] = {};
1605 if (homeDir
[0] == '\0') {
1606 struct passwd
* pwd
= getpwuid(getuid());
1610 if (realpath(pwd
->pw_dir
, homeDir
) == NULL
) {
1611 strlcpy(homeDir
, pwd
->pw_dir
, sizeof(homeDir
));
1619 int main(int argc
, char *argv
[])
1621 DisableLocalization();
1623 char *wait4debugger
= getenv("WAIT4DEBUGGER");
1624 if (wait4debugger
&& !strcasecmp("YES", wait4debugger
)) {
1625 seccritical("SIGSTOPing self, awaiting debugger");
1626 kill(getpid(), SIGSTOP
);
1627 seccritical("Again, for good luck (or bad debuggers)");
1628 kill(getpid(), SIGSTOP
);
1631 /* <rdar://problem/15792007> Users with network home folders are unable to use/save password for Mail/Cal/Contacts/websites
1632 Secd doesn't realize DB connections get invalidated when network home directory users logout
1633 and their home gets unmounted. Exit secd, start fresh when user logs back in.
1636 int sessionstatechanged_tok
;
1637 notify_register_dispatch(kSA_SessionStateChangedNotification
, &sessionstatechanged_tok
, dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT
, 0), ^(int token __unused
) {
1638 // we could be a process running as root.
1639 // However, since root never logs out this isn't an issue.
1640 if (SASSessionStateForUser(getuid()) == kSA_state_loggingout_pointofnoreturn
) {
1641 dispatch_after(dispatch_time(DISPATCH_TIME_NOW
, 3ull*NSEC_PER_SEC
), dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT
, 0), ^{
1642 xpc_transaction_exit_clean();
1649 #define SECD_PROFILE_NAME "com.apple.secd"
1650 const char *homedir
= homedirPath();
1651 if (homedir
== NULL
)
1652 errx(1, "failed to get home directory for secd");
1654 char *errorbuf
= NULL
;
1655 const char *sandbox_params
[] = {
1661 rc
= sandbox_init_with_parameters(SECD_PROFILE_NAME
, SANDBOX_NAMED
, sandbox_params
, &errorbuf
);
1663 err(1, "Failed to process in a sandbox: %d %s", rc
, errorbuf
);
1664 #endif /* TARGET_OS_OSX */
1666 const char *serviceName
= kSecuritydXPCServiceName
;
1668 // Mark our interest in running some features (before we bring the DB layer up)
1670 EscrowRequestServerSetEnabled(true);
1671 OctagonSetShouldPerformInitialization(true);
1675 /* setup SQDLite before some other component have a chance to create a database connection */
1676 _SecDbServerSetup();
1678 securityd_init_server();
1679 securityd_xpc_init(serviceName
);
1681 SecCreateSecuritydXPCServer();
1682 #if SECUREOBJECTSYNC
1683 SOSControlServerInitialize();
1686 CKKSControlServerInitialize();
1687 OctagonControlServerInitialize();
1688 EscrowRequestXPCServerInitialize();
1691 // <rdar://problem/22425706> 13B104+Roots:Device never moved past spinner after using approval to ENABLE icdp
1692 #if TARGET_OS_IPHONE && !TARGET_OS_SIMULATOR
1693 securityd_soscc_lock_hack();
1699 /* vi:set ts=4 sw=4 et: */