utils.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385
  1. /* utils.h
  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. #ifndef NO_FILESYSTEM
  27. #ifdef _MSC_VER
  28. #include <direct.h>
  29. #endif
  30. #define TMP_DIR_PREFIX "tmpDir-"
  31. /* len is length of tmpDir name, assuming
  32. * len does not include null terminating character */
  33. char* create_tmp_dir(char *tmpDir, int len)
  34. {
  35. if (len < (int)XSTR_SIZEOF(TMP_DIR_PREFIX))
  36. return NULL;
  37. XMEMCPY(tmpDir, TMP_DIR_PREFIX, XSTR_SIZEOF(TMP_DIR_PREFIX));
  38. if (mymktemp(tmpDir, len, len - XSTR_SIZEOF(TMP_DIR_PREFIX)) == NULL)
  39. return NULL;
  40. #ifdef _MSC_VER
  41. if (_mkdir(tmpDir) != 0)
  42. return NULL;
  43. #elif defined(__MINGW32__)
  44. if (mkdir(tmpDir) != 0)
  45. return NULL;
  46. #else
  47. if (mkdir(tmpDir, 0700) != 0)
  48. return NULL;
  49. #endif
  50. return tmpDir;
  51. }
  52. int rem_dir(const char* dirName)
  53. {
  54. #ifdef _MSC_VER
  55. if (_rmdir(dirName) != 0)
  56. return -1;
  57. #else
  58. if (rmdir(dirName) != 0)
  59. return -1;
  60. #endif
  61. return 0;
  62. }
  63. int rem_file(const char* fileName)
  64. {
  65. #ifdef _MSC_VER
  66. if (_unlink(fileName) != 0)
  67. return -1;
  68. #else
  69. if (unlink(fileName) != 0)
  70. return -1;
  71. #endif
  72. return 0;
  73. }
  74. int copy_file(const char* in, const char* out)
  75. {
  76. byte buf[100];
  77. XFILE inFile = XBADFILE;
  78. XFILE outFile = XBADFILE;
  79. size_t sz;
  80. int ret = -1;
  81. inFile = XFOPEN(in, "rb");
  82. if (inFile == XBADFILE)
  83. goto cleanup;
  84. outFile = XFOPEN(out, "wb");
  85. if (outFile == XBADFILE)
  86. goto cleanup;
  87. while ((sz = XFREAD(buf, 1, sizeof(buf), inFile)) != 0) {
  88. if (XFWRITE(buf, 1, sz, outFile) != sz)
  89. goto cleanup;
  90. }
  91. ret = 0;
  92. cleanup:
  93. if (inFile != XBADFILE)
  94. XFCLOSE(inFile);
  95. if (outFile != XBADFILE)
  96. XFCLOSE(outFile);
  97. return ret;
  98. }
  99. #endif /* !NO_FILESYSTEM */
  100. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && !defined(NO_RSA) && \
  101. !defined(NO_WOLFSSL_SERVER) && !defined(NO_WOLFSSL_CLIENT)
  102. /* This set of memio functions allows for more fine tuned control of the TLS
  103. * connection operations. For new tests, try to use ssl_memio first. */
  104. /* To dump the memory in gdb use
  105. * dump memory client.bin test_ctx.c_buff test_ctx.c_buff+test_ctx.c_len
  106. * dump memory server.bin test_ctx.s_buff test_ctx.s_buff+test_ctx.s_len
  107. * This can be imported into Wireshark by transforming the file with
  108. * od -Ax -tx1 -v client.bin > client.bin.hex
  109. * od -Ax -tx1 -v server.bin > server.bin.hex
  110. * And then loading test_output.dump.hex into Wireshark using the
  111. * "Import from Hex Dump..." option ion and selecting the TCP
  112. * encapsulation option.
  113. */
  114. #define HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES
  115. #define TEST_MEMIO_BUF_SZ (64 * 1024)
  116. struct test_memio_ctx
  117. {
  118. byte c_buff[TEST_MEMIO_BUF_SZ];
  119. int c_len;
  120. const char* c_ciphers;
  121. byte s_buff[TEST_MEMIO_BUF_SZ];
  122. int s_len;
  123. const char* s_ciphers;
  124. };
  125. int test_memio_do_handshake(WOLFSSL *ssl_c, WOLFSSL *ssl_s,
  126. int max_rounds, int *rounds);
  127. int test_memio_setup(struct test_memio_ctx *ctx,
  128. WOLFSSL_CTX **ctx_c, WOLFSSL_CTX **ctx_s, WOLFSSL **ssl_c, WOLFSSL **ssl_s,
  129. method_provider method_c, method_provider method_s);
  130. static WC_INLINE int test_memio_write_cb(WOLFSSL *ssl, char *data, int sz,
  131. void *ctx)
  132. {
  133. struct test_memio_ctx *test_ctx;
  134. byte *buf;
  135. int *len;
  136. test_ctx = (struct test_memio_ctx*)ctx;
  137. if (wolfSSL_GetSide(ssl) == WOLFSSL_SERVER_END) {
  138. buf = test_ctx->c_buff;
  139. len = &test_ctx->c_len;
  140. }
  141. else {
  142. buf = test_ctx->s_buff;
  143. len = &test_ctx->s_len;
  144. }
  145. if ((unsigned)(*len + sz) > TEST_MEMIO_BUF_SZ)
  146. return WOLFSSL_CBIO_ERR_WANT_WRITE;
  147. #ifdef WOLFSSL_DUMP_MEMIO_STREAM
  148. {
  149. WOLFSSL_BIO *dump_file = wolfSSL_BIO_new_file("test_memio.dump", "a");
  150. if (dump_file != NULL) {
  151. (void)wolfSSL_BIO_write(dump_file, data, sz);
  152. wolfSSL_BIO_free(dump_file);
  153. }
  154. }
  155. #endif
  156. XMEMCPY(buf + *len, data, sz);
  157. *len += sz;
  158. return sz;
  159. }
  160. static WC_INLINE int test_memio_read_cb(WOLFSSL *ssl, char *data, int sz,
  161. void *ctx)
  162. {
  163. struct test_memio_ctx *test_ctx;
  164. int read_sz;
  165. byte *buf;
  166. int *len;
  167. test_ctx = (struct test_memio_ctx*)ctx;
  168. if (wolfSSL_GetSide(ssl) == WOLFSSL_SERVER_END) {
  169. buf = test_ctx->s_buff;
  170. len = &test_ctx->s_len;
  171. }
  172. else {
  173. buf = test_ctx->c_buff;
  174. len = &test_ctx->c_len;
  175. }
  176. if (*len == 0)
  177. return WOLFSSL_CBIO_ERR_WANT_READ;
  178. read_sz = sz < *len ? sz : *len;
  179. XMEMCPY(data, buf, read_sz);
  180. XMEMMOVE(buf, buf + read_sz, *len - read_sz);
  181. *len -= read_sz;
  182. return read_sz;
  183. }
  184. int test_memio_do_handshake(WOLFSSL *ssl_c, WOLFSSL *ssl_s,
  185. int max_rounds, int *rounds)
  186. {
  187. byte handshake_complete = 0, hs_c = 0, hs_s = 0;
  188. int ret, err;
  189. if (rounds != NULL)
  190. *rounds = 0;
  191. while (!handshake_complete && max_rounds > 0) {
  192. if (!hs_c) {
  193. wolfSSL_SetLoggingPrefix("client");
  194. ret = wolfSSL_connect(ssl_c);
  195. wolfSSL_SetLoggingPrefix(NULL);
  196. if (ret == WOLFSSL_SUCCESS) {
  197. hs_c = 1;
  198. }
  199. else {
  200. err = wolfSSL_get_error(ssl_c, ret);
  201. if (err != WOLFSSL_ERROR_WANT_READ &&
  202. err != WOLFSSL_ERROR_WANT_WRITE)
  203. return -1;
  204. }
  205. }
  206. if (!hs_s) {
  207. wolfSSL_SetLoggingPrefix("server");
  208. ret = wolfSSL_accept(ssl_s);
  209. wolfSSL_SetLoggingPrefix(NULL);
  210. if (ret == WOLFSSL_SUCCESS) {
  211. hs_s = 1;
  212. }
  213. else {
  214. err = wolfSSL_get_error(ssl_s, ret);
  215. if (err != WOLFSSL_ERROR_WANT_READ &&
  216. err != WOLFSSL_ERROR_WANT_WRITE)
  217. return -1;
  218. }
  219. }
  220. handshake_complete = hs_c && hs_s;
  221. max_rounds--;
  222. if (rounds != NULL)
  223. *rounds = *rounds + 1;
  224. }
  225. if (!handshake_complete)
  226. return -1;
  227. return 0;
  228. }
  229. int test_memio_setup(struct test_memio_ctx *ctx,
  230. WOLFSSL_CTX **ctx_c, WOLFSSL_CTX **ctx_s, WOLFSSL **ssl_c, WOLFSSL **ssl_s,
  231. method_provider method_c, method_provider method_s)
  232. {
  233. int ret;
  234. if (ctx_c != NULL && *ctx_c == NULL) {
  235. *ctx_c = wolfSSL_CTX_new(method_c());
  236. if (*ctx_c == NULL)
  237. return -1;
  238. #ifndef NO_CERTS
  239. ret = wolfSSL_CTX_load_verify_locations(*ctx_c, caCertFile, 0);
  240. if (ret != WOLFSSL_SUCCESS)
  241. return -1;
  242. #endif /* NO_CERTS */
  243. wolfSSL_SetIORecv(*ctx_c, test_memio_read_cb);
  244. wolfSSL_SetIOSend(*ctx_c, test_memio_write_cb);
  245. if (ctx->c_ciphers != NULL) {
  246. ret = wolfSSL_CTX_set_cipher_list(*ctx_c, ctx->c_ciphers);
  247. if (ret != WOLFSSL_SUCCESS)
  248. return -1;
  249. }
  250. }
  251. if (ctx_s != NULL && *ctx_s == NULL) {
  252. *ctx_s = wolfSSL_CTX_new(method_s());
  253. if (*ctx_s == NULL)
  254. return -1;
  255. #ifndef NO_CERTS
  256. ret = wolfSSL_CTX_use_PrivateKey_file(*ctx_s, svrKeyFile,
  257. WOLFSSL_FILETYPE_PEM);
  258. if (ret != WOLFSSL_SUCCESS)
  259. return- -1;
  260. ret = wolfSSL_CTX_use_certificate_file(*ctx_s, svrCertFile,
  261. WOLFSSL_FILETYPE_PEM);
  262. if (ret != WOLFSSL_SUCCESS)
  263. return -1;
  264. #endif
  265. wolfSSL_SetIORecv(*ctx_s, test_memio_read_cb);
  266. wolfSSL_SetIOSend(*ctx_s, test_memio_write_cb);
  267. if (ctx->s_ciphers != NULL) {
  268. ret = wolfSSL_CTX_set_cipher_list(*ctx_s, ctx->s_ciphers);
  269. if (ret != WOLFSSL_SUCCESS)
  270. return -1;
  271. }
  272. }
  273. if (ctx_c != NULL && ssl_c != NULL) {
  274. *ssl_c = wolfSSL_new(*ctx_c);
  275. if (*ssl_c == NULL)
  276. return -1;
  277. wolfSSL_SetIOWriteCtx(*ssl_c, ctx);
  278. wolfSSL_SetIOReadCtx(*ssl_c, ctx);
  279. }
  280. if (ctx_s != NULL && ssl_s != NULL) {
  281. *ssl_s = wolfSSL_new(*ctx_s);
  282. if (*ssl_s == NULL)
  283. return -1;
  284. wolfSSL_SetIOWriteCtx(*ssl_s, ctx);
  285. wolfSSL_SetIOReadCtx(*ssl_s, ctx);
  286. #if !defined(NO_DH)
  287. SetDH(*ssl_s);
  288. #endif
  289. }
  290. return 0;
  291. }
  292. #endif
  293. #if !defined(SINGLE_THREADED) && defined(WOLFSSL_COND)
  294. void signal_ready(tcp_ready* ready)
  295. {
  296. THREAD_CHECK_RET(wolfSSL_CondStart(&ready->cond));
  297. ready->ready = 1;
  298. THREAD_CHECK_RET(wolfSSL_CondSignal(&ready->cond));
  299. THREAD_CHECK_RET(wolfSSL_CondEnd(&ready->cond));
  300. }
  301. #endif
  302. void wait_tcp_ready(func_args* args)
  303. {
  304. #if !defined(SINGLE_THREADED) && defined(WOLFSSL_COND)
  305. tcp_ready* ready = args->signal;
  306. THREAD_CHECK_RET(wolfSSL_CondStart(&ready->cond));
  307. if (!ready->ready) {
  308. THREAD_CHECK_RET(wolfSSL_CondWait(&ready->cond));
  309. }
  310. ready->ready = 0; /* reset */
  311. THREAD_CHECK_RET(wolfSSL_CondEnd(&ready->cond));
  312. #else
  313. /* no threading wait or single threaded */
  314. (void)args;
  315. #endif
  316. }
  317. #ifndef SINGLE_THREADED
  318. /* Start a thread.
  319. *
  320. * @param [in] fun Function to execute in thread.
  321. * @param [in] args Object to send to function in thread.
  322. * @param [out] thread Handle to thread.
  323. */
  324. void start_thread(THREAD_CB fun, func_args* args, THREAD_TYPE* thread)
  325. {
  326. THREAD_CHECK_RET(wolfSSL_NewThread(thread, fun, args));
  327. }
  328. /* Join thread to wait for completion.
  329. *
  330. * @param [in] thread Handle to thread.
  331. */
  332. void join_thread(THREAD_TYPE thread)
  333. {
  334. THREAD_CHECK_RET(wolfSSL_JoinThread(thread));
  335. }
  336. #endif /* SINGLE_THREADED */