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