]> git.saurik.com Git - apple/security.git/blob - OSX/sec/securityd/Regressions/secd-60-account-cloud-identity.m
Security-58286.200.222.tar.gz
[apple/security.git] / OSX / sec / securityd / Regressions / secd-60-account-cloud-identity.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 <Security/SecureObjectSync/SOSAccount.h>
33 #include <Security/SecureObjectSync/SOSCloudCircle.h>
34 #include <Security/SecureObjectSync/SOSInternal.h>
35 #include <Security/SecureObjectSync/SOSUserKeygen.h>
36 #include <Security/SecureObjectSync/SOSTransport.h>
37 #include <Security/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 <securityd/SOSCloudCircleServer.h>
50
51 #include "SOSAccountTesting.h"
52
53 #include "SecdTestKeychainUtilities.h"
54
55 static int kTestTestCount = 161;
56
57 static bool purgeICloudIdentity(SOSAccount* account) {
58 bool retval = false;
59 SOSFullPeerInfoRef icfpi = SOSCircleCopyiCloudFullPeerInfoRef([account.trust getCircle:NULL], NULL);
60 if(!icfpi) return false;
61 retval = SOSFullPeerInfoPurgePersistentKey(icfpi, NULL);
62 CFReleaseNull(icfpi);
63 return retval;
64 }
65
66 static void tests(void)
67 {
68 CFErrorRef error = NULL;
69 CFDataRef cfpassword = CFDataCreate(NULL, (uint8_t *) "FooFooFoo", 10);
70 CFStringRef cfaccount = CFSTR("test@test.org");
71
72 CFMutableDictionaryRef changes = CFDictionaryCreateMutableForCFTypes(kCFAllocatorDefault);
73
74 SOSAccount* alice_account = CreateAccountForLocalChanges( CFSTR("Alice"), CFSTR("TestSource"));
75 SOSAccount* bob_account = CreateAccountForLocalChanges(CFSTR("Bob"), CFSTR("TestSource"));
76 SOSAccount* carole_account = CreateAccountForLocalChanges(CFSTR("Carole"), CFSTR("TestSource"));
77
78 ok(SOSAccountAssertUserCredentialsAndUpdate(bob_account, cfaccount, cfpassword, &error), "Credential setting (%@)", error);
79
80 // Bob wins writing at this point, feed the changes back to alice.
81 is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, carole_account, NULL), 1, "updates");
82
83 ok(SOSAccountAssertUserCredentialsAndUpdate(alice_account, cfaccount, cfpassword, &error), "Credential setting (%@)", error);
84 CFReleaseNull(error);
85
86 ok(SOSAccountAssertUserCredentialsAndUpdate(carole_account, cfaccount, cfpassword, &error), "Credential setting (%@)", error);
87 CFReleaseNull(error);
88 ok(SOSAccountResetToOffering_wTxn(alice_account, &error), "Reset to offering (%@)", error);
89 CFReleaseNull(error);
90
91 is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, carole_account, NULL), 2, "updates");
92
93 ok(SOSAccountJoinCircles_wTxn(bob_account, &error), "Bob Applies (%@)", error);
94 CFReleaseNull(error);
95 is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, carole_account, NULL), 2, "updates");
96
97 {
98 CFArrayRef applicants = SOSAccountCopyApplicants(alice_account, &error);
99
100 ok(applicants && CFArrayGetCount(applicants) == 1, "See one applicant %@ (%@)", applicants, error);
101 ok(SOSAccountAcceptApplicants(alice_account, applicants, &error), "Alice accepts (%@)", error);
102 CFReleaseNull(error);
103 CFReleaseNull(applicants);
104 }
105
106 is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, carole_account, NULL), 3, "updates");
107
108 accounts_agree("bob&alice pair", bob_account, alice_account);
109
110 /*----- normal join after restore -----*/
111
112 ok(SOSAccountJoinCirclesAfterRestore_wTxn(carole_account, &error), "Carole cloud identity joins (%@)", error);
113 CFReleaseNull(error);
114 is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, carole_account, NULL), 4, "updates");
115
116 is(countApplicants(alice_account), 0, "See no applicants");
117
118 is(countPeers(carole_account), 3, "Carole sees 3 valid peers after sliding in");
119
120 is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, carole_account, NULL), 1, "updates");
121
122 accounts_agree_internal("Carole's in", bob_account, alice_account, false);
123 accounts_agree_internal("Carole's in - 2", bob_account, carole_account, false);
124
125 ok([carole_account.trust leaveCircle:carole_account err:&error], "Carol Leaves again");
126 CFReleaseNull(error);
127
128 is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, carole_account, NULL), 2, "updates");
129
130 /*----- join - join after restore -----*/
131
132 ok(SOSAccountJoinCircles_wTxn(carole_account, &error), "Carole normally joins (%@)", error);
133 CFReleaseNull(error);
134
135 is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, carole_account, NULL), 2, "updates");
136
137 is(countApplicants(alice_account), 1, "See one applicant");
138
139 ok(SOSAccountJoinCirclesAfterRestore_wTxn(carole_account, &error), "Carole cloud identity joins (%@)", error);
140 CFReleaseNull(error);
141
142 is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, carole_account, NULL), 4, "updates");
143
144
145 is(countApplicants(alice_account), 0, "See no applicants");
146
147 is(countPeers(carole_account), 3, "Carole sees 3 valid peers after sliding in");
148
149 is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, carole_account, NULL), 1, "updates");
150
151 accounts_agree_internal("Carole's in", bob_account, alice_account, false);
152 accounts_agree_internal("Carole's in - 2", bob_account, carole_account, false);
153
154 /* Break iCloud identity FPI in all peers */
155
156 ok(purgeICloudIdentity(alice_account), "remove iCloud private key");
157
158 is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, carole_account, NULL), 1, "updates");
159
160
161 ok(SOSAccountAssertUserCredentialsAndUpdate(alice_account, cfaccount, cfpassword, &error), "Credential setting (%@)", error);
162 CFReleaseNull(error);
163
164 is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, carole_account, NULL), 4, "updates");
165
166 ok([carole_account.trust leaveCircle:carole_account err:&error], "Carol Leaves again");
167 CFReleaseNull(error);
168
169 is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, carole_account, NULL), 2, "updates");
170
171 /*----- join - join after restore -----*/
172
173 ok(SOSAccountJoinCircles_wTxn(carole_account, &error), "Carole normally joins (%@)", error);
174 CFReleaseNull(error);
175
176 is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, carole_account, NULL), 2, "updates");
177
178 is(countApplicants(alice_account), 1, "See one applicant");
179
180 ok(SOSAccountJoinCirclesAfterRestore_wTxn(carole_account, &error), "Carole cloud identity joins (%@)", error);
181 CFReleaseNull(error);
182
183 is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, carole_account, NULL), 4, "updates");
184
185
186 is(countApplicants(alice_account), 0, "See no applicants");
187
188 is(countPeers(carole_account), 3, "Carole sees 3 valid peers after sliding in");
189
190 is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, carole_account, NULL), 1, "updates");
191
192 accounts_agree_internal("Carole's in", bob_account, alice_account, false);
193 accounts_agree_internal("Carole's in - 2", bob_account, carole_account, false);
194
195 //join after piggybacking the icloud identity??
196 CFMutableArrayRef identityArray = SOSAccountCopyiCloudIdentities(alice_account);
197
198 NSMutableArray *encodedIdenities = [NSMutableArray array];
199 CFIndex i, count = CFArrayGetCount(identityArray);
200 for (i = 0; i < count; i++) {
201 SOSPeerInfoRef fpi = (SOSPeerInfoRef)CFArrayGetValueAtIndex(identityArray, i);
202 NSData *data = CFBridgingRelease(SOSPeerInfoCopyData(fpi, NULL));
203 if (data)
204 [encodedIdenities addObject:data];
205 }
206
207 //store in keychain as the piggy icloud
208 [encodedIdenities enumerateObjectsUsingBlock:^(NSData *v_data, NSUInteger idx, BOOL *stop) {
209
210 NSMutableDictionary* query = [@{
211 (id)kSecClass : (id)kSecClassKey,
212 (id)kSecAttrNoLegacy : @YES,
213 (id)kSecAttrAccessGroup: @"com.apple.security.sos",
214 (id)kSecAttrLabel : @"Cloud Identity - piggy",
215 (id)kSecAttrSynchronizable : (id)kCFBooleanTrue,
216 (id)kSecUseTombstones : (id)kCFBooleanTrue,
217 (id)kSecValueData : v_data,
218 } mutableCopy];
219
220 OSStatus status = SecItemAdd((__bridge CFDictionaryRef) query, NULL);
221
222 if(status == errSecDuplicateItem) {
223 // Sure, okay, fine, we'll update.
224 NSMutableDictionary* update = [@{
225 (id)kSecValueData: v_data,
226 } mutableCopy];
227 query[(id)kSecValueData] = nil;
228
229 status = SecItemUpdate((__bridge CFDictionaryRef) query, (__bridge CFDictionaryRef)update);
230 ok(status == 0, "Grabbed icloud identity from the keychain %@", error);
231 }
232 }];
233
234
235 //now grab this grom the keychain
236 NSMutableDictionary* query2 = [@{
237 (id)kSecClass : (id)kSecClassKey,
238 (id)kSecAttrNoLegacy : @YES,
239 (id)kSecAttrAccessGroup: @"com.apple.security.sos",
240 (id)kSecAttrLabel : @"Cloud Identity - piggy",
241 (id)kSecAttrSynchronizable : (id)kCFBooleanTrue,
242 (id)kSecUseTombstones : (id)kCFBooleanTrue,
243 (id)kSecReturnData : (id)kCFBooleanTrue,
244 } mutableCopy];
245 CFTypeRef result = NULL;
246 OSStatus status = SecItemCopyMatching((__bridge CFDictionaryRef) query2, &result);
247 ok(status == 0, "Grabbed icloud identity from the keychain %@", error);
248 ok(result != NULL, "result from sec item copy matching query %@", error);
249
250 NSDictionary *keyAttributes = @{
251 (__bridge id)kSecAttrKeyClass : (__bridge id)kSecAttrKeyClassPrivate,
252 (__bridge id)kSecAttrKeyType : (__bridge id)kSecAttrKeyTypeEC,
253 };
254
255
256 SecKeyRef privKey = SecKeyCreateWithData(result, (__bridge CFDictionaryRef)keyAttributes, NULL);
257
258 ok(privKey != NULL, "Private key is NULL");
259 SecKeyRef publicKey = SecKeyCreatePublicFromPrivate(privKey);
260 ok(publicKey != NULL, "Private key is NULL");
261
262 CFDataRef public_key_hash = SecKeyCopyPublicKeyHash(publicKey);
263 ok(public_key_hash != NULL, "hash is not null");
264
265 CFReleaseNull(publicKey);
266
267 SOSAccount* margaret_account = CreateAccountForLocalChanges(CFSTR("margaret"), CFSTR("TestSource"));
268
269 ok(SOSAccountAssertUserCredentialsAndUpdate(margaret_account, cfaccount, cfpassword, &error), "Credential setting (%@)", error);
270 ok(SOSAccountJoinCirclesAfterRestore_wTxn(margaret_account, &error), "Carole cloud identity joins (%@)", error);
271
272 CFReleaseNull(identityArray);
273 CFReleaseNull(changes);
274 CFReleaseNull(error);
275 CFReleaseNull(public_key_hash);
276 CFReleaseNull(cfpassword);
277 CFReleaseNull(privKey);
278
279 alice_account = nil;
280 bob_account = nil;
281 SOSTestCleanup();
282 }
283
284 int secd_60_account_cloud_identity(int argc, char *const *argv)
285 {
286 plan_tests(kTestTestCount);
287
288 secd_test_setup_temp_keychain(__FUNCTION__, NULL);
289
290 tests();
291
292 return 0;
293 }