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"
57 #include "SOSAccountTesting.h"
58 #include "SecdTestKeychainUtilities.h"
61 static CFDataRef CopyBackupKeyForString(CFStringRef string, CFErrorRef *error)
63 __block CFDataRef result = NULL;
64 CFStringPerformWithUTF8CFData(string, ^(CFDataRef stringAsData) {
65 result = SOSCopyDeviceBackupPublicKey(stringAsData, error);
70 static void tests(void)
72 __block CFErrorRef error = NULL;
73 CFDataRef cfpassword = CFDataCreate(NULL, (uint8_t *) "FooFooFoo", 10);
74 CFStringRef cfaccount = CFSTR("test@test.org");
77 CFMutableDictionaryRef changes = CFDictionaryCreateMutableForCFTypes(kCFAllocatorDefault);
78 SOSAccount* alice_account = CreateAccountForLocalChanges(CFSTR("Alice"), CFSTR("TestSource"));
79 SOSAccount* bob_account = CreateAccountForLocalChanges(CFSTR("Bob"), CFSTR("TestSource"));
81 CFDataRef alice_backup_key = CopyBackupKeyForString(CFSTR("Alice Backup Entropy"), &error);
82 CFDataRef bob_backup_key = CopyBackupKeyForString(CFSTR("Bob Backup Entropy"), &error);
84 // Bob wins writing at this point, feed the changes back to alice.
85 is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, NULL), 1, "updates");
87 ok(SOSAccountAssertUserCredentialsAndUpdate(alice_account, cfaccount, cfpassword, &error), "Credential setting (%@)", error);
90 ok(SOSAccountResetToOffering_wTxn(alice_account, &error), "Reset to offering (%@)", error);
93 is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, NULL), 2, "updates");
95 ok(SOSAccountAssertUserCredentialsAndUpdate(bob_account, cfaccount, cfpassword, &error), "Credential setting (%@)", error);
98 is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, NULL), 1, "updates");
100 ok(SOSAccountJoinCircles_wTxn(bob_account, &error), "Bob Applies (%@)", error);
101 CFReleaseNull(error);
103 is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, NULL), 2, "updates");
106 CFArrayRef applicants = SOSAccountCopyApplicants(alice_account, &error);
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);
114 is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, NULL), 3, "updates");
116 CFArrayRef peers = SOSAccountCopyPeers(alice_account, &error);
117 ok(peers && CFArrayGetCount(peers) == 2, "See two peers %@ (%@)", peers, error);
118 CFReleaseNull(peers);
120 is([alice_account.trust updateView:alice_account name:kTestView1 code:kSOSCCViewEnable err:&error], kSOSCCViewMember, "Enable view (%@)", error);
121 CFReleaseNull(error);
123 ok([alice_account.trust checkForRings:&error], "Alice_account is good");
124 CFReleaseNull(error);
126 is([bob_account.trust updateView:bob_account name:kTestView1 code:kSOSCCViewEnable err:&error], kSOSCCViewMember, "Enable view (%@)", error);
127 CFReleaseNull(error);
129 ok([bob_account.trust checkForRings:&error], "Bob_account is good");
130 CFReleaseNull(error);
132 ok(SOSAccountSetBackupPublicKey_wTxn(alice_account, alice_backup_key, &error), "Set backup public key, alice (%@)", error);
133 CFReleaseNull(error);
135 ok([alice_account.trust checkForRings:&error], "Alice_account is good");
136 CFReleaseNull(error);
138 ok(SOSAccountSetBackupPublicKey_wTxn(bob_account, bob_backup_key, &error), "Set backup public key, bob (%@)", error);
139 CFReleaseNull(error);
141 ok([bob_account.trust checkForRings:&error], "Alice_account is good");
142 CFReleaseNull(error);
144 ok(SOSAccountIsMyPeerInBackupAndCurrentInView(alice_account, kTestView1), "Is alice's key in backup before sync?");
146 ok([alice_account.trust checkForRings:&error], "Alice_account is good");
147 CFReleaseNull(error);
149 ok(SOSAccountIsMyPeerInBackupAndCurrentInView(bob_account, kTestView1), "Is bob in the backup after sync? - 1");
151 ok([bob_account.trust checkForRings:&error], "Alice_account is good");
152 CFReleaseNull(error);
154 is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, NULL), 4, "updates");
157 ok([alice_account.trust checkForRings:&error], "Alice_account is good");
158 CFReleaseNull(error);
160 ok(SOSAccountIsMyPeerInBackupAndCurrentInView(alice_account, kTestView1), "Is alice in backup after sync?");
162 ok(SOSAccountIsMyPeerInBackupAndCurrentInView(bob_account, kTestView1), "IS bob in the backup after sync");
165 //Bob leaves the circle
167 ok([bob_account.trust leaveCircle:bob_account err:&error], "Bob Leaves (%@)", error);
168 CFReleaseNull(error);
170 //Alice should kick Bob out of the backup!
171 is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, NULL), 2, "updates");
173 ok(SOSAccountIsMyPeerInBackupAndCurrentInView(alice_account, kTestView1), "Bob left the circle, Alice is not in the backup");
175 ok(testAccountPersistence(alice_account), "Test Account->DER->Account Equivalence");
177 SOSAccountTrustClassic *bobTrust = bob_account.trust;
178 ok(!SOSAccountIsPeerInBackupAndCurrentInView(alice_account, bobTrust.peerInfo, kTestView1), "Bob is still in the backup!");
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");
184 CFArrayRef applicants = SOSAccountCopyApplicants(alice_account, &error);
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);
192 is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, NULL), 3, "updates");
196 is([bob_account.trust updateView:bob_account name:kTestView1 code:kSOSCCViewEnable err:&error], kSOSCCViewMember, "Enable view (%@)", error);
197 CFReleaseNull(error);
199 ok(!SOSAccountIsMyPeerInBackupAndCurrentInView(bob_account, kTestView1), "Bob isn't in the backup yet");
201 ok(SOSAccountSetBackupPublicKey_wTxn(bob_account, bob_backup_key, &error), "Set backup public key, bob (%@)", error);
203 is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, NULL), 3, "updates");
206 //removing backup key for bob account
209 ok(SOSAccountRemoveBackupPublickey_wTxn(bob_account, &error), "Removing Bob's backup key (%@)", error);
210 is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, NULL), 2, "updates");
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!");
216 // Setting new backup public key for Bob
219 ok(SOSAccountSetBackupPublicKey_wTxn(bob_account, bob_backup_key, &error), "Set backup public key, bob (%@)", error);
220 CFReleaseNull(error);
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);
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!");
230 is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, NULL), 5, "updates");
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");
242 CFReleaseNull(cfpassword);
249 int secd_62_account_backup(int argc, char *const *argv)
253 secd_test_setup_temp_keychain(__FUNCTION__, NULL);
254 secd_test_setup_testviews(); // for running this test solo