]> git.saurik.com Git - apple/security.git/blob - OSX/sec/securityd/Regressions/secd-70-otr-remote.c
Security-57740.1.18.tar.gz
[apple/security.git] / OSX / sec / securityd / Regressions / secd-70-otr-remote.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 <stdio.h>
26
27 #include "secd_regressions.h"
28
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>
35
36 #include <Security/SecureObjectSync/SOSPeerInfo.h>
37 #include <Security/SecureObjectSync/SOSCircle.h>
38 #include <Security/SecureObjectSync/SOSCloudCircle.h>
39 #include <Security/SecureObjectSync/SOSInternal.h>
40 #include <Security/SecureObjectSync/SOSUserKeygen.h>
41 #include <Security/SecureObjectSync/SOSTransport.h>
42
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"
49
50 #include "SecdTestKeychainUtilities.h"
51
52
53 static void RegressionsLogError(CFErrorRef error) {
54 if (error == NULL) {
55 return;
56 }
57 CFDictionaryRef tempDictionary = CFErrorCopyUserInfo(error);
58 CFIndex errorCode = CFErrorGetCode(error);
59 CFStringRef errorDomain = CFErrorGetDomain(error);
60 CFStringRef errorString = CFDictionaryGetValue(tempDictionary, kCFErrorDescriptionKey);
61 CFErrorRef previousError = (CFErrorRef)CFDictionaryGetValue(tempDictionary, kCFErrorUnderlyingErrorKey);
62 if (previousError != NULL) {
63 RegressionsLogError(previousError);
64 }
65 char errorDomainStr[1024];
66 char errorStringStr[1024];
67
68 CFStringGetCString(errorDomain, errorDomainStr, 1024, kCFStringEncodingUTF8);
69 CFStringGetCString(errorString, errorStringStr, 1024, kCFStringEncodingUTF8);
70 printf("OTR: %s (%ld) -- %s\n", errorDomainStr, errorCode, errorStringStr);
71 CFReleaseSafe(tempDictionary);
72 }
73
74 static int kTestTestCount = 11;
75 static void tests(void)
76 {
77 __block CFErrorRef testError = NULL;
78
79 CFDataRef cfpassword = CFDataCreate(NULL, (uint8_t *) "FooFooFoo", 10);
80
81 CFStringRef circleName = CFSTR("Woot Circle");
82
83 /* DataSource */
84 SOSDataSourceRef aliceDs = SOSTestDataSourceCreate();
85 SOSDataSourceRef bobDs = SOSTestDataSourceCreate();
86
87 SOSDataSourceFactoryRef aliceDsf = SOSTestDataSourceFactoryCreate();
88 SOSTestDataSourceFactorySetDataSource(aliceDsf, circleName, aliceDs);
89
90 SOSDataSourceFactoryRef bobDsf = SOSTestDataSourceFactoryCreate();
91 SOSTestDataSourceFactorySetDataSource(bobDsf, circleName, bobDs);
92
93 CFDictionaryRef alice_gestalt = SOSCreatePeerGestaltFromName(CFSTR("Alice Device"));
94 CFDictionaryRef bob_gestalt = SOSCreatePeerGestaltFromName(CFSTR("Bob Device"));
95
96 SOSAccountRef alice_account = SOSAccountCreate(kCFAllocatorDefault, alice_gestalt, aliceDsf);
97 SOSAccountRef bob_account = SOSAccountCreate(kCFAllocatorDefault, bob_gestalt, bobDsf);
98
99 SOSAccountAssertUserCredentialsAndUpdate(alice_account, CFSTR("alice"), cfpassword, &testError);
100 SOSAccountAssertUserCredentialsAndUpdate(bob_account, CFSTR("bob"), cfpassword, &testError);
101
102 CFReleaseNull(cfpassword);
103
104 SOSAccountJoinCircles_wTxn(alice_account, &testError);
105 SOSAccountJoinCircles_wTxn(bob_account, &testError);
106
107 CFDataRef alice_account_data = SOSAccountCopyEncodedData(alice_account, kCFAllocatorDefault, &testError);
108 CFDataRef bob_account_data = SOSAccountCopyEncodedData(bob_account, kCFAllocatorDefault, &testError);
109
110 CFArrayRef alice_peers = SOSAccountCopyPeers(alice_account, &testError);
111 CFArrayRef bob_peers = SOSAccountCopyPeers(bob_account, &testError);
112
113 SOSPeerInfoRef alice_peer_info = (SOSPeerInfoRef)CFArrayGetValueAtIndex(alice_peers, 0);
114 SOSPeerInfoRef bob_peer_info = (SOSPeerInfoRef)CFArrayGetValueAtIndex(bob_peers, 0);
115
116 CFStringRef alice_peer_id = SOSPeerInfoGetPeerID(alice_peer_info);
117 CFStringRef bob_peer_id = SOSPeerInfoGetPeerID(bob_peer_info);
118
119 CFDataRef alice_peer_external_form = CFStringCreateExternalRepresentation(kCFAllocatorDefault, alice_peer_id, kCFStringEncodingUTF8, '?');
120 CFDataRef bob_peer_external_form = CFStringCreateExternalRepresentation(kCFAllocatorDefault, bob_peer_id, kCFStringEncodingUTF8, '?');
121
122 bool aliceReady = false;
123 bool bobReady = false;
124
125 CFDataRef aliceSideSession = SecOTRSessionCreateRemote_internal(bob_account_data, bob_peer_external_form, alice_account_data, &testError);
126 RegressionsLogError(testError);
127 CFReleaseNull(testError);
128
129 ok(aliceSideSession != NULL, "Make Alice side remote session");
130
131 CFDataRef bobSideSession = SecOTRSessionCreateRemote_internal(alice_account_data, alice_peer_external_form, bob_account_data, &testError);
132 RegressionsLogError(testError);
133 CFReleaseNull(testError);
134
135 ok(bobSideSession != NULL, "Make Bob side remote session");
136
137 CFDataRef aliceSideSessionResult = NULL;
138 CFDataRef bobSideSessionResult = NULL;
139 CFDataRef aliceToBob = NULL;
140 CFDataRef bobToAlice = NULL;
141
142 do {
143 bool aliceStatus = SecOTRSessionProcessPacketRemote(aliceSideSession, bobToAlice, &aliceSideSessionResult, &aliceToBob, &aliceReady, &testError);
144 ok (aliceStatus, "Alice sent packet OK");
145 RegressionsLogError(testError);
146 CFReleaseNull(testError);
147 CFReleaseSafe(aliceSideSession);
148 aliceSideSession = aliceSideSessionResult;
149
150 if (aliceReady) {
151 break;
152 }
153
154 bool bobStatus = SecOTRSessionProcessPacketRemote(bobSideSession, aliceToBob, &bobSideSessionResult, &bobToAlice, &bobReady, &testError);
155 ok (bobStatus, "Bob sent packet OK");
156 RegressionsLogError(testError);
157 CFReleaseNull(testError);
158 CFReleaseSafe(bobSideSession);
159 bobSideSession = bobSideSessionResult;
160 } while (1);
161
162 ok(bobReady, "Bob finished negotiating at the same time as Alice.");
163
164 CFReleaseNull(aliceSideSession);
165 CFReleaseNull(bobSideSession);
166 SOSDataSourceRelease(aliceDs, NULL);
167 SOSDataSourceFactoryRelease(aliceDsf);
168
169 SOSDataSourceRelease(bobDs, NULL);
170 SOSDataSourceFactoryRelease(bobDsf);
171
172 SecOTRFIPurgeAllFromKeychain(&testError);
173 RegressionsLogError(testError);
174 CFReleaseNull(bob_peer_external_form);
175 CFReleaseNull(alice_peer_external_form);
176 CFReleaseNull(alice_account_data);
177 CFReleaseNull(bob_account_data);
178 CFReleaseNull(alice_peers);
179 CFReleaseNull(bob_peers);
180 CFReleaseNull(aliceSideSession);
181 CFReleaseNull(bobSideSession);
182 CFReleaseNull(testError);
183 }
184
185 int secd_70_otr_remote(int argc, char *const *argv)
186 {
187 plan_tests(kTestTestCount);
188
189 secd_test_setup_temp_keychain(__FUNCTION__, NULL);
190
191 tests();
192
193 return 0;
194 }