]> git.saurik.com Git - apple/security.git/blob - keychain/securityd/Regressions/secd-66-account-recovery.m
Security-59306.101.1.tar.gz
[apple/security.git] / keychain / securityd / Regressions / secd-66-account-recovery.m
1 //
2 // secd-66-account-recovery.c
3 // Security
4 //
5 // Created by Richard Murphy on 10/5/16.
6 //
7 //
8
9 #include <stdio.h>
10
11 /*
12 * Copyright (c) 2012-2014 Apple Inc. All Rights Reserved.
13 *
14 * @APPLE_LICENSE_HEADER_START@
15 *
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
21 * file.
22 *
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.
30 *
31 * @APPLE_LICENSE_HEADER_END@
32 */
33
34
35
36
37 #include <Security/SecBase.h>
38 #include <Security/SecItem.h>
39
40 #include <CoreFoundation/CFDictionary.h>
41
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"
49
50 #include <stdlib.h>
51 #include <unistd.h>
52
53 #include "secd_regressions.h"
54 #include "keychain/SecureObjectSync/Regressions/SOSTestDataSource.h"
55
56 #include "SOSRegressionUtilities.h"
57 #include <Security/SecRecoveryKey.h>
58
59 #include <utilities/SecCFWrappers.h>
60 #include <Security/SecKeyPriv.h>
61
62 #include "keychain/securityd/SOSCloudCircleServer.h"
63 #include "SecdTestKeychainUtilities.h"
64
65 #if TARGET_OS_SIMULATOR
66
67 int secd_66_account_recovery(int argc, char *const *argv) {
68 plan_tests(1);
69 secd_test_setup_temp_keychain(__FUNCTION__, NULL);
70 return 0;
71 }
72
73 #else
74
75 #include "SOSAccountTesting.h"
76
77 static CFDataRef CopyBackupKeyForString(CFStringRef string, CFErrorRef *error)
78 {
79 __block CFDataRef result = NULL;
80 CFStringPerformWithUTF8CFData(string, ^(CFDataRef stringAsData) {
81 result = SOSCopyDeviceBackupPublicKey(stringAsData, error);
82 });
83 return result;
84 }
85
86
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);
91 }];
92 return result;
93 }
94
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);
99 }];
100 return result;
101 }
102
103 // 6 test cases
104 static void registerRecoveryKeyNow(CFMutableDictionaryRef changes, SOSAccount* registrar, SOSAccount* observer, CFDataRef recoveryPub, bool recKeyFirst) {
105 CFErrorRef error = NULL;
106
107 is(ProcessChangesUntilNoChange(changes, registrar, observer, NULL), 1, "updates");
108
109 if(recoveryPub) {
110 ok(SOSAccountSetRecoveryKey_wTxn(registrar, recoveryPub, &error), "Set Recovery Key");
111 CFReleaseNull(error);
112 } else {
113 ok(SOSAccountSOSAccountRemoveRecoveryKey_wTxn(registrar, &error), "Clear Recovery Key");
114 CFReleaseNull(error);
115 }
116 ok(error == NULL, "Error shouldn't be %@", error);
117 CFReleaseNull(error);
118 ProcessChangesUntilNoChange(changes, registrar, observer, NULL);
119
120 CFDataRef registrar_recKey = SOSAccountCopyRecoveryPublic(kCFAllocatorDefault, registrar, &error);
121 CFReleaseNull(error);
122 CFDataRef observer_recKey = SOSAccountCopyRecoveryPublic(kCFAllocatorDefault, observer, &error);
123 CFReleaseNull(error);
124
125 if(recoveryPub) {
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");
130 } else {
131 ok((!registrar_recKey && !observer_recKey), "recKeys are NULL");
132 }
133 CFReleaseNull(observer_recKey);
134 CFReleaseNull(registrar_recKey);
135 }
136
137 static void tests(bool recKeyFirst)
138 {
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;
144
145 sRecKey = SecRKCreateRecoveryKeyWithError((__bridge NSString*)sock_drawer_key, NULL);
146 ok(sRecKey, "Create SecRecoveryKey from String");
147 if(sRecKey) {
148 fullKeyBytes = (__bridge CFDataRef)(SecRKCopyBackupFullKey(sRecKey));
149 pubKeyBytes = (__bridge CFDataRef)(SecRKCopyBackupPublicKey(sRecKey));
150 ok(fullKeyBytes && pubKeyBytes, "Got KeyPair from SecRecoveryKey");
151 }
152 if(!(fullKeyBytes && pubKeyBytes)) {
153 diag("Cannot Proceed - couldn't make usable recoveryKey from sock-drawer-key");
154 CFReleaseNull(fullKeyBytes);
155 CFReleaseNull(pubKeyBytes);
156 return;
157 }
158
159 CFDataRef cfpassword = CFDataCreate(NULL, (uint8_t *) "FooFooFoo", 10);
160 CFStringRef cfaccount = CFSTR("test@test.org");
161 CFStringRef cfdsid = CFSTR("DSIDFooFoo");
162
163 secd_test_setup_testviews(); // for running this test solo
164
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);
170
171 CFDataRef alice_backup_key = CopyBackupKeyForString(CFSTR("Alice Backup Entropy"), &error);
172 CFDataRef bob_backup_key = CopyBackupKeyForString(CFSTR("Bob Backup Entropy"), &error);
173
174 // Start Circle
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");
178
179 CFArrayRef peers = SOSAccountCopyPeers(alice_account, &error);
180 ok(peers && CFArrayGetCount(peers) == 2, "See two peers %@ (%@)", peers, error);
181 CFReleaseNull(peers);
182
183 if(recKeyFirst) registerRecoveryKeyNow(changes, alice_account, bob_account, pubKeyBytes, recKeyFirst);
184
185
186 is([alice_account.trust updateView:alice_account name:kTestView1 code:kSOSCCViewEnable err:&error], kSOSCCViewMember, "Enable view (%@)", error);
187 CFReleaseNull(error);
188
189 ok([alice_account.trust checkForRings:&error], "Alice_account is good");
190 CFReleaseNull(error);
191
192 ProcessChangesUntilNoChange(changes, alice_account, bob_account, NULL);
193
194 is([bob_account.trust updateView:bob_account name:kTestView1 code:kSOSCCViewEnable err:&error], kSOSCCViewMember, "Enable view (%@)", error);
195 CFReleaseNull(error);
196
197 ok([bob_account.trust checkForRings:&error], "Bob_account is good");
198 CFReleaseNull(error);
199
200 ok(SOSAccountSetBackupPublicKey_wTxn(alice_account, alice_backup_key, &error), "Set backup public key, alice (%@)", error);
201 CFReleaseNull(error);
202
203 ok([alice_account.trust checkForRings:&error], "Alice_account is good");
204 CFReleaseNull(error);
205
206 ok(SOSAccountSetBackupPublicKey_wTxn(bob_account, bob_backup_key, &error), "Set backup public key, bob (%@)", error);
207 CFReleaseNull(error);
208
209 ok([bob_account.trust checkForRings:&error], "Bob_account is good");
210 CFReleaseNull(error);
211
212 ok(SOSAccountIsMyPeerInBackupAndCurrentInView_wTxn(alice_account, kTestView1), "Is alice is in backup before sync?");
213
214 if(!recKeyFirst) {
215 SOSBackupSliceKeyBagRef bskb = SOSAccountBackupSliceKeyBagForView_wTxn(alice_account, kTestView1, &error);
216 CFReleaseNull(error);
217 ok(!SOSBSKBHasRecoveryKey(bskb), "BSKB should not have recovery key");
218 CFReleaseNull(bskb);
219 }
220
221 ok([alice_account.trust checkForRings:&error], "Alice_account is good");
222 CFReleaseNull(error);
223
224 ok(SOSAccountIsMyPeerInBackupAndCurrentInView_wTxn(bob_account, kTestView1), "Is bob in the backup after sync? - 1");
225
226 ok([bob_account.trust checkForRings:&error], "Alice_account is good");
227 CFReleaseNull(error);
228
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
231
232 ok([alice_account.trust checkForRings:&error], "Alice_account is good");
233 CFReleaseNull(error);
234
235 ok(SOSAccountIsMyPeerInBackupAndCurrentInView_wTxn(alice_account, kTestView1), "Is alice is in backup after sync?");
236
237 ok(SOSAccountIsMyPeerInBackupAndCurrentInView_wTxn(bob_account, kTestView1), "IS bob in the backup after sync");
238
239 //
240 //Bob leaves the circle
241 //
242 ok([bob_account.trust leaveCircle:bob_account err:&error], "Bob Leaves (%@)", error);
243 CFReleaseNull(error);
244
245 //Alice should kick Bob out of the backup!
246 is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, NULL), 2, "updates");
247
248 ok(SOSAccountIsMyPeerInBackupAndCurrentInView_wTxn(alice_account, kTestView1), "Bob left the circle, Alice is in the backup");
249
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!");
253
254 //Bob gets back into the circle
255 ok(SOSTestJoinWithApproval(cfpassword, cfaccount, changes, alice_account, bob_account, KEEP_USERKEY, 2, false), "Bob Re-Joins");
256
257 //enables view
258 is([bob_account.trust updateView:bob_account name:kTestView1 code:kSOSCCViewEnable err:&error], kSOSCCViewMember, "Enable view (%@)", error);
259 CFReleaseNull(error);
260
261 ok(!SOSAccountIsMyPeerInBackupAndCurrentInView_wTxn(bob_account, kTestView1), "Bob isn't in the backup yet");
262
263 ok(SOSAccountSetBackupPublicKey_wTxn(bob_account, bob_backup_key, &error), "Set backup public key, alice (%@)", error);
264
265 is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, NULL), 3, "updates");
266
267 //
268 //removing backup key for bob account
269 //
270
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");
274
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!");
277
278 //
279 // Setting new backup public key for Bob
280 //
281
282 ok(SOSAccountSetBackupPublicKey_wTxn(bob_account, bob_backup_key, &error), "Set backup public key, alice (%@)", error);
283 CFReleaseNull(error);
284
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);
287
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!");
292
293 is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, NULL), 5, "updates");
294
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");
297
298 if(!recKeyFirst) registerRecoveryKeyNow(changes, alice_account, bob_account, pubKeyBytes, recKeyFirst);
299
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");
302
303 SOSBackupSliceKeyBagRef bskb = SOSAccountBackupSliceKeyBagForView_wTxn(alice_account, kTestView1, &error);
304 CFReleaseNull(error);
305
306 ok(SOSBSKBHasRecoveryKey(bskb), "BSKB should have recovery key");
307
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" );
313
314 // Testing reset (Null) recoveryKey =========
315
316 CFReleaseNull(bskb);
317 CFReleaseNull(wrappingKey);
318
319 registerRecoveryKeyNow(changes, alice_account, bob_account, NULL, recKeyFirst);
320
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");
323
324 bskb = SOSAccountBackupSliceKeyBagForView_wTxn(alice_account, kTestView1, &error);
325 CFReleaseNull(error);
326
327 ok(!SOSBSKBHasRecoveryKey(bskb), "BSKB should not have recovery key");
328
329 //=========
330
331
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");
337
338 CFReleaseNull(fullKeyBytes);
339 CFReleaseNull(pubKeyBytes);
340 CFReleaseNull(bskb);
341
342 CFReleaseNull(cfpassword);
343 CFReleaseNull(wrappingKey);
344 bob_account = nil;
345 alice_account = nil;
346
347 SOSTestCleanup();
348 }
349
350 int secd_66_account_recovery(int argc, char *const *argv) {
351 plan_tests(281);
352
353 secd_test_setup_temp_keychain(__FUNCTION__, NULL);
354
355 tests(true);
356 tests(false);
357
358 return 0;
359 }
360
361 #endif