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"
67 #include "SOSAccountTesting.h"
69 static CFDataRef CopyBackupKeyForString(CFStringRef string, CFErrorRef *error)
71 __block CFDataRef result = NULL;
72 CFStringPerformWithUTF8CFData(string, ^(CFDataRef stringAsData) {
73 result = SOSCopyDeviceBackupPublicKey(stringAsData, error);
79 static inline bool SOSAccountSetRecoveryKey_wTxn(SOSAccount* acct, CFDataRef recoveryPub, CFErrorRef* error) {
80 __block bool result = false;
81 [acct performTransaction:^(SOSAccountTransaction * _Nonnull txn) {
82 result = SOSAccountSetRecoveryKey(txn.account, recoveryPub, error);
87 static inline bool SOSAccountSOSAccountRemoveRecoveryKey_wTxn(SOSAccount* acct, CFErrorRef* error) {
88 __block bool result = false;
89 [acct performTransaction:^(SOSAccountTransaction * _Nonnull txn) {
90 result = SOSAccountRemoveRecoveryKey(txn.account, error);
96 static void registerRecoveryKeyNow(CFMutableDictionaryRef changes, SOSAccount* registrar, SOSAccount* observer, CFDataRef recoveryPub, bool recKeyFirst) {
97 CFErrorRef error = NULL;
99 is(ProcessChangesUntilNoChange(changes, registrar, observer, NULL), 1, "updates");
102 ok(SOSAccountSetRecoveryKey_wTxn(registrar, recoveryPub, &error), "Set Recovery Key");
103 CFReleaseNull(error);
105 ok(SOSAccountSOSAccountRemoveRecoveryKey_wTxn(registrar, &error), "Clear Recovery Key");
106 CFReleaseNull(error);
108 ok(error == NULL, "Error shouldn't be %@", error);
109 CFReleaseNull(error);
110 ProcessChangesUntilNoChange(changes, registrar, observer, NULL);
112 CFDataRef registrar_recKey = SOSAccountCopyRecoveryPublic(kCFAllocatorDefault, registrar, &error);
113 CFReleaseNull(error);
114 CFDataRef observer_recKey = SOSAccountCopyRecoveryPublic(kCFAllocatorDefault, observer, &error);
115 CFReleaseNull(error);
118 ok(registrar_recKey, "Registrar retrieved recKey");
119 ok(observer_recKey, "Observer retrieved recKey");
120 ok(CFEqualSafe(registrar_recKey, observer_recKey), "recKeys are the same");
121 ok(CFEqualSafe(registrar_recKey, recoveryPub), "recKeys are as expected");
123 ok((!registrar_recKey && !observer_recKey), "recKeys are NULL");
125 CFReleaseNull(observer_recKey);
126 CFReleaseNull(registrar_recKey);
129 static void tests(bool recKeyFirst)
131 __block CFErrorRef error = NULL;
132 CFStringRef sock_drawer_key = CFSTR("AAAA-AAAA-AAAA-AAAA-AAAA-AAAA-AAGW");
133 SecRecoveryKey *sRecKey = NULL;
134 CFDataRef fullKeyBytes = NULL;
135 CFDataRef pubKeyBytes = NULL;
137 sRecKey = SecRKCreateRecoveryKeyWithError((__bridge NSString*)sock_drawer_key, NULL);
138 ok(sRecKey, "Create SecRecoveryKey from String");
140 fullKeyBytes = (__bridge CFDataRef)(SecRKCopyBackupFullKey(sRecKey));
141 pubKeyBytes = (__bridge CFDataRef)(SecRKCopyBackupPublicKey(sRecKey));
142 ok(fullKeyBytes && pubKeyBytes, "Got KeyPair from SecRecoveryKey");
144 if(!(fullKeyBytes && pubKeyBytes)) {
145 diag("Cannot Proceed - couldn't make usable recoveryKey from sock-drawer-key");
146 CFReleaseNull(fullKeyBytes);
147 CFReleaseNull(pubKeyBytes);
151 CFDataRef cfpassword = CFDataCreate(NULL, (uint8_t *) "FooFooFoo", 10);
152 CFStringRef cfaccount = CFSTR("test@test.org");
153 CFStringRef cfdsid = CFSTR("DSIDFooFoo");
155 secd_test_setup_testviews(); // for running this test solo
157 CFMutableDictionaryRef changes = CFDictionaryCreateMutableForCFTypes(kCFAllocatorDefault);
158 SOSAccount* alice_account = CreateAccountForLocalChanges(CFSTR("Alice"), CFSTR("TestSource"));
159 SOSAccountAssertDSID(alice_account, cfdsid);
160 SOSAccount* bob_account = CreateAccountForLocalChanges(CFSTR("Bob"), CFSTR("TestSource"));
161 SOSAccountAssertDSID(bob_account, cfdsid);
163 CFDataRef alice_backup_key = CopyBackupKeyForString(CFSTR("Alice Backup Entropy"), &error);
164 CFDataRef bob_backup_key = CopyBackupKeyForString(CFSTR("Bob Backup Entropy"), &error);
167 ok(SOSTestStartCircleWithAccount(alice_account, changes, cfaccount, cfpassword), "Have Alice start a circle");
168 is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, NULL), 1, "updates");
169 ok(SOSTestJoinWithApproval(cfpassword, cfaccount, changes, alice_account, bob_account, KEEP_USERKEY, 2, false), "Bob Joins");
171 CFArrayRef peers = SOSAccountCopyPeers(alice_account, &error);
172 ok(peers && CFArrayGetCount(peers) == 2, "See two peers %@ (%@)", peers, error);
173 CFReleaseNull(peers);
175 if(recKeyFirst) registerRecoveryKeyNow(changes, alice_account, bob_account, pubKeyBytes, recKeyFirst);
178 is([alice_account.trust updateView:alice_account name:kTestView1 code:kSOSCCViewEnable err:&error], kSOSCCViewMember, "Enable view (%@)", error);
179 CFReleaseNull(error);
181 ok([alice_account.trust checkForRings:&error], "Alice_account is good");
182 CFReleaseNull(error);
184 ProcessChangesUntilNoChange(changes, alice_account, bob_account, NULL);
186 is([bob_account.trust updateView:bob_account name:kTestView1 code:kSOSCCViewEnable err:&error], kSOSCCViewMember, "Enable view (%@)", error);
187 CFReleaseNull(error);
189 ok([bob_account.trust checkForRings:&error], "Bob_account is good");
190 CFReleaseNull(error);
192 ok(SOSAccountSetBackupPublicKey_wTxn(alice_account, alice_backup_key, &error), "Set backup public key, alice (%@)", error);
193 CFReleaseNull(error);
195 ok([alice_account.trust checkForRings:&error], "Alice_account is good");
196 CFReleaseNull(error);
198 ok(SOSAccountSetBackupPublicKey_wTxn(bob_account, bob_backup_key, &error), "Set backup public key, bob (%@)", error);
199 CFReleaseNull(error);
201 ok([bob_account.trust checkForRings:&error], "Bob_account is good");
202 CFReleaseNull(error);
204 ok(SOSAccountIsMyPeerInBackupAndCurrentInView_wTxn(alice_account, kTestView1), "Is alice is in backup before sync?");
207 SOSBackupSliceKeyBagRef bskb = SOSAccountBackupSliceKeyBagForView_wTxn(alice_account, kTestView1, &error);
208 CFReleaseNull(error);
209 ok(!SOSBSKBHasRecoveryKey(bskb), "BSKB should not have recovery key");
213 ok([alice_account.trust checkForRings:&error], "Alice_account is good");
214 CFReleaseNull(error);
216 ok(SOSAccountIsMyPeerInBackupAndCurrentInView_wTxn(bob_account, kTestView1), "Is bob in the backup after sync? - 1");
218 ok([bob_account.trust checkForRings:&error], "Alice_account is good");
219 CFReleaseNull(error);
221 int passes = ProcessChangesUntilNoChange(changes, alice_account, bob_account, NULL);
222 ok(passes < 6, "updates"); // this was varying between 4 and 5 on different BATS runs - we just don't want it going crazy
224 ok([alice_account.trust checkForRings:&error], "Alice_account is good");
225 CFReleaseNull(error);
227 ok(SOSAccountIsMyPeerInBackupAndCurrentInView_wTxn(alice_account, kTestView1), "Is alice is in backup after sync?");
229 ok(SOSAccountIsMyPeerInBackupAndCurrentInView_wTxn(bob_account, kTestView1), "IS bob in the backup after sync");
232 //Bob leaves the circle
234 ok([bob_account.trust leaveCircle:bob_account err:&error], "Bob Leaves (%@)", error);
235 CFReleaseNull(error);
237 //Alice should kick Bob out of the backup!
238 is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, NULL), 2, "updates");
240 ok(SOSAccountIsMyPeerInBackupAndCurrentInView_wTxn(alice_account, kTestView1), "Bob left the circle, Alice is in the backup");
242 //ok(testAccountPersistence(alice_account), "Test Account->DER->Account Equivalence");
243 SOSAccountTrustClassic* bobTrust = bob_account.trust;
244 ok(!SOSAccountIsPeerInBackupAndCurrentInView_wTxn(alice_account, bobTrust.peerInfo, kTestView1), "Bob is still in the backup!");
246 //Bob gets back into the circle
247 ok(SOSTestJoinWithApproval(cfpassword, cfaccount, changes, alice_account, bob_account, KEEP_USERKEY, 2, false), "Bob Re-Joins");
250 is([bob_account.trust updateView:bob_account name:kTestView1 code:kSOSCCViewEnable err:&error], kSOSCCViewMember, "Enable view (%@)", error);
251 CFReleaseNull(error);
253 ok(!SOSAccountIsMyPeerInBackupAndCurrentInView_wTxn(bob_account, kTestView1), "Bob isn't in the backup yet");
255 ok(SOSAccountSetBackupPublicKey_wTxn(bob_account, bob_backup_key, &error), "Set backup public key, alice (%@)", error);
257 is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, NULL), 3, "updates");
260 //removing backup key for bob account
263 ok(SOSAccountRemoveBackupPublickey_wTxn(bob_account, &error), "Removing Bob's backup key (%@)", error);
264 int nchanges = (recKeyFirst) ? 2: 2;
265 is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, NULL), nchanges, "updates");
267 ok(!SOSAccountIsMyPeerInBackupAndCurrentInView_wTxn(bob_account, kTestView1), "Bob's backup key is in the backup - should not be so!");
268 ok(!SOSAccountIsPeerInBackupAndCurrentInView_wTxn(alice_account, bobTrust.peerInfo, kTestView1), "Bob is up to date in the backup!");
271 // Setting new backup public key for Bob
274 ok(SOSAccountSetBackupPublicKey_wTxn(bob_account, bob_backup_key, &error), "Set backup public key, alice (%@)", error);
275 CFReleaseNull(error);
277 is([bob_account.trust updateView:bob_account name:kTestView1 code:kSOSCCViewEnable err:&error], kSOSCCViewMember, "Enable view (%@)", error);
278 ok(SOSAccountNewBKSBForView(bob_account, kTestView1, &error), "Setting new backup public key for bob account failed: (%@)", error);
280 //bob is in his own backup
281 ok(SOSAccountIsMyPeerInBackupAndCurrentInView_wTxn(bob_account, kTestView1), "Bob's backup key is not in the backup");
282 //alice does not have bob in her backup
283 ok(!SOSAccountIsPeerInBackupAndCurrentInView_wTxn(alice_account, bobTrust.peerInfo, kTestView1), "Bob is up to date in the backup - should not be so!");
285 is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, NULL), 5, "updates");
287 ok(SOSAccountIsMyPeerInBackupAndCurrentInView_wTxn(bob_account, kTestView1), "Bob's backup key should be in the backup");
288 ok(SOSAccountIsMyPeerInBackupAndCurrentInView_wTxn(alice_account, kTestView1), "Alice is in the backup");
290 if(!recKeyFirst) registerRecoveryKeyNow(changes, alice_account, bob_account, pubKeyBytes, recKeyFirst);
292 ok(SOSAccountRecoveryKeyIsInBackupAndCurrentInView_wTxn(alice_account, kTestView1), "Recovery Key is also in the backup");
293 ok(SOSAccountRecoveryKeyIsInBackupAndCurrentInView_wTxn(bob_account, kTestView1), "Recovery Key is also in the backup");
295 SOSBackupSliceKeyBagRef bskb = SOSAccountBackupSliceKeyBagForView_wTxn(alice_account, kTestView1, &error);
296 CFReleaseNull(error);
298 ok(SOSBSKBHasRecoveryKey(bskb), "BSKB should have recovery key");
300 CFDataRef wrappingKey = CFStringCreateExternalRepresentation(kCFAllocatorDefault, sock_drawer_key, kCFStringEncodingUTF8, 0);
301 ok(wrappingKey, "Made wrapping key from with sock drawer key");
302 bskb_keybag_handle_t bskbHandle = SOSBSKBLoadAndUnlockWithWrappingSecret(bskb, wrappingKey, &error);
303 ok(bskbHandle, "Made bskbHandle with recover key");
304 ok(SOSAccountHasPublicKey(alice_account, &error), "Has Public Key" );
306 // Testing reset (Null) recoveryKey =========
309 CFReleaseNull(wrappingKey);
311 registerRecoveryKeyNow(changes, alice_account, bob_account, NULL, recKeyFirst);
313 ok(!SOSAccountRecoveryKeyIsInBackupAndCurrentInView_wTxn(alice_account, kTestView1), "Recovery Key is not in the backup");
314 ok(!SOSAccountRecoveryKeyIsInBackupAndCurrentInView_wTxn(bob_account, kTestView1), "Recovery Key is not in the backup");
316 bskb = SOSAccountBackupSliceKeyBagForView_wTxn(alice_account, kTestView1, &error);
317 CFReleaseNull(error);
319 ok(!SOSBSKBHasRecoveryKey(bskb), "BSKB should not have recovery key");
324 ok([alice_account.trust resetAccountToEmpty:alice_account transport:alice_account.circle_transport err:&error], "Reset circle to empty");
325 CFReleaseNull(error);
326 is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, NULL), 2, "updates");
327 ok(SOSAccountIsBackupRingEmpty(bob_account, kTestView1), "Bob should not be in the backup");
328 ok(SOSAccountIsBackupRingEmpty(alice_account, kTestView1), "Alice should not be in the backup");
330 CFReleaseNull(fullKeyBytes);
331 CFReleaseNull(pubKeyBytes);
334 CFReleaseNull(cfpassword);
335 CFReleaseNull(wrappingKey);
343 int secd_66_account_recovery(int argc, char *const *argv) {
346 secd_test_setup_temp_keychain(__FUNCTION__, NULL);