wolf_client.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. /* wolf_client.c
  2. *
  3. * Copyright (C) 2006-2021 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 "wolf_demo.h"
  28. ER t4_tcp_callback(ID cepid, FN fncd , VP p_parblk);
  29. static int my_IORecv(WOLFSSL* ssl, char* buff, int sz, void* ctx)
  30. {
  31. int ret;
  32. ID cepid;
  33. if(ctx != NULL)cepid = *(ID *)ctx;
  34. else return WOLFSSL_CBIO_ERR_GENERAL;
  35. ret = tcp_rcv_dat(cepid, buff, sz, TMO_FEVR);
  36. if(ret > 0)return ret;
  37. else return WOLFSSL_CBIO_ERR_GENERAL;
  38. }
  39. static int my_IOSend(WOLFSSL* ssl, char* buff, int sz, void* ctx)
  40. {
  41. int ret;
  42. ID cepid;
  43. if(ctx != NULL)cepid = *(ID *)ctx;
  44. else return WOLFSSL_CBIO_ERR_GENERAL;
  45. ret = tcp_snd_dat(cepid, buff, sz, TMO_FEVR);
  46. if(ret == sz)return ret;
  47. else return WOLFSSL_CBIO_ERR_GENERAL;
  48. }
  49. static int getIPaddr(char *arg)
  50. {
  51. int a1, a2, a3, a4;
  52. if(sscanf(arg, "%d.%d.%d.%d", &a1, &a2, &a3, &a4) == 4)
  53. return (a1 << 24) | (a2 << 16) | (a3 << 8) | a4;
  54. else return 0;
  55. }
  56. static int getPort(char *arg)
  57. {
  58. int port;
  59. if(sscanf(arg, "%d", &port) == 1)
  60. return port;
  61. else return 0;
  62. }
  63. WOLFSSL_CTX *wolfSSL_TLS_client_init()
  64. {
  65. WOLFSSL_CTX* ctx;
  66. #ifndef NO_FILESYSTEM
  67. #ifdef USE_ECC_CERT
  68. char *cert = "./certs/ca-ecc-cert.pem";
  69. #else
  70. char *cert = "./certs/ca-cert.pem";
  71. #endif
  72. #else
  73. #ifdef USE_ECC_CERT
  74. const unsigned char *cert = ca_ecc_der_256;
  75. #define SIZEOF_CERT sizeof_ca_ecc_der_256
  76. #else
  77. const unsigned char *cert = ca_cert_der_2048;
  78. #define SIZEOF_CERT sizeof_ca_cert_der_2048
  79. #endif
  80. #endif
  81. wolfSSL_Init();
  82. #ifdef DEBUG_WOLFSSL
  83. wolfSSL_Debugging_ON();
  84. #endif
  85. /* Create and initialize WOLFSSL_CTX */
  86. if ((ctx = wolfSSL_CTX_new(wolfTLSv1_2_client_method_ex((void *)NULL))) == NULL) {
  87. printf("ERROR: failed to create WOLFSSL_CTX\n");
  88. return NULL;
  89. }
  90. #if !defined(NO_FILESYSTEM)
  91. if (wolfSSL_CTX_load_verify_locations(ctx, cert, 0) != SSL_SUCCESS) {
  92. printf("ERROR: can't load \"%s\"\n", cert);
  93. return NULL;
  94. }
  95. #else
  96. if (wolfSSL_CTX_load_verify_buffer(ctx, cert, SIZEOF_CERT, SSL_FILETYPE_ASN1) != SSL_SUCCESS){
  97. printf("ERROR: can't load certificate data\n");
  98. return NULL;
  99. }
  100. #endif
  101. /* Register callbacks */
  102. wolfSSL_SetIORecv(ctx, my_IORecv);
  103. wolfSSL_SetIOSend(ctx, my_IOSend);
  104. return (void *)ctx;
  105. }
  106. void wolfSSL_TLS_client(void *v_ctx, func_args *args)
  107. {
  108. ID cepid = 1;
  109. ER ercd;
  110. int ret;
  111. WOLFSSL_CTX *ctx = (WOLFSSL_CTX *)v_ctx;
  112. WOLFSSL *ssl;
  113. #define BUFF_SIZE 256
  114. static const char sendBuff[]= "Hello Server\n" ;
  115. char rcvBuff[BUFF_SIZE] = {0};
  116. static T_IPV4EP my_addr = { 0, 0 };
  117. T_IPV4EP dst_addr;
  118. if(args->argc >= 2){
  119. if((dst_addr.ipaddr = getIPaddr(args->argv[1])) == 0){
  120. printf("ERROR: IP address\n");
  121. return;
  122. }
  123. if((dst_addr.portno = getPort(args->argv[2])) == 0){
  124. printf("ERROR: IP address\n");
  125. return;
  126. }
  127. }
  128. if((ercd = tcp_con_cep(cepid, &my_addr, &dst_addr, TMO_FEVR)) != E_OK) {
  129. printf("ERROR TCP Connect: %d\n", ercd);
  130. return;
  131. }
  132. if((ssl = wolfSSL_new(ctx)) == NULL) {
  133. printf("ERROR wolfSSL_new: %d\n", wolfSSL_get_error(ssl, 0));
  134. return;
  135. }
  136. /* set callback context */
  137. wolfSSL_SetIOReadCtx(ssl, (void *)&cepid);
  138. wolfSSL_SetIOWriteCtx(ssl, (void *)&cepid);
  139. if(wolfSSL_connect(ssl) != SSL_SUCCESS) {
  140. printf("ERROR SSL connect: %d\n", wolfSSL_get_error(ssl, 0));
  141. return;
  142. }
  143. if (wolfSSL_write(ssl, sendBuff, strlen(sendBuff)) != strlen(sendBuff)) {
  144. printf("ERROR SSL write: %d\n", wolfSSL_get_error(ssl, 0));
  145. return;
  146. }
  147. if ((ret=wolfSSL_read(ssl, rcvBuff, BUFF_SIZE)) < 0) {
  148. printf("ERROR SSL read: %d\n", wolfSSL_get_error(ssl, 0));
  149. return;
  150. }
  151. rcvBuff[ret] = '\0' ;
  152. printf("Received: %s\n", rcvBuff);
  153. /* frees all data before client termination */
  154. wolfSSL_free(ssl);
  155. wolfSSL_CTX_free(ctx);
  156. wolfSSL_Cleanup();
  157. tcp_sht_cep(cepid);
  158. tcp_cls_cep(cepid, TMO_FEVR);
  159. return;
  160. }