]> git.saurik.com Git - apple/security.git/blob - keychain/securityd/Regressions/secd-52-offering-gencount-reset.m
Security-59306.101.1.tar.gz
[apple/security.git] / keychain / securityd / Regressions / secd-52-offering-gencount-reset.m
1 /*
2 * Copyright (c) 2012-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 void tests(void)
57 {
58 CFErrorRef error = NULL;
59 CFDataRef cfpassword = CFDataCreate(NULL, (uint8_t *) "FooFooFoo", 10);
60 CFDataRef cfwrong_password = CFDataCreate(NULL, (uint8_t *) "NotFooFooFoo", 10);
61 CFStringRef cfaccount = CFSTR("test@test.org");
62
63 CFMutableDictionaryRef changes = CFDictionaryCreateMutableForCFTypes(kCFAllocatorDefault);
64 SOSAccount* alice_account = CreateAccountForLocalChanges(CFSTR("Alice"), CFSTR("TestSource"));
65 SOSAccount* bob_account = CreateAccountForLocalChanges(CFSTR("Bob"), CFSTR("TestSource"));
66 SOSAccount* carol_account = CreateAccountForLocalChanges(CFSTR("Carol"), CFSTR("TestSource"));
67
68 SOSAccountTrustClassic *carolTrust = carol_account.trust;
69 ok(SOSAccountAssertUserCredentialsAndUpdate(bob_account, cfaccount, cfpassword, &error), "Credential setting (%@)", error);
70
71 // Bob wins writing at this point, feed the changes back to alice.
72 is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, NULL), 1, "updates");
73
74 ok(SOSAccountAssertUserCredentialsAndUpdate(alice_account, cfaccount, cfpassword, &error), "Credential setting (%@)", error);
75 CFReleaseNull(error);
76 ok(SOSAccountTryUserCredentials(alice_account, cfaccount, cfpassword, &error), "Credential trying (%@)", error);
77 CFReleaseNull(error);
78 ok(!SOSAccountTryUserCredentials(alice_account, cfaccount, cfwrong_password, &error), "Credential failing (%@)", error);
79 CFReleaseNull(cfwrong_password);
80 is(error ? CFErrorGetCode(error) : 0, kSOSErrorWrongPassword, "Expected SOSErrorWrongPassword");
81 CFReleaseNull(error);
82
83 ok(SOSAccountResetToOffering_wTxn(alice_account, &error), "Reset to offering (%@)", error);
84 CFReleaseNull(error);
85
86 is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, NULL), 2, "updates");
87
88 ok(SOSAccountJoinCircles_wTxn(bob_account, &error), "Bob Applies (%@)", error);
89 CFReleaseNull(error);
90
91 is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, NULL), 2, "updates");
92
93 {
94 CFArrayRef applicants = SOSAccountCopyApplicants(alice_account, &error);
95
96 ok(applicants && CFArrayGetCount(applicants) == 1, "See one applicant %@ (%@)", applicants, error);
97 ok(SOSAccountAcceptApplicants(alice_account, applicants, &error), "Alice accepts (%@)", error);
98 CFReleaseNull(error);
99 CFReleaseNull(applicants);
100 }
101
102 is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, NULL), 3, "updates");
103
104 accounts_agree("bob&alice pair", bob_account, alice_account);
105
106 CFArrayRef peers = SOSAccountCopyPeers(alice_account, &error);
107 ok(peers && CFArrayGetCount(peers) == 2, "See two peers %@ (%@)", peers, error);
108 CFReleaseNull(peers);
109
110 //bob now goes def while Alice does some stuff.
111
112 ok([alice_account.trust leaveCircle:alice_account err:&error], "ALICE LEAVES THE CIRCLE (%@)", error);
113 ok(SOSAccountResetToOffering_wTxn(alice_account, &error), "Alice resets to offering again (%@)", error);
114
115 is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, NULL), 2, "updates");
116
117 accounts_agree("bob&alice pair", bob_account, alice_account);
118
119 is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, carol_account, NULL), 1, "updates");
120
121
122 ok(SOSAccountAssertUserCredentialsAndUpdate(carol_account, cfaccount, cfpassword, &error), "Credential setting (%@)", error);
123 SOSAccountSetUserPublicTrustedForTesting(carol_account);
124 ok(SOSAccountResetToOffering_wTxn(carol_account, &error), "Carol is going to push a reset to offering (%@)", error);
125
126 int64_t valuePtr = 0;
127 CFNumberRef gencount = CFNumberCreate(kCFAllocatorDefault, kCFNumberCFIndexType, &valuePtr);
128
129 SOSCircleSetGeneration(carolTrust.trustedCircle, gencount);
130
131 SecKeyRef user_privkey = SOSUserKeygen(cfpassword, (__bridge CFDataRef)(carol_account.accountKeyDerivationParamters), &error);
132 CFNumberRef genCountTest = SOSCircleGetGeneration(carolTrust.trustedCircle);
133 CFIndex testPtr;
134 CFNumberGetValue(genCountTest, kCFNumberCFIndexType, &testPtr);
135 ok(testPtr== 0);
136
137 SOSCircleSignOldStyleResetToOfferingCircle(carolTrust.trustedCircle, carolTrust.fullPeerInfo, user_privkey, &error);
138
139 SOSTransportCircleTestRemovePendingChange((SOSCircleStorageTransportTest*)carol_account.circle_transport, SOSCircleGetName(carolTrust.trustedCircle), NULL);
140 CFDataRef circle_data = SOSCircleCopyEncodedData(carolTrust.trustedCircle, kCFAllocatorDefault, &error);
141 if (circle_data) {
142 [carol_account.circle_transport postCircle:SOSCircleGetName(carolTrust.trustedCircle) circleData:circle_data err:&error];
143 }
144 CFReleaseNull(circle_data);
145
146 genCountTest = SOSCircleGetGeneration(carolTrust.trustedCircle);
147 CFNumberGetValue(genCountTest, kCFNumberCFIndexType, &testPtr);
148 ok(testPtr== 0);
149
150 ok(SOSAccountAssertUserCredentialsAndUpdate(bob_account, cfaccount, cfpassword, &error), "Credential setting (%@)", error);
151 ok(SOSAccountAssertUserCredentialsAndUpdate(alice_account, cfaccount, cfpassword, &error), "Credential setting (%@)", error);
152
153 SOSAccountSetUserPublicTrustedForTesting(alice_account);
154 SOSAccountSetUserPublicTrustedForTesting(bob_account);
155
156 is(ProcessChangesUntilNoChange(changes, carol_account, alice_account, bob_account, NULL), 2, "updates");
157
158 is([alice_account getCircleStatus:&error],kSOSCCNotInCircle,"alice is not in the account (%@)", error);
159 is([bob_account getCircleStatus:&error], kSOSCCNotInCircle,"bob is not in the account (%@)", error);
160 is([carol_account getCircleStatus:&error], kSOSCCInCircle,"carol is in the account (%@)", error);
161
162 CFReleaseNull(gencount);
163 CFReleaseNull(cfpassword);
164 CFReleaseNull(user_privkey);
165 alice_account = nil;
166 bob_account = nil;
167 carol_account = nil;
168
169 SOSTestCleanup();
170 }
171
172 int secd_52_offering_gencount_reset(int argc, char *const *argv)
173 {
174 plan_tests(63);
175
176 secd_test_setup_temp_keychain(__FUNCTION__, NULL);
177
178 tests();
179
180 return 0;
181 }