wolf_client.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394
  1. /* wolf_client.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 "r_t4_itcpip.h"
  24. #include "wolfssl/wolfcrypt/settings.h"
  25. #include "wolfssl/ssl.h"
  26. #include "wolfssl/certs_test.h"
  27. #include "key_data.h"
  28. #include "wolfssl_demo.h"
  29. #if defined(WOLFSSL_RENESAS_TSIP_TLS)
  30. #include <wolfssl/wolfcrypt/port/Renesas/renesas-tsip-crypt.h>
  31. #endif
  32. #define SIMPLE_TLSSEVER_IP "192.168.1.12"
  33. #define SIMPLE_TLSSERVER_PORT "11111"
  34. ER t4_tcp_callback(ID cepid, FN fncd , VP p_parblk);
  35. static WOLFSSL_CTX *client_ctx;
  36. #if defined(WOLFSSL_RENESAS_TSIP_TLS)
  37. uint32_t g_encrypted_root_public_key[140];
  38. static TsipUserCtx userContext;
  39. #endif
  40. #if defined(TLS_CLIENT)
  41. #if defined(WOLFSSL_RENESAS_TSIP_TLS) && defined(WOLFSSL_STATIC_MEMORY)
  42. extern WOLFSSL_HEAP_HINT* heapHint;
  43. #define BUFFSIZE_IO (16 * 1024)
  44. unsigned char heapBufIO[BUFFSIZE_IO];
  45. #endif /* WOLFSSL_RENESAS_TSIP_TLS && WOLFSSL_STATIC_MEMORY */
  46. #endif /* TLS_CLIENT */
  47. static int my_IORecv(WOLFSSL* ssl, char* buff, int sz, void* ctx)
  48. {
  49. int ret;
  50. ID cepid;
  51. if(ctx != NULL)
  52. cepid = *(ID *)ctx;
  53. else
  54. return WOLFSSL_CBIO_ERR_GENERAL;
  55. ret = tcp_rcv_dat(cepid, buff, sz, TMO_FEVR);
  56. if(ret > 0)
  57. return ret;
  58. else
  59. return WOLFSSL_CBIO_ERR_GENERAL;
  60. }
  61. static int my_IOSend(WOLFSSL* ssl, char* buff, int sz, void* ctx)
  62. {
  63. int ret;
  64. ID cepid;
  65. if(ctx != NULL)
  66. cepid = *(ID *)ctx;
  67. else
  68. return WOLFSSL_CBIO_ERR_GENERAL;
  69. ret = tcp_snd_dat(cepid, buff, sz, TMO_FEVR);
  70. if(ret == sz)
  71. return ret;
  72. else
  73. return WOLFSSL_CBIO_ERR_GENERAL;
  74. }
  75. static int getIPaddr(char *arg)
  76. {
  77. int a1, a2, a3, a4;
  78. if(sscanf(arg, "%d.%d.%d.%d", &a1, &a2, &a3, &a4) == 4)
  79. return (a1 << 24) | (a2 << 16) | (a3 << 8) | a4;
  80. else return 0;
  81. }
  82. static int getPort(char *arg)
  83. {
  84. int port;
  85. if(sscanf(arg, "%d", &port) == 1)
  86. return port;
  87. else return 0;
  88. }
  89. void wolfSSL_TLS_client_init(const char* cipherlist)
  90. {
  91. #ifndef NO_FILESYSTEM
  92. #ifdef USE_ECC_CERT
  93. char *cert = "./certs/ca-ecc-cert.pem";
  94. #else
  95. char *cert = "./certs/ca-cert.pem";
  96. #endif
  97. #else
  98. #if defined(USE_ECC_CERT) && defined(USE_CERT_BUFFERS_256)
  99. const unsigned char *cert = ca_ecc_cert_der_256;
  100. #define SIZEOF_CERT sizeof_ca_ecc_cert_der_256
  101. #else
  102. const unsigned char *cert = ca_cert_der_2048;
  103. #define SIZEOF_CERT sizeof_ca_cert_der_2048
  104. #endif
  105. #endif
  106. wolfSSL_Init();
  107. #ifdef DEBUG_WOLFSSL
  108. wolfSSL_Debugging_ON();
  109. #endif
  110. /*---------------------------------------------*/
  111. /* Allocate WOLFSSL_CTX */
  112. /*---------------------------------------------*/
  113. #if defined(WOLFSSL_STATIC_MEMORY)
  114. if ((client_ctx = wolfSSL_CTX_new_ex(wolfSSLv23_client_method_ex(heapHint),
  115. heapHint)) == NULL) {
  116. printf("ERROR: failed to create WOLFSSL_CTX\n");
  117. return;
  118. }
  119. if ((wolfSSL_CTX_load_static_memory(&client_ctx, NULL, heapBufIO,
  120. sizeof(heapBufIO), WOLFMEM_IO_POOL, 10)) != WOLFSSL_SUCCESS) {
  121. printf("ERROR: failed to set static memory for IO\n");
  122. return;
  123. }
  124. #else
  125. /* Create and initialize WOLFSSL_CTX */
  126. if ((client_ctx =
  127. wolfSSL_CTX_new(wolfSSLv23_client_method_ex((void *)NULL))) == NULL) {
  128. printf("ERROR: failed to create WOLFSSL_CTX\n");
  129. return;
  130. }
  131. #endif /* WOLFSSL_STATIC_MEMORY */
  132. /*---------------------------------------------*/
  133. /* Set up TSIP callbacks */
  134. /*---------------------------------------------*/
  135. #ifdef WOLFSSL_RENESAS_TSIP_TLS
  136. tsip_set_callbacks(client_ctx);
  137. #endif
  138. /*---------------------------------------------*/
  139. /* Root CA certificate */
  140. /*---------------------------------------------*/
  141. if (wolfSSL_CTX_load_verify_buffer(client_ctx, cert, SIZEOF_CERT,
  142. SSL_FILETYPE_ASN1) != WOLFSSL_SUCCESS){
  143. printf("ERROR: can't load certificate data\n");
  144. return;
  145. }
  146. /*---------------------------------------------*/
  147. /* Set up IO callbacks */
  148. /*---------------------------------------------*/
  149. wolfSSL_SetIORecv(client_ctx, my_IORecv);
  150. wolfSSL_SetIOSend(client_ctx, my_IOSend);
  151. /*---------------------------------------------*/
  152. /* Set up cipher suites */
  153. /*---------------------------------------------*/
  154. /* use specific cipher */
  155. if (cipherlist != NULL &&
  156. wolfSSL_CTX_set_cipher_list(client_ctx, cipherlist) != WOLFSSL_SUCCESS) {
  157. wolfSSL_CTX_free(client_ctx); client_ctx = NULL;
  158. printf("client can't set cipher list");
  159. return;
  160. }
  161. #if defined(WOLFSSL_TLS13)
  162. if (wolfSSL_CTX_UseSupportedCurve(client_ctx, WOLFSSL_ECC_SECP256R1)
  163. != WOLFSSL_SUCCESS) {
  164. wolfSSL_CTX_free(client_ctx); client_ctx = NULL;
  165. printf("client can't set use supported curves\n");
  166. return;
  167. }
  168. #endif
  169. }
  170. void wolfSSL_TLS_client( )
  171. {
  172. ID cepid = 1;
  173. ER ercd;
  174. int ret;
  175. WOLFSSL_CTX *ctx = (WOLFSSL_CTX *)client_ctx;
  176. WOLFSSL *ssl = NULL;
  177. #define BUFF_SIZE 256
  178. static const char sendBuff[]= "Hello Server\n" ;
  179. char rcvBuff[BUFF_SIZE] = {0};
  180. static T_IPV4EP my_addr = { 0, 0 };
  181. T_IPV4EP dst_addr;
  182. if((dst_addr.ipaddr = getIPaddr(SIMPLE_TLSSEVER_IP)) == 0){
  183. printf("ERROR: IP address\n");
  184. goto out;
  185. }
  186. if((dst_addr.portno = getPort(SIMPLE_TLSSERVER_PORT)) == 0){
  187. printf("ERROR: Port number\n");
  188. goto out;
  189. }
  190. if((ercd = tcp_con_cep(cepid, &my_addr, &dst_addr, TMO_FEVR)) != E_OK) {
  191. printf("ERROR TCP Connect: %d\n", ercd);
  192. goto out;
  193. }
  194. /*---------------------------------------------*/
  195. /* Allocate WOLFSSL object */
  196. /*---------------------------------------------*/
  197. if((ssl = wolfSSL_new(ctx)) == NULL) {
  198. printf("ERROR wolfSSL_new: %d\n", wolfSSL_get_error(ssl, 0));
  199. goto out;
  200. }
  201. /*---------------------------------------------*/
  202. /* Set up callback context for TSIP */
  203. /*---------------------------------------------*/
  204. #ifdef WOLFSSL_RENESAS_TSIP_TLS
  205. tsip_set_callback_ctx(ssl, &userContext);
  206. #endif
  207. /*---------------------------------------------*/
  208. /* Client Certificate */
  209. /*---------------------------------------------*/
  210. #ifdef USE_ECC_CERT
  211. /* ECDSA client certificate */
  212. if (wolfSSL_use_certificate_buffer(ssl, cliecc_cert_der_256,
  213. sizeof_cliecc_cert_der_256, WOLFSSL_FILETYPE_ASN1) != WOLFSSL_SUCCESS) {
  214. printf("ERROR wolfSSL_use_certificate_buffer: %d\n",
  215. wolfSSL_get_error(ssl, 0));
  216. goto out;
  217. }
  218. #else
  219. /* RSA client certificate */
  220. if (wolfSSL_use_certificate_buffer(ssl, client_cert_der_2048,
  221. sizeof_client_cert_der_2048, WOLFSSL_FILETYPE_ASN1) != WOLFSSL_SUCCESS) {
  222. printf("ERROR wolfSSL_use_certificate_buffer: %d\n",
  223. wolfSSL_get_error(ssl, 0));
  224. goto out;
  225. }
  226. #endif /* USE_ECC_CERT */
  227. /*---------------------------------------------*/
  228. /* Client Keys */
  229. /*---------------------------------------------*/
  230. #ifdef USE_ECC_CERT
  231. #ifdef WOLFSSL_RENESAS_TSIP_TLS
  232. /* TSIP specific ECC private key */
  233. if (tsip_use_PrivateKey_buffer_TLS(ssl,
  234. (const char*)g_key_block_data.encrypted_user_ecc256_private_key,
  235. sizeof(g_key_block_data.encrypted_user_ecc256_private_key),
  236. TSIP_ECCP256) != 0) {
  237. printf("ERROR tsip_use_PrivateKey_buffer_TLS\n");
  238. goto out;
  239. }
  240. #else
  241. /* DER format ECC private key */
  242. if (wolfSSL_use_PrivateKey_buffer(ssl,
  243. ecc_clikey_der_256,
  244. sizeof_ecc_clikey_der_256,
  245. WOLFSSL_FILETYPE_ASN1) != WOLFSSL_SUCCESS) {
  246. printf("ERROR wolfSSL_use_PrivateKey_buffer: %d\n",
  247. wolfSSL_get_error(ssl, 0));
  248. goto out;
  249. }
  250. #endif
  251. #else
  252. #if defined(WOLFSSL_RENESAS_TSIP_TLS)
  253. /* Note: TSIP asks client key pair for client authentication. */
  254. /* TSIP specific RSA private key */
  255. if (tsip_use_PrivateKey_buffer_TLS(ssl,
  256. (const char*)g_key_block_data.encrypted_user_rsa2048_private_key,
  257. sizeof(g_key_block_data.encrypted_user_rsa2048_private_key),
  258. TSIP_RSA2048) != 0) {
  259. printf("ERROR tsip_use_PrivateKey_buffer_TLS\n");
  260. goto out;
  261. }
  262. /* TSIP specific RSA public key */
  263. if (tsip_use_PublicKey_buffer_TLS(ssl,
  264. (const char*)g_key_block_data.encrypted_user_rsa2048_public_key,
  265. sizeof(g_key_block_data.encrypted_user_rsa2048_public_key),
  266. TSIP_RSA2048) != 0) {
  267. printf("ERROR tsip_use_PublicKey_buffer_TLS\n");
  268. goto out;
  269. }
  270. #else
  271. if (wolfSSL_use_PrivateKey_buffer(ssl, client_key_der_2048,
  272. sizeof_client_key_der_2048, WOLFSSL_FILETYPE_ASN1) != WOLFSSL_SUCCESS) {
  273. printf("ERROR wolfSSL_use_PrivateKey_buffer: %d\n",
  274. wolfSSL_get_error(ssl, 0));
  275. goto out;
  276. }
  277. #endif /* WOLFSSL_RENESAS_TSIP_TLS */
  278. #endif /* USE_ECC_CERT */
  279. /*---------------------------------------------*/
  280. /* Set up IO callback context */
  281. /*---------------------------------------------*/
  282. wolfSSL_SetIOReadCtx(ssl, (void *)&cepid);
  283. wolfSSL_SetIOWriteCtx(ssl, (void *)&cepid);
  284. /*---------------------------------------------*/
  285. /* TLS handshake */
  286. /*---------------------------------------------*/
  287. if(wolfSSL_connect(ssl) != WOLFSSL_SUCCESS) {
  288. printf("ERROR SSL connect: %d\n", wolfSSL_get_error(ssl, 0));
  289. goto out;
  290. }
  291. if (wolfSSL_write(ssl, sendBuff, strlen(sendBuff)) != strlen(sendBuff)) {
  292. printf("ERROR SSL write: %d\n", wolfSSL_get_error(ssl, 0));
  293. goto out;
  294. }
  295. if ((ret=wolfSSL_read(ssl, rcvBuff, BUFF_SIZE)) < 0) {
  296. printf("ERROR SSL read: %d\n", wolfSSL_get_error(ssl, 0));
  297. goto out;
  298. }
  299. rcvBuff[ret] = '\0' ;
  300. printf("Received: %s\n\n", rcvBuff);
  301. out:
  302. /* frees all data before client termination */
  303. if(ssl) {
  304. wolfSSL_shutdown(ssl);
  305. wolfSSL_free(ssl);
  306. }
  307. if(ctx) {
  308. wolfSSL_CTX_free(ctx);
  309. }
  310. wolfSSL_Cleanup();
  311. tcp_sht_cep(cepid);
  312. tcp_cls_cep(cepid, TMO_FEVR);
  313. return;
  314. }