tls_threaded.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642
  1. /* tls_threaded.c
  2. *
  3. * Copyright (C) 2006-2023 wolfSSL Inc.
  4. *
  5. * This file is part of wolfSSL.
  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-1335, USA
  20. */
  21. #ifndef WOLFSSL_USER_SETTINGS
  22. #include <wolfssl/options.h>
  23. #endif
  24. #include <wolfssl/wolfcrypt/settings.h>
  25. #include <wolfssl/ssl.h>
  26. #define USE_CERT_BUFFERS_256
  27. #include <wolfssl/certs_test.h>
  28. #include <wolfssl/test.h>
  29. #ifdef WOLFSSL_ZEPHYR
  30. #define printf printk
  31. #endif
  32. /* wolfSSL PSA Crypto API integration with ECDH/ECDSA currently requires
  33. * use of wolfSSL Public Key (PK) callbacks.
  34. *
  35. * PSA Crypto API integration for this sample was tested on a
  36. * Nordic nRF5340dk.
  37. */
  38. #if defined(WOLFSSL_HAVE_PSA) && defined(HAVE_PK_CALLBACKS)
  39. #include <wolfssl/wolfcrypt/port/psa/psa.h>
  40. #endif
  41. #define BUFFER_SIZE 2048
  42. #define STATIC_MEM_SIZE (96*1024)
  43. #define THREAD_STACK_SIZE (13*1024)
  44. /* The stack to use in the server's thread. */
  45. K_THREAD_STACK_DEFINE(server_stack, THREAD_STACK_SIZE);
  46. #ifdef WOLFSSL_STATIC_MEMORY
  47. static WOLFSSL_HEAP_HINT* HEAP_HINT_SERVER;
  48. static WOLFSSL_HEAP_HINT* HEAP_HINT_CLIENT;
  49. static byte gMemoryServer[STATIC_MEM_SIZE];
  50. static byte gMemoryClient[STATIC_MEM_SIZE];
  51. #else
  52. #define HEAP_HINT_SERVER NULL
  53. #define HEAP_HINT_CLIENT NULL
  54. #endif /* WOLFSSL_STATIC_MEMORY */
  55. /* Buffer to hold data for client to read. */
  56. unsigned char client_buffer[BUFFER_SIZE];
  57. int client_buffer_sz = 0;
  58. wolfSSL_Mutex client_mutex;
  59. /* Buffer to hold data for server to read. */
  60. unsigned char server_buffer[BUFFER_SIZE];
  61. int server_buffer_sz = 0;
  62. wolfSSL_Mutex server_mutex;
  63. #if defined(WOLFSSL_HAVE_PSA) && defined(HAVE_PK_CALLBACKS)
  64. static struct psa_ssl_ctx server_psa_ctx;
  65. static struct psa_ssl_ctx client_psa_ctx;
  66. /* psa_key_id_t representing server key loaded into PSA Crypto API */
  67. static psa_key_id_t ecc_key_id;
  68. #endif
  69. /* Application data to send. */
  70. static const char msgHTTPGet[] = "GET /index.html HTTP/1.0\r\n\r\n";
  71. static const char msgHTTPIndex[] =
  72. "HTTP/1.1 200 OK\n"
  73. "Content-Type: text/html\n"
  74. "Connection: close\n"
  75. "\n"
  76. "<html>\n"
  77. "<head>\n"
  78. "<title>Welcome to wolfSSL!</title>\n"
  79. "</head>\n"
  80. "<body>\n"
  81. "<p>wolfSSL has successfully performed handshake!</p>\n"
  82. "</body>\n"
  83. "</html>\n";
  84. /* wolfSSL client wants to read data from the server. */
  85. static int recv_client(WOLFSSL* ssl, char* buff, int sz, void* ctx)
  86. {
  87. wc_LockMutex(&client_mutex);
  88. if (client_buffer_sz > 0) {
  89. /* Take as many bytes is available or requested from buffer. */
  90. if (sz > client_buffer_sz)
  91. sz = client_buffer_sz;
  92. XMEMCPY(buff, client_buffer, sz);
  93. if (sz < client_buffer_sz) {
  94. XMEMMOVE(client_buffer, client_buffer + sz, client_buffer_sz - sz);
  95. }
  96. client_buffer_sz -= sz;
  97. }
  98. else
  99. sz = WOLFSSL_CBIO_ERR_WANT_READ;
  100. wc_UnLockMutex(&client_mutex);
  101. return sz;
  102. }
  103. /* wolfSSL client wants to write data to the server. */
  104. static int send_client(WOLFSSL* ssl, char* buff, int sz, void* ctx)
  105. {
  106. wc_LockMutex(&server_mutex);
  107. if (server_buffer_sz < BUFFER_SIZE)
  108. {
  109. /* Put in as many bytes requested or will fit in buffer. */
  110. if (sz > BUFFER_SIZE - server_buffer_sz)
  111. sz = BUFFER_SIZE - server_buffer_sz;
  112. XMEMCPY(server_buffer + server_buffer_sz, buff, sz);
  113. server_buffer_sz += sz;
  114. }
  115. else
  116. sz = WOLFSSL_CBIO_ERR_WANT_WRITE;
  117. wc_UnLockMutex(&server_mutex);
  118. return sz;
  119. }
  120. /* wolfSSL server wants to read data from the client. */
  121. static int recv_server(WOLFSSL* ssl, char* buff, int sz, void* ctx)
  122. {
  123. wc_LockMutex(&server_mutex);
  124. if (server_buffer_sz > 0) {
  125. /* Take as many bytes is available or requested from buffer. */
  126. if (sz > server_buffer_sz)
  127. sz = server_buffer_sz;
  128. XMEMCPY(buff, server_buffer, sz);
  129. if (sz < server_buffer_sz) {
  130. XMEMMOVE(server_buffer, server_buffer + sz, server_buffer_sz - sz);
  131. }
  132. server_buffer_sz -= sz;
  133. }
  134. else
  135. sz = WOLFSSL_CBIO_ERR_WANT_READ;
  136. wc_UnLockMutex(&server_mutex);
  137. return sz;
  138. }
  139. /* wolfSSL server wants to write data to the client. */
  140. static int send_server(WOLFSSL* ssl, char* buff, int sz, void* ctx)
  141. {
  142. wc_LockMutex(&client_mutex);
  143. if (client_buffer_sz < BUFFER_SIZE)
  144. {
  145. /* Put in as many bytes requested or will fit in buffer. */
  146. if (sz > BUFFER_SIZE - client_buffer_sz)
  147. sz = BUFFER_SIZE - client_buffer_sz;
  148. XMEMCPY(client_buffer + client_buffer_sz, buff, sz);
  149. client_buffer_sz += sz;
  150. }
  151. else
  152. sz = WOLFSSL_CBIO_ERR_WANT_WRITE;
  153. wc_UnLockMutex(&client_mutex);
  154. return sz;
  155. }
  156. /* Create a new wolfSSL client with a server CA certificate. */
  157. static int wolfssl_client_new(WOLFSSL_CTX** ctx, WOLFSSL** ssl)
  158. {
  159. int ret = 0;
  160. WOLFSSL_CTX* client_ctx = NULL;
  161. WOLFSSL* client_ssl = NULL;
  162. /* Create and initialize WOLFSSL_CTX */
  163. if ((client_ctx = wolfSSL_CTX_new_ex(wolfTLSv1_3_client_method(),
  164. HEAP_HINT_CLIENT)) == NULL) {
  165. printf("ERROR: failed to create WOLFSSL_CTX\n");
  166. ret = -1;
  167. }
  168. if (ret == 0) {
  169. /* Load client certificates into WOLFSSL_CTX */
  170. if (wolfSSL_CTX_load_verify_buffer(client_ctx, ca_ecc_cert_der_256,
  171. sizeof_ca_ecc_cert_der_256, WOLFSSL_FILETYPE_ASN1) !=
  172. WOLFSSL_SUCCESS) {
  173. printf("ERROR: failed to load CA certificate\n");
  174. ret = -1;
  175. }
  176. }
  177. #if defined(WOLFSSL_HAVE_PSA) && defined(HAVE_PK_CALLBACKS)
  178. if (ret == 0) {
  179. if (wolfSSL_CTX_psa_enable(client_ctx) != WOLFSSL_SUCCESS) {
  180. printf("ERROR: failed to enable PSA Crypto API for WOLFSSL_CTX\n");
  181. ret = -1;
  182. }
  183. }
  184. #endif
  185. if (ret == 0) {
  186. /* Register callbacks */
  187. wolfSSL_SetIORecv(client_ctx, recv_client);
  188. wolfSSL_SetIOSend(client_ctx, send_client);
  189. /* Create a WOLFSSL object */
  190. if ((client_ssl = wolfSSL_new(client_ctx)) == NULL) {
  191. printf("ERROR: failed to create WOLFSSL object\n");
  192. ret = -1;
  193. }
  194. }
  195. #if defined(WOLFSSL_HAVE_PSA) && defined(HAVE_PK_CALLBACKS)
  196. if (ret == 0) {
  197. XMEMSET(&client_psa_ctx, 0, sizeof(client_psa_ctx));
  198. if (wolfSSL_set_psa_ctx(client_ssl, &client_psa_ctx) != WOLFSSL_SUCCESS) {
  199. printf("ERROR: wolfSSL_set_psa_ctx() failed\n");
  200. ret = -1;
  201. }
  202. }
  203. #endif
  204. if (ret == 0) {
  205. /* make wolfSSL object nonblocking */
  206. wolfSSL_set_using_nonblock(client_ssl, 1);
  207. /* Return newly created wolfSSL context and object */
  208. *ctx = client_ctx;
  209. *ssl = client_ssl;
  210. }
  211. else {
  212. if (client_ssl != NULL) {
  213. #if defined(WOLFSSL_HAVE_PSA) && defined(HAVE_PK_CALLBACKS)
  214. wolfSSL_free_psa_ctx(&client_psa_ctx);
  215. #endif
  216. wolfSSL_free(client_ssl);
  217. }
  218. if (client_ctx != NULL)
  219. wolfSSL_CTX_free(client_ctx);
  220. }
  221. return ret;
  222. }
  223. /* Client connecting to server using TLS */
  224. static int wolfssl_client_connect(WOLFSSL* ssl)
  225. {
  226. int ret = 0;
  227. if (wolfSSL_connect(ssl) != WOLFSSL_SUCCESS) {
  228. if (!wolfSSL_want_read(ssl) && !wolfSSL_want_write(ssl))
  229. ret = -1;
  230. }
  231. return ret;
  232. }
  233. #if defined(WOLFSSL_HAVE_PSA) && defined(HAVE_PK_CALLBACKS)
  234. /* ./certs/ecc-key.pem */
  235. static const unsigned char ecc_key_256[] =
  236. {
  237. 0x45, 0xB6, 0x69, 0x02, 0x73, 0x9C, 0x6C, 0x85, 0xA1, 0x38,
  238. 0x5B, 0x72, 0xE8, 0xE8, 0xC7, 0xAC, 0xC4, 0x03, 0x8D, 0x53,
  239. 0x35, 0x04, 0xFA, 0x6C, 0x28, 0xDC, 0x34, 0x8D, 0xE1, 0xA8,
  240. 0x09, 0x8C
  241. };
  242. /* Provision server private key using PSA Crypto API.
  243. *
  244. * key_id - resulting psa_key_id_t
  245. *
  246. * Returns - 0 on success, negative on error
  247. */
  248. static int psa_private_key_provisioning(psa_key_id_t *key_id)
  249. {
  250. psa_key_attributes_t key_attr = { 0 };
  251. psa_key_type_t key_type;
  252. psa_status_t status;
  253. key_type = PSA_KEY_TYPE_ECC_KEY_PAIR(PSA_ECC_FAMILY_SECP_R1);
  254. psa_set_key_usage_flags(&key_attr, PSA_KEY_USAGE_SIGN_HASH);
  255. psa_set_key_lifetime(&key_attr, PSA_KEY_LIFETIME_VOLATILE);
  256. psa_set_key_algorithm(&key_attr, PSA_ALG_ECDSA(PSA_ALG_SHA_256));
  257. psa_set_key_type(&key_attr, key_type);
  258. psa_set_key_bits(&key_attr, 256);
  259. status = psa_import_key(&key_attr, ecc_key_256,
  260. sizeof(ecc_key_256), key_id);
  261. if (status != PSA_SUCCESS) {
  262. printf("ERROR: provisioning of private key failed: [%d] \n", status);
  263. return -1;
  264. }
  265. return 0;
  266. }
  267. #endif /* WOLFSSL_HAVE_PSA & HAVE_PK_CALLBACKS */
  268. /* Create a new wolfSSL server with a certificate for authentication. */
  269. static int wolfssl_server_new(WOLFSSL_CTX** ctx, WOLFSSL** ssl)
  270. {
  271. int ret = 0;
  272. WOLFSSL_CTX* server_ctx = NULL;
  273. WOLFSSL* server_ssl = NULL;
  274. #if defined(WOLFSSL_HAVE_PSA) && defined(HAVE_PK_CALLBACKS)
  275. /* Provision ECC private key with PSA Crypto API */
  276. if (psa_private_key_provisioning(&ecc_key_id) != 0) {
  277. printf("ERROR: failed to provision PSA private key\n");
  278. ret = -1;
  279. }
  280. if (ret == 0) {
  281. XMEMSET(&server_psa_ctx, 0, sizeof(server_psa_ctx));
  282. wolfSSL_psa_set_private_key_id(&server_psa_ctx, ecc_key_id);
  283. }
  284. #endif
  285. if (ret == 0) {
  286. /* Create and initialize WOLFSSL_CTX */
  287. if ((server_ctx = wolfSSL_CTX_new_ex(wolfTLSv1_3_server_method(),
  288. HEAP_HINT_SERVER)) == NULL) {
  289. printf("ERROR: failed to create WOLFSSL_CTX\n");
  290. ret = -1;
  291. }
  292. }
  293. if (ret == 0) {
  294. /* Load client certificates into WOLFSSL_CTX */
  295. if (wolfSSL_CTX_use_certificate_buffer(server_ctx,
  296. serv_ecc_der_256, sizeof_serv_ecc_der_256,
  297. WOLFSSL_FILETYPE_ASN1) != WOLFSSL_SUCCESS) {
  298. printf("ERROR: failed to load server certificate\n");
  299. ret = -1;
  300. }
  301. }
  302. #if !defined(WOLFSSL_HAVE_PSA) || \
  303. (defined(WOLFSSL_HAVE_PSA) && !defined(HAVE_PK_CALLBACKS))
  304. if (ret == 0) {
  305. /* Load client certificates into WOLFSSL_CTX */
  306. if (wolfSSL_CTX_use_PrivateKey_buffer(server_ctx,
  307. ecc_key_der_256, sizeof_ecc_key_der_256,
  308. WOLFSSL_FILETYPE_ASN1) != WOLFSSL_SUCCESS) {
  309. printf("ERROR: failed to load server key\n");
  310. ret = -1;
  311. }
  312. }
  313. #else
  314. if (ret == 0) {
  315. if (wolfSSL_CTX_psa_enable(server_ctx) != WOLFSSL_SUCCESS) {
  316. printf("ERROR: failed to enable PSA\n");
  317. ret = -1;
  318. }
  319. }
  320. #endif /* WOLFSSL_HAVE_PSA */
  321. if (ret == 0) {
  322. /* Register callbacks */
  323. wolfSSL_SetIORecv(server_ctx, recv_server);
  324. wolfSSL_SetIOSend(server_ctx, send_server);
  325. /* Create a WOLFSSL object */
  326. if ((server_ssl = wolfSSL_new(server_ctx)) == NULL) {
  327. printf("ERROR: failed to create WOLFSSL object\n");
  328. ret = -1;
  329. }
  330. }
  331. #if defined(WOLFSSL_HAVE_PSA) && defined(HAVE_PK_CALLBACKS)
  332. if (ret == 0) {
  333. if (wolfSSL_set_psa_ctx(server_ssl, &server_psa_ctx)
  334. != WOLFSSL_SUCCESS) {
  335. printf("ERROR: failed to enable PSA in WOLFSSL struct\n");
  336. ret = -1;
  337. }
  338. }
  339. #endif
  340. if (ret == 0) {
  341. /* make wolfSSL object nonblocking */
  342. wolfSSL_set_using_nonblock(server_ssl, 1);
  343. /* Return newly created wolfSSL context and object */
  344. *ctx = server_ctx;
  345. *ssl = server_ssl;
  346. }
  347. else {
  348. if (server_ssl != NULL) {
  349. #if defined(WOLFSSL_HAVE_PSA) && defined(HAVE_PK_CALLBACKS)
  350. wolfSSL_free_psa_ctx(&server_psa_ctx);
  351. #endif
  352. wolfSSL_free(server_ssl);
  353. }
  354. if (server_ctx != NULL)
  355. wolfSSL_CTX_free(server_ctx);
  356. }
  357. return ret;
  358. }
  359. /* Server accepting a client using TLS */
  360. static int wolfssl_server_accept(WOLFSSL* ssl)
  361. {
  362. int ret = 0;
  363. if (wolfSSL_accept(ssl) != WOLFSSL_SUCCESS) {
  364. if (!wolfSSL_want_read(ssl) && !wolfSSL_want_write(ssl))
  365. ret = -1;
  366. }
  367. return ret;
  368. }
  369. /* Send application data. */
  370. static int wolfssl_send(WOLFSSL* ssl, const char* msg)
  371. {
  372. int ret = 0;
  373. int len;
  374. printf("Sending:\n%s\n", msg);
  375. len = wolfSSL_write(ssl, msg, XSTRLEN(msg));
  376. if (len < 0)
  377. ret = len;
  378. else if (len != XSTRLEN(msg))
  379. ret = -1;
  380. return ret;
  381. }
  382. /* Receive application data. */
  383. static int wolfssl_recv(WOLFSSL* ssl)
  384. {
  385. int ret;
  386. byte reply[256];
  387. ret = wolfSSL_read(ssl, reply, sizeof(reply)-1);
  388. if (ret > 0) {
  389. reply[ret] = '\0';
  390. printf("Received:\n%s\n", reply);
  391. ret = 1;
  392. }
  393. else if (wolfSSL_want_read(ssl) || wolfSSL_want_write(ssl))
  394. ret = 0;
  395. return ret;
  396. }
  397. /* Free the WOLFSSL object and context. */
  398. static void wolfssl_free(WOLFSSL_CTX* ctx, WOLFSSL* ssl)
  399. {
  400. if (ssl != NULL)
  401. wolfSSL_free(ssl);
  402. if (ctx != NULL)
  403. wolfSSL_CTX_free(ctx);
  404. }
  405. /* Display the static memory usage. */
  406. static void wolfssl_memstats(WOLFSSL* ssl)
  407. {
  408. #ifdef WOLFSSL_STATIC_MEMORY
  409. WOLFSSL_MEM_CONN_STATS ssl_stats;
  410. XMEMSET(&ssl_stats, 0 , sizeof(ssl_stats));
  411. if (wolfSSL_is_static_memory(ssl, &ssl_stats) != 1)
  412. printf("static memory was not used with ssl");
  413. else {
  414. printf("*** This is memory state before wolfSSL_free is called\n");
  415. printf("peak connection memory = %d\n", ssl_stats.peakMem);
  416. printf("current memory in use = %d\n", ssl_stats.curMem);
  417. printf("peak connection allocs = %d\n", ssl_stats.peakAlloc);
  418. printf("current connection allocs = %d\n",ssl_stats.curAlloc);
  419. printf("total connection allocs = %d\n",ssl_stats.totalAlloc);
  420. printf("total connection frees = %d\n\n", ssl_stats.totalFr);
  421. }
  422. #else
  423. (void)ssl;
  424. #endif
  425. }
  426. /* Start the server thread. */
  427. void start_thread(THREAD_FUNC func, func_args* args, THREAD_TYPE* thread)
  428. {
  429. k_thread_create(thread, server_stack, K_THREAD_STACK_SIZEOF(server_stack),
  430. func, args, NULL, NULL, 5, 0, K_NO_WAIT);
  431. }
  432. void join_thread(THREAD_TYPE thread)
  433. {
  434. /* Threads are handled in the kernel. */
  435. }
  436. /* Thread to do the server operations. */
  437. void server_thread(void* arg1, void* arg2, void* arg3)
  438. {
  439. int ret = 0;
  440. WOLFSSL_CTX* server_ctx = NULL;
  441. WOLFSSL* server_ssl = NULL;
  442. #ifdef WOLFSSL_STATIC_MEMORY
  443. if (wc_LoadStaticMemory(&HEAP_HINT_SERVER, gMemoryServer,
  444. sizeof(gMemoryServer),
  445. WOLFMEM_GENERAL | WOLFMEM_TRACK_STATS, 1) != 0) {
  446. printf("unable to load static memory");
  447. ret = -1;
  448. }
  449. if (ret == 0)
  450. #endif
  451. ret = wolfssl_server_new(&server_ctx, &server_ssl);
  452. while (ret == 0) {
  453. ret = wolfssl_server_accept(server_ssl);
  454. if (ret == 0 && wolfSSL_is_init_finished(server_ssl))
  455. break;
  456. }
  457. /* Receive HTTP request */
  458. while (ret == 0) {
  459. ret = wolfssl_recv(server_ssl);
  460. }
  461. if (ret == 1)
  462. ret = 0;
  463. /* Send HTTP response */
  464. if (ret == 0)
  465. ret = wolfssl_send(server_ssl, msgHTTPIndex);
  466. printf("Server Return: %d\n", ret);
  467. #ifdef WOLFSSL_STATIC_MEMORY
  468. printf("Server Memory Stats\n");
  469. #endif
  470. wolfssl_memstats(server_ssl);
  471. #if defined(WOLFSSL_HAVE_PSA) && defined(HAVE_PK_CALLBACKS)
  472. if (server_ssl != NULL) {
  473. wolfSSL_free_psa_ctx(&server_psa_ctx);
  474. }
  475. #endif
  476. wolfssl_free(server_ctx, server_ssl);
  477. }
  478. int main()
  479. {
  480. int ret = 0;
  481. WOLFSSL_CTX* client_ctx = NULL;
  482. WOLFSSL* client_ssl = NULL;
  483. THREAD_TYPE serverThread;
  484. /* set dummy wallclock time for cert validation without NTP/etc */
  485. struct timespec utctime;
  486. utctime.tv_sec = 1658510212; /* Friday, July 22, 2022 5:16:52 PM GMT */
  487. utctime.tv_nsec = 0;
  488. clock_settime(CLOCK_REALTIME, &utctime);
  489. wolfSSL_Init();
  490. #ifdef DEBUG_WOLFSSL
  491. wolfSSL_Debugging_ON();
  492. #endif
  493. wc_InitMutex(&client_mutex);
  494. wc_InitMutex(&server_mutex);
  495. /* Start server */
  496. start_thread(server_thread, NULL, &serverThread);
  497. #ifdef WOLFSSL_STATIC_MEMORY
  498. if (wc_LoadStaticMemory(&HEAP_HINT_CLIENT, gMemoryClient,
  499. sizeof(gMemoryClient),
  500. WOLFMEM_GENERAL | WOLFMEM_TRACK_STATS, 1) != 0) {
  501. printf("unable to load static memory");
  502. ret = -1;
  503. }
  504. if (ret == 0)
  505. #endif
  506. {
  507. /* Client connection */
  508. ret = wolfssl_client_new(&client_ctx, &client_ssl);
  509. }
  510. while (ret == 0) {
  511. ret = wolfssl_client_connect(client_ssl);
  512. if (ret == 0 && wolfSSL_is_init_finished(client_ssl))
  513. break;
  514. k_sleep(Z_TIMEOUT_TICKS(10));
  515. }
  516. if (ret == 0) {
  517. printf("Handshake complete\n");
  518. /* Send HTTP request */
  519. ret = wolfssl_send(client_ssl, msgHTTPGet);
  520. }
  521. /* Receive HTTP response */
  522. while (ret == 0) {
  523. k_sleep(Z_TIMEOUT_TICKS(10));
  524. ret = wolfssl_recv(client_ssl);
  525. }
  526. if (ret == 1)
  527. ret = 0;
  528. printf("Client Return: %d\n", ret);
  529. join_thread(serverThread);
  530. #ifdef WOLFSSL_STATIC_MEMORY
  531. printf("Client Memory Stats\n");
  532. #endif
  533. wolfssl_memstats(client_ssl);
  534. #if defined(WOLFSSL_HAVE_PSA) && defined(HAVE_PK_CALLBACKS)
  535. if (client_ssl != NULL) {
  536. wolfSSL_free_psa_ctx(&client_psa_ctx);
  537. }
  538. #endif
  539. wolfssl_free(client_ctx, client_ssl);
  540. wolfSSL_Cleanup();
  541. printf("Done\n");
  542. return (ret == 0) ? 0 : 1;
  543. }