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