]> git.saurik.com Git - apple/security.git/blob - keychain/securityd/Regressions/secd-65-account-retirement-reset.m
Security-59754.41.1.tar.gz
[apple/security.git] / keychain / securityd / Regressions / secd-65-account-retirement-reset.m
1 /*
2 * Copyright (c) 2013-2014 Apple Inc. All Rights Reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
11 * file.
12 *
13 * The Original Code and all software distributed under the License are
14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18 * Please see the License for the specific language governing rights and
19 * limitations under the License.
20 *
21 * @APPLE_LICENSE_HEADER_END@
22 */
23
24
25
26
27 #include <Security/SecBase.h>
28 #include <Security/SecItem.h>
29
30 #include <CoreFoundation/CFDictionary.h>
31
32 #include "keychain/SecureObjectSync/SOSAccount.h"
33 #include <Security/SecureObjectSync/SOSCloudCircle.h>
34 #include "keychain/SecureObjectSync/SOSInternal.h"
35 #include "keychain/SecureObjectSync/SOSUserKeygen.h"
36 #include "keychain/SecureObjectSync/SOSTransport.h"
37 #include "keychain/SecureObjectSync/SOSAccountTrustClassic+Circle.h"
38
39 #include <stdlib.h>
40 #include <unistd.h>
41
42 #include "secd_regressions.h"
43 #include "SOSTestDataSource.h"
44
45 #include "SOSRegressionUtilities.h"
46 #include <utilities/SecCFWrappers.h>
47 #include <Security/SecKeyPriv.h>
48
49 #include "keychain/securityd/SOSCloudCircleServer.h"
50
51 #include "SOSAccountTesting.h"
52
53 #include "SecdTestKeychainUtilities.h"
54
55 #if SOS_ENABLED
56
57 typedef void (^stir_block)(int expected_iterations);
58 typedef int (^execute_block)(void);
59
60 static void stirBetween(stir_block stir, ...) {
61 va_list va;
62 va_start(va, stir);
63
64 execute_block execute = NULL;
65
66 while ((execute = va_arg(va, execute_block)) != NULL)
67 stir(execute());
68 }
69
70 __unused static void VerifyCountAndAcceptAllApplicants(SOSAccount* account, int expected)
71 {
72 CFErrorRef error = NULL;
73 CFArrayRef applicants = SOSAccountCopyApplicants(account, &error);
74
75 SKIP: {
76 skip("Empty applicant array", 2, applicants);
77
78 is(CFArrayGetCount(applicants), expected, "Applicants: %@ (%@)", applicants, error);
79 CFReleaseNull(error);
80
81 ok(SOSAccountAcceptApplicants(account , applicants, &error), "Accepting all (%@)", error);
82 CFReleaseNull(error);
83 }
84
85 CFReleaseNull(applicants);
86 }
87
88
89 static void tests(void)
90 {
91 __block CFErrorRef error = NULL;
92 CFDataRef cfpassword = CFDataCreate(NULL, (uint8_t *) "FooFooFoo", 10);
93 CFStringRef cfaccount = CFSTR("test@test.org");
94
95 CFMutableDictionaryRef changes = CFDictionaryCreateMutableForCFTypes(kCFAllocatorDefault);
96
97 const CFStringRef data_source_name = CFSTR("TestSource");
98
99 SOSAccount* alice_account = CreateAccountForLocalChanges(CFSTR("Alice"), data_source_name);
100 SOSAccount* bob_account = CreateAccountForLocalChanges(CFSTR("Bob"), data_source_name);
101 SOSAccount* carole_account = CreateAccountForLocalChanges(CFSTR("Carole"), data_source_name);
102
103 SOSAccount* alice_resurrected = NULL;
104
105 __block CFDataRef frozen_alice = NULL;
106
107
108 stirBetween(^(int expected){
109 is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, carole_account, NULL), expected, "stirring");
110 }, ^{
111 ok(SOSAccountAssertUserCredentialsAndUpdate(bob_account , cfaccount, cfpassword, &error), "bob credential setting (%@)", error);
112
113 return 1;
114 }, ^{
115 ok(SOSAccountAssertUserCredentialsAndUpdate(alice_account , cfaccount, cfpassword, &error), "alice credential setting (%@)", error);
116 CFReleaseNull(error);
117
118 ok(SOSAccountAssertUserCredentialsAndUpdate(carole_account, cfaccount, cfpassword, &error), "carole credential setting (%@)", error);
119 CFReleaseNull(error);
120
121 ok(SOSAccountResetToOffering_wTxn(alice_account , &error), "Reset to offering (%@)", error);
122 CFReleaseNull(error);
123
124 return 2;
125 }, ^{
126 [alice_account performTransaction:^(SOSAccountTransaction * _Nonnull txn) {
127 NSError *ns_error = nil;
128 frozen_alice = (CFDataRef) CFBridgingRetain([alice_account encodedData:&ns_error]);
129 ok(frozen_alice, "Copy encoded %@", ns_error);
130 ns_error = nil;
131 }];
132
133 SOSAccountPurgePrivateCredential(alice_account);
134
135 ok([alice_account.trust leaveCircle:alice_account err:&error], "Alice Leaves (%@)", error);
136 CFReleaseNull(error);
137
138 return 2;
139 }, ^{
140 ok(SOSAccountResetToOffering_wTxn(bob_account , &error), "Reset to offering (%@)", error);
141 CFReleaseNull(error);
142
143 return 2;
144 },
145 NULL);
146
147 alice_resurrected = CreateAccountForLocalChangesFromData(frozen_alice, CFSTR("Alice risen"), data_source_name);
148 // This is necessary from the change that makes accounts not inflate if the private key was lost - alice_resurected now
149 // Starts as a brand new account, so this whole series of tests needs to amount to "is this brand new"?
150 // The trigger is alice leaving the circle - that kills the deviceKey.
151 ProcessChangesUntilNoChange(changes, alice_resurrected, bob_account, carole_account, NULL);
152
153 stirBetween(^(int expected){
154 is(ProcessChangesUntilNoChange(changes, alice_resurrected, bob_account, carole_account, NULL), expected, "stirring");
155 }, ^{
156 ok(SOSAccountAssertUserCredentialsAndUpdate(alice_resurrected, cfaccount, cfpassword, &error), "alice_resurrected credential setting (%@)", error);
157 CFReleaseNull(error);
158 return 1;
159 }, ^{
160 ok(![alice_resurrected isInCircle:&error], "Ressurrected not in circle: %@", error);
161 CFReleaseNull(error);
162
163 ok([bob_account isInCircle:&error], "Should be in circle: %@", error);
164 CFReleaseNull(error);
165
166 return 1;
167 },
168 NULL);
169
170 CFReleaseNull(frozen_alice);
171 alice_account = nil;
172 bob_account = nil;
173 SOSTestCleanup();
174 }
175 #endif
176
177 int secd_65_account_retirement_reset(int argc, char *const *argv)
178 {
179 #if SOS_ENABLED
180 plan_tests(28);
181 secd_test_setup_temp_keychain(__FUNCTION__, NULL);
182 tests();
183 #else
184 plan_tests(0);
185 #endif
186 return 0;
187 }