]> git.saurik.com Git - apple/security.git/blob - keychain/securityd/Regressions/secd-62-account-backup.m
Security-59306.101.1.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), 5, "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 //
170 //Bob leaves the circle
171 //
172 ok([bob_account.trust leaveCircle:bob_account err:&error], "Bob Leaves (%@)", error);
173 CFReleaseNull(error);
174
175 //Alice should kick Bob out of the backup!
176 is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, NULL), 2, "updates");
177
178 ok(SOSAccountIsMyPeerInBackupAndCurrentInView(alice_account, kTestView1), "Bob left the circle, Alice is not in the backup");
179
180 ok(testAccountPersistence(alice_account), "Test Account->DER->Account Equivalence");
181
182 SOSAccountTrustClassic *bobTrust = bob_account.trust;
183 ok(!SOSAccountIsPeerInBackupAndCurrentInView(alice_account, bobTrust.peerInfo, kTestView1), "Bob is still in the backup!");
184
185 //Bob gets back into the circle
186 ok(SOSAccountJoinCircles_wTxn(bob_account, &error));
187 is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, NULL), 2, "updates");
188 {
189 CFArrayRef applicants = SOSAccountCopyApplicants(alice_account, &error);
190
191 ok(applicants && CFArrayGetCount(applicants) == 1, "See one applicant %@ (%@)", applicants, error);
192 ok(SOSAccountAcceptApplicants(alice_account, applicants, &error), "Alice accepts (%@)", error);
193 CFReleaseNull(error);
194 CFReleaseNull(applicants);
195 }
196
197 is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, NULL), 3, "updates");
198
199
200 //enables view
201 is([bob_account.trust updateView:bob_account name:kTestView1 code:kSOSCCViewEnable err:&error], kSOSCCViewMember, "Enable view (%@)", error);
202 CFReleaseNull(error);
203
204 ok(!SOSAccountIsMyPeerInBackupAndCurrentInView(bob_account, kTestView1), "Bob isn't in the backup yet");
205
206 ok(SOSAccountSetBackupPublicKey_wTxn(bob_account, bob_backup_key, &error), "Set backup public key, bob (%@)", error);
207
208 is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, NULL), 3, "updates");
209
210 //
211 //removing backup key for bob account
212 //
213
214 ok(SOSAccountRemoveBackupPublickey_wTxn(bob_account, &error), "Removing Bob's backup key (%@)", error);
215 is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, NULL), 2, "updates");
216
217 ok(!SOSAccountIsMyPeerInBackupAndCurrentInView(bob_account, kTestView1), "Bob's backup key is in the backup - should not be so!");
218 ok(!SOSAccountIsPeerInBackupAndCurrentInView(alice_account, bobTrust.peerInfo, kTestView1), "Bob is up to date in the backup!");
219
220 //
221 // Setting new backup public key for Bob
222 //
223
224 ok(SOSAccountSetBackupPublicKey_wTxn(bob_account, bob_backup_key, &error), "Set backup public key, bob (%@)", error);
225 CFReleaseNull(error);
226
227 is([bob_account.trust updateView:bob_account name:kTestView1 code:kSOSCCViewEnable err:&error], kSOSCCViewMember, "Enable view (%@)", error);
228 ok(SOSAccountNewBKSBForView(bob_account, kTestView1, &error), "Setting new backup public key for bob account failed: (%@)", error);
229
230 //bob is in his own backup
231 ok(SOSAccountIsMyPeerInBackupAndCurrentInView(bob_account, kTestView1), "Bob's backup key is not in the backup");
232 //alice does not have bob in her backup
233 ok(!SOSAccountIsPeerInBackupAndCurrentInView(alice_account, bobTrust.peerInfo, kTestView1), "Bob is up to date in the backup - should not be so!");
234
235 is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, NULL), 5, "updates");
236
237 ok(SOSAccountIsMyPeerInBackupAndCurrentInView(bob_account, kTestView1), "Bob's backup key should be in the backup");
238 ok(SOSAccountIsMyPeerInBackupAndCurrentInView(alice_account, kTestView1), "Alice is in the backup");
239 ok(SOSAccountHasPublicKey(alice_account, &error), "Has Public Key" );
240 ok([alice_account.trust resetAccountToEmpty:alice_account transport:alice_account.circle_transport err:&error], "Reset circle to empty");
241 CFReleaseNull(error);
242 is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, NULL), 2, "updates");
243 ok(SOSAccountIsBackupRingEmpty(bob_account, kTestView1), "Bob should not be in the backup");
244 ok(SOSAccountIsBackupRingEmpty(alice_account, kTestView1), "Alice should not be in the backup");
245
246
247 CFReleaseNull(cfpassword);
248 alice_account = nil;
249 bob_account = nil;
250 SOSTestCleanup();
251 #endif
252
253 }
254
255 int secd_62_account_backup(int argc, char *const *argv)
256 {
257 #if !TARGET_OS_SIMULATOR
258 plan_tests(98);
259 #else
260 plan_tests(1);
261 #endif
262
263 secd_test_setup_temp_keychain(__FUNCTION__, NULL);
264 secd_test_setup_testviews(); // for running this test solo
265
266 tests();
267
268 return 0;
269 }