]> git.saurik.com Git - apple/security.git/blob - sec/securityd/Regressions/secd-01-items.c
Security-55471.14.18.tar.gz
[apple/security.git] / sec / securityd / Regressions / secd-01-items.c
1 //
2 // secd-01-dbitems.c
3 // sec
4 //
5 // Created by Fabrice Gautier on 5/29/13.
6 //
7 //
8
9
10 #include "secd_regressions.h"
11
12 #include <securityd/SecDbItem.h>
13 #include <securityd/SecItemServer.h>
14
15 #include <utilities/array_size.h>
16 #include <utilities/SecFileLocations.h>
17
18 #include <unistd.h>
19
20 #include "SecdTestKeychainUtilities.h"
21
22 #if USE_KEYSTORE
23 #include <libaks.h>
24 void kc_dbhandle_reset(void);
25
26 int secd_01_items(int argc, char *const *argv)
27 {
28 plan_tests(24 + kSecdTestSetupTestCount);
29
30 /* custom keychain dir */
31 secd_test_setup_temp_keychain("secd_01_items", ^{
32 });
33
34 /* custom keybag */
35 keybag_handle_t keybag;
36 keybag_state_t state;
37 char *passcode="password";
38 int passcode_len=(int)strlen(passcode);
39
40 ok(kIOReturnSuccess==aks_create_bag(passcode, passcode_len, kAppleKeyStoreDeviceBag, &keybag), "create keybag");
41 ok(kIOReturnSuccess==aks_get_lock_state(keybag, &state), "get keybag state");
42 ok(!(state&keybag_state_locked), "keybag unlocked");
43 SecItemServerSetKeychainKeybag(keybag);
44
45 /* lock */
46 ok(kIOReturnSuccess==aks_lock_bag(keybag), "lock keybag");
47 ok(kIOReturnSuccess==aks_get_lock_state(keybag, &state), "get keybag state");
48 ok(state&keybag_state_locked, "keybag locked");
49
50
51 kc_dbhandle_reset();
52
53 /* Creating a password */
54 int v_eighty = 80;
55 CFNumberRef eighty = CFNumberCreate(NULL, kCFNumberSInt32Type, &v_eighty);
56 const char *v_data = "test";
57 CFDataRef pwdata = CFDataCreate(NULL, (UInt8 *)v_data, strlen(v_data));
58 const void *keys[] = {
59 kSecClass,
60 kSecAttrServer,
61 kSecAttrAccount,
62 kSecAttrPort,
63 kSecAttrProtocol,
64 kSecAttrAuthenticationType,
65 kSecValueData
66 };
67 const void *values[] = {
68 kSecClassInternetPassword,
69 CFSTR("members.spamcop.net"),
70 CFSTR("smith"),
71 eighty,
72 CFSTR("http"),
73 CFSTR("dflt"),
74 pwdata
75 };
76 CFDictionaryRef item = CFDictionaryCreate(NULL, keys, values,
77 array_size(keys), NULL, NULL);
78
79
80 is_status(SecItemAdd(item, NULL), errSecInteractionNotAllowed, "add internet password while locked");
81
82 /* unlock */
83 ok(kIOReturnSuccess==aks_unlock_bag(keybag, passcode, passcode_len), "unlock keybag");
84 ok(kIOReturnSuccess==aks_get_lock_state(keybag, &state), "get keybag state");
85 ok(!(state&keybag_state_locked), "keybag unlocked");
86
87 ok_status(SecItemAdd(item, NULL), "add internet password, while unlocked");
88
89
90 /* lock */
91 ok(kIOReturnSuccess==aks_lock_bag(keybag), "lock keybag");
92 ok(kIOReturnSuccess==aks_get_lock_state(keybag, &state), "get keybag state");
93 ok(state&keybag_state_locked, "keybag locked");
94
95 is_status(SecItemAdd(item, NULL), errSecInteractionNotAllowed,
96 "add internet password again, while locked");
97
98 /* unlock */
99 ok(kIOReturnSuccess==aks_unlock_bag(keybag, passcode, passcode_len), "unlock keybag");
100 ok(kIOReturnSuccess==aks_get_lock_state(keybag, &state), "get keybag state");
101 ok(!(state&keybag_state_locked), "keybag unlocked");
102
103 is_status(SecItemAdd(item, NULL), errSecDuplicateItem,
104 "add internet password again, while unlocked");
105
106 CFTypeRef results = NULL;
107 /* Create a dict with all attrs except the data. */
108 CFDictionaryRef query = CFDictionaryCreate(NULL, keys, values,
109 (array_size(keys)) - 1, NULL, NULL);
110 ok_status(SecItemCopyMatching(query, &results), "find internet password, while unlocked ");
111 if (results) {
112 CFRelease(results);
113 results = NULL;
114 }
115
116 /* lock */
117 ok(kIOReturnSuccess==aks_lock_bag(keybag), "lock keybag");
118 ok(kIOReturnSuccess==aks_get_lock_state(keybag, &state), "get keybag state");
119 ok(state&keybag_state_locked, "keybag locked");
120
121 is_status(SecItemCopyMatching(query, &results), errSecInteractionNotAllowed, "find internet password, while locked ");
122
123 /* Reset keybag and custom $HOME */
124 SecItemServerResetKeychainKeybag();
125 SetCustomHomeURL(NULL);
126 kc_dbhandle_reset();
127
128 return 0;
129 }
130
131 #else
132
133 int secd_01_items(int argc, char *const *argv)
134 {
135 plan_tests(1);
136
137 todo("Not yet working in simulator");
138
139 TODO: {
140 ok(false);
141 }
142
143 /* not implemented in simulator (no keybag) */
144 return 0;
145 }
146 #endif
147