2 * Copyright (c) 2012-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 void tests(void)
59 CFErrorRef error = NULL;
60 CFDataRef cfpassword = CFDataCreate(NULL, (uint8_t *) "FooFooFoo", 10);
61 CFDataRef cfwrong_password = CFDataCreate(NULL, (uint8_t *) "NotFooFooFoo", 10);
62 CFStringRef cfaccount = CFSTR("test@test.org");
64 CFMutableDictionaryRef changes = CFDictionaryCreateMutableForCFTypes(kCFAllocatorDefault);
65 SOSAccount* alice_account = CreateAccountForLocalChanges(CFSTR("Alice"), CFSTR("TestSource"));
66 SOSAccount* bob_account = CreateAccountForLocalChanges(CFSTR("Bob"), CFSTR("TestSource"));
67 SOSAccount* carol_account = CreateAccountForLocalChanges(CFSTR("Carol"), CFSTR("TestSource"));
69 SOSAccountTrustClassic *carolTrust = carol_account.trust;
70 ok(SOSAccountAssertUserCredentialsAndUpdate(bob_account, cfaccount, cfpassword, &error), "Credential setting (%@)", error);
72 // Bob wins writing at this point, feed the changes back to alice.
73 is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, NULL), 1, "updates");
75 ok(SOSAccountAssertUserCredentialsAndUpdate(alice_account, cfaccount, cfpassword, &error), "Credential setting (%@)", error);
77 ok(SOSAccountTryUserCredentials(alice_account, cfaccount, cfpassword, &error), "Credential trying (%@)", error);
79 ok(!SOSAccountTryUserCredentials(alice_account, cfaccount, cfwrong_password, &error), "Credential failing (%@)", error);
80 CFReleaseNull(cfwrong_password);
81 is(error ? CFErrorGetCode(error) : 0, kSOSErrorWrongPassword, "Expected SOSErrorWrongPassword");
84 ok(SOSAccountResetToOffering_wTxn(alice_account, &error), "Reset to offering (%@)", error);
87 is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, NULL), 2, "updates");
89 ok(SOSAccountJoinCircles_wTxn(bob_account, &error), "Bob Applies (%@)", error);
92 is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, NULL), 2, "updates");
95 CFArrayRef applicants = SOSAccountCopyApplicants(alice_account, &error);
97 ok(applicants && CFArrayGetCount(applicants) == 1, "See one applicant %@ (%@)", applicants, error);
98 ok(SOSAccountAcceptApplicants(alice_account, applicants, &error), "Alice accepts (%@)", error);
100 CFReleaseNull(applicants);
103 is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, NULL), 3, "updates");
105 accounts_agree("bob&alice pair", bob_account, alice_account);
107 CFArrayRef peers = SOSAccountCopyPeers(alice_account, &error);
108 ok(peers && CFArrayGetCount(peers) == 2, "See two peers %@ (%@)", peers, error);
109 CFReleaseNull(peers);
111 //bob now goes def while Alice does some stuff.
113 ok([alice_account.trust leaveCircle:alice_account err:&error], "ALICE LEAVES THE CIRCLE (%@)", error);
114 ok(SOSAccountResetToOffering_wTxn(alice_account, &error), "Alice resets to offering again (%@)", error);
116 is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, NULL), 2, "updates");
118 accounts_agree("bob&alice pair", bob_account, alice_account);
120 is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, carol_account, NULL), 1, "updates");
123 ok(SOSAccountAssertUserCredentialsAndUpdate(carol_account, cfaccount, cfpassword, &error), "Credential setting (%@)", error);
124 SOSAccountSetUserPublicTrustedForTesting(carol_account);
125 ok(SOSAccountResetToOffering_wTxn(carol_account, &error), "Carol is going to push a reset to offering (%@)", error);
127 int64_t valuePtr = 0;
128 CFNumberRef gencount = CFNumberCreate(kCFAllocatorDefault, kCFNumberCFIndexType, &valuePtr);
130 SOSCircleSetGeneration(carolTrust.trustedCircle, gencount);
132 SecKeyRef user_privkey = SOSUserKeygen(cfpassword, (__bridge CFDataRef)(carol_account.accountKeyDerivationParameters), &error);
133 CFNumberRef genCountTest = SOSCircleGetGeneration(carolTrust.trustedCircle);
135 CFNumberGetValue(genCountTest, kCFNumberCFIndexType, &testPtr);
138 SOSCircleSignOldStyleResetToOfferingCircle(carolTrust.trustedCircle, carolTrust.fullPeerInfo, user_privkey, &error);
140 SOSTransportCircleTestRemovePendingChange((SOSCircleStorageTransportTest*)carol_account.circle_transport, SOSCircleGetName(carolTrust.trustedCircle), NULL);
141 CFDataRef circle_data = SOSCircleCopyEncodedData(carolTrust.trustedCircle, kCFAllocatorDefault, &error);
143 [carol_account.circle_transport postCircle:SOSCircleGetName(carolTrust.trustedCircle) circleData:circle_data err:&error];
145 CFReleaseNull(circle_data);
147 genCountTest = SOSCircleGetGeneration(carolTrust.trustedCircle);
148 CFNumberGetValue(genCountTest, kCFNumberCFIndexType, &testPtr);
151 ok(SOSAccountAssertUserCredentialsAndUpdate(bob_account, cfaccount, cfpassword, &error), "Credential setting (%@)", error);
152 ok(SOSAccountAssertUserCredentialsAndUpdate(alice_account, cfaccount, cfpassword, &error), "Credential setting (%@)", error);
154 SOSAccountSetUserPublicTrustedForTesting(alice_account);
155 SOSAccountSetUserPublicTrustedForTesting(bob_account);
157 is(ProcessChangesUntilNoChange(changes, carol_account, alice_account, bob_account, NULL), 2, "updates");
159 is([alice_account getCircleStatus:&error],kSOSCCNotInCircle,"alice is not in the account (%@)", error);
160 is([bob_account getCircleStatus:&error], kSOSCCNotInCircle,"bob is not in the account (%@)", error);
161 is([carol_account getCircleStatus:&error], kSOSCCInCircle,"carol is in the account (%@)", error);
163 [carol_account iCloudIdentityStatus:^(NSData *json, NSError *error) {
164 diag("icloud identity JSON\n%s\n", [[[NSString alloc] initWithData:json encoding:NSUTF8StringEncoding] UTF8String]);
167 CFReleaseNull(gencount);
168 CFReleaseNull(cfpassword);
169 CFReleaseNull(user_privkey);
178 int secd_52_offering_gencount_reset(int argc, char *const *argv)
182 secd_test_setup_temp_keychain(__FUNCTION__, NULL);