]> git.saurik.com Git - apple/security.git/blob - OSX/sec/securityd/Regressions/secd-66-account-recovery.m
Security-59306.11.20.tar.gz
[apple/security.git] / OSX / sec / 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 "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 <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(alice_account, kTestView1), "Is alice is in backup before sync?");
213
214 if(!recKeyFirst) {
215 SOSBackupSliceKeyBagRef bskb = SOSAccountBackupSliceKeyBagForView(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(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 is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, NULL), 4, "updates");
230
231
232 ok([alice_account.trust checkForRings:&error], "Alice_account is good");
233 CFReleaseNull(error);
234
235 ok(SOSAccountIsMyPeerInBackupAndCurrentInView(alice_account, kTestView1), "Is alice is in backup after sync?");
236
237 ok(SOSAccountIsMyPeerInBackupAndCurrentInView(bob_account, kTestView1), "IS bob in the backup after sync");
238
239 ok(!SOSAccountIsLastBackupPeer(alice_account, &error), "Alice is not last backup peer");
240 CFReleaseNull(error);
241
242 //
243 //Bob leaves the circle
244 //
245 ok([bob_account.trust leaveCircle:bob_account err:&error], "Bob Leaves (%@)", error);
246 CFReleaseNull(error);
247
248 //Alice should kick Bob out of the backup!
249 is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, NULL), 2, "updates");
250
251 ok(SOSAccountIsMyPeerInBackupAndCurrentInView(alice_account, kTestView1), "Bob left the circle, Alice is in the backup");
252
253 ok(SOSAccountIsLastBackupPeer(alice_account, &error), "Alice is last backup peer");
254 CFReleaseNull(error);
255 ok(!SOSAccountIsLastBackupPeer(bob_account, &error), "Bob is not last backup peer");
256 CFReleaseNull(error);
257
258 //ok(testAccountPersistence(alice_account), "Test Account->DER->Account Equivalence");
259 SOSAccountTrustClassic* bobTrust = bob_account.trust;
260 ok(!SOSAccountIsPeerInBackupAndCurrentInView(alice_account, bobTrust.peerInfo, kTestView1), "Bob is still in the backup!");
261
262 //Bob gets back into the circle
263 ok(SOSTestJoinWithApproval(cfpassword, cfaccount, changes, alice_account, bob_account, KEEP_USERKEY, 2, false), "Bob Re-Joins");
264
265 //enables view
266 is([bob_account.trust updateView:bob_account name:kTestView1 code:kSOSCCViewEnable err:&error], kSOSCCViewMember, "Enable view (%@)", error);
267 CFReleaseNull(error);
268
269 ok(!SOSAccountIsMyPeerInBackupAndCurrentInView(bob_account, kTestView1), "Bob isn't in the backup yet");
270
271 ok(!SOSAccountIsLastBackupPeer(alice_account, &error), "Alice is the not the last backup peer - Bob still registers as one");
272 CFReleaseNull(error);
273
274 ok(SOSAccountSetBackupPublicKey_wTxn(bob_account, bob_backup_key, &error), "Set backup public key, alice (%@)", error);
275
276 is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, NULL), 3, "updates");
277
278 ok(!SOSAccountIsLastBackupPeer(alice_account, &error), "Alice is not last backup peer");
279 CFReleaseNull(error);
280
281 //
282 //removing backup key for bob account
283 //
284
285 ok(SOSAccountRemoveBackupPublickey_wTxn(bob_account, &error), "Removing Bob's backup key (%@)", error);
286 int nchanges = (recKeyFirst) ? 2: 2;
287 is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, NULL), nchanges, "updates");
288
289 ok(!SOSAccountIsMyPeerInBackupAndCurrentInView(bob_account, kTestView1), "Bob's backup key is in the backup - should not be so!");
290 ok(!SOSAccountIsPeerInBackupAndCurrentInView(alice_account, bobTrust.peerInfo, kTestView1), "Bob is up to date in the backup!");
291
292 //
293 // Setting new backup public key for Bob
294 //
295
296 ok(SOSAccountSetBackupPublicKey_wTxn(bob_account, bob_backup_key, &error), "Set backup public key, alice (%@)", error);
297 CFReleaseNull(error);
298
299 is([bob_account.trust updateView:bob_account name:kTestView1 code:kSOSCCViewEnable err:&error], kSOSCCViewMember, "Enable view (%@)", error);
300 ok(SOSAccountNewBKSBForView(bob_account, kTestView1, &error), "Setting new backup public key for bob account failed: (%@)", error);
301
302 //bob is in his own backup
303 ok(SOSAccountIsMyPeerInBackupAndCurrentInView(bob_account, kTestView1), "Bob's backup key is not in the backup");
304 //alice does not have bob in her backup
305 ok(!SOSAccountIsPeerInBackupAndCurrentInView(alice_account, bobTrust.peerInfo, kTestView1), "Bob is up to date in the backup - should not be so!");
306
307 is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, NULL), 5, "updates");
308
309 ok(SOSAccountIsMyPeerInBackupAndCurrentInView(bob_account, kTestView1), "Bob's backup key should be in the backup");
310 ok(SOSAccountIsMyPeerInBackupAndCurrentInView(alice_account, kTestView1), "Alice is in the backup");
311
312 if(!recKeyFirst) registerRecoveryKeyNow(changes, alice_account, bob_account, pubKeyBytes, recKeyFirst);
313
314 ok(SOSAccountRecoveryKeyIsInBackupAndCurrentInView(alice_account, kTestView1), "Recovery Key is also in the backup");
315 ok(SOSAccountRecoveryKeyIsInBackupAndCurrentInView(bob_account, kTestView1), "Recovery Key is also in the backup");
316
317 SOSBackupSliceKeyBagRef bskb = SOSAccountBackupSliceKeyBagForView(alice_account, kTestView1, &error);
318 CFReleaseNull(error);
319
320 ok(SOSBSKBHasRecoveryKey(bskb), "BSKB should have recovery key");
321
322 CFDataRef wrappingKey = CFStringCreateExternalRepresentation(kCFAllocatorDefault, sock_drawer_key, kCFStringEncodingUTF8, 0);
323 ok(wrappingKey, "Made wrapping key from with sock drawer key");
324 bskb_keybag_handle_t bskbHandle = SOSBSKBLoadAndUnlockWithWrappingSecret(bskb, wrappingKey, &error);
325 ok(bskbHandle, "Made bskbHandle with recover key");
326 ok(SOSAccountHasPublicKey(alice_account, &error), "Has Public Key" );
327
328 // Testing reset (Null) recoveryKey =========
329
330 CFReleaseNull(bskb);
331 CFReleaseNull(wrappingKey);
332
333 registerRecoveryKeyNow(changes, alice_account, bob_account, NULL, recKeyFirst);
334
335 ok(!SOSAccountRecoveryKeyIsInBackupAndCurrentInView(alice_account, kTestView1), "Recovery Key is not in the backup");
336 ok(!SOSAccountRecoveryKeyIsInBackupAndCurrentInView(bob_account, kTestView1), "Recovery Key is not in the backup");
337
338 bskb = SOSAccountBackupSliceKeyBagForView(alice_account, kTestView1, &error);
339 CFReleaseNull(error);
340
341 ok(!SOSBSKBHasRecoveryKey(bskb), "BSKB should not have recovery key");
342
343 //=========
344
345
346 ok([alice_account.trust resetAccountToEmpty:alice_account transport:alice_account.circle_transport err:&error], "Reset circle to empty");
347 CFReleaseNull(error);
348 is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, NULL), 2, "updates");
349 ok(SOSAccountIsBackupRingEmpty(bob_account, kTestView1), "Bob should not be in the backup");
350 ok(SOSAccountIsBackupRingEmpty(alice_account, kTestView1), "Alice should not be in the backup");
351
352 CFReleaseNull(fullKeyBytes);
353 CFReleaseNull(pubKeyBytes);
354 CFReleaseNull(bskb);
355
356 CFReleaseNull(cfpassword);
357 CFReleaseNull(wrappingKey);
358 bob_account = nil;
359 alice_account = nil;
360
361 SOSTestCleanup();
362 }
363
364 int secd_66_account_recovery(int argc, char *const *argv) {
365 plan_tests(278);
366
367 secd_test_setup_temp_keychain(__FUNCTION__, NULL);
368
369 tests(true);
370 tests(false);
371
372 return 0;
373 }
374
375 #endif