utils.h 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376
  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. XMEMCPY(buf + *len, data, sz);
  148. *len += sz;
  149. return sz;
  150. }
  151. static WC_INLINE int test_memio_read_cb(WOLFSSL *ssl, char *data, int sz,
  152. void *ctx)
  153. {
  154. struct test_memio_ctx *test_ctx;
  155. int read_sz;
  156. byte *buf;
  157. int *len;
  158. test_ctx = (struct test_memio_ctx*)ctx;
  159. if (wolfSSL_GetSide(ssl) == WOLFSSL_SERVER_END) {
  160. buf = test_ctx->s_buff;
  161. len = &test_ctx->s_len;
  162. }
  163. else {
  164. buf = test_ctx->c_buff;
  165. len = &test_ctx->c_len;
  166. }
  167. if (*len == 0)
  168. return WOLFSSL_CBIO_ERR_WANT_READ;
  169. read_sz = sz < *len ? sz : *len;
  170. XMEMCPY(data, buf, read_sz);
  171. XMEMMOVE(buf, buf + read_sz, *len - read_sz);
  172. *len -= read_sz;
  173. return read_sz;
  174. }
  175. int test_memio_do_handshake(WOLFSSL *ssl_c, WOLFSSL *ssl_s,
  176. int max_rounds, int *rounds)
  177. {
  178. byte handshake_complete = 0, hs_c = 0, hs_s = 0;
  179. int ret, err;
  180. if (rounds != NULL)
  181. *rounds = 0;
  182. while (!handshake_complete && max_rounds > 0) {
  183. if (!hs_c) {
  184. wolfSSL_SetLoggingPrefix("client");
  185. ret = wolfSSL_connect(ssl_c);
  186. wolfSSL_SetLoggingPrefix(NULL);
  187. if (ret == WOLFSSL_SUCCESS) {
  188. hs_c = 1;
  189. }
  190. else {
  191. err = wolfSSL_get_error(ssl_c, ret);
  192. if (err != WOLFSSL_ERROR_WANT_READ &&
  193. err != WOLFSSL_ERROR_WANT_WRITE)
  194. return -1;
  195. }
  196. }
  197. if (!hs_s) {
  198. wolfSSL_SetLoggingPrefix("server");
  199. ret = wolfSSL_accept(ssl_s);
  200. wolfSSL_SetLoggingPrefix(NULL);
  201. if (ret == WOLFSSL_SUCCESS) {
  202. hs_s = 1;
  203. }
  204. else {
  205. err = wolfSSL_get_error(ssl_s, ret);
  206. if (err != WOLFSSL_ERROR_WANT_READ &&
  207. err != WOLFSSL_ERROR_WANT_WRITE)
  208. return -1;
  209. }
  210. }
  211. handshake_complete = hs_c && hs_s;
  212. max_rounds--;
  213. if (rounds != NULL)
  214. *rounds = *rounds + 1;
  215. }
  216. if (!handshake_complete)
  217. return -1;
  218. return 0;
  219. }
  220. int test_memio_setup(struct test_memio_ctx *ctx,
  221. WOLFSSL_CTX **ctx_c, WOLFSSL_CTX **ctx_s, WOLFSSL **ssl_c, WOLFSSL **ssl_s,
  222. method_provider method_c, method_provider method_s)
  223. {
  224. int ret;
  225. if (ctx_c != NULL && *ctx_c == NULL) {
  226. *ctx_c = wolfSSL_CTX_new(method_c());
  227. if (*ctx_c == NULL)
  228. return -1;
  229. #ifndef NO_CERTS
  230. ret = wolfSSL_CTX_load_verify_locations(*ctx_c, caCertFile, 0);
  231. if (ret != WOLFSSL_SUCCESS)
  232. return -1;
  233. #endif /* NO_CERTS */
  234. wolfSSL_SetIORecv(*ctx_c, test_memio_read_cb);
  235. wolfSSL_SetIOSend(*ctx_c, test_memio_write_cb);
  236. if (ctx->c_ciphers != NULL) {
  237. ret = wolfSSL_CTX_set_cipher_list(*ctx_c, ctx->c_ciphers);
  238. if (ret != WOLFSSL_SUCCESS)
  239. return -1;
  240. }
  241. }
  242. if (ctx_s != NULL && *ctx_s == NULL) {
  243. *ctx_s = wolfSSL_CTX_new(method_s());
  244. if (*ctx_s == NULL)
  245. return -1;
  246. #ifndef NO_CERTS
  247. ret = wolfSSL_CTX_use_PrivateKey_file(*ctx_s, svrKeyFile,
  248. WOLFSSL_FILETYPE_PEM);
  249. if (ret != WOLFSSL_SUCCESS)
  250. return- -1;
  251. ret = wolfSSL_CTX_use_certificate_file(*ctx_s, svrCertFile,
  252. WOLFSSL_FILETYPE_PEM);
  253. if (ret != WOLFSSL_SUCCESS)
  254. return -1;
  255. #endif
  256. wolfSSL_SetIORecv(*ctx_s, test_memio_read_cb);
  257. wolfSSL_SetIOSend(*ctx_s, test_memio_write_cb);
  258. if (ctx->s_ciphers != NULL) {
  259. ret = wolfSSL_CTX_set_cipher_list(*ctx_s, ctx->s_ciphers);
  260. if (ret != WOLFSSL_SUCCESS)
  261. return -1;
  262. }
  263. }
  264. if (ctx_c != NULL && ssl_c != NULL) {
  265. *ssl_c = wolfSSL_new(*ctx_c);
  266. if (*ssl_c == NULL)
  267. return -1;
  268. wolfSSL_SetIOWriteCtx(*ssl_c, ctx);
  269. wolfSSL_SetIOReadCtx(*ssl_c, ctx);
  270. }
  271. if (ctx_s != NULL && ssl_s != NULL) {
  272. *ssl_s = wolfSSL_new(*ctx_s);
  273. if (*ssl_s == NULL)
  274. return -1;
  275. wolfSSL_SetIOWriteCtx(*ssl_s, ctx);
  276. wolfSSL_SetIOReadCtx(*ssl_s, ctx);
  277. #if !defined(NO_DH)
  278. SetDH(*ssl_s);
  279. #endif
  280. }
  281. return 0;
  282. }
  283. #endif
  284. #if !defined(SINGLE_THREADED) && defined(WOLFSSL_COND)
  285. void signal_ready(tcp_ready* ready)
  286. {
  287. THREAD_CHECK_RET(wolfSSL_CondStart(&ready->cond));
  288. ready->ready = 1;
  289. THREAD_CHECK_RET(wolfSSL_CondSignal(&ready->cond));
  290. THREAD_CHECK_RET(wolfSSL_CondEnd(&ready->cond));
  291. }
  292. #endif
  293. void wait_tcp_ready(func_args* args)
  294. {
  295. #if !defined(SINGLE_THREADED) && defined(WOLFSSL_COND)
  296. tcp_ready* ready = args->signal;
  297. THREAD_CHECK_RET(wolfSSL_CondStart(&ready->cond));
  298. if (!ready->ready) {
  299. THREAD_CHECK_RET(wolfSSL_CondWait(&ready->cond));
  300. }
  301. ready->ready = 0; /* reset */
  302. THREAD_CHECK_RET(wolfSSL_CondEnd(&ready->cond));
  303. #else
  304. /* no threading wait or single threaded */
  305. (void)args;
  306. #endif
  307. }
  308. #ifndef SINGLE_THREADED
  309. /* Start a thread.
  310. *
  311. * @param [in] fun Function to execute in thread.
  312. * @param [in] args Object to send to function in thread.
  313. * @param [out] thread Handle to thread.
  314. */
  315. void start_thread(THREAD_CB fun, func_args* args, THREAD_TYPE* thread)
  316. {
  317. THREAD_CHECK_RET(wolfSSL_NewThread(thread, fun, args));
  318. }
  319. /* Join thread to wait for completion.
  320. *
  321. * @param [in] thread Handle to thread.
  322. */
  323. void join_thread(THREAD_TYPE thread)
  324. {
  325. THREAD_CHECK_RET(wolfSSL_JoinThread(thread));
  326. }
  327. #endif /* SINGLE_THREADED */