]> begriffs open source - freertos/blob - FreeRTOS-Plus/CyaSSL/tests/api.c
Commit 3 RX100 low power demos.
[freertos] / FreeRTOS-Plus / CyaSSL / tests / api.c
1 /* api.c API unit tests
2  *
3  * Copyright (C) 2006-2012 Sawtooth Consulting Ltd.
4  *
5  * This file is part of CyaSSL.
6  *
7  * CyaSSL is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2 of the License, or
10  * (at your option) any later version.
11  *
12  * CyaSSL is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
20  */
21
22 #include <stdlib.h>
23 #include <cyassl/ssl.h>
24 #include <cyassl/test.h>
25 #include <tests/unit.h>
26
27 #define TEST_FAIL       (-1)
28 #define TEST_SUCCESS    (0)
29
30 static int test_CyaSSL_Init(void);
31 static int test_CyaSSL_Cleanup(void);
32 static int test_CyaSSL_Method_Allocators(void);
33 static int test_CyaSSL_CTX_new(CYASSL_METHOD *method);
34 #ifndef NO_FILESYSTEM
35 static int test_CyaSSL_CTX_use_certificate_file(void);
36 static int test_CyaSSL_CTX_use_PrivateKey_file(void);
37 static int test_CyaSSL_CTX_load_verify_locations(void);
38 static int test_server_CyaSSL_new(void);
39 static int test_client_CyaSSL_new(void);
40 static int test_CyaSSL_read_write(void);
41 #endif
42
43 /* test function helpers */
44 static int test_method(CYASSL_METHOD *method, const char *name);
45 static int test_method2(CYASSL_METHOD *method, const char *name);
46 #ifndef NO_FILESYSTEM
47 static int test_ucf(CYASSL_CTX *ctx, const char* file, int type,
48     int cond, const char* name);
49 static int test_upkf(CYASSL_CTX *ctx, const char* file, int type,
50     int cond, const char* name);
51 static int test_lvl(CYASSL_CTX *ctx, const char* file, const char* path,
52     int cond, const char* name);
53
54 THREAD_RETURN CYASSL_THREAD test_server_nofail(void*);
55 void test_client_nofail(void*);
56 #endif
57
58 static const char* bogusFile  = "/dev/null";
59 static const char* testingFmt = "   %s:";
60 static const char* resultFmt  = " %s\n";
61 static const char* passed     = "passed";
62 static const char* failed     = "failed";
63
64 /* List of methods found in echoserver.c that I'm skipping for the moment:
65  * - CyaSSL_CTX_set_session_cache_mode()
66  */
67
68 int ApiTest(void)
69 {
70     printf(" Begin API Tests\n");
71     test_CyaSSL_Init();
72     test_CyaSSL_Method_Allocators();
73     test_CyaSSL_CTX_new(CyaSSLv23_server_method());
74 #ifndef NO_FILESYSTEM
75     test_CyaSSL_CTX_use_certificate_file();
76     test_CyaSSL_CTX_use_PrivateKey_file();
77     test_CyaSSL_CTX_load_verify_locations();
78     test_server_CyaSSL_new();
79     test_client_CyaSSL_new();
80     test_CyaSSL_read_write();
81 #endif
82     test_CyaSSL_Cleanup();
83     printf(" End API Tests\n");
84
85     return TEST_SUCCESS;
86 }
87
88 int test_CyaSSL_Init(void)
89 {
90     int result;
91
92     printf(testingFmt, "CyaSSL_Init()");
93     result = CyaSSL_Init();
94     printf(resultFmt, result ? failed : passed);
95
96     return result;
97 }
98
99 static int test_CyaSSL_Cleanup(void)
100 {
101     int result;
102
103     printf(testingFmt, "CyaSSL_Cleanup()");
104     result = CyaSSL_Cleanup();
105     printf(resultFmt, result ? failed : passed);
106
107     return result;
108 }
109
110 int test_method(CYASSL_METHOD *method, const char *name)
111 {
112     printf(testingFmt, name);
113     if (method == NULL)
114     {
115         printf(resultFmt, failed);
116         return TEST_FAIL;
117     }
118     XFREE(method, 0, DYNAMIC_TYPE_METHOD);
119     printf(resultFmt, passed);
120     return TEST_SUCCESS;
121 }
122
123 int test_method2(CYASSL_METHOD *method, const char *name)
124 {
125     printf(testingFmt, name);
126     if (method != NULL)
127     {
128         XFREE(method, 0, DYNAMIC_TYPE_METHOD);
129         printf(resultFmt, failed);
130         return TEST_FAIL;
131     }
132     printf(resultFmt, passed);
133     return TEST_SUCCESS;
134 }
135
136 int test_CyaSSL_Method_Allocators(void)
137 {
138     test_method(CyaSSLv3_server_method(), "CyaSSLv3_server_method()");
139     test_method(CyaSSLv3_client_method(), "CyaSSLv3_client_method()");
140     test_method(CyaTLSv1_server_method(), "CyaTLSv1_server_method()");
141     test_method(CyaTLSv1_client_method(), "CyaTLSv1_client_method()");
142     test_method(CyaTLSv1_1_server_method(), "CyaTLSv1_1_server_method()");
143     test_method(CyaTLSv1_1_client_method(), "CyaTLSv1_1_client_method()");
144     test_method(CyaTLSv1_2_server_method(), "CyaTLSv1_2_server_method()");
145     test_method(CyaTLSv1_2_client_method(), "CyaTLSv1_2_client_method()");
146     test_method(CyaSSLv23_client_method(), "CyaSSLv23_client_method()");
147
148 #ifdef CYASSL_DTLS
149     test_method(CyaDTLSv1_server_method(), "CyaDTLSv1_server_method()");
150     test_method(CyaDTLSv1_client_method(), "CyaDTLSv1_client_method()");
151 #endif /* CYASSL_DTLS */
152
153 #ifdef OPENSSL_EXTRA
154     test_method2(CyaSSLv2_server_method(), "CyaSSLv2_server_method()");
155     test_method2(CyaSSLv2_client_method(), "CyaSSLv2_client_method()");
156 #endif /* OPENSSL_EXTRA */
157
158     return TEST_SUCCESS;
159 }
160
161 int test_CyaSSL_CTX_new(CYASSL_METHOD *method)
162 {
163     if (method != NULL)
164     {
165         CYASSL_CTX *ctx;
166     
167         printf(testingFmt, "CyaSSL_CTX_new(NULL)");
168         ctx = CyaSSL_CTX_new(NULL);
169         if (ctx != NULL)
170         {
171             CyaSSL_CTX_free(ctx);
172             printf(resultFmt, failed);
173         }
174         else
175             printf(resultFmt, passed);
176     
177         printf(testingFmt, "CyaSSL_CTX_new(method)");
178         ctx = CyaSSL_CTX_new(method);
179         if (ctx == NULL)
180         {
181             printf(resultFmt, failed);
182             XFREE(method, 0, DYNAMIC_TYPE_METHOD);
183             /* free the method data. if this was successful, freeing
184                the CTX frees the method. */
185         }
186         else
187         {
188             CyaSSL_CTX_free(ctx);
189             printf(resultFmt, passed);
190         }
191     }
192     else
193         printf("test_CyaSSL_CTX_new() called without method\n");
194
195     return TEST_SUCCESS;
196 }
197
198 #ifndef NO_FILESYSTEM
199 /* Helper for testing CyaSSL_CTX_use_certificate_file() */
200 int test_ucf(CYASSL_CTX *ctx, const char* file, int type, int cond,
201     const char* name)
202 {
203     int result;
204
205     printf(testingFmt, name);
206     result = CyaSSL_CTX_use_certificate_file(ctx, file, type);
207     if (result != cond)
208     {
209         printf(resultFmt, failed);
210         return TEST_FAIL;
211     }
212     printf(resultFmt, passed);
213     return TEST_SUCCESS;
214 }
215
216 int test_CyaSSL_CTX_use_certificate_file(void)
217 {
218     CYASSL_METHOD *method;
219     CYASSL_CTX *ctx;
220
221     method = CyaSSLv23_server_method();
222     if (method == NULL)
223     {
224         printf("test_CyaSSL_CTX_use_certificate_file() cannot create method\n");
225         return TEST_FAIL;
226     }
227
228     ctx = CyaSSL_CTX_new(method);
229     if (ctx == NULL)
230     {
231         printf("test_CyaSSL_CTX_use_certificate_file() cannot create context\n");
232         XFREE(method, 0, DYNAMIC_TYPE_METHOD);
233         return TEST_FAIL;
234     }
235
236     /* setting all parameters to garbage. this should succeed with
237         failure */
238     /* Then set the parameters to legit values but set each item to
239         bogus and call again. Finish with a successful success. */
240
241     test_ucf(NULL, NULL, 9999, SSL_FAILURE,
242         "CyaSSL_CTX_use_certificate_file(NULL, NULL, 9999)");
243 /*  test_ucf(NULL, svrCert, SSL_FILETYPE_PEM, SSL_FAILURE,
244         "CyaSSL_CTX_use_certificate_file(NULL, svrCert, SSL_FILETYPE_PEM)");*/
245     test_ucf(ctx, bogusFile, SSL_FILETYPE_PEM, SSL_FAILURE,
246         "CyaSSL_CTX_use_certificate_file(ctx, bogusFile, SSL_FILETYPE_PEM)");
247     test_ucf(ctx, svrCert, 9999, SSL_FAILURE,
248         "CyaSSL_CTX_use_certificate_file(ctx, svrCert, 9999)");
249     test_ucf(ctx, svrCert, SSL_FILETYPE_PEM, SSL_SUCCESS,
250         "CyaSSL_CTX_use_certificate_file(ctx, svrCert, SSL_FILETYPE_PEM)");
251
252     CyaSSL_CTX_free(ctx);
253     return TEST_SUCCESS;
254 }
255
256 /* Helper for testing CyaSSL_CTX_use_PrivateKey_file() */
257 int test_upkf(CYASSL_CTX *ctx, const char* file, int type, int cond,
258     const char* name)
259 {
260     int result;
261
262     printf(testingFmt, name);
263     result = CyaSSL_CTX_use_PrivateKey_file(ctx, file, type);
264     if (result != cond)
265     {
266         printf(resultFmt, failed);
267         return TEST_FAIL;
268     }
269     printf(resultFmt, passed);
270     return TEST_SUCCESS;
271 }
272
273 int test_CyaSSL_CTX_use_PrivateKey_file(void)
274 {
275     CYASSL_METHOD *method;
276     CYASSL_CTX *ctx;
277
278     method = CyaSSLv23_server_method();
279     if (method == NULL)
280     {
281         printf("test_CyaSSL_CTX_use_PrivateKey_file() cannot create method\n");
282         return TEST_FAIL;
283     }
284
285     ctx = CyaSSL_CTX_new(method);
286     if (ctx == NULL)
287     {
288         printf("test_CyaSSL_CTX_use_PrivateKey_file() cannot create context\n");
289         XFREE(method, 0, DYNAMIC_TYPE_METHOD);
290         return TEST_FAIL;
291     }
292
293     test_upkf(NULL, NULL, 9999, SSL_FAILURE,
294         "CyaSSL_CTX_use_PrivateKey_file(NULL, NULL, 9999)");
295 /*  test_upkf(NULL, svrKey, SSL_FILETYPE_PEM, SSL_FAILURE,
296         "CyaSSL_CTX_use_PrivateKey_file(NULL, svrKey, SSL_FILETYPE_PEM)");*/
297     test_upkf(ctx, bogusFile, SSL_FILETYPE_PEM, SSL_FAILURE,
298         "CyaSSL_CTX_use_PrivateKey_file(ctx, bogusFile, SSL_FILETYPE_PEM)");
299     test_upkf(ctx, svrKey, 9999, SSL_FAILURE,
300         "CyaSSL_CTX_use_PrivateKey_file(ctx, svrKey, 9999)");
301     test_upkf(ctx, svrKey, SSL_FILETYPE_PEM, SSL_SUCCESS,
302         "CyaSSL_CTX_use_PrivateKey_file(ctx, svrKey, SSL_FILETYPE_PEM)");
303
304     CyaSSL_CTX_free(ctx);
305     return TEST_SUCCESS;
306 }
307
308 /* Helper for testing CyaSSL_CTX_load_verify_locations() */
309 int test_lvl(CYASSL_CTX *ctx, const char* file, const char* path, int cond,
310     const char* name)
311 {
312     int result;
313
314     printf(testingFmt, name);
315     /*
316      * CyaSSL_CTX_load_verify_locations() returns SSL_SUCCESS (1) for 
317      * success, SSL_FAILURE (0) for a non-specific failure, or a specific
318      * failure code (<0). Need to normalize the return code to 1 or 0.
319      */
320     result = CyaSSL_CTX_load_verify_locations(ctx, file, path) >= SSL_SUCCESS;
321     if (result != cond)
322     {
323         printf(resultFmt, failed);
324         return TEST_FAIL;
325     }
326     printf(resultFmt, passed);
327     return TEST_SUCCESS;
328 }
329
330 int test_CyaSSL_CTX_load_verify_locations(void)
331 {
332     CYASSL_METHOD *method;
333     CYASSL_CTX *ctx;
334
335     method = CyaSSLv23_client_method();
336     if (method == NULL)
337     {
338         printf("test_CyaSSL_CTX_load_verify_locations() cannot create method\n");
339         return TEST_FAIL;
340     }
341
342     ctx = CyaSSL_CTX_new(method);
343     if (ctx == NULL)
344     {
345         printf("test_CyaSSL_CTX_load_verify_locations() cannot create context\n");
346         free(method);
347         return TEST_FAIL;
348     }
349     
350     test_lvl(NULL, NULL, NULL, SSL_FAILURE,
351         "CyaSSL_CTX_load_verify_locations(NULL, NULL, NULL)");
352     test_lvl(ctx, NULL, NULL, SSL_FAILURE,
353         "CyaSSL_CTX_load_verify_locations(ctx, NULL, NULL)");
354     test_lvl(NULL, caCert, NULL, SSL_FAILURE,
355         "CyaSSL_CTX_load_verify_locations(ctx, NULL, NULL)");
356     test_lvl(ctx, caCert, bogusFile, SSL_FAILURE,
357         "CyaSSL_CTX_load_verify_locations(ctx, caCert, bogusFile)");
358     /* Add a test for the certs directory path loading. */
359     /* There is a leak here. If you load a second cert, the first one
360        is lost. */
361     test_lvl(ctx, caCert, 0, SSL_SUCCESS,
362         "CyaSSL_CTX_load_verify_locations(ctx, caCert, 0)");
363
364     CyaSSL_CTX_free(ctx);
365     return TEST_SUCCESS;
366 }
367
368 int test_server_CyaSSL_new(void)
369 {
370     int result;
371     CYASSL_CTX *ctx;
372     CYASSL_CTX *ctx_nocert;
373     CYASSL *ssl;
374
375     ctx = CyaSSL_CTX_new(CyaSSLv23_server_method());
376     if (ctx == NULL)
377     {
378         printf("test_server_CyaSSL_new() cannot create context\n");
379         return TEST_FAIL;
380     }
381
382     result = CyaSSL_CTX_use_certificate_file(ctx, svrCert, SSL_FILETYPE_PEM);
383     if (result == SSL_FAILURE)
384     {
385         printf("test_server_CyaSSL_new() cannot obtain certificate\n");
386         CyaSSL_CTX_free(ctx);
387         return TEST_FAIL;
388     }
389
390     result = CyaSSL_CTX_use_PrivateKey_file(ctx, svrKey, SSL_FILETYPE_PEM);
391     if (result == SSL_FAILURE)
392     {
393         printf("test_server_CyaSSL_new() cannot obtain key\n");
394         CyaSSL_CTX_free(ctx);
395         return TEST_FAIL;
396     }
397
398     ctx_nocert = CyaSSL_CTX_new(CyaSSLv23_server_method());
399     if (ctx_nocert == NULL)
400     {
401         printf("test_server_CyaSSL_new() cannot create bogus context\n");
402         CyaSSL_CTX_free(ctx);
403         return TEST_FAIL;
404     }
405
406     printf(testingFmt, "CyaSSL_new(NULL) server");
407     ssl = CyaSSL_new(NULL);
408     if (ssl != NULL)
409     {
410         printf(resultFmt, failed);
411         CyaSSL_free(ssl);
412     }
413     else
414         printf(resultFmt, passed);
415
416     printf(testingFmt, "CyaSSL_new(ctx_nocert) server");
417     ssl = CyaSSL_new(ctx_nocert);
418     if (ssl != NULL)
419     {
420         printf(resultFmt, failed);
421         CyaSSL_free(ssl);
422     }
423     else
424         printf(resultFmt, passed);
425
426     printf(testingFmt, "CyaSSL_new(ctx) server");
427     ssl = CyaSSL_new(ctx);
428     if (ssl == NULL)
429         printf(resultFmt, failed);
430     else
431     {
432         printf(resultFmt, passed);
433         CyaSSL_free(ssl);
434     }
435     
436     CyaSSL_CTX_free(ctx_nocert);
437     CyaSSL_CTX_free(ctx);
438     return TEST_SUCCESS;
439 }
440
441 int test_client_CyaSSL_new(void)
442 {
443     int result;
444     CYASSL_CTX *ctx;
445     CYASSL_CTX *ctx_nocert;
446     CYASSL *ssl;
447
448     ctx = CyaSSL_CTX_new(CyaSSLv23_client_method());
449     if (ctx == NULL)
450     {
451         printf("test_client_CyaSSL_new() cannot create context\n");
452         return TEST_FAIL;
453     }
454
455     result = CyaSSL_CTX_load_verify_locations(ctx, caCert, 0);
456     if (result == SSL_FAILURE)
457     {
458         printf("test_client_CyaSSL_new() cannot obtain certificate\n");
459         CyaSSL_CTX_free(ctx);
460         return TEST_FAIL;
461     }
462
463     ctx_nocert = CyaSSL_CTX_new(CyaSSLv23_client_method());
464     if (ctx_nocert == NULL)
465     {
466         printf("test_client_CyaSSL_new() cannot create bogus context\n");
467         CyaSSL_CTX_free(ctx);
468         return TEST_FAIL;
469     }
470
471     printf(testingFmt, "CyaSSL_new(NULL) client");
472     ssl = CyaSSL_new(NULL);
473     if (ssl != NULL)
474     {
475         printf(resultFmt, failed);
476         CyaSSL_free(ssl);
477     }
478     else
479         printf(resultFmt, passed);
480
481     printf(testingFmt, "CyaSSL_new(ctx_nocert) client");
482     ssl = CyaSSL_new(ctx_nocert);
483     if (ssl == NULL)
484         printf(resultFmt, failed);
485     else
486     {
487         printf(resultFmt, passed);
488         CyaSSL_free(ssl);
489     }
490
491     printf(testingFmt, "CyaSSL_new(ctx) client");
492     ssl = CyaSSL_new(ctx);
493     if (ssl == NULL)
494         printf(resultFmt, failed);
495     else
496     {
497         printf(resultFmt, passed);
498         CyaSSL_free(ssl);
499     }
500
501     CyaSSL_CTX_free(ctx_nocert);
502     CyaSSL_CTX_free(ctx);
503     return TEST_SUCCESS;
504 }
505
506
507 static int test_CyaSSL_read_write(void)
508 {
509     /* The unit testing for read and write shall happen simutaneously, since
510      * one can't do anything with one without the other. (Except for a failure
511      * test case.) This function will call all the others that will set up,
512      * execute, and report their test findings.
513      *
514      * Set up the success case first. This function will become the template
515      * for the other tests. This should eventually be renamed
516      *
517      * The success case isn't interesting, how can this fail?
518      * - Do not give the client context a CA certificate. The connect should
519      *   fail. Do not need server for this?
520      * - Using NULL for the ssl object on server. Do not need client for this.
521      * - Using NULL for the ssl object on client. Do not need server for this.
522      * - Good ssl objects for client and server. Client write() without server
523      *   read().
524      * - Good ssl objects for client and server. Server write() without client
525      *   read().
526      * - Forgetting the password callback?
527     */
528     int test_result = TEST_SUCCESS;
529     tcp_ready ready;
530     func_args client_args;
531     func_args server_args;
532     THREAD_TYPE serverThread;
533
534     StartTCP();
535
536     InitTcpReady(&ready);
537     server_args.signal = &ready;
538     start_thread(test_server_nofail, &server_args, &serverThread);
539     wait_tcp_ready(&server_args);
540     test_client_nofail(&client_args);
541     join_thread(serverThread);
542
543     if (client_args.return_code != TEST_SUCCESS)
544     {
545         printf(resultFmt, "client failure");
546         test_result = TEST_FAIL;
547     }
548     if (server_args.return_code != TEST_SUCCESS)
549     {
550         printf(resultFmt, "server failure");
551         test_result = TEST_FAIL;
552     }
553
554     FreeTcpReady(&ready);
555
556     return test_result;
557 };
558
559
560 THREAD_RETURN CYASSL_THREAD test_server_nofail(void* args)
561 {
562     SOCKET_T sockfd = 0;
563     int clientfd = 0;
564
565     CYASSL_METHOD* method = 0;
566     CYASSL_CTX* ctx = 0;
567     CYASSL* ssl = 0;
568
569     char msg[] = "I hear you fa shizzle!";
570     char input[1024];
571     int idx;
572    
573     ((func_args*)args)->return_code = TEST_FAIL;
574     method = CyaSSLv23_server_method();
575     ctx = CyaSSL_CTX_new(method);
576
577     CyaSSL_CTX_set_verify(ctx,
578                     SSL_VERIFY_PEER | SSL_VERIFY_FAIL_IF_NO_PEER_CERT, 0);
579
580 #ifdef OPENSSL_EXTRA
581     CyaSSL_CTX_set_default_passwd_cb(ctx, PasswordCallBack);
582 #endif
583
584     if (CyaSSL_CTX_load_verify_locations(ctx, cliCert, 0) != SSL_SUCCESS)
585     {
586         /*err_sys("can't load ca file, Please run from CyaSSL home dir");*/
587         return 0;
588     }
589     if (CyaSSL_CTX_use_certificate_file(ctx, svrCert, SSL_FILETYPE_PEM)
590             != SSL_SUCCESS)
591     {
592         /*err_sys("can't load server cert chain file, "
593                 "Please run from CyaSSL home dir");*/
594         return 0;
595     }
596     if (CyaSSL_CTX_use_PrivateKey_file(ctx, svrKey, SSL_FILETYPE_PEM)
597             != SSL_SUCCESS)
598     {
599         /*err_sys("can't load server key file, "
600                 "Please run from CyaSSL home dir");*/
601         return 0;
602     }
603     ssl = CyaSSL_new(ctx);
604     tcp_accept(&sockfd, &clientfd, (func_args*)args, yasslPort, 0, 0);
605     CloseSocket(sockfd);
606
607     CyaSSL_set_fd(ssl, clientfd);
608
609 #ifdef NO_PSK
610     #if !defined(NO_FILESYSTEM) && defined(OPENSSL_EXTRA)
611         CyaSSL_SetTmpDH_file(ssl, dhParam, SSL_FILETYPE_PEM);
612     #else
613         SetDH(ssl);  /* will repick suites with DHE, higher priority than PSK */
614     #endif
615 #endif
616     if (CyaSSL_accept(ssl) != SSL_SUCCESS)
617     {
618         int err = CyaSSL_get_error(ssl, 0);
619         char buffer[80];
620         printf("error = %d, %s\n", err, CyaSSL_ERR_error_string(err, buffer));
621         /*err_sys("SSL_accept failed");*/
622         return 0;
623     }
624
625     idx = CyaSSL_read(ssl, input, sizeof(input));
626     if (idx > 0) {
627         input[idx] = 0;
628         printf("Client message: %s\n", input);
629     }
630     
631     if (CyaSSL_write(ssl, msg, sizeof(msg)) != sizeof(msg))
632     {
633         /*err_sys("SSL_write failed");*/
634         return 0;
635     }
636
637     CyaSSL_shutdown(ssl);
638     CyaSSL_free(ssl);
639     CyaSSL_CTX_free(ctx);
640     
641     CloseSocket(clientfd);
642     ((func_args*)args)->return_code = TEST_SUCCESS;
643     return 0;
644 }
645
646 void test_client_nofail(void* args)
647 {
648     SOCKET_T sockfd = 0;
649
650     CYASSL_METHOD*  method  = 0;
651     CYASSL_CTX*     ctx     = 0;
652     CYASSL*         ssl     = 0;
653
654     char msg[64] = "hello cyassl!";
655     char reply[1024];
656     int  input;
657     int  msgSz = strlen(msg);
658
659     int     argc = ((func_args*)args)->argc;
660     char**  argv = ((func_args*)args)->argv;
661
662     ((func_args*)args)->return_code = TEST_FAIL;
663     method = CyaSSLv23_client_method();
664     ctx = CyaSSL_CTX_new(method);
665
666 #ifdef OPENSSL_EXTRA
667     CyaSSL_CTX_set_default_passwd_cb(ctx, PasswordCallBack);
668 #endif
669
670     if (CyaSSL_CTX_load_verify_locations(ctx, caCert, 0) != SSL_SUCCESS)
671     {
672         /* err_sys("can't load ca file, Please run from CyaSSL home dir");*/
673         return;
674     }
675     if (CyaSSL_CTX_use_certificate_file(ctx, cliCert, SSL_FILETYPE_PEM)
676             != SSL_SUCCESS)
677     {
678         /*err_sys("can't load client cert file, "
679                 "Please run from CyaSSL home dir");*/
680         return;
681     }
682     if (CyaSSL_CTX_use_PrivateKey_file(ctx, cliKey, SSL_FILETYPE_PEM)
683             != SSL_SUCCESS)
684     {
685         /*err_sys("can't load client key file, "
686                 "Please run from CyaSSL home dir");*/
687         return;
688     }
689
690     tcp_connect(&sockfd, yasslIP, yasslPort, 0);
691
692     ssl = CyaSSL_new(ctx);
693     CyaSSL_set_fd(ssl, sockfd);
694     if (CyaSSL_connect(ssl) != SSL_SUCCESS)
695     {
696         int  err = CyaSSL_get_error(ssl, 0);
697         char buffer[80];
698         printf("err = %d, %s\n", err, CyaSSL_ERR_error_string(err, buffer));
699         /*printf("SSL_connect failed");*/
700         return;
701     }
702
703     if (CyaSSL_write(ssl, msg, msgSz) != msgSz)
704     {
705         /*err_sys("SSL_write failed");*/
706         return;
707     }
708
709     input = CyaSSL_read(ssl, reply, sizeof(reply));
710     if (input > 0)
711     {
712         reply[input] = 0;
713         printf("Server response: %s\n", reply);
714     }
715
716     ((func_args*)args)->return_code = TEST_SUCCESS;
717     return;
718 }
719
720
721
722
723 #endif /* NO_FILESYSTEM */
724
725