]> git.saurik.com Git - apple/security.git/blob - OSX/sec/securityd/Regressions/secd-62-account-backup.m
Security-58286.200.222.tar.gz
[apple/security.git] / OSX / sec / securityd / Regressions / secd-62-account-backup.m
1
2 /*
3 * Copyright (c) 2012-2014 Apple Inc. All Rights Reserved.
4 *
5 * @APPLE_LICENSE_HEADER_START@
6 *
7 * This file contains Original Code and/or Modifications of Original Code
8 * as defined in and that are subject to the Apple Public Source License
9 * Version 2.0 (the 'License'). You may not use this file except in
10 * compliance with the License. Please obtain a copy of the License at
11 * http://www.opensource.apple.com/apsl/ and read it before using this
12 * file.
13 *
14 * The Original Code and all software distributed under the License are
15 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
16 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
17 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
19 * Please see the License for the specific language governing rights and
20 * limitations under the License.
21 *
22 * @APPLE_LICENSE_HEADER_END@
23 */
24
25
26
27
28 #include <Security/SecBase.h>
29 #include <Security/SecItem.h>
30
31 #include <CoreFoundation/CFDictionary.h>
32
33 #include <Security/SecureObjectSync/SOSAccount.h>
34 #include <Security/SecureObjectSync/SOSCloudCircle.h>
35 #include <Security/SecureObjectSync/SOSInternal.h>
36 #include <Security/SecureObjectSync/SOSUserKeygen.h>
37 #include <Security/SecureObjectSync/SOSTransport.h>
38 #include <Security/SecureObjectSync/SOSViews.h>
39 #include <Security/SecureObjectSync/SOSTypes.h>
40
41 #import "Security/SecureObjectSync/SOSAccountTrustClassic.h"
42 #import "Security/SecureObjectSync/SOSAccountTrustClassic+Expansion.h"
43
44 #include <stdlib.h>
45 #include <unistd.h>
46
47 #include "secd_regressions.h"
48 #include "SOSTestDataSource.h"
49
50 #include "SOSRegressionUtilities.h"
51 #include <utilities/SecCFWrappers.h>
52 #include <Security/SecKeyPriv.h>
53
54 #include <securityd/SOSCloudCircleServer.h>
55
56 #if !TARGET_IPHONE_SIMULATOR
57 #include "SOSAccountTesting.h"
58 #endif
59 #include "SecdTestKeychainUtilities.h"
60
61 #if !TARGET_IPHONE_SIMULATOR
62
63 static CFDataRef CopyBackupKeyForString(CFStringRef string, CFErrorRef *error)
64 {
65 __block CFDataRef result = NULL;
66 CFStringPerformWithUTF8CFData(string, ^(CFDataRef stringAsData) {
67 result = SOSCopyDeviceBackupPublicKey(stringAsData, error);
68 });
69 return result;
70 }
71
72 static int kTestTestCount = 129;
73 #else
74 static int kTestTestCount = 1;
75 #endif
76
77 static void tests(void)
78 {
79 #if !TARGET_IPHONE_SIMULATOR
80
81 __block CFErrorRef error = NULL;
82 CFDataRef cfpassword = CFDataCreate(NULL, (uint8_t *) "FooFooFoo", 10);
83 CFStringRef cfaccount = CFSTR("test@test.org");
84
85 secd_test_setup_testviews(); // for running this test solo
86
87 CFMutableDictionaryRef changes = CFDictionaryCreateMutableForCFTypes(kCFAllocatorDefault);
88 SOSAccount* alice_account = CreateAccountForLocalChanges(CFSTR("Alice"), CFSTR("TestSource"));
89 SOSAccount* bob_account = CreateAccountForLocalChanges(CFSTR("Bob"), CFSTR("TestSource"));
90
91 CFDataRef alice_backup_key = CopyBackupKeyForString(CFSTR("Alice Backup Entropy"), &error);
92 CFDataRef bob_backup_key = CopyBackupKeyForString(CFSTR("Bob Backup Entropy"), &error);
93
94 // Bob wins writing at this point, feed the changes back to alice.
95 is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, NULL), 1, "updates");
96
97 ok(SOSAccountAssertUserCredentialsAndUpdate(alice_account, cfaccount, cfpassword, &error), "Credential setting (%@)", error);
98 CFReleaseNull(error);
99
100 ok(SOSAccountResetToOffering_wTxn(alice_account, &error), "Reset to offering (%@)", error);
101 CFReleaseNull(error);
102
103 is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, NULL), 2, "updates");
104
105 ok(SOSAccountAssertUserCredentialsAndUpdate(bob_account, cfaccount, cfpassword, &error), "Credential setting (%@)", error);
106 CFReleaseNull(error);
107
108 is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, NULL), 1, "updates");
109
110 ok(SOSAccountJoinCircles_wTxn(bob_account, &error), "Bob Applies (%@)", error);
111 CFReleaseNull(error);
112
113 is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, NULL), 2, "updates");
114
115 {
116 CFArrayRef applicants = SOSAccountCopyApplicants(alice_account, &error);
117
118 ok(applicants && CFArrayGetCount(applicants) == 1, "See one applicant %@ (%@)", applicants, error);
119 ok(SOSAccountAcceptApplicants(alice_account, applicants, &error), "Alice accepts (%@)", error);
120 CFReleaseNull(error);
121 CFReleaseNull(applicants);
122 }
123
124 is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, NULL), 3, "updates");
125
126 CFArrayRef peers = SOSAccountCopyPeers(alice_account, &error);
127 ok(peers && CFArrayGetCount(peers) == 2, "See two peers %@ (%@)", peers, error);
128 CFReleaseNull(peers);
129
130 is([alice_account.trust updateView:alice_account name:kTestView1 code:kSOSCCViewEnable err:&error], kSOSCCViewMember, "Enable view (%@)", error);
131 CFReleaseNull(error);
132
133 ok([alice_account.trust checkForRings:&error], "Alice_account is good");
134 CFReleaseNull(error);
135
136 is([bob_account.trust updateView:bob_account name:kTestView1 code:kSOSCCViewEnable err:&error], kSOSCCViewMember, "Enable view (%@)", error);
137 CFReleaseNull(error);
138
139 ok([bob_account.trust checkForRings:&error], "Bob_account is good");
140 CFReleaseNull(error);
141
142 ok(SOSAccountSetBackupPublicKey_wTxn(alice_account, alice_backup_key, &error), "Set backup public key, alice (%@)", error);
143 CFReleaseNull(error);
144
145 ok([alice_account.trust checkForRings:&error], "Alice_account is good");
146 CFReleaseNull(error);
147
148 ok(SOSAccountSetBackupPublicKey_wTxn(bob_account, bob_backup_key, &error), "Set backup public key, bob (%@)", error);
149 CFReleaseNull(error);
150
151 ok([bob_account.trust checkForRings:&error], "Alice_account is good");
152 CFReleaseNull(error);
153
154 ok(SOSAccountIsMyPeerInBackupAndCurrentInView(alice_account, kTestView1), "Is alice's key in backup before sync?");
155
156 ok([alice_account.trust checkForRings:&error], "Alice_account is good");
157 CFReleaseNull(error);
158
159 ok(SOSAccountIsMyPeerInBackupAndCurrentInView(bob_account, kTestView1), "Is bob in the backup after sync? - 1");
160
161 ok([bob_account.trust checkForRings:&error], "Alice_account is good");
162 CFReleaseNull(error);
163
164 is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, NULL), 4, "updates");
165
166
167 ok([alice_account.trust checkForRings:&error], "Alice_account is good");
168 CFReleaseNull(error);
169
170 ok(SOSAccountIsMyPeerInBackupAndCurrentInView(alice_account, kTestView1), "Is alice in backup after sync?");
171
172 ok(SOSAccountIsMyPeerInBackupAndCurrentInView(bob_account, kTestView1), "IS bob in the backup after sync");
173
174 ok(!SOSAccountIsLastBackupPeer(alice_account, &error), "Alice is not last backup peer");
175 CFReleaseNull(error);
176
177 //
178 //Bob leaves the circle
179 //
180 ok([bob_account.trust leaveCircle:bob_account err:&error], "Bob Leaves (%@)", error);
181 CFReleaseNull(error);
182
183 //Alice should kick Bob out of the backup!
184 is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, NULL), 2, "updates");
185
186 ok(SOSAccountIsMyPeerInBackupAndCurrentInView(alice_account, kTestView1), "Bob left the circle, Alice is not in the backup");
187
188 ok(SOSAccountIsLastBackupPeer(alice_account, &error), "Alice is last backup peer");
189 CFReleaseNull(error);
190 ok(!SOSAccountIsLastBackupPeer(bob_account, &error), "Bob is not last backup peer");
191 CFReleaseNull(error);
192
193 ok(testAccountPersistence(alice_account), "Test Account->DER->Account Equivalence");
194
195 SOSAccountTrustClassic *bobTrust = bob_account.trust;
196 ok(!SOSAccountIsPeerInBackupAndCurrentInView(alice_account, bobTrust.peerInfo, kTestView1), "Bob is still in the backup!");
197
198 //Bob gets back into the circle
199 ok(SOSAccountJoinCircles_wTxn(bob_account, &error));
200 is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, NULL), 2, "updates");
201 {
202 CFArrayRef applicants = SOSAccountCopyApplicants(alice_account, &error);
203
204 ok(applicants && CFArrayGetCount(applicants) == 1, "See one applicant %@ (%@)", applicants, error);
205 ok(SOSAccountAcceptApplicants(alice_account, applicants, &error), "Alice accepts (%@)", error);
206 CFReleaseNull(error);
207 CFReleaseNull(applicants);
208 }
209
210 is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, NULL), 3, "updates");
211
212
213 //enables view
214 is([bob_account.trust updateView:bob_account name:kTestView1 code:kSOSCCViewEnable err:&error], kSOSCCViewMember, "Enable view (%@)", error);
215 CFReleaseNull(error);
216
217 ok(!SOSAccountIsMyPeerInBackupAndCurrentInView(bob_account, kTestView1), "Bob isn't in the backup yet");
218
219 ok(!SOSAccountIsLastBackupPeer(alice_account, &error), "Alice is not last backup peer - Bob still registers as one");
220 CFReleaseNull(error);
221
222 ok(SOSAccountSetBackupPublicKey_wTxn(bob_account, bob_backup_key, &error), "Set backup public key, alice (%@)", error);
223
224 is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, NULL), 1, "updates");
225
226 ok(!SOSAccountIsLastBackupPeer(alice_account, &error), "Alice is not last backup peer");
227 CFReleaseNull(error);
228
229 //
230 //removing backup key for bob account
231 //
232
233 ok(SOSAccountRemoveBackupPublickey_wTxn(bob_account, &error), "Removing Bob's backup key (%@)", error);
234 is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, NULL), 3, "updates");
235
236 ok(!SOSAccountIsMyPeerInBackupAndCurrentInView(bob_account, kTestView1), "Bob's backup key is in the backup - should not be so!");
237 ok(!SOSAccountIsPeerInBackupAndCurrentInView(alice_account, bobTrust.peerInfo, kTestView1), "Bob is up to date in the backup!");
238
239 //
240 // Setting new backup public key for Bob
241 //
242
243 ok(SOSAccountSetBackupPublicKey_wTxn(bob_account, bob_backup_key, &error), "Set backup public key, alice (%@)", error);
244 CFReleaseNull(error);
245
246 is([bob_account.trust updateView:bob_account name:kTestView1 code:kSOSCCViewEnable err:&error], kSOSCCViewMember, "Enable view (%@)", error);
247 ok(SOSAccountNewBKSBForView(bob_account, kTestView1, &error), "Setting new backup public key for bob account failed: (%@)", error);
248
249 //bob is in his own backup
250 ok(SOSAccountIsMyPeerInBackupAndCurrentInView(bob_account, kTestView1), "Bob's backup key is not in the backup");
251 //alice does not have bob in her backup
252 ok(!SOSAccountIsPeerInBackupAndCurrentInView(alice_account, bobTrust.peerInfo, kTestView1), "Bob is up to date in the backup - should not be so!");
253
254 is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, NULL), 5, "updates");
255
256 ok(SOSAccountIsMyPeerInBackupAndCurrentInView(bob_account, kTestView1), "Bob's backup key should be in the backup");
257 ok(SOSAccountIsMyPeerInBackupAndCurrentInView(alice_account, kTestView1), "Alice is in the backup");
258 ok(SOSAccountHasPublicKey(alice_account, &error), "Has Public Key" );
259 ok([alice_account.trust resetAccountToEmpty:alice_account transport:alice_account.circle_transport err:&error], "Reset circle to empty");
260 CFReleaseNull(error);
261 is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, NULL), 2, "updates");
262 ok(SOSAccountIsBackupRingEmpty(bob_account, kTestView1), "Bob should not be in the backup");
263 ok(SOSAccountIsBackupRingEmpty(alice_account, kTestView1), "Alice should not be in the backup");
264
265
266 CFReleaseNull(cfpassword);
267 alice_account = nil;
268 bob_account = nil;
269 SOSTestCleanup();
270 #endif
271
272 }
273
274 int secd_62_account_backup(int argc, char *const *argv)
275 {
276 plan_tests(kTestTestCount);
277
278 secd_test_setup_temp_keychain(__FUNCTION__, NULL);
279
280 tests();
281
282 return 0;
283 }