]> git.saurik.com Git - apple/security.git/blob - keychain/securityd/Regressions/secd-60-account-cloud-identity.m
Security-59754.41.1.tar.gz
[apple/security.git] / keychain / 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 "keychain/securityd/SOSCloudCircleServer.h"
50
51 #include "SOSAccountTesting.h"
52
53 #include "SecdTestKeychainUtilities.h"
54 #if SOS_ENABLED
55
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 // fix this as if the timed event had triggered in handleUpdateCircles
165 bool fixedIdentities = [alice_account.trust fixICloudIdentities:alice_account circle:alice_account.trust.trustedCircle];
166 ok(fixedIdentities, "failed to fix icloud identity");
167
168 is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, carole_account, NULL), 1, "updates");
169
170 ok([carole_account.trust leaveCircle:carole_account err:&error], "Carol Leaves again");
171 CFReleaseNull(error);
172
173 is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, carole_account, NULL), 4, "updates");
174
175 /*----- join - join after restore -----*/
176
177 ok(SOSAccountJoinCircles_wTxn(carole_account, &error), "Carole normally joins (%@)", error);
178 CFReleaseNull(error);
179
180 is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, carole_account, NULL), 2, "updates");
181
182 is(countApplicants(alice_account), 1, "See one applicant");
183
184 ok(SOSAccountJoinCirclesAfterRestore_wTxn(carole_account, &error), "Carole cloud identity joins (%@)", error);
185 CFReleaseNull(error);
186
187 is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, carole_account, NULL), 4, "updates");
188
189
190 is(countApplicants(alice_account), 0, "See no applicants");
191
192 is(countPeers(carole_account), 3, "Carole sees 3 valid peers after sliding in");
193
194 is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, carole_account, NULL), 1, "updates");
195
196 accounts_agree_internal("Carole's in", bob_account, alice_account, false);
197 accounts_agree_internal("Carole's in - 2", bob_account, carole_account, false);
198
199 //join after piggybacking the icloud identity??
200 CFMutableArrayRef identityArray = SOSAccountCopyiCloudIdentities(alice_account);
201
202 NSMutableArray *encodedIdenities = [NSMutableArray array];
203 CFIndex i, count = CFArrayGetCount(identityArray);
204 for (i = 0; i < count; i++) {
205 SOSPeerInfoRef fpi = (SOSPeerInfoRef)CFArrayGetValueAtIndex(identityArray, i);
206 NSData *data = CFBridgingRelease(SOSPeerInfoCopyData(fpi, NULL));
207 if (data)
208 [encodedIdenities addObject:data];
209 }
210
211 //store in keychain as the piggy icloud
212 [encodedIdenities enumerateObjectsUsingBlock:^(NSData *v_data, NSUInteger idx, BOOL *stop) {
213
214 NSMutableDictionary* query = [@{
215 (id)kSecClass : (id)kSecClassKey,
216 (id)kSecUseDataProtectionKeychain : @YES,
217 (id)kSecAttrAccessGroup: @"com.apple.security.sos",
218 (id)kSecAttrLabel : @"Cloud Identity - piggy",
219 (id)kSecAttrSynchronizable : (id)kCFBooleanTrue,
220 (id)kSecUseTombstones : (id)kCFBooleanTrue,
221 (id)kSecValueData : v_data,
222 } mutableCopy];
223
224 OSStatus status = SecItemAdd((__bridge CFDictionaryRef) query, NULL);
225
226 if(status == errSecDuplicateItem) {
227 // Sure, okay, fine, we'll update.
228 NSMutableDictionary* update = [@{
229 (id)kSecValueData: v_data,
230 } mutableCopy];
231 query[(id)kSecValueData] = nil;
232
233 status = SecItemUpdate((__bridge CFDictionaryRef) query, (__bridge CFDictionaryRef)update);
234 ok(status == 0, "Grabbed icloud identity from the keychain %@", error);
235 }
236 }];
237
238
239 //now grab this grom the keychain
240 NSMutableDictionary* query2 = [@{
241 (id)kSecClass : (id)kSecClassKey,
242 (id)kSecUseDataProtectionKeychain : @YES,
243 (id)kSecAttrAccessGroup: @"com.apple.security.sos",
244 (id)kSecAttrLabel : @"Cloud Identity - piggy",
245 (id)kSecAttrSynchronizable : (id)kCFBooleanTrue,
246 (id)kSecUseTombstones : (id)kCFBooleanTrue,
247 (id)kSecReturnData : (id)kCFBooleanTrue,
248 } mutableCopy];
249 CFTypeRef result = NULL;
250 OSStatus status = SecItemCopyMatching((__bridge CFDictionaryRef) query2, &result);
251 ok(status == 0, "Grabbed icloud identity from the keychain %@", error);
252 ok(result != NULL, "result from sec item copy matching query %@", error);
253
254 NSDictionary *keyAttributes = @{
255 (__bridge id)kSecAttrKeyClass : (__bridge id)kSecAttrKeyClassPrivate,
256 (__bridge id)kSecAttrKeyType : (__bridge id)kSecAttrKeyTypeEC,
257 };
258
259
260 SecKeyRef privKey = SecKeyCreateWithData(result, (__bridge CFDictionaryRef)keyAttributes, NULL);
261
262 ok(privKey != NULL, "Private key is NULL");
263 SecKeyRef publicKey = SecKeyCreatePublicFromPrivate(privKey);
264 ok(publicKey != NULL, "Private key is NULL");
265
266 CFDataRef public_key_hash = SecKeyCopyPublicKeyHash(publicKey);
267 ok(public_key_hash != NULL, "hash is not null");
268
269 CFReleaseNull(publicKey);
270
271 SOSAccount* margaret_account = CreateAccountForLocalChanges(CFSTR("margaret"), CFSTR("TestSource"));
272
273 ok(SOSAccountAssertUserCredentialsAndUpdate(margaret_account, cfaccount, cfpassword, &error), "Credential setting (%@)", error);
274 ok(SOSAccountJoinCirclesAfterRestore_wTxn(margaret_account, &error), "Carole cloud identity joins (%@)", error);
275
276 CFReleaseNull(identityArray);
277 CFReleaseNull(changes);
278 CFReleaseNull(error);
279 CFReleaseNull(public_key_hash);
280 CFReleaseNull(cfpassword);
281 CFReleaseNull(privKey);
282
283 alice_account = nil;
284 bob_account = nil;
285 SOSTestCleanup();
286 }
287 #endif
288
289 int secd_60_account_cloud_identity(int argc, char *const *argv)
290 {
291 #if SOS_ENABLED
292 plan_tests(159);
293 secd_test_setup_temp_keychain(__FUNCTION__, NULL);
294 tests();
295 #else
296 plan_tests(0);
297 #endif
298 return 0;
299 }