]> git.saurik.com Git - apple/security.git/blob - keychain/securityd/Regressions/secd-55-account-circle.m
Security-59754.60.13.tar.gz
[apple/security.git] / keychain / securityd / Regressions / secd-55-account-circle.m
1 /*
2 * Copyright (c) 2012-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
27 #include <Security/SecBase.h>
28 #include <Security/SecItem.h>
29
30 #include <CoreFoundation/CFDictionary.h>
31
32 #include "keychain/SecureObjectSync/SOSAccount.h"
33 #include <Security/SecureObjectSync/SOSCloudCircle.h>
34 #include "keychain/SecureObjectSync/SOSInternal.h"
35 #include "keychain/SecureObjectSync/SOSUserKeygen.h"
36 #include "keychain/SecureObjectSync/SOSTransport.h"
37 #import "keychain/SecureObjectSync/SOSAccountTrustClassic+Expansion.h"
38 #import "keychain/SecureObjectSync/SOSAccountTrustClassic+Circle.h"
39
40 #include <stdlib.h>
41 #include <unistd.h>
42
43 #include "secd_regressions.h"
44 #include "SOSTestDataSource.h"
45
46 #include "SOSRegressionUtilities.h"
47 #include <utilities/SecCFWrappers.h>
48 #include <Security/SecKeyPriv.h>
49
50 #include "keychain/securityd/SOSCloudCircleServer.h"
51
52 #include "SOSAccountTesting.h"
53
54 #include "SecdTestKeychainUtilities.h"
55
56 #if SOS_ENABLED
57
58 static int kTestTestCount = 257;
59
60 static void tests(void)
61 {
62 CFErrorRef error = NULL;
63 CFDataRef cfpassword = CFDataCreate(NULL, (uint8_t *) "FooFooFoo", 10);
64 CFDataRef cfwrong_password = CFDataCreate(NULL, (uint8_t *) "NotFooFooFoo", 10);
65 CFStringRef cfaccount = CFSTR("test@test.org");
66
67 CFMutableDictionaryRef changes = CFDictionaryCreateMutableForCFTypes(kCFAllocatorDefault);
68 SOSAccount* alice_account = CreateAccountForLocalChanges(CFSTR("Alice"), CFSTR("TestSource"));
69 SOSAccount* bob_account = CreateAccountForLocalChanges(CFSTR("Bob"), CFSTR("TestSource"));
70 SOSAccount* carol_account = CreateAccountForLocalChanges(CFSTR("Carol"), CFSTR("TestSource"));
71
72 ok(SOSAccountAssertUserCredentialsAndUpdate(bob_account, cfaccount, cfpassword, &error), "Credential setting (%@)", error);
73
74 // Bob wins writing at this point, feed the changes back to alice.
75 is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, carol_account, NULL), 1, "updates");
76
77 ok(SOSAccountAssertUserCredentialsAndUpdate(alice_account, cfaccount, cfpassword, &error), "Credential setting (%@)", error);
78 CFReleaseNull(error);
79 ok(SOSAccountTryUserCredentials(alice_account, cfaccount, cfpassword, &error), "Credential trying (%@)", error);
80 CFReleaseNull(error);
81 ok(!SOSAccountTryUserCredentials(alice_account, cfaccount, cfwrong_password, &error), "Credential failing (%@)", error);
82 CFReleaseNull(cfwrong_password);
83 is(error ? CFErrorGetCode(error) : 0, kSOSErrorWrongPassword, "Expected SOSErrorWrongPassword");
84 CFReleaseNull(error);
85
86 ok(SOSAccountResetToOffering_wTxn(alice_account, &error), "Reset to offering (%@)", error);
87 CFReleaseNull(error);
88
89 is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, carol_account, NULL), 2, "updates");
90
91 ok(SOSAccountHasCompletedInitialSync(alice_account), "Alice thinks she's completed initial sync");
92
93 ok(SOSAccountJoinCircles_wTxn(bob_account, &error), "Bob Applies (%@)", error);
94 CFReleaseNull(error);
95
96 is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, carol_account, NULL), 2, "updates");
97
98 {
99 CFArrayRef applicants = SOSAccountCopyApplicants(alice_account, &error);
100
101 ok(applicants && CFArrayGetCount(applicants) == 1, "See one applicant %@ (%@)", applicants, error);
102 ok(SOSAccountAcceptApplicants(alice_account, applicants, &error), "Alice accepts (%@)", error);
103 CFReleaseNull(error);
104 CFReleaseNull(applicants);
105 }
106
107 is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, carol_account, NULL), 3, "updates");
108
109 accounts_agree("bob&alice pair", bob_account, alice_account);
110
111 CFArrayRef peers = SOSAccountCopyPeers(alice_account, &error);
112 ok(peers && CFArrayGetCount(peers) == 2, "See two peers %@ (%@)", peers, error);
113 CFReleaseNull(peers);
114
115 ok(!SOSAccountHasCompletedInitialSync(bob_account), "Bob thinks he hasn't completed initial sync");
116
117 CFDictionaryRef alice_new_gestalt = SOSCreatePeerGestaltFromName(CFSTR("Alice, but different"));
118
119 ok([alice_account.trust updateGestalt:alice_account newGestalt:alice_new_gestalt], "Update gestalt %@ (%@)", alice_account, error);
120 SOSAccountUpdateTestTransports(alice_account, alice_new_gestalt);
121 CFReleaseNull(alice_new_gestalt);
122
123 is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, carol_account, NULL), 2, "updates");
124
125 accounts_agree("Alice's name changed", bob_account, alice_account);
126
127 ok([alice_account.trust leaveCircle:alice_account err:&error], "Alice Leaves (%@)", error);
128 CFReleaseNull(error);
129
130 is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, carol_account, NULL), 2, "updates");
131
132 accounts_agree("Alice bails", bob_account, alice_account);
133
134 peers = SOSAccountCopyPeers(alice_account, &error);
135 ok(peers && CFArrayGetCount(peers) == 1, "See one peer %@ (%@)", peers, error);
136 CFReleaseNull(peers);
137
138 ok(SOSAccountJoinCircles_wTxn(alice_account, &error), "Alice re-applies (%@)", error);
139 CFReleaseNull(error);
140
141 is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, carol_account, NULL), 2, "updates");
142
143
144 {
145 CFArrayRef applicants = SOSAccountCopyApplicants(alice_account, &error);
146
147 ok(applicants && CFArrayGetCount(applicants) == 1, "See one applicant %@ (%@)", applicants, error);
148 ok(SOSAccountAcceptApplicants(bob_account, applicants, &error), "Bob accepts (%@)", error);
149 CFReleaseNull(error);
150 CFReleaseNull(applicants);
151 }
152
153 is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, carol_account, NULL), 3, "updates");
154
155 accounts_agree("Alice accepts' Bob", bob_account, alice_account);
156
157 ok([alice_account.trust leaveCircle:alice_account err:&error], "Alice Leaves (%@)", error);
158 CFReleaseNull(error);
159 ok(SOSAccountJoinCircles_wTxn(alice_account, &error), "Alice re-applies (%@)", error);
160 CFReleaseNull(error);
161
162 is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, carol_account, NULL), 2, "updates");
163
164 {
165 CFArrayRef applicants = SOSAccountCopyApplicants(alice_account, &error);
166
167 ok(applicants && CFArrayGetCount(applicants) == 1, "See one applicant %@ (%@)", applicants, error);
168 ok(SOSAccountAcceptApplicants(bob_account, applicants, &error), "Bob accepts (%@)", error);
169 CFReleaseNull(error);
170 CFReleaseNull(applicants);
171 }
172
173 is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, carol_account, NULL), 3, "updates");
174
175 accounts_agree("Bob accepts Alice", bob_account, alice_account);
176
177 // As of PR-13917727/PR-13906870 this no longer works (by "design"), in favor of making another more common
178 // failure (apply/OSX-psudo-reject/re-apply) work. Write races might be "fixed better" with affirmitave rejection.
179 #if 0
180
181 //
182 // Write race emulation.
183 //
184 //
185
186 ok([alice_account.trust leaveCircle:alice_account err:&error], "Alice Leaves (%@)", error);
187 CFReleaseNull(error);
188 FeedChangesTo(changes, bob_account); // Bob sees Alice leaving and rejoining
189 FeedChangesTo(changes, alice_account); // Alice sees bob concurring
190
191 accounts_agree("Alice leaves & returns", bob_account, alice_account);
192
193 ok(SOSAccountJoinCircles(alice_account, &error), "Alice re-applies (%@)", error);
194 CFReleaseNull(error);
195
196 FeedChangesTo(changes, bob_account); // Bob sees Alice Applying
197
198 {
199 CFArrayRef applicants = SOSAccountCopyApplicants(alice_account, &error);
200
201 ok(applicants && CFArrayGetCount(applicants) == 1, "See one applicant %@ (%@)", applicants, error);
202 ok(SOSAccountAcceptApplicants(bob_account, applicants, &error), "Bob accepts (%@)", error);
203 CFReleaseNull(error);
204 CFReleaseNull(applicants);
205 }
206
207 CFMutableDictionaryRef bobAcceptanceChanges = ExtractPendingChanges(changes);
208
209 // Alice re-applies without seeing that she was accepted.
210 ok([alice_account.trust leaveCircle:alice_account err:&error], "Alice Leaves again (%@)", error);
211 CFReleaseNull(error);
212 ok(SOSAccountJoinCircles(alice_account, &error), "Alice re-applies (%@)", error);
213 CFReleaseNull(error);
214
215 CFReleaseSafe(ExtractPendingChanges(changes)); // Alice loses the race to write her changes - bob never sees em.
216
217 FeedChangesTo(&bobAcceptanceChanges, alice_account); // Alice sees bob inviting her in the circle.
218
219 FeedChangesTo(changes, bob_account); // Bob sees Alice Concurring
220
221 // As of PR-13917727/PR-13906870
222 accounts_agree("Alice leave, applies back, loses a race and eventually gets in", bob_account, alice_account);
223 #endif
224
225 // Both in circle.
226
227 // Emulation of <rdar://problem/13919554> Innsbruck11A368 +Roots: Device A was removed when Device B joined.
228
229 // We want Alice to leave circle while an Applicant on a full concordance signed circle with old-Alice as an Alum and Bob a peer.
230 // ZZZ
231 ok([alice_account.trust leaveCircle:alice_account err:&error], "Alice leaves once more (%@)", error);
232 CFReleaseNull(error);
233
234 is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, carol_account, NULL), 2, "updates");
235 accounts_agree("Alice and Bob see Alice out of circle", bob_account, alice_account);
236
237 ok(SOSAccountJoinCircles_wTxn(alice_account, &error), "Alice re-applies (%@)", error);
238 CFReleaseNull(error);
239
240 ok([alice_account.trust leaveCircle:alice_account err:&error], "Alice leaves while applying (%@)", error);
241 FeedChangesTo(changes, bob_account); // Bob sees Alice become an Alum.
242
243 CFReleaseNull(error);
244
245 is([alice_account getCircleStatus:&error], kSOSCCNotInCircle, "Alice isn't applying any more");
246 accounts_agree("Alice leaves & some fancy concordance stuff happens", bob_account, alice_account);
247
248 ok(SOSAccountJoinCircles_wTxn(alice_account, &error), "Alice re-applies (%@)", error);
249 CFReleaseNull(error);
250
251 //FeedChangesTo(changes, bob_account); // Bob sees Alice reapply.
252 is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, NULL), 2, "updates");
253
254 {
255 CFArrayRef applicants = SOSAccountCopyApplicants(alice_account, &error);
256
257 ok(applicants && CFArrayGetCount(applicants) == 1, "See one applicant %@ (%@)", applicants, error);
258 ok(SOSAccountAcceptApplicants(bob_account, applicants, &error), "Bob accepts (%@)", error);
259 CFReleaseNull(error);
260 CFReleaseNull(applicants);
261 }
262 is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, carol_account, NULL), 3, "updates");
263
264 accounts_agree("Alice comes back", bob_account, alice_account);
265
266 // Emulation of <rdar://problem/13889901>
267 is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, carol_account, NULL), 1, "updates");
268
269 ok(SOSAccountAssertUserCredentialsAndUpdate(carol_account, cfaccount, cfpassword, &error), "Credential setting (%@)", error);
270 CFReleaseNull(cfpassword);
271 ok(SOSAccountJoinCircles_wTxn(carol_account, &error), "Carol Applies (%@)", error);
272 CFReleaseNull(error);
273
274 CFMutableDictionaryRef dropped_changes = CFDictionaryCreateMutableForCFTypes(kCFAllocatorDefault);
275 FillChanges(dropped_changes, carol_account);
276 CFReleaseNull(dropped_changes);
277
278 ok(SOSAccountResetToOffering_wTxn(carol_account, &error), "Reset to offering (%@)", error);
279 CFReleaseNull(error);
280
281 is(ProcessChangesUntilNoChange(changes, bob_account, carol_account, NULL), 2, "updates");
282 accounts_agree("13889901", carol_account, bob_account);
283 is(SOSAccountGetLastDepartureReason(bob_account, &error), kSOSMembershipRevoked, "Bob affirms he hasn't left.");
284
285 ok(SOSAccountJoinCircles_wTxn(bob_account, &error), "Bob ReApplies (%@)", error);
286 is(ProcessChangesUntilNoChange(changes, bob_account, carol_account, NULL), 2, "updates");
287 {
288 CFArrayRef applicants = SOSAccountCopyApplicants(carol_account, &error);
289
290 ok(applicants && CFArrayGetCount(applicants) == 1, "See one applicant %@ (%@)", applicants, error);
291 ok(SOSAccountAcceptApplicants(carol_account, applicants, &error), "Carol accepts (%@)", error);
292 CFReleaseNull(error);
293 CFReleaseNull(applicants);
294 }
295 is(ProcessChangesUntilNoChange(changes, bob_account, carol_account, NULL), 3, "updates");
296 accounts_agree("rdar://problem/13889901-II", bob_account, carol_account);
297
298 // Alice has been out of the loop, bring her back up to speed before changing things (to avoid gen count race)
299 is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, carol_account, NULL), 1, "Reset propogation");
300
301 // Test multiple removal, including our own departure via that API
302 ok(SOSAccountResetToOffering_wTxn(alice_account, NULL), "Reset to offering");
303
304 is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, carol_account, NULL), 2, "Reset propogation");
305
306 ok(SOSAccountJoinCircles_wTxn(bob_account, NULL), "bob joins again");
307
308 is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, carol_account, NULL), 2, "Bob request");
309
310 {
311 CFArrayRef applicants = SOSAccountCopyApplicants(alice_account, &error);
312
313 ok(applicants && CFArrayGetCount(applicants) == 1, "See one applicant %@ (%@)", applicants, error);
314 ok(SOSAccountAcceptApplicants(alice_account, applicants, &error), "Alice accepts (%@)", error);
315 CFReleaseNull(error);
316 CFReleaseNull(applicants);
317 }
318
319 is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, carol_account, NULL), 3, "carol request");
320
321 ok(SOSAccountJoinCircles_wTxn(carol_account, NULL), "carol joins again");
322
323 is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, carol_account, NULL), 2, "carol request");
324
325 CFArrayRef peers_to_remove_array = CFArrayCreateForCFTypes(kCFAllocatorDefault,
326 bob_account.peerInfo,
327 carol_account.peerInfo,
328 NULL);
329
330 ok(SOSAccountRemovePeersFromCircle(bob_account, peers_to_remove_array, NULL));
331
332 is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, carol_account, NULL), 4, "Remove peers");
333
334 ok([alice_account isInCircle:&error], "Alice still here");
335 ok(![bob_account isInCircle:&error], "Bob not in circle");
336 // Carol's not in circle, but reapplied, as she's persistent until positive rejection.
337 ok(![carol_account isInCircle:NULL], "carol not in circle");
338
339 CFReleaseNull(peers_to_remove_array);
340
341 CFReleaseNull(alice_new_gestalt);
342 alice_account = nil;
343 bob_account = nil;
344 carol_account = nil;
345
346 SOSTestCleanup();
347 }
348 #endif
349
350 int secd_55_account_circle(int argc, char *const *argv)
351 {
352 #if SOS_ENABLED
353 plan_tests(kTestTestCount);
354 secd_test_setup_temp_keychain(__FUNCTION__, NULL);
355 tests();
356 #else
357 plan_tests(0);
358 #endif
359 return 0;
360 }