]> git.saurik.com Git - apple/security.git/blob - OSX/sec/securityd/Regressions/secd-02-upgrade-while-locked.m
Security-58286.31.2.tar.gz
[apple/security.git] / OSX / sec / securityd / Regressions / secd-02-upgrade-while-locked.m
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 #include "secd_regressions.h"
26
27 #include <securityd/SecDbItem.h>
28 #include <utilities/array_size.h>
29 #include <utilities/SecCFWrappers.h>
30 #include <utilities/SecFileLocations.h>
31 #include <utilities/fileIo.h>
32
33 #include <securityd/SOSCloudCircleServer.h>
34 #include <securityd/SecItemServer.h>
35
36 #include <Security/SecBasePriv.h>
37
38 #include <TargetConditionals.h>
39 #include <AssertMacros.h>
40
41 #include <stdio.h>
42 #include <unistd.h>
43 #include <sys/stat.h>
44 #include <pthread.h>
45
46 #if TARGET_OS_IPHONE && USE_KEYSTORE
47 #include <libaks.h>
48
49 #include "SecdTestKeychainUtilities.h"
50
51 #include "ios6_1_keychain_2_db.h"
52
53 static OSStatus query_one(void)
54 {
55 OSStatus ok;
56
57 /* querying a password */
58 const void *keys[] = {
59 kSecClass,
60 kSecAttrAccessGroup,
61 };
62 const void *values[] = {
63 kSecClassGenericPassword,
64 CFSTR("test"),
65 };
66 CFDictionaryRef query = CFDictionaryCreate(NULL, keys, values,
67 array_size(keys), NULL, NULL);
68 CFTypeRef results = NULL;
69
70 ok = SecItemCopyMatching(query, &results);
71
72 CFReleaseSafe(results);
73 CFReleaseSafe(query);
74
75 return ok;
76 }
77
78
79
80 static void *do_query(void *arg)
81 {
82 /* querying a password */
83 const void *keys[] = {
84 kSecClass,
85 kSecAttrAccessGroup,
86 };
87 const void *values[] = {
88 kSecClassGenericPassword,
89 CFSTR("test"),
90 };
91 CFDictionaryRef query = CFDictionaryCreate(NULL, keys, values,
92 array_size(keys), NULL, NULL);
93 CFTypeRef results = NULL;
94
95 for(int i=0;i<20;i++)
96 verify_action(SecItemCopyMatching(query, &results)==errSecInteractionNotAllowed, CFReleaseSafe(query); return (void *)-1);
97
98 CFReleaseSafe(query);
99
100 return NULL;
101 }
102
103 static void *do_sos(void *arg)
104 {
105
106 for(int i=0;i<20;i++)
107 verify_action(SOSCCThisDeviceIsInCircle_Server(NULL)==-1, return (void *)-1);
108
109 return NULL;
110 }
111
112
113 #define N_THREADS 10
114
115 void SecAccessGroupsSetCurrent(CFArrayRef accessGroups);
116 CFArrayRef SecAccessGroupsGetCurrent();
117
118 int secd_02_upgrade_while_locked(int argc, char *const *argv)
119 {
120 plan_tests(11 + N_THREADS + kSecdTestSetupTestCount);
121
122 __block keybag_handle_t keybag;
123 __block keybag_state_t state;
124 char *passcode="password";
125 int passcode_len=(int)strlen(passcode);
126
127 /* custom keychain dir */
128 secd_test_setup_temp_keychain("secd_02_upgrade_while_locked", ^{
129 CFStringRef keychain_path_cf = __SecKeychainCopyPath();
130
131 CFStringPerformWithCString(keychain_path_cf, ^(const char *keychain_path) {
132 writeFile(keychain_path, ios6_1_keychain_2_db, ios6_1_keychain_2_db_len);
133
134 /* custom notification */
135 SecItemServerSetKeychainChangedNotification("com.apple.secdtests.keychainchanged");
136
137 /* Create and lock custom keybag */
138 ok(kIOReturnSuccess==aks_create_bag(passcode, passcode_len, kAppleKeyStoreDeviceBag, &keybag), "create keybag");
139 ok(kIOReturnSuccess==aks_get_lock_state(keybag, &state), "get keybag state");
140 ok(!(state&keybag_state_locked), "keybag unlocked");
141 SecItemServerSetKeychainKeybag(keybag);
142
143 /* lock */
144 ok(kIOReturnSuccess==aks_lock_bag(keybag), "lock keybag");
145 ok(kIOReturnSuccess==aks_get_lock_state(keybag, &state), "get keybag state");
146 ok(state&keybag_state_locked, "keybag locked");
147 });
148
149 CFReleaseSafe(keychain_path_cf);
150 });
151
152 CFArrayRef old_ag = SecAccessGroupsGetCurrent();
153 CFMutableArrayRef test_ag = CFArrayCreateMutableCopy(NULL, 0, old_ag);
154 CFArrayAppendValue(test_ag, CFSTR("test"));
155 SecAccessGroupsSetCurrent(test_ag);
156
157 pthread_t query_thread[N_THREADS];
158 pthread_t sos_thread;
159 void *query_err[N_THREADS] = {NULL,};
160 void *sos_err = NULL;
161
162 for(int i=0; i<N_THREADS; i++)
163 pthread_create(&query_thread[i], NULL, do_query, NULL);
164 pthread_create(&sos_thread, NULL, do_sos, NULL);
165
166 for(int i=0; i<N_THREADS; i++)
167 pthread_join(query_thread[i],&query_err[i]);
168 pthread_join(sos_thread, &sos_err);
169
170 for(int i=0; i<N_THREADS; i++)
171 ok(query_err[i]==NULL, "query thread ok");
172 ok(sos_err==NULL, "sos thread ok");
173
174 ok(kIOReturnSuccess==aks_unlock_bag(keybag, passcode, passcode_len), "lock keybag");
175 ok(kIOReturnSuccess==aks_get_lock_state(keybag, &state), "get keybag state");
176 ok(!(state&keybag_state_locked), "keybag unlocked");
177
178 is_status(query_one(), errSecItemNotFound, "Query after unlock");
179
180 /* Reset keybag */
181 SecItemServerResetKeychainKeybag();
182
183 // Reset server accessgroups.
184 SecAccessGroupsSetCurrent(old_ag);
185 CFReleaseSafe(test_ag);
186
187 return 0;
188 }
189
190 #else
191
192 int secd_02_upgrade_while_locked(int argc, char *const *argv)
193 {
194 plan_tests(1);
195
196 todo("Not yet working in simulator");
197
198 TODO: {
199 ok(false);
200 }
201 /* not implemented in simulator (no keybag) */
202 /* Not implemented in OSX (no upgrade scenario) */
203 return 0;
204 }
205 #endif