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