]> git.saurik.com Git - apple/security.git/blob - OSX/sec/securityd/Regressions/secd-62-account-backup.c
Security-57740.1.18.tar.gz
[apple/security.git] / OSX / sec / securityd / Regressions / secd-62-account-backup.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 #include <Security/SecureObjectSync/SOSViews.h>
38
39 #include <stdlib.h>
40 #include <unistd.h>
41
42 #include "secd_regressions.h"
43 #include "SOSTestDataSource.h"
44
45 #include "SOSRegressionUtilities.h"
46 #include <utilities/SecCFWrappers.h>
47 #include <Security/SecKeyPriv.h>
48
49 #include <securityd/SOSCloudCircleServer.h>
50
51 #if !TARGET_IPHONE_SIMULATOR
52 #include "SOSAccountTesting.h"
53 #endif
54 #include "SecdTestKeychainUtilities.h"
55
56 #if !TARGET_IPHONE_SIMULATOR
57
58 static CFDataRef CopyBackupKeyForString(CFStringRef string, CFErrorRef *error)
59 {
60 __block CFDataRef result = NULL;
61 CFStringPerformWithUTF8CFData(string, ^(CFDataRef stringAsData) {
62 result = SOSCopyDeviceBackupPublicKey(stringAsData, error);
63 });
64 return result;
65 }
66
67 static int kTestTestCount = 133;
68 #else
69 static int kTestTestCount = 1;
70 #endif
71
72 static void tests(void)
73 {
74 #if !TARGET_IPHONE_SIMULATOR
75
76 __block CFErrorRef error = NULL;
77 CFDataRef cfpassword = CFDataCreate(NULL, (uint8_t *) "FooFooFoo", 10);
78 CFStringRef cfaccount = CFSTR("test@test.org");
79
80 secd_test_setup_testviews(); // for running this test solo
81
82 CFMutableDictionaryRef changes = CFDictionaryCreateMutableForCFTypes(kCFAllocatorDefault);
83 SOSAccountRef alice_account = CreateAccountForLocalChanges(CFSTR("Alice"), CFSTR("TestSource"));
84 SOSAccountRef bob_account = CreateAccountForLocalChanges(CFSTR("Bob"), CFSTR("TestSource"));
85
86 CFDataRef alice_backup_key = CopyBackupKeyForString(CFSTR("Alice Backup Entropy"), &error);
87 CFDataRef bob_backup_key = CopyBackupKeyForString(CFSTR("Bob Backup Entropy"), &error);
88
89 // Bob wins writing at this point, feed the changes back to alice.
90 is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, NULL), 1, "updates");
91
92 ok(SOSAccountAssertUserCredentialsAndUpdate(alice_account, cfaccount, cfpassword, &error), "Credential setting (%@)", error);
93 CFReleaseNull(error);
94
95 ok(SOSAccountResetToOffering_wTxn(alice_account, &error), "Reset to offering (%@)", error);
96 CFReleaseNull(error);
97
98 is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, NULL), 2, "updates");
99
100 ok(SOSAccountAssertUserCredentialsAndUpdate(bob_account, cfaccount, cfpassword, &error), "Credential setting (%@)", error);
101 CFReleaseNull(error);
102
103 is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, NULL), 1, "updates");
104
105 ok(SOSAccountJoinCircles_wTxn(bob_account, &error), "Bob Applies (%@)", error);
106 CFReleaseNull(error);
107
108 is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, NULL), 2, "updates");
109
110 {
111 CFArrayRef applicants = SOSAccountCopyApplicants(alice_account, &error);
112
113 ok(applicants && CFArrayGetCount(applicants) == 1, "See one applicant %@ (%@)", applicants, error);
114 ok(SOSAccountAcceptApplicants(alice_account, applicants, &error), "Alice accepts (%@)", error);
115 CFReleaseNull(error);
116 CFReleaseNull(applicants);
117 }
118
119 is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, NULL), 3, "updates");
120
121 CFArrayRef peers = SOSAccountCopyPeers(alice_account, &error);
122 ok(peers && CFArrayGetCount(peers) == 2, "See two peers %@ (%@)", peers, error);
123 CFReleaseNull(peers);
124
125 is(SOSAccountUpdateView(alice_account, kTestView1, kSOSCCViewEnable, &error), kSOSCCViewMember, "Enable view (%@)", error);
126 CFReleaseNull(error);
127
128 ok(SOSAccountCheckForRings(alice_account, &error), "Alice_account is good");
129 CFReleaseNull(error);
130
131 is(SOSAccountUpdateView(bob_account, kTestView1, kSOSCCViewEnable, &error), kSOSCCViewMember, "Enable view (%@)", error);
132 CFReleaseNull(error);
133
134 ok(SOSAccountCheckForRings(bob_account, &error), "Alice_account is good");
135 CFReleaseNull(error);
136
137 ok(SOSAccountSetBackupPublicKey_wTxn(alice_account, alice_backup_key, &error), "Set backup public key, alice (%@)", error);
138 CFReleaseNull(error);
139
140 ok(SOSAccountCheckForRings(alice_account, &error), "Alice_account is good");
141 CFReleaseNull(error);
142
143 ok(SOSAccountSetBackupPublicKey_wTxn(bob_account, bob_backup_key, &error), "Set backup public key, alice (%@)", error);
144 CFReleaseNull(error);
145
146 ok(SOSAccountCheckForRings(bob_account, &error), "Alice_account is good");
147 CFReleaseNull(error);
148
149 ok(SOSAccountIsMyPeerInBackupAndCurrentInView(alice_account, kTestView1), "Is alice is in backup before sync?");
150
151 ok(SOSAccountCheckForRings(alice_account, &error), "Alice_account is good");
152 CFReleaseNull(error);
153
154 ok(SOSAccountIsMyPeerInBackupAndCurrentInView(bob_account, kTestView1), "Is bob in the backup after sync? - 1");
155
156 ok(SOSAccountCheckForRings(bob_account, &error), "Alice_account is good");
157 CFReleaseNull(error);
158
159 is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, NULL), 4, "updates");
160
161
162 ok(SOSAccountCheckForRings(alice_account, &error), "Alice_account is good");
163 CFReleaseNull(error);
164
165 ok(SOSAccountIsMyPeerInBackupAndCurrentInView(alice_account, kTestView1), "Is alice is in backup after sync?");
166
167 ok(SOSAccountIsMyPeerInBackupAndCurrentInView(bob_account, kTestView1), "IS bob in the backup after sync");
168
169 ok(!SOSAccountIsLastBackupPeer(alice_account, &error), "Alice is not last backup peer");
170 CFReleaseNull(error);
171
172 //
173 //Bob leaves the circle
174 //
175 ok(SOSAccountLeaveCircle(bob_account, &error), "Bob Leaves (%@)", error);
176 CFReleaseNull(error);
177
178 //Alice should kick Bob out of the backup!
179 is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, NULL), 2, "updates");
180
181 ok(SOSAccountIsMyPeerInBackupAndCurrentInView(alice_account, kTestView1), "Bob left the circle, Alice is not in the backup");
182
183 ok(SOSAccountIsLastBackupPeer(alice_account, &error), "Alice is last backup peer");
184 CFReleaseNull(error);
185 ok(!SOSAccountIsLastBackupPeer(bob_account, &error), "Bob is not last backup peer");
186 CFReleaseNull(error);
187
188 ok(testAccountPersistence(alice_account), "Test Account->DER->Account Equivalence");
189
190 ok(!SOSAccountIsPeerInBackupAndCurrentInView(alice_account, SOSFullPeerInfoGetPeerInfo(bob_account->my_identity), kTestView1), "Bob is still in the backup!");
191
192 //Bob gets back into the circle
193 ok(SOSAccountJoinCircles_wTxn(bob_account, &error));
194 is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, NULL), 2, "updates");
195 {
196 CFArrayRef applicants = SOSAccountCopyApplicants(alice_account, &error);
197
198 ok(applicants && CFArrayGetCount(applicants) == 1, "See one applicant %@ (%@)", applicants, error);
199 ok(SOSAccountAcceptApplicants(alice_account, applicants, &error), "Alice accepts (%@)", error);
200 CFReleaseNull(error);
201 CFReleaseNull(applicants);
202 }
203
204 is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, NULL), 3, "updates");
205
206
207 //enables view
208 is(SOSAccountUpdateView(bob_account, kTestView1, kSOSCCViewEnable, &error), kSOSCCViewMember, "Enable view (%@)", error);
209 CFReleaseNull(error);
210
211 ok(!SOSAccountIsMyPeerInBackupAndCurrentInView(bob_account, kTestView1), "Bob isn't in the backup yet");
212
213 ok(!SOSAccountIsLastBackupPeer(alice_account, &error), "Alice is not last backup peer - Bob still registers as one");
214 CFReleaseNull(error);
215
216 ok(SOSAccountSetBackupPublicKey_wTxn(bob_account, bob_backup_key, &error), "Set backup public key, alice (%@)", error);
217
218 is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, NULL), 1, "updates");
219
220 ok(!SOSAccountIsLastBackupPeer(alice_account, &error), "Alice is not last backup peer");
221 CFReleaseNull(error);
222
223 //
224 //removing backup key for bob account
225 //
226
227 ok(SOSAccountRemoveBackupPublickey_wTxn(bob_account, &error), "Removing Bob's backup key (%@)", error);
228 is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, NULL), 3, "updates");
229
230 ok(!SOSAccountIsMyPeerInBackupAndCurrentInView(bob_account, kTestView1), "Bob's backup key is in the backup - should not be so!");
231 ok(!SOSAccountIsPeerInBackupAndCurrentInView(alice_account, SOSFullPeerInfoGetPeerInfo(bob_account->my_identity), kTestView1), "Bob is up to date in the backup!");
232
233 //
234 // Setting new backup public key for Bob
235 //
236
237 ok(SOSAccountSetBackupPublicKey_wTxn(bob_account, bob_backup_key, &error), "Set backup public key, alice (%@)", error);
238 CFReleaseNull(error);
239
240 is(SOSAccountUpdateView(bob_account, kTestView1, kSOSCCViewEnable, &error), kSOSCCViewMember, "Enable view (%@)", error);
241 ok(SOSAccountNewBKSBForView(bob_account, kTestView1, &error), "Setting new backup public key for bob account failed: (%@)", error);
242
243 //bob is in his own backup
244 ok(SOSAccountIsMyPeerInBackupAndCurrentInView(bob_account, kTestView1), "Bob's backup key is not in the backup");
245 //alice does not have bob in her backup
246 ok(!SOSAccountIsPeerInBackupAndCurrentInView(alice_account, SOSFullPeerInfoGetPeerInfo(bob_account->my_identity), kTestView1), "Bob is up to date in the backup - should not be so!");
247
248 is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, NULL), 5, "updates");
249
250 ok(SOSAccountIsMyPeerInBackupAndCurrentInView(bob_account, kTestView1), "Bob's backup key should be in the backup");
251 ok(SOSAccountIsMyPeerInBackupAndCurrentInView(alice_account, kTestView1), "Alice is in the backup");
252
253 ok(SOSAccountResetToEmpty(alice_account, &error), "Reset circle to empty");
254 CFReleaseNull(error);
255 is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, NULL), 2, "updates");
256 ok(SOSAccountIsBackupRingEmpty(bob_account, kTestView1), "Bob should not be in the backup");
257 ok(SOSAccountIsBackupRingEmpty(alice_account, kTestView1), "Alice should not be in the backup");
258
259 CFReleaseNull(bob_account);
260 CFReleaseNull(alice_account);
261 CFReleaseNull(cfpassword);
262
263 SOSUnregisterAllTransportMessages();
264 SOSUnregisterAllTransportCircles();
265 SOSUnregisterAllTransportKeyParameters();
266 CFArrayRemoveAllValues(key_transports);
267 CFArrayRemoveAllValues(circle_transports);
268 CFArrayRemoveAllValues(message_transports);
269
270 CFReleaseNull(kTestView1);
271 CFReleaseNull(kTestView2);
272 #endif
273
274 }
275
276 int secd_62_account_backup(int argc, char *const *argv)
277 {
278 plan_tests(kTestTestCount);
279
280 secd_test_setup_temp_keychain(__FUNCTION__, NULL);
281
282 tests();
283
284 return 0;
285 }