]> git.saurik.com Git - apple/security.git/blob - OSX/sec/securityd/Regressions/secd-95-escrow-persistence.c
Security-57336.10.29.tar.gz
[apple/security.git] / OSX / sec / securityd / Regressions / secd-95-escrow-persistence.c
1 /*
2 * Copyright (c) 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 #include <Security/SecBase.h>
26 #include <Security/SecItem.h>
27
28 #include <CoreFoundation/CFDictionary.h>
29
30 #include <Security/SecureObjectSync/SOSAccount.h>
31 #include <Security/SecureObjectSync/SOSCloudCircle.h>
32 #include <Security/SecureObjectSync/SOSInternal.h>
33 #include <Security/SecureObjectSync/SOSUserKeygen.h>
34 #include <Security/SecureObjectSync/SOSTransport.h>
35
36 #include <stdlib.h>
37 #include <unistd.h>
38
39 #include "secd_regressions.h"
40 #include "SOSTestDataSource.h"
41
42 #include "SOSRegressionUtilities.h"
43 #include <utilities/SecCFWrappers.h>
44 #include <Security/SecKeyPriv.h>
45
46 #include <securityd/SOSCloudCircleServer.h>
47
48 #include "SOSAccountTesting.h"
49
50 #include "SecdTestKeychainUtilities.h"
51
52 static int kTestTestCount = 63;
53
54 static void tests(void)
55 {
56 CFErrorRef error = NULL;
57
58 CFDataRef cfpassword = CFDataCreate(NULL, (uint8_t *) "FooFooFoo", 10);
59 CFStringRef cfaccount = CFSTR("test@test.org");
60
61 CFMutableDictionaryRef changes = CFDictionaryCreateMutableForCFTypes(kCFAllocatorDefault);
62
63 SOSAccountRef alice_account = CreateAccountForLocalChanges(CFSTR("Alice"), CFSTR("TestSource"));
64 SOSAccountRef bob_account = CreateAccountForLocalChanges(CFSTR("Bob"), CFSTR("TestSource"));
65
66 ok(SOSAccountAssertUserCredentialsAndUpdate(bob_account, cfaccount, cfpassword, &error), "Credential setting (%@)", error);
67
68 // Bob wins writing at this point, feed the changes back to alice.
69 is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, NULL), 1, "updates");
70
71 ok(SOSAccountAssertUserCredentialsAndUpdate(alice_account, cfaccount, cfpassword, &error), "Credential setting (%@)", error);
72 CFReleaseNull(error);
73
74 CFReleaseNull(cfpassword);
75 CFReleaseNull(error);
76
77 ok(SOSAccountResetToOffering(alice_account, &error), "Reset to offering (%@)", error);
78 CFReleaseNull(error);
79
80 is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, NULL), 2, "updates");
81
82 ok(SOSAccountJoinCircles(bob_account, &error), "Bob Applies (%@)", error);
83 CFReleaseNull(error);
84
85 is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, NULL), 2, "updates");
86 {
87 CFArrayRef applicants = SOSAccountCopyApplicants(alice_account, &error);
88
89 ok(applicants && CFArrayGetCount(applicants) == 1, "See one applicants %@ (%@)", applicants, error);
90 CFReleaseNull(error);
91 CFReleaseSafe(applicants);
92 }
93
94 is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, NULL), 1, "updates");
95 CFMutableStringRef timeDescription = CFStringCreateMutableCopy(kCFAllocatorDefault, 0, CFSTR("["));
96 CFAbsoluteTime currentTimeAndDate = CFAbsoluteTimeGetCurrent();
97
98 withStringOfAbsoluteTime(currentTimeAndDate, ^(CFStringRef decription) {
99 CFStringAppend(timeDescription, decription);
100 });
101 CFStringAppend(timeDescription, CFSTR("]"));
102
103 uint64_t tries = 5;
104
105 CFNumberRef attempts = CFNumberCreate(kCFAllocatorDefault, kCFNumberLongLongType, (const void*)&tries);
106
107 CFMutableArrayRef escrowTimeAndTries = CFArrayCreateMutableForCFTypes(kCFAllocatorDefault);
108 CFArrayAppendValue(escrowTimeAndTries, timeDescription);
109 CFArrayAppendValue(escrowTimeAndTries, attempts);
110 CFDictionaryRef escrowRecord = CFDictionaryCreateForCFTypes(kCFAllocatorDefault, CFSTR("account label"), escrowTimeAndTries, NULL);
111
112 CFMutableDictionaryRef record = CFDictionaryCreateMutableForCFTypes(kCFAllocatorDefault);
113 CFDictionaryAddValue(record, CFSTR("12345"), escrowRecord);
114
115 SOSFullPeerInfoRef alice_fpi = SOSAccountGetMyFullPeerInfo(alice_account);
116 ok(SOSFullPeerInfoAddEscrowRecord(alice_fpi, CFSTR("12345"), escrowRecord, &error), "Adding Escrow records to Alice FPI(%@)", error);
117 CFDictionaryRef fpi_escrow = SOSPeerInfoCopyEscrowRecord(SOSFullPeerInfoGetPeerInfo(alice_fpi));
118 ok(CFEqualSafe(CFDictionaryGetValue(fpi_escrow, CFSTR("12345")), escrowRecord), "Alice's FPI has escrow (%@)", error);
119
120 ok(SOSAccountAddEscrowRecords(bob_account, CFSTR("12345"), escrowRecord, &error), "Adding escrow to Bob's account (%@)", error);
121 CFReleaseNull(fpi_escrow);
122
123 fpi_escrow = (CFDictionaryRef)SOSAccountGetValue(bob_account, kSOSEscrowRecord, NULL);
124 ok(CFEqualSafe(CFDictionaryGetValue(fpi_escrow, CFSTR("12345")), escrowRecord), "Bob has escrow records in account (%@)", error);
125
126 ok(SOSAccountResetToEmpty(alice_account, &error), "Reset to offering (%@)", error);
127 is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, NULL), 2, "updates");
128
129 ok(SOSAccountResetToEmpty(bob_account, &error), "Reset to offering (%@)", error);
130 is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, NULL), 2, "updates");
131
132 ok(SOSAccountAddEscrowRecords(bob_account, CFSTR("12345"), escrowRecord, &error), "Adding escrow to Bob's account (%@)", error);
133
134 ok(SOSAccountResetToOffering(alice_account, &error), "Reset to offering (%@)", error);
135 is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, NULL), 2, "updates");
136
137 CFDictionaryRef bob_fpi_escrow = SOSPeerInfoCopyEscrowRecord(SOSFullPeerInfoGetPeerInfo(bob_account->my_identity));
138 ok(bob_fpi_escrow == NULL, "Bob's FPI escrow should be null");
139 CFReleaseNull(bob_fpi_escrow);
140
141 ok(SOSAccountJoinCircles(bob_account, &error), "Bob Applies (%@)", error);
142 bob_fpi_escrow = SOSPeerInfoCopyEscrowRecord(SOSFullPeerInfoGetPeerInfo(bob_account->my_identity));
143 ok(CFEqualSafe(CFDictionaryGetValue(bob_fpi_escrow, CFSTR("12345")), escrowRecord), "Bob has escrow records in account (%@)", error);
144 CFReleaseNull(bob_fpi_escrow);
145
146 is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, NULL), 2, "updates");
147
148 {
149 CFArrayRef applicants = SOSAccountCopyApplicants(alice_account, &error);
150 ok(applicants && CFArrayGetCount(applicants) == 1, "See one applicants %@ (%@)", applicants, error);
151 ok(SOSAccountAcceptApplicants(alice_account, applicants, &error), "Accept bob into the fold");
152 CFReleaseNull(error);
153 CFReleaseNull(applicants);
154 }
155 is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, NULL), 3, "updates");
156
157 fpi_escrow = (CFDictionaryRef)SOSAccountGetValue(bob_account, kSOSEscrowRecord, NULL);
158 ok(isNull(fpi_escrow), "Bob's escrow records in the account object should be gone");
159
160 CFReleaseNull(record);
161 CFReleaseNull(escrowRecord);
162 CFReleaseNull(timeDescription);
163 CFReleaseNull(attempts);
164 }
165
166 int secd_95_escrow_persistence(int argc, char *const *argv)
167 {
168 plan_tests(kTestTestCount);
169
170 secd_test_setup_temp_keychain(__FUNCTION__, NULL);
171
172 tests();
173
174 return 0;
175 }