]>
git.saurik.com Git - apple/security.git/blob - SecurityTests/clxutils/clAppUtils/sslRingBufferThreads.cpp
2 * sslRingBufferThreads.cpp - SecureTransport client and server thread
3 * routines which use ringBufferIo for I/O (no sockets).
6 #include "sslRingBufferThreads.h"
11 #include <clAppUtils/sslAppUtils.h>
12 #include <utilLib/common.h>
21 pthread_mutex_lock(&printfMutex
);
22 printf("+++ %s wrote %4lu bytes\n", who
, (unsigned long)written
);
23 pthread_mutex_unlock(&printfMutex
);
30 pthread_mutex_lock(&printfMutex
);
31 printf("+++ %s read %4lu bytes\n", who
, (unsigned long)bytesRead
);
32 pthread_mutex_unlock(&printfMutex
);
35 #else /* LOG_TOP_IO */
36 #define logWrite(who, w)
37 #define logRead(who, r)
38 #endif /* LOG_TOP_IO */
40 /* client thread - handshake and write a ton of data */
41 void *sslRbClientThread(void *arg
)
43 SslRingBufferArgs
*sslArgs
= (SslRingBufferArgs
*)arg
;
45 SSLContextRef ctx
= NULL
;
46 RingBuffers ringBufs
= {sslArgs
->ringRead
, sslArgs
->ringWrite
};
50 ortn
= SSLNewContext(false, &ctx
);
52 printSslErrStr("SSLNewContext", ortn
);
55 ortn
= SSLSetIOFuncs(ctx
, ringReadFunc
, ringWriteFunc
);
57 printSslErrStr("SSLSetIOFuncs", ortn
);
60 ortn
= SSLSetConnection(ctx
, (SSLConnectionRef
)&ringBufs
);
62 printSslErrStr("SSLSetConnection", ortn
);
65 ortn
= SSLSetEnabledCiphers(ctx
, &sslArgs
->cipherSuite
, 1);
67 printSslErrStr("SSLSetEnabledCiphers", ortn
);
70 if(sslArgs
->idArray
) {
71 ortn
= SSLSetCertificate(ctx
, sslArgs
->idArray
);
73 printSslErrStr("SSLSetCertificate", ortn
);
77 if(sslArgs
->trustedRoots
) {
78 ortn
= SSLSetTrustedRoots(ctx
, sslArgs
->trustedRoots
, true);
80 printSslErrStr("SSLSetTrustedRoots", ortn
);
84 SSLSetProtocolVersionEnabled(ctx
, kSSLProtocolAll
, false);
85 ortn
= SSLSetProtocolVersionEnabled(ctx
, sslArgs
->prot
, true);
87 printSslErrStr("SSLSetProtocolVersionEnabled", ortn
);
91 /* tell main thread we're ready; wait for sync flag */
92 sslArgs
->iAmReady
= true;
93 while(!(*sslArgs
->goFlag
)) {
94 if(*sslArgs
->abortFlag
) {
100 sslArgs
->startHandshake
= CFAbsoluteTimeGetCurrent();
102 ortn
= SSLHandshake(ctx
);
103 if(*sslArgs
->abortFlag
) {
106 } while (ortn
== errSSLWouldBlock
);
109 printSslErrStr("SSLHandshake", ortn
);
113 SSLGetNegotiatedCipher(ctx
, &sslArgs
->negotiatedCipher
);
114 SSLGetNegotiatedProtocolVersion(ctx
, &sslArgs
->negotiatedProt
);
116 sslArgs
->startData
= CFAbsoluteTimeGetCurrent();
118 toMove
= sslArgs
->xferSize
;
121 sslArgs
->endData
= sslArgs
->startData
;
127 thisMove
= sslArgs
->chunkSize
;
128 if(thisMove
> toMove
) {
132 ortn
= SSLWrite(ctx
, sslArgs
->xferBuf
, thisMove
, &moved
);
133 /* should never fail - implemented as blocking */
135 printSslErrStr("SSLWrite", ortn
);
138 logWrite("client", moved
);
139 if(!sslArgs
->runForever
) {
142 if(*sslArgs
->abortFlag
) {
145 } while(toMove
|| sslArgs
->runForever
);
147 sslArgs
->endData
= CFAbsoluteTimeGetCurrent();
151 *sslArgs
->abortFlag
= true;
153 if(*sslArgs
->abortFlag
&& sslArgs
->pauseOnError
) {
154 /* abort for any reason - freeze! */
155 testError(CSSM_FALSE
);
159 SSLDisposeContext(ctx
);
162 printf("***Client thread returning %lu\n", (unsigned long)ortn
);
164 pthread_exit((void*)ortn
);
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
)
175 SSLContextRef ctx
= NULL
;
176 RingBuffers ringBufs
= {sslArgs
->ringRead
, sslArgs
->ringWrite
};
180 ortn
= SSLNewContext(true, &ctx
);
182 printSslErrStr("SSLNewContext", ortn
);
185 ortn
= SSLSetIOFuncs(ctx
, ringReadFunc
, ringWriteFunc
);
187 printSslErrStr("SSLSetIOFuncs", ortn
);
190 ortn
= SSLSetConnection(ctx
, (SSLConnectionRef
)&ringBufs
);
192 printSslErrStr("SSLSetConnection", ortn
);
195 ortn
= SSLSetEnabledCiphers(ctx
, &sslArgs
->cipherSuite
, 1);
197 printSslErrStr("SSLSetEnabledCiphers", ortn
);
200 if(sslArgs
->idArray
) {
201 ortn
= SSLSetCertificate(ctx
, sslArgs
->idArray
);
203 printSslErrStr("SSLSetCertificate", ortn
);
207 if(sslArgs
->trustedRoots
) {
208 ortn
= SSLSetTrustedRoots(ctx
, sslArgs
->trustedRoots
, true);
210 printSslErrStr("SSLSetTrustedRoots", ortn
);
214 SSLSetProtocolVersionEnabled(ctx
, kSSLProtocolAll
, false);
215 ortn
= SSLSetProtocolVersionEnabled(ctx
, sslArgs
->prot
, true);
217 printSslErrStr("SSLSetProtocolVersionEnabled", ortn
);
221 /* tell client thread we're ready; wait for sync flag */
222 sslArgs
->iAmReady
= true;
223 while(!(*sslArgs
->goFlag
)) {
224 if(*sslArgs
->abortFlag
) {
230 sslArgs
->startHandshake
= CFAbsoluteTimeGetCurrent();
232 ortn
= SSLHandshake(ctx
);
233 if(*sslArgs
->abortFlag
) {
236 } while (ortn
== errSSLWouldBlock
);
239 printSslErrStr("SSLHandshake", ortn
);
243 SSLGetNegotiatedCipher(ctx
, &sslArgs
->negotiatedCipher
);
244 SSLGetNegotiatedProtocolVersion(ctx
, &sslArgs
->negotiatedProt
);
246 sslArgs
->startData
= CFAbsoluteTimeGetCurrent();
248 toMove
= sslArgs
->xferSize
;
251 sslArgs
->endData
= sslArgs
->startData
;
257 thisMove
= sslArgs
->xferSize
;
258 if(thisMove
> toMove
) {
262 ortn
= SSLRead(ctx
, sslArgs
->xferBuf
, thisMove
, &moved
);
266 case errSSLWouldBlock
:
267 /* cool, try again */
274 printSslErrStr("SSLRead", ortn
);
277 logRead("server", moved
);
278 if(!sslArgs
->runForever
) {
281 if(*sslArgs
->abortFlag
) {
284 } while(toMove
|| sslArgs
->runForever
);
286 sslArgs
->endData
= CFAbsoluteTimeGetCurrent();
290 *sslArgs
->abortFlag
= true;
292 if(*sslArgs
->abortFlag
&& sslArgs
->pauseOnError
) {
293 /* abort for any reason - freeze! */
294 testError(CSSM_FALSE
);
298 SSLDisposeContext(ctx
);
301 printf("***Server thread returning %lu\n", (unsigned long)ortn
);