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