utils.h 12 KB

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