2 // secd-66-account-recovery.c
5 // Created by Richard Murphy on 10/5/16.
12 * Copyright (c) 2012-2014 Apple Inc. All Rights Reserved.
14 * @APPLE_LICENSE_HEADER_START@
16 * This file contains Original Code and/or Modifications of Original Code
17 * as defined in and that are subject to the Apple Public Source License
18 * Version 2.0 (the 'License'). You may not use this file except in
19 * compliance with the License. Please obtain a copy of the License at
20 * http://www.opensource.apple.com/apsl/ and read it before using this
23 * The Original Code and all software distributed under the License are
24 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
25 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
26 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
27 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
28 * Please see the License for the specific language governing rights and
29 * limitations under the License.
31 * @APPLE_LICENSE_HEADER_END@
37 #include <Security/SecBase.h>
38 #include <Security/SecItem.h>
40 #include <CoreFoundation/CFDictionary.h>
42 #include "keychain/SecureObjectSync/SOSAccount.h"
43 #include <Security/SecureObjectSync/SOSCloudCircle.h>
44 #include "keychain/SecureObjectSync/SOSInternal.h"
45 #include "keychain/SecureObjectSync/SOSUserKeygen.h"
46 #include "keychain/SecureObjectSync/SOSTransport.h"
47 #include <Security/SecureObjectSync/SOSViews.h>
48 #include "keychain/SecureObjectSync/SOSAccountTrustClassic+Expansion.h"
53 #include "secd_regressions.h"
54 #include "keychain/SecureObjectSync/Regressions/SOSTestDataSource.h"
56 #include "SOSRegressionUtilities.h"
57 #include <Security/SecRecoveryKey.h>
59 #include <utilities/SecCFWrappers.h>
60 #include <Security/SecKeyPriv.h>
62 #include "keychain/securityd/SOSCloudCircleServer.h"
63 #include "SecdTestKeychainUtilities.h"
65 #if TARGET_OS_SIMULATOR
67 int secd_66_account_recovery(int argc, char *const *argv) {
69 secd_test_setup_temp_keychain(__FUNCTION__, NULL);
75 #include "SOSAccountTesting.h"
77 static CFDataRef CopyBackupKeyForString(CFStringRef string, CFErrorRef *error)
79 __block CFDataRef result = NULL;
80 CFStringPerformWithUTF8CFData(string, ^(CFDataRef stringAsData) {
81 result = SOSCopyDeviceBackupPublicKey(stringAsData, error);
87 static inline bool SOSAccountSetRecoveryKey_wTxn(SOSAccount* acct, CFDataRef recoveryPub, CFErrorRef* error) {
88 __block bool result = false;
89 [acct performTransaction:^(SOSAccountTransaction * _Nonnull txn) {
90 result = SOSAccountSetRecoveryKey(txn.account, recoveryPub, error);
95 static inline bool SOSAccountSOSAccountRemoveRecoveryKey_wTxn(SOSAccount* acct, CFErrorRef* error) {
96 __block bool result = false;
97 [acct performTransaction:^(SOSAccountTransaction * _Nonnull txn) {
98 result = SOSAccountRemoveRecoveryKey(txn.account, error);
104 static void registerRecoveryKeyNow(CFMutableDictionaryRef changes, SOSAccount* registrar, SOSAccount* observer, CFDataRef recoveryPub, bool recKeyFirst) {
105 CFErrorRef error = NULL;
107 is(ProcessChangesUntilNoChange(changes, registrar, observer, NULL), 1, "updates");
110 ok(SOSAccountSetRecoveryKey_wTxn(registrar, recoveryPub, &error), "Set Recovery Key");
111 CFReleaseNull(error);
113 ok(SOSAccountSOSAccountRemoveRecoveryKey_wTxn(registrar, &error), "Clear Recovery Key");
114 CFReleaseNull(error);
116 ok(error == NULL, "Error shouldn't be %@", error);
117 CFReleaseNull(error);
118 ProcessChangesUntilNoChange(changes, registrar, observer, NULL);
120 CFDataRef registrar_recKey = SOSAccountCopyRecoveryPublic(kCFAllocatorDefault, registrar, &error);
121 CFReleaseNull(error);
122 CFDataRef observer_recKey = SOSAccountCopyRecoveryPublic(kCFAllocatorDefault, observer, &error);
123 CFReleaseNull(error);
126 ok(registrar_recKey, "Registrar retrieved recKey");
127 ok(observer_recKey, "Observer retrieved recKey");
128 ok(CFEqualSafe(registrar_recKey, observer_recKey), "recKeys are the same");
129 ok(CFEqualSafe(registrar_recKey, recoveryPub), "recKeys are as expected");
131 ok((!registrar_recKey && !observer_recKey), "recKeys are NULL");
133 CFReleaseNull(observer_recKey);
134 CFReleaseNull(registrar_recKey);
137 static void tests(bool recKeyFirst)
139 __block CFErrorRef error = NULL;
140 CFStringRef sock_drawer_key = CFSTR("AAAA-AAAA-AAAA-AAAA-AAAA-AAAA-AAGW");
141 SecRecoveryKey *sRecKey = NULL;
142 CFDataRef fullKeyBytes = NULL;
143 CFDataRef pubKeyBytes = NULL;
145 sRecKey = SecRKCreateRecoveryKeyWithError((__bridge NSString*)sock_drawer_key, NULL);
146 ok(sRecKey, "Create SecRecoveryKey from String");
148 fullKeyBytes = (__bridge CFDataRef)(SecRKCopyBackupFullKey(sRecKey));
149 pubKeyBytes = (__bridge CFDataRef)(SecRKCopyBackupPublicKey(sRecKey));
150 ok(fullKeyBytes && pubKeyBytes, "Got KeyPair from SecRecoveryKey");
152 if(!(fullKeyBytes && pubKeyBytes)) {
153 diag("Cannot Proceed - couldn't make usable recoveryKey from sock-drawer-key");
154 CFReleaseNull(fullKeyBytes);
155 CFReleaseNull(pubKeyBytes);
159 CFDataRef cfpassword = CFDataCreate(NULL, (uint8_t *) "FooFooFoo", 10);
160 CFStringRef cfaccount = CFSTR("test@test.org");
161 CFStringRef cfdsid = CFSTR("DSIDFooFoo");
163 secd_test_setup_testviews(); // for running this test solo
165 CFMutableDictionaryRef changes = CFDictionaryCreateMutableForCFTypes(kCFAllocatorDefault);
166 SOSAccount* alice_account = CreateAccountForLocalChanges(CFSTR("Alice"), CFSTR("TestSource"));
167 SOSAccountAssertDSID(alice_account, cfdsid);
168 SOSAccount* bob_account = CreateAccountForLocalChanges(CFSTR("Bob"), CFSTR("TestSource"));
169 SOSAccountAssertDSID(bob_account, cfdsid);
171 CFDataRef alice_backup_key = CopyBackupKeyForString(CFSTR("Alice Backup Entropy"), &error);
172 CFDataRef bob_backup_key = CopyBackupKeyForString(CFSTR("Bob Backup Entropy"), &error);
175 ok(SOSTestStartCircleWithAccount(alice_account, changes, cfaccount, cfpassword), "Have Alice start a circle");
176 is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, NULL), 1, "updates");
177 ok(SOSTestJoinWithApproval(cfpassword, cfaccount, changes, alice_account, bob_account, KEEP_USERKEY, 2, false), "Bob Joins");
179 CFArrayRef peers = SOSAccountCopyPeers(alice_account, &error);
180 ok(peers && CFArrayGetCount(peers) == 2, "See two peers %@ (%@)", peers, error);
181 CFReleaseNull(peers);
183 if(recKeyFirst) registerRecoveryKeyNow(changes, alice_account, bob_account, pubKeyBytes, recKeyFirst);
186 is([alice_account.trust updateView:alice_account name:kTestView1 code:kSOSCCViewEnable err:&error], kSOSCCViewMember, "Enable view (%@)", error);
187 CFReleaseNull(error);
189 ok([alice_account.trust checkForRings:&error], "Alice_account is good");
190 CFReleaseNull(error);
192 ProcessChangesUntilNoChange(changes, alice_account, bob_account, NULL);
194 is([bob_account.trust updateView:bob_account name:kTestView1 code:kSOSCCViewEnable err:&error], kSOSCCViewMember, "Enable view (%@)", error);
195 CFReleaseNull(error);
197 ok([bob_account.trust checkForRings:&error], "Bob_account is good");
198 CFReleaseNull(error);
200 ok(SOSAccountSetBackupPublicKey_wTxn(alice_account, alice_backup_key, &error), "Set backup public key, alice (%@)", error);
201 CFReleaseNull(error);
203 ok([alice_account.trust checkForRings:&error], "Alice_account is good");
204 CFReleaseNull(error);
206 ok(SOSAccountSetBackupPublicKey_wTxn(bob_account, bob_backup_key, &error), "Set backup public key, bob (%@)", error);
207 CFReleaseNull(error);
209 ok([bob_account.trust checkForRings:&error], "Bob_account is good");
210 CFReleaseNull(error);
212 ok(SOSAccountIsMyPeerInBackupAndCurrentInView_wTxn(alice_account, kTestView1), "Is alice is in backup before sync?");
215 SOSBackupSliceKeyBagRef bskb = SOSAccountBackupSliceKeyBagForView_wTxn(alice_account, kTestView1, &error);
216 CFReleaseNull(error);
217 ok(!SOSBSKBHasRecoveryKey(bskb), "BSKB should not have recovery key");
221 ok([alice_account.trust checkForRings:&error], "Alice_account is good");
222 CFReleaseNull(error);
224 ok(SOSAccountIsMyPeerInBackupAndCurrentInView_wTxn(bob_account, kTestView1), "Is bob in the backup after sync? - 1");
226 ok([bob_account.trust checkForRings:&error], "Alice_account is good");
227 CFReleaseNull(error);
229 int passes = ProcessChangesUntilNoChange(changes, alice_account, bob_account, NULL);
230 ok(passes < 6, "updates"); // this was varying between 4 and 5 on different BATS runs - we just don't want it going crazy
232 ok([alice_account.trust checkForRings:&error], "Alice_account is good");
233 CFReleaseNull(error);
235 ok(SOSAccountIsMyPeerInBackupAndCurrentInView_wTxn(alice_account, kTestView1), "Is alice is in backup after sync?");
237 ok(SOSAccountIsMyPeerInBackupAndCurrentInView_wTxn(bob_account, kTestView1), "IS bob in the backup after sync");
240 //Bob leaves the circle
242 ok([bob_account.trust leaveCircle:bob_account err:&error], "Bob Leaves (%@)", error);
243 CFReleaseNull(error);
245 //Alice should kick Bob out of the backup!
246 is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, NULL), 2, "updates");
248 ok(SOSAccountIsMyPeerInBackupAndCurrentInView_wTxn(alice_account, kTestView1), "Bob left the circle, Alice is in the backup");
250 //ok(testAccountPersistence(alice_account), "Test Account->DER->Account Equivalence");
251 SOSAccountTrustClassic* bobTrust = bob_account.trust;
252 ok(!SOSAccountIsPeerInBackupAndCurrentInView_wTxn(alice_account, bobTrust.peerInfo, kTestView1), "Bob is still in the backup!");
254 //Bob gets back into the circle
255 ok(SOSTestJoinWithApproval(cfpassword, cfaccount, changes, alice_account, bob_account, KEEP_USERKEY, 2, false), "Bob Re-Joins");
258 is([bob_account.trust updateView:bob_account name:kTestView1 code:kSOSCCViewEnable err:&error], kSOSCCViewMember, "Enable view (%@)", error);
259 CFReleaseNull(error);
261 ok(!SOSAccountIsMyPeerInBackupAndCurrentInView_wTxn(bob_account, kTestView1), "Bob isn't in the backup yet");
263 ok(SOSAccountSetBackupPublicKey_wTxn(bob_account, bob_backup_key, &error), "Set backup public key, alice (%@)", error);
265 is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, NULL), 3, "updates");
268 //removing backup key for bob account
271 ok(SOSAccountRemoveBackupPublickey_wTxn(bob_account, &error), "Removing Bob's backup key (%@)", error);
272 int nchanges = (recKeyFirst) ? 2: 2;
273 is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, NULL), nchanges, "updates");
275 ok(!SOSAccountIsMyPeerInBackupAndCurrentInView_wTxn(bob_account, kTestView1), "Bob's backup key is in the backup - should not be so!");
276 ok(!SOSAccountIsPeerInBackupAndCurrentInView_wTxn(alice_account, bobTrust.peerInfo, kTestView1), "Bob is up to date in the backup!");
279 // Setting new backup public key for Bob
282 ok(SOSAccountSetBackupPublicKey_wTxn(bob_account, bob_backup_key, &error), "Set backup public key, alice (%@)", error);
283 CFReleaseNull(error);
285 is([bob_account.trust updateView:bob_account name:kTestView1 code:kSOSCCViewEnable err:&error], kSOSCCViewMember, "Enable view (%@)", error);
286 ok(SOSAccountNewBKSBForView(bob_account, kTestView1, &error), "Setting new backup public key for bob account failed: (%@)", error);
288 //bob is in his own backup
289 ok(SOSAccountIsMyPeerInBackupAndCurrentInView_wTxn(bob_account, kTestView1), "Bob's backup key is not in the backup");
290 //alice does not have bob in her backup
291 ok(!SOSAccountIsPeerInBackupAndCurrentInView_wTxn(alice_account, bobTrust.peerInfo, kTestView1), "Bob is up to date in the backup - should not be so!");
293 is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, NULL), 5, "updates");
295 ok(SOSAccountIsMyPeerInBackupAndCurrentInView_wTxn(bob_account, kTestView1), "Bob's backup key should be in the backup");
296 ok(SOSAccountIsMyPeerInBackupAndCurrentInView_wTxn(alice_account, kTestView1), "Alice is in the backup");
298 if(!recKeyFirst) registerRecoveryKeyNow(changes, alice_account, bob_account, pubKeyBytes, recKeyFirst);
300 ok(SOSAccountRecoveryKeyIsInBackupAndCurrentInView_wTxn(alice_account, kTestView1), "Recovery Key is also in the backup");
301 ok(SOSAccountRecoveryKeyIsInBackupAndCurrentInView_wTxn(bob_account, kTestView1), "Recovery Key is also in the backup");
303 SOSBackupSliceKeyBagRef bskb = SOSAccountBackupSliceKeyBagForView_wTxn(alice_account, kTestView1, &error);
304 CFReleaseNull(error);
306 ok(SOSBSKBHasRecoveryKey(bskb), "BSKB should have recovery key");
308 CFDataRef wrappingKey = CFStringCreateExternalRepresentation(kCFAllocatorDefault, sock_drawer_key, kCFStringEncodingUTF8, 0);
309 ok(wrappingKey, "Made wrapping key from with sock drawer key");
310 bskb_keybag_handle_t bskbHandle = SOSBSKBLoadAndUnlockWithWrappingSecret(bskb, wrappingKey, &error);
311 ok(bskbHandle, "Made bskbHandle with recover key");
312 ok(SOSAccountHasPublicKey(alice_account, &error), "Has Public Key" );
314 // Testing reset (Null) recoveryKey =========
317 CFReleaseNull(wrappingKey);
319 registerRecoveryKeyNow(changes, alice_account, bob_account, NULL, recKeyFirst);
321 ok(!SOSAccountRecoveryKeyIsInBackupAndCurrentInView_wTxn(alice_account, kTestView1), "Recovery Key is not in the backup");
322 ok(!SOSAccountRecoveryKeyIsInBackupAndCurrentInView_wTxn(bob_account, kTestView1), "Recovery Key is not in the backup");
324 bskb = SOSAccountBackupSliceKeyBagForView_wTxn(alice_account, kTestView1, &error);
325 CFReleaseNull(error);
327 ok(!SOSBSKBHasRecoveryKey(bskb), "BSKB should not have recovery key");
332 ok([alice_account.trust resetAccountToEmpty:alice_account transport:alice_account.circle_transport err:&error], "Reset circle to empty");
333 CFReleaseNull(error);
334 is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, NULL), 2, "updates");
335 ok(SOSAccountIsBackupRingEmpty(bob_account, kTestView1), "Bob should not be in the backup");
336 ok(SOSAccountIsBackupRingEmpty(alice_account, kTestView1), "Alice should not be in the backup");
338 CFReleaseNull(fullKeyBytes);
339 CFReleaseNull(pubKeyBytes);
342 CFReleaseNull(cfpassword);
343 CFReleaseNull(wrappingKey);
350 int secd_66_account_recovery(int argc, char *const *argv) {
353 secd_test_setup_temp_keychain(__FUNCTION__, NULL);