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 <securityd/SOSCloudCircleServer.h>
51 #include "SOSAccountTesting.h"
53 #include "SecdTestKeychainUtilities.h"
55 static int kTestTestCount = 111;
57 #if FIX_ICLOUD_IDENTITY_AS_SET_CRED_SIDE_EFFECT
59 static bool purgeICloudIdentity(SOSAccount* account) {
61 SOSFullPeerInfoRef icfpi = SOSCircleCopyiCloudFullPeerInfoRef([account.trust getCircle:NULL], NULL);
62 if(!icfpi) return false;
63 retval = SOSFullPeerInfoPurgePersistentKey(icfpi, NULL);
70 static void tests(void)
72 CFErrorRef error = NULL;
73 CFDataRef cfpassword = CFDataCreate(NULL, (uint8_t *) "FooFooFoo", 10);
74 CFStringRef cfaccount = CFSTR("test@test.org");
76 CFMutableDictionaryRef changes = CFDictionaryCreateMutableForCFTypes(kCFAllocatorDefault);
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"));
82 ok(SOSAccountAssertUserCredentialsAndUpdate(bob_account, cfaccount, cfpassword, &error), "Credential setting (%@)", error);
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");
87 ok(SOSAccountAssertUserCredentialsAndUpdate(alice_account, cfaccount, cfpassword, &error), "Credential setting (%@)", error);
90 ok(SOSAccountAssertUserCredentialsAndUpdate(carole_account, cfaccount, cfpassword, &error), "Credential setting (%@)", error);
92 ok(SOSAccountResetToOffering_wTxn(alice_account, &error), "Reset to offering (%@)", error);
95 is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, carole_account, NULL), 2, "updates");
97 ok(SOSAccountJoinCircles_wTxn(bob_account, &error), "Bob Applies (%@)", error);
99 is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, carole_account, NULL), 2, "updates");
102 CFArrayRef applicants = SOSAccountCopyApplicants(alice_account, &error);
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);
110 is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, carole_account, NULL), 3, "updates");
112 accounts_agree("bob&alice pair", bob_account, alice_account);
114 /*----- normal join after restore -----*/
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");
120 is(countApplicants(alice_account), 0, "See no applicants");
122 is(countPeers(carole_account), 3, "Carole sees 3 valid peers after sliding in");
124 is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, carole_account, NULL), 1, "updates");
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);
129 ok([carole_account.trust leaveCircle:carole_account err:&error], "Carol Leaves again");
130 CFReleaseNull(error);
132 is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, carole_account, NULL), 2, "updates");
134 /*----- join - join after restore -----*/
136 ok(SOSAccountJoinCircles_wTxn(carole_account, &error), "Carole normally joins (%@)", error);
137 CFReleaseNull(error);
139 is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, carole_account, NULL), 2, "updates");
141 is(countApplicants(alice_account), 1, "See one applicant");
143 ok(SOSAccountJoinCirclesAfterRestore_wTxn(carole_account, &error), "Carole cloud identity joins (%@)", error);
144 CFReleaseNull(error);
146 is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, carole_account, NULL), 4, "updates");
149 is(countApplicants(alice_account), 0, "See no applicants");
151 is(countPeers(carole_account), 3, "Carole sees 3 valid peers after sliding in");
153 is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, carole_account, NULL), 1, "updates");
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);
158 #if FIX_ICLOUD_IDENTITY_AS_SET_CRED_SIDE_EFFECT
159 /* Break iCloud identity FPI in all peers */
161 ok(purgeICloudIdentity(alice_account), "remove iCloud private key");
163 is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, carole_account, NULL), 1, "updates");
166 ok(SOSAccountAssertUserCredentialsAndUpdate(alice_account, cfaccount, cfpassword, &error), "Credential setting (%@)", error);
167 CFReleaseNull(error);
169 is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, carole_account, NULL), 4, "updates");
171 ok([carole_account.trust leaveCircle:carole_account err:&error], "Carol Leaves again");
172 CFReleaseNull(error);
174 is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, carole_account, NULL), 2, "updates");
176 /*----- join - join after restore -----*/
178 ok(SOSAccountJoinCircles_wTxn(carole_account, &error), "Carole normally joins (%@)", error);
179 CFReleaseNull(error);
181 is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, carole_account, NULL), 2, "updates");
183 is(countApplicants(alice_account), 1, "See one applicant");
185 ok(SOSAccountJoinCirclesAfterRestore_wTxn(carole_account, &error), "Carole cloud identity joins (%@)", error);
186 CFReleaseNull(error);
188 is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, carole_account, NULL), 4, "updates");
191 is(countApplicants(alice_account), 0, "See no applicants");
193 is(countPeers(carole_account), 3, "Carole sees 3 valid peers after sliding in");
195 is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, carole_account, NULL), 1, "updates");
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);
200 //join after piggybacking the icloud identity??
201 CFMutableArrayRef identityArray = SOSAccountCopyiCloudIdentities(alice_account);
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));
209 [encodedIdenities addObject:data];
212 //store in keychain as the piggy icloud
213 [encodedIdenities enumerateObjectsUsingBlock:^(NSData *v_data, NSUInteger idx, BOOL *stop) {
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,
225 OSStatus status = SecItemAdd((__bridge CFDictionaryRef) query, NULL);
227 if(status == errSecDuplicateItem) {
228 // Sure, okay, fine, we'll update.
229 NSMutableDictionary* update = [@{
230 (id)kSecValueData: v_data,
232 query[(id)kSecValueData] = nil;
234 status = SecItemUpdate((__bridge CFDictionaryRef) query, (__bridge CFDictionaryRef)update);
235 ok(status == 0, "Grabbed icloud identity from the keychain %@", error);
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,
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);
255 NSDictionary *keyAttributes = @{
256 (__bridge id)kSecAttrKeyClass : (__bridge id)kSecAttrKeyClassPrivate,
257 (__bridge id)kSecAttrKeyType : (__bridge id)kSecAttrKeyTypeEC,
261 SecKeyRef privKey = SecKeyCreateWithData(result, (__bridge CFDictionaryRef)keyAttributes, NULL);
263 ok(privKey != NULL, "Private key is NULL");
264 SecKeyRef publicKey = SecKeyCreatePublicFromPrivate(privKey);
265 ok(publicKey != NULL, "Private key is NULL");
267 CFDataRef public_key_hash = SecKeyCopyPublicKeyHash(publicKey);
268 ok(public_key_hash != NULL, "hash is not null");
270 CFReleaseNull(publicKey);
272 SOSAccount* margaret_account = CreateAccountForLocalChanges(CFSTR("margaret"), CFSTR("TestSource"));
274 ok(SOSAccountAssertUserCredentialsAndUpdate(margaret_account, cfaccount, cfpassword, &error), "Credential setting (%@)", error);
275 ok(SOSAccountJoinCirclesAfterRestore_wTxn(margaret_account, &error), "Carole cloud identity joins (%@)", error);
277 CFReleaseNull(identityArray);
278 CFReleaseNull(changes);
279 CFReleaseNull(error);
280 CFReleaseNull(public_key_hash);
281 CFReleaseNull(cfpassword);
282 CFReleaseNull(privKey);
289 int secd_60_account_cloud_identity(int argc, char *const *argv)
291 plan_tests(kTestTestCount);
293 secd_test_setup_temp_keychain(__FUNCTION__, NULL);