utils.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439
  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. #if defined(__MACH__) || defined(__FreeBSD__)
  100. int link_file(const char* in, const char* out)
  101. {
  102. return link(in, out);
  103. }
  104. #endif
  105. #endif /* !NO_FILESYSTEM */
  106. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && !defined(NO_RSA) && \
  107. !defined(NO_WOLFSSL_SERVER) && !defined(NO_WOLFSSL_CLIENT)
  108. /* This set of memio functions allows for more fine tuned control of the TLS
  109. * connection operations. For new tests, try to use ssl_memio first. */
  110. /* To dump the memory in gdb use
  111. * dump memory client.bin test_ctx.c_buff test_ctx.c_buff+test_ctx.c_len
  112. * dump memory server.bin test_ctx.s_buff test_ctx.s_buff+test_ctx.s_len
  113. * This can be imported into Wireshark by transforming the file with
  114. * od -Ax -tx1 -v client.bin > client.bin.hex
  115. * od -Ax -tx1 -v server.bin > server.bin.hex
  116. * And then loading test_output.dump.hex into Wireshark using the
  117. * "Import from Hex Dump..." option ion and selecting the TCP
  118. * encapsulation option.
  119. */
  120. #define HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES
  121. #define TEST_MEMIO_BUF_SZ (64 * 1024)
  122. struct test_memio_ctx
  123. {
  124. byte c_buff[TEST_MEMIO_BUF_SZ];
  125. int c_len;
  126. const char* c_ciphers;
  127. byte s_buff[TEST_MEMIO_BUF_SZ];
  128. int s_len;
  129. const char* s_ciphers;
  130. };
  131. int test_memio_do_handshake(WOLFSSL *ssl_c, WOLFSSL *ssl_s,
  132. int max_rounds, int *rounds);
  133. int test_memio_setup(struct test_memio_ctx *ctx,
  134. WOLFSSL_CTX **ctx_c, WOLFSSL_CTX **ctx_s, WOLFSSL **ssl_c, WOLFSSL **ssl_s,
  135. method_provider method_c, method_provider method_s);
  136. int test_memio_setup_ex(struct test_memio_ctx *ctx,
  137. WOLFSSL_CTX **ctx_c, WOLFSSL_CTX **ctx_s, WOLFSSL **ssl_c, WOLFSSL **ssl_s,
  138. method_provider method_c, method_provider method_s,
  139. byte *caCert, int caCertSz, byte *serverCert, int serverCertSz,
  140. byte *serverKey, int serverKeySz);
  141. static WC_INLINE int test_memio_write_cb(WOLFSSL *ssl, char *data, int sz,
  142. void *ctx)
  143. {
  144. struct test_memio_ctx *test_ctx;
  145. byte *buf;
  146. int *len;
  147. test_ctx = (struct test_memio_ctx*)ctx;
  148. if (wolfSSL_GetSide(ssl) == WOLFSSL_SERVER_END) {
  149. buf = test_ctx->c_buff;
  150. len = &test_ctx->c_len;
  151. }
  152. else {
  153. buf = test_ctx->s_buff;
  154. len = &test_ctx->s_len;
  155. }
  156. if ((unsigned)(*len + sz) > TEST_MEMIO_BUF_SZ)
  157. return WOLFSSL_CBIO_ERR_WANT_WRITE;
  158. #ifdef WOLFSSL_DUMP_MEMIO_STREAM
  159. {
  160. WOLFSSL_BIO *dump_file = wolfSSL_BIO_new_file("test_memio.dump", "a");
  161. if (dump_file != NULL) {
  162. (void)wolfSSL_BIO_write(dump_file, data, sz);
  163. wolfSSL_BIO_free(dump_file);
  164. }
  165. }
  166. #endif
  167. XMEMCPY(buf + *len, data, sz);
  168. *len += sz;
  169. return sz;
  170. }
  171. static WC_INLINE int test_memio_read_cb(WOLFSSL *ssl, char *data, int sz,
  172. void *ctx)
  173. {
  174. struct test_memio_ctx *test_ctx;
  175. int read_sz;
  176. byte *buf;
  177. int *len;
  178. test_ctx = (struct test_memio_ctx*)ctx;
  179. if (wolfSSL_GetSide(ssl) == WOLFSSL_SERVER_END) {
  180. buf = test_ctx->s_buff;
  181. len = &test_ctx->s_len;
  182. }
  183. else {
  184. buf = test_ctx->c_buff;
  185. len = &test_ctx->c_len;
  186. }
  187. if (*len == 0)
  188. return WOLFSSL_CBIO_ERR_WANT_READ;
  189. read_sz = sz < *len ? sz : *len;
  190. XMEMCPY(data, buf, read_sz);
  191. XMEMMOVE(buf, buf + read_sz, *len - read_sz);
  192. *len -= read_sz;
  193. return read_sz;
  194. }
  195. int test_memio_do_handshake(WOLFSSL *ssl_c, WOLFSSL *ssl_s,
  196. int max_rounds, int *rounds)
  197. {
  198. byte handshake_complete = 0, hs_c = 0, hs_s = 0;
  199. int ret, err;
  200. if (rounds != NULL)
  201. *rounds = 0;
  202. while (!handshake_complete && max_rounds > 0) {
  203. if (!hs_c) {
  204. wolfSSL_SetLoggingPrefix("client");
  205. ret = wolfSSL_connect(ssl_c);
  206. wolfSSL_SetLoggingPrefix(NULL);
  207. if (ret == WOLFSSL_SUCCESS) {
  208. hs_c = 1;
  209. }
  210. else {
  211. err = wolfSSL_get_error(ssl_c, ret);
  212. if (err != WOLFSSL_ERROR_WANT_READ &&
  213. err != WOLFSSL_ERROR_WANT_WRITE)
  214. return -1;
  215. }
  216. }
  217. if (!hs_s) {
  218. wolfSSL_SetLoggingPrefix("server");
  219. ret = wolfSSL_accept(ssl_s);
  220. wolfSSL_SetLoggingPrefix(NULL);
  221. if (ret == WOLFSSL_SUCCESS) {
  222. hs_s = 1;
  223. }
  224. else {
  225. err = wolfSSL_get_error(ssl_s, ret);
  226. if (err != WOLFSSL_ERROR_WANT_READ &&
  227. err != WOLFSSL_ERROR_WANT_WRITE)
  228. return -1;
  229. }
  230. }
  231. handshake_complete = hs_c && hs_s;
  232. max_rounds--;
  233. if (rounds != NULL)
  234. *rounds = *rounds + 1;
  235. }
  236. if (!handshake_complete)
  237. return -1;
  238. return 0;
  239. }
  240. int test_memio_setup_ex(struct test_memio_ctx *ctx,
  241. WOLFSSL_CTX **ctx_c, WOLFSSL_CTX **ctx_s, WOLFSSL **ssl_c, WOLFSSL **ssl_s,
  242. method_provider method_c, method_provider method_s,
  243. byte *caCert, int caCertSz, byte *serverCert, int serverCertSz,
  244. byte *serverKey, int serverKeySz)
  245. {
  246. int ret;
  247. (void)caCert;
  248. (void)caCertSz;
  249. (void)serverCert;
  250. (void)serverCertSz;
  251. (void)serverKey;
  252. (void)serverKeySz;
  253. if (ctx_c != NULL && *ctx_c == NULL) {
  254. *ctx_c = wolfSSL_CTX_new(method_c());
  255. if (*ctx_c == NULL)
  256. return -1;
  257. #ifndef NO_CERTS
  258. if (caCert == NULL) {
  259. ret = wolfSSL_CTX_load_verify_locations(*ctx_c, caCertFile, 0);
  260. }
  261. else {
  262. ret = wolfSSL_CTX_load_verify_buffer(*ctx_c, caCert, (long)caCertSz,
  263. WOLFSSL_FILETYPE_ASN1);
  264. }
  265. if (ret != WOLFSSL_SUCCESS)
  266. return -1;
  267. #endif /* NO_CERTS */
  268. wolfSSL_SetIORecv(*ctx_c, test_memio_read_cb);
  269. wolfSSL_SetIOSend(*ctx_c, test_memio_write_cb);
  270. if (ctx->c_ciphers != NULL) {
  271. ret = wolfSSL_CTX_set_cipher_list(*ctx_c, ctx->c_ciphers);
  272. if (ret != WOLFSSL_SUCCESS)
  273. return -1;
  274. }
  275. }
  276. if (ctx_s != NULL && *ctx_s == NULL) {
  277. *ctx_s = wolfSSL_CTX_new(method_s());
  278. if (*ctx_s == NULL)
  279. return -1;
  280. #ifndef NO_CERTS
  281. if (serverKey == NULL) {
  282. ret = wolfSSL_CTX_use_PrivateKey_file(*ctx_s, svrKeyFile,
  283. WOLFSSL_FILETYPE_PEM);
  284. }
  285. else {
  286. ret = wolfSSL_CTX_use_PrivateKey_buffer(*ctx_s, serverKey,
  287. (long)serverKeySz, WOLFSSL_FILETYPE_ASN1);
  288. }
  289. if (ret != WOLFSSL_SUCCESS)
  290. return- -1;
  291. if (serverCert == NULL) {
  292. ret = wolfSSL_CTX_use_certificate_file(*ctx_s, svrCertFile,
  293. WOLFSSL_FILETYPE_PEM);
  294. }
  295. else {
  296. ret = wolfSSL_CTX_use_certificate_chain_buffer_format(*ctx_s,
  297. serverCert, (long)serverCertSz, WOLFSSL_FILETYPE_ASN1);
  298. }
  299. if (ret != WOLFSSL_SUCCESS)
  300. return -1;
  301. #endif /* NO_CERTS */
  302. wolfSSL_SetIORecv(*ctx_s, test_memio_read_cb);
  303. wolfSSL_SetIOSend(*ctx_s, test_memio_write_cb);
  304. if (ctx->s_ciphers != NULL) {
  305. ret = wolfSSL_CTX_set_cipher_list(*ctx_s, ctx->s_ciphers);
  306. if (ret != WOLFSSL_SUCCESS)
  307. return -1;
  308. }
  309. }
  310. if (ctx_c != NULL && ssl_c != NULL) {
  311. *ssl_c = wolfSSL_new(*ctx_c);
  312. if (*ssl_c == NULL)
  313. return -1;
  314. wolfSSL_SetIOWriteCtx(*ssl_c, ctx);
  315. wolfSSL_SetIOReadCtx(*ssl_c, ctx);
  316. }
  317. if (ctx_s != NULL && ssl_s != NULL) {
  318. *ssl_s = wolfSSL_new(*ctx_s);
  319. if (*ssl_s == NULL)
  320. return -1;
  321. wolfSSL_SetIOWriteCtx(*ssl_s, ctx);
  322. wolfSSL_SetIOReadCtx(*ssl_s, ctx);
  323. #if !defined(NO_DH)
  324. SetDH(*ssl_s);
  325. #endif
  326. }
  327. return 0;
  328. }
  329. int test_memio_setup(struct test_memio_ctx *ctx,
  330. WOLFSSL_CTX **ctx_c, WOLFSSL_CTX **ctx_s, WOLFSSL **ssl_c, WOLFSSL **ssl_s,
  331. method_provider method_c, method_provider method_s)
  332. {
  333. return test_memio_setup_ex(ctx, ctx_c, ctx_s, ssl_c, ssl_s, method_c,
  334. method_s, NULL, 0, NULL, 0, NULL, 0);
  335. }
  336. #endif
  337. #if !defined(SINGLE_THREADED) && defined(WOLFSSL_COND)
  338. void signal_ready(tcp_ready* ready)
  339. {
  340. THREAD_CHECK_RET(wolfSSL_CondStart(&ready->cond));
  341. ready->ready = 1;
  342. THREAD_CHECK_RET(wolfSSL_CondSignal(&ready->cond));
  343. THREAD_CHECK_RET(wolfSSL_CondEnd(&ready->cond));
  344. }
  345. #endif
  346. void wait_tcp_ready(func_args* args)
  347. {
  348. #if !defined(SINGLE_THREADED) && defined(WOLFSSL_COND)
  349. tcp_ready* ready = args->signal;
  350. THREAD_CHECK_RET(wolfSSL_CondStart(&ready->cond));
  351. if (!ready->ready) {
  352. THREAD_CHECK_RET(wolfSSL_CondWait(&ready->cond));
  353. }
  354. ready->ready = 0; /* reset */
  355. THREAD_CHECK_RET(wolfSSL_CondEnd(&ready->cond));
  356. #else
  357. /* no threading wait or single threaded */
  358. (void)args;
  359. #endif
  360. }
  361. #ifndef SINGLE_THREADED
  362. /* Start a thread.
  363. *
  364. * @param [in] fun Function to execute in thread.
  365. * @param [in] args Object to send to function in thread.
  366. * @param [out] thread Handle to thread.
  367. */
  368. void start_thread(THREAD_CB fun, func_args* args, THREAD_TYPE* thread)
  369. {
  370. THREAD_CHECK_RET(wolfSSL_NewThread(thread, fun, args));
  371. }
  372. /* Join thread to wait for completion.
  373. *
  374. * @param [in] thread Handle to thread.
  375. */
  376. void join_thread(THREAD_TYPE thread)
  377. {
  378. THREAD_CHECK_RET(wolfSSL_JoinThread(thread));
  379. }
  380. #endif /* SINGLE_THREADED */
  381. /* These correspond to WOLFSSL_SSLV3...WOLFSSL_DTLSV1_3 */
  382. const char* tls_desc[] = {
  383. "SSLv3", "TLSv1.0", "TLSv1.1", "TLSv1.2", "TLSv1.3",
  384. "DTLSv1.0", "DTLSv1.2", "DTLSv1.3"
  385. };