3 * Copyright (c) 2012-2014 Apple Inc. All Rights Reserved.
5 * @APPLE_LICENSE_HEADER_START@
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
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.
22 * @APPLE_LICENSE_HEADER_END@
28 #include <Security/SecBase.h>
29 #include <Security/SecItem.h>
31 #include <CoreFoundation/CFDictionary.h>
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>
41 #import "keychain/SecureObjectSync/SOSAccountTrustClassic.h"
42 #import "keychain/SecureObjectSync/SOSAccountTrustClassic+Expansion.h"
47 #include "secd_regressions.h"
48 #include "SOSTestDataSource.h"
50 #include "SOSRegressionUtilities.h"
51 #include <utilities/SecCFWrappers.h>
52 #include <Security/SecKeyPriv.h>
54 #include "keychain/securityd/SOSCloudCircleServer.h"
56 #if !TARGET_OS_SIMULATOR
57 #include "SOSAccountTesting.h"
59 #include "SecdTestKeychainUtilities.h"
61 #if !TARGET_OS_SIMULATOR
63 static CFDataRef CopyBackupKeyForString(CFStringRef string, CFErrorRef *error)
65 __block CFDataRef result = NULL;
66 CFStringPerformWithUTF8CFData(string, ^(CFDataRef stringAsData) {
67 result = SOSCopyDeviceBackupPublicKey(stringAsData, error);
73 static void tests(void)
75 #if !TARGET_OS_SIMULATOR
77 __block CFErrorRef error = NULL;
78 CFDataRef cfpassword = CFDataCreate(NULL, (uint8_t *) "FooFooFoo", 10);
79 CFStringRef cfaccount = CFSTR("test@test.org");
82 CFMutableDictionaryRef changes = CFDictionaryCreateMutableForCFTypes(kCFAllocatorDefault);
83 SOSAccount* alice_account = CreateAccountForLocalChanges(CFSTR("Alice"), CFSTR("TestSource"));
84 SOSAccount* bob_account = CreateAccountForLocalChanges(CFSTR("Bob"), CFSTR("TestSource"));
86 CFDataRef alice_backup_key = CopyBackupKeyForString(CFSTR("Alice Backup Entropy"), &error);
87 CFDataRef bob_backup_key = CopyBackupKeyForString(CFSTR("Bob Backup Entropy"), &error);
89 // Bob wins writing at this point, feed the changes back to alice.
90 is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, NULL), 1, "updates");
92 ok(SOSAccountAssertUserCredentialsAndUpdate(alice_account, cfaccount, cfpassword, &error), "Credential setting (%@)", error);
95 ok(SOSAccountResetToOffering_wTxn(alice_account, &error), "Reset to offering (%@)", error);
98 is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, NULL), 2, "updates");
100 ok(SOSAccountAssertUserCredentialsAndUpdate(bob_account, cfaccount, cfpassword, &error), "Credential setting (%@)", error);
101 CFReleaseNull(error);
103 is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, NULL), 1, "updates");
105 ok(SOSAccountJoinCircles_wTxn(bob_account, &error), "Bob Applies (%@)", error);
106 CFReleaseNull(error);
108 is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, NULL), 2, "updates");
111 CFArrayRef applicants = SOSAccountCopyApplicants(alice_account, &error);
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);
119 is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, NULL), 3, "updates");
121 CFArrayRef peers = SOSAccountCopyPeers(alice_account, &error);
122 ok(peers && CFArrayGetCount(peers) == 2, "See two peers %@ (%@)", peers, error);
123 CFReleaseNull(peers);
125 is([alice_account.trust updateView:alice_account name:kTestView1 code:kSOSCCViewEnable err:&error], kSOSCCViewMember, "Enable view (%@)", error);
126 CFReleaseNull(error);
128 ok([alice_account.trust checkForRings:&error], "Alice_account is good");
129 CFReleaseNull(error);
131 is([bob_account.trust updateView:bob_account name:kTestView1 code:kSOSCCViewEnable err:&error], kSOSCCViewMember, "Enable view (%@)", error);
132 CFReleaseNull(error);
134 ok([bob_account.trust checkForRings:&error], "Bob_account is good");
135 CFReleaseNull(error);
137 ok(SOSAccountSetBackupPublicKey_wTxn(alice_account, alice_backup_key, &error), "Set backup public key, alice (%@)", error);
138 CFReleaseNull(error);
140 ok([alice_account.trust checkForRings:&error], "Alice_account is good");
141 CFReleaseNull(error);
143 ok(SOSAccountSetBackupPublicKey_wTxn(bob_account, bob_backup_key, &error), "Set backup public key, bob (%@)", error);
144 CFReleaseNull(error);
146 ok([bob_account.trust checkForRings:&error], "Alice_account is good");
147 CFReleaseNull(error);
149 ok(SOSAccountIsMyPeerInBackupAndCurrentInView(alice_account, kTestView1), "Is alice's key in backup before sync?");
151 ok([alice_account.trust checkForRings:&error], "Alice_account is good");
152 CFReleaseNull(error);
154 ok(SOSAccountIsMyPeerInBackupAndCurrentInView(bob_account, kTestView1), "Is bob in the backup after sync? - 1");
156 ok([bob_account.trust checkForRings:&error], "Alice_account is good");
157 CFReleaseNull(error);
159 is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, NULL), 5, "updates");
162 ok([alice_account.trust checkForRings:&error], "Alice_account is good");
163 CFReleaseNull(error);
165 ok(SOSAccountIsMyPeerInBackupAndCurrentInView(alice_account, kTestView1), "Is alice in backup after sync?");
167 ok(SOSAccountIsMyPeerInBackupAndCurrentInView(bob_account, kTestView1), "IS bob in the backup after sync");
169 ok(!SOSAccountIsLastBackupPeer(alice_account, &error), "Alice is not last backup peer");
170 CFReleaseNull(error);
173 //Bob leaves the circle
175 ok([bob_account.trust leaveCircle:bob_account err:&error], "Bob Leaves (%@)", error);
176 CFReleaseNull(error);
178 //Alice should kick Bob out of the backup!
179 is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, NULL), 2, "updates");
181 ok(SOSAccountIsMyPeerInBackupAndCurrentInView(alice_account, kTestView1), "Bob left the circle, Alice is not in the backup");
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);
188 ok(testAccountPersistence(alice_account), "Test Account->DER->Account Equivalence");
190 SOSAccountTrustClassic *bobTrust = bob_account.trust;
191 ok(!SOSAccountIsPeerInBackupAndCurrentInView(alice_account, bobTrust.peerInfo, kTestView1), "Bob is still in the backup!");
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");
197 CFArrayRef applicants = SOSAccountCopyApplicants(alice_account, &error);
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);
205 is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, NULL), 3, "updates");
209 is([bob_account.trust updateView:bob_account name:kTestView1 code:kSOSCCViewEnable err:&error], kSOSCCViewMember, "Enable view (%@)", error);
210 CFReleaseNull(error);
212 ok(!SOSAccountIsMyPeerInBackupAndCurrentInView(bob_account, kTestView1), "Bob isn't in the backup yet");
214 ok(!SOSAccountIsLastBackupPeer(alice_account, &error), "Alice is the not the last backup peer - Bob still registers as one");
215 CFReleaseNull(error);
217 ok(SOSAccountSetBackupPublicKey_wTxn(bob_account, bob_backup_key, &error), "Set backup public key, bob (%@)", error);
219 is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, NULL), 3, "updates");
221 ok(!SOSAccountIsLastBackupPeer(alice_account, &error), "Alice is not last backup peer");
222 CFReleaseNull(error);
225 //removing backup key for bob account
228 ok(SOSAccountRemoveBackupPublickey_wTxn(bob_account, &error), "Removing Bob's backup key (%@)", error);
229 is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, NULL), 2, "updates");
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!");
235 // Setting new backup public key for Bob
238 ok(SOSAccountSetBackupPublicKey_wTxn(bob_account, bob_backup_key, &error), "Set backup public key, bob (%@)", error);
239 CFReleaseNull(error);
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);
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!");
249 is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, NULL), 5, "updates");
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");
261 CFReleaseNull(cfpassword);
269 int secd_62_account_backup(int argc, char *const *argv)
271 #if !TARGET_OS_SIMULATOR
277 secd_test_setup_temp_keychain(__FUNCTION__, NULL);
278 secd_test_setup_testviews(); // for running this test solo