]> git.saurik.com Git - apple/security.git/blob - OSX/libsecurity_keychain/regressions/si-33-keychain-backup.c
Security-59754.80.3.tar.gz
[apple/security.git] / OSX / libsecurity_keychain / regressions / si-33-keychain-backup.c
1 /*
2 * Copyright (c) 2013-2014 Apple Inc. All Rights Reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
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
11 * file.
12 *
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.
20 *
21 * @APPLE_LICENSE_HEADER_END@
22 */
23
24 #include <CoreFoundation/CoreFoundation.h>
25 #include <TargetConditionals.h>
26 #include <stdio.h>
27
28 #include "keychain_regressions.h"
29 #include <Security/SecBase.h>
30 #include <Security/SecItem.h>
31 #include <Security/SecItemPriv.h>
32 #include <utilities/SecCFRelease.h>
33 #include <libaks.h>
34 #include <AssertMacros.h>
35
36 #define DATA_ARG(x) (x) ? CFDataGetBytePtr((x)) : NULL, (x) ? (int)CFDataGetLength((x)) : 0
37
38 static CFDataRef create_keybag(keybag_handle_t bag_type, CFDataRef password)
39 {
40 keybag_handle_t handle = bad_keybag_handle;
41
42 if (aks_create_bag(DATA_ARG(password), bag_type, &handle) == 0) {
43 void * keybag = NULL;
44 int keybag_size = 0;
45 if (aks_save_bag(handle, &keybag, &keybag_size) == 0) {
46 return CFDataCreate(kCFAllocatorDefault, keybag, keybag_size);
47 }
48 }
49
50 return CFDataCreate(kCFAllocatorDefault, NULL, 0);
51 }
52
53 /* Test low level keychain migration from device to device interface. */
54 static void tests(void)
55 {
56 int v_eighty = 80;
57 CFNumberRef eighty = CFNumberCreate(NULL, kCFNumberSInt32Type, &v_eighty);
58 const char *v_data = "test";
59 CFDataRef pwdata = CFDataCreate(NULL, (UInt8 *)v_data, strlen(v_data));
60 CFMutableDictionaryRef query = CFDictionaryCreateMutable(NULL, 0, &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks);
61 CFTypeRef result = NULL;
62 CFDictionaryAddValue(query, kSecClass, kSecClassInternetPassword);
63 CFDictionaryAddValue(query, kSecAttrServer, CFSTR("members.spamcop.net"));
64 CFDictionaryAddValue(query, kSecAttrAccount, CFSTR("smith"));
65 CFDictionaryAddValue(query, kSecAttrPort, eighty); CFReleaseNull(eighty);
66 CFDictionaryAddValue(query, kSecAttrProtocol, kSecAttrProtocolHTTP);
67 CFDictionaryAddValue(query, kSecAttrAuthenticationType, kSecAttrAuthenticationTypeDefault);
68 CFDictionaryAddValue(query, kSecValueData, pwdata); CFReleaseNull(pwdata);
69 CFDictionaryAddValue(query, kSecAttrSynchronizable, kCFBooleanTrue);
70
71 CFDataRef keybag = NULL;
72 const char *p = "sup3rsekretpassc0de";
73 CFDataRef password = CFDataCreate(NULL, (UInt8 *)p, strlen(p));
74
75 keybag = create_keybag(kAppleKeyStoreAsymmetricBackupBag, password);
76
77 SecItemDelete(query);
78
79 // add syncable item
80 ok_status(SecItemAdd(query, NULL), "add internet password");
81
82 ok_status(SecItemCopyMatching(query, &result), "find item we are about to destroy");
83 if (result) { CFRelease(result); result = NULL; }
84
85 CFDictionaryRef backup = NULL;
86
87 ok_status(_SecKeychainBackupSyncable(keybag, password, NULL, &backup), "export items");
88
89 ok_status(SecItemDelete(query), "delete item we backed up");
90 is_status(SecItemCopyMatching(query, &result), errSecItemNotFound, "find item we are about to destroy");
91 if (result) { CFRelease(result); result = NULL; }
92
93 ok_status(_SecKeychainRestoreSyncable(keybag, password, backup), "import items");
94
95 ok_status(SecItemCopyMatching(query, &result), "find restored item");
96 if (result) { CFRelease(result); result = NULL; }
97
98 ok_status(SecItemDelete(query), "delete restored item");
99
100 CFReleaseNull(backup);
101 CFReleaseNull(keybag);
102 CFReleaseNull(query);
103 CFReleaseNull(password);
104 }
105
106 int si_33_keychain_backup(int argc, char *const *argv)
107 {
108 plan_tests(8);
109
110
111 tests();
112
113 return 0;
114 }