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