2 // si-33-keychain-backup.c
3 // libsecurity_keychain
5 // Created by Wade Benson on 4/16/13.
9 #include <TargetConditionals.h>
12 #include "keychain_regressions.h"
13 #include <Security/SecBase.h>
14 #include <Security/SecItem.h>
15 #include <Security/SecItemPriv.h>
17 #include <AssertMacros.h>
20 static CFDataRef
create_keybag(keybag_handle_t bag_type
, CFDataRef password
)
22 keybag_handle_t handle
= bad_keybag_handle
;
24 if (aks_create_bag(NULL
, 0, bag_type
, &handle
) == 0) {
27 if (aks_save_bag(handle
, &keybag
, &keybag_size
) == 0) {
28 return CFDataCreate(kCFAllocatorDefault
, keybag
, keybag_size
);
32 return CFDataCreate(kCFAllocatorDefault
, NULL
, 0);
35 /* Test low level keychain migration from device to device interface. */
36 static void tests(void)
39 CFNumberRef eighty
= CFNumberCreate(NULL
, kCFNumberSInt32Type
, &v_eighty
);
40 const char *v_data
= "test";
41 CFDataRef pwdata
= CFDataCreate(NULL
, (UInt8
*)v_data
, strlen(v_data
));
42 CFMutableDictionaryRef query
= CFDictionaryCreateMutable(NULL
, 0, NULL
, NULL
);
43 CFTypeRef result
= NULL
;
44 CFDictionaryAddValue(query
, kSecClass
, kSecClassInternetPassword
);
45 CFDictionaryAddValue(query
, kSecAttrServer
, CFSTR("members.spamcop.net"));
46 CFDictionaryAddValue(query
, kSecAttrAccount
, CFSTR("smith"));
47 CFDictionaryAddValue(query
, kSecAttrPort
, eighty
);
48 CFDictionaryAddValue(query
, kSecAttrProtocol
, kSecAttrProtocolHTTP
);
49 CFDictionaryAddValue(query
, kSecAttrAuthenticationType
, kSecAttrAuthenticationTypeDefault
);
50 CFDictionaryAddValue(query
, kSecValueData
, pwdata
);
51 CFDictionaryAddValue(query
, kSecAttrSynchronizable
, kCFBooleanTrue
);
53 CFDataRef keybag
= NULL
, password
= NULL
;
55 keybag
= create_keybag(kAppleKeyStoreAsymmetricBackupBag
, password
);
60 ok_status(SecItemAdd(query
, NULL
), "add internet password");
62 ok_status(SecItemCopyMatching(query
, &result
), "find item we are about to destroy");
63 if (result
) { CFRelease(result
); result
= NULL
; }
65 CFDictionaryRef backup
= NULL
;
67 ok_status(_SecKeychainBackupSyncable(keybag
, password
, NULL
, &backup
), "export items");
69 ok_status(SecItemDelete(query
), "delete item we backed up");
70 is_status(SecItemCopyMatching(query
, &result
), errSecItemNotFound
, "find item we are about to destroy");
71 if (result
) { CFRelease(result
); result
= NULL
; }
73 ok_status(_SecKeychainRestoreSyncable(keybag
, password
, backup
), "import items");
75 ok_status(SecItemCopyMatching(query
, &result
), "find restored item");
76 if (result
) { CFRelease(result
); result
= NULL
; }
78 ok_status(SecItemDelete(query
), "delete restored item");
80 if (backup
) { CFRelease(backup
); }
83 int si_33_keychain_backup(int argc
, char *const *argv
)