]> git.saurik.com Git - apple/security.git/blob - SecurityTests/clxutils/clAppUtils/sslRingBufferThreads.cpp
Security-57031.1.35.tar.gz
[apple/security.git] / SecurityTests / clxutils / clAppUtils / sslRingBufferThreads.cpp
1 /*
2 * sslRingBufferThreads.cpp - SecureTransport client and server thread
3 * routines which use ringBufferIo for I/O (no sockets).
4 */
5
6 #include "sslRingBufferThreads.h"
7 #include <stdlib.h>
8 #include <pthread.h>
9 #include <stdio.h>
10 #include <strings.h>
11 #include <clAppUtils/sslAppUtils.h>
12 #include <utilLib/common.h>
13
14 #define LOG_TOP_IO 0
15 #if LOG_TOP_IO
16
17 static void logWrite(
18 char *who,
19 size_t written)
20 {
21 pthread_mutex_lock(&printfMutex);
22 printf("+++ %s wrote %4lu bytes\n", who, (unsigned long)written);
23 pthread_mutex_unlock(&printfMutex);
24 }
25
26 static void logRead(
27 char *who,
28 size_t bytesRead)
29 {
30 pthread_mutex_lock(&printfMutex);
31 printf("+++ %s read %4lu bytes\n", who, (unsigned long)bytesRead);
32 pthread_mutex_unlock(&printfMutex);
33 }
34
35 #else /* LOG_TOP_IO */
36 #define logWrite(who, w)
37 #define logRead(who, r)
38 #endif /* LOG_TOP_IO */
39
40 /* client thread - handshake and write a ton of data */
41 void *sslRbClientThread(void *arg)
42 {
43 SslRingBufferArgs *sslArgs = (SslRingBufferArgs *)arg;
44 OSStatus ortn;
45 SSLContextRef ctx = NULL;
46 RingBuffers ringBufs = {sslArgs->ringRead, sslArgs->ringWrite};
47 unsigned toMove = 0;
48 unsigned thisMove;
49
50 ortn = SSLNewContext(false, &ctx);
51 if(ortn) {
52 printSslErrStr("SSLNewContext", ortn);
53 goto cleanup;
54 }
55 ortn = SSLSetIOFuncs(ctx, ringReadFunc, ringWriteFunc);
56 if(ortn) {
57 printSslErrStr("SSLSetIOFuncs", ortn);
58 goto cleanup;
59 }
60 ortn = SSLSetConnection(ctx, (SSLConnectionRef)&ringBufs);
61 if(ortn) {
62 printSslErrStr("SSLSetConnection", ortn);
63 goto cleanup;
64 }
65 ortn = SSLSetEnabledCiphers(ctx, &sslArgs->cipherSuite, 1);
66 if(ortn) {
67 printSslErrStr("SSLSetEnabledCiphers", ortn);
68 goto cleanup;
69 }
70 if(sslArgs->idArray) {
71 ortn = SSLSetCertificate(ctx, sslArgs->idArray);
72 if(ortn) {
73 printSslErrStr("SSLSetCertificate", ortn);
74 goto cleanup;
75 }
76 }
77 if(sslArgs->trustedRoots) {
78 ortn = SSLSetTrustedRoots(ctx, sslArgs->trustedRoots, true);
79 if(ortn) {
80 printSslErrStr("SSLSetTrustedRoots", ortn);
81 goto cleanup;
82 }
83 }
84 SSLSetProtocolVersionEnabled(ctx, kSSLProtocolAll, false);
85 ortn = SSLSetProtocolVersionEnabled(ctx, sslArgs->prot, true);
86 if(ortn) {
87 printSslErrStr("SSLSetProtocolVersionEnabled", ortn);
88 goto cleanup;
89 }
90
91 /* tell main thread we're ready; wait for sync flag */
92 sslArgs->iAmReady = true;
93 while(!(*sslArgs->goFlag)) {
94 if(*sslArgs->abortFlag) {
95 goto cleanup;
96 }
97 }
98
99 /* GO handshake */
100 sslArgs->startHandshake = CFAbsoluteTimeGetCurrent();
101 do {
102 ortn = SSLHandshake(ctx);
103 if(*sslArgs->abortFlag) {
104 goto cleanup;
105 }
106 } while (ortn == errSSLWouldBlock);
107
108 if(ortn) {
109 printSslErrStr("SSLHandshake", ortn);
110 goto cleanup;
111 }
112
113 SSLGetNegotiatedCipher(ctx, &sslArgs->negotiatedCipher);
114 SSLGetNegotiatedProtocolVersion(ctx, &sslArgs->negotiatedProt);
115
116 sslArgs->startData = CFAbsoluteTimeGetCurrent();
117
118 toMove = sslArgs->xferSize;
119
120 if(toMove == 0) {
121 sslArgs->endData = sslArgs->startData;
122 goto cleanup;
123 }
124
125 /* GO data xfer */
126 do {
127 thisMove = sslArgs->chunkSize;
128 if(thisMove > toMove) {
129 thisMove = toMove;
130 }
131 size_t moved;
132 ortn = SSLWrite(ctx, sslArgs->xferBuf, thisMove, &moved);
133 /* should never fail - implemented as blocking */
134 if(ortn) {
135 printSslErrStr("SSLWrite", ortn);
136 goto cleanup;
137 }
138 logWrite("client", moved);
139 if(!sslArgs->runForever) {
140 toMove -= moved;
141 }
142 if(*sslArgs->abortFlag) {
143 goto cleanup;
144 }
145 } while(toMove || sslArgs->runForever);
146
147 sslArgs->endData = CFAbsoluteTimeGetCurrent();
148
149 cleanup:
150 if(ortn) {
151 *sslArgs->abortFlag = true;
152 }
153 if(*sslArgs->abortFlag && sslArgs->pauseOnError) {
154 /* abort for any reason - freeze! */
155 testError(CSSM_FALSE);
156 }
157 if(ctx) {
158 SSLClose(ctx);
159 SSLDisposeContext(ctx);
160 }
161 if(ortn) {
162 printf("***Client thread returning %lu\n", (unsigned long)ortn);
163 }
164 pthread_exit((void*)ortn);
165 /* NOT REACHED */
166 return (void *)ortn;
167
168 }
169
170 /* server function - like clientThread except it runs from the main thread */
171 /* handshake and read a ton of data */
172 OSStatus sslRbServerThread(SslRingBufferArgs *sslArgs)
173 {
174 OSStatus ortn;
175 SSLContextRef ctx = NULL;
176 RingBuffers ringBufs = {sslArgs->ringRead, sslArgs->ringWrite};
177 unsigned toMove = 0;
178 unsigned thisMove;
179
180 ortn = SSLNewContext(true, &ctx);
181 if(ortn) {
182 printSslErrStr("SSLNewContext", ortn);
183 goto cleanup;
184 }
185 ortn = SSLSetIOFuncs(ctx, ringReadFunc, ringWriteFunc);
186 if(ortn) {
187 printSslErrStr("SSLSetIOFuncs", ortn);
188 goto cleanup;
189 }
190 ortn = SSLSetConnection(ctx, (SSLConnectionRef)&ringBufs);
191 if(ortn) {
192 printSslErrStr("SSLSetConnection", ortn);
193 goto cleanup;
194 }
195 ortn = SSLSetEnabledCiphers(ctx, &sslArgs->cipherSuite, 1);
196 if(ortn) {
197 printSslErrStr("SSLSetEnabledCiphers", ortn);
198 goto cleanup;
199 }
200 if(sslArgs->idArray) {
201 ortn = SSLSetCertificate(ctx, sslArgs->idArray);
202 if(ortn) {
203 printSslErrStr("SSLSetCertificate", ortn);
204 goto cleanup;
205 }
206 }
207 if(sslArgs->trustedRoots) {
208 ortn = SSLSetTrustedRoots(ctx, sslArgs->trustedRoots, true);
209 if(ortn) {
210 printSslErrStr("SSLSetTrustedRoots", ortn);
211 goto cleanup;
212 }
213 }
214 SSLSetProtocolVersionEnabled(ctx, kSSLProtocolAll, false);
215 ortn = SSLSetProtocolVersionEnabled(ctx, sslArgs->prot, true);
216 if(ortn) {
217 printSslErrStr("SSLSetProtocolVersionEnabled", ortn);
218 goto cleanup;
219 }
220
221 /* tell client thread we're ready; wait for sync flag */
222 sslArgs->iAmReady = true;
223 while(!(*sslArgs->goFlag)) {
224 if(*sslArgs->abortFlag) {
225 goto cleanup;
226 }
227 }
228
229 /* GO handshake */
230 sslArgs->startHandshake = CFAbsoluteTimeGetCurrent();
231 do {
232 ortn = SSLHandshake(ctx);
233 if(*sslArgs->abortFlag) {
234 goto cleanup;
235 }
236 } while (ortn == errSSLWouldBlock);
237
238 if(ortn) {
239 printSslErrStr("SSLHandshake", ortn);
240 goto cleanup;
241 }
242
243 SSLGetNegotiatedCipher(ctx, &sslArgs->negotiatedCipher);
244 SSLGetNegotiatedProtocolVersion(ctx, &sslArgs->negotiatedProt);
245
246 sslArgs->startData = CFAbsoluteTimeGetCurrent();
247
248 toMove = sslArgs->xferSize;
249
250 if(toMove == 0) {
251 sslArgs->endData = sslArgs->startData;
252 goto cleanup;
253 }
254
255 /* GO data xfer */
256 do {
257 thisMove = sslArgs->xferSize;
258 if(thisMove > toMove) {
259 thisMove = toMove;
260 }
261 size_t moved;
262 ortn = SSLRead(ctx, sslArgs->xferBuf, thisMove, &moved);
263 switch(ortn) {
264 case noErr:
265 break;
266 case errSSLWouldBlock:
267 /* cool, try again */
268 ortn = noErr;
269 break;
270 default:
271 break;
272 }
273 if(ortn) {
274 printSslErrStr("SSLRead", ortn);
275 goto cleanup;
276 }
277 logRead("server", moved);
278 if(!sslArgs->runForever) {
279 toMove -= moved;
280 }
281 if(*sslArgs->abortFlag) {
282 goto cleanup;
283 }
284 } while(toMove || sslArgs->runForever);
285
286 sslArgs->endData = CFAbsoluteTimeGetCurrent();
287
288 cleanup:
289 if(ortn) {
290 *sslArgs->abortFlag = true;
291 }
292 if(*sslArgs->abortFlag && sslArgs->pauseOnError) {
293 /* abort for any reason - freeze! */
294 testError(CSSM_FALSE);
295 }
296 if(ctx) {
297 SSLClose(ctx);
298 SSLDisposeContext(ctx);
299 }
300 if(ortn) {
301 printf("***Server thread returning %lu\n", (unsigned long)ortn);
302 }
303 return ortn;
304 }