wolfssl_demo.c 15 KB

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