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@
26 * SecItemBackup.c - Client side backup interfaces and support code
29 #include <Security/SecItemBackup.h>
31 #include <Security/SecItemPriv.h>
32 #include <Security/SecuritydXPC.h>
33 #include <Security/SecFramework.h>
34 #include <securityd/SecItemServer.h>
35 #include <ipc/securityd_client.h>
36 #include <Security/SecureObjectSync/SOSBackupEvent.h>
37 #include <Security/SecureObjectSync/SOSCloudCircle.h>
38 #include <Security/SecureObjectSync/SOSViews.h>
39 #include <corecrypto/ccsha1.h>
40 #include <utilities/SecCFError.h>
41 #include <utilities/SecCFRelease.h>
42 #include <utilities/SecCFCCWrappers.h>
43 #include <utilities/array_size.h>
44 #include <utilities/der_plist.h>
45 #include <utilities/der_plist_internal.h>
46 #include <AssertMacros.h>
47 #include <os/activity.h>
52 static CFDataRef
client_data_data_to_data_error_request(enum SecXPCOperation op
, SecurityClient
*client
, CFDataRef keybag
, CFDataRef passcode
, CFErrorRef
*error
) {
53 __block CFDataRef result
= NULL
;
54 securityd_send_sync_and_do(op
, error
, ^bool(xpc_object_t message
, CFErrorRef
*error
) {
55 return SecXPCDictionarySetDataOptional(message
, kSecXPCKeyKeybag
, keybag
, error
)
56 && SecXPCDictionarySetDataOptional(message
, kSecXPCKeyUserPassword
, passcode
, error
);
57 }, ^bool(xpc_object_t response
, CFErrorRef
*error
) {
58 return (result
= SecXPCDictionaryCopyData(response
, kSecXPCKeyResult
, error
));
63 static bool data_client_data_data_to_error_request(enum SecXPCOperation op
, CFDataRef backup
, SecurityClient
*client
, CFDataRef keybag
, CFDataRef passcode
, CFErrorRef
*error
) {
64 return securityd_send_sync_and_do(op
, error
, ^bool(xpc_object_t message
, CFErrorRef
*error
) {
65 return SecXPCDictionarySetData(message
, kSecXPCKeyBackup
, backup
, error
)
66 && SecXPCDictionarySetData(message
, kSecXPCKeyKeybag
, keybag
, error
)
67 && SecXPCDictionarySetDataOptional(message
, kSecXPCKeyUserPassword
, passcode
, error
);
71 static bool dict_data_data_to_error_request(enum SecXPCOperation op
, CFDictionaryRef backup
, CFDataRef keybag
, CFDataRef passcode
, CFErrorRef
*error
) {
72 return securityd_send_sync_and_do(op
, error
, ^bool(xpc_object_t message
, CFErrorRef
*error
) {
73 return SecXPCDictionarySetPList(message
, kSecXPCKeyBackup
, backup
, error
)
74 && SecXPCDictionarySetData(message
, kSecXPCKeyKeybag
, keybag
, error
)
75 && SecXPCDictionarySetDataOptional(message
, kSecXPCKeyUserPassword
, passcode
, error
);
79 static CFDictionaryRef
data_data_dict_to_dict_error_request(enum SecXPCOperation op
, CFDictionaryRef backup
, CFDataRef keybag
, CFDataRef passcode
, CFErrorRef
*error
) {
80 __block CFDictionaryRef dict
= NULL
;
81 securityd_send_sync_and_do(op
, error
, ^bool(xpc_object_t message
, CFErrorRef
*error
) {
82 return SecXPCDictionarySetPListOptional(message
, kSecXPCKeyBackup
, backup
, error
)
83 && SecXPCDictionarySetData(message
, kSecXPCKeyKeybag
, keybag
, error
)
84 && SecXPCDictionarySetDataOptional(message
, kSecXPCKeyUserPassword
, passcode
, error
);
85 }, ^bool(xpc_object_t response
, CFErrorRef
*error
) {
86 return (dict
= SecXPCDictionaryCopyDictionary(response
, kSecXPCKeyResult
, error
));
91 static int string_to_fd_error_request(enum SecXPCOperation op
, CFStringRef backupName
, CFErrorRef
*error
) {
93 securityd_send_sync_and_do(op
, error
, ^bool(xpc_object_t message
, CFErrorRef
*error
) {
94 return SecXPCDictionarySetString(message
, kSecXPCKeyBackup
, backupName
, error
);
95 }, ^bool(xpc_object_t response
, CFErrorRef
*error
) {
96 fd
= SecXPCDictionaryDupFileDescriptor(response
, kSecXPCKeyResult
, error
);
102 static bool string_data_data_to_bool_error_request(enum SecXPCOperation op
, CFStringRef backupName
, CFDataRef keybagDigest
, CFDataRef manifest
, CFErrorRef
*error
)
104 return securityd_send_sync_and_do(op
, error
, ^bool(xpc_object_t message
, CFErrorRef
*error
) {
105 return SecXPCDictionarySetString(message
, kSecXPCKeyBackup
, backupName
, error
) &&
106 SecXPCDictionarySetDataOptional(message
, kSecXPCKeyKeybag
, keybagDigest
, error
) &&
107 SecXPCDictionarySetDataOptional(message
, kSecXPCData
, manifest
, error
);
108 }, ^bool(xpc_object_t response
, __unused CFErrorRef
*error
) {
109 return xpc_dictionary_get_bool(response
, kSecXPCKeyResult
);
113 static bool string_string_data_data_data_to_bool_error_request(enum SecXPCOperation op
, CFStringRef backupName
, CFStringRef peerID
, CFDataRef keybag
, CFDataRef secret
, CFDataRef backup
, CFErrorRef
*error
)
115 return securityd_send_sync_and_do(op
, error
, ^bool(xpc_object_t message
, CFErrorRef
*error
) {
116 return SecXPCDictionarySetString(message
, kSecXPCKeyBackup
, backupName
, error
) &&
117 SecXPCDictionarySetStringOptional(message
, kSecXPCKeyDigest
, peerID
, error
) &&
118 SecXPCDictionarySetData(message
, kSecXPCKeyKeybag
, keybag
, error
) &&
119 SecXPCDictionarySetData(message
, kSecXPCKeyUserPassword
, secret
, error
) &&
120 SecXPCDictionarySetData(message
, kSecXPCData
, backup
, error
);
121 }, ^bool(xpc_object_t response
, __unused CFErrorRef
*error
) {
122 return xpc_dictionary_get_bool(response
, kSecXPCKeyResult
);
126 static CFArrayRef
to_array_error_request(enum SecXPCOperation op
, CFErrorRef
*error
)
128 __block CFArrayRef result
= NULL
;
129 securityd_send_sync_and_do(op
, error
, ^bool(xpc_object_t message
, CFErrorRef
*error
) {
131 }, ^bool(xpc_object_t response
, CFErrorRef
*error
) {
132 return result
= SecXPCDictionaryCopyArray(response
, kSecXPCKeyResult
, error
);
139 static int SecItemBackupHandoffFD(CFStringRef backupName
, CFErrorRef
*error
) {
140 __block
int fileDesc
= -1;
141 os_activity_initiate("SecItemBackupHandoffFD", OS_ACTIVITY_FLAG_DEFAULT
, ^{
142 fileDesc
= SECURITYD_XPC(sec_item_backup_handoff_fd
, string_to_fd_error_request
, backupName
, error
);
147 CFDataRef
_SecKeychainCopyOTABackup(void) {
148 __block CFDataRef result
;
149 os_activity_initiate("_SecKeychainCopyOTABackup", OS_ACTIVITY_FLAG_DEFAULT
, ^{
150 result
= SECURITYD_XPC(sec_keychain_backup
, client_data_data_to_data_error_request
, SecSecurityClientGet(), NULL
, NULL
, NULL
);
155 CFDataRef
_SecKeychainCopyBackup(CFDataRef backupKeybag
, CFDataRef password
) {
156 __block CFDataRef result
;
157 os_activity_initiate("_SecKeychainCopyBackup", OS_ACTIVITY_FLAG_DEFAULT
, ^{
158 result
= SECURITYD_XPC(sec_keychain_backup
, client_data_data_to_data_error_request
, SecSecurityClientGet(), backupKeybag
, password
, NULL
);
163 bool _SecKeychainWriteBackupToFileDescriptor(CFDataRef backupKeybag
, CFDataRef password
, int fd
, CFErrorRef
*error
) {
164 __block
bool result
= false;
165 os_activity_initiate("_SecKeychainWriteBackupToFile", OS_ACTIVITY_FLAG_DEFAULT
, ^{
167 securityd_send_sync_and_do(sec_keychain_backup_id
, error
, ^bool(xpc_object_t message
, CFErrorRef
*error
) {
168 return SecXPCDictionarySetDataOptional(message
, kSecXPCKeyKeybag
, backupKeybag
, error
)
169 && SecXPCDictionarySetDataOptional(message
, kSecXPCKeyUserPassword
, password
, error
)
170 && SecXPCDictionarySetFileDescriptor(message
, kSecXPCKeyFileDescriptor
, fd
, error
);
171 }, ^bool(xpc_object_t response
, CFErrorRef
*error
) {
172 return (result
= SecXPCDictionaryGetBool(response
, kSecXPCKeyResult
, error
));
179 _SecKeychainRestoreBackupFromFileDescriptor(int fd
, CFDataRef backupKeybag
, CFDataRef password
, CFErrorRef
*error
)
182 os_activity_initiate("_SecKeychainRestoreBackup", OS_ACTIVITY_FLAG_DEFAULT
, ^{
183 securityd_send_sync_and_do(sec_keychain_restore_id
, error
, ^bool(xpc_object_t message
, CFErrorRef
*error
) {
184 return SecXPCDictionarySetFileDescriptor(message
, kSecXPCKeyFileDescriptor
, fd
, error
)
185 && SecXPCDictionarySetData(message
, kSecXPCKeyKeybag
, backupKeybag
, error
)
186 && SecXPCDictionarySetDataOptional(message
, kSecXPCKeyUserPassword
, password
, error
);
187 }, ^bool(xpc_object_t response
, CFErrorRef
*error
) {
188 return (result
= SecXPCDictionaryGetBool(response
, kSecXPCKeyResult
, error
));
195 * Current promise is that this is low memory usage, so in the current format, ask securityd
196 * to resolve the item for us.
200 _SecKeychainCopyKeybagUUIDFromFileDescriptor(int fd
, CFErrorRef
*error
)
202 __block CFStringRef result
;
203 os_activity_initiate("_SecKeychainCopyKeybagUUID", OS_ACTIVITY_FLAG_DEFAULT
, ^{
204 securityd_send_sync_and_do(sec_keychain_backup_keybag_uuid_id
, error
, ^bool(xpc_object_t message
, CFErrorRef
*error
) {
205 return SecXPCDictionarySetFileDescriptor(message
, kSecXPCKeyFileDescriptor
, fd
, error
);
206 }, ^bool(xpc_object_t response
, CFErrorRef
*error
) {
207 return (result
= SecXPCDictionaryCopyString(response
, kSecXPCKeyResult
, error
));
214 OSStatus
_SecKeychainRestoreBackup(CFDataRef backup
, CFDataRef backupKeybag
,
215 CFDataRef password
) {
216 __block OSStatus result
;
217 os_activity_initiate("_SecKeychainRestoreBackup", OS_ACTIVITY_FLAG_DEFAULT
, ^{
218 result
= SecOSStatusWith(^bool (CFErrorRef
*error
) {
219 return SECURITYD_XPC(sec_keychain_restore
, data_client_data_data_to_error_request
, backup
, SecSecurityClientGet(), backupKeybag
, password
, error
);
226 static int compareDigests(const void *l
, const void *r
) {
227 return memcmp(l
, r
, CCSHA1_OUTPUT_SIZE
);
230 CFDataRef
SecItemBackupCreateManifest(CFDictionaryRef backup
, CFErrorRef
*error
)
232 CFIndex count
= backup
? CFDictionaryGetCount(backup
) : 0;
233 CFMutableDataRef manifest
= CFDataCreateMutable(kCFAllocatorDefault
, CCSHA1_OUTPUT_SIZE
* count
);
235 CFDictionaryForEach(backup
, ^void (const void *key
, const void *value
) {
236 if (isDictionary(value
)) {
237 /* converting key back to binary blob is horrible */
238 CFDataRef sha1
= CFDictionaryGetValue(value
, kSecItemBackupHashKey
);
239 if (isData(sha1
) && CFDataGetLength(sha1
) == CCSHA1_OUTPUT_SIZE
) {
240 CFDataAppend(manifest
, sha1
);
242 CFStringRef sha1Hex
= CFDataCopyHexString(sha1
);
243 secerror("bad hash %@ in backup", sha1Hex
);
244 CFReleaseSafe(sha1Hex
);
245 // TODO: Drop this key from dictionary (outside the loop)
249 qsort(CFDataGetMutableBytePtr(manifest
), CFDataGetLength(manifest
) / CCSHA1_OUTPUT_SIZE
, CCSHA1_OUTPUT_SIZE
, compareDigests
);
254 OSStatus
_SecKeychainBackupSyncable(CFDataRef keybag
, CFDataRef password
, CFDictionaryRef backup_in
, CFDictionaryRef
*backup_out
)
256 return SecOSStatusWith(^bool (CFErrorRef
*error
) {
257 *backup_out
= SECURITYD_XPC(sec_keychain_backup_syncable
, data_data_dict_to_dict_error_request
, backup_in
, keybag
, password
, error
);
258 return *backup_out
!= NULL
;
262 OSStatus
_SecKeychainRestoreSyncable(CFDataRef keybag
, CFDataRef password
, CFDictionaryRef backup_in
)
264 __block OSStatus result
;
265 os_activity_initiate("_SecKeychainRestoreSyncable", OS_ACTIVITY_FLAG_DEFAULT
, ^{
266 result
= SecOSStatusWith(^bool (CFErrorRef
*error
) {
267 return SECURITYD_XPC(sec_keychain_restore_syncable
, dict_data_data_to_error_request
, backup_in
, keybag
, password
, error
);
275 static bool SecKeychainWithBackupFile(CFStringRef backupName
, CFErrorRef
*error
, void(^with
)(FILE *bufile
)) {
276 int fd
= SecItemBackupHandoffFD(backupName
, error
);
278 secdebug("backup", "SecItemBackupHandoffFD returned %d", fd
);
282 // Rewind file to start
283 lseek(fd
, 0, SEEK_SET
);
285 FILE *backup
= fdopen(fd
, "r");
288 secdebug("backup", "Receiving file for %@ failed, %d", backupName
, errno
);
289 return SecCheckErrno(!backup
, error
, CFSTR("fdopen"));
293 secdebug("backup", "Receiving file for %@ with fd %d of size %llu", backupName
, fd
, sb
.st_size
);
301 static CFArrayRef
SecItemBackupCopyNames(CFErrorRef
*error
)
303 __block CFArrayRef result
;
304 os_activity_initiate("SecItemBackupCopyNames", OS_ACTIVITY_FLAG_DEFAULT
, ^{
305 result
= SECURITYD_XPC(sec_item_backup_copy_names
, to_array_error_request
, error
);
310 bool SecItemBackupWithRegisteredBackups(CFErrorRef
*error
, void(^backup
)(CFStringRef backupName
)) {
311 CFArrayRef backupNames
= SecItemBackupCopyNames(error
);
312 if (!backupNames
) return false;
314 CFArrayForEachC(backupNames
, name
) {
317 CFRelease(backupNames
);
321 static bool SecItemBackupDoResetEventBody(const uint8_t *der
, const uint8_t *der_end
, CFErrorRef
*error
, void (^handleEvent
)(SecBackupEventType et
, CFTypeRef key
, CFTypeRef item
)) {
323 const uint8_t *sequence_body
= ccder_decode_len(&sequence_len
, der
, der_end
);
324 bool ok
= sequence_body
;
325 if (ok
&& sequence_body
+ sequence_len
!= der_end
) {
326 // Can't ever happen!
327 SecError(errSecDecode
, error
, CFSTR("trailing junk after reset"));
331 CFDataRef keybag
= NULL
;
332 if (sequence_body
!= der_end
) {
333 size_t keybag_len
= 0;
334 const uint8_t *keybag_start
= ccder_decode_tl(CCDER_OCTET_STRING
, &keybag_len
, sequence_body
, der_end
);
336 ok
= SecError(errSecDecode
, error
, CFSTR("failed to decode keybag"));
337 } else if (keybag_start
+ keybag_len
!= der_end
) {
338 ok
= SecError(errSecDecode
, error
, CFSTR("trailing junk after keybag"));
340 keybag
= CFDataCreate(kCFAllocatorDefault
, keybag_start
, keybag_len
);
343 handleEvent(kSecBackupEventReset
, keybag
, NULL
);
344 CFReleaseSafe(keybag
);
350 static bool SecItemBackupDoAddEvent(const uint8_t *der
, const uint8_t *der_end
, CFErrorRef
*error
, void (^handleEvent
)(SecBackupEventType et
, CFTypeRef key
, CFTypeRef item
)) {
351 CFDictionaryRef eventDict
= NULL
;
352 const uint8_t *der_end_of_dict
= der_decode_dictionary(kCFAllocatorDefault
, kCFPropertyListImmutable
, &eventDict
, error
, der
, der_end
);
353 if (der_end_of_dict
&& der_end_of_dict
!= der_end
) {
354 // Can't ever happen!
355 SecError(errSecDecode
, error
, CFSTR("trailing junk after add"));
356 der_end_of_dict
= NULL
;
358 if (der_end_of_dict
) {
359 CFDataRef hash
= CFDictionaryGetValue(eventDict
, kSecItemBackupHashKey
);
360 handleEvent(kSecBackupEventAdd
, hash
, eventDict
);
363 CFReleaseSafe(eventDict
);
364 return der_end_of_dict
;
367 static bool SecItemBackupDoCompleteEvent(const uint8_t *der
, const uint8_t *der_end
, CFErrorRef
*error
, void (^handleEvent
)(SecBackupEventType et
, CFTypeRef key
, CFTypeRef item
)) {
368 uint64_t event_num
= 0;
369 const uint8_t *der_end_of_num
= ccder_decode_uint64(&event_num
, der
, der_end
);
370 if (der_end_of_num
&& der_end_of_num
!= der_end
) {
371 // Can't ever happen!
372 SecError(errSecDecode
, error
, CFSTR("trailing junk after complete"));
373 der_end_of_num
= NULL
;
375 if (der_end_of_num
) {
376 handleEvent(kSecBackupEventComplete
, NULL
, NULL
);
378 return der_end_of_num
;
381 static bool SecItemBackupDoDeleteEventBody(const uint8_t *der
, const uint8_t *der_end
, CFErrorRef
*error
, void (^handleEvent
)(SecBackupEventType et
, CFTypeRef key
, CFTypeRef item
)) {
382 size_t digest_len
= 0;
383 const uint8_t *digest_start
= ccder_decode_len(&digest_len
, der
, der_end
);
384 if (digest_start
&& digest_start
+ digest_len
!= der_end
) {
385 // Can't ever happen!
386 SecError(errSecDecode
, error
, CFSTR("trailing junk after delete"));
390 CFDataRef hash
= CFDataCreate(kCFAllocatorDefault
, digest_start
, digest_len
);
391 handleEvent(kSecBackupEventRemove
, hash
, NULL
);
398 static void debugDisplayBackupEventTag(ccder_tag tag
) {
400 const char *eventDesc
;
402 case CCDER_CONSTRUCTED_SEQUENCE
: eventDesc
= "ResetEvent"; break;
403 case CCDER_CONSTRUCTED_SET
: eventDesc
= "AddEvent"; break;
404 case CCDER_INTEGER
: eventDesc
= "ResetEvent"; break;
405 case CCDER_OCTET_STRING
: eventDesc
= "DeleteEvent"; break;
406 default: eventDesc
= "UnknownEvent"; break;
408 secdebug("backup", "processing event %s (tag %08lX)", eventDesc
, tag
);
412 static bool SecItemBackupDoEvent(const uint8_t *der
, const uint8_t *der_end
, CFErrorRef
*error
, void (^handleEvent
)(SecBackupEventType et
, CFTypeRef key
, CFTypeRef item
)) {
414 const uint8_t *der_start_of_len
= ccder_decode_tag(&tag
, der
, der_end
);
415 debugDisplayBackupEventTag(tag
);
417 case CCDER_CONSTRUCTED_SEQUENCE
:
418 return SecItemBackupDoResetEventBody(der_start_of_len
, der_end
, error
, handleEvent
);
419 case CCDER_CONSTRUCTED_SET
:
420 return SecItemBackupDoAddEvent(der
, der_end
, error
, handleEvent
);
422 return SecItemBackupDoCompleteEvent(der
, der_end
, error
, handleEvent
);
423 case CCDER_OCTET_STRING
:
424 return SecItemBackupDoDeleteEventBody(der_start_of_len
, der_end
, error
, handleEvent
);
426 return SecError(errSecDecode
, error
, CFSTR("unsupported event tag: %lu"), tag
);
430 // TODO: Move to ccder and give better name.
431 static const uint8_t *ccder_decode_len_unchecked(size_t *lenp
, const uint8_t *der
, const uint8_t *der_end
) {
432 if (der
&& der
< der_end
) {
435 } else if (len
== 0x81) {
436 if (der_end
- der
< 1) goto errOut
;
438 } else if (len
== 0x82) {
439 if (der_end
- der
< 2) goto errOut
;
442 } else if (len
== 0x83) {
443 if (der_end
- der
< 3) goto errOut
;
444 len
= *(der
++) << 16;
445 len
+= *(der
++) << 8;
457 static bool SecKeychainWithBackupFileParse(FILE *backup
, CFErrorRef
*error
, void (^handleEvent
)(SecBackupEventType et
, CFTypeRef key
, CFTypeRef item
)) {
458 __block
bool ok
= true;
459 size_t buf_remaining
= 0;
460 const size_t read_ahead
= 16;
461 size_t buf_len
= read_ahead
;
462 uint8_t *buf
= malloc(buf_len
);
464 const size_t bytes_read
= fread(buf
+ buf_remaining
, 1, read_ahead
- buf_remaining
, backup
);
465 if (bytes_read
<= 0) {
467 ok
= SecCheckErrno(true, error
, CFSTR("read backup event header"));
468 else if (!buf_remaining
) {
469 // Nothing read, nothing in buffer, clean eof.
473 const size_t buf_avail
= bytes_read
+ buf_remaining
;
475 const uint8_t *der
= buf
;
476 const uint8_t *der_end
= der
+ buf_avail
;
479 der
= ccder_decode_tag(&tag
, der
, der_end
);
480 der
= ccder_decode_len_unchecked(&body_len
, der
, der_end
);
483 ok
= SecError(errSecDecode
, error
, CFSTR("failed to decode backup event header"));
487 const size_t decoded_len
= der
- buf
;
488 size_t event_len
= decoded_len
+ body_len
;
489 if (event_len
> buf_avail
) {
490 // We need to read the rest of this event, first
491 // ensure we have enough space.
492 if (buf_len
< event_len
) {
493 // TODO: Think about a max for buf_len here to prevent attacks.
494 uint8_t *new_buf
= realloc(buf
, event_len
);
496 ok
= SecError(errSecAllocate
, error
, CFSTR("realloc buf failed"));
503 // Read tail of current event.
504 const size_t tail_len
= fread(buf
+ buf_avail
, 1, event_len
- buf_avail
, backup
);
505 if (tail_len
< event_len
- buf_avail
) {
507 ok
= SecCheckErrno(true, error
, CFSTR("failed to read event body"));
509 ok
= SecError(errSecDecode
, error
, CFSTR("unexpected end of event file %zu of %zu bytes read"), tail_len
, event_len
- buf_avail
);
515 // Adjust der_end to the end of the event.
516 der_end
= buf
+ event_len
;
518 ok
&= SecItemBackupDoEvent(buf
, der_end
, error
, handleEvent
);
520 if (event_len
< buf_avail
) {
521 // Shift remaining bytes to start of buffer.
522 buf_remaining
= buf_avail
- event_len
;
523 memmove(buf
, der_end
, buf_remaining
);
532 bool SecItemBackupWithChanges(CFStringRef backupName
, CFErrorRef
*error
, void (^handleEvent
)(SecBackupEventType et
, CFTypeRef key
, CFTypeRef item
)) {
533 __block
bool ok
= true;
534 __block CFErrorRef localError
= NULL
;
535 ok
&= SecKeychainWithBackupFile(backupName
, &localError
, ^(FILE *backup
) {
536 ok
&= SecKeychainWithBackupFileParse(backup
, &localError
, handleEvent
);
538 if (!ok
) { // TODO: remove this logging
539 secdebug("backup", "SecItemBackupWithChanges failed: %@", localError
);
540 handleEvent(kSecBackupEventComplete
, NULL
, NULL
);
541 CFErrorPropagate(localError
, error
);
547 bool SecItemBackupSetConfirmedManifest(CFStringRef backupName
, CFDataRef keybagDigest
, CFDataRef manifest
, CFErrorRef
*error
) {
549 os_activity_initiate("SecItemBackupSetConfirmedManifest", OS_ACTIVITY_FLAG_DEFAULT
, ^{
550 result
= SECURITYD_XPC(sec_item_backup_set_confirmed_manifest
, string_data_data_to_bool_error_request
, backupName
, keybagDigest
, manifest
, error
);
555 void SecItemBackupRestore(CFStringRef backupName
, CFStringRef peerID
, CFDataRef keybag
, CFDataRef secret
, CFTypeRef backup
, void (^completion
)(CFErrorRef error
)) {
556 __block CFErrorRef localError
= NULL
;
557 os_activity_initiate("SecItemBackupRestore", OS_ACTIVITY_FLAG_DEFAULT
, ^{
558 SECURITYD_XPC(sec_item_backup_restore
, string_string_data_data_data_to_bool_error_request
, backupName
, peerID
, keybag
, secret
, backup
, &localError
);
560 completion(localError
);
561 CFReleaseSafe(localError
);
564 CFDictionaryRef
SecItemBackupCopyMatching(CFDataRef keybag
, CFDataRef secret
, CFDictionaryRef backup
, CFDictionaryRef query
, CFErrorRef
*error
) {
565 SecError(errSecUnimplemented
, error
, CFSTR("SecItemBackupCopyMatching unimplemented"));