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