tls_client.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. /* tls_client.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 <wolfssl/wolfcrypt/settings.h>
  22. #include <wolfssl/wolfcrypt/error-crypt.h>
  23. #if !defined(WOLFCRYPT_ONLY) && !defined(NO_WOLFSSL_CLIENT)
  24. #include <wolfssl/ssl.h>
  25. #include <wolfssl/wolfcrypt/logging.h>
  26. #include <stdio.h>
  27. #define MAXSZ 1024
  28. /*------------------------------------------------------------------------*/
  29. /* TLS CLIENT */
  30. /*------------------------------------------------------------------------*/
  31. static int CbIORecv(WOLFSSL *ssl, char *buf, int sz, void *ctx)
  32. {
  33. int ret = WOLFSSL_CBIO_ERR_GENERAL;
  34. (void)ssl;
  35. (void)ctx;
  36. /* TODO: Exchange data over your own transport */
  37. #warning TODO: Implement your own recv data transport
  38. #if 0
  39. ret = usart_read_buffer_wait(&cdc_uart_module, buf, sz);
  40. if (ret == STATUS_ERR_TIMEOUT)
  41. return WOLFSSL_CBIO_ERR_WANT_READ;
  42. return (ret == STATUS_OK) ? sz : WOLFSSL_CBIO_ERR_GENERAL;
  43. #else
  44. return ret;
  45. #endif
  46. }
  47. static int CbIOSend(WOLFSSL *ssl, char *buf, int sz, void *ctx)
  48. {
  49. int ret = WOLFSSL_CBIO_ERR_GENERAL;
  50. (void)ssl;
  51. (void)ctx;
  52. /* TODO: Exchange data over your own transport */
  53. #warning TODO: Implement your own send data transport
  54. #if 0
  55. ret = usart_write_buffer_wait(&cdc_uart_module, buf, sz);
  56. if (ret == STATUS_ERR_TIMEOUT)
  57. return WOLFSSL_CBIO_ERR_WANT_WRITE;
  58. return (ret == STATUS_OK) ? sz : WOLFSSL_CBIO_ERR_GENERAL;
  59. #else
  60. return ret;
  61. #endif
  62. }
  63. static int tls_client(void)
  64. {
  65. char msg[] = "Hello WolfSSL!\r\n";
  66. char reply[MAXSZ];
  67. int ret, msgSz, error;
  68. WOLFSSL* ssl = NULL;
  69. WOLFSSL_CTX* ctx = NULL;
  70. if ((ctx = wolfSSL_CTX_new(wolfTLSv1_2_client_method())) == NULL) {
  71. printf("CTXnew failed.\n");
  72. goto fail;
  73. }
  74. /*------------------------------------------------------------------------*/
  75. /* ECDHE-ECDSA */
  76. /*------------------------------------------------------------------------*/
  77. /*--------------------*/
  78. /* for peer auth use: */
  79. /*--------------------*/
  80. // wolfSSL_CTX_load_verify_buffer(ctx, rsa_key_der_1024,
  81. // sizeof_rsa_key_der_1024, SSL_FILETYPE_ASN1);
  82. // wolfSSL_CTX_load_verify_buffer(ctx, server_cert_der_1024,
  83. // sizeof_server_cert_der_1024, SSL_FILETYPE_ASN1);
  84. /*---------------------*/
  85. /* for no peer auth: */
  86. /*---------------------*/
  87. wolfSSL_CTX_set_verify(ctx, WOLFSSL_VERIFY_NONE, NULL);
  88. /*---------------------*/
  89. /* end peer auth option*/
  90. /*---------------------*/
  91. if ((ret = wolfSSL_CTX_set_cipher_list(ctx, "ECDHE-ECDSA-AES128-SHA256")) != WOLFSSL_SUCCESS) {
  92. wolfSSL_CTX_free(ctx);
  93. printf("CTXset_cipher_list failed, error: %d\n", ret);
  94. goto fail;
  95. }
  96. /*------------------------------------------------------------------------*/
  97. /* END CIPHER SUITE OPTIONS */
  98. /*------------------------------------------------------------------------*/
  99. wolfSSL_CTX_SetIORecv(ctx, CbIORecv);
  100. wolfSSL_CTX_SetIOSend(ctx, CbIOSend);
  101. if ((ssl = wolfSSL_new(ctx)) == NULL) {
  102. error = wolfSSL_get_error(ssl, 0);
  103. printf("wolfSSL_new failed %d\n", error);
  104. wolfSSL_CTX_free(ctx);
  105. return -1;
  106. }
  107. /* non blocking accept and connect */
  108. ret = WOLFSSL_FAILURE;
  109. while (ret != WOLFSSL_SUCCESS) {
  110. /* client connect */
  111. ret = wolfSSL_connect(ssl);
  112. error = wolfSSL_get_error(ssl, 0);
  113. if (ret != WOLFSSL_SUCCESS) {
  114. if (error != WOLFSSL_ERROR_WANT_READ && error != WOLFSSL_ERROR_WANT_WRITE) {
  115. /* Fail */
  116. printf("wolfSSL connect failed with return code %d\n", error);
  117. goto fail;
  118. }
  119. }
  120. /* Success */
  121. }
  122. /* read and write */
  123. while (1) {
  124. /* client send/read */
  125. msgSz = sizeof(msg);
  126. ret = wolfSSL_write(ssl, msg, msgSz);
  127. error = wolfSSL_get_error(ssl, 0);
  128. if (ret != msgSz) {
  129. if (error != WOLFSSL_ERROR_WANT_READ && error != WOLFSSL_ERROR_WANT_WRITE) {
  130. /* Write failed */
  131. goto fail;
  132. }
  133. }
  134. /* Write succeeded */
  135. break;
  136. }
  137. while (1) {
  138. ret = wolfSSL_read(ssl, reply, sizeof(reply) - 1);
  139. error = wolfSSL_get_error(ssl, 0);
  140. if (ret < 0) {
  141. if (error != WOLFSSL_ERROR_WANT_READ && error != WOLFSSL_ERROR_WANT_WRITE) {
  142. /* Can put print here, the server enters a loop waiting to read
  143. * a confirmation message at this point */
  144. // printf("client read failed\n");
  145. goto fail;
  146. }
  147. continue;
  148. }
  149. else {
  150. /* Can put print here, the server enters a loop waiting to read
  151. * a confirmation message at this point */
  152. reply[ret] = '\0';
  153. // printf("Client Received Reply: %s\n", reply);
  154. break;
  155. }
  156. }
  157. return 0;
  158. fail:
  159. wolfSSL_shutdown(ssl);
  160. wolfSSL_free(ssl);
  161. wolfSSL_CTX_free(ctx);
  162. return -1;
  163. }
  164. #endif /* !WOLFCRYPT_ONLY && !NO_WOLFSSL_CLIENT */
  165. int main(void)
  166. {
  167. int ret;
  168. #if !defined(WOLFCRYPT_ONLY) && !defined(NO_WOLFSSL_CLIENT)
  169. wolfSSL_Init();
  170. ret = tls_client();
  171. wolfSSL_Cleanup();
  172. #else
  173. ret = NOT_COMPILED_IN;
  174. #endif
  175. return ret;
  176. }