api.c 36 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174
  1. /* api.c API unit tests
  2. *
  3. * Copyright (C) 2006-2015 wolfSSL Inc.
  4. *
  5. * This file is part of wolfSSL. (formerly known as CyaSSL)
  6. *
  7. * wolfSSL 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. * wolfSSL 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
  20. */
  21. /*----------------------------------------------------------------------------*
  22. | Includes
  23. *----------------------------------------------------------------------------*/
  24. #ifdef HAVE_CONFIG_H
  25. #include <config.h>
  26. #endif
  27. #include <wolfssl/wolfcrypt/settings.h>
  28. #ifdef HAVE_ECC
  29. #include <wolfssl/wolfcrypt/ecc.h> /* wc_ecc_fp_free */
  30. #endif
  31. #include <wolfssl/error-ssl.h>
  32. #include <stdlib.h>
  33. #include <wolfssl/ssl.h> /* compatibility layer */
  34. #include <wolfssl/test.h>
  35. #include <tests/unit.h>
  36. /*----------------------------------------------------------------------------*
  37. | Constants
  38. *----------------------------------------------------------------------------*/
  39. #define TEST_SUCCESS (1)
  40. #define TEST_FAIL (0)
  41. #define testingFmt " %s:"
  42. #define resultFmt " %s\n"
  43. static const char* passed = "passed";
  44. static const char* failed = "failed";
  45. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS)
  46. static const char* bogusFile = "/dev/null";
  47. #endif
  48. /*----------------------------------------------------------------------------*
  49. | Setup
  50. *----------------------------------------------------------------------------*/
  51. static int test_wolfSSL_Init(void)
  52. {
  53. int result;
  54. printf(testingFmt, "wolfSSL_Init()");
  55. result = wolfSSL_Init();
  56. printf(resultFmt, result == SSL_SUCCESS ? passed : failed);
  57. return result;
  58. }
  59. static int test_wolfSSL_Cleanup(void)
  60. {
  61. int result;
  62. printf(testingFmt, "wolfSSL_Cleanup()");
  63. result = wolfSSL_Cleanup();
  64. printf(resultFmt, result == SSL_SUCCESS ? passed : failed);
  65. return result;
  66. }
  67. /*----------------------------------------------------------------------------*
  68. | Method Allocators
  69. *----------------------------------------------------------------------------*/
  70. static void test_wolfSSL_Method_Allocators(void)
  71. {
  72. #define TEST_METHOD_ALLOCATOR(allocator, condition) \
  73. do { \
  74. WOLFSSL_METHOD *method; \
  75. condition(method = allocator()); \
  76. XFREE(method, 0, DYNAMIC_TYPE_METHOD); \
  77. } while(0)
  78. #define TEST_VALID_METHOD_ALLOCATOR(a) \
  79. TEST_METHOD_ALLOCATOR(a, AssertNotNull)
  80. #define TEST_INVALID_METHOD_ALLOCATOR(a) \
  81. TEST_METHOD_ALLOCATOR(a, AssertNull)
  82. #ifndef NO_OLD_TLS
  83. TEST_VALID_METHOD_ALLOCATOR(wolfSSLv3_server_method);
  84. TEST_VALID_METHOD_ALLOCATOR(wolfSSLv3_client_method);
  85. TEST_VALID_METHOD_ALLOCATOR(wolfTLSv1_server_method);
  86. TEST_VALID_METHOD_ALLOCATOR(wolfTLSv1_client_method);
  87. TEST_VALID_METHOD_ALLOCATOR(wolfTLSv1_1_server_method);
  88. TEST_VALID_METHOD_ALLOCATOR(wolfTLSv1_1_client_method);
  89. #endif
  90. TEST_VALID_METHOD_ALLOCATOR(wolfTLSv1_2_server_method);
  91. TEST_VALID_METHOD_ALLOCATOR(wolfTLSv1_2_client_method);
  92. TEST_VALID_METHOD_ALLOCATOR(wolfSSLv23_client_method);
  93. #ifdef WOLFSSL_DTLS
  94. TEST_VALID_METHOD_ALLOCATOR(wolfDTLSv1_server_method);
  95. TEST_VALID_METHOD_ALLOCATOR(wolfDTLSv1_client_method);
  96. TEST_VALID_METHOD_ALLOCATOR(wolfDTLSv1_2_server_method);
  97. TEST_VALID_METHOD_ALLOCATOR(wolfDTLSv1_2_client_method);
  98. #endif
  99. #ifdef OPENSSL_EXTRA
  100. TEST_INVALID_METHOD_ALLOCATOR(wolfSSLv2_server_method);
  101. TEST_INVALID_METHOD_ALLOCATOR(wolfSSLv2_client_method);
  102. #endif
  103. }
  104. /*----------------------------------------------------------------------------*
  105. | Context
  106. *----------------------------------------------------------------------------*/
  107. static void test_wolfSSL_CTX_new(WOLFSSL_METHOD *method)
  108. {
  109. WOLFSSL_CTX *ctx;
  110. AssertNull(ctx = wolfSSL_CTX_new(NULL));
  111. AssertNotNull(method);
  112. AssertNotNull(ctx = wolfSSL_CTX_new(method));
  113. wolfSSL_CTX_free(ctx);
  114. }
  115. static void test_wolfSSL_CTX_use_certificate_file(void)
  116. {
  117. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS)
  118. WOLFSSL_CTX *ctx;
  119. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  120. /* invalid context */
  121. AssertFalse(wolfSSL_CTX_use_certificate_file(NULL, svrCert,
  122. SSL_FILETYPE_PEM));
  123. /* invalid cert file */
  124. AssertFalse(wolfSSL_CTX_use_certificate_file(ctx, bogusFile,
  125. SSL_FILETYPE_PEM));
  126. /* invalid cert type */
  127. AssertFalse(wolfSSL_CTX_use_certificate_file(ctx, svrCert, 9999));
  128. #ifdef NO_RSA
  129. /* rsa needed */
  130. AssertFalse(wolfSSL_CTX_use_certificate_file(ctx, svrCert,SSL_FILETYPE_PEM));
  131. #else
  132. /* success */
  133. AssertTrue(wolfSSL_CTX_use_certificate_file(ctx, svrCert, SSL_FILETYPE_PEM));
  134. #endif
  135. wolfSSL_CTX_free(ctx);
  136. #endif
  137. }
  138. static void test_wolfSSL_CTX_use_PrivateKey_file(void)
  139. {
  140. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS)
  141. WOLFSSL_CTX *ctx;
  142. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  143. /* invalid context */
  144. AssertFalse(wolfSSL_CTX_use_PrivateKey_file(NULL, svrKey,
  145. SSL_FILETYPE_PEM));
  146. /* invalid key file */
  147. AssertFalse(wolfSSL_CTX_use_PrivateKey_file(ctx, bogusFile,
  148. SSL_FILETYPE_PEM));
  149. /* invalid key type */
  150. AssertFalse(wolfSSL_CTX_use_PrivateKey_file(ctx, svrKey, 9999));
  151. /* success */
  152. #ifdef NO_RSA
  153. /* rsa needed */
  154. AssertFalse(wolfSSL_CTX_use_PrivateKey_file(ctx, svrKey, SSL_FILETYPE_PEM));
  155. #else
  156. /* success */
  157. AssertTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, svrKey, SSL_FILETYPE_PEM));
  158. #endif
  159. wolfSSL_CTX_free(ctx);
  160. #endif
  161. }
  162. static void test_wolfSSL_CTX_load_verify_locations(void)
  163. {
  164. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS)
  165. WOLFSSL_CTX *ctx;
  166. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  167. /* invalid context */
  168. AssertFalse(wolfSSL_CTX_load_verify_locations(NULL, caCert, 0));
  169. /* invalid ca file */
  170. AssertFalse(wolfSSL_CTX_load_verify_locations(ctx, NULL, 0));
  171. AssertFalse(wolfSSL_CTX_load_verify_locations(ctx, bogusFile, 0));
  172. #ifndef WOLFSSL_TIRTOS
  173. /* invalid path */
  174. /* not working... investigate! */
  175. /* AssertFalse(wolfSSL_CTX_load_verify_locations(ctx, caCert, bogusFile)); */
  176. #endif
  177. /* success */
  178. AssertTrue(wolfSSL_CTX_load_verify_locations(ctx, caCert, 0));
  179. wolfSSL_CTX_free(ctx);
  180. #endif
  181. }
  182. /*----------------------------------------------------------------------------*
  183. | SSL
  184. *----------------------------------------------------------------------------*/
  185. static void test_server_wolfSSL_new(void)
  186. {
  187. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && !defined(NO_RSA)
  188. WOLFSSL_CTX *ctx;
  189. WOLFSSL_CTX *ctx_nocert;
  190. WOLFSSL *ssl;
  191. AssertNotNull(ctx_nocert = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  192. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  193. AssertTrue(wolfSSL_CTX_use_certificate_file(ctx, svrCert, SSL_FILETYPE_PEM));
  194. AssertTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, svrKey, SSL_FILETYPE_PEM));
  195. /* invalid context */
  196. AssertNull(ssl = wolfSSL_new(NULL));
  197. AssertNull(ssl = wolfSSL_new(ctx_nocert));
  198. /* success */
  199. AssertNotNull(ssl = wolfSSL_new(ctx));
  200. wolfSSL_free(ssl);
  201. wolfSSL_CTX_free(ctx);
  202. wolfSSL_CTX_free(ctx_nocert);
  203. #endif
  204. }
  205. static void test_client_wolfSSL_new(void)
  206. {
  207. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && !defined(NO_RSA)
  208. WOLFSSL_CTX *ctx;
  209. WOLFSSL_CTX *ctx_nocert;
  210. WOLFSSL *ssl;
  211. AssertNotNull(ctx_nocert = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  212. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  213. AssertTrue(wolfSSL_CTX_load_verify_locations(ctx, caCert, 0));
  214. /* invalid context */
  215. AssertNull(ssl = wolfSSL_new(NULL));
  216. /* success */
  217. AssertNotNull(ssl = wolfSSL_new(ctx_nocert));
  218. wolfSSL_free(ssl);
  219. /* success */
  220. AssertNotNull(ssl = wolfSSL_new(ctx));
  221. wolfSSL_free(ssl);
  222. wolfSSL_CTX_free(ctx);
  223. wolfSSL_CTX_free(ctx_nocert);
  224. #endif
  225. }
  226. /*----------------------------------------------------------------------------*
  227. | IO
  228. *----------------------------------------------------------------------------*/
  229. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && \
  230. !defined(NO_RSA) && !defined(SINGLE_THREADED)
  231. #define HAVE_IO_TESTS_DEPENDENCIES
  232. #endif
  233. /* helper functions */
  234. #ifdef HAVE_IO_TESTS_DEPENDENCIES
  235. static THREAD_RETURN WOLFSSL_THREAD test_server_nofail(void* args)
  236. {
  237. SOCKET_T sockfd = 0;
  238. SOCKET_T clientfd = 0;
  239. word16 port = yasslPort;
  240. WOLFSSL_METHOD* method = 0;
  241. WOLFSSL_CTX* ctx = 0;
  242. WOLFSSL* ssl = 0;
  243. char msg[] = "I hear you fa shizzle!";
  244. char input[1024];
  245. int idx;
  246. #ifdef WOLFSSL_TIRTOS
  247. fdOpenSession(Task_self());
  248. #endif
  249. ((func_args*)args)->return_code = TEST_FAIL;
  250. method = wolfSSLv23_server_method();
  251. ctx = wolfSSL_CTX_new(method);
  252. #if defined(NO_MAIN_DRIVER) && !defined(USE_WINDOWS_API) && \
  253. !defined(WOLFSSL_SNIFFER) && !defined(WOLFSSL_MDK_SHELL) && \
  254. !defined(WOLFSSL_TIRTOS)
  255. port = 0;
  256. #endif
  257. wolfSSL_CTX_set_verify(ctx,
  258. SSL_VERIFY_PEER | SSL_VERIFY_FAIL_IF_NO_PEER_CERT, 0);
  259. #ifdef OPENSSL_EXTRA
  260. wolfSSL_CTX_set_default_passwd_cb(ctx, PasswordCallBack);
  261. #endif
  262. if (wolfSSL_CTX_load_verify_locations(ctx, cliCert, 0) != SSL_SUCCESS)
  263. {
  264. /*err_sys("can't load ca file, Please run from wolfSSL home dir");*/
  265. goto done;
  266. }
  267. if (wolfSSL_CTX_use_certificate_file(ctx, svrCert, SSL_FILETYPE_PEM)
  268. != SSL_SUCCESS)
  269. {
  270. /*err_sys("can't load server cert chain file, "
  271. "Please run from wolfSSL home dir");*/
  272. goto done;
  273. }
  274. if (wolfSSL_CTX_use_PrivateKey_file(ctx, svrKey, SSL_FILETYPE_PEM)
  275. != SSL_SUCCESS)
  276. {
  277. /*err_sys("can't load server key file, "
  278. "Please run from wolfSSL home dir");*/
  279. goto done;
  280. }
  281. ssl = wolfSSL_new(ctx);
  282. tcp_accept(&sockfd, &clientfd, (func_args*)args, port, 0, 0, 0);
  283. CloseSocket(sockfd);
  284. wolfSSL_set_fd(ssl, clientfd);
  285. #ifdef NO_PSK
  286. #if !defined(NO_FILESYSTEM) && !defined(NO_DH)
  287. wolfSSL_SetTmpDH_file(ssl, dhParam, SSL_FILETYPE_PEM);
  288. #elif !defined(NO_DH)
  289. SetDH(ssl); /* will repick suites with DHE, higher priority than PSK */
  290. #endif
  291. #endif
  292. if (wolfSSL_accept(ssl) != SSL_SUCCESS)
  293. {
  294. int err = wolfSSL_get_error(ssl, 0);
  295. char buffer[WOLFSSL_MAX_ERROR_SZ];
  296. printf("error = %d, %s\n", err, wolfSSL_ERR_error_string(err, buffer));
  297. /*err_sys("SSL_accept failed");*/
  298. goto done;
  299. }
  300. idx = wolfSSL_read(ssl, input, sizeof(input)-1);
  301. if (idx > 0) {
  302. input[idx] = 0;
  303. printf("Client message: %s\n", input);
  304. }
  305. if (wolfSSL_write(ssl, msg, sizeof(msg)) != sizeof(msg))
  306. {
  307. /*err_sys("SSL_write failed");*/
  308. #ifdef WOLFSSL_TIRTOS
  309. return;
  310. #else
  311. return 0;
  312. #endif
  313. }
  314. #ifdef WOLFSSL_TIRTOS
  315. Task_yield();
  316. #endif
  317. done:
  318. wolfSSL_shutdown(ssl);
  319. wolfSSL_free(ssl);
  320. wolfSSL_CTX_free(ctx);
  321. CloseSocket(clientfd);
  322. ((func_args*)args)->return_code = TEST_SUCCESS;
  323. #ifdef WOLFSSL_TIRTOS
  324. fdCloseSession(Task_self());
  325. #endif
  326. #if defined(NO_MAIN_DRIVER) && defined(HAVE_ECC) && defined(FP_ECC) \
  327. && defined(HAVE_THREAD_LS)
  328. wc_ecc_fp_free(); /* free per thread cache */
  329. #endif
  330. #ifndef WOLFSSL_TIRTOS
  331. return 0;
  332. #endif
  333. }
  334. static void test_client_nofail(void* args)
  335. {
  336. SOCKET_T sockfd = 0;
  337. WOLFSSL_METHOD* method = 0;
  338. WOLFSSL_CTX* ctx = 0;
  339. WOLFSSL* ssl = 0;
  340. char msg[64] = "hello wolfssl!";
  341. char reply[1024];
  342. int input;
  343. int msgSz = (int)strlen(msg);
  344. #ifdef WOLFSSL_TIRTOS
  345. fdOpenSession(Task_self());
  346. #endif
  347. ((func_args*)args)->return_code = TEST_FAIL;
  348. method = wolfSSLv23_client_method();
  349. ctx = wolfSSL_CTX_new(method);
  350. #ifdef OPENSSL_EXTRA
  351. wolfSSL_CTX_set_default_passwd_cb(ctx, PasswordCallBack);
  352. #endif
  353. if (wolfSSL_CTX_load_verify_locations(ctx, caCert, 0) != SSL_SUCCESS)
  354. {
  355. /* err_sys("can't load ca file, Please run from wolfSSL home dir");*/
  356. goto done2;
  357. }
  358. if (wolfSSL_CTX_use_certificate_file(ctx, cliCert, SSL_FILETYPE_PEM)
  359. != SSL_SUCCESS)
  360. {
  361. /*err_sys("can't load client cert file, "
  362. "Please run from wolfSSL home dir");*/
  363. goto done2;
  364. }
  365. if (wolfSSL_CTX_use_PrivateKey_file(ctx, cliKey, SSL_FILETYPE_PEM)
  366. != SSL_SUCCESS)
  367. {
  368. /*err_sys("can't load client key file, "
  369. "Please run from wolfSSL home dir");*/
  370. goto done2;
  371. }
  372. tcp_connect(&sockfd, yasslIP, ((func_args*)args)->signal->port, 0);
  373. ssl = wolfSSL_new(ctx);
  374. wolfSSL_set_fd(ssl, sockfd);
  375. if (wolfSSL_connect(ssl) != SSL_SUCCESS)
  376. {
  377. int err = wolfSSL_get_error(ssl, 0);
  378. char buffer[WOLFSSL_MAX_ERROR_SZ];
  379. printf("err = %d, %s\n", err, wolfSSL_ERR_error_string(err, buffer));
  380. /*printf("SSL_connect failed");*/
  381. goto done2;
  382. }
  383. if (wolfSSL_write(ssl, msg, msgSz) != msgSz)
  384. {
  385. /*err_sys("SSL_write failed");*/
  386. goto done2;
  387. }
  388. input = wolfSSL_read(ssl, reply, sizeof(reply)-1);
  389. if (input > 0)
  390. {
  391. reply[input] = 0;
  392. printf("Server response: %s\n", reply);
  393. }
  394. done2:
  395. wolfSSL_free(ssl);
  396. wolfSSL_CTX_free(ctx);
  397. CloseSocket(sockfd);
  398. ((func_args*)args)->return_code = TEST_SUCCESS;
  399. #ifdef WOLFSSL_TIRTOS
  400. fdCloseSession(Task_self());
  401. #endif
  402. return;
  403. }
  404. /* SNI helper functions */
  405. #ifdef HAVE_SNI
  406. static THREAD_RETURN WOLFSSL_THREAD run_wolfssl_server(void* args)
  407. {
  408. callback_functions* callbacks = ((func_args*)args)->callbacks;
  409. WOLFSSL_CTX* ctx = wolfSSL_CTX_new(callbacks->method());
  410. WOLFSSL* ssl = NULL;
  411. SOCKET_T sfd = 0;
  412. SOCKET_T cfd = 0;
  413. word16 port = yasslPort;
  414. char msg[] = "I hear you fa shizzle!";
  415. int len = (int) XSTRLEN(msg);
  416. char input[1024];
  417. int idx;
  418. #ifdef WOLFSSL_TIRTOS
  419. fdOpenSession(Task_self());
  420. #endif
  421. ((func_args*)args)->return_code = TEST_FAIL;
  422. #if defined(NO_MAIN_DRIVER) && !defined(USE_WINDOWS_API) && \
  423. !defined(WOLFSSL_SNIFFER) && !defined(WOLFSSL_MDK_SHELL) && \
  424. !defined(WOLFSSL_TIRTOS)
  425. port = 0;
  426. #endif
  427. wolfSSL_CTX_set_verify(ctx,
  428. SSL_VERIFY_PEER | SSL_VERIFY_FAIL_IF_NO_PEER_CERT, 0);
  429. #ifdef OPENSSL_EXTRA
  430. wolfSSL_CTX_set_default_passwd_cb(ctx, PasswordCallBack);
  431. #endif
  432. AssertIntEQ(SSL_SUCCESS, wolfSSL_CTX_load_verify_locations(ctx, cliCert, 0));
  433. AssertIntEQ(SSL_SUCCESS,
  434. wolfSSL_CTX_use_certificate_file(ctx, svrCert, SSL_FILETYPE_PEM));
  435. AssertIntEQ(SSL_SUCCESS,
  436. wolfSSL_CTX_use_PrivateKey_file(ctx, svrKey, SSL_FILETYPE_PEM));
  437. if (callbacks->ctx_ready)
  438. callbacks->ctx_ready(ctx);
  439. ssl = wolfSSL_new(ctx);
  440. tcp_accept(&sfd, &cfd, (func_args*)args, port, 0, 0, 0);
  441. CloseSocket(sfd);
  442. wolfSSL_set_fd(ssl, cfd);
  443. #ifdef NO_PSK
  444. #if !defined(NO_FILESYSTEM) && !defined(NO_DH)
  445. wolfSSL_SetTmpDH_file(ssl, dhParam, SSL_FILETYPE_PEM);
  446. #elif !defined(NO_DH)
  447. SetDH(ssl); /* will repick suites with DHE, higher priority than PSK */
  448. #endif
  449. #endif
  450. if (callbacks->ssl_ready)
  451. callbacks->ssl_ready(ssl);
  452. /* AssertIntEQ(SSL_SUCCESS, wolfSSL_accept(ssl)); */
  453. if (wolfSSL_accept(ssl) != SSL_SUCCESS) {
  454. int err = wolfSSL_get_error(ssl, 0);
  455. char buffer[WOLFSSL_MAX_ERROR_SZ];
  456. printf("error = %d, %s\n", err, wolfSSL_ERR_error_string(err, buffer));
  457. } else {
  458. if (0 < (idx = wolfSSL_read(ssl, input, sizeof(input)-1))) {
  459. input[idx] = 0;
  460. printf("Client message: %s\n", input);
  461. }
  462. AssertIntEQ(len, wolfSSL_write(ssl, msg, len));
  463. #ifdef WOLFSSL_TIRTOS
  464. Task_yield();
  465. #endif
  466. wolfSSL_shutdown(ssl);
  467. }
  468. if (callbacks->on_result)
  469. callbacks->on_result(ssl);
  470. wolfSSL_free(ssl);
  471. wolfSSL_CTX_free(ctx);
  472. CloseSocket(cfd);
  473. ((func_args*)args)->return_code = TEST_SUCCESS;
  474. #ifdef WOLFSSL_TIRTOS
  475. fdCloseSession(Task_self());
  476. #endif
  477. #if defined(NO_MAIN_DRIVER) && defined(HAVE_ECC) && defined(FP_ECC) \
  478. && defined(HAVE_THREAD_LS)
  479. wc_ecc_fp_free(); /* free per thread cache */
  480. #endif
  481. #ifndef WOLFSSL_TIRTOS
  482. return 0;
  483. #endif
  484. }
  485. static void run_wolfssl_client(void* args)
  486. {
  487. callback_functions* callbacks = ((func_args*)args)->callbacks;
  488. WOLFSSL_CTX* ctx = wolfSSL_CTX_new(callbacks->method());
  489. WOLFSSL* ssl = NULL;
  490. SOCKET_T sfd = 0;
  491. char msg[] = "hello wolfssl server!";
  492. int len = (int) XSTRLEN(msg);
  493. char input[1024];
  494. int idx;
  495. #ifdef WOLFSSL_TIRTOS
  496. fdOpenSession(Task_self());
  497. #endif
  498. ((func_args*)args)->return_code = TEST_FAIL;
  499. #ifdef OPENSSL_EXTRA
  500. wolfSSL_CTX_set_default_passwd_cb(ctx, PasswordCallBack);
  501. #endif
  502. AssertIntEQ(SSL_SUCCESS, wolfSSL_CTX_load_verify_locations(ctx, caCert, 0));
  503. AssertIntEQ(SSL_SUCCESS,
  504. wolfSSL_CTX_use_certificate_file(ctx, cliCert, SSL_FILETYPE_PEM));
  505. AssertIntEQ(SSL_SUCCESS,
  506. wolfSSL_CTX_use_PrivateKey_file(ctx, cliKey, SSL_FILETYPE_PEM));
  507. if (callbacks->ctx_ready)
  508. callbacks->ctx_ready(ctx);
  509. tcp_connect(&sfd, yasslIP, ((func_args*)args)->signal->port, 0);
  510. ssl = wolfSSL_new(ctx);
  511. wolfSSL_set_fd(ssl, sfd);
  512. if (callbacks->ssl_ready)
  513. callbacks->ssl_ready(ssl);
  514. if (wolfSSL_connect(ssl) != SSL_SUCCESS) {
  515. int err = wolfSSL_get_error(ssl, 0);
  516. char buffer[WOLFSSL_MAX_ERROR_SZ];
  517. printf("error = %d, %s\n", err, wolfSSL_ERR_error_string(err, buffer));
  518. } else {
  519. AssertIntEQ(len, wolfSSL_write(ssl, msg, len));
  520. if (0 < (idx = wolfSSL_read(ssl, input, sizeof(input)-1))) {
  521. input[idx] = 0;
  522. printf("Server response: %s\n", input);
  523. }
  524. }
  525. if (callbacks->on_result)
  526. callbacks->on_result(ssl);
  527. wolfSSL_free(ssl);
  528. wolfSSL_CTX_free(ctx);
  529. CloseSocket(sfd);
  530. ((func_args*)args)->return_code = TEST_SUCCESS;
  531. #ifdef WOLFSSL_TIRTOS
  532. fdCloseSession(Task_self());
  533. #endif
  534. }
  535. #endif /* HAVE_SNI */
  536. #endif /* io tests dependencies */
  537. static void test_wolfSSL_read_write(void)
  538. {
  539. #ifdef HAVE_IO_TESTS_DEPENDENCIES
  540. /* The unit testing for read and write shall happen simutaneously, since
  541. * one can't do anything with one without the other. (Except for a failure
  542. * test case.) This function will call all the others that will set up,
  543. * execute, and report their test findings.
  544. *
  545. * Set up the success case first. This function will become the template
  546. * for the other tests. This should eventually be renamed
  547. *
  548. * The success case isn't interesting, how can this fail?
  549. * - Do not give the client context a CA certificate. The connect should
  550. * fail. Do not need server for this?
  551. * - Using NULL for the ssl object on server. Do not need client for this.
  552. * - Using NULL for the ssl object on client. Do not need server for this.
  553. * - Good ssl objects for client and server. Client write() without server
  554. * read().
  555. * - Good ssl objects for client and server. Server write() without client
  556. * read().
  557. * - Forgetting the password callback?
  558. */
  559. tcp_ready ready;
  560. func_args client_args;
  561. func_args server_args;
  562. THREAD_TYPE serverThread;
  563. #ifdef WOLFSSL_TIRTOS
  564. fdOpenSession(Task_self());
  565. #endif
  566. StartTCP();
  567. InitTcpReady(&ready);
  568. server_args.signal = &ready;
  569. client_args.signal = &ready;
  570. start_thread(test_server_nofail, &server_args, &serverThread);
  571. wait_tcp_ready(&server_args);
  572. test_client_nofail(&client_args);
  573. join_thread(serverThread);
  574. AssertTrue(client_args.return_code);
  575. AssertTrue(server_args.return_code);
  576. FreeTcpReady(&ready);
  577. #ifdef WOLFSSL_TIRTOS
  578. fdOpenSession(Task_self());
  579. #endif
  580. #endif
  581. }
  582. /*----------------------------------------------------------------------------*
  583. | TLS extensions tests
  584. *----------------------------------------------------------------------------*/
  585. #ifdef HAVE_SNI
  586. static void use_SNI_at_ctx(WOLFSSL_CTX* ctx)
  587. {
  588. byte type = WOLFSSL_SNI_HOST_NAME;
  589. char name[] = "www.yassl.com";
  590. AssertIntEQ(SSL_SUCCESS,
  591. wolfSSL_CTX_UseSNI(ctx, type, (void *) name, XSTRLEN(name)));
  592. }
  593. static void use_SNI_at_ssl(WOLFSSL* ssl)
  594. {
  595. byte type = WOLFSSL_SNI_HOST_NAME;
  596. char name[] = "www.yassl.com";
  597. AssertIntEQ(SSL_SUCCESS,
  598. wolfSSL_UseSNI(ssl, type, (void *) name, XSTRLEN(name)));
  599. }
  600. static void different_SNI_at_ssl(WOLFSSL* ssl)
  601. {
  602. byte type = WOLFSSL_SNI_HOST_NAME;
  603. char name[] = "ww2.yassl.com";
  604. AssertIntEQ(SSL_SUCCESS,
  605. wolfSSL_UseSNI(ssl, type, (void *) name, XSTRLEN(name)));
  606. }
  607. static void use_SNI_WITH_CONTINUE_at_ssl(WOLFSSL* ssl)
  608. {
  609. byte type = WOLFSSL_SNI_HOST_NAME;
  610. use_SNI_at_ssl(ssl);
  611. wolfSSL_SNI_SetOptions(ssl, type, WOLFSSL_SNI_CONTINUE_ON_MISMATCH);
  612. }
  613. static void use_SNI_WITH_FAKE_ANSWER_at_ssl(WOLFSSL* ssl)
  614. {
  615. byte type = WOLFSSL_SNI_HOST_NAME;
  616. use_SNI_at_ssl(ssl);
  617. wolfSSL_SNI_SetOptions(ssl, type, WOLFSSL_SNI_ANSWER_ON_MISMATCH);
  618. }
  619. static void verify_SNI_abort_on_client(WOLFSSL* ssl)
  620. {
  621. AssertIntEQ(FATAL_ERROR, wolfSSL_get_error(ssl, 0));
  622. }
  623. static void verify_SNI_abort_on_server(WOLFSSL* ssl)
  624. {
  625. AssertIntEQ(UNKNOWN_SNI_HOST_NAME_E, wolfSSL_get_error(ssl, 0));
  626. }
  627. static void verify_SNI_no_matching(WOLFSSL* ssl)
  628. {
  629. byte type = WOLFSSL_SNI_HOST_NAME;
  630. char* request = (char*) &type; /* to be overwriten */
  631. AssertIntEQ(WOLFSSL_SNI_NO_MATCH, wolfSSL_SNI_Status(ssl, type));
  632. AssertNotNull(request);
  633. AssertIntEQ(0, wolfSSL_SNI_GetRequest(ssl, type, (void**) &request));
  634. AssertNull(request);
  635. }
  636. static void verify_SNI_real_matching(WOLFSSL* ssl)
  637. {
  638. byte type = WOLFSSL_SNI_HOST_NAME;
  639. char* request = NULL;
  640. char name[] = "www.yassl.com";
  641. word16 length = XSTRLEN(name);
  642. AssertIntEQ(WOLFSSL_SNI_REAL_MATCH, wolfSSL_SNI_Status(ssl, type));
  643. AssertIntEQ(length, wolfSSL_SNI_GetRequest(ssl, type, (void**) &request));
  644. AssertNotNull(request);
  645. AssertStrEQ(name, request);
  646. }
  647. static void verify_SNI_fake_matching(WOLFSSL* ssl)
  648. {
  649. byte type = WOLFSSL_SNI_HOST_NAME;
  650. char* request = NULL;
  651. char name[] = "ww2.yassl.com";
  652. word16 length = XSTRLEN(name);
  653. AssertIntEQ(WOLFSSL_SNI_FAKE_MATCH, wolfSSL_SNI_Status(ssl, type));
  654. AssertIntEQ(length, wolfSSL_SNI_GetRequest(ssl, type, (void**) &request));
  655. AssertNotNull(request);
  656. AssertStrEQ(name, request);
  657. }
  658. static void test_wolfSSL_SNI_GetFromBuffer(void)
  659. {
  660. byte buffer[] = { /* www.paypal.com */
  661. 0x00, 0x00, 0x00, 0x00, 0xff, 0x01, 0x00, 0x00, 0x60, 0x03, 0x03, 0x5c,
  662. 0xc4, 0xb3, 0x8c, 0x87, 0xef, 0xa4, 0x09, 0xe0, 0x02, 0xab, 0x86, 0xca,
  663. 0x76, 0xf0, 0x9e, 0x01, 0x65, 0xf6, 0xa6, 0x06, 0x13, 0x1d, 0x0f, 0xa5,
  664. 0x79, 0xb0, 0xd4, 0x77, 0x22, 0xeb, 0x1a, 0x00, 0x00, 0x16, 0x00, 0x6b,
  665. 0x00, 0x67, 0x00, 0x39, 0x00, 0x33, 0x00, 0x3d, 0x00, 0x3c, 0x00, 0x35,
  666. 0x00, 0x2f, 0x00, 0x05, 0x00, 0x04, 0x00, 0x0a, 0x01, 0x00, 0x00, 0x21,
  667. 0x00, 0x00, 0x00, 0x13, 0x00, 0x11, 0x00, 0x00, 0x0e, 0x77, 0x77, 0x77,
  668. 0x2e, 0x70, 0x61, 0x79, 0x70, 0x61, 0x6c, 0x2e, 0x63, 0x6f, 0x6d, 0x00,
  669. 0x0d, 0x00, 0x06, 0x00, 0x04, 0x04, 0x01, 0x02, 0x01
  670. };
  671. byte buffer2[] = { /* api.textmate.org */
  672. 0x16, 0x03, 0x01, 0x00, 0xc6, 0x01, 0x00, 0x00, 0xc2, 0x03, 0x03, 0x52,
  673. 0x8b, 0x7b, 0xca, 0x69, 0xec, 0x97, 0xd5, 0x08, 0x03, 0x50, 0xfe, 0x3b,
  674. 0x99, 0xc3, 0x20, 0xce, 0xa5, 0xf6, 0x99, 0xa5, 0x71, 0xf9, 0x57, 0x7f,
  675. 0x04, 0x38, 0xf6, 0x11, 0x0b, 0xb8, 0xd3, 0x00, 0x00, 0x5e, 0x00, 0xff,
  676. 0xc0, 0x24, 0xc0, 0x23, 0xc0, 0x0a, 0xc0, 0x09, 0xc0, 0x07, 0xc0, 0x08,
  677. 0xc0, 0x28, 0xc0, 0x27, 0xc0, 0x14, 0xc0, 0x13, 0xc0, 0x11, 0xc0, 0x12,
  678. 0xc0, 0x26, 0xc0, 0x25, 0xc0, 0x2a, 0xc0, 0x29, 0xc0, 0x05, 0xc0, 0x04,
  679. 0xc0, 0x02, 0xc0, 0x03, 0xc0, 0x0f, 0xc0, 0x0e, 0xc0, 0x0c, 0xc0, 0x0d,
  680. 0x00, 0x3d, 0x00, 0x3c, 0x00, 0x2f, 0x00, 0x05, 0x00, 0x04, 0x00, 0x35,
  681. 0x00, 0x0a, 0x00, 0x67, 0x00, 0x6b, 0x00, 0x33, 0x00, 0x39, 0x00, 0x16,
  682. 0x00, 0xaf, 0x00, 0xae, 0x00, 0x8d, 0x00, 0x8c, 0x00, 0x8a, 0x00, 0x8b,
  683. 0x00, 0xb1, 0x00, 0xb0, 0x00, 0x2c, 0x00, 0x3b, 0x01, 0x00, 0x00, 0x3b,
  684. 0x00, 0x00, 0x00, 0x15, 0x00, 0x13, 0x00, 0x00, 0x10, 0x61, 0x70, 0x69,
  685. 0x2e, 0x74, 0x65, 0x78, 0x74, 0x6d, 0x61, 0x74, 0x65, 0x2e, 0x6f, 0x72,
  686. 0x67, 0x00, 0x0a, 0x00, 0x08, 0x00, 0x06, 0x00, 0x17, 0x00, 0x18, 0x00,
  687. 0x19, 0x00, 0x0b, 0x00, 0x02, 0x01, 0x00, 0x00, 0x0d, 0x00, 0x0c, 0x00,
  688. 0x0a, 0x05, 0x01, 0x04, 0x01, 0x02, 0x01, 0x04, 0x03, 0x02, 0x03
  689. };
  690. byte buffer3[] = { /* no sni extension */
  691. 0x16, 0x03, 0x03, 0x00, 0x4d, 0x01, 0x00, 0x00, 0x49, 0x03, 0x03, 0xea,
  692. 0xa1, 0x9f, 0x60, 0xdd, 0x52, 0x12, 0x13, 0xbd, 0x84, 0x34, 0xd5, 0x1c,
  693. 0x38, 0x25, 0xa8, 0x97, 0xd2, 0xd5, 0xc6, 0x45, 0xaf, 0x1b, 0x08, 0xe4,
  694. 0x1e, 0xbb, 0xdf, 0x9d, 0x39, 0xf0, 0x65, 0x00, 0x00, 0x16, 0x00, 0x6b,
  695. 0x00, 0x67, 0x00, 0x39, 0x00, 0x33, 0x00, 0x3d, 0x00, 0x3c, 0x00, 0x35,
  696. 0x00, 0x2f, 0x00, 0x05, 0x00, 0x04, 0x00, 0x0a, 0x01, 0x00, 0x00, 0x0a,
  697. 0x00, 0x0d, 0x00, 0x06, 0x00, 0x04, 0x04, 0x01, 0x02, 0x01
  698. };
  699. byte buffer4[] = { /* last extension has zero size */
  700. 0x16, 0x03, 0x01, 0x00, 0xba, 0x01, 0x00, 0x00,
  701. 0xb6, 0x03, 0x03, 0x83, 0xa3, 0xe6, 0xdc, 0x16, 0xa1, 0x43, 0xe9, 0x45,
  702. 0x15, 0xbd, 0x64, 0xa9, 0xb6, 0x07, 0xb4, 0x50, 0xc6, 0xdd, 0xff, 0xc2,
  703. 0xd3, 0x0d, 0x4f, 0x36, 0xb4, 0x41, 0x51, 0x61, 0xc1, 0xa5, 0x9e, 0x00,
  704. 0x00, 0x28, 0xcc, 0x14, 0xcc, 0x13, 0xc0, 0x2b, 0xc0, 0x2f, 0x00, 0x9e,
  705. 0xc0, 0x0a, 0xc0, 0x09, 0xc0, 0x13, 0xc0, 0x14, 0xc0, 0x07, 0xc0, 0x11,
  706. 0x00, 0x33, 0x00, 0x32, 0x00, 0x39, 0x00, 0x9c, 0x00, 0x2f, 0x00, 0x35,
  707. 0x00, 0x0a, 0x00, 0x05, 0x00, 0x04, 0x01, 0x00, 0x00, 0x65, 0xff, 0x01,
  708. 0x00, 0x01, 0x00, 0x00, 0x0a, 0x00, 0x08, 0x00, 0x06, 0x00, 0x17, 0x00,
  709. 0x18, 0x00, 0x19, 0x00, 0x0b, 0x00, 0x02, 0x01, 0x00, 0x00, 0x23, 0x00,
  710. 0x00, 0x33, 0x74, 0x00, 0x00, 0x00, 0x10, 0x00, 0x1b, 0x00, 0x19, 0x06,
  711. 0x73, 0x70, 0x64, 0x79, 0x2f, 0x33, 0x08, 0x73, 0x70, 0x64, 0x79, 0x2f,
  712. 0x33, 0x2e, 0x31, 0x08, 0x68, 0x74, 0x74, 0x70, 0x2f, 0x31, 0x2e, 0x31,
  713. 0x75, 0x50, 0x00, 0x00, 0x00, 0x05, 0x00, 0x05, 0x01, 0x00, 0x00, 0x00,
  714. 0x00, 0x00, 0x0d, 0x00, 0x12, 0x00, 0x10, 0x04, 0x01, 0x05, 0x01, 0x02,
  715. 0x01, 0x04, 0x03, 0x05, 0x03, 0x02, 0x03, 0x04, 0x02, 0x02, 0x02, 0x00,
  716. 0x12, 0x00, 0x00
  717. };
  718. byte result[32] = {0};
  719. word32 length = 32;
  720. AssertIntEQ(0, wolfSSL_SNI_GetFromBuffer(buffer4, sizeof(buffer4),
  721. 0, result, &length));
  722. AssertIntEQ(0, wolfSSL_SNI_GetFromBuffer(buffer3, sizeof(buffer3),
  723. 0, result, &length));
  724. AssertIntEQ(0, wolfSSL_SNI_GetFromBuffer(buffer2, sizeof(buffer2),
  725. 1, result, &length));
  726. AssertIntEQ(BUFFER_ERROR, wolfSSL_SNI_GetFromBuffer(buffer, sizeof(buffer),
  727. 0, result, &length));
  728. buffer[0] = 0x16;
  729. AssertIntEQ(BUFFER_ERROR, wolfSSL_SNI_GetFromBuffer(buffer, sizeof(buffer),
  730. 0, result, &length));
  731. buffer[1] = 0x03;
  732. AssertIntEQ(SNI_UNSUPPORTED, wolfSSL_SNI_GetFromBuffer(buffer,
  733. sizeof(buffer), 0, result, &length));
  734. buffer[2] = 0x03;
  735. AssertIntEQ(INCOMPLETE_DATA, wolfSSL_SNI_GetFromBuffer(buffer,
  736. sizeof(buffer), 0, result, &length));
  737. buffer[4] = 0x64;
  738. AssertIntEQ(SSL_SUCCESS, wolfSSL_SNI_GetFromBuffer(buffer, sizeof(buffer),
  739. 0, result, &length));
  740. result[length] = 0;
  741. AssertStrEQ("www.paypal.com", (const char*) result);
  742. length = 32;
  743. AssertIntEQ(SSL_SUCCESS, wolfSSL_SNI_GetFromBuffer(buffer2, sizeof(buffer2),
  744. 0, result, &length));
  745. result[length] = 0;
  746. AssertStrEQ("api.textmate.org", (const char*) result);
  747. }
  748. static void test_wolfSSL_client_server(callback_functions* client_callbacks,
  749. callback_functions* server_callbacks)
  750. {
  751. #ifdef HAVE_IO_TESTS_DEPENDENCIES
  752. tcp_ready ready;
  753. func_args client_args;
  754. func_args server_args;
  755. THREAD_TYPE serverThread;
  756. StartTCP();
  757. client_args.callbacks = client_callbacks;
  758. server_args.callbacks = server_callbacks;
  759. #ifdef WOLFSSL_TIRTOS
  760. fdOpenSession(Task_self());
  761. #endif
  762. /* RUN Server side */
  763. InitTcpReady(&ready);
  764. server_args.signal = &ready;
  765. client_args.signal = &ready;
  766. start_thread(run_wolfssl_server, &server_args, &serverThread);
  767. wait_tcp_ready(&server_args);
  768. /* RUN Client side */
  769. run_wolfssl_client(&client_args);
  770. join_thread(serverThread);
  771. FreeTcpReady(&ready);
  772. #ifdef WOLFSSL_TIRTOS
  773. fdCloseSession(Task_self());
  774. #endif
  775. #else
  776. (void)client_callbacks;
  777. (void)server_callbacks;
  778. #endif
  779. }
  780. #endif /* HAVE_SNI */
  781. static void test_wolfSSL_UseSNI(void)
  782. {
  783. #ifdef HAVE_SNI
  784. callback_functions client_callbacks = {wolfSSLv23_client_method, 0, 0, 0};
  785. callback_functions server_callbacks = {wolfSSLv23_server_method, 0, 0, 0};
  786. WOLFSSL_CTX *ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
  787. WOLFSSL *ssl = wolfSSL_new(ctx);
  788. AssertNotNull(ctx);
  789. AssertNotNull(ssl);
  790. /* error cases */
  791. AssertIntNE(SSL_SUCCESS,
  792. wolfSSL_CTX_UseSNI(NULL, 0, (void *) "ctx", XSTRLEN("ctx")));
  793. AssertIntNE(SSL_SUCCESS,
  794. wolfSSL_UseSNI( NULL, 0, (void *) "ssl", XSTRLEN("ssl")));
  795. AssertIntNE(SSL_SUCCESS,
  796. wolfSSL_CTX_UseSNI(ctx, -1, (void *) "ctx", XSTRLEN("ctx")));
  797. AssertIntNE(SSL_SUCCESS,
  798. wolfSSL_UseSNI( ssl, -1, (void *) "ssl", XSTRLEN("ssl")));
  799. AssertIntNE(SSL_SUCCESS,
  800. wolfSSL_CTX_UseSNI(ctx, 0, (void *) NULL, XSTRLEN("ctx")));
  801. AssertIntNE(SSL_SUCCESS,
  802. wolfSSL_UseSNI( ssl, 0, (void *) NULL, XSTRLEN("ssl")));
  803. /* success case */
  804. AssertIntEQ(SSL_SUCCESS,
  805. wolfSSL_CTX_UseSNI(ctx, 0, (void *) "ctx", XSTRLEN("ctx")));
  806. AssertIntEQ(SSL_SUCCESS,
  807. wolfSSL_UseSNI( ssl, 0, (void *) "ssl", XSTRLEN("ssl")));
  808. wolfSSL_free(ssl);
  809. wolfSSL_CTX_free(ctx);
  810. /* Testing success case at ctx */
  811. client_callbacks.ctx_ready = server_callbacks.ctx_ready = use_SNI_at_ctx;
  812. server_callbacks.on_result = verify_SNI_real_matching;
  813. test_wolfSSL_client_server(&client_callbacks, &server_callbacks);
  814. /* Testing success case at ssl */
  815. client_callbacks.ctx_ready = server_callbacks.ctx_ready = NULL;
  816. client_callbacks.ssl_ready = server_callbacks.ssl_ready = use_SNI_at_ssl;
  817. test_wolfSSL_client_server(&client_callbacks, &server_callbacks);
  818. /* Testing default mismatch behaviour */
  819. client_callbacks.ssl_ready = different_SNI_at_ssl;
  820. client_callbacks.on_result = verify_SNI_abort_on_client;
  821. server_callbacks.on_result = verify_SNI_abort_on_server;
  822. test_wolfSSL_client_server(&client_callbacks, &server_callbacks);
  823. client_callbacks.on_result = NULL;
  824. /* Testing continue on mismatch */
  825. client_callbacks.ssl_ready = different_SNI_at_ssl;
  826. server_callbacks.ssl_ready = use_SNI_WITH_CONTINUE_at_ssl;
  827. server_callbacks.on_result = verify_SNI_no_matching;
  828. test_wolfSSL_client_server(&client_callbacks, &server_callbacks);
  829. /* Testing fake answer on mismatch */
  830. server_callbacks.ssl_ready = use_SNI_WITH_FAKE_ANSWER_at_ssl;
  831. server_callbacks.on_result = verify_SNI_fake_matching;
  832. test_wolfSSL_client_server(&client_callbacks, &server_callbacks);
  833. test_wolfSSL_SNI_GetFromBuffer();
  834. #endif
  835. }
  836. static void test_wolfSSL_UseMaxFragment(void)
  837. {
  838. #ifdef HAVE_MAX_FRAGMENT
  839. WOLFSSL_CTX *ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
  840. WOLFSSL *ssl = wolfSSL_new(ctx);
  841. AssertNotNull(ctx);
  842. AssertNotNull(ssl);
  843. /* error cases */
  844. AssertIntNE(SSL_SUCCESS, wolfSSL_CTX_UseMaxFragment(NULL, WOLFSSL_MFL_2_9));
  845. AssertIntNE(SSL_SUCCESS, wolfSSL_UseMaxFragment( NULL, WOLFSSL_MFL_2_9));
  846. AssertIntNE(SSL_SUCCESS, wolfSSL_CTX_UseMaxFragment(ctx, 0));
  847. AssertIntNE(SSL_SUCCESS, wolfSSL_CTX_UseMaxFragment(ctx, 6));
  848. AssertIntNE(SSL_SUCCESS, wolfSSL_UseMaxFragment(ssl, 0));
  849. AssertIntNE(SSL_SUCCESS, wolfSSL_UseMaxFragment(ssl, 6));
  850. /* success case */
  851. AssertIntEQ(SSL_SUCCESS, wolfSSL_CTX_UseMaxFragment(ctx, WOLFSSL_MFL_2_9));
  852. AssertIntEQ(SSL_SUCCESS, wolfSSL_CTX_UseMaxFragment(ctx, WOLFSSL_MFL_2_10));
  853. AssertIntEQ(SSL_SUCCESS, wolfSSL_CTX_UseMaxFragment(ctx, WOLFSSL_MFL_2_11));
  854. AssertIntEQ(SSL_SUCCESS, wolfSSL_CTX_UseMaxFragment(ctx, WOLFSSL_MFL_2_12));
  855. AssertIntEQ(SSL_SUCCESS, wolfSSL_CTX_UseMaxFragment(ctx, WOLFSSL_MFL_2_13));
  856. AssertIntEQ(SSL_SUCCESS, wolfSSL_UseMaxFragment( ssl, WOLFSSL_MFL_2_9));
  857. AssertIntEQ(SSL_SUCCESS, wolfSSL_UseMaxFragment( ssl, WOLFSSL_MFL_2_10));
  858. AssertIntEQ(SSL_SUCCESS, wolfSSL_UseMaxFragment( ssl, WOLFSSL_MFL_2_11));
  859. AssertIntEQ(SSL_SUCCESS, wolfSSL_UseMaxFragment( ssl, WOLFSSL_MFL_2_12));
  860. AssertIntEQ(SSL_SUCCESS, wolfSSL_UseMaxFragment( ssl, WOLFSSL_MFL_2_13));
  861. wolfSSL_free(ssl);
  862. wolfSSL_CTX_free(ctx);
  863. #endif
  864. }
  865. static void test_wolfSSL_UseTruncatedHMAC(void)
  866. {
  867. #ifdef HAVE_TRUNCATED_HMAC
  868. WOLFSSL_CTX *ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
  869. WOLFSSL *ssl = wolfSSL_new(ctx);
  870. AssertNotNull(ctx);
  871. AssertNotNull(ssl);
  872. /* error cases */
  873. AssertIntNE(SSL_SUCCESS, wolfSSL_CTX_UseTruncatedHMAC(NULL));
  874. AssertIntNE(SSL_SUCCESS, wolfSSL_UseTruncatedHMAC(NULL));
  875. /* success case */
  876. AssertIntEQ(SSL_SUCCESS, wolfSSL_CTX_UseTruncatedHMAC(ctx));
  877. AssertIntEQ(SSL_SUCCESS, wolfSSL_UseTruncatedHMAC(ssl));
  878. wolfSSL_free(ssl);
  879. wolfSSL_CTX_free(ctx);
  880. #endif
  881. }
  882. static void test_wolfSSL_UseSupportedCurve(void)
  883. {
  884. #ifdef HAVE_SUPPORTED_CURVES
  885. WOLFSSL_CTX *ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
  886. WOLFSSL *ssl = wolfSSL_new(ctx);
  887. AssertNotNull(ctx);
  888. AssertNotNull(ssl);
  889. #ifndef NO_WOLFSSL_CLIENT
  890. /* error cases */
  891. AssertIntNE(SSL_SUCCESS,
  892. wolfSSL_CTX_UseSupportedCurve(NULL, WOLFSSL_ECC_SECP160R1));
  893. AssertIntNE(SSL_SUCCESS, wolfSSL_CTX_UseSupportedCurve(ctx, 0));
  894. AssertIntNE(SSL_SUCCESS,
  895. wolfSSL_UseSupportedCurve(NULL, WOLFSSL_ECC_SECP160R1));
  896. AssertIntNE(SSL_SUCCESS, wolfSSL_UseSupportedCurve(ssl, 0));
  897. /* success case */
  898. AssertIntEQ(SSL_SUCCESS,
  899. wolfSSL_CTX_UseSupportedCurve(ctx, WOLFSSL_ECC_SECP160R1));
  900. AssertIntEQ(SSL_SUCCESS,
  901. wolfSSL_UseSupportedCurve(ssl, WOLFSSL_ECC_SECP160R1));
  902. #endif
  903. wolfSSL_free(ssl);
  904. wolfSSL_CTX_free(ctx);
  905. #endif
  906. }
  907. /*----------------------------------------------------------------------------*
  908. | Main
  909. *----------------------------------------------------------------------------*/
  910. void ApiTest(void)
  911. {
  912. printf(" Begin API Tests\n");
  913. test_wolfSSL_Init();
  914. test_wolfSSL_Method_Allocators();
  915. test_wolfSSL_CTX_new(wolfSSLv23_server_method());
  916. test_wolfSSL_CTX_use_certificate_file();
  917. test_wolfSSL_CTX_use_PrivateKey_file();
  918. test_wolfSSL_CTX_load_verify_locations();
  919. test_server_wolfSSL_new();
  920. test_client_wolfSSL_new();
  921. test_wolfSSL_read_write();
  922. /* TLS extensions tests */
  923. test_wolfSSL_UseSNI();
  924. test_wolfSSL_UseMaxFragment();
  925. test_wolfSSL_UseTruncatedHMAC();
  926. test_wolfSSL_UseSupportedCurve();
  927. test_wolfSSL_Cleanup();
  928. printf(" End API Tests\n");
  929. }