]> git.saurik.com Git - apple/security.git/blob - keychain/securityd/Regressions/secd-62-account-backup.m
Security-59754.41.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
57 #include "SOSAccountTesting.h"
58 #include "SecdTestKeychainUtilities.h"
59 #if SOS_ENABLED
60
61 static CFDataRef CopyBackupKeyForString(CFStringRef string, CFErrorRef *error)
62 {
63 __block CFDataRef result = NULL;
64 CFStringPerformWithUTF8CFData(string, ^(CFDataRef stringAsData) {
65 result = SOSCopyDeviceBackupPublicKey(stringAsData, error);
66 });
67 return result;
68 }
69
70 static void tests(void)
71 {
72 __block CFErrorRef error = NULL;
73 CFDataRef cfpassword = CFDataCreate(NULL, (uint8_t *) "FooFooFoo", 10);
74 CFStringRef cfaccount = CFSTR("test@test.org");
75
76
77 CFMutableDictionaryRef changes = CFDictionaryCreateMutableForCFTypes(kCFAllocatorDefault);
78 SOSAccount* alice_account = CreateAccountForLocalChanges(CFSTR("Alice"), CFSTR("TestSource"));
79 SOSAccount* bob_account = CreateAccountForLocalChanges(CFSTR("Bob"), CFSTR("TestSource"));
80
81 CFDataRef alice_backup_key = CopyBackupKeyForString(CFSTR("Alice Backup Entropy"), &error);
82 CFDataRef bob_backup_key = CopyBackupKeyForString(CFSTR("Bob Backup Entropy"), &error);
83
84 // Bob wins writing at this point, feed the changes back to alice.
85 is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, NULL), 1, "updates");
86
87 ok(SOSAccountAssertUserCredentialsAndUpdate(alice_account, cfaccount, cfpassword, &error), "Credential setting (%@)", error);
88 CFReleaseNull(error);
89
90 ok(SOSAccountResetToOffering_wTxn(alice_account, &error), "Reset to offering (%@)", error);
91 CFReleaseNull(error);
92
93 is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, NULL), 2, "updates");
94
95 ok(SOSAccountAssertUserCredentialsAndUpdate(bob_account, cfaccount, cfpassword, &error), "Credential setting (%@)", error);
96 CFReleaseNull(error);
97
98 is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, NULL), 1, "updates");
99
100 ok(SOSAccountJoinCircles_wTxn(bob_account, &error), "Bob Applies (%@)", error);
101 CFReleaseNull(error);
102
103 is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, NULL), 2, "updates");
104
105 {
106 CFArrayRef applicants = SOSAccountCopyApplicants(alice_account, &error);
107
108 ok(applicants && CFArrayGetCount(applicants) == 1, "See one applicant %@ (%@)", applicants, error);
109 ok(SOSAccountAcceptApplicants(alice_account, applicants, &error), "Alice accepts (%@)", error);
110 CFReleaseNull(error);
111 CFReleaseNull(applicants);
112 }
113
114 is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, NULL), 3, "updates");
115
116 CFArrayRef peers = SOSAccountCopyPeers(alice_account, &error);
117 ok(peers && CFArrayGetCount(peers) == 2, "See two peers %@ (%@)", peers, error);
118 CFReleaseNull(peers);
119
120 is([alice_account.trust updateView:alice_account name:kTestView1 code:kSOSCCViewEnable err:&error], kSOSCCViewMember, "Enable view (%@)", error);
121 CFReleaseNull(error);
122
123 ok([alice_account.trust checkForRings:&error], "Alice_account is good");
124 CFReleaseNull(error);
125
126 is([bob_account.trust updateView:bob_account name:kTestView1 code:kSOSCCViewEnable err:&error], kSOSCCViewMember, "Enable view (%@)", error);
127 CFReleaseNull(error);
128
129 ok([bob_account.trust checkForRings:&error], "Bob_account is good");
130 CFReleaseNull(error);
131
132 ok(SOSAccountSetBackupPublicKey_wTxn(alice_account, alice_backup_key, &error), "Set backup public key, alice (%@)", error);
133 CFReleaseNull(error);
134
135 ok([alice_account.trust checkForRings:&error], "Alice_account is good");
136 CFReleaseNull(error);
137
138 ok(SOSAccountSetBackupPublicKey_wTxn(bob_account, bob_backup_key, &error), "Set backup public key, bob (%@)", error);
139 CFReleaseNull(error);
140
141 ok([bob_account.trust checkForRings:&error], "Alice_account is good");
142 CFReleaseNull(error);
143
144 ok(SOSAccountIsMyPeerInBackupAndCurrentInView(alice_account, kTestView1), "Is alice's key in backup before sync?");
145
146 ok([alice_account.trust checkForRings:&error], "Alice_account is good");
147 CFReleaseNull(error);
148
149 ok(SOSAccountIsMyPeerInBackupAndCurrentInView(bob_account, kTestView1), "Is bob in the backup after sync? - 1");
150
151 ok([bob_account.trust checkForRings:&error], "Alice_account is good");
152 CFReleaseNull(error);
153
154 is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, NULL), 4, "updates");
155
156
157 ok([alice_account.trust checkForRings:&error], "Alice_account is good");
158 CFReleaseNull(error);
159
160 ok(SOSAccountIsMyPeerInBackupAndCurrentInView(alice_account, kTestView1), "Is alice in backup after sync?");
161
162 ok(SOSAccountIsMyPeerInBackupAndCurrentInView(bob_account, kTestView1), "IS bob in the backup after sync");
163
164 //
165 //Bob leaves the circle
166 //
167 ok([bob_account.trust leaveCircle:bob_account err:&error], "Bob Leaves (%@)", error);
168 CFReleaseNull(error);
169
170 //Alice should kick Bob out of the backup!
171 is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, NULL), 2, "updates");
172
173 ok(SOSAccountIsMyPeerInBackupAndCurrentInView(alice_account, kTestView1), "Bob left the circle, Alice is not in the backup");
174
175 ok(testAccountPersistence(alice_account), "Test Account->DER->Account Equivalence");
176
177 SOSAccountTrustClassic *bobTrust = bob_account.trust;
178 ok(!SOSAccountIsPeerInBackupAndCurrentInView(alice_account, bobTrust.peerInfo, kTestView1), "Bob is still in the backup!");
179
180 //Bob gets back into the circle
181 ok(SOSAccountJoinCircles_wTxn(bob_account, &error));
182 is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, NULL), 2, "updates");
183 {
184 CFArrayRef applicants = SOSAccountCopyApplicants(alice_account, &error);
185
186 ok(applicants && CFArrayGetCount(applicants) == 1, "See one applicant %@ (%@)", applicants, error);
187 ok(SOSAccountAcceptApplicants(alice_account, applicants, &error), "Alice accepts (%@)", error);
188 CFReleaseNull(error);
189 CFReleaseNull(applicants);
190 }
191
192 is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, NULL), 3, "updates");
193
194
195 //enables view
196 is([bob_account.trust updateView:bob_account name:kTestView1 code:kSOSCCViewEnable err:&error], kSOSCCViewMember, "Enable view (%@)", error);
197 CFReleaseNull(error);
198
199 ok(!SOSAccountIsMyPeerInBackupAndCurrentInView(bob_account, kTestView1), "Bob isn't in the backup yet");
200
201 ok(SOSAccountSetBackupPublicKey_wTxn(bob_account, bob_backup_key, &error), "Set backup public key, bob (%@)", error);
202
203 is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, NULL), 3, "updates");
204
205 //
206 //removing backup key for bob account
207 //
208
209 ok(SOSAccountRemoveBackupPublickey_wTxn(bob_account, &error), "Removing Bob's backup key (%@)", error);
210 is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, NULL), 2, "updates");
211
212 ok(!SOSAccountIsMyPeerInBackupAndCurrentInView(bob_account, kTestView1), "Bob's backup key is in the backup - should not be so!");
213 ok(!SOSAccountIsPeerInBackupAndCurrentInView(alice_account, bobTrust.peerInfo, kTestView1), "Bob is up to date in the backup!");
214
215 //
216 // Setting new backup public key for Bob
217 //
218
219 ok(SOSAccountSetBackupPublicKey_wTxn(bob_account, bob_backup_key, &error), "Set backup public key, bob (%@)", error);
220 CFReleaseNull(error);
221
222 is([bob_account.trust updateView:bob_account name:kTestView1 code:kSOSCCViewEnable err:&error], kSOSCCViewMember, "Enable view (%@)", error);
223 ok(SOSAccountNewBKSBForView(bob_account, kTestView1, &error), "Setting new backup public key for bob account failed: (%@)", error);
224
225 //bob is in his own backup
226 ok(SOSAccountIsMyPeerInBackupAndCurrentInView(bob_account, kTestView1), "Bob's backup key is not in the backup");
227 //alice does not have bob in her backup
228 ok(!SOSAccountIsPeerInBackupAndCurrentInView(alice_account, bobTrust.peerInfo, kTestView1), "Bob is up to date in the backup - should not be so!");
229
230 is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, NULL), 5, "updates");
231
232 ok(SOSAccountIsMyPeerInBackupAndCurrentInView(bob_account, kTestView1), "Bob's backup key should be in the backup");
233 ok(SOSAccountIsMyPeerInBackupAndCurrentInView(alice_account, kTestView1), "Alice is in the backup");
234 ok(SOSAccountHasPublicKey(alice_account, &error), "Has Public Key" );
235 ok([alice_account.trust resetAccountToEmpty:alice_account transport:alice_account.circle_transport err:&error], "Reset circle to empty");
236 CFReleaseNull(error);
237 is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, NULL), 2, "updates");
238 ok(SOSAccountIsBackupRingEmpty(bob_account, kTestView1), "Bob should not be in the backup");
239 ok(SOSAccountIsBackupRingEmpty(alice_account, kTestView1), "Alice should not be in the backup");
240
241
242 CFReleaseNull(cfpassword);
243 alice_account = nil;
244 bob_account = nil;
245 SOSTestCleanup();
246 }
247 #endif
248
249 int secd_62_account_backup(int argc, char *const *argv)
250 {
251 #if SOS_ENABLED
252 plan_tests(98);
253 secd_test_setup_temp_keychain(__FUNCTION__, NULL);
254 secd_test_setup_testviews(); // for running this test solo
255 tests();
256 #else
257 plan_tests(0);
258 #endif
259 return 0;
260 }