wolfssl_demo.c 21 KB

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