2 * Copyright (c) 2013-2014 Apple Inc. All Rights Reserved.
4 * @APPLE_LICENSE_HEADER_START@
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
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.
21 * @APPLE_LICENSE_HEADER_END@
27 #include <Security/SecBase.h>
28 #include <Security/SecItem.h>
30 #include <CoreFoundation/CFDictionary.h>
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"
42 #include "secd_regressions.h"
43 #include "SOSTestDataSource.h"
45 #include "SOSRegressionUtilities.h"
46 #include <utilities/SecCFWrappers.h>
47 #include <Security/SecKeyPriv.h>
49 #include "keychain/securityd/SOSCloudCircleServer.h"
51 #include "SOSAccountTesting.h"
53 #include "SecdTestKeychainUtilities.h"
56 static bool purgeICloudIdentity(SOSAccount* account) {
58 SOSFullPeerInfoRef icfpi = SOSCircleCopyiCloudFullPeerInfoRef([account.trust getCircle:NULL], NULL);
59 if(!icfpi) return false;
60 retval = SOSFullPeerInfoPurgePersistentKey(icfpi, NULL);
65 static void tests(void)
67 CFErrorRef error = NULL;
68 CFDataRef cfpassword = CFDataCreate(NULL, (uint8_t *) "FooFooFoo", 10);
69 CFStringRef cfaccount = CFSTR("test@test.org");
71 CFMutableDictionaryRef changes = CFDictionaryCreateMutableForCFTypes(kCFAllocatorDefault);
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"));
77 ok(SOSAccountAssertUserCredentialsAndUpdate(bob_account, cfaccount, cfpassword, &error), "Credential setting (%@)", error);
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");
82 ok(SOSAccountAssertUserCredentialsAndUpdate(alice_account, cfaccount, cfpassword, &error), "Credential setting (%@)", error);
85 ok(SOSAccountAssertUserCredentialsAndUpdate(carole_account, cfaccount, cfpassword, &error), "Credential setting (%@)", error);
87 ok(SOSAccountResetToOffering_wTxn(alice_account, &error), "Reset to offering (%@)", error);
90 is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, carole_account, NULL), 2, "updates");
92 ok(SOSAccountJoinCircles_wTxn(bob_account, &error), "Bob Applies (%@)", error);
94 is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, carole_account, NULL), 2, "updates");
97 CFArrayRef applicants = SOSAccountCopyApplicants(alice_account, &error);
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);
105 is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, carole_account, NULL), 3, "updates");
107 accounts_agree("bob&alice pair", bob_account, alice_account);
109 /*----- normal join after restore -----*/
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");
115 is(countApplicants(alice_account), 0, "See no applicants");
117 is(countPeers(carole_account), 3, "Carole sees 3 valid peers after sliding in");
119 is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, carole_account, NULL), 1, "updates");
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);
124 ok([carole_account.trust leaveCircle:carole_account err:&error], "Carol Leaves again");
125 CFReleaseNull(error);
127 is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, carole_account, NULL), 2, "updates");
129 /*----- join - join after restore -----*/
131 ok(SOSAccountJoinCircles_wTxn(carole_account, &error), "Carole normally joins (%@)", error);
132 CFReleaseNull(error);
134 is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, carole_account, NULL), 2, "updates");
136 is(countApplicants(alice_account), 1, "See one applicant");
138 ok(SOSAccountJoinCirclesAfterRestore_wTxn(carole_account, &error), "Carole cloud identity joins (%@)", error);
139 CFReleaseNull(error);
141 is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, carole_account, NULL), 4, "updates");
144 is(countApplicants(alice_account), 0, "See no applicants");
146 is(countPeers(carole_account), 3, "Carole sees 3 valid peers after sliding in");
148 is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, carole_account, NULL), 1, "updates");
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);
153 /* Break iCloud identity FPI in all peers */
155 ok(purgeICloudIdentity(alice_account), "remove iCloud private key");
157 is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, carole_account, NULL), 1, "updates");
160 ok(SOSAccountAssertUserCredentialsAndUpdate(alice_account, cfaccount, cfpassword, &error), "Credential setting (%@)", error);
161 CFReleaseNull(error);
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");
167 is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, carole_account, NULL), 1, "updates");
169 ok([carole_account.trust leaveCircle:carole_account err:&error], "Carol Leaves again");
170 CFReleaseNull(error);
172 is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, carole_account, NULL), 4, "updates");
174 /*----- join - join after restore -----*/
176 ok(SOSAccountJoinCircles_wTxn(carole_account, &error), "Carole normally joins (%@)", error);
177 CFReleaseNull(error);
179 is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, carole_account, NULL), 2, "updates");
181 is(countApplicants(alice_account), 1, "See one applicant");
183 ok(SOSAccountJoinCirclesAfterRestore_wTxn(carole_account, &error), "Carole cloud identity joins (%@)", error);
184 CFReleaseNull(error);
186 is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, carole_account, NULL), 4, "updates");
189 is(countApplicants(alice_account), 0, "See no applicants");
191 is(countPeers(carole_account), 3, "Carole sees 3 valid peers after sliding in");
193 is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, carole_account, NULL), 1, "updates");
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);
198 //join after piggybacking the icloud identity??
199 CFMutableArrayRef identityArray = SOSAccountCopyiCloudIdentities(alice_account);
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));
207 [encodedIdenities addObject:data];
210 //store in keychain as the piggy icloud
211 [encodedIdenities enumerateObjectsUsingBlock:^(NSData *v_data, NSUInteger idx, BOOL *stop) {
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,
223 OSStatus status = SecItemAdd((__bridge CFDictionaryRef) query, NULL);
225 if(status == errSecDuplicateItem) {
226 // Sure, okay, fine, we'll update.
227 NSMutableDictionary* update = [@{
228 (id)kSecValueData: v_data,
230 query[(id)kSecValueData] = nil;
232 status = SecItemUpdate((__bridge CFDictionaryRef) query, (__bridge CFDictionaryRef)update);
233 ok(status == 0, "Grabbed icloud identity from the keychain %@", error);
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,
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);
253 NSDictionary *keyAttributes = @{
254 (__bridge id)kSecAttrKeyClass : (__bridge id)kSecAttrKeyClassPrivate,
255 (__bridge id)kSecAttrKeyType : (__bridge id)kSecAttrKeyTypeEC,
259 SecKeyRef privKey = SecKeyCreateWithData(result, (__bridge CFDictionaryRef)keyAttributes, NULL);
261 ok(privKey != NULL, "Private key is NULL");
262 SecKeyRef publicKey = SecKeyCreatePublicFromPrivate(privKey);
263 ok(publicKey != NULL, "Private key is NULL");
265 CFDataRef public_key_hash = SecKeyCopyPublicKeyHash(publicKey);
266 ok(public_key_hash != NULL, "hash is not null");
268 CFReleaseNull(publicKey);
270 SOSAccount* margaret_account = CreateAccountForLocalChanges(CFSTR("margaret"), CFSTR("TestSource"));
272 ok(SOSAccountAssertUserCredentialsAndUpdate(margaret_account, cfaccount, cfpassword, &error), "Credential setting (%@)", error);
273 ok(SOSAccountJoinCirclesAfterRestore_wTxn(margaret_account, &error), "Carole cloud identity joins (%@)", error);
275 CFReleaseNull(identityArray);
276 CFReleaseNull(changes);
277 CFReleaseNull(error);
278 CFReleaseNull(public_key_hash);
279 CFReleaseNull(cfpassword);
280 CFReleaseNull(privKey);
287 int secd_60_account_cloud_identity(int argc, char *const *argv)
291 secd_test_setup_temp_keychain(__FUNCTION__, NULL);