]> git.saurik.com Git - apple/security.git/blob - keychain/securityd/Regressions/secd-64-circlereset.m
Security-59306.41.2.tar.gz
[apple/security.git] / keychain / securityd / Regressions / secd-64-circlereset.m
1 //
2 // secd-64-circlereset.c
3 // sec
4 //
5 // Created by Richard Murphy on 7/22/15.
6 //
7 //
8
9
10
11 #include <Security/SecBase.h>
12 #include <Security/SecItem.h>
13
14 #include <CoreFoundation/CFDictionary.h>
15
16 #include "keychain/SecureObjectSync/SOSAccount.h"
17 #include <Security/SecureObjectSync/SOSCloudCircle.h>
18 #include "keychain/SecureObjectSync/SOSInternal.h"
19 #include "keychain/SecureObjectSync/SOSUserKeygen.h"
20 #include "keychain/SecureObjectSync/SOSTransport.h"
21 #include "keychain/SecureObjectSync/SOSAccountTrustClassic+Circle.h"
22
23 #include <stdlib.h>
24 #include <unistd.h>
25
26 #include "secd_regressions.h"
27 #include "SOSTestDataSource.h"
28
29 #include "SOSRegressionUtilities.h"
30 #include <utilities/SecCFWrappers.h>
31 #include <Security/SecKeyPriv.h>
32
33 #include "keychain/securityd/SOSCloudCircleServer.h"
34
35 #include "SOSAccountTesting.h"
36
37 #include "SecdTestKeychainUtilities.h"
38
39 static int64_t getCurrentGenCount(SOSAccount* account) {
40 SOSAccountTrustClassic* trust = account.trust;
41 return SOSCircleGetGenerationSint(trust.trustedCircle);
42 }
43
44 static bool SOSAccountResetWithGenCountValue(SOSAccount* account, int64_t gcount, CFErrorRef* error) {
45 if (!SOSAccountHasPublicKey(account, error))
46 return false;
47 __block bool result = true;
48 SOSAccountTrustClassic* trust = account.trust;
49
50 result &= [account.trust resetAllRings:account err:error];
51
52 trust.fullPeerInfo = nil;
53 [trust setDepartureCode:kSOSWithdrewMembership];
54
55 result &= [account.trust modifyCircle:account.circle_transport err:error action:^(SOSCircleRef circle) {
56 SOSGenCountRef gencount = SOSGenerationCreateWithValue(gcount);
57 result = SOSCircleResetToEmpty(circle, error);
58 SOSCircleSetGeneration(circle, gencount);
59 CFReleaseNull(gencount);
60 return result;
61 }];
62
63 if (!result) {
64 secerror("error: %@", error ? *error : NULL);
65 }
66
67 return result;
68 }
69
70 static SOSCircleRef SOSCircleCreateWithGenCount(int64_t gcount) {
71 SOSCircleRef c = SOSCircleCreate(kCFAllocatorDefault, CFSTR("a"), NULL);
72 SOSGenCountRef gencount = SOSGenerationCreateWithValue(gcount);
73 SOSCircleSetGeneration(c, gencount);
74 CFReleaseNull(gencount);
75 return c;
76 }
77
78 static void tests(void)
79 {
80 CFErrorRef error = NULL;
81 CFDataRef cfpassword = CFDataCreate(NULL, (uint8_t *) "FooFooFoo", 10);
82 CFStringRef cfaccount = CFSTR("test@test.org");
83
84 SOSCircleRef c1 = SOSCircleCreateWithGenCount(1);
85 SOSCircleRef c99 = SOSCircleCreateWithGenCount(99);
86 ok(SOSCircleIsOlderGeneration(c1, c99), "Is Comparison working correctly?", NULL);
87 CFReleaseNull(c1);
88 CFReleaseNull(c99);
89
90
91 CFMutableDictionaryRef changes = CFDictionaryCreateMutableForCFTypes(kCFAllocatorDefault);
92 SOSAccount* alice_account = CreateAccountForLocalChanges(CFSTR("Alice"), CFSTR("TestSource"));
93 SOSAccount* bob_account = CreateAccountForLocalChanges(CFSTR("Bob"), CFSTR("TestSource"));
94
95 // Setup Circle with Bob and Alice in it
96 ok(SOSAccountAssertUserCredentialsAndUpdate(bob_account, cfaccount, cfpassword, &error), "Credential setting (%@)", error);
97 is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, NULL), 1, "updates");
98 ok(SOSAccountAssertUserCredentialsAndUpdate(alice_account, cfaccount, cfpassword, &error), "Credential setting (%@)", error);
99 CFReleaseNull(error);
100 ok(SOSAccountResetToOffering_wTxn(alice_account, &error), "Reset to offering (%@)", error);
101 CFReleaseNull(error);
102 is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, NULL), 2, "updates");
103 ok(SOSAccountJoinCircles_wTxn(bob_account, &error), "Bob Applies (%@)", error);
104 CFReleaseNull(error);
105 is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, NULL), 2, "updates");
106 {
107 CFArrayRef applicants = SOSAccountCopyApplicants(alice_account, &error);
108
109 ok(applicants && CFArrayGetCount(applicants) == 1, "See one applicant %@ (%@)", applicants, error);
110 ok(SOSAccountAcceptApplicants(alice_account, applicants, &error), "Alice accepts (%@)", error);
111 CFReleaseNull(error);
112 CFReleaseNull(applicants);
113 }
114 is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, NULL), 3, "updates");
115 accounts_agree("bob&alice pair", bob_account, alice_account);
116 CFArrayRef peers = SOSAccountCopyPeers(alice_account, &error);
117 ok(peers && CFArrayGetCount(peers) == 2, "See two peers %@ (%@)", peers, error);
118 CFReleaseNull(peers);
119
120 uint64_t cnt = getCurrentGenCount(alice_account);
121
122 ok(SOSAccountResetWithGenCountValue(alice_account, cnt-1, &error), "Alice resets the circle to empty with old value");
123 CFReleaseNull(error);
124
125 is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, NULL), 1, "updates");
126 is([bob_account getCircleStatus:&error], 0, "Bob Survives bad circle post");
127 is([alice_account getCircleStatus:&error], 1, "Alice does not survive bad circle post");
128
129 CFReleaseNull(cfpassword);
130 alice_account = nil;
131 bob_account = nil;
132 SOSTestCleanup();
133 }
134
135 int secd_64_circlereset(int argc, char *const *argv)
136 {
137 plan_tests(35);
138
139 secd_test_setup_temp_keychain(__FUNCTION__, NULL);
140
141 tests();
142
143 return 0;
144 }