2 * Copyright (c) 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@
25 #include <Security/SecBase.h>
26 #include <Security/SecItem.h>
28 #include <CoreFoundation/CFDictionary.h>
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"
39 #include "secd_regressions.h"
40 #include "SOSTestDataSource.h"
42 #include "SOSRegressionUtilities.h"
43 #include <utilities/SecCFWrappers.h>
44 #include <Security/SecKeyPriv.h>
46 #include "keychain/securityd/SOSCloudCircleServer.h"
48 #include "SOSAccountTesting.h"
50 #include "SecdTestKeychainUtilities.h"
52 static void tests(void)
54 CFErrorRef error = NULL;
56 CFDataRef cfpassword = CFDataCreate(NULL, (uint8_t *) "FooFooFoo", 10);
57 CFStringRef cfaccount = CFSTR("test@test.org");
59 CFMutableDictionaryRef changes = CFDictionaryCreateMutableForCFTypes(kCFAllocatorDefault);
61 SOSAccount* alice_account = CreateAccountForLocalChanges(CFSTR("Alice"), CFSTR("TestSource"));
62 SOSAccount* bob_account = CreateAccountForLocalChanges(CFSTR("Bob"), CFSTR("TestSource"));
64 ok(SOSAccountAssertUserCredentialsAndUpdate(bob_account, cfaccount, cfpassword, &error), "Credential setting (%@)", error);
66 // Bob wins writing at this point, feed the changes back to alice.
67 is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, NULL), 1, "updates");
69 ok(SOSAccountAssertUserCredentialsAndUpdate(alice_account, cfaccount, cfpassword, &error), "Credential setting (%@)", error);
72 CFReleaseNull(cfpassword);
75 ok(SOSAccountResetToOffering_wTxn(alice_account, &error), "Reset to offering (%@)", error);
78 is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, NULL), 2, "updates");
80 ok(SOSAccountJoinCircles_wTxn(bob_account, &error), "Bob Applies (%@)", error);
83 is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, NULL), 2, "updates");
85 CFArrayRef applicants = SOSAccountCopyApplicants(alice_account, &error);
87 ok(applicants && CFArrayGetCount(applicants) == 1, "See one applicants %@ (%@)", applicants, error);
89 CFReleaseSafe(applicants);
92 is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, NULL), 1, "updates");
93 CFMutableStringRef timeDescription = CFStringCreateMutableCopy(kCFAllocatorDefault, 0, CFSTR("["));
94 CFAbsoluteTime currentTimeAndDate = CFAbsoluteTimeGetCurrent();
96 withStringOfAbsoluteTime(currentTimeAndDate, ^(CFStringRef decription) {
97 CFStringAppend(timeDescription, decription);
99 CFStringAppend(timeDescription, CFSTR("]"));
103 CFNumberRef attempts = CFNumberCreate(kCFAllocatorDefault, kCFNumberIntType, &tries);
105 CFMutableArrayRef escrowTimeAndTries = CFArrayCreateMutableForCFTypes(kCFAllocatorDefault);
106 CFArrayAppendValue(escrowTimeAndTries, timeDescription);
107 CFArrayAppendValue(escrowTimeAndTries, attempts);
108 CFDictionaryRef escrowRecord = CFDictionaryCreateForCFTypes(kCFAllocatorDefault, CFSTR("account label"), escrowTimeAndTries, NULL);
110 CFMutableDictionaryRef record = CFDictionaryCreateMutableForCFTypes(kCFAllocatorDefault);
111 CFDictionaryAddValue(record, CFSTR("12345"), escrowRecord);
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);
118 ok(SOSAccountAddEscrowRecords(bob_account, CFSTR("12345"), escrowRecord, &error), "Adding escrow to Bob's account (%@)", error);
119 CFReleaseNull(fpi_escrow);
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" );
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" );
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");
132 ok(SOSAccountAddEscrowRecords(bob_account, CFSTR("12345"), escrowRecord, &error), "Adding escrow to Bob's account (%@)", error);
134 ok(SOSAccountResetToOffering_wTxn(alice_account, &error), "Reset to offering (%@)", error);
135 is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, NULL), 2, "updates");
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);
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);
147 is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, NULL), 2, "updates");
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);
156 is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, NULL), 3, "updates");
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");
161 CFReleaseNull(record);
162 CFReleaseNull(escrowRecord);
163 CFReleaseNull(timeDescription);
164 CFReleaseNull(attempts);
169 int secd_95_escrow_persistence(int argc, char *const *argv)
173 secd_test_setup_temp_keychain(__FUNCTION__, NULL);