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 <Security/SecureObjectSync/SOSAccount.h>
43 #include <Security/SecureObjectSync/SOSCloudCircle.h>
44 #include <Security/SecureObjectSync/SOSInternal.h>
45 #include <Security/SecureObjectSync/SOSUserKeygen.h>
46 #include <Security/SecureObjectSync/SOSTransport.h>
47 #include <Security/SecureObjectSync/SOSViews.h>
52 #include "secd_regressions.h"
53 #include "SOSTestDataSource.h"
55 #include "SOSRegressionUtilities.h"
56 #include "SecRecoveryKey.h"
58 #include <utilities/SecCFWrappers.h>
59 #include <Security/SecKeyPriv.h>
61 #include <securityd/SOSCloudCircleServer.h>
62 #include "SecdTestKeychainUtilities.h"
64 #if TARGET_IPHONE_SIMULATOR
66 int secd_66_account_recovery(int argc
, char *const *argv
) {
68 secd_test_setup_temp_keychain(__FUNCTION__
, NULL
);
74 #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 void registerRecoveryKeyNow(CFMutableDictionaryRef changes
, SOSAccountRef registrar
, SOSAccountRef observer
, CFDataRef recoveryPub
, bool recKeyFirst
) {
88 CFErrorRef error
= NULL
;
90 ok(SOSAccountSetRecoveryKey(registrar
, recoveryPub
, &error
), "Set Recovery Key");
93 int nchanges
= (recKeyFirst
) ? 3: 4;
94 is(ProcessChangesUntilNoChange(changes
, registrar
, observer
, NULL
), nchanges
, "updates");
96 CFDataRef registrar_recKey
= SOSAccountCopyRecoveryPublic(kCFAllocatorDefault
, registrar
, &error
);
97 ok(registrar_recKey
, "Registrar retrieved recKey");
100 CFDataRef observer_recKey
= SOSAccountCopyRecoveryPublic(kCFAllocatorDefault
, observer
, &error
);
101 ok(observer_recKey
, "Observer retrieved recKey");
102 CFReleaseNull(error
);
104 ok(CFEqualSafe(registrar_recKey
, observer_recKey
), "recKeys are the same");
105 ok(CFEqualSafe(registrar_recKey
, recoveryPub
), "recKeys are as expected");
106 CFReleaseNull(observer_recKey
);
107 CFReleaseNull(registrar_recKey
);
110 static void tests(bool recKeyFirst
)
112 __block CFErrorRef error
= NULL
;
113 CFStringRef sock_drawer_key
= CFSTR("AAAA-AAAA-AAAA-AAAA-AAAA-AAAA-AAGW");
114 SecRecoveryKey
*sRecKey
= NULL
;
115 CFDataRef fullKeyBytes
= NULL
;
116 CFDataRef pubKeyBytes
= NULL
;
118 sRecKey
= SecRKCreateRecoveryKey(sock_drawer_key
);
119 ok(sRecKey
, "Create SecRecoveryKey from String");
121 fullKeyBytes
= SecRKCopyBackupFullKey(sRecKey
);
122 pubKeyBytes
= SecRKCopyBackupPublicKey(sRecKey
);
123 ok(fullKeyBytes
&& pubKeyBytes
, "Got KeyPair from SecRecoveryKey");
125 if(!(fullKeyBytes
&& pubKeyBytes
)) {
126 diag("Cannot Proceed - couldn't make usable recoveryKey from sock-drawer-key");
127 CFReleaseNull(fullKeyBytes
);
128 CFReleaseNull(pubKeyBytes
);
132 CFDataRef cfpassword
= CFDataCreate(NULL
, (uint8_t *) "FooFooFoo", 10);
133 CFStringRef cfaccount
= CFSTR("test@test.org");
134 CFStringRef cfdsid
= CFSTR("DSIDFooFoo");
136 secd_test_setup_testviews(); // for running this test solo
138 CFMutableDictionaryRef changes
= CFDictionaryCreateMutableForCFTypes(kCFAllocatorDefault
);
139 SOSAccountRef alice_account
= CreateAccountForLocalChanges(CFSTR("Alice"), CFSTR("TestSource"));
140 SOSAccountAssertDSID(alice_account
, cfdsid
);
141 SOSAccountRef bob_account
= CreateAccountForLocalChanges(CFSTR("Bob"), CFSTR("TestSource"));
142 SOSAccountAssertDSID(bob_account
, cfdsid
);
144 CFDataRef alice_backup_key
= CopyBackupKeyForString(CFSTR("Alice Backup Entropy"), &error
);
145 CFDataRef bob_backup_key
= CopyBackupKeyForString(CFSTR("Bob Backup Entropy"), &error
);
148 ok(SOSTestStartCircleWithAccount(alice_account
, changes
, cfaccount
, cfpassword
), "Have Alice start a circle");
149 is(ProcessChangesUntilNoChange(changes
, alice_account
, bob_account
, NULL
), 1, "updates");
150 ok(SOSTestJoinWithApproval(cfpassword
, cfaccount
, changes
, alice_account
, bob_account
, KEEP_USERKEY
, 2, false), "Bob Joins");
152 CFArrayRef peers
= SOSAccountCopyPeers(alice_account
, &error
);
153 ok(peers
&& CFArrayGetCount(peers
) == 2, "See two peers %@ (%@)", peers
, error
);
154 CFReleaseNull(peers
);
156 if(recKeyFirst
) registerRecoveryKeyNow(changes
, alice_account
, bob_account
, pubKeyBytes
, recKeyFirst
);
158 is(SOSAccountUpdateView(alice_account
, kTestView1
, kSOSCCViewEnable
, &error
), kSOSCCViewMember
, "Enable view (%@)", error
);
159 CFReleaseNull(error
);
161 ok(SOSAccountCheckForRings(alice_account
, &error
), "Alice_account is good");
162 CFReleaseNull(error
);
164 is(SOSAccountUpdateView(bob_account
, kTestView1
, kSOSCCViewEnable
, &error
), kSOSCCViewMember
, "Enable view (%@)", error
);
165 CFReleaseNull(error
);
167 ok(SOSAccountCheckForRings(bob_account
, &error
), "Bob_account is good");
168 CFReleaseNull(error
);
170 ok(SOSAccountSetBackupPublicKey_wTxn(alice_account
, alice_backup_key
, &error
), "Set backup public key, alice (%@)", error
);
171 CFReleaseNull(error
);
173 ok(SOSAccountCheckForRings(alice_account
, &error
), "Alice_account is good");
174 CFReleaseNull(error
);
176 ok(SOSAccountSetBackupPublicKey_wTxn(bob_account
, bob_backup_key
, &error
), "Set backup public key, bob (%@)", error
);
177 CFReleaseNull(error
);
179 ok(SOSAccountCheckForRings(bob_account
, &error
), "Bob_account is good");
180 CFReleaseNull(error
);
182 ok(SOSAccountIsMyPeerInBackupAndCurrentInView(alice_account
, kTestView1
), "Is alice is in backup before sync?");
185 SOSBackupSliceKeyBagRef bskb
= SOSAccountBackupSliceKeyBagForView(alice_account
, kTestView1
, &error
);
186 CFReleaseNull(error
);
187 ok(!SOSBSKBHasRecoveryKey(bskb
), "BSKB should not have recovery key");
191 ok(SOSAccountCheckForRings(alice_account
, &error
), "Alice_account is good");
192 CFReleaseNull(error
);
194 ok(SOSAccountIsMyPeerInBackupAndCurrentInView(bob_account
, kTestView1
), "Is bob in the backup after sync? - 1");
196 ok(SOSAccountCheckForRings(bob_account
, &error
), "Alice_account is good");
197 CFReleaseNull(error
);
199 is(ProcessChangesUntilNoChange(changes
, alice_account
, bob_account
, NULL
), 4, "updates");
202 ok(SOSAccountCheckForRings(alice_account
, &error
), "Alice_account is good");
203 CFReleaseNull(error
);
205 ok(SOSAccountIsMyPeerInBackupAndCurrentInView(alice_account
, kTestView1
), "Is alice is in backup after sync?");
207 ok(SOSAccountIsMyPeerInBackupAndCurrentInView(bob_account
, kTestView1
), "IS bob in the backup after sync");
209 ok(!SOSAccountIsLastBackupPeer(alice_account
, &error
), "Alice is not last backup peer");
210 CFReleaseNull(error
);
213 //Bob leaves the circle
215 ok(SOSAccountLeaveCircle(bob_account
, &error
), "Bob Leaves (%@)", error
);
216 CFReleaseNull(error
);
218 //Alice should kick Bob out of the backup!
219 is(ProcessChangesUntilNoChange(changes
, alice_account
, bob_account
, NULL
), 2, "updates");
221 ok(SOSAccountIsMyPeerInBackupAndCurrentInView(alice_account
, kTestView1
), "Bob left the circle, Alice is not in the backup");
223 ok(SOSAccountIsLastBackupPeer(alice_account
, &error
), "Alice is last backup peer");
224 CFReleaseNull(error
);
225 ok(!SOSAccountIsLastBackupPeer(bob_account
, &error
), "Bob is not last backup peer");
226 CFReleaseNull(error
);
228 ok(testAccountPersistence(alice_account
), "Test Account->DER->Account Equivalence");
230 ok(!SOSAccountIsPeerInBackupAndCurrentInView(alice_account
, SOSFullPeerInfoGetPeerInfo(bob_account
->my_identity
), kTestView1
), "Bob is still in the backup!");
232 //Bob gets back into the circle
233 ok(SOSTestJoinWithApproval(cfpassword
, cfaccount
, changes
, alice_account
, bob_account
, KEEP_USERKEY
, 2, false), "Bob Re-Joins");
236 is(SOSAccountUpdateView(bob_account
, kTestView1
, kSOSCCViewEnable
, &error
), kSOSCCViewMember
, "Enable view (%@)", error
);
237 CFReleaseNull(error
);
239 ok(!SOSAccountIsMyPeerInBackupAndCurrentInView(bob_account
, kTestView1
), "Bob isn't in the backup yet");
241 ok(!SOSAccountIsLastBackupPeer(alice_account
, &error
), "Alice is not last backup peer - Bob still registers as one");
242 CFReleaseNull(error
);
244 ok(SOSAccountSetBackupPublicKey_wTxn(bob_account
, bob_backup_key
, &error
), "Set backup public key, alice (%@)", error
);
246 is(ProcessChangesUntilNoChange(changes
, alice_account
, bob_account
, NULL
), 1, "updates");
248 ok(!SOSAccountIsLastBackupPeer(alice_account
, &error
), "Alice is not last backup peer");
249 CFReleaseNull(error
);
252 //removing backup key for bob account
255 ok(SOSAccountRemoveBackupPublickey_wTxn(bob_account
, &error
), "Removing Bob's backup key (%@)", error
);
256 int nchanges
= (recKeyFirst
) ? 4: 3;
257 is(ProcessChangesUntilNoChange(changes
, alice_account
, bob_account
, NULL
), nchanges
, "updates");
259 ok(!SOSAccountIsMyPeerInBackupAndCurrentInView(bob_account
, kTestView1
), "Bob's backup key is in the backup - should not be so!");
260 ok(!SOSAccountIsPeerInBackupAndCurrentInView(alice_account
, SOSFullPeerInfoGetPeerInfo(bob_account
->my_identity
), kTestView1
), "Bob is up to date in the backup!");
263 // Setting new backup public key for Bob
266 ok(SOSAccountSetBackupPublicKey_wTxn(bob_account
, bob_backup_key
, &error
), "Set backup public key, alice (%@)", error
);
267 CFReleaseNull(error
);
269 is(SOSAccountUpdateView(bob_account
, kTestView1
, kSOSCCViewEnable
, &error
), kSOSCCViewMember
, "Enable view (%@)", error
);
270 ok(SOSAccountNewBKSBForView(bob_account
, kTestView1
, &error
), "Setting new backup public key for bob account failed: (%@)", error
);
272 //bob is in his own backup
273 ok(SOSAccountIsMyPeerInBackupAndCurrentInView(bob_account
, kTestView1
), "Bob's backup key is not in the backup");
274 //alice does not have bob in her backup
275 ok(!SOSAccountIsPeerInBackupAndCurrentInView(alice_account
, SOSFullPeerInfoGetPeerInfo(bob_account
->my_identity
), kTestView1
), "Bob is up to date in the backup - should not be so!");
277 is(ProcessChangesUntilNoChange(changes
, alice_account
, bob_account
, NULL
), 5, "updates");
279 ok(SOSAccountIsMyPeerInBackupAndCurrentInView(bob_account
, kTestView1
), "Bob's backup key should be in the backup");
280 ok(SOSAccountIsMyPeerInBackupAndCurrentInView(alice_account
, kTestView1
), "Alice is in the backup");
282 if(!recKeyFirst
) registerRecoveryKeyNow(changes
, alice_account
, bob_account
, pubKeyBytes
, recKeyFirst
);
284 ok(SOSAccountRecoveryKeyIsInBackupAndCurrentInView(alice_account
, kTestView1
), "Recovery Key is also in the backup");
285 ok(SOSAccountRecoveryKeyIsInBackupAndCurrentInView(bob_account
, kTestView1
), "Recovery Key is also in the backup");
287 SOSBackupSliceKeyBagRef bskb
= SOSAccountBackupSliceKeyBagForView(alice_account
, kTestView1
, &error
);
288 CFReleaseNull(error
);
290 ok(SOSBSKBHasRecoveryKey(bskb
), "BSKB should have recovery key");
292 CFDataRef wrappingKey
= CFStringCreateExternalRepresentation(kCFAllocatorDefault
, sock_drawer_key
, kCFStringEncodingUTF8
, 0);
293 ok(wrappingKey
, "Made wrapping key from with sock drawer key");
294 bskb_keybag_handle_t bskbHandle
= SOSBSKBLoadAndUnlockWithWrappingSecret(bskb
, wrappingKey
, &error
);
295 ok(bskbHandle
, "Made bskbHandle with recover key");
297 ok(SOSAccountResetToEmpty(alice_account
, &error
), "Reset circle to empty");
298 CFReleaseNull(error
);
299 is(ProcessChangesUntilNoChange(changes
, alice_account
, bob_account
, NULL
), 2, "updates");
300 ok(SOSAccountIsBackupRingEmpty(bob_account
, kTestView1
), "Bob should not be in the backup");
301 ok(SOSAccountIsBackupRingEmpty(alice_account
, kTestView1
), "Alice should not be in the backup");
303 CFReleaseNull(fullKeyBytes
);
304 CFReleaseNull(pubKeyBytes
);
307 CFReleaseNull(bob_account
);
308 CFReleaseNull(alice_account
);
309 CFReleaseNull(cfpassword
);
310 CFReleaseNull(wrappingKey
);
315 int secd_66_account_recovery(int argc
, char *const *argv
) {
318 secd_test_setup_temp_keychain(__FUNCTION__
, NULL
);