wolfssl_demo.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769
  1. /* wolfssl_demo.c
  2. *
  3. * Copyright (C) 2006-2024 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. #include <stdio.h>
  22. #include <string.h>
  23. #include <stdlib.h>
  24. #include <stdarg.h>
  25. #include <wolfssl/wolfcrypt/settings.h>
  26. #include "wolfssl/ssl.h"
  27. #include <wolfssl/wolfio.h>
  28. #include "wolfssl/certs_test.h"
  29. #include "wolfssl/wolfcrypt/types.h"
  30. #include "wolfssl_demo.h"
  31. #include <wolfcrypt/test/test.h>
  32. #include <wolfcrypt/benchmark/benchmark.h>
  33. #include "FreeRTOS.h"
  34. #if defined(FREERTOS_TCP)
  35. #include "FreeRTOS_IP.h"
  36. #include "FreeRTOS_Sockets.h"
  37. #include "platform/iot_network.h"
  38. #include "platform.h"
  39. #endif
  40. #if defined(BENCHMARK)
  41. #include "r_cmt_rx_if.h"
  42. #endif
  43. #if defined(TLS_CLIENT)
  44. #if defined(WOLFSSL_RENESAS_TSIP_TLS)
  45. #include "key_data.h"
  46. #include <wolfssl/wolfcrypt/port/Renesas/renesas-tsip-crypt.h>
  47. extern const st_key_block_data_t g_key_block_data;
  48. uint32_t g_encrypted_root_public_key[140];
  49. #if defined(TLS_MULTITHREAD_TEST)
  50. static TsipUserCtx userContext_taskA;
  51. static TsipUserCtx userContext_taskB;
  52. #else
  53. static TsipUserCtx userContext;
  54. #endif
  55. #endif /* WOLFSSL_RENESAS_TSIP_TLS */
  56. static WOLFSSL_CTX* client_ctx;
  57. #endif /* TLS_CLIENT */
  58. #define TLSSERVER_IP "192.168.11.47"
  59. #define TLSSERVER_PORT 11111
  60. #define YEAR 2024
  61. #define MON 9
  62. #define FREQ 10000 /* Hz */
  63. #define MAX_MSGSTR 80
  64. static long tick;
  65. static int tmTick;
  66. #if defined(TSIP_CRYPT_UNIT_TEST)
  67. int tsip_crypt_test();
  68. int tsip_crypt_sha_multitest();
  69. int tsip_crypt_AesCbc_multitest();
  70. int tsip_crypt_AesGcm_multitest();
  71. int tsip_crypt_Sha_AesCbcGcm_multitest();
  72. #endif
  73. #if defined(TLS_MULTITHREAD_TEST)
  74. xSemaphoreHandle exit_semaph;
  75. static xSemaphoreHandle Mutex;
  76. #endif
  77. static int msg(const char* pname, int l,
  78. const char * sFormat, ...)
  79. {
  80. int ret = 0;
  81. char buf[MAX_MSGSTR] = {0};
  82. va_list ParamList;
  83. #if defined(TLS_MULTITHREAD_TEST)
  84. xSemaphoreTake(Mutex, portMAX_DELAY);
  85. #endif
  86. va_start(ParamList, sFormat);
  87. printf("[%s][%02d] ", pname, l);
  88. ret = vsnprintf(buf, sizeof(buf), sFormat, ParamList);
  89. printf(buf);
  90. va_end(ParamList);
  91. #if defined(TLS_MULTITHREAD_TEST)
  92. xSemaphoreGive(Mutex);
  93. #endif
  94. return ret;
  95. }
  96. #if defined(TLS_MULTITHREAD_TEST)
  97. static void my_Logging_cb(const int logLevel, const char *const logMessage)
  98. {
  99. (void)logLevel;
  100. msg("custom-log", logLevel, "%s\n", logMessage);
  101. }
  102. #endif
  103. /* time
  104. * returns seconds from EPOCH
  105. */
  106. time_t time(time_t *t)
  107. {
  108. (void)t;
  109. return ((YEAR-1970)*365+30*MON)*24*60*60 + tmTick++;
  110. }
  111. /* timeTick
  112. * called periodically by H/W timer to increase tmTick.
  113. */
  114. #if defined(BENCHMARK)
  115. static void timeTick(void* pdata)
  116. {
  117. (void)pdata;
  118. tick++;
  119. }
  120. #endif
  121. double current_time(int reset)
  122. {
  123. if(reset) tick = 0 ;
  124. return ((double)tick/FREQ) ;
  125. }
  126. /* --------------------------------------------------------*/
  127. /* Benchmark_demo */
  128. /* --------------------------------------------------------*/
  129. #if defined(BENCHMARK)
  130. static void Benchmark_demo(void)
  131. {
  132. uint32_t channel;
  133. R_CMT_CreatePeriodic(FREQ, &timeTick, &channel);
  134. printf("Start wolfCrypt Benchmark\n");
  135. benchmark_test(NULL);
  136. printf("End wolfCrypt Benchmark\n");
  137. }
  138. #endif /* BENCHMARK */
  139. /* --------------------------------------------------------*/
  140. /* CryptTest_demo */
  141. /* --------------------------------------------------------*/
  142. #if defined(CRYPT_TEST)
  143. static void CryptTest_demo(void)
  144. {
  145. int ret;
  146. if ((ret = wolfCrypt_Init()) != 0) {
  147. printf("wolfCrypt_Init failed %d\n", ret);
  148. }
  149. printf("Start wolfCrypt Test\n");
  150. wolfcrypt_test(NULL);
  151. printf("End wolfCrypt Test\n");
  152. if ((ret = wolfCrypt_Cleanup()) != 0) {
  153. printf("wolfCrypt_Cleanup failed %d\n", ret);
  154. }
  155. }
  156. #endif /* CRYPT_TEST */
  157. /* --------------------------------------------------------*/
  158. /* Tls_client_demo */
  159. /* --------------------------------------------------------*/
  160. #if defined(TLS_CLIENT)
  161. static void Tls_client_init()
  162. {
  163. #ifndef NO_FILESYSTEM
  164. #ifdef USE_ECC_CERT
  165. char *cert = "./certs/ca-ecc-cert.pem";
  166. #else
  167. char *cert = "./certs/ca-cert.pem";
  168. #endif
  169. #else
  170. #if defined(USE_ECC_CERT) && defined(USE_CERT_BUFFERS_256)
  171. const unsigned char *cert = ca_ecc_cert_der_256;
  172. #define SIZEOF_CERT sizeof_ca_ecc_cert_der_256
  173. #else
  174. const unsigned char *cert = ca_cert_der_2048;
  175. #define SIZEOF_CERT sizeof_ca_cert_der_2048
  176. #endif
  177. #endif
  178. client_ctx = NULL;
  179. wolfSSL_Init();
  180. /* Create and initialize WOLFSSL_CTX */
  181. if ((client_ctx =
  182. wolfSSL_CTX_new(wolfSSLv23_client_method_ex((void *)NULL))) == NULL) {
  183. printf("ERROR: failed to create WOLFSSL_CTX\n");
  184. return;
  185. }
  186. #ifdef WOLFSSL_RENESAS_TSIP_TLS
  187. tsip_set_callbacks(client_ctx);
  188. #endif
  189. /* load root CA certificate */
  190. #if defined(NO_FILESYSTEM)
  191. if (wolfSSL_CTX_load_verify_buffer(client_ctx, cert,
  192. SIZEOF_CERT, SSL_FILETYPE_ASN1) != SSL_SUCCESS) {
  193. printf("ERROR: can't load certificate data\n");
  194. return;
  195. }
  196. #else
  197. if (wolfSSL_CTX_load_verify_locations(client_ctx, cert, 0) != SSL_SUCCESS) {
  198. printf("ERROR: can't load \"%s\"\n", cert);
  199. return NULL;
  200. }
  201. #endif
  202. #if defined(WOLFSSL_TLS13) && defined(WOLFSSL_RENESAS_TSIP_TLS)
  203. if (wolfSSL_CTX_UseSupportedCurve(client_ctx, WOLFSSL_ECC_SECP256R1)
  204. != WOLFSSL_SUCCESS) {
  205. wolfSSL_CTX_free(client_ctx); client_ctx = NULL;
  206. printf("client can't set use supported curves\n");
  207. return;
  208. }
  209. #endif
  210. }
  211. static void Tls_client(void *pvParam)
  212. {
  213. #define BUFF_SIZE 256
  214. #define ADDR_SIZE 16
  215. int ret;
  216. int err;
  217. #if defined(TLS_MULTITHREAD_TEST)
  218. BaseType_t xStatus;
  219. #endif
  220. TestInfo* p = (TestInfo*)pvParam;
  221. WOLFSSL_CTX* ctx = (WOLFSSL_CTX *)client_ctx;
  222. WOLFSSL* ssl = NULL;
  223. Socket_t socket;
  224. socklen_t socksize = sizeof(struct freertos_sockaddr);
  225. struct freertos_sockaddr PeerAddr;
  226. char addrBuff[ADDR_SIZE] = {0};
  227. const char* pcName = p->name;
  228. static const char sendBuff[]= "Hello Server\n" ;
  229. char rcvBuff[BUFF_SIZE] = {0};
  230. if (!p) {
  231. printf("Unexpected error. Thread parameter is null\n");
  232. return;
  233. }
  234. /* create TCP socket */
  235. socket = FreeRTOS_socket(FREERTOS_AF_INET,
  236. FREERTOS_SOCK_STREAM,
  237. FREERTOS_IPPROTO_TCP);
  238. configASSERT(socket != FREERTOS_INVALID_SOCKET);
  239. FreeRTOS_bind(socket, NULL, socksize);
  240. /* attempt to connect TLS server */
  241. PeerAddr.sin_addr = FreeRTOS_inet_addr(TLSSERVER_IP);
  242. PeerAddr.sin_port = FreeRTOS_htons(p->port);
  243. ret = FreeRTOS_connect(socket, &PeerAddr, sizeof(PeerAddr));
  244. if (ret != 0) {
  245. msg(pcName, p->id, "ERROR FreeRTOS_connect: %d\n",ret);
  246. ret = -1;
  247. }
  248. #if defined(TLS_MULTITHREAD_TEST)
  249. msg(pcName, p->id, " Ready to connect.\n");
  250. xStatus = xSemaphoreTake(p->xBinarySemaphore, portMAX_DELAY);
  251. if (xStatus != pdTRUE) {
  252. msg(pcName, p->id, " Error : Failed to xSemaphoreTake\n");
  253. goto out;
  254. }
  255. #endif
  256. /* create WOLFSSL object */
  257. if (ret == 0) {
  258. ssl = wolfSSL_new(ctx);
  259. if (ssl == NULL) {
  260. msg(pcName, p->id, "ERROR wolfSSL_new: %d\n",
  261. wolfSSL_get_error(ssl, 0));
  262. ret = -1;
  263. }
  264. }
  265. if (ret == 0) {
  266. #ifdef WOLFSSL_RENESAS_TSIP_TLS
  267. #if !defined(TLS_MULTITHREAD_TEST)
  268. memset(&userContext, 0, sizeof(TsipUserCtx));
  269. tsip_set_callback_ctx(ssl, &userContext);
  270. #else
  271. if (p->port - TLSSERVER_PORT == 0) {
  272. memset(&userContext_taskA, 0, sizeof(TsipUserCtx));
  273. tsip_set_callback_ctx(ssl, (void*)&userContext_taskA);
  274. }
  275. else {
  276. memset(&userContext_taskB, 0, sizeof(TsipUserCtx));
  277. tsip_set_callback_ctx(ssl, (void*)&userContext_taskB);
  278. }
  279. #endif
  280. #endif
  281. }
  282. msg(pcName, p->id, " Cipher : %s\n", p->cipher);
  283. /* use specific cipher */
  284. if (p->cipher != NULL &&
  285. wolfSSL_set_cipher_list(ssl, p->cipher) != WOLFSSL_SUCCESS) {
  286. ret = -1;
  287. }
  288. if (ret == 0) {
  289. /* associate socket with ssl object */
  290. if (wolfSSL_set_fd(ssl, (int)socket) != WOLFSSL_SUCCESS) {
  291. msg(pcName, p->id, "ERROR wolfSSL_set_fd: %d\n",
  292. wolfSSL_get_error(ssl, 0));
  293. ret = -1;
  294. }
  295. }
  296. /* set client certificate */
  297. #if defined(USE_ECC_CERT)
  298. if (ret == 0) {
  299. err = wolfSSL_use_certificate_buffer(ssl,
  300. cliecc_cert_der_256,
  301. sizeof_cliecc_cert_der_256,
  302. WOLFSSL_FILETYPE_ASN1);
  303. if(err != SSL_SUCCESS) {
  304. printf("ERROR: can't load client-certificate\n");
  305. ret = -1;
  306. }
  307. }
  308. #else
  309. if (ret == 0) {
  310. err = wolfSSL_use_certificate_buffer(ssl,
  311. client_cert_der_2048,
  312. sizeof_client_cert_der_2048,
  313. WOLFSSL_FILETYPE_ASN1);
  314. if (err != SSL_SUCCESS) {
  315. printf("ERROR: can't load client-certificate\n");
  316. ret = -1;
  317. }
  318. }
  319. #endif /* USE_ECC_CERT */
  320. /* set client key(s) */
  321. #if defined(WOLFSSL_RENESAS_TSIP_TLS)
  322. #if defined(USE_ECC_CERT)
  323. /* Client authentication using ECDSA certificate can be handled by TSIP.
  324. * Therefore, the client private key should be TSIP-specific format
  325. * and be set by tsip_use_PrivateKey_buffer_TLS.
  326. */
  327. if (ret == 0){
  328. ret = tsip_use_PrivateKey_buffer_TLS(ssl,
  329. (const char*)g_key_block_data.encrypted_user_ecc256_private_key,
  330. sizeof(g_key_block_data.encrypted_user_ecc256_private_key),
  331. TSIP_ECCP256);
  332. if (ret != 0) {
  333. printf("ERROR tsip_use_PrivateKey_buffer_TLS\n");
  334. }
  335. }
  336. # if defined(WOLFSSL_CHECK_SIG_FAULTS)
  337. if (ret == 0){
  338. ret = tsip_use_PublicKey_buffer(ssl,
  339. (const char*)g_key_block_data.encrypted_user_ecc256_public_key,
  340. sizeof(g_key_block_data.encrypted_user_ecc256_public_key),
  341. TSIP_ECCP256);
  342. if (ret != 0) {
  343. printf("ERROR tsip_use_PublicKey_buffer\n");
  344. }
  345. }
  346. #endif /* WOLFSSL_CHECK_SIG_FAULTS */
  347. #else
  348. /* Client authentication using RSA certificate can be handled by TSIP.
  349. * Note that the internal verification of the signature process requires
  350. * not only the client's private key but also its public key, so pass them
  351. * using tsip_use_PrivateKey_buffer_TLS and tsip_use_PublicKey_buffer_TLS
  352. * respectively.
  353. */
  354. if (ret == 0) {
  355. ret = tsip_use_PrivateKey_buffer_TLS(ssl,
  356. (const char*)g_key_block_data.encrypted_user_rsa2048_private_key,
  357. sizeof(g_key_block_data.encrypted_user_rsa2048_private_key),
  358. TSIP_RSA2048);
  359. if (ret != 0) {
  360. printf("ERROR tsip_use_PrivateKey_buffer_TLS :%d\n", ret);
  361. }
  362. }
  363. if (ret == 0) {
  364. ret = tsip_use_PublicKey_buffer_TLS(ssl,
  365. (const char*)g_key_block_data.encrypted_user_rsa2048_public_key,
  366. sizeof(g_key_block_data.encrypted_user_rsa2048_public_key),
  367. TSIP_RSA2048);
  368. if (ret != 0) {
  369. printf("ERROR tsip_use_PublicKey_buffer: %d\n", ret);
  370. }
  371. }
  372. #endif /* USE_ECC_CERT */
  373. #else
  374. #if defined(USE_ECC_CERT)
  375. if (ret == 0) {
  376. err = wolfSSL_use_PrivateKey_buffer(ssl,
  377. ecc_clikey_der_256,
  378. sizeof_ecc_clikey_der_256,
  379. WOLFSSL_FILETYPE_ASN1);
  380. if (err != SSL_SUCCESS) {
  381. printf("ERROR wolfSSL_use_PrivateKey_buffer: %d\n",
  382. wolfSSL_get_error(ssl, 0));
  383. ret = -1;
  384. }
  385. }
  386. #else
  387. if (ret == 0) {
  388. err = wolfSSL_use_PrivateKey_buffer(ssl, client_key_der_2048,
  389. sizeof_client_key_der_2048, WOLFSSL_FILETYPE_ASN1);
  390. if (err != SSL_SUCCESS) {
  391. printf("ERROR wolfSSL_use_PrivateKey_buffer: %d\n",
  392. wolfSSL_get_error(ssl, 0));
  393. ret = -1;
  394. }
  395. }
  396. #endif /* USE_ECC_CERT */
  397. #endif /* WOLFSSL_RENESAS_TSIP_TLS */
  398. #ifdef DEBUG_WOLFSSL
  399. wolfSSL_Debugging_ON();
  400. #endif
  401. if (ret == 0) {
  402. if (wolfSSL_connect(ssl) != WOLFSSL_SUCCESS) {
  403. msg(pcName, p->id, "ERROR wolfSSL_connect: %d\n",
  404. wolfSSL_get_error(ssl, 0));
  405. ret = -1;
  406. }
  407. }
  408. #ifdef DEBUG_WOLFSSL
  409. wolfSSL_Debugging_OFF();
  410. #endif
  411. if (ret == 0) {
  412. if (wolfSSL_write(ssl, sendBuff, strlen(sendBuff)) !=
  413. strlen(sendBuff)) {
  414. msg(pcName, p->id, "ERROR wolfSSL_write: %d\n",
  415. wolfSSL_get_error(ssl, 0));
  416. ret = -1;
  417. }
  418. }
  419. if (ret == 0) {
  420. if ((ret=wolfSSL_read(ssl, rcvBuff, BUFF_SIZE -1)) < 0) {
  421. msg(pcName, p->id, "ERROR wolfSSL_read: %d\n",
  422. wolfSSL_get_error(ssl, 0));
  423. ret = -1;
  424. }
  425. else {
  426. rcvBuff[ret] = '\0';
  427. msg(pcName, p->id, "Received: %s\n\n", rcvBuff);
  428. ret = 0;
  429. }
  430. }
  431. #if defined(TLS_MULTITHREAD_TEST)
  432. out:
  433. #endif
  434. if (ssl) {
  435. wolfSSL_shutdown(ssl);
  436. wolfSSL_free(ssl);
  437. ssl = NULL;
  438. /* reset call backs */
  439. #ifdef WOLFSSL_RENESAS_TSIP_TLS
  440. tsip_set_callbacks(client_ctx);
  441. #endif
  442. }
  443. if (socket) {
  444. FreeRTOS_shutdown(socket, FREERTOS_SHUT_RDWR);
  445. while (FreeRTOS_recv(socket, rcvBuff, BUFF_SIZE -1, 0) >=0) {
  446. vTaskDelay(250);
  447. }
  448. FreeRTOS_closesocket(socket);
  449. socket = NULL;
  450. }
  451. #ifdef TLS_MULTITHREAD_TEST
  452. xSemaphoreGive(exit_semaph);
  453. vTaskDelete(NULL);
  454. #endif
  455. return;
  456. }
  457. static void Tls_client_demo(void)
  458. {
  459. /* setup ciphersuite list to use for TLS handshake */
  460. #if defined(WOLFSSL_RENESAS_TSIP_TLS)
  461. #ifdef USE_ECC_CERT
  462. const char* cipherlist[] = {
  463. #if defined(WOLFSSL_TLS13)
  464. "TLS13-AES128-GCM-SHA256",
  465. "TLS13-AES128-CCM-SHA256",
  466. #endif
  467. "ECDHE-ECDSA-AES128-SHA256",
  468. "ECDHE-ECDSA-AES128-GCM-SHA256",
  469. };
  470. #if defined(WOLFSSL_TLS13)
  471. #define cipherlist_sz 2
  472. #else
  473. #define cipherlist_sz 2
  474. #endif
  475. TestInfo info[cipherlist_sz];
  476. #else
  477. const char* cipherlist[] = {
  478. #if defined(WOLFSSL_TLS13)
  479. "TLS13-AES128-GCM-SHA256",
  480. "TLS13-AES128-CCM-SHA256",
  481. #endif
  482. "ECDHE-RSA-AES128-GCM-SHA256",
  483. "ECDHE-RSA-AES128-SHA256",
  484. "AES128-SHA",
  485. "AES128-SHA256",
  486. "AES256-SHA",
  487. "AES256-SHA256"
  488. };
  489. #if defined(WOLFSSL_TLS13)
  490. #define cipherlist_sz 2
  491. #else
  492. #define cipherlist_sz 6
  493. #endif
  494. TestInfo info[cipherlist_sz];
  495. #endif
  496. #else
  497. const char* cipherlist[] = { NULL };
  498. #define cipherlist_sz 1
  499. TestInfo info[cipherlist_sz];
  500. #endif
  501. int i = 0;
  502. #ifdef TLS_MULTITHREAD_TEST
  503. int j = 0;
  504. BaseType_t xReturned;
  505. BaseType_t xHigherPriorityTaskWoken;
  506. xHigherPriorityTaskWoken = pdFALSE;
  507. Mutex = xSemaphoreCreateMutex();
  508. #endif
  509. printf("/*------------------------------------------------*/\n");
  510. printf(" TLS_Client demo\n");
  511. printf(" - TLS server address:" TLSSERVER_IP " port: %d\n",
  512. TLSSERVER_PORT);
  513. #if defined(WOLFSSL_RENESAS_TSIP_TLS) && (WOLFSSL_RENESAS_TSIP_VER >=109)
  514. printf(" - with TSIP\n");
  515. #endif
  516. printf("/*------------------------------------------------*/\n");
  517. /* setup credentials for TLS handshake */
  518. #if defined(WOLFSSL_RENESAS_TSIP_TLS) && (WOLFSSL_RENESAS_TSIP_VER >=109)
  519. #if defined(USE_ECC_CERT)
  520. /* Root CA cert has ECC-P256 public key */
  521. tsip_inform_cert_sign((const byte*)ca_ecc_cert_der_sig);
  522. #else
  523. /* Root CA cert has RSA public key */
  524. tsip_inform_cert_sign((const byte*)ca_cert_der_sig);
  525. #endif
  526. wc_tsip_inform_user_keys_ex(
  527. (byte*)&g_key_block_data.encrypted_provisioning_key,
  528. (byte*)&g_key_block_data.iv,
  529. (byte*)&g_key_block_data.encrypted_user_rsa2048_ne_key,
  530. encrypted_user_key_type);
  531. #endif /* WOLFSSL_RENESAS_TSIP_TLS && (WOLFSSL_RENESAS_TSIP_VER >=109) */
  532. Tls_client_init();
  533. #ifdef TLS_MULTITHREAD_TEST
  534. exit_semaph = xSemaphoreCreateCounting(cipherlist_sz, 0);
  535. #ifdef DEBUG_WOLFSSL
  536. wolfSSL_SetLoggingCb(my_Logging_cb);
  537. #endif
  538. do {
  539. for (j = i; j < (i+2); j++) {
  540. info[j].id = j;
  541. info[j].port = TLSSERVER_PORT + (j%2);
  542. info[j].cipher = cipherlist[j];
  543. info[j].ctx = client_ctx;
  544. info[j].xBinarySemaphore = xSemaphoreCreateBinary();
  545. info[j].log_f = my_Logging_cb;
  546. memset(info[j].name, 0, sizeof(info[j].name));
  547. sprintf(info[j].name, "clt_thd_%s", ((j%2) == 0) ?
  548. "taskA" : "taskB");
  549. printf(" %s connecting to %d port\n", info[j].name, info[j].port);
  550. xReturned = xTaskCreate(Tls_client, info[j].name,
  551. THREAD_STACK_SIZE, &info[j], 3, NULL);
  552. if (xReturned != pdPASS) {
  553. printf("Failed to create task\n");
  554. }
  555. }
  556. for (j = i; j < (i+2); j++) {
  557. xSemaphoreGiveFromISR(info[j].xBinarySemaphore,
  558. &xHigherPriorityTaskWoken);
  559. }
  560. /* check if all tasks are completed */
  561. for (j = i; j < (i+2); j++) {
  562. if(!xSemaphoreTake(exit_semaph, portMAX_DELAY)) {
  563. printf("a semaphore was not given by a test task.");
  564. }
  565. }
  566. i += 2;
  567. } while (i < cipherlist_sz);
  568. vSemaphoreDelete(exit_semaph);
  569. vSemaphoreDelete(Mutex);
  570. #else
  571. do {
  572. info[i].port = TLSSERVER_PORT;
  573. info[i].cipher = cipherlist[i];
  574. info[i].ctx = client_ctx;
  575. info[i].id = i;
  576. memset(info[i].name, 0, sizeof(info[i].name));
  577. sprintf(info[i].name, "wolfSSL_TLS_client_do(%02d)", i);
  578. Tls_client(&info[i]);
  579. i++;
  580. } while (i < cipherlist_sz);
  581. if (client_ctx) {
  582. wolfSSL_CTX_free(client_ctx);
  583. }
  584. #endif
  585. wolfSSL_Cleanup();
  586. printf("End of TLS_Client demo.\n");
  587. }
  588. #endif /* TLS_CLIENT */
  589. /* Demo entry function called by iot_demo_runner
  590. * To run this entry function as an aws_iot_demo, define this as
  591. * DEMO_entryFUNCTION in aws_demo_config.h.
  592. */
  593. void wolfSSL_demo_task(bool awsIotMqttMode,
  594. const char* pIdentifier,
  595. void* pNetworkServerInfo,
  596. void* pNetworkCredentialInfo,
  597. const IotNetworkInterface_t* pNetworkInterface)
  598. {
  599. (void)awsIotMqttMode;
  600. (void)pIdentifier;
  601. (void)pNetworkServerInfo;
  602. (void)pNetworkCredentialInfo;
  603. (void)pNetworkInterface;
  604. #if defined(CRYPT_TEST)
  605. CryptTest_demo();
  606. #elif defined(BENCHMARK)
  607. Benchmark_demo();
  608. #elif defined(TSIP_CRYPT_UNIT_TEST)
  609. int ret = 0;
  610. if ((ret = wolfCrypt_Init()) != 0) {
  611. printf("wolfCrypt_Init failed %d\n", ret);
  612. }
  613. printf("Start wolf tsip crypt Test\n");
  614. printf(" \n");
  615. printf(" simple crypt test by using TSIP\n");
  616. tsip_crypt_test();
  617. printf(" \n");
  618. printf(" multi sha thread test\n");
  619. tsip_crypt_sha_multitest();
  620. printf(" \n");
  621. printf(" multi aes cbc thread test\n");
  622. tsip_crypt_AesCbc_multitest();
  623. printf(" \n");
  624. printf(" multi aes gcm thread test\n");
  625. tsip_crypt_AesGcm_multitest();
  626. printf(" \n");
  627. printf(" multi sha aescbc aesgcm thread test\n");
  628. tsip_crypt_Sha_AesCbcGcm_multitest();
  629. printf(" \n");
  630. printf("End wolf tsip crypt Test\n");
  631. if ((ret = wolfCrypt_Cleanup()) != 0) {
  632. printf("wolfCrypt_Cleanup failed %d\n", ret);
  633. }
  634. #elif defined(TLS_CLIENT)
  635. Tls_client_demo();
  636. #endif
  637. while (1) {
  638. vTaskDelay(10000);
  639. }
  640. }