api.c 38 KB

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