wolfssl_demo.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582
  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 "FreeRTOS.h"
  25. #include "FreeRTOS_IP.h"
  26. #include "FreeRTOS_Sockets.h"
  27. #include "platform/iot_network.h"
  28. #include "platform.h"
  29. #include <wolfssl/wolfcrypt/settings.h>
  30. #include "wolfssl/ssl.h"
  31. #include <wolfssl/wolfio.h>
  32. #include "wolfssl/certs_test.h"
  33. #include "wolfssl/wolfcrypt/types.h"
  34. #include "wolfssl_demo.h"
  35. #include <wolfcrypt/test/test.h>
  36. #include <wolfcrypt/benchmark/benchmark.h>
  37. #if defined(BENCHMARK)
  38. #include "r_cmt_rx_if.h"
  39. #endif
  40. #if defined(TLS_CLIENT)
  41. #if defined(WOLFSSL_RENESAS_TSIP_TLS)
  42. #include "key_data.h"
  43. #include <wolfssl/wolfcrypt/port/Renesas/renesas-tsip-crypt.h>
  44. extern const st_key_block_data_t g_key_block_data;
  45. user_PKCbInfo guser_PKCbInfo;
  46. uint32_t g_encrypted_root_public_key[140];
  47. static TsipUserCtx userContext;
  48. #endif /* WOLFSSL_RENESAS_TSIP_TLS */
  49. static WOLFSSL_CTX* client_ctx;
  50. #endif /* TLS_CLIENT */
  51. #define TLSSERVER_IP "192.168.10.6"
  52. #define TLSSERVER_PORT 11111
  53. #define FREQ 10000 /* Hz */
  54. static long tick;
  55. static int tmTick;
  56. #define YEAR ( \
  57. ((__DATE__)[7] - '0') * 1000 + \
  58. ((__DATE__)[8] - '0') * 100 + \
  59. ((__DATE__)[9] - '0') * 10 + \
  60. ((__DATE__)[10] - '0') * 1 \
  61. )
  62. #define MONTH ( \
  63. __DATE__[2] == 'n' ? (__DATE__[1] == 'a' ? 1 : 6) \
  64. : __DATE__[2] == 'b' ? 2 \
  65. : __DATE__[2] == 'r' ? (__DATE__[0] == 'M' ? 3 : 4) \
  66. : __DATE__[2] == 'y' ? 5 \
  67. : __DATE__[2] == 'l' ? 7 \
  68. : __DATE__[2] == 'g' ? 8 \
  69. : __DATE__[2] == 'p' ? 9 \
  70. : __DATE__[2] == 't' ? 10 \
  71. : __DATE__[2] == 'v' ? 11 \
  72. : 12 \
  73. )
  74. /* time
  75. * returns seconds from EPOCH
  76. */
  77. time_t time(time_t *t)
  78. {
  79. (void)t;
  80. return ((YEAR-1970)*365+30*MONTH)*24*60*60 + tmTick++;
  81. }
  82. /* timeTick
  83. * called periodically by H/W timer to increase tmTick.
  84. */
  85. #if defined(BENCHMARK)
  86. static void timeTick(void* pdata)
  87. {
  88. (void)pdata;
  89. tick++;
  90. }
  91. #endif
  92. double current_time(int reset)
  93. {
  94. if(reset) tick = 0 ;
  95. return ((double)tick/FREQ) ;
  96. }
  97. /* --------------------------------------------------------*/
  98. /* Benchmark_demo */
  99. /* --------------------------------------------------------*/
  100. #if defined(BENCHMARK)
  101. static void Benchmark_demo(void)
  102. {
  103. uint32_t channel;
  104. R_CMT_CreatePeriodic(FREQ, &timeTick, &channel);
  105. printf("Start wolfCrypt Benchmark\n");
  106. benchmark_test(NULL);
  107. printf("End wolfCrypt Benchmark\n");
  108. }
  109. #endif /* BENCHMARK */
  110. /* --------------------------------------------------------*/
  111. /* CryptTest_demo */
  112. /* --------------------------------------------------------*/
  113. #if defined(CRYPT_TEST)
  114. static void CryptTest_demo(void)
  115. {
  116. int ret;
  117. if ((ret = wolfCrypt_Init()) != 0) {
  118. printf("wolfCrypt_Init failed %d\n", ret);
  119. }
  120. printf("Start wolfCrypt Test\n");
  121. wolfcrypt_test(NULL);
  122. printf("End wolfCrypt Test\n");
  123. if ((ret = wolfCrypt_Cleanup()) != 0) {
  124. printf("wolfCrypt_Cleanup failed %d\n", ret);
  125. }
  126. }
  127. #endif /* CRYPT_TEST */
  128. /* --------------------------------------------------------*/
  129. /* Tls_client_demo */
  130. /* --------------------------------------------------------*/
  131. #if defined(TLS_CLIENT)
  132. static void Tls_client_init(const char* cipherlist)
  133. {
  134. #ifndef NO_FILESYSTEM
  135. #ifdef USE_ECC_CERT
  136. char *cert = "./certs/ca-ecc-cert.pem";
  137. #else
  138. char *cert = "./certs/ca-cert.pem";
  139. #endif
  140. #else
  141. #if defined(USE_ECC_CERT) && defined(USE_CERT_BUFFERS_256)
  142. const unsigned char *cert = ca_ecc_cert_der_256;
  143. #define SIZEOF_CERT sizeof_ca_ecc_cert_der_256
  144. #else
  145. const unsigned char *cert = ca_cert_der_2048;
  146. #define SIZEOF_CERT sizeof_ca_cert_der_2048
  147. #endif
  148. #endif
  149. client_ctx = NULL;
  150. wolfSSL_Init();
  151. #ifdef DEBUG_WOLFSSL
  152. wolfSSL_Debugging_ON();
  153. #endif
  154. /* Create and initialize WOLFSSL_CTX */
  155. if ((client_ctx =
  156. wolfSSL_CTX_new(wolfSSLv23_client_method_ex((void *)NULL))) == NULL) {
  157. printf("ERROR: failed to create WOLFSSL_CTX\n");
  158. return;
  159. }
  160. #ifdef WOLFSSL_RENESAS_TSIP_TLS
  161. tsip_set_callbacks(client_ctx);
  162. #endif
  163. #if defined(NO_FILESYSTEM)
  164. if (wolfSSL_CTX_load_verify_buffer(client_ctx, cert,
  165. SIZEOF_CERT, SSL_FILETYPE_ASN1) != SSL_SUCCESS) {
  166. printf("ERROR: can't load certificate data\n");
  167. return;
  168. }
  169. #else
  170. if (wolfSSL_CTX_load_verify_locations(client_ctx, cert, 0) != SSL_SUCCESS) {
  171. printf("ERROR: can't load \"%s\"\n", cert);
  172. return NULL;
  173. }
  174. #endif
  175. /* use specific cipher */
  176. if (cipherlist != NULL &&
  177. wolfSSL_CTX_set_cipher_list(client_ctx, cipherlist) !=
  178. WOLFSSL_SUCCESS) {
  179. wolfSSL_CTX_free(client_ctx); client_ctx = NULL;
  180. printf("client can't set cipher list");
  181. }
  182. #if defined(WOLFSSL_TLS13) && defined(WOLFSSL_RENESAS_TSIP_TLS)
  183. if (wolfSSL_CTX_UseSupportedCurve(client_ctx, WOLFSSL_ECC_SECP256R1)
  184. != WOLFSSL_SUCCESS) {
  185. wolfSSL_CTX_free(client_ctx); client_ctx = NULL;
  186. printf("client can't set use supported curves\n");
  187. return;
  188. }
  189. #endif
  190. }
  191. static void Tls_client()
  192. {
  193. #define BUFF_SIZE 256
  194. #define ADDR_SIZE 16
  195. int ret = 0;
  196. int err;
  197. WOLFSSL_CTX* ctx = (WOLFSSL_CTX *)client_ctx;
  198. WOLFSSL* ssl;
  199. Socket_t socket;
  200. socklen_t socksize = sizeof(struct freertos_sockaddr);
  201. struct freertos_sockaddr PeerAddr;
  202. char addrBuff[ADDR_SIZE] = {0};
  203. static const char sendBuff[]= "Hello Server\n" ;
  204. char rcvBuff[BUFF_SIZE] = {0};
  205. /* create TCP socket */
  206. socket = FreeRTOS_socket(FREERTOS_AF_INET,
  207. FREERTOS_SOCK_STREAM,
  208. FREERTOS_IPPROTO_TCP);
  209. configASSERT(socket != FREERTOS_INVALID_SOCKET);
  210. FreeRTOS_bind(socket, NULL, socksize);
  211. /* attempt to connect TLS server */
  212. PeerAddr.sin_addr = FreeRTOS_inet_addr(TLSSERVER_IP);
  213. PeerAddr.sin_port = FreeRTOS_htons(TLSSERVER_PORT);
  214. ret = FreeRTOS_connect(socket, &PeerAddr, sizeof(PeerAddr));
  215. if (ret != 0) {
  216. printf("ERROR FreeRTOS_connect: %d\n",ret);
  217. }
  218. if (ret == 0) {
  219. ssl = wolfSSL_new(ctx);
  220. if (ssl == NULL) {
  221. printf("ERROR wolfSSL_new: %d\n", wolfSSL_get_error(ssl, 0));
  222. ret = -1;
  223. }
  224. }
  225. if (ret == 0) {
  226. #ifdef WOLFSSL_RENESAS_TSIP_TLS
  227. tsip_set_callback_ctx(ssl, &userContext);
  228. #endif
  229. }
  230. #ifdef USE_ECC_CERT
  231. if (ret == 0) {
  232. err = wolfSSL_use_certificate_buffer(ssl,
  233. cliecc_cert_der_256,
  234. sizeof_cliecc_cert_der_256,
  235. WOLFSSL_FILETYPE_ASN1);
  236. if(err != SSL_SUCCESS) {
  237. printf("ERROR: can't load client-certificate\n");
  238. ret = -1;
  239. }
  240. }
  241. #else
  242. if (ret == 0) {
  243. err = wolfSSL_use_certificate_buffer(ssl,
  244. client_cert_der_2048,
  245. sizeof_client_cert_der_2048,
  246. WOLFSSL_FILETYPE_ASN1);
  247. if (err != SSL_SUCCESS) {
  248. printf("ERROR: can't load client-certificate\n");
  249. ret = -1;
  250. }
  251. }
  252. #endif /* USE_ECC_CERT */
  253. #ifdef USE_ECC_CERT
  254. #ifdef WOLFSSL_RENESAS_TSIP_TLS
  255. /* TSIP specific ECC private key */
  256. if (ret == 0){
  257. ret = tsip_use_PrivateKey_buffer_TLS(ssl,
  258. (const char*)g_key_block_data.encrypted_user_ecc256_private_key,
  259. sizeof(g_key_block_data.encrypted_user_ecc256_private_key),
  260. TSIP_ECCP256);
  261. if (ret != 0) {
  262. printf("ERROR tsip_use_PrivateKey_buffer_TLS\n");
  263. }
  264. }
  265. # if defined(WOLFSSL_CHECK_SIG_FAULTS)
  266. if (ret == 0){
  267. ret = tsip_use_PublicKey_buffer_TLS(ssl,
  268. (const char*)g_key_block_data.encrypted_user_ecc256_public_key,
  269. sizeof(g_key_block_data.encrypted_user_ecc256_public_key),
  270. TSIP_ECCP256);
  271. if (ret != 0) {
  272. printf("ERROR tsip_use_PublicKey_buffer_TLS\n");
  273. }
  274. }
  275. #endif /* WOLFSSL_CHECK_SIG_FAULTS */
  276. #else
  277. /* DER format ECC private key */
  278. if (ret == 0) {
  279. err = wolfSSL_use_PrivateKey_buffer(ssl,
  280. ecc_clikey_der_256,
  281. sizeof_ecc_clikey_der_256,
  282. WOLFSSL_FILETYPE_ASN1);
  283. if (err != SSL_SUCCESS) {
  284. printf("ERROR wolfSSL_use_PrivateKey_buffer: %d\n",
  285. wolfSSL_get_error(ssl, 0));
  286. ret = -1;
  287. }
  288. }
  289. #endif /* WOLFSSL_RENESAS_TSIP_TLS */
  290. #else
  291. #if defined(WOLFSSL_RENESAS_TSIP_TLS)
  292. /* Note: TSIP asks RSA client key pair for client authentication. */
  293. /* TSIP specific RSA private key */
  294. if (ret == 0) {
  295. ret = tsip_use_PrivateKey_buffer_TLS(ssl,
  296. (const char*)g_key_block_data.encrypted_user_rsa2048_private_key,
  297. sizeof(g_key_block_data.encrypted_user_rsa2048_private_key),
  298. TSIP_RSA2048);
  299. if (ret != 0) {
  300. printf("ERROR tsip_use_PrivateKey_buffer_TLS :%d\n", ret);
  301. }
  302. }
  303. if (ret == 0) {
  304. ret = tsip_use_PublicKey_buffer_TLS(ssl,
  305. (const char*)g_key_block_data.encrypted_user_rsa2048_public_key,
  306. sizeof(g_key_block_data.encrypted_user_rsa2048_public_key),
  307. TSIP_RSA2048);
  308. if (ret != 0) {
  309. printf("ERROR tsip_use_PublicKey_buffer_TLS: %d\n", ret);
  310. }
  311. }
  312. #else
  313. if (ret == 0) {
  314. err = wolfSSL_use_PrivateKey_buffer(ssl, client_key_der_2048,
  315. sizeof_client_key_der_2048, WOLFSSL_FILETYPE_ASN1);
  316. if (err != SSL_SUCCESS) {
  317. printf("ERROR wolfSSL_use_PrivateKey_buffer: %d\n",
  318. wolfSSL_get_error(ssl, 0));
  319. ret = -1;
  320. }
  321. }
  322. #endif /* WOLFSSL_RENESAS_TSIP_TLS */
  323. #endif /* USE_ECC_CERT */
  324. if (ret == 0) {
  325. /* associate socket with ssl object */
  326. if (wolfSSL_set_fd(ssl, (int)socket) != WOLFSSL_SUCCESS) {
  327. printf("ERROR wolfSSL_set_fd: %d\n", wolfSSL_get_error(ssl, 0));
  328. ret = -1;
  329. }
  330. }
  331. if (ret == 0) {
  332. if (wolfSSL_connect(ssl) != WOLFSSL_SUCCESS) {
  333. printf("ERROR wolfSSL_connect: %d\n", wolfSSL_get_error(ssl, 0));
  334. ret = -1;
  335. }
  336. }
  337. if (ret == 0) {
  338. if (wolfSSL_write(ssl, sendBuff, strlen(sendBuff)) !=
  339. strlen(sendBuff)) {
  340. printf("ERROR wolfSSL_write: %d\n", wolfSSL_get_error(ssl, 0));
  341. ret = -1;
  342. }
  343. }
  344. if (ret == 0) {
  345. if ((ret=wolfSSL_read(ssl, rcvBuff, BUFF_SIZE -1)) < 0) {
  346. printf("ERROR wolfSSL_read: %d\n", wolfSSL_get_error(ssl, 0));
  347. ret = -1;
  348. }
  349. else {
  350. rcvBuff[ret] = '\0';
  351. printf("Received: %s\n\n", rcvBuff);
  352. ret = 0;
  353. }
  354. }
  355. wolfSSL_shutdown(ssl);
  356. FreeRTOS_shutdown(socket, FREERTOS_SHUT_RDWR);
  357. while(FreeRTOS_recv(socket, rcvBuff, BUFF_SIZE -1, 0) >=0) {
  358. vTaskDelay(250);
  359. }
  360. FreeRTOS_closesocket(socket);
  361. wolfSSL_free(ssl);
  362. wolfSSL_CTX_free(ctx);
  363. wolfSSL_Cleanup();
  364. return;
  365. }
  366. static void Tls_client_demo(void)
  367. {
  368. /* setup ciphersuite list to use for TLS handshake */
  369. #if defined(WOLFSSL_RENESAS_TSIP_TLS)
  370. #ifdef USE_ECC_CERT
  371. const char* cipherlist[] = {
  372. #if defined(WOLFSSL_TLS13)
  373. "TLS13-AES128-GCM-SHA256",
  374. "TLS13-AES128-CCM-SHA256",
  375. #endif
  376. "ECDHE-ECDSA-AES128-GCM-SHA256",
  377. "ECDHE-ECDSA-AES128-SHA256"
  378. };
  379. int cipherlist_sz;
  380. #if defined(WOLFSSL_TLS13)
  381. cipherlist_sz = 2;
  382. #else
  383. cipherlist_sz = 2;
  384. #endif
  385. #else
  386. const char* cipherlist[] = {
  387. #if defined(WOLFSSL_TLS13)
  388. "TLS13-AES128-GCM-SHA256",
  389. "TLS13-AES128-CCM-SHA256",
  390. #endif
  391. "ECDHE-RSA-AES128-GCM-SHA256",
  392. "ECDHE-RSA-AES128-SHA256",
  393. "AES128-SHA",
  394. "AES128-SHA256",
  395. "AES256-SHA",
  396. "AES256-SHA256"
  397. };
  398. int cipherlist_sz;
  399. #if defined(WOLFSSL_TLS13)
  400. cipherlist_sz = 2;
  401. #else
  402. cipherlist_sz = 6;
  403. #endif
  404. #endif
  405. #else
  406. const char* cipherlist[] = { NULL };
  407. const int cipherlist_sz = 0;
  408. #endif
  409. int i = 0;
  410. printf("/*------------------------------------------------*/\n");
  411. printf(" TLS_Client demo\n");
  412. printf(" - TLS server address:" TLSSERVER_IP " port: %d\n",
  413. TLSSERVER_PORT);
  414. #if defined(WOLFSSL_RENESAS_TSIP_TLS) && (WOLFSSL_RENESAS_TSIP_VER >=109)
  415. printf(" - with TSIP\n");
  416. #endif
  417. printf("/*------------------------------------------------*/\n");
  418. /* setup credentials for TLS handshake */
  419. #if defined(WOLFSSL_RENESAS_TSIP_TLS) && (WOLFSSL_RENESAS_TSIP_VER >=109)
  420. #if defined(USE_ECC_CERT)
  421. /* Root CA cert has ECC-P256 public key */
  422. tsip_inform_cert_sign((const byte*)ca_ecc_cert_der_sig);
  423. #else
  424. /* Root CA cert has RSA public key */
  425. tsip_inform_cert_sign((const byte*)ca_cert_der_sig);
  426. #endif
  427. wc_tsip_inform_user_keys_ex(
  428. (byte*)&g_key_block_data.encrypted_provisioning_key,
  429. (byte*)&g_key_block_data.iv,
  430. (byte*)&g_key_block_data.encrypted_user_rsa2048_ne_key,
  431. encrypted_user_key_type);
  432. #endif /* WOLFSSL_RENESAS_TSIP_TLS && (WOLFSSL_RENESAS_TSIP_VER >=109) */
  433. do {
  434. if(cipherlist_sz > 0 ) printf("cipher : %s\n", cipherlist[i]);
  435. Tls_client_init(cipherlist[i]);
  436. Tls_client();
  437. i++;
  438. } while (i < cipherlist_sz);
  439. printf("End of TLS_Client demo.\n");
  440. }
  441. #endif /* TLS_CLIENT */
  442. /* Demo entry function called by iot_demo_runner
  443. * To run this entry function as an aws_iot_demo, define this as
  444. * DEMO_entryFUNCTION in aws_demo_config.h.
  445. */
  446. void wolfSSL_demo_task(bool awsIotMqttMode,
  447. const char* pIdentifier,
  448. void* pNetworkServerInfo,
  449. void* pNetworkCredentialInfo,
  450. const IotNetworkInterface_t* pNetworkInterface)
  451. {
  452. (void)awsIotMqttMode;
  453. (void)pIdentifier;
  454. (void)pNetworkServerInfo;
  455. (void)pNetworkCredentialInfo;
  456. (void)pNetworkInterface;
  457. #if defined(CRYPT_TEST)
  458. CryptTest_demo();
  459. #elif defined(BENCHMARK)
  460. Benchmark_demo();
  461. #elif defined(TLS_CLIENT)
  462. Tls_client_demo();
  463. #endif
  464. while (1) {
  465. vTaskDelay(10000);
  466. }
  467. }