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"
57 static bool purgeICloudIdentity(SOSAccount* account) {
59 SOSFullPeerInfoRef icfpi = SOSCircleCopyiCloudFullPeerInfoRef([account.trust getCircle:NULL], NULL);
60 if(!icfpi) return false;
61 retval = SOSFullPeerInfoPurgePersistentKey(icfpi, NULL);
66 static void tests(void)
68 CFErrorRef error = NULL;
69 CFDataRef cfpassword = CFDataCreate(NULL, (uint8_t *) "FooFooFoo", 10);
70 CFStringRef cfaccount = CFSTR("test@test.org");
72 CFMutableDictionaryRef changes = CFDictionaryCreateMutableForCFTypes(kCFAllocatorDefault);
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"));
78 ok(SOSAccountAssertUserCredentialsAndUpdate(bob_account, cfaccount, cfpassword, &error), "Credential setting (%@)", error);
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");
83 ok(SOSAccountAssertUserCredentialsAndUpdate(alice_account, cfaccount, cfpassword, &error), "Credential setting (%@)", error);
86 ok(SOSAccountAssertUserCredentialsAndUpdate(carole_account, cfaccount, cfpassword, &error), "Credential setting (%@)", error);
88 ok(SOSAccountResetToOffering_wTxn(alice_account, &error), "Reset to offering (%@)", error);
91 is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, carole_account, NULL), 2, "updates");
93 ok(SOSAccountJoinCircles_wTxn(bob_account, &error), "Bob Applies (%@)", error);
95 is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, carole_account, NULL), 2, "updates");
98 CFArrayRef applicants = SOSAccountCopyApplicants(alice_account, &error);
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);
106 is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, carole_account, NULL), 3, "updates");
108 accounts_agree("bob&alice pair", bob_account, alice_account);
110 /*----- normal join after restore -----*/
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");
116 is(countApplicants(alice_account), 0, "See no applicants");
118 is(countPeers(carole_account), 3, "Carole sees 3 valid peers after sliding in");
120 is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, carole_account, NULL), 1, "updates");
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);
125 ok([carole_account.trust leaveCircle:carole_account err:&error], "Carol Leaves again");
126 CFReleaseNull(error);
128 is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, carole_account, NULL), 2, "updates");
130 /*----- join - join after restore -----*/
132 ok(SOSAccountJoinCircles_wTxn(carole_account, &error), "Carole normally joins (%@)", error);
133 CFReleaseNull(error);
135 is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, carole_account, NULL), 2, "updates");
137 is(countApplicants(alice_account), 1, "See one applicant");
139 ok(SOSAccountJoinCirclesAfterRestore_wTxn(carole_account, &error), "Carole cloud identity joins (%@)", error);
140 CFReleaseNull(error);
142 is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, carole_account, NULL), 4, "updates");
145 is(countApplicants(alice_account), 0, "See no applicants");
147 is(countPeers(carole_account), 3, "Carole sees 3 valid peers after sliding in");
149 is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, carole_account, NULL), 1, "updates");
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);
154 /* Break iCloud identity FPI in all peers */
156 ok(purgeICloudIdentity(alice_account), "remove iCloud private key");
158 is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, carole_account, NULL), 1, "updates");
161 ok(SOSAccountAssertUserCredentialsAndUpdate(alice_account, cfaccount, cfpassword, &error), "Credential setting (%@)", error);
162 CFReleaseNull(error);
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");
168 is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, carole_account, NULL), 1, "updates");
170 ok([carole_account.trust leaveCircle:carole_account err:&error], "Carol Leaves again");
171 CFReleaseNull(error);
173 is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, carole_account, NULL), 4, "updates");
175 /*----- join - join after restore -----*/
177 ok(SOSAccountJoinCircles_wTxn(carole_account, &error), "Carole normally joins (%@)", error);
178 CFReleaseNull(error);
180 is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, carole_account, NULL), 2, "updates");
182 is(countApplicants(alice_account), 1, "See one applicant");
184 ok(SOSAccountJoinCirclesAfterRestore_wTxn(carole_account, &error), "Carole cloud identity joins (%@)", error);
185 CFReleaseNull(error);
187 is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, carole_account, NULL), 4, "updates");
190 is(countApplicants(alice_account), 0, "See no applicants");
192 is(countPeers(carole_account), 3, "Carole sees 3 valid peers after sliding in");
194 is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, carole_account, NULL), 1, "updates");
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);
199 //join after piggybacking the icloud identity??
200 CFMutableArrayRef identityArray = SOSAccountCopyiCloudIdentities(alice_account);
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));
208 [encodedIdenities addObject:data];
211 //store in keychain as the piggy icloud
212 [encodedIdenities enumerateObjectsUsingBlock:^(NSData *v_data, NSUInteger idx, BOOL *stop) {
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,
224 OSStatus status = SecItemAdd((__bridge CFDictionaryRef) query, NULL);
226 if(status == errSecDuplicateItem) {
227 // Sure, okay, fine, we'll update.
228 NSMutableDictionary* update = [@{
229 (id)kSecValueData: v_data,
231 query[(id)kSecValueData] = nil;
233 status = SecItemUpdate((__bridge CFDictionaryRef) query, (__bridge CFDictionaryRef)update);
234 ok(status == 0, "Grabbed icloud identity from the keychain %@", error);
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,
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);
254 NSDictionary *keyAttributes = @{
255 (__bridge id)kSecAttrKeyClass : (__bridge id)kSecAttrKeyClassPrivate,
256 (__bridge id)kSecAttrKeyType : (__bridge id)kSecAttrKeyTypeEC,
260 SecKeyRef privKey = SecKeyCreateWithData(result, (__bridge CFDictionaryRef)keyAttributes, NULL);
262 ok(privKey != NULL, "Private key is NULL");
263 SecKeyRef publicKey = SecKeyCreatePublicFromPrivate(privKey);
264 ok(publicKey != NULL, "Private key is NULL");
266 CFDataRef public_key_hash = SecKeyCopyPublicKeyHash(publicKey);
267 ok(public_key_hash != NULL, "hash is not null");
269 CFReleaseNull(publicKey);
271 SOSAccount* margaret_account = CreateAccountForLocalChanges(CFSTR("margaret"), CFSTR("TestSource"));
273 ok(SOSAccountAssertUserCredentialsAndUpdate(margaret_account, cfaccount, cfpassword, &error), "Credential setting (%@)", error);
274 ok(SOSAccountJoinCirclesAfterRestore_wTxn(margaret_account, &error), "Carole cloud identity joins (%@)", error);
276 CFReleaseNull(identityArray);
277 CFReleaseNull(changes);
278 CFReleaseNull(error);
279 CFReleaseNull(public_key_hash);
280 CFReleaseNull(cfpassword);
281 CFReleaseNull(privKey);
289 int secd_60_account_cloud_identity(int argc, char *const *argv)
293 secd_test_setup_temp_keychain(__FUNCTION__, NULL);