utils.c 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  1. /* utils.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. #ifdef HAVE_CONFIG_H
  22. #include <config.h>
  23. #endif
  24. #include <wolfssl/wolfcrypt/settings.h>
  25. #include <tests/unit.h>
  26. #if !defined(WOLFSSL_TEST_UTILS_INCLUDED)
  27. #ifndef WOLFSSL_IGNORE_FILE_WARN
  28. #warning utils.c does not need to be compiled separately
  29. #endif
  30. #else
  31. #ifndef NO_FILESYSTEM
  32. #ifdef _MSC_VER
  33. #include <direct.h>
  34. #endif
  35. #define TMP_DIR_PREFIX "tmpDir-"
  36. /* len is length of tmpDir name, assuming
  37. * len does not include null terminating character */
  38. char* create_tmp_dir(char *tmpDir, int len)
  39. {
  40. if (len < (int)XSTR_SIZEOF(TMP_DIR_PREFIX))
  41. return NULL;
  42. XMEMCPY(tmpDir, TMP_DIR_PREFIX, XSTR_SIZEOF(TMP_DIR_PREFIX));
  43. if (mymktemp(tmpDir, len, len - XSTR_SIZEOF(TMP_DIR_PREFIX)) == NULL)
  44. return NULL;
  45. #ifdef _MSC_VER
  46. if (_mkdir(tmpDir) != 0)
  47. return NULL;
  48. #elif defined(__CYGWIN__) || defined(__MINGW32__)
  49. if (mkdir(tmpDir) != 0)
  50. return NULL;
  51. #else
  52. if (mkdir(tmpDir, 0700) != 0)
  53. return NULL;
  54. #endif
  55. return tmpDir;
  56. }
  57. int rem_dir(const char* dirName)
  58. {
  59. #ifdef _MSC_VER
  60. if (_rmdir(dirName) != 0)
  61. return -1;
  62. #else
  63. if (rmdir(dirName) != 0)
  64. return -1;
  65. #endif
  66. return 0;
  67. }
  68. int rem_file(const char* fileName)
  69. {
  70. #ifdef _MSC_VER
  71. if (_unlink(fileName) != 0)
  72. return -1;
  73. #else
  74. if (unlink(fileName) != 0)
  75. return -1;
  76. #endif
  77. return 0;
  78. }
  79. int copy_file(const char* in, const char* out)
  80. {
  81. byte buf[100];
  82. XFILE inFile = XBADFILE;
  83. XFILE outFile = XBADFILE;
  84. size_t sz;
  85. int ret = -1;
  86. inFile = XFOPEN(in, "rb");
  87. if (inFile == XBADFILE)
  88. goto cleanup;
  89. outFile = XFOPEN(out, "wb");
  90. if (outFile == XBADFILE)
  91. goto cleanup;
  92. while ((sz = XFREAD(buf, 1, sizeof(buf), inFile)) != 0) {
  93. if (XFWRITE(buf, 1, sz, outFile) != sz)
  94. goto cleanup;
  95. }
  96. ret = 0;
  97. cleanup:
  98. if (inFile != XBADFILE)
  99. XFCLOSE(inFile);
  100. if (outFile != XBADFILE)
  101. XFCLOSE(outFile);
  102. return ret;
  103. }
  104. #endif /* !NO_FILESYSTEM */
  105. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && !defined(NO_RSA) && \
  106. !defined(NO_WOLFSSL_SERVER) && !defined(NO_WOLFSSL_CLIENT)
  107. /* This set of memio functions allows for more fine tuned control of the TLS
  108. * connection operations. For new tests, try to use ssl_memio first. */
  109. #define HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES
  110. #define TEST_MEMIO_BUF_SZ (64 * 1024)
  111. struct test_memio_ctx
  112. {
  113. byte c_buff[TEST_MEMIO_BUF_SZ];
  114. int c_len;
  115. const char* c_ciphers;
  116. byte s_buff[TEST_MEMIO_BUF_SZ];
  117. int s_len;
  118. const char* s_ciphers;
  119. };
  120. int test_memio_do_handshake(WOLFSSL *ssl_c, WOLFSSL *ssl_s,
  121. int max_rounds, int *rounds);
  122. int test_memio_setup(struct test_memio_ctx *ctx,
  123. WOLFSSL_CTX **ctx_c, WOLFSSL_CTX **ctx_s, WOLFSSL **ssl_c, WOLFSSL **ssl_s,
  124. method_provider method_c, method_provider method_s);
  125. static WC_INLINE int test_memio_write_cb(WOLFSSL *ssl, char *data, int sz,
  126. void *ctx)
  127. {
  128. struct test_memio_ctx *test_ctx;
  129. byte *buf;
  130. int *len;
  131. test_ctx = (struct test_memio_ctx*)ctx;
  132. if (wolfSSL_GetSide(ssl) == WOLFSSL_SERVER_END) {
  133. buf = test_ctx->c_buff;
  134. len = &test_ctx->c_len;
  135. }
  136. else {
  137. buf = test_ctx->s_buff;
  138. len = &test_ctx->s_len;
  139. }
  140. if ((unsigned)(*len + sz) > TEST_MEMIO_BUF_SZ)
  141. return WOLFSSL_CBIO_ERR_WANT_READ;
  142. XMEMCPY(buf + *len, data, sz);
  143. *len += sz;
  144. return sz;
  145. }
  146. static WC_INLINE int test_memio_read_cb(WOLFSSL *ssl, char *data, int sz,
  147. void *ctx)
  148. {
  149. struct test_memio_ctx *test_ctx;
  150. int read_sz;
  151. byte *buf;
  152. int *len;
  153. test_ctx = (struct test_memio_ctx*)ctx;
  154. if (wolfSSL_GetSide(ssl) == WOLFSSL_SERVER_END) {
  155. buf = test_ctx->s_buff;
  156. len = &test_ctx->s_len;
  157. }
  158. else {
  159. buf = test_ctx->c_buff;
  160. len = &test_ctx->c_len;
  161. }
  162. if (*len == 0)
  163. return WOLFSSL_CBIO_ERR_WANT_READ;
  164. read_sz = sz < *len ? sz : *len;
  165. XMEMCPY(data, buf, read_sz);
  166. XMEMMOVE(buf, buf + read_sz, *len - read_sz);
  167. *len -= read_sz;
  168. return read_sz;
  169. }
  170. int test_memio_do_handshake(WOLFSSL *ssl_c, WOLFSSL *ssl_s,
  171. int max_rounds, int *rounds)
  172. {
  173. byte handshake_complete = 0, hs_c = 0, hs_s = 0;
  174. int ret, err;
  175. if (rounds != NULL)
  176. *rounds = 0;
  177. while (!handshake_complete && max_rounds > 0) {
  178. if (!hs_c) {
  179. ret = wolfSSL_connect(ssl_c);
  180. if (ret == WOLFSSL_SUCCESS) {
  181. hs_c = 1;
  182. }
  183. else {
  184. err = wolfSSL_get_error(ssl_c, ret);
  185. if (err != WOLFSSL_ERROR_WANT_READ &&
  186. err != WOLFSSL_ERROR_WANT_WRITE)
  187. return -1;
  188. }
  189. }
  190. if (!hs_s) {
  191. ret = wolfSSL_accept(ssl_s);
  192. if (ret == WOLFSSL_SUCCESS) {
  193. hs_s = 1;
  194. }
  195. else {
  196. err = wolfSSL_get_error(ssl_s, ret);
  197. if (err != WOLFSSL_ERROR_WANT_READ &&
  198. err != WOLFSSL_ERROR_WANT_WRITE)
  199. return -1;
  200. }
  201. }
  202. handshake_complete = hs_c && hs_s;
  203. max_rounds--;
  204. if (rounds != NULL)
  205. *rounds = *rounds + 1;
  206. }
  207. if (!handshake_complete)
  208. return -1;
  209. return 0;
  210. }
  211. int test_memio_setup(struct test_memio_ctx *ctx,
  212. WOLFSSL_CTX **ctx_c, WOLFSSL_CTX **ctx_s, WOLFSSL **ssl_c, WOLFSSL **ssl_s,
  213. method_provider method_c, method_provider method_s)
  214. {
  215. int ret;
  216. if (ctx_c != NULL && *ctx_c == NULL) {
  217. *ctx_c = wolfSSL_CTX_new(method_c());
  218. if (*ctx_c == NULL)
  219. return -1;
  220. #ifndef NO_CERTS
  221. ret = wolfSSL_CTX_load_verify_locations(*ctx_c, caCertFile, 0);
  222. if (ret != WOLFSSL_SUCCESS)
  223. return -1;
  224. #endif /* NO_CERTS */
  225. wolfSSL_SetIORecv(*ctx_c, test_memio_read_cb);
  226. wolfSSL_SetIOSend(*ctx_c, test_memio_write_cb);
  227. if (ctx->c_ciphers != NULL) {
  228. ret = wolfSSL_CTX_set_cipher_list(*ctx_c, ctx->c_ciphers);
  229. if (ret != WOLFSSL_SUCCESS)
  230. return -1;
  231. }
  232. }
  233. if (ctx_s != NULL && *ctx_s == NULL) {
  234. *ctx_s = wolfSSL_CTX_new(method_s());
  235. if (*ctx_s == NULL)
  236. return -1;
  237. #ifndef NO_CERTS
  238. ret = wolfSSL_CTX_use_PrivateKey_file(*ctx_s, svrKeyFile,
  239. WOLFSSL_FILETYPE_PEM);
  240. if (ret != WOLFSSL_SUCCESS)
  241. return- -1;
  242. ret = wolfSSL_CTX_use_certificate_file(*ctx_s, svrCertFile,
  243. WOLFSSL_FILETYPE_PEM);
  244. if (ret != WOLFSSL_SUCCESS)
  245. return -1;
  246. #endif
  247. wolfSSL_SetIORecv(*ctx_s, test_memio_read_cb);
  248. wolfSSL_SetIOSend(*ctx_s, test_memio_write_cb);
  249. if (ctx->s_ciphers != NULL) {
  250. ret = wolfSSL_CTX_set_cipher_list(*ctx_s, ctx->s_ciphers);
  251. if (ret != WOLFSSL_SUCCESS)
  252. return -1;
  253. }
  254. }
  255. if (ctx_c != NULL && ssl_c != NULL) {
  256. *ssl_c = wolfSSL_new(*ctx_c);
  257. if (*ssl_c == NULL)
  258. return -1;
  259. wolfSSL_SetIOWriteCtx(*ssl_c, ctx);
  260. wolfSSL_SetIOReadCtx(*ssl_c, ctx);
  261. }
  262. if (ctx_s != NULL && ssl_s != NULL) {
  263. *ssl_s = wolfSSL_new(*ctx_s);
  264. if (*ssl_s == NULL)
  265. return -1;
  266. wolfSSL_SetIOWriteCtx(*ssl_s, ctx);
  267. wolfSSL_SetIOReadCtx(*ssl_s, ctx);
  268. #if !defined(NO_DH)
  269. SetDH(*ssl_s);
  270. #endif
  271. }
  272. return 0;
  273. }
  274. #endif
  275. #endif /* WOLFSSL_TEST_UTILS_INCLUDED */