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
= NULL
;
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 if (lseek(fd
, 0, SEEK_SET
)) {
284 secdebug("backup", "Could not seek in fd %d for %@", fd
, backupName
);
285 return SecCheckErrno(true, error
, CFSTR("lseek"));
288 FILE *backup
= fdopen(fd
, "r");
290 secdebug("backup", "Receiving file for %@ failed, %d", backupName
, errno
);
291 SecCheckErrno(!backup
, error
, CFSTR("fdopen"));
293 secdebug("backup", "Encountered error closing file %@: %d", backupName
, errno
);
294 SecCheckErrno(true, error
, CFSTR("close"));
299 if (fstat(fd
, &sb
)) {
300 secdebug("backup", "Unable to get file metadata for %@, fd %d", backupName
, fd
);
301 SecCheckErrno(true, error
, CFSTR("fstat"));
302 if (fclose(backup
)) {
303 secdebug("backup", "Encountered error closing file %@: %d", backupName
, errno
);
304 SecCheckErrno(true, error
, CFSTR("fclose"));
308 secdebug("backup", "Receiving file for %@ with fd %d of size %llu", backupName
, fd
, sb
.st_size
);
312 if (fclose(backup
)) {
313 secdebug("backup", "Encountered error %d closing file %@ after backup handler", errno
, backupName
);
314 SecCheckErrno(true, error
, CFSTR("fclose"));
315 // read only file and block has its own error handling for IO failure, no need to return false
320 static CFArrayRef
SecItemBackupCopyNames(CFErrorRef
*error
)
322 __block CFArrayRef result
;
323 os_activity_initiate("SecItemBackupCopyNames", OS_ACTIVITY_FLAG_DEFAULT
, ^{
324 result
= SECURITYD_XPC(sec_item_backup_copy_names
, to_array_error_request
, error
);
329 bool SecItemBackupWithRegisteredBackups(CFErrorRef
*error
, void(^backup
)(CFStringRef backupName
)) {
330 CFArrayRef backupNames
= SecItemBackupCopyNames(error
);
331 if (!backupNames
) return false;
333 CFArrayForEachC(backupNames
, name
) {
336 CFRelease(backupNames
);
340 static bool SecItemBackupDoResetEventBody(const uint8_t *der
, const uint8_t *der_end
, CFErrorRef
*error
, void (^handleEvent
)(SecBackupEventType et
, CFTypeRef key
, CFTypeRef item
)) {
342 const uint8_t *sequence_body
= ccder_decode_len(&sequence_len
, der
, der_end
);
343 bool ok
= sequence_body
;
344 if (ok
&& sequence_body
+ sequence_len
!= der_end
) {
345 // Can't ever happen!
346 SecError(errSecDecode
, error
, CFSTR("trailing junk after reset"));
350 CFDataRef keybag
= NULL
;
351 if (sequence_body
!= der_end
) {
352 size_t keybag_len
= 0;
353 const uint8_t *keybag_start
= ccder_decode_tl(CCDER_OCTET_STRING
, &keybag_len
, sequence_body
, der_end
);
355 ok
= SecError(errSecDecode
, error
, CFSTR("failed to decode keybag"));
356 } else if (keybag_start
+ keybag_len
!= der_end
) {
357 ok
= SecError(errSecDecode
, error
, CFSTR("trailing junk after keybag"));
359 keybag
= CFDataCreate(kCFAllocatorDefault
, keybag_start
, keybag_len
);
362 handleEvent(kSecBackupEventReset
, keybag
, NULL
);
363 CFReleaseSafe(keybag
);
369 static bool SecItemBackupDoAddEvent(const uint8_t *der
, const uint8_t *der_end
, CFErrorRef
*error
, void (^handleEvent
)(SecBackupEventType et
, CFTypeRef key
, CFTypeRef item
)) {
370 CFDictionaryRef eventDict
= NULL
;
371 const uint8_t *der_end_of_dict
= der_decode_dictionary(kCFAllocatorDefault
, kCFPropertyListImmutable
, &eventDict
, error
, der
, der_end
);
372 if (der_end_of_dict
&& der_end_of_dict
!= der_end
) {
373 // Can't ever happen!
374 SecError(errSecDecode
, error
, CFSTR("trailing junk after add"));
375 der_end_of_dict
= NULL
;
377 if (der_end_of_dict
) {
378 CFDataRef hash
= CFDictionaryGetValue(eventDict
, kSecItemBackupHashKey
);
379 handleEvent(kSecBackupEventAdd
, hash
, eventDict
);
382 CFReleaseSafe(eventDict
);
383 return der_end_of_dict
;
386 static bool SecItemBackupDoCompleteEvent(const uint8_t *der
, const uint8_t *der_end
, CFErrorRef
*error
, void (^handleEvent
)(SecBackupEventType et
, CFTypeRef key
, CFTypeRef item
)) {
387 uint64_t event_num
= 0;
388 const uint8_t *der_end_of_num
= ccder_decode_uint64(&event_num
, der
, der_end
);
389 if (der_end_of_num
&& der_end_of_num
!= der_end
) {
390 // Can't ever happen!
391 SecError(errSecDecode
, error
, CFSTR("trailing junk after complete"));
392 der_end_of_num
= NULL
;
394 if (der_end_of_num
) {
395 handleEvent(kSecBackupEventComplete
, NULL
, NULL
);
397 return der_end_of_num
;
400 static bool SecItemBackupDoDeleteEventBody(const uint8_t *der
, const uint8_t *der_end
, CFErrorRef
*error
, void (^handleEvent
)(SecBackupEventType et
, CFTypeRef key
, CFTypeRef item
)) {
401 size_t digest_len
= 0;
402 const uint8_t *digest_start
= ccder_decode_len(&digest_len
, der
, der_end
);
403 if (digest_start
&& digest_start
+ digest_len
!= der_end
) {
404 // Can't ever happen!
405 SecError(errSecDecode
, error
, CFSTR("trailing junk after delete"));
409 CFDataRef hash
= CFDataCreate(kCFAllocatorDefault
, digest_start
, digest_len
);
410 handleEvent(kSecBackupEventRemove
, hash
, NULL
);
417 static void debugDisplayBackupEventTag(ccder_tag tag
) {
419 const char *eventDesc
;
421 case CCDER_CONSTRUCTED_SEQUENCE
: eventDesc
= "ResetEvent"; break;
422 case CCDER_CONSTRUCTED_SET
: eventDesc
= "AddEvent"; break;
423 case CCDER_INTEGER
: eventDesc
= "ResetEvent"; break;
424 case CCDER_OCTET_STRING
: eventDesc
= "DeleteEvent"; break;
425 default: eventDesc
= "UnknownEvent"; break;
427 secdebug("backup", "processing event %s (tag %08lX)", eventDesc
, tag
);
431 static bool SecItemBackupDoEvent(const uint8_t *der
, const uint8_t *der_end
, CFErrorRef
*error
, void (^handleEvent
)(SecBackupEventType et
, CFTypeRef key
, CFTypeRef item
)) {
433 const uint8_t *der_start_of_len
= ccder_decode_tag(&tag
, der
, der_end
);
434 debugDisplayBackupEventTag(tag
);
436 case CCDER_CONSTRUCTED_SEQUENCE
:
437 return SecItemBackupDoResetEventBody(der_start_of_len
, der_end
, error
, handleEvent
);
438 case CCDER_CONSTRUCTED_SET
:
439 return SecItemBackupDoAddEvent(der
, der_end
, error
, handleEvent
);
441 return SecItemBackupDoCompleteEvent(der
, der_end
, error
, handleEvent
);
442 case CCDER_OCTET_STRING
:
443 return SecItemBackupDoDeleteEventBody(der_start_of_len
, der_end
, error
, handleEvent
);
445 return SecError(errSecDecode
, error
, CFSTR("unsupported event tag: %lu"), tag
);
449 // TODO: Move to ccder and give better name.
450 static const uint8_t *ccder_decode_len_unchecked(size_t *lenp
, const uint8_t *der
, const uint8_t *der_end
) {
451 if (der
&& der
< der_end
) {
454 } else if (len
== 0x81) {
455 if (der_end
- der
< 1) goto errOut
;
457 } else if (len
== 0x82) {
458 if (der_end
- der
< 2) goto errOut
;
461 } else if (len
== 0x83) {
462 if (der_end
- der
< 3) goto errOut
;
463 len
= *(der
++) << 16;
464 len
+= *(der
++) << 8;
476 static bool SecKeychainWithBackupFileParse(FILE *backup
, CFErrorRef
*error
, void (^handleEvent
)(SecBackupEventType et
, CFTypeRef key
, CFTypeRef item
)) {
477 __block
bool ok
= true;
478 size_t buf_remaining
= 0;
479 const size_t read_ahead
= 16;
480 size_t buf_len
= read_ahead
;
481 uint8_t *buf
= malloc(buf_len
);
483 const size_t bytes_read
= fread(buf
+ buf_remaining
, 1, read_ahead
- buf_remaining
, backup
);
484 if (bytes_read
<= 0) {
486 ok
= SecCheckErrno(true, error
, CFSTR("read backup event header"));
487 else if (!buf_remaining
) {
488 // Nothing read, nothing in buffer, clean eof.
492 const size_t buf_avail
= bytes_read
+ buf_remaining
;
494 const uint8_t *der
= buf
;
495 const uint8_t *der_end
= der
+ buf_avail
;
498 der
= ccder_decode_tag(&tag
, der
, der_end
);
499 der
= ccder_decode_len_unchecked(&body_len
, der
, der_end
);
502 ok
= SecError(errSecDecode
, error
, CFSTR("failed to decode backup event header"));
506 const size_t decoded_len
= der
- buf
;
507 size_t event_len
= decoded_len
+ body_len
;
508 if (event_len
> buf_avail
) {
509 // We need to read the rest of this event, first
510 // ensure we have enough space.
511 if (buf_len
< event_len
) {
512 // TODO: Think about a max for buf_len here to prevent attacks.
513 uint8_t *new_buf
= realloc(buf
, event_len
);
515 ok
= SecError(errSecAllocate
, error
, CFSTR("realloc buf failed"));
522 // Read tail of current event.
523 const size_t tail_len
= fread(buf
+ buf_avail
, 1, event_len
- buf_avail
, backup
);
524 if (tail_len
< event_len
- buf_avail
) {
526 ok
= SecCheckErrno(true, error
, CFSTR("failed to read event body"));
528 ok
= SecError(errSecDecode
, error
, CFSTR("unexpected end of event file %zu of %zu bytes read"), tail_len
, event_len
- buf_avail
);
534 // Adjust der_end to the end of the event.
535 der_end
= buf
+ event_len
;
537 ok
&= SecItemBackupDoEvent(buf
, der_end
, error
, handleEvent
);
539 if (event_len
< buf_avail
) {
540 // Shift remaining bytes to start of buffer.
541 buf_remaining
= buf_avail
- event_len
;
542 memmove(buf
, der_end
, buf_remaining
);
551 bool SecItemBackupWithChanges(CFStringRef backupName
, CFErrorRef
*error
, void (^handleEvent
)(SecBackupEventType et
, CFTypeRef key
, CFTypeRef item
)) {
552 __block
bool ok
= true;
553 __block CFErrorRef localError
= NULL
;
554 ok
&= SecKeychainWithBackupFile(backupName
, &localError
, ^(FILE *backup
) {
555 ok
&= SecKeychainWithBackupFileParse(backup
, &localError
, handleEvent
);
557 if (!ok
) { // TODO: remove this logging
558 secdebug("backup", "SecItemBackupWithChanges failed: %@", localError
);
559 handleEvent(kSecBackupEventComplete
, NULL
, NULL
);
560 CFErrorPropagate(localError
, error
);
566 bool SecItemBackupSetConfirmedManifest(CFStringRef backupName
, CFDataRef keybagDigest
, CFDataRef manifest
, CFErrorRef
*error
) {
568 os_activity_initiate("SecItemBackupSetConfirmedManifest", OS_ACTIVITY_FLAG_DEFAULT
, ^{
569 result
= SECURITYD_XPC(sec_item_backup_set_confirmed_manifest
, string_data_data_to_bool_error_request
, backupName
, keybagDigest
, manifest
, error
);
574 void SecItemBackupRestore(CFStringRef backupName
, CFStringRef peerID
, CFDataRef keybag
, CFDataRef secret
, CFTypeRef backup
, void (^completion
)(CFErrorRef error
)) {
575 __block CFErrorRef localError
= NULL
;
576 os_activity_initiate("SecItemBackupRestore", OS_ACTIVITY_FLAG_DEFAULT
, ^{
577 SECURITYD_XPC(sec_item_backup_restore
, string_string_data_data_data_to_bool_error_request
, backupName
, peerID
, keybag
, secret
, backup
, &localError
);
579 completion(localError
);
580 CFReleaseSafe(localError
);
583 CFDictionaryRef
SecItemBackupCopyMatching(CFDataRef keybag
, CFDataRef secret
, CFDictionaryRef backup
, CFDictionaryRef query
, CFErrorRef
*error
) {
584 SecError(errSecUnimplemented
, error
, CFSTR("SecItemBackupCopyMatching unimplemented"));
588 bool SecBackupKeybagAdd(CFDataRef passcode
, CFDataRef
*identifier
, CFURLRef
*pathinfo
, CFErrorRef
*error
) {
589 __block
bool result
= false;
590 os_activity_initiate("_SecServerBackupKeybagAdd", OS_ACTIVITY_FLAG_DEFAULT
, ^{
591 securityd_send_sync_and_do(kSecXPCOpBackupKeybagAdd
, error
, ^bool(xpc_object_t message
, CFErrorRef
*error
) {
592 return SecXPCDictionarySetDataOptional(message
, kSecXPCKeyUserPassword
, passcode
, error
);
593 }, ^bool(xpc_object_t response
, CFErrorRef
*error
) {
594 result
= SecXPCDictionaryCopyDataOptional(response
, kSecXPCKeyBackupKeybagIdentifier
, identifier
, error
) &&
595 SecXPCDictionaryCopyURLOptional(response
, kSecXPCKeyBackupKeybagPath
, pathinfo
, error
) &&
596 SecXPCDictionaryGetBool(response
, kSecXPCKeyResult
, error
);
603 bool SecBackupKeybagDelete(CFDictionaryRef query
, CFErrorRef
*error
) {
604 __block
bool result
= false;
605 os_activity_initiate("_SecBackupKeybagDelete", OS_ACTIVITY_FLAG_DEFAULT
, ^{
606 securityd_send_sync_and_do(kSecXPCOpBackupKeybagDelete
, error
, ^bool(xpc_object_t message
, CFErrorRef
*error
) {
607 return SecXPCDictionarySetPList(message
, kSecXPCKeyQuery
, query
, error
);
608 }, ^bool(xpc_object_t response
, CFErrorRef
*error
) {
609 result
= SecXPCDictionaryGetBool(response
, kSecXPCKeyResult
, error
);