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