async_client.c 8.6 KB

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