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@
27 #include "secd_regressions.h"
29 #include <CoreFoundation/CFData.h>
30 #include <Security/SecOTRSession.h>
31 #include <Security/SecOTRIdentityPriv.h>
32 #include <Security/SecInternal.h>
33 #include <Security/SecBasePriv.h>
34 #include <Security/SecKeyPriv.h>
36 #include <Security/SecureObjectSync/SOSPeerInfo.h>
37 #include "keychain/SecureObjectSync/SOSCircle.h"
38 #include <Security/SecureObjectSync/SOSCloudCircle.h>
39 #include "keychain/SecureObjectSync/SOSInternal.h"
40 #include "keychain/SecureObjectSync/SOSUserKeygen.h"
41 #include "keychain/SecureObjectSync/SOSTransport.h"
43 #include "SOSCircle_regressions.h"
44 #include "SOSRegressionUtilities.h"
45 #include "SOSTestDataSource.h"
46 #include "SecOTRRemote.h"
47 #include "SOSAccount.h"
48 #include "SOSAccountTesting.h"
50 #include "SecdTestKeychainUtilities.h"
54 static void RegressionsLogError(CFErrorRef error) {
58 CFDictionaryRef tempDictionary = CFErrorCopyUserInfo(error);
59 CFIndex errorCode = CFErrorGetCode(error);
60 CFStringRef errorDomain = CFErrorGetDomain(error);
61 CFStringRef errorString = CFDictionaryGetValue(tempDictionary, kCFErrorDescriptionKey);
62 CFErrorRef previousError = (CFErrorRef)CFDictionaryGetValue(tempDictionary, kCFErrorUnderlyingErrorKey);
63 if (previousError != NULL) {
64 RegressionsLogError(previousError);
66 char errorDomainStr[1024];
67 char errorStringStr[1024];
69 CFStringGetCString(errorDomain, errorDomainStr, 1024, kCFStringEncodingUTF8);
70 CFStringGetCString(errorString, errorStringStr, 1024, kCFStringEncodingUTF8);
71 printf("OTR: %s (%ld) -- %s\n", errorDomainStr, errorCode, errorStringStr);
72 CFReleaseSafe(tempDictionary);
75 static int kTestTestCount = 11;
76 static void tests(void)
78 NSError* ns_testError = nil;
79 __block CFErrorRef testError = NULL;
81 CFDataRef cfpassword = CFDataCreate(NULL, (uint8_t *) "FooFooFoo", 10);
83 CFStringRef circleName = CFSTR("Woot Circle");
86 SOSDataSourceRef aliceDs = SOSTestDataSourceCreate();
87 SOSDataSourceRef bobDs = SOSTestDataSourceCreate();
89 SOSDataSourceFactoryRef aliceDsf = SOSTestDataSourceFactoryCreate();
90 SOSTestDataSourceFactorySetDataSource(aliceDsf, circleName, aliceDs);
92 SOSDataSourceFactoryRef bobDsf = SOSTestDataSourceFactoryCreate();
93 SOSTestDataSourceFactorySetDataSource(bobDsf, circleName, bobDs);
95 CFDictionaryRef alice_gestalt = SOSCreatePeerGestaltFromName(CFSTR("Alice Device"));
96 CFDictionaryRef bob_gestalt = SOSCreatePeerGestaltFromName(CFSTR("Bob Device"));
98 SOSAccount* alice_account = SOSAccountCreate(kCFAllocatorDefault, alice_gestalt, aliceDsf);
99 SOSAccount* bob_account = SOSAccountCreate(kCFAllocatorDefault, bob_gestalt, bobDsf);
101 SOSAccountAssertUserCredentialsAndUpdate(alice_account, CFSTR("alice"), cfpassword, &testError);
102 SOSAccountAssertUserCredentialsAndUpdate(bob_account, CFSTR("bob"), cfpassword, &testError);
104 CFReleaseNull(cfpassword);
106 SOSAccountJoinCircles_wTxn(alice_account, &testError);
107 SOSAccountJoinCircles_wTxn(bob_account, &testError);
109 NSData* alice_account_data = [alice_account encodedData:&ns_testError];
110 NSData* bob_account_data = [bob_account encodedData:&ns_testError];;
112 CFArrayRef alice_peers = SOSAccountCopyPeers(alice_account, &testError);
113 CFArrayRef bob_peers = SOSAccountCopyPeers(bob_account, &testError);
115 SOSPeerInfoRef alice_peer_info = (SOSPeerInfoRef)CFArrayGetValueAtIndex(alice_peers, 0);
116 SOSPeerInfoRef bob_peer_info = (SOSPeerInfoRef)CFArrayGetValueAtIndex(bob_peers, 0);
118 CFStringRef alice_peer_id = SOSPeerInfoGetPeerID(alice_peer_info);
119 CFStringRef bob_peer_id = SOSPeerInfoGetPeerID(bob_peer_info);
121 CFDataRef alice_peer_external_form = CFStringCreateExternalRepresentation(kCFAllocatorDefault, alice_peer_id, kCFStringEncodingUTF8, '?');
122 CFDataRef bob_peer_external_form = CFStringCreateExternalRepresentation(kCFAllocatorDefault, bob_peer_id, kCFStringEncodingUTF8, '?');
124 bool aliceReady = false;
125 bool bobReady = false;
127 CFDataRef aliceSideSession = SecOTRSessionCreateRemote_internal((__bridge CFDataRef) bob_account_data, bob_peer_external_form, (__bridge CFDataRef) alice_account_data, &testError);
128 RegressionsLogError(testError);
129 CFReleaseNull(testError);
131 ok(aliceSideSession != NULL, "Make Alice side remote session");
133 CFDataRef bobSideSession = SecOTRSessionCreateRemote_internal((__bridge CFDataRef) alice_account_data, alice_peer_external_form, (__bridge CFDataRef) bob_account_data, &testError);
134 RegressionsLogError(testError);
135 CFReleaseNull(testError);
137 ok(bobSideSession != NULL, "Make Bob side remote session");
139 CFDataRef aliceSideSessionResult = NULL;
140 CFDataRef bobSideSessionResult = NULL;
141 CFDataRef aliceToBob = NULL;
142 CFDataRef bobToAlice = NULL;
145 bool aliceStatus = SecOTRSessionProcessPacketRemote(aliceSideSession, bobToAlice, &aliceSideSessionResult, &aliceToBob, &aliceReady, &testError);
146 ok (aliceStatus, "Alice sent packet OK");
147 RegressionsLogError(testError);
148 CFReleaseNull(testError);
149 CFReleaseSafe(aliceSideSession);
150 aliceSideSession = aliceSideSessionResult;
156 bool bobStatus = SecOTRSessionProcessPacketRemote(bobSideSession, aliceToBob, &bobSideSessionResult, &bobToAlice, &bobReady, &testError);
157 ok (bobStatus, "Bob sent packet OK");
158 RegressionsLogError(testError);
159 CFReleaseNull(testError);
160 CFReleaseSafe(bobSideSession);
161 bobSideSession = bobSideSessionResult;
164 ok(bobReady, "Bob finished negotiating at the same time as Alice.");
166 CFReleaseNull(aliceSideSession);
167 CFReleaseNull(bobSideSession);
168 SOSDataSourceRelease(aliceDs, NULL);
169 SOSDataSourceFactoryRelease(aliceDsf);
171 SOSDataSourceRelease(bobDs, NULL);
172 SOSDataSourceFactoryRelease(bobDsf);
174 SecOTRFIPurgeAllFromKeychain(&testError);
175 RegressionsLogError(testError);
176 CFReleaseNull(bob_peer_external_form);
177 CFReleaseNull(alice_peer_external_form);
178 CFReleaseNull(alice_peers);
179 CFReleaseNull(bob_peers);
180 CFReleaseNull(aliceSideSession);
181 CFReleaseNull(bobSideSession);
182 CFReleaseNull(testError);
186 int secd_70_otr_remote(int argc, char *const *argv)
189 plan_tests(kTestTestCount);
190 secd_test_setup_temp_keychain(__FUNCTION__, NULL);