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