async_client.c 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  1. /* async_client.c
  2. *
  3. * Copyright (C) 2006-2023 wolfSSL Inc.
  4. *
  5. * This file is part of wolfSSL. (formerly known as CyaSSL)
  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-1301, USA
  20. */
  21. /* TLS client demonstrating asynchronous cryptography features and optionally
  22. * using the crypto or PK callbacks */
  23. /* std */
  24. #include <stdlib.h>
  25. #include <stdio.h>
  26. #include <string.h>
  27. /* socket */
  28. #include <sys/socket.h>
  29. #include <arpa/inet.h>
  30. #include <netinet/in.h>
  31. #include <unistd.h>
  32. /* wolfSSL */
  33. #ifndef WOLFSSL_USER_SETTINGS
  34. #include <wolfssl/options.h>
  35. #endif
  36. #include <wolfssl/ssl.h>
  37. #include <wolfssl/wolfio.h>
  38. #include <wolfssl/wolfcrypt/error-crypt.h>
  39. #include "examples/async/async_tls.h"
  40. /* Test certificates and keys for RSA and ECC */
  41. #ifndef NO_RSA
  42. #define CERT_FILE "./certs/client-cert.pem"
  43. #define KEY_FILE "./certs/client-key.pem"
  44. #define CA_FILE "./certs/ca-cert.pem"
  45. #elif defined(HAVE_ECC)
  46. #define CERT_FILE "./certs/client-ecc-cert.pem"
  47. #define KEY_FILE "./certs/ecc-client-key.pem"
  48. #define CA_FILE "./certs/ca-ecc-cert.pem"
  49. #else
  50. #error No authentication algorithm (ECC/RSA)
  51. #endif
  52. int client_async_test(int argc, char** argv)
  53. {
  54. int ret = 0;
  55. int sockfd = SOCKET_INVALID;
  56. struct sockaddr_in servAddr;
  57. char buff[TEST_BUF_SZ];
  58. size_t len;
  59. int devId = 1; /* anything besides -2 (INVALID_DEVID) */
  60. #ifdef WOLF_CRYPTO_CB
  61. AsyncTlsCryptoCbCtx myCtx;
  62. #endif
  63. int err;
  64. char errBuff[WOLFSSL_MAX_ERROR_SZ];
  65. /* declare wolfSSL objects */
  66. WOLFSSL_CTX* ctx = NULL;
  67. WOLFSSL* ssl = NULL;
  68. /* Check for proper calling convention */
  69. if (argc != 2) {
  70. printf("usage: %s <IPv4 address>\n", argv[0]);
  71. return 0;
  72. }
  73. /* Create a socket that uses an internet IPv4 address,
  74. * Sets the socket to be stream based (TCP),
  75. * 0 means choose the default protocol. */
  76. if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
  77. fprintf(stderr, "ERROR: failed to create the socket\n");
  78. ret = -1; goto exit;
  79. }
  80. /* Initialize the server address struct with zeros */
  81. memset(&servAddr, 0, sizeof(servAddr));
  82. /* Fill in the server address */
  83. servAddr.sin_family = AF_INET; /* using IPv4 */
  84. servAddr.sin_port = htons(DEFAULT_PORT); /* on DEFAULT_PORT */
  85. /* Get the server IPv4 address from the command line call */
  86. if (inet_pton(AF_INET, argv[1], &servAddr.sin_addr) != 1) {
  87. fprintf(stderr, "ERROR: invalid address\n");
  88. ret = -1; goto exit;
  89. }
  90. /* Connect to the server */
  91. if ((ret = connect(sockfd, (struct sockaddr*) &servAddr, sizeof(servAddr)))
  92. == -1) {
  93. fprintf(stderr, "ERROR: failed to connect\n");
  94. goto exit;
  95. }
  96. /*---------------------------------*/
  97. /* Start of wolfSSL initialization and configuration */
  98. /*---------------------------------*/
  99. #ifdef DEBUG_WOLFSSL
  100. wolfSSL_Debugging_ON();
  101. #endif
  102. /* Initialize wolfSSL */
  103. if ((ret = wolfSSL_Init()) != WOLFSSL_SUCCESS) {
  104. fprintf(stderr, "ERROR: Failed to initialize the library\n");
  105. goto exit;
  106. }
  107. /* Create and initialize WOLFSSL_CTX */
  108. if ((ctx = wolfSSL_CTX_new(wolfSSLv23_client_method())) == NULL) {
  109. fprintf(stderr, "ERROR: failed to create WOLFSSL_CTX\n");
  110. ret = -1; goto exit;
  111. }
  112. #ifdef WOLF_CRYPTO_CB
  113. XMEMSET(&myCtx, 0, sizeof(myCtx));
  114. /* register a devID for crypto callbacks */
  115. ret = wc_CryptoCb_RegisterDevice(devId, AsyncTlsCryptoCb, &myCtx);
  116. if (ret != 0) {
  117. fprintf(stderr, "wc_CryptoCb_RegisterDevice: error %d", ret);
  118. goto exit;
  119. }
  120. #endif
  121. /* register a devID for crypto callbacks */
  122. wolfSSL_CTX_SetDevId(ctx, devId);
  123. /* Load client certificate into WOLFSSL_CTX */
  124. if ((ret = wolfSSL_CTX_use_certificate_file(ctx, CERT_FILE, WOLFSSL_FILETYPE_PEM))
  125. != WOLFSSL_SUCCESS) {
  126. fprintf(stderr, "ERROR: failed to load %s, please check the file.\n",
  127. CERT_FILE);
  128. goto exit;
  129. }
  130. /* Load client key into WOLFSSL_CTX */
  131. if ((ret = wolfSSL_CTX_use_PrivateKey_file(ctx, KEY_FILE, WOLFSSL_FILETYPE_PEM))
  132. != WOLFSSL_SUCCESS) {
  133. fprintf(stderr, "ERROR: failed to load %s, please check the file.\n",
  134. KEY_FILE);
  135. goto exit;
  136. }
  137. /* Load CA certificate into WOLFSSL_CTX */
  138. if ((ret = wolfSSL_CTX_load_verify_locations(ctx, CA_FILE, NULL))
  139. != WOLFSSL_SUCCESS) {
  140. fprintf(stderr, "ERROR: failed to load %s, please check the file.\n",
  141. CA_FILE);
  142. goto exit;
  143. }
  144. /* Create a WOLFSSL object */
  145. if ((ssl = wolfSSL_new(ctx)) == NULL) {
  146. fprintf(stderr, "ERROR: failed to create WOLFSSL object\n");
  147. ret = -1; goto exit;
  148. }
  149. /* Attach wolfSSL to the socket */
  150. if ((ret = wolfSSL_set_fd(ssl, sockfd)) != WOLFSSL_SUCCESS) {
  151. fprintf(stderr, "ERROR: Failed to set the file descriptor\n");
  152. goto exit;
  153. }
  154. /* Connect to wolfSSL on the server side */
  155. #ifdef WOLFSSL_ASYNC_CRYPT
  156. err = 0; /* Reset error */
  157. #endif
  158. do {
  159. #ifdef WOLFSSL_ASYNC_CRYPT
  160. if (err == WC_PENDING_E) {
  161. ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  162. if (ret < 0)
  163. break;
  164. }
  165. #endif
  166. ret = wolfSSL_connect(ssl);
  167. err = wolfSSL_get_error(ssl, 0);
  168. } while (err == WC_PENDING_E);
  169. if (ret != WOLFSSL_SUCCESS) {
  170. fprintf(stderr, "wolfSSL_connect error %d: %s\n",
  171. err, wolfSSL_ERR_error_string(err, errBuff));
  172. goto exit;
  173. }
  174. /* Get a message for the server from stdin */
  175. printf("Message for server: ");
  176. memset(buff, 0, sizeof(buff));
  177. if (fgets(buff, sizeof(buff), stdin) == NULL) {
  178. fprintf(stderr, "ERROR: failed to get message for server\n");
  179. ret = -1; goto exit;
  180. }
  181. len = strnlen(buff, sizeof(buff));
  182. /* Send the message to the server */
  183. #ifdef WOLFSSL_ASYNC_CRYPT
  184. err = 0; /* Reset error */
  185. #endif
  186. do {
  187. #ifdef WOLFSSL_ASYNC_CRYPT
  188. if (err == WC_PENDING_E) {
  189. ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  190. if (ret < 0)
  191. break;
  192. }
  193. #endif
  194. ret = wolfSSL_write(ssl, buff, (int)len);
  195. err = wolfSSL_get_error(ssl, 0);
  196. } while (err == WC_PENDING_E);
  197. if (ret != (int)len) {
  198. fprintf(stderr, "wolfSSL_write error %d: %s\n",
  199. err, wolfSSL_ERR_error_string(err, errBuff));
  200. goto exit;
  201. }
  202. /* Read the server data into our buff array */
  203. memset(buff, 0, sizeof(buff));
  204. #ifdef WOLFSSL_ASYNC_CRYPT
  205. err = 0; /* Reset error */
  206. #endif
  207. do {
  208. #ifdef WOLFSSL_ASYNC_CRYPT
  209. if (err == WC_PENDING_E) {
  210. ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  211. if (ret < 0)
  212. break;
  213. }
  214. #endif
  215. ret = wolfSSL_read(ssl, buff, sizeof(buff)-1);
  216. err = wolfSSL_get_error(ssl, 0);
  217. } while (err == WC_PENDING_E);
  218. if (ret < 0) {
  219. fprintf(stderr, "wolfSSL_read error %d: %s\n",
  220. err, wolfSSL_ERR_error_string(err, errBuff));
  221. goto exit;
  222. }
  223. /* Print to stdout any data the server sends */
  224. printf("Server: %s\n", buff);
  225. /* Return reporting a success */
  226. ret = 0;
  227. exit:
  228. /* Cleanup and return */
  229. if (sockfd != SOCKET_INVALID)
  230. close(sockfd); /* Close the connection to the server */
  231. if (ssl)
  232. wolfSSL_free(ssl); /* Free the wolfSSL object */
  233. if (ctx)
  234. wolfSSL_CTX_free(ctx); /* Free the wolfSSL context object */
  235. wolfSSL_Cleanup(); /* Cleanup the wolfSSL environment */
  236. (void)argc;
  237. (void)argv;
  238. return ret;
  239. }
  240. #ifndef NO_MAIN_DRIVER
  241. int main(int argc, char** argv)
  242. {
  243. return client_async_test(argc, argv);
  244. }
  245. #endif /* !NO_MAIN_DRIVER */