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