]> git.saurik.com Git - apple/security.git/blob - keychain/securityd/Regressions/secd-58-password-change.m
Security-59306.120.7.tar.gz
[apple/security.git] / keychain / securityd / Regressions / secd-58-password-change.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
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
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 "keychain/securityd/SOSCloudCircleServer.h"
49
50 #include "SOSAccountTesting.h"
51 #include "SecdTestKeychainUtilities.h"
52
53
54 static bool AssertCreds(SOSAccount* account,CFStringRef acct_name, CFDataRef password) {
55 CFErrorRef error = NULL;
56 bool retval;
57 ok((retval = SOSAccountAssertUserCredentialsAndUpdate(account, acct_name, password, &error)), "Credential setting (%@)", error);
58 CFReleaseNull(error);
59 return retval;
60 }
61
62 static inline bool SOSAccountEvaluateKeysAndCircle_wTxn(SOSAccount* acct, CFErrorRef* error)
63 {
64 __block bool result = false;
65 [acct performTransaction:^(SOSAccountTransaction * _Nonnull txn) {
66 result = SOSAccountEvaluateKeysAndCircle(txn, NULL);
67 }];
68 return result;
69 }
70
71 static bool ResetToOffering(SOSAccount* account) {
72 CFErrorRef error = NULL;
73 bool retval;
74 ok((retval = SOSAccountResetToOffering_wTxn(account, &error)), "Reset to offering (%@)", error);
75 CFReleaseNull(error);
76 return retval;
77 }
78
79 static bool JoinCircle(SOSAccount* account) {
80 CFErrorRef error = NULL;
81 bool retval;
82 ok((retval = SOSAccountJoinCircles_wTxn(account, &error)), "Join Circle (%@)", error);
83 CFReleaseNull(error);
84 return retval;
85 }
86
87 static bool AcceptApplicants(SOSAccount* account, CFIndex cnt) {
88 CFErrorRef error = NULL;
89 bool retval = false;
90 CFArrayRef applicants = SOSAccountCopyApplicants(account, &error);
91
92 ok((retval = (applicants && CFArrayGetCount(applicants) == cnt)), "See applicants %@ (%@)", applicants, error);
93 if(retval) ok((retval = SOSAccountAcceptApplicants(account, applicants, &error)), "Accept Applicants (%@)", error);
94 CFReleaseNull(applicants);
95 CFReleaseNull(error);
96 return retval;
97 }
98
99 static void tests(void)
100 {
101 CFDataRef cfpassword = CFDataCreate(NULL, (uint8_t *) "FooFooFoo", 10);
102 CFStringRef cfaccount = CFSTR("test@test.org");
103
104 CFMutableDictionaryRef changes = CFDictionaryCreateMutableForCFTypes(kCFAllocatorDefault);
105 SOSAccount* alice_account = CreateAccountForLocalChanges(CFSTR("Alice"), CFSTR("TestSource"));
106 SOSAccount* bob_account = CreateAccountForLocalChanges(CFSTR("Bob"), CFSTR("TestSource"));
107 SOSAccount* carol_account = CreateAccountForLocalChanges(CFSTR("Carol"), CFSTR("TestSource"));
108
109 /* Set Initial Credentials and Parameters for the Syncing Circles ---------------------------------------*/
110 ok(AssertCreds(bob_account, cfaccount, cfpassword), "Setting credentials for Bob");
111 // Bob wins writing at this point, feed the changes back to alice.
112
113 is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, carol_account, NULL), 1, "updates");
114
115 ok(AssertCreds(alice_account, cfaccount, cfpassword), "Setting credentials for Alice");
116 ok(AssertCreds(carol_account, cfaccount, cfpassword), "Setting credentials for Carol");
117 CFReleaseNull(cfpassword);
118
119 /* Make Alice First Peer -------------------------------------------------------------------------------*/
120 ok(ResetToOffering(alice_account), "Reset to offering - Alice as first peer");
121
122 is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, carol_account, NULL), 2, "updates");
123
124 /* Bob Joins -------------------------------------------------------------------------------------------*/
125 ok(JoinCircle(bob_account), "Bob Applies");
126
127 is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, carol_account, NULL), 2, "updates");
128
129 /* Alice Accepts -------------------------------------------------------------------------------------------*/
130 ok(AcceptApplicants(alice_account, 1), "Alice Accepts Bob's Application");
131
132 is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, carol_account, NULL), 3, "4 updates");
133 accounts_agree("bob&alice pair", bob_account, alice_account);
134
135 /* Carol Applies -------------------------------------------------------------------------------------------*/
136 ok(JoinCircle(carol_account), "Carol Applies");
137
138 is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, carol_account, NULL), 2, "updates");
139 is(countPeers(alice_account), 2, "See two peers");
140
141
142 /* Change Password ------------------------------------------------------------------------------------------*/
143 CFDataRef cfnewpassword = CFDataCreate(NULL, (uint8_t *) "ooFooFooF", 10);
144
145 ok(AssertCreds(bob_account , cfaccount, cfnewpassword), "Credential resetting for Bob");
146 is(countPeers(bob_account), 2, "There are two valid peers - iCloud and Bob");
147 is(countActivePeers(bob_account), 3, "There are three active peers - bob, alice, and iCloud");
148 is(countActiveValidPeers(bob_account), 2, "There is two active valid peer - Bob and iCloud");
149
150 is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, carol_account, NULL), 2, "updates");
151
152 ok(AssertCreds(alice_account , cfaccount, cfnewpassword), "Credential resetting for Alice");
153
154 is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, carol_account, NULL), 3, "updates");
155
156 is(countPeers(alice_account), 2, "There are two peers - bob and alice");
157 is(countActiveValidPeers(alice_account), 3, "There are three active valid peers - alice, bob, and icloud");
158
159 is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, carol_account, NULL), 1, "updates");
160
161 accounts_agree("bob&alice pair", bob_account, alice_account);
162 is(countPeers(alice_account), 2, "There are two peers - bob and alice");
163 is(countActiveValidPeers(alice_account), 3, "There are three active valid peers - alice, bob, and icloud");
164
165 ok(AssertCreds(carol_account , cfaccount, cfnewpassword), "Credential resetting for Carol");
166
167 is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, carol_account, NULL), 1, "updates");
168 is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, carol_account, NULL), 2, "updates");
169
170 accounts_agree("bob&alice pair", bob_account, alice_account);
171 accounts_agree_internal("bob&carol pair", bob_account, carol_account, false);
172
173 ok(AcceptApplicants(alice_account , 1), "Alice Accepts Carol's Application");
174
175 is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, carol_account, NULL), 4, "updates");
176
177 accounts_agree_internal("bob&alice pair", bob_account, alice_account, false);
178 accounts_agree_internal("bob&carol pair", bob_account, carol_account, false);
179 accounts_agree_internal("carol&alice pair", alice_account, carol_account, false);
180
181
182 /* Change Password 2 ----------------------------------------------------------------------------------------*/
183 CFReleaseNull(cfnewpassword);
184 cfnewpassword = CFDataCreate(NULL, (uint8_t *) "ffoffoffo", 10);
185
186 /* Bob */
187 ok(AssertCreds(bob_account , cfaccount, cfnewpassword), "Credential resetting for Bob");
188 is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, carol_account, NULL), 2, "updates");
189
190 is(countPeers(bob_account), 3, "There are three peers - Alice, Carol, Bob");
191 is(countActivePeers(bob_account), 4, "There are four active peers - bob, alice, carol and iCloud");
192 is(countActiveValidPeers(bob_account), 2, "There is two active valid peer - Bob and iCloud");
193
194
195 /* Alice */
196 ok(AssertCreds(alice_account , cfaccount, cfnewpassword), "Credential resetting for Alice");
197 is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, carol_account, NULL), 3, "updates");
198
199 is(countPeers(alice_account), 3, "There are three peers - Alice, Carol, Bob");
200 is(countActivePeers(alice_account), 4, "There are four active peers - bob, alice, carol and iCloud");
201 is(countActiveValidPeers(alice_account), 3, "There are three active valid peers - alice, bob, and icloud");
202
203
204 /* Carol */
205 ok(AssertCreds(carol_account , cfaccount, cfnewpassword), "Credential resetting for Carol");
206 is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, carol_account, NULL), 4, "updates");
207
208 is(countPeers(carol_account), 3, "There are three peers - Alice, Carol, Bob");
209 is(countActivePeers(carol_account), 4, "There are four active peers - bob, alice, carol and iCloud");
210 is(countActiveValidPeers(carol_account), 4, "There are three active valid peers - alice, bob, carol, and icloud");
211
212 accounts_agree_internal("bob&alice pair", bob_account, alice_account, false);
213
214 /* Change Password 3 - cause a parm lost update collision ----------------------------------------------------*/
215 CFReleaseNull(cfnewpassword);
216 cfnewpassword = CFDataCreate(NULL, (uint8_t *) "cococococ", 10);
217
218 ok(AssertCreds(bob_account , cfaccount, cfnewpassword), "Credential resetting for Bob");
219 ok(AssertCreds(alice_account , cfaccount, cfnewpassword), "Credential resetting for Alice");
220 is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, carol_account, NULL), 4, "updates");
221
222 is(countPeers(alice_account), 3, "There are three peers - Alice, Carol, Bob");
223 is(countActivePeers(alice_account), 4, "There are four active peers - bob, alice, carol and iCloud");
224 is(countActiveValidPeers(alice_account), 3, "There are three active valid peers - alice, bob, and icloud");
225
226 /* Change Password 4 - new peer changes the password and joins ----------------------------------------------------*/
227 CFReleaseNull(cfnewpassword);
228 cfnewpassword = CFDataCreate(NULL, (uint8_t *) "dodododod", 10);
229
230 SOSAccount* david_account = CreateAccountForLocalChanges(CFSTR("David"), CFSTR("TestSource"));
231 ok(AssertCreds(david_account , cfaccount, cfnewpassword), "Credential resetting for David");
232 is(ProcessChangesUntilNoChange(changes, david_account, NULL), 2, "updates");
233 is(countPeers(david_account), 3, "Still 3 peers");
234
235
236 ok(JoinCircle(david_account), "David Applies");
237 is(ProcessChangesUntilNoChange(changes, david_account, NULL), 2, "updates");
238 is(countPeers(david_account), 1, "Only David is in circle");
239
240
241 CFReleaseNull(cfnewpassword);
242 alice_account = nil;
243 bob_account = nil;
244 carol_account = nil;
245 david_account = nil;
246 SOSTestCleanup();
247 }
248
249 int secd_58_password_change(int argc, char *const *argv)
250 {
251 plan_tests(211);
252
253 secd_test_setup_temp_keychain(__FUNCTION__, NULL);
254
255 tests();
256
257 return 0;
258 }