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