api.c 33 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163
  1. /* api.c API unit tests
  2. *
  3. * Copyright (C) 2006-2013 wolfSSL Inc.
  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. #ifdef HAVE_CONFIG_H
  22. #include <config.h>
  23. #endif
  24. #include <cyassl/ctaocrypt/settings.h>
  25. #include <stdlib.h>
  26. #include <cyassl/ssl.h>
  27. #include <cyassl/test.h>
  28. #include <tests/unit.h>
  29. #define TEST_FAIL (-1)
  30. #define TEST_SUCCESS (0)
  31. static int test_CyaSSL_Init(void);
  32. static int test_CyaSSL_Cleanup(void);
  33. static int test_CyaSSL_Method_Allocators(void);
  34. static int test_CyaSSL_CTX_new(CYASSL_METHOD *method);
  35. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS)
  36. static int test_CyaSSL_CTX_use_certificate_file(void);
  37. static int test_CyaSSL_CTX_use_PrivateKey_file(void);
  38. static int test_CyaSSL_CTX_load_verify_locations(void);
  39. #ifndef NO_RSA
  40. static int test_server_CyaSSL_new(void);
  41. static int test_client_CyaSSL_new(void);
  42. static int test_CyaSSL_read_write(void);
  43. #endif /* NO_RSA */
  44. #endif /* NO_FILESYSTEM */
  45. #ifdef HAVE_SNI
  46. static void test_CyaSSL_UseSNI(void);
  47. #endif /* HAVE_SNI */
  48. #ifdef HAVE_MAX_FRAGMENT
  49. static void test_CyaSSL_UseMaxFragment(void);
  50. #endif /* HAVE_MAX_FRAGMENT */
  51. #ifdef HAVE_TRUNCATED_HMAC
  52. static void test_CyaSSL_UseTruncatedHMAC(void);
  53. #endif /* HAVE_TRUNCATED_HMAC */
  54. /* test function helpers */
  55. static int test_method(CYASSL_METHOD *method, const char *name);
  56. #ifdef OPENSSL_EXTRA
  57. static int test_method2(CYASSL_METHOD *method, const char *name);
  58. #endif
  59. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS)
  60. static int test_ucf(CYASSL_CTX *ctx, const char* file, int type,
  61. int cond, const char* name);
  62. static int test_upkf(CYASSL_CTX *ctx, const char* file, int type,
  63. int cond, const char* name);
  64. static int test_lvl(CYASSL_CTX *ctx, const char* file, const char* path,
  65. int cond, const char* name);
  66. THREAD_RETURN CYASSL_THREAD test_server_nofail(void*);
  67. void test_client_nofail(void*);
  68. void run_cyassl_client(void* args);
  69. THREAD_RETURN CYASSL_THREAD run_cyassl_server(void* args);
  70. void test_CyaSSL_client_server(callback_functions* client_callbacks,
  71. callback_functions* server_callbacks);
  72. static const char* bogusFile = "/dev/null";
  73. #endif
  74. #define testingFmt " %s:"
  75. #define resultFmt " %s\n"
  76. static const char* passed = "passed";
  77. static const char* failed = "failed";
  78. /* List of methods found in echoserver.c that I'm skipping for the moment:
  79. * - CyaSSL_CTX_set_session_cache_mode()
  80. */
  81. int ApiTest(void)
  82. {
  83. printf(" Begin API Tests\n");
  84. test_CyaSSL_Init();
  85. test_CyaSSL_Method_Allocators();
  86. test_CyaSSL_CTX_new(CyaSSLv23_server_method());
  87. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS)
  88. test_CyaSSL_CTX_use_certificate_file();
  89. test_CyaSSL_CTX_use_PrivateKey_file();
  90. test_CyaSSL_CTX_load_verify_locations();
  91. #ifndef NO_RSA
  92. test_server_CyaSSL_new();
  93. test_client_CyaSSL_new();
  94. test_CyaSSL_read_write();
  95. #endif /* NO_RSA */
  96. #endif /* NO_FILESYSTEM */
  97. #ifdef HAVE_SNI
  98. test_CyaSSL_UseSNI();
  99. #endif /* HAVE_SNI */
  100. #ifdef HAVE_MAX_FRAGMENT
  101. test_CyaSSL_UseMaxFragment();
  102. #endif /* HAVE_MAX_FRAGMENT */
  103. #ifdef HAVE_TRUNCATED_HMAC
  104. test_CyaSSL_UseTruncatedHMAC();
  105. #endif /* HAVE_TRUNCATED_HMAC */
  106. test_CyaSSL_Cleanup();
  107. printf(" End API Tests\n");
  108. return TEST_SUCCESS;
  109. }
  110. int test_CyaSSL_Init(void)
  111. {
  112. int result;
  113. printf(testingFmt, "CyaSSL_Init()");
  114. result = CyaSSL_Init();
  115. printf(resultFmt, result == SSL_SUCCESS ? passed : failed);
  116. return result;
  117. }
  118. static int test_CyaSSL_Cleanup(void)
  119. {
  120. int result;
  121. printf(testingFmt, "CyaSSL_Cleanup()");
  122. result = CyaSSL_Cleanup();
  123. printf(resultFmt, result == SSL_SUCCESS ? passed : failed);
  124. return result;
  125. }
  126. int test_method(CYASSL_METHOD *method, const char *name)
  127. {
  128. printf(testingFmt, name);
  129. if (method == NULL)
  130. {
  131. printf(resultFmt, failed);
  132. return TEST_FAIL;
  133. }
  134. XFREE(method, 0, DYNAMIC_TYPE_METHOD);
  135. printf(resultFmt, passed);
  136. return TEST_SUCCESS;
  137. }
  138. #ifdef OPENSSL_EXTRA
  139. int test_method2(CYASSL_METHOD *method, const char *name)
  140. {
  141. printf(testingFmt, name);
  142. if (method != NULL)
  143. {
  144. XFREE(method, 0, DYNAMIC_TYPE_METHOD);
  145. printf(resultFmt, failed);
  146. return TEST_FAIL;
  147. }
  148. printf(resultFmt, passed);
  149. return TEST_SUCCESS;
  150. }
  151. #endif
  152. int test_CyaSSL_Method_Allocators(void)
  153. {
  154. #ifndef NO_OLD_TLS
  155. test_method(CyaSSLv3_server_method(), "CyaSSLv3_server_method()");
  156. test_method(CyaSSLv3_client_method(), "CyaSSLv3_client_method()");
  157. test_method(CyaTLSv1_server_method(), "CyaTLSv1_server_method()");
  158. test_method(CyaTLSv1_client_method(), "CyaTLSv1_client_method()");
  159. test_method(CyaTLSv1_1_server_method(), "CyaTLSv1_1_server_method()");
  160. test_method(CyaTLSv1_1_client_method(), "CyaTLSv1_1_client_method()");
  161. #endif /* NO_OLD_TLS */
  162. test_method(CyaTLSv1_2_server_method(), "CyaTLSv1_2_server_method()");
  163. test_method(CyaTLSv1_2_client_method(), "CyaTLSv1_2_client_method()");
  164. test_method(CyaSSLv23_client_method(), "CyaSSLv23_client_method()");
  165. #ifdef CYASSL_DTLS
  166. test_method(CyaDTLSv1_server_method(), "CyaDTLSv1_server_method()");
  167. test_method(CyaDTLSv1_client_method(), "CyaDTLSv1_client_method()");
  168. #endif /* CYASSL_DTLS */
  169. #ifdef OPENSSL_EXTRA
  170. test_method2(CyaSSLv2_server_method(), "CyaSSLv2_server_method()");
  171. test_method2(CyaSSLv2_client_method(), "CyaSSLv2_client_method()");
  172. #endif /* OPENSSL_EXTRA */
  173. return TEST_SUCCESS;
  174. }
  175. int test_CyaSSL_CTX_new(CYASSL_METHOD *method)
  176. {
  177. if (method != NULL)
  178. {
  179. CYASSL_CTX *ctx;
  180. printf(testingFmt, "CyaSSL_CTX_new(NULL)");
  181. ctx = CyaSSL_CTX_new(NULL);
  182. if (ctx != NULL)
  183. {
  184. CyaSSL_CTX_free(ctx);
  185. printf(resultFmt, failed);
  186. }
  187. else
  188. printf(resultFmt, passed);
  189. printf(testingFmt, "CyaSSL_CTX_new(method)");
  190. ctx = CyaSSL_CTX_new(method);
  191. if (ctx == NULL)
  192. {
  193. printf(resultFmt, failed);
  194. XFREE(method, 0, DYNAMIC_TYPE_METHOD);
  195. /* free the method data. if this was successful, freeing
  196. the CTX frees the method. */
  197. }
  198. else
  199. {
  200. CyaSSL_CTX_free(ctx);
  201. printf(resultFmt, passed);
  202. }
  203. }
  204. else
  205. printf("test_CyaSSL_CTX_new() called without method\n");
  206. return TEST_SUCCESS;
  207. }
  208. #ifdef HAVE_TLS_EXTENSIONS
  209. #ifdef HAVE_SNI
  210. static void use_SNI_at_ctx(CYASSL_CTX* ctx)
  211. {
  212. byte type = CYASSL_SNI_HOST_NAME;
  213. char name[] = "www.yassl.com";
  214. AssertIntEQ(0, CyaSSL_CTX_UseSNI(ctx, type, (void *) name, XSTRLEN(name)));
  215. }
  216. static void use_SNI_at_ssl(CYASSL* ssl)
  217. {
  218. byte type = CYASSL_SNI_HOST_NAME;
  219. char name[] = "www.yassl.com";
  220. AssertIntEQ(0, CyaSSL_UseSNI(ssl, type, (void *) name, XSTRLEN(name)));
  221. }
  222. static void different_SNI_at_ssl(CYASSL* ssl)
  223. {
  224. byte type = CYASSL_SNI_HOST_NAME;
  225. char name[] = "ww2.yassl.com";
  226. AssertIntEQ(0, CyaSSL_UseSNI(ssl, type, (void *) name, XSTRLEN(name)));
  227. }
  228. static void use_SNI_WITH_CONTINUE_at_ssl(CYASSL* ssl)
  229. {
  230. byte type = CYASSL_SNI_HOST_NAME;
  231. use_SNI_at_ssl(ssl);
  232. CyaSSL_SNI_SetOptions(ssl, type, CYASSL_SNI_CONTINUE_ON_MISMATCH);
  233. }
  234. static void use_SNI_WITH_FAKE_ANSWER_at_ssl(CYASSL* ssl)
  235. {
  236. byte type = CYASSL_SNI_HOST_NAME;
  237. use_SNI_at_ssl(ssl);
  238. CyaSSL_SNI_SetOptions(ssl, type, CYASSL_SNI_ANSWER_ON_MISMATCH);
  239. }
  240. static void verify_SNI_abort_on_client(CYASSL* ssl)
  241. {
  242. /* FATAL_ERROR */
  243. AssertIntEQ(-213, CyaSSL_get_error(ssl, 0));
  244. }
  245. static void verify_SNI_abort_on_server(CYASSL* ssl)
  246. {
  247. /* UNKNOWN_SNI_HOST_NAME_E */
  248. AssertIntEQ(-281, CyaSSL_get_error(ssl, 0));
  249. }
  250. static void verify_SNI_no_matching(CYASSL* ssl)
  251. {
  252. byte type = CYASSL_SNI_HOST_NAME;
  253. char* request = (char*) &type; /* to be overwriten */
  254. AssertIntEQ(CYASSL_SNI_NO_MATCH, CyaSSL_SNI_Status(ssl, type));
  255. AssertNotNull(request);
  256. AssertIntEQ(0, CyaSSL_SNI_GetRequest(ssl, type, (void**) &request));
  257. AssertNull(request);
  258. }
  259. static void verify_SNI_real_matching(CYASSL* ssl)
  260. {
  261. byte type = CYASSL_SNI_HOST_NAME;
  262. char* request = NULL;
  263. char name[] = "www.yassl.com";
  264. word16 length = XSTRLEN(name);
  265. AssertIntEQ(CYASSL_SNI_REAL_MATCH, CyaSSL_SNI_Status(ssl, type));
  266. AssertIntEQ(length, CyaSSL_SNI_GetRequest(ssl, type, (void**) &request));
  267. AssertNotNull(request);
  268. AssertStrEQ(name, request);
  269. }
  270. static void verify_SNI_fake_matching(CYASSL* ssl)
  271. {
  272. byte type = CYASSL_SNI_HOST_NAME;
  273. char* request = NULL;
  274. char name[] = "ww2.yassl.com";
  275. word16 length = XSTRLEN(name);
  276. AssertIntEQ(CYASSL_SNI_FAKE_MATCH, CyaSSL_SNI_Status(ssl, type));
  277. AssertIntEQ(length, CyaSSL_SNI_GetRequest(ssl, type, (void**) &request));
  278. AssertNotNull(request);
  279. AssertStrEQ(name, request);
  280. }
  281. void test_CyaSSL_UseSNI(void)
  282. {
  283. callback_functions client_callbacks = {CyaSSLv23_client_method, 0, 0, 0};
  284. callback_functions server_callbacks = {CyaSSLv23_server_method, 0, 0, 0};
  285. CYASSL_CTX *ctx = CyaSSL_CTX_new(CyaSSLv23_client_method());
  286. CYASSL *ssl = CyaSSL_new(ctx);
  287. AssertNotNull(ctx);
  288. AssertNotNull(ssl);
  289. /* error cases */
  290. AssertIntNE(0, CyaSSL_CTX_UseSNI(NULL, 0, (void *) "ctx", XSTRLEN("ctx")));
  291. AssertIntNE(0, CyaSSL_UseSNI( NULL, 0, (void *) "ssl", XSTRLEN("ssl")));
  292. AssertIntNE(0, CyaSSL_CTX_UseSNI(ctx, -1, (void *) "ctx", XSTRLEN("ctx")));
  293. AssertIntNE(0, CyaSSL_UseSNI( ssl, -1, (void *) "ssl", XSTRLEN("ssl")));
  294. AssertIntNE(0, CyaSSL_CTX_UseSNI(ctx, 0, (void *) NULL, XSTRLEN("ctx")));
  295. AssertIntNE(0, CyaSSL_UseSNI( ssl, 0, (void *) NULL, XSTRLEN("ssl")));
  296. /* success case */
  297. AssertIntEQ(0, CyaSSL_CTX_UseSNI(ctx, 0, (void *) "ctx", XSTRLEN("ctx")));
  298. AssertIntEQ(0, CyaSSL_UseSNI( ssl, 0, (void *) "ssl", XSTRLEN("ssl")));
  299. CyaSSL_free(ssl);
  300. CyaSSL_CTX_free(ctx);
  301. /* Testing success case at ctx */
  302. client_callbacks.ctx_ready = server_callbacks.ctx_ready = use_SNI_at_ctx;
  303. server_callbacks.on_result = verify_SNI_real_matching;
  304. test_CyaSSL_client_server(&client_callbacks, &server_callbacks);
  305. /* Testing success case at ssl */
  306. client_callbacks.ctx_ready = server_callbacks.ctx_ready = NULL;
  307. client_callbacks.ssl_ready = server_callbacks.ssl_ready = use_SNI_at_ssl;
  308. test_CyaSSL_client_server(&client_callbacks, &server_callbacks);
  309. /* Testing default mismatch behaviour */
  310. client_callbacks.ssl_ready = different_SNI_at_ssl;
  311. client_callbacks.on_result = verify_SNI_abort_on_client;
  312. server_callbacks.on_result = verify_SNI_abort_on_server;
  313. test_CyaSSL_client_server(&client_callbacks, &server_callbacks);
  314. client_callbacks.on_result = NULL;
  315. /* Testing continue on mismatch */
  316. client_callbacks.ssl_ready = different_SNI_at_ssl;
  317. server_callbacks.ssl_ready = use_SNI_WITH_CONTINUE_at_ssl;
  318. server_callbacks.on_result = verify_SNI_no_matching;
  319. test_CyaSSL_client_server(&client_callbacks, &server_callbacks);
  320. /* Testing fake answer on mismatch */
  321. server_callbacks.ssl_ready = use_SNI_WITH_FAKE_ANSWER_at_ssl;
  322. server_callbacks.on_result = verify_SNI_fake_matching;
  323. test_CyaSSL_client_server(&client_callbacks, &server_callbacks);
  324. }
  325. #endif /* HAVE_SNI */
  326. #ifdef HAVE_MAX_FRAGMENT
  327. static void test_CyaSSL_UseMaxFragment(void)
  328. {
  329. CYASSL_CTX *ctx = CyaSSL_CTX_new(CyaSSLv23_client_method());
  330. CYASSL *ssl = CyaSSL_new(ctx);
  331. AssertNotNull(ctx);
  332. AssertNotNull(ssl);
  333. /* error cases */
  334. AssertIntNE(0, CyaSSL_CTX_UseMaxFragment(NULL, CYASSL_MFL_2_9));
  335. AssertIntNE(0, CyaSSL_UseMaxFragment( NULL, CYASSL_MFL_2_9));
  336. AssertIntNE(0, CyaSSL_CTX_UseMaxFragment(ctx, 0));
  337. AssertIntNE(0, CyaSSL_CTX_UseMaxFragment(ctx, 6));
  338. AssertIntNE(0, CyaSSL_UseMaxFragment(ssl, 0));
  339. AssertIntNE(0, CyaSSL_UseMaxFragment(ssl, 6));
  340. /* success case */
  341. AssertIntEQ(0, CyaSSL_CTX_UseMaxFragment(ctx, CYASSL_MFL_2_9));
  342. AssertIntEQ(0, CyaSSL_CTX_UseMaxFragment(ctx, CYASSL_MFL_2_10));
  343. AssertIntEQ(0, CyaSSL_CTX_UseMaxFragment(ctx, CYASSL_MFL_2_11));
  344. AssertIntEQ(0, CyaSSL_CTX_UseMaxFragment(ctx, CYASSL_MFL_2_12));
  345. AssertIntEQ(0, CyaSSL_CTX_UseMaxFragment(ctx, CYASSL_MFL_2_13));
  346. AssertIntEQ(0, CyaSSL_UseMaxFragment( ssl, CYASSL_MFL_2_9));
  347. AssertIntEQ(0, CyaSSL_UseMaxFragment( ssl, CYASSL_MFL_2_10));
  348. AssertIntEQ(0, CyaSSL_UseMaxFragment( ssl, CYASSL_MFL_2_11));
  349. AssertIntEQ(0, CyaSSL_UseMaxFragment( ssl, CYASSL_MFL_2_12));
  350. AssertIntEQ(0, CyaSSL_UseMaxFragment( ssl, CYASSL_MFL_2_13));
  351. CyaSSL_free(ssl);
  352. CyaSSL_CTX_free(ctx);
  353. }
  354. #endif /* HAVE_MAX_FRAGMENT */
  355. #ifdef HAVE_TRUNCATED_HMAC
  356. static void test_CyaSSL_UseTruncatedHMAC(void)
  357. {
  358. CYASSL_CTX *ctx = CyaSSL_CTX_new(CyaSSLv23_client_method());
  359. CYASSL *ssl = CyaSSL_new(ctx);
  360. AssertNotNull(ctx);
  361. AssertNotNull(ssl);
  362. /* error cases */
  363. AssertIntNE(0, CyaSSL_CTX_UseTruncatedHMAC(NULL));
  364. AssertIntNE(0, CyaSSL_UseTruncatedHMAC(NULL));
  365. /* success case */
  366. AssertIntEQ(0, CyaSSL_CTX_UseTruncatedHMAC(ctx));
  367. AssertIntEQ(0, CyaSSL_UseTruncatedHMAC(ssl));
  368. CyaSSL_free(ssl);
  369. CyaSSL_CTX_free(ctx);
  370. }
  371. #endif /* HAVE_TRUNCATED_HMAC */
  372. #endif /* HAVE_TLS_EXTENSIONS */
  373. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS)
  374. /* Helper for testing CyaSSL_CTX_use_certificate_file() */
  375. int test_ucf(CYASSL_CTX *ctx, const char* file, int type, int cond,
  376. const char* name)
  377. {
  378. int result;
  379. printf(testingFmt, name);
  380. result = CyaSSL_CTX_use_certificate_file(ctx, file, type);
  381. if (result != cond)
  382. {
  383. printf(resultFmt, failed);
  384. return TEST_FAIL;
  385. }
  386. printf(resultFmt, passed);
  387. return TEST_SUCCESS;
  388. }
  389. int test_CyaSSL_CTX_use_certificate_file(void)
  390. {
  391. CYASSL_METHOD *method;
  392. CYASSL_CTX *ctx;
  393. method = CyaSSLv23_server_method();
  394. if (method == NULL)
  395. {
  396. printf("test_CyaSSL_CTX_use_certificate_file() cannot create method\n");
  397. return TEST_FAIL;
  398. }
  399. ctx = CyaSSL_CTX_new(method);
  400. if (ctx == NULL)
  401. {
  402. printf("test_CyaSSL_CTX_use_certificate_file() cannot create context\n");
  403. XFREE(method, 0, DYNAMIC_TYPE_METHOD);
  404. return TEST_FAIL;
  405. }
  406. /* setting all parameters to garbage. this should succeed with
  407. failure */
  408. /* Then set the parameters to legit values but set each item to
  409. bogus and call again. Finish with a successful success. */
  410. /* If the build is configured to not have RSA, loading the
  411. certificate files will fail. */
  412. test_ucf(NULL, NULL, 9999, SSL_FAILURE,
  413. "CyaSSL_CTX_use_certificate_file(NULL, NULL, 9999)");
  414. /* test_ucf(NULL, svrCert, SSL_FILETYPE_PEM, SSL_FAILURE,
  415. "CyaSSL_CTX_use_certificate_file(NULL, svrCert, SSL_FILETYPE_PEM)");*/
  416. test_ucf(ctx, bogusFile, SSL_FILETYPE_PEM, SSL_FAILURE,
  417. "CyaSSL_CTX_use_certificate_file(ctx, bogusFile, SSL_FILETYPE_PEM)");
  418. test_ucf(ctx, svrCert, 9999, SSL_FAILURE,
  419. "CyaSSL_CTX_use_certificate_file(ctx, svrCert, 9999)");
  420. #ifndef NO_RSA
  421. test_ucf(ctx, svrCert, SSL_FILETYPE_PEM, SSL_SUCCESS,
  422. "CyaSSL_CTX_use_certificate_file(ctx, svrCert, SSL_FILETYPE_PEM)");
  423. #else
  424. test_ucf(ctx, svrCert, SSL_FILETYPE_PEM, SSL_FAILURE,
  425. "NO_RSA: CyaSSL_CTX_use_certificate_file(ctx, svrCert, SSL_FILETYPE_PEM)");
  426. #endif
  427. CyaSSL_CTX_free(ctx);
  428. return TEST_SUCCESS;
  429. }
  430. /* Helper for testing CyaSSL_CTX_use_PrivateKey_file() */
  431. int test_upkf(CYASSL_CTX *ctx, const char* file, int type, int cond,
  432. const char* name)
  433. {
  434. int result;
  435. printf(testingFmt, name);
  436. result = CyaSSL_CTX_use_PrivateKey_file(ctx, file, type);
  437. if (result != cond)
  438. {
  439. printf(resultFmt, failed);
  440. return TEST_FAIL;
  441. }
  442. printf(resultFmt, passed);
  443. return TEST_SUCCESS;
  444. }
  445. int test_CyaSSL_CTX_use_PrivateKey_file(void)
  446. {
  447. CYASSL_METHOD *method;
  448. CYASSL_CTX *ctx;
  449. method = CyaSSLv23_server_method();
  450. if (method == NULL)
  451. {
  452. printf("test_CyaSSL_CTX_use_PrivateKey_file() cannot create method\n");
  453. return TEST_FAIL;
  454. }
  455. ctx = CyaSSL_CTX_new(method);
  456. if (ctx == NULL)
  457. {
  458. printf("test_CyaSSL_CTX_use_PrivateKey_file() cannot create context\n");
  459. XFREE(method, 0, DYNAMIC_TYPE_METHOD);
  460. return TEST_FAIL;
  461. }
  462. test_upkf(NULL, NULL, 9999, SSL_FAILURE,
  463. "CyaSSL_CTX_use_PrivateKey_file(NULL, NULL, 9999)");
  464. /* test_upkf(NULL, svrKey, SSL_FILETYPE_PEM, SSL_FAILURE,
  465. "CyaSSL_CTX_use_PrivateKey_file(NULL, svrKey, SSL_FILETYPE_PEM)");*/
  466. test_upkf(ctx, bogusFile, SSL_FILETYPE_PEM, SSL_FAILURE,
  467. "CyaSSL_CTX_use_PrivateKey_file(ctx, bogusFile, SSL_FILETYPE_PEM)");
  468. test_upkf(ctx, svrKey, 9999, SSL_FAILURE,
  469. "CyaSSL_CTX_use_PrivateKey_file(ctx, svrKey, 9999)");
  470. test_upkf(ctx, svrKey, SSL_FILETYPE_PEM, SSL_SUCCESS,
  471. "CyaSSL_CTX_use_PrivateKey_file(ctx, svrKey, SSL_FILETYPE_PEM)");
  472. CyaSSL_CTX_free(ctx);
  473. return TEST_SUCCESS;
  474. }
  475. /* Helper for testing CyaSSL_CTX_load_verify_locations() */
  476. int test_lvl(CYASSL_CTX *ctx, const char* file, const char* path, int cond,
  477. const char* name)
  478. {
  479. int result;
  480. printf(testingFmt, name);
  481. /*
  482. * CyaSSL_CTX_load_verify_locations() returns SSL_SUCCESS (1) for
  483. * success, SSL_FAILURE (0) for a non-specific failure, or a specific
  484. * failure code (<0). Need to normalize the return code to 1 or 0.
  485. */
  486. result = CyaSSL_CTX_load_verify_locations(ctx, file, path) >= SSL_SUCCESS;
  487. if (result != cond)
  488. {
  489. printf(resultFmt, failed);
  490. return TEST_FAIL;
  491. }
  492. printf(resultFmt, passed);
  493. return TEST_SUCCESS;
  494. }
  495. int test_CyaSSL_CTX_load_verify_locations(void)
  496. {
  497. CYASSL_METHOD *method;
  498. CYASSL_CTX *ctx;
  499. method = CyaSSLv23_client_method();
  500. if (method == NULL)
  501. {
  502. printf("test_CyaSSL_CTX_load_verify_locations() cannot create method\n");
  503. return TEST_FAIL;
  504. }
  505. ctx = CyaSSL_CTX_new(method);
  506. if (ctx == NULL)
  507. {
  508. printf("test_CyaSSL_CTX_load_verify_locations() cannot create context\n");
  509. free(method);
  510. return TEST_FAIL;
  511. }
  512. test_lvl(NULL, NULL, NULL, SSL_FAILURE,
  513. "CyaSSL_CTX_load_verify_locations(NULL, NULL, NULL)");
  514. test_lvl(ctx, NULL, NULL, SSL_FAILURE,
  515. "CyaSSL_CTX_load_verify_locations(ctx, NULL, NULL)");
  516. test_lvl(NULL, caCert, NULL, SSL_FAILURE,
  517. "CyaSSL_CTX_load_verify_locations(ctx, NULL, NULL)");
  518. test_lvl(ctx, caCert, bogusFile, SSL_FAILURE,
  519. "CyaSSL_CTX_load_verify_locations(ctx, caCert, bogusFile)");
  520. /* Add a test for the certs directory path loading. */
  521. /* There is a leak here. If you load a second cert, the first one
  522. is lost. */
  523. #ifndef NO_RSA
  524. test_lvl(ctx, caCert, 0, SSL_SUCCESS,
  525. "CyaSSL_CTX_load_verify_locations(ctx, caCert, 0)");
  526. #else
  527. test_lvl(ctx, caCert, 0, SSL_FAILURE,
  528. "NO_RSA: CyaSSL_CTX_load_verify_locations(ctx, caCert, 0)");
  529. #endif
  530. CyaSSL_CTX_free(ctx);
  531. return TEST_SUCCESS;
  532. }
  533. #ifndef NO_RSA
  534. int test_server_CyaSSL_new(void)
  535. {
  536. int result;
  537. CYASSL_CTX *ctx;
  538. CYASSL_CTX *ctx_nocert;
  539. CYASSL *ssl;
  540. ctx = CyaSSL_CTX_new(CyaSSLv23_server_method());
  541. if (ctx == NULL)
  542. {
  543. printf("test_server_CyaSSL_new() cannot create context\n");
  544. return TEST_FAIL;
  545. }
  546. result = CyaSSL_CTX_use_certificate_file(ctx, svrCert, SSL_FILETYPE_PEM);
  547. if (result == SSL_FAILURE)
  548. {
  549. printf("test_server_CyaSSL_new() cannot obtain certificate\n");
  550. CyaSSL_CTX_free(ctx);
  551. return TEST_FAIL;
  552. }
  553. result = CyaSSL_CTX_use_PrivateKey_file(ctx, svrKey, SSL_FILETYPE_PEM);
  554. if (result == SSL_FAILURE)
  555. {
  556. printf("test_server_CyaSSL_new() cannot obtain key\n");
  557. CyaSSL_CTX_free(ctx);
  558. return TEST_FAIL;
  559. }
  560. ctx_nocert = CyaSSL_CTX_new(CyaSSLv23_server_method());
  561. if (ctx_nocert == NULL)
  562. {
  563. printf("test_server_CyaSSL_new() cannot create bogus context\n");
  564. CyaSSL_CTX_free(ctx);
  565. return TEST_FAIL;
  566. }
  567. printf(testingFmt, "CyaSSL_new(NULL) server");
  568. ssl = CyaSSL_new(NULL);
  569. if (ssl != NULL)
  570. {
  571. printf(resultFmt, failed);
  572. CyaSSL_free(ssl);
  573. }
  574. else
  575. printf(resultFmt, passed);
  576. printf(testingFmt, "CyaSSL_new(ctx_nocert) server");
  577. ssl = CyaSSL_new(ctx_nocert);
  578. if (ssl != NULL)
  579. {
  580. printf(resultFmt, failed);
  581. CyaSSL_free(ssl);
  582. }
  583. else
  584. printf(resultFmt, passed);
  585. printf(testingFmt, "CyaSSL_new(ctx) server");
  586. ssl = CyaSSL_new(ctx);
  587. if (ssl == NULL)
  588. printf(resultFmt, failed);
  589. else
  590. {
  591. printf(resultFmt, passed);
  592. CyaSSL_free(ssl);
  593. }
  594. CyaSSL_CTX_free(ctx_nocert);
  595. CyaSSL_CTX_free(ctx);
  596. return TEST_SUCCESS;
  597. }
  598. int test_client_CyaSSL_new(void)
  599. {
  600. int result;
  601. CYASSL_CTX *ctx;
  602. CYASSL_CTX *ctx_nocert;
  603. CYASSL *ssl;
  604. ctx = CyaSSL_CTX_new(CyaSSLv23_client_method());
  605. if (ctx == NULL)
  606. {
  607. printf("test_client_CyaSSL_new() cannot create context\n");
  608. return TEST_FAIL;
  609. }
  610. result = CyaSSL_CTX_load_verify_locations(ctx, caCert, 0);
  611. if (result == SSL_FAILURE)
  612. {
  613. printf("test_client_CyaSSL_new() cannot obtain certificate\n");
  614. CyaSSL_CTX_free(ctx);
  615. return TEST_FAIL;
  616. }
  617. ctx_nocert = CyaSSL_CTX_new(CyaSSLv23_client_method());
  618. if (ctx_nocert == NULL)
  619. {
  620. printf("test_client_CyaSSL_new() cannot create bogus context\n");
  621. CyaSSL_CTX_free(ctx);
  622. return TEST_FAIL;
  623. }
  624. printf(testingFmt, "CyaSSL_new(NULL) client");
  625. ssl = CyaSSL_new(NULL);
  626. if (ssl != NULL)
  627. {
  628. printf(resultFmt, failed);
  629. CyaSSL_free(ssl);
  630. }
  631. else
  632. printf(resultFmt, passed);
  633. printf(testingFmt, "CyaSSL_new(ctx_nocert) client");
  634. ssl = CyaSSL_new(ctx_nocert);
  635. if (ssl == NULL)
  636. printf(resultFmt, failed);
  637. else
  638. {
  639. printf(resultFmt, passed);
  640. CyaSSL_free(ssl);
  641. }
  642. printf(testingFmt, "CyaSSL_new(ctx) client");
  643. ssl = CyaSSL_new(ctx);
  644. if (ssl == NULL)
  645. printf(resultFmt, failed);
  646. else
  647. {
  648. printf(resultFmt, passed);
  649. CyaSSL_free(ssl);
  650. }
  651. CyaSSL_CTX_free(ctx_nocert);
  652. CyaSSL_CTX_free(ctx);
  653. return TEST_SUCCESS;
  654. }
  655. static int test_CyaSSL_read_write(void)
  656. {
  657. /* The unit testing for read and write shall happen simutaneously, since
  658. * one can't do anything with one without the other. (Except for a failure
  659. * test case.) This function will call all the others that will set up,
  660. * execute, and report their test findings.
  661. *
  662. * Set up the success case first. This function will become the template
  663. * for the other tests. This should eventually be renamed
  664. *
  665. * The success case isn't interesting, how can this fail?
  666. * - Do not give the client context a CA certificate. The connect should
  667. * fail. Do not need server for this?
  668. * - Using NULL for the ssl object on server. Do not need client for this.
  669. * - Using NULL for the ssl object on client. Do not need server for this.
  670. * - Good ssl objects for client and server. Client write() without server
  671. * read().
  672. * - Good ssl objects for client and server. Server write() without client
  673. * read().
  674. * - Forgetting the password callback?
  675. */
  676. int test_result = TEST_SUCCESS;
  677. tcp_ready ready;
  678. func_args client_args;
  679. func_args server_args;
  680. THREAD_TYPE serverThread;
  681. StartTCP();
  682. InitTcpReady(&ready);
  683. server_args.signal = &ready;
  684. start_thread(test_server_nofail, &server_args, &serverThread);
  685. wait_tcp_ready(&server_args);
  686. test_client_nofail(&client_args);
  687. join_thread(serverThread);
  688. if (client_args.return_code != TEST_SUCCESS)
  689. {
  690. printf(resultFmt, "client failure");
  691. test_result = TEST_FAIL;
  692. }
  693. if (server_args.return_code != TEST_SUCCESS)
  694. {
  695. printf(resultFmt, "server failure");
  696. test_result = TEST_FAIL;
  697. }
  698. FreeTcpReady(&ready);
  699. return test_result;
  700. };
  701. #endif
  702. THREAD_RETURN CYASSL_THREAD test_server_nofail(void* args)
  703. {
  704. SOCKET_T sockfd = 0;
  705. SOCKET_T clientfd = 0;
  706. CYASSL_METHOD* method = 0;
  707. CYASSL_CTX* ctx = 0;
  708. CYASSL* ssl = 0;
  709. char msg[] = "I hear you fa shizzle!";
  710. char input[1024];
  711. int idx;
  712. ((func_args*)args)->return_code = TEST_FAIL;
  713. method = CyaSSLv23_server_method();
  714. ctx = CyaSSL_CTX_new(method);
  715. CyaSSL_CTX_set_verify(ctx,
  716. SSL_VERIFY_PEER | SSL_VERIFY_FAIL_IF_NO_PEER_CERT, 0);
  717. #ifdef OPENSSL_EXTRA
  718. CyaSSL_CTX_set_default_passwd_cb(ctx, PasswordCallBack);
  719. #endif
  720. if (CyaSSL_CTX_load_verify_locations(ctx, cliCert, 0) != SSL_SUCCESS)
  721. {
  722. /*err_sys("can't load ca file, Please run from CyaSSL home dir");*/
  723. goto done;
  724. }
  725. if (CyaSSL_CTX_use_certificate_file(ctx, svrCert, SSL_FILETYPE_PEM)
  726. != SSL_SUCCESS)
  727. {
  728. /*err_sys("can't load server cert chain file, "
  729. "Please run from CyaSSL home dir");*/
  730. goto done;
  731. }
  732. if (CyaSSL_CTX_use_PrivateKey_file(ctx, svrKey, SSL_FILETYPE_PEM)
  733. != SSL_SUCCESS)
  734. {
  735. /*err_sys("can't load server key file, "
  736. "Please run from CyaSSL home dir");*/
  737. goto done;
  738. }
  739. ssl = CyaSSL_new(ctx);
  740. tcp_accept(&sockfd, &clientfd, (func_args*)args, yasslPort, 0, 0);
  741. CloseSocket(sockfd);
  742. CyaSSL_set_fd(ssl, clientfd);
  743. #ifdef NO_PSK
  744. #if !defined(NO_FILESYSTEM) && defined(OPENSSL_EXTRA)
  745. CyaSSL_SetTmpDH_file(ssl, dhParam, SSL_FILETYPE_PEM);
  746. #else
  747. SetDH(ssl); /* will repick suites with DHE, higher priority than PSK */
  748. #endif
  749. #endif
  750. if (CyaSSL_accept(ssl) != SSL_SUCCESS)
  751. {
  752. int err = CyaSSL_get_error(ssl, 0);
  753. char buffer[80];
  754. printf("error = %d, %s\n", err, CyaSSL_ERR_error_string(err, buffer));
  755. /*err_sys("SSL_accept failed");*/
  756. goto done;
  757. }
  758. idx = CyaSSL_read(ssl, input, sizeof(input)-1);
  759. if (idx > 0) {
  760. input[idx] = 0;
  761. printf("Client message: %s\n", input);
  762. }
  763. if (CyaSSL_write(ssl, msg, sizeof(msg)) != sizeof(msg))
  764. {
  765. /*err_sys("SSL_write failed");*/
  766. return 0;
  767. }
  768. done:
  769. CyaSSL_shutdown(ssl);
  770. CyaSSL_free(ssl);
  771. CyaSSL_CTX_free(ctx);
  772. CloseSocket(clientfd);
  773. ((func_args*)args)->return_code = TEST_SUCCESS;
  774. return 0;
  775. }
  776. void test_client_nofail(void* args)
  777. {
  778. SOCKET_T sockfd = 0;
  779. CYASSL_METHOD* method = 0;
  780. CYASSL_CTX* ctx = 0;
  781. CYASSL* ssl = 0;
  782. char msg[64] = "hello cyassl!";
  783. char reply[1024];
  784. int input;
  785. int msgSz = (int)strlen(msg);
  786. ((func_args*)args)->return_code = TEST_FAIL;
  787. method = CyaSSLv23_client_method();
  788. ctx = CyaSSL_CTX_new(method);
  789. #ifdef OPENSSL_EXTRA
  790. CyaSSL_CTX_set_default_passwd_cb(ctx, PasswordCallBack);
  791. #endif
  792. if (CyaSSL_CTX_load_verify_locations(ctx, caCert, 0) != SSL_SUCCESS)
  793. {
  794. /* err_sys("can't load ca file, Please run from CyaSSL home dir");*/
  795. goto done2;
  796. }
  797. if (CyaSSL_CTX_use_certificate_file(ctx, cliCert, SSL_FILETYPE_PEM)
  798. != SSL_SUCCESS)
  799. {
  800. /*err_sys("can't load client cert file, "
  801. "Please run from CyaSSL home dir");*/
  802. goto done2;
  803. }
  804. if (CyaSSL_CTX_use_PrivateKey_file(ctx, cliKey, SSL_FILETYPE_PEM)
  805. != SSL_SUCCESS)
  806. {
  807. /*err_sys("can't load client key file, "
  808. "Please run from CyaSSL home dir");*/
  809. goto done2;
  810. }
  811. tcp_connect(&sockfd, yasslIP, yasslPort, 0);
  812. ssl = CyaSSL_new(ctx);
  813. CyaSSL_set_fd(ssl, sockfd);
  814. if (CyaSSL_connect(ssl) != SSL_SUCCESS)
  815. {
  816. int err = CyaSSL_get_error(ssl, 0);
  817. char buffer[80];
  818. printf("err = %d, %s\n", err, CyaSSL_ERR_error_string(err, buffer));
  819. /*printf("SSL_connect failed");*/
  820. goto done2;
  821. }
  822. if (CyaSSL_write(ssl, msg, msgSz) != msgSz)
  823. {
  824. /*err_sys("SSL_write failed");*/
  825. goto done2;
  826. }
  827. input = CyaSSL_read(ssl, reply, sizeof(reply)-1);
  828. if (input > 0)
  829. {
  830. reply[input] = 0;
  831. printf("Server response: %s\n", reply);
  832. }
  833. done2:
  834. CyaSSL_free(ssl);
  835. CyaSSL_CTX_free(ctx);
  836. CloseSocket(sockfd);
  837. ((func_args*)args)->return_code = TEST_SUCCESS;
  838. return;
  839. }
  840. void run_cyassl_client(void* args)
  841. {
  842. callback_functions* callbacks = ((func_args*)args)->callbacks;
  843. CYASSL_CTX* ctx = CyaSSL_CTX_new(callbacks->method());
  844. CYASSL* ssl = NULL;
  845. SOCKET_T sfd = 0;
  846. char msg[] = "hello cyassl server!";
  847. int len = (int) XSTRLEN(msg);
  848. char input[1024];
  849. int idx;
  850. ((func_args*)args)->return_code = TEST_FAIL;
  851. #ifdef OPENSSL_EXTRA
  852. CyaSSL_CTX_set_default_passwd_cb(ctx, PasswordCallBack);
  853. #endif
  854. AssertIntEQ(SSL_SUCCESS, CyaSSL_CTX_load_verify_locations(ctx, caCert, 0));
  855. AssertIntEQ(SSL_SUCCESS,
  856. CyaSSL_CTX_use_certificate_file(ctx, cliCert, SSL_FILETYPE_PEM));
  857. AssertIntEQ(SSL_SUCCESS,
  858. CyaSSL_CTX_use_PrivateKey_file(ctx, cliKey, SSL_FILETYPE_PEM));
  859. if (callbacks->ctx_ready)
  860. callbacks->ctx_ready(ctx);
  861. tcp_connect(&sfd, yasslIP, yasslPort, 0);
  862. ssl = CyaSSL_new(ctx);
  863. CyaSSL_set_fd(ssl, sfd);
  864. if (callbacks->ssl_ready)
  865. callbacks->ssl_ready(ssl);
  866. if (CyaSSL_connect(ssl) != SSL_SUCCESS) {
  867. int err = CyaSSL_get_error(ssl, 0);
  868. char buffer[80];
  869. printf("error = %d, %s\n", err, CyaSSL_ERR_error_string(err, buffer));
  870. } else {
  871. AssertIntEQ(len, CyaSSL_write(ssl, msg, len));
  872. if (0 < (idx = CyaSSL_read(ssl, input, sizeof(input)-1))) {
  873. input[idx] = 0;
  874. printf("Server response: %s\n", input);
  875. }
  876. }
  877. if (callbacks->on_result)
  878. callbacks->on_result(ssl);
  879. CyaSSL_free(ssl);
  880. CyaSSL_CTX_free(ctx);
  881. CloseSocket(sfd);
  882. ((func_args*)args)->return_code = TEST_SUCCESS;
  883. }
  884. THREAD_RETURN CYASSL_THREAD run_cyassl_server(void* args)
  885. {
  886. callback_functions* callbacks = ((func_args*)args)->callbacks;
  887. CYASSL_CTX* ctx = CyaSSL_CTX_new(callbacks->method());
  888. CYASSL* ssl = NULL;
  889. SOCKET_T sfd = 0;
  890. SOCKET_T cfd = 0;
  891. char msg[] = "I hear you fa shizzle!";
  892. int len = (int) XSTRLEN(msg);
  893. char input[1024];
  894. int idx;
  895. ((func_args*)args)->return_code = TEST_FAIL;
  896. CyaSSL_CTX_set_verify(ctx,
  897. SSL_VERIFY_PEER | SSL_VERIFY_FAIL_IF_NO_PEER_CERT, 0);
  898. #ifdef OPENSSL_EXTRA
  899. CyaSSL_CTX_set_default_passwd_cb(ctx, PasswordCallBack);
  900. #endif
  901. AssertIntEQ(SSL_SUCCESS, CyaSSL_CTX_load_verify_locations(ctx, cliCert, 0));
  902. AssertIntEQ(SSL_SUCCESS,
  903. CyaSSL_CTX_use_certificate_file(ctx, svrCert, SSL_FILETYPE_PEM));
  904. AssertIntEQ(SSL_SUCCESS,
  905. CyaSSL_CTX_use_PrivateKey_file(ctx, svrKey, SSL_FILETYPE_PEM));
  906. if (callbacks->ctx_ready)
  907. callbacks->ctx_ready(ctx);
  908. ssl = CyaSSL_new(ctx);
  909. tcp_accept(&sfd, &cfd, (func_args*)args, yasslPort, 0, 0);
  910. CloseSocket(sfd);
  911. CyaSSL_set_fd(ssl, cfd);
  912. #ifdef NO_PSK
  913. #if !defined(NO_FILESYSTEM) && defined(OPENSSL_EXTRA)
  914. CyaSSL_SetTmpDH_file(ssl, dhParam, SSL_FILETYPE_PEM);
  915. #else
  916. SetDH(ssl); /* will repick suites with DHE, higher priority than PSK */
  917. #endif
  918. #endif
  919. if (callbacks->ssl_ready)
  920. callbacks->ssl_ready(ssl);
  921. /* AssertIntEQ(SSL_SUCCESS, CyaSSL_accept(ssl)); */
  922. if (CyaSSL_accept(ssl) != SSL_SUCCESS) {
  923. int err = CyaSSL_get_error(ssl, 0);
  924. char buffer[80];
  925. printf("error = %d, %s\n", err, CyaSSL_ERR_error_string(err, buffer));
  926. } else {
  927. if (0 < (idx = CyaSSL_read(ssl, input, sizeof(input)-1))) {
  928. input[idx] = 0;
  929. printf("Client message: %s\n", input);
  930. }
  931. AssertIntEQ(len, CyaSSL_write(ssl, msg, len));
  932. CyaSSL_shutdown(ssl);
  933. }
  934. if (callbacks->on_result)
  935. callbacks->on_result(ssl);
  936. CyaSSL_free(ssl);
  937. CyaSSL_CTX_free(ctx);
  938. CloseSocket(cfd);
  939. ((func_args*)args)->return_code = TEST_SUCCESS;
  940. return 0;
  941. }
  942. void test_CyaSSL_client_server(callback_functions* client_callbacks,
  943. callback_functions* server_callbacks)
  944. {
  945. tcp_ready ready;
  946. func_args client_args;
  947. func_args server_args;
  948. THREAD_TYPE serverThread;
  949. StartTCP();
  950. client_args.callbacks = client_callbacks;
  951. server_args.callbacks = server_callbacks;
  952. /* RUN Server side */
  953. InitTcpReady(&ready);
  954. server_args.signal = &ready;
  955. start_thread(run_cyassl_server, &server_args, &serverThread);
  956. wait_tcp_ready(&server_args);
  957. /* RUN Client side */
  958. run_cyassl_client(&client_args);
  959. join_thread(serverThread);
  960. FreeTcpReady(&ready);
  961. }
  962. #endif /* NO_FILESYSTEM */