]> git.saurik.com Git - apple/security.git/blob - keychain/securityd/Regressions/secd-62-account-backup.m
Security-59306.41.2.tar.gz
[apple/security.git] / keychain / 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 "keychain/SecureObjectSync/SOSAccount.h"
34 #include <Security/SecureObjectSync/SOSCloudCircle.h>
35 #include "keychain/SecureObjectSync/SOSInternal.h"
36 #include "keychain/SecureObjectSync/SOSUserKeygen.h"
37 #include "keychain/SecureObjectSync/SOSTransport.h"
38 #include <Security/SecureObjectSync/SOSViews.h>
39 #include <Security/SecureObjectSync/SOSTypes.h>
40
41 #import "keychain/SecureObjectSync/SOSAccountTrustClassic.h"
42 #import "keychain/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 "keychain/securityd/SOSCloudCircleServer.h"
55
56 #if !TARGET_OS_SIMULATOR
57 #include "SOSAccountTesting.h"
58 #endif
59 #include "SecdTestKeychainUtilities.h"
60
61 #if !TARGET_OS_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 #endif
72
73 static void tests(void)
74 {
75 #if !TARGET_OS_SIMULATOR
76
77 __block CFErrorRef error = NULL;
78 CFDataRef cfpassword = CFDataCreate(NULL, (uint8_t *) "FooFooFoo", 10);
79 CFStringRef cfaccount = CFSTR("test@test.org");
80
81
82 CFMutableDictionaryRef changes = CFDictionaryCreateMutableForCFTypes(kCFAllocatorDefault);
83 SOSAccount* alice_account = CreateAccountForLocalChanges(CFSTR("Alice"), CFSTR("TestSource"));
84 SOSAccount* 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([alice_account.trust updateView:alice_account name:kTestView1 code:kSOSCCViewEnable err:&error], kSOSCCViewMember, "Enable view (%@)", error);
126 CFReleaseNull(error);
127
128 ok([alice_account.trust checkForRings:&error], "Alice_account is good");
129 CFReleaseNull(error);
130
131 is([bob_account.trust updateView:bob_account name:kTestView1 code:kSOSCCViewEnable err:&error], kSOSCCViewMember, "Enable view (%@)", error);
132 CFReleaseNull(error);
133
134 ok([bob_account.trust checkForRings:&error], "Bob_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([alice_account.trust checkForRings:&error], "Alice_account is good");
141 CFReleaseNull(error);
142
143 ok(SOSAccountSetBackupPublicKey_wTxn(bob_account, bob_backup_key, &error), "Set backup public key, bob (%@)", error);
144 CFReleaseNull(error);
145
146 ok([bob_account.trust checkForRings:&error], "Alice_account is good");
147 CFReleaseNull(error);
148
149 ok(SOSAccountIsMyPeerInBackupAndCurrentInView(alice_account, kTestView1), "Is alice's key in backup before sync?");
150
151 ok([alice_account.trust checkForRings:&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([bob_account.trust checkForRings:&error], "Alice_account is good");
157 CFReleaseNull(error);
158
159 is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, NULL), 4, "updates");
160
161
162 ok([alice_account.trust checkForRings:&error], "Alice_account is good");
163 CFReleaseNull(error);
164
165 ok(SOSAccountIsMyPeerInBackupAndCurrentInView(alice_account, kTestView1), "Is alice 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([bob_account.trust leaveCircle:bob_account err:&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 SOSAccountTrustClassic *bobTrust = bob_account.trust;
191 ok(!SOSAccountIsPeerInBackupAndCurrentInView(alice_account, bobTrust.peerInfo, kTestView1), "Bob is still in the backup!");
192
193 //Bob gets back into the circle
194 ok(SOSAccountJoinCircles_wTxn(bob_account, &error));
195 is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, NULL), 2, "updates");
196 {
197 CFArrayRef applicants = SOSAccountCopyApplicants(alice_account, &error);
198
199 ok(applicants && CFArrayGetCount(applicants) == 1, "See one applicant %@ (%@)", applicants, error);
200 ok(SOSAccountAcceptApplicants(alice_account, applicants, &error), "Alice accepts (%@)", error);
201 CFReleaseNull(error);
202 CFReleaseNull(applicants);
203 }
204
205 is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, NULL), 3, "updates");
206
207
208 //enables view
209 is([bob_account.trust updateView:bob_account name:kTestView1 code:kSOSCCViewEnable err:&error], kSOSCCViewMember, "Enable view (%@)", error);
210 CFReleaseNull(error);
211
212 ok(!SOSAccountIsMyPeerInBackupAndCurrentInView(bob_account, kTestView1), "Bob isn't in the backup yet");
213
214 ok(!SOSAccountIsLastBackupPeer(alice_account, &error), "Alice is the not the last backup peer - Bob still registers as one");
215 CFReleaseNull(error);
216
217 ok(SOSAccountSetBackupPublicKey_wTxn(bob_account, bob_backup_key, &error), "Set backup public key, bob (%@)", error);
218
219 is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, NULL), 3, "updates");
220
221 ok(!SOSAccountIsLastBackupPeer(alice_account, &error), "Alice is not last backup peer");
222 CFReleaseNull(error);
223
224 //
225 //removing backup key for bob account
226 //
227
228 ok(SOSAccountRemoveBackupPublickey_wTxn(bob_account, &error), "Removing Bob's backup key (%@)", error);
229 is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, NULL), 2, "updates");
230
231 ok(!SOSAccountIsMyPeerInBackupAndCurrentInView(bob_account, kTestView1), "Bob's backup key is in the backup - should not be so!");
232 ok(!SOSAccountIsPeerInBackupAndCurrentInView(alice_account, bobTrust.peerInfo, kTestView1), "Bob is up to date in the backup!");
233
234 //
235 // Setting new backup public key for Bob
236 //
237
238 ok(SOSAccountSetBackupPublicKey_wTxn(bob_account, bob_backup_key, &error), "Set backup public key, bob (%@)", error);
239 CFReleaseNull(error);
240
241 is([bob_account.trust updateView:bob_account name:kTestView1 code:kSOSCCViewEnable err:&error], kSOSCCViewMember, "Enable view (%@)", error);
242 ok(SOSAccountNewBKSBForView(bob_account, kTestView1, &error), "Setting new backup public key for bob account failed: (%@)", error);
243
244 //bob is in his own backup
245 ok(SOSAccountIsMyPeerInBackupAndCurrentInView(bob_account, kTestView1), "Bob's backup key is not in the backup");
246 //alice does not have bob in her backup
247 ok(!SOSAccountIsPeerInBackupAndCurrentInView(alice_account, bobTrust.peerInfo, kTestView1), "Bob is up to date in the backup - should not be so!");
248
249 is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, NULL), 5, "updates");
250
251 ok(SOSAccountIsMyPeerInBackupAndCurrentInView(bob_account, kTestView1), "Bob's backup key should be in the backup");
252 ok(SOSAccountIsMyPeerInBackupAndCurrentInView(alice_account, kTestView1), "Alice is in the backup");
253 ok(SOSAccountHasPublicKey(alice_account, &error), "Has Public Key" );
254 ok([alice_account.trust resetAccountToEmpty:alice_account transport:alice_account.circle_transport err:&error], "Reset circle to empty");
255 CFReleaseNull(error);
256 is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, NULL), 2, "updates");
257 ok(SOSAccountIsBackupRingEmpty(bob_account, kTestView1), "Bob should not be in the backup");
258 ok(SOSAccountIsBackupRingEmpty(alice_account, kTestView1), "Alice should not be in the backup");
259
260
261 CFReleaseNull(cfpassword);
262 alice_account = nil;
263 bob_account = nil;
264 SOSTestCleanup();
265 #endif
266
267 }
268
269 int secd_62_account_backup(int argc, char *const *argv)
270 {
271 #if !TARGET_OS_SIMULATOR
272 plan_tests(95);
273 #else
274 plan_tests(1);
275 #endif
276
277 secd_test_setup_temp_keychain(__FUNCTION__, NULL);
278 secd_test_setup_testviews(); // for running this test solo
279
280 tests();
281
282 return 0;
283 }