]> git.saurik.com Git - apple/security.git/blob - OSX/sec/securityd/Regressions/secd-66-account-recovery.c
Security-57740.51.3.tar.gz
[apple/security.git] / OSX / sec / securityd / Regressions / secd-66-account-recovery.c
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
49 #include <stdlib.h>
50 #include <unistd.h>
51
52 #include "secd_regressions.h"
53 #include "SOSTestDataSource.h"
54
55 #include "SOSRegressionUtilities.h"
56 #include "SecRecoveryKey.h"
57
58 #include <utilities/SecCFWrappers.h>
59 #include <Security/SecKeyPriv.h>
60
61 #include <securityd/SOSCloudCircleServer.h>
62 #include "SecdTestKeychainUtilities.h"
63
64 #if TARGET_IPHONE_SIMULATOR
65
66 int secd_66_account_recovery(int argc, char *const *argv) {
67 plan_tests(1);
68 secd_test_setup_temp_keychain(__FUNCTION__, NULL);
69 return 0;
70 }
71
72 #else
73
74 #include "SOSAccountTesting.h"
75
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 // 6 test cases
87 static void registerRecoveryKeyNow(CFMutableDictionaryRef changes, SOSAccountRef registrar, SOSAccountRef observer, CFDataRef recoveryPub, bool recKeyFirst) {
88 CFErrorRef error = NULL;
89
90 ok(SOSAccountSetRecoveryKey(registrar, recoveryPub, &error), "Set Recovery Key");
91 CFReleaseNull(error);
92
93 int nchanges = (recKeyFirst) ? 3: 4;
94 is(ProcessChangesUntilNoChange(changes, registrar, observer, NULL), nchanges, "updates");
95
96 CFDataRef registrar_recKey = SOSAccountCopyRecoveryPublic(kCFAllocatorDefault, registrar, &error);
97 ok(registrar_recKey, "Registrar retrieved recKey");
98 CFReleaseNull(error);
99
100 CFDataRef observer_recKey = SOSAccountCopyRecoveryPublic(kCFAllocatorDefault, observer, &error);
101 ok(observer_recKey, "Observer retrieved recKey");
102 CFReleaseNull(error);
103
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);
108 }
109
110 static void tests(bool recKeyFirst)
111 {
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;
117
118 sRecKey = SecRKCreateRecoveryKey(sock_drawer_key);
119 ok(sRecKey, "Create SecRecoveryKey from String");
120 if(sRecKey) {
121 fullKeyBytes = SecRKCopyBackupFullKey(sRecKey);
122 pubKeyBytes = SecRKCopyBackupPublicKey(sRecKey);
123 ok(fullKeyBytes && pubKeyBytes, "Got KeyPair from SecRecoveryKey");
124 }
125 if(!(fullKeyBytes && pubKeyBytes)) {
126 diag("Cannot Proceed - couldn't make usable recoveryKey from sock-drawer-key");
127 CFReleaseNull(fullKeyBytes);
128 CFReleaseNull(pubKeyBytes);
129 return;
130 }
131
132 CFDataRef cfpassword = CFDataCreate(NULL, (uint8_t *) "FooFooFoo", 10);
133 CFStringRef cfaccount = CFSTR("test@test.org");
134 CFStringRef cfdsid = CFSTR("DSIDFooFoo");
135
136 secd_test_setup_testviews(); // for running this test solo
137
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);
143
144 CFDataRef alice_backup_key = CopyBackupKeyForString(CFSTR("Alice Backup Entropy"), &error);
145 CFDataRef bob_backup_key = CopyBackupKeyForString(CFSTR("Bob Backup Entropy"), &error);
146
147 // Start Circle
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");
151
152 CFArrayRef peers = SOSAccountCopyPeers(alice_account, &error);
153 ok(peers && CFArrayGetCount(peers) == 2, "See two peers %@ (%@)", peers, error);
154 CFReleaseNull(peers);
155
156 if(recKeyFirst) registerRecoveryKeyNow(changes, alice_account, bob_account, pubKeyBytes, recKeyFirst);
157
158 is(SOSAccountUpdateView(alice_account, kTestView1, kSOSCCViewEnable, &error), kSOSCCViewMember, "Enable view (%@)", error);
159 CFReleaseNull(error);
160
161 ok(SOSAccountCheckForRings(alice_account, &error), "Alice_account is good");
162 CFReleaseNull(error);
163
164 is(SOSAccountUpdateView(bob_account, kTestView1, kSOSCCViewEnable, &error), kSOSCCViewMember, "Enable view (%@)", error);
165 CFReleaseNull(error);
166
167 ok(SOSAccountCheckForRings(bob_account, &error), "Bob_account is good");
168 CFReleaseNull(error);
169
170 ok(SOSAccountSetBackupPublicKey_wTxn(alice_account, alice_backup_key, &error), "Set backup public key, alice (%@)", error);
171 CFReleaseNull(error);
172
173 ok(SOSAccountCheckForRings(alice_account, &error), "Alice_account is good");
174 CFReleaseNull(error);
175
176 ok(SOSAccountSetBackupPublicKey_wTxn(bob_account, bob_backup_key, &error), "Set backup public key, bob (%@)", error);
177 CFReleaseNull(error);
178
179 ok(SOSAccountCheckForRings(bob_account, &error), "Bob_account is good");
180 CFReleaseNull(error);
181
182 ok(SOSAccountIsMyPeerInBackupAndCurrentInView(alice_account, kTestView1), "Is alice is in backup before sync?");
183
184 if(!recKeyFirst) {
185 SOSBackupSliceKeyBagRef bskb = SOSAccountBackupSliceKeyBagForView(alice_account, kTestView1, &error);
186 CFReleaseNull(error);
187 ok(!SOSBSKBHasRecoveryKey(bskb), "BSKB should not have recovery key");
188 CFReleaseNull(bskb);
189 }
190
191 ok(SOSAccountCheckForRings(alice_account, &error), "Alice_account is good");
192 CFReleaseNull(error);
193
194 ok(SOSAccountIsMyPeerInBackupAndCurrentInView(bob_account, kTestView1), "Is bob in the backup after sync? - 1");
195
196 ok(SOSAccountCheckForRings(bob_account, &error), "Alice_account is good");
197 CFReleaseNull(error);
198
199 is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, NULL), 4, "updates");
200
201
202 ok(SOSAccountCheckForRings(alice_account, &error), "Alice_account is good");
203 CFReleaseNull(error);
204
205 ok(SOSAccountIsMyPeerInBackupAndCurrentInView(alice_account, kTestView1), "Is alice is in backup after sync?");
206
207 ok(SOSAccountIsMyPeerInBackupAndCurrentInView(bob_account, kTestView1), "IS bob in the backup after sync");
208
209 ok(!SOSAccountIsLastBackupPeer(alice_account, &error), "Alice is not last backup peer");
210 CFReleaseNull(error);
211
212 //
213 //Bob leaves the circle
214 //
215 ok(SOSAccountLeaveCircle(bob_account, &error), "Bob Leaves (%@)", error);
216 CFReleaseNull(error);
217
218 //Alice should kick Bob out of the backup!
219 is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, NULL), 2, "updates");
220
221 ok(SOSAccountIsMyPeerInBackupAndCurrentInView(alice_account, kTestView1), "Bob left the circle, Alice is not in the backup");
222
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);
227
228 ok(testAccountPersistence(alice_account), "Test Account->DER->Account Equivalence");
229
230 ok(!SOSAccountIsPeerInBackupAndCurrentInView(alice_account, SOSFullPeerInfoGetPeerInfo(bob_account->my_identity), kTestView1), "Bob is still in the backup!");
231
232 //Bob gets back into the circle
233 ok(SOSTestJoinWithApproval(cfpassword, cfaccount, changes, alice_account, bob_account, KEEP_USERKEY, 2, false), "Bob Re-Joins");
234
235 //enables view
236 is(SOSAccountUpdateView(bob_account, kTestView1, kSOSCCViewEnable, &error), kSOSCCViewMember, "Enable view (%@)", error);
237 CFReleaseNull(error);
238
239 ok(!SOSAccountIsMyPeerInBackupAndCurrentInView(bob_account, kTestView1), "Bob isn't in the backup yet");
240
241 ok(!SOSAccountIsLastBackupPeer(alice_account, &error), "Alice is not last backup peer - Bob still registers as one");
242 CFReleaseNull(error);
243
244 ok(SOSAccountSetBackupPublicKey_wTxn(bob_account, bob_backup_key, &error), "Set backup public key, alice (%@)", error);
245
246 is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, NULL), 1, "updates");
247
248 ok(!SOSAccountIsLastBackupPeer(alice_account, &error), "Alice is not last backup peer");
249 CFReleaseNull(error);
250
251 //
252 //removing backup key for bob account
253 //
254
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");
258
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!");
261
262 //
263 // Setting new backup public key for Bob
264 //
265
266 ok(SOSAccountSetBackupPublicKey_wTxn(bob_account, bob_backup_key, &error), "Set backup public key, alice (%@)", error);
267 CFReleaseNull(error);
268
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);
271
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!");
276
277 is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, NULL), 5, "updates");
278
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");
281
282 if(!recKeyFirst) registerRecoveryKeyNow(changes, alice_account, bob_account, pubKeyBytes, recKeyFirst);
283
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");
286
287 SOSBackupSliceKeyBagRef bskb = SOSAccountBackupSliceKeyBagForView(alice_account, kTestView1, &error);
288 CFReleaseNull(error);
289
290 ok(SOSBSKBHasRecoveryKey(bskb), "BSKB should have recovery key");
291
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");
296
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");
302
303 CFReleaseNull(fullKeyBytes);
304 CFReleaseNull(pubKeyBytes);
305 CFReleaseNull(bskb);
306
307 CFReleaseNull(bob_account);
308 CFReleaseNull(alice_account);
309 CFReleaseNull(cfpassword);
310 CFReleaseNull(wrappingKey);
311
312 SOSTestCleanup();
313 }
314
315 int secd_66_account_recovery(int argc, char *const *argv) {
316 plan_tests(358);
317
318 secd_test_setup_temp_keychain(__FUNCTION__, NULL);
319
320 tests(true);
321 tests(false);
322
323 return 0;
324 }
325
326 #endif