suites.c 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989
  1. /* suites.c
  2. *
  3. * Copyright (C) 2006-2019 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 <stdlib.h>
  26. #include <stdio.h>
  27. #include <string.h>
  28. #include <wolfssl/ssl.h>
  29. #include <tests/unit.h>
  30. #define MAX_ARGS 40
  31. #define MAX_COMMAND_SZ 240
  32. #ifdef WOLFSSL_TLS13
  33. #define MAX_SUITE_SZ 200
  34. #else
  35. #define MAX_SUITE_SZ 80
  36. #endif
  37. #define NOT_BUILT_IN -123
  38. #if defined(NO_OLD_TLS) || !defined(WOLFSSL_ALLOW_SSLV3) || \
  39. !defined(WOLFSSL_ALLOW_TLSV10)
  40. #define VERSION_TOO_OLD -124
  41. #endif
  42. #include "examples/client/client.h"
  43. #include "examples/server/server.h"
  44. #if !defined(NO_WOLFSSL_SERVER) && !defined(NO_WOLFSSL_CLIENT)
  45. static WOLFSSL_CTX* cipherSuiteCtx = NULL;
  46. static char nonblockFlag[] = "-N";
  47. static char noVerifyFlag[] = "-d";
  48. static char disableEMSFlag[] = "-n";
  49. static char flagSep[] = " ";
  50. #if !defined(USE_WINDOWS_API) && !defined(WOLFSSL_TIRTOS)
  51. static char portFlag[] = "-p";
  52. static char svrPort[] = "0";
  53. #endif
  54. static char intTestFlag[] = "-H";
  55. static char forceDefCipherListFlag[] = "defCipherList";
  56. static char exitWithRetFlag[] = "exitWithRet";
  57. static char disableDHPrimeTest[] = "-2";
  58. #ifdef WOLFSSL_ASYNC_CRYPT
  59. static int devId = INVALID_DEVID;
  60. #endif
  61. #ifdef VERSION_TOO_OLD
  62. static int GetTlsVersion(const char* line)
  63. {
  64. int version = -1;
  65. const char* find = "-v ";
  66. const char* begin = strstr(line, find);
  67. if (begin) {
  68. begin += 3;
  69. version = atoi(begin);
  70. }
  71. return version;
  72. }
  73. #ifndef WOLFSSL_ALLOW_SSLV3
  74. /* if the protocol version is sslv3 return 1, else 0 */
  75. static int IsSslVersion(const char* line)
  76. {
  77. int version = GetTlsVersion(line);
  78. return (version == 0) ? 1 : 0;
  79. }
  80. #endif /* !WOLFSSL_ALLOW_SSLV3 */
  81. #ifndef WOLFSSL_ALLOW_TLSV10
  82. /* if the protocol version is TLSv1.0 return 1, else 0 */
  83. static int IsTls10Version(const char* line)
  84. {
  85. int version = GetTlsVersion(line);
  86. return (version == 1) ? 1 : 0;
  87. }
  88. #endif /* !WOLFSSL_ALLOW_TLSV10 */
  89. #ifdef NO_OLD_TLS
  90. /* if the protocol version is less than tls 1.2 return 1, else 0 */
  91. static int IsOldTlsVersion(const char* line)
  92. {
  93. int version = GetTlsVersion(line);
  94. return (version < 3) ? 1 : 0;
  95. }
  96. #endif /* NO_OLD_TLS */
  97. #endif /* VERSION_TOO_OLD */
  98. /* if the cipher suite on line is valid store in suite and return 1, else 0 */
  99. static int IsValidCipherSuite(const char* line, char* suite)
  100. {
  101. int found = 0;
  102. int valid = 0;
  103. const char* find = "-l ";
  104. const char* begin = strstr(line, find);
  105. const char* end;
  106. suite[0] = '\0';
  107. if (begin) {
  108. begin += 3;
  109. end = XSTRSTR(begin, " ");
  110. if (end) {
  111. long len = end - begin;
  112. if (len > MAX_SUITE_SZ) {
  113. printf("suite too long!\n");
  114. return 0;
  115. }
  116. XMEMCPY(suite, begin, len);
  117. suite[len] = '\0';
  118. }
  119. else
  120. XSTRNCPY(suite, begin, MAX_SUITE_SZ);
  121. suite[MAX_SUITE_SZ] = '\0';
  122. found = 1;
  123. }
  124. /* if QSH not enabled then do not use QSH suite */
  125. #ifdef HAVE_QSH
  126. if (XSTRNCMP(suite, "QSH", 3) == 0) {
  127. if (wolfSSL_CTX_set_cipher_list(cipherSuiteCtx, suite + 4)
  128. != WOLFSSL_SUCCESS)
  129. return 0;
  130. }
  131. #endif
  132. if (found) {
  133. if (wolfSSL_CTX_set_cipher_list(cipherSuiteCtx, suite) == WOLFSSL_SUCCESS)
  134. valid = 1;
  135. }
  136. return valid;
  137. }
  138. static int IsValidCert(const char* line)
  139. {
  140. int ret = 1;
  141. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS)
  142. WOLFSSL_CTX* ctx;
  143. size_t i;
  144. const char* begin;
  145. char cert[80];
  146. begin = XSTRSTR(line, "-c ");
  147. if (begin == NULL)
  148. return 1;
  149. begin += 3;
  150. for (i = 0; i < sizeof(cert) - 1 && *begin != ' ' && *begin != '\0'; i++)
  151. cert[i] = *(begin++);
  152. cert[i] = '\0';
  153. ctx = wolfSSL_CTX_new(wolfSSLv23_server_method_ex(NULL));
  154. if (ctx == NULL)
  155. return 0;
  156. ret = wolfSSL_CTX_use_certificate_chain_file(ctx, cert) == WOLFSSL_SUCCESS;
  157. wolfSSL_CTX_free(ctx);
  158. #endif /* !NO_FILESYSTEM && !NO_CERTS */
  159. (void)line;
  160. return ret;
  161. }
  162. static int IsValidCA(const char* line)
  163. {
  164. int ret = 1;
  165. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS)
  166. WOLFSSL_CTX* ctx;
  167. size_t i;
  168. const char* begin;
  169. char cert[80];
  170. begin = XSTRSTR(line, "-A ");
  171. if (begin == NULL)
  172. return 1;
  173. begin += 3;
  174. for (i = 0; i < sizeof(cert) - 1 && *begin != ' ' && *begin != '\0'; i++)
  175. cert[i] = *(begin++);
  176. cert[i] = '\0';
  177. ctx = wolfSSL_CTX_new(wolfSSLv23_server_method_ex(NULL));
  178. if (ctx == NULL)
  179. return 0;
  180. ret = wolfSSL_CTX_use_certificate_chain_file(ctx, cert) == WOLFSSL_SUCCESS;
  181. wolfSSL_CTX_free(ctx);
  182. #endif /* !NO_FILESYSTEM && !NO_CERTS */
  183. (void)line;
  184. return ret;
  185. }
  186. static int execute_test_case(int svr_argc, char** svr_argv,
  187. int cli_argc, char** cli_argv,
  188. int addNoVerify, int addNonBlocking,
  189. int addDisableEMS, int forceSrvDefCipherList,
  190. int forceCliDefCipherList)
  191. {
  192. #ifdef WOLFSSL_TIRTOS
  193. func_args cliArgs = {0};
  194. func_args svrArgs = {0};
  195. cliArgs.argc = cli_argc;
  196. cliArgs.argv = cli_argv;
  197. svrArgs.argc = svr_argc;
  198. svrArgs.argv = svr_argv;
  199. #else
  200. func_args cliArgs = {cli_argc, cli_argv, 0, NULL, NULL};
  201. func_args svrArgs = {svr_argc, svr_argv, 0, NULL, NULL};
  202. #endif
  203. tcp_ready ready;
  204. THREAD_TYPE serverThread;
  205. char commandLine[MAX_COMMAND_SZ];
  206. char cipherSuite[MAX_SUITE_SZ+1];
  207. int i;
  208. size_t added;
  209. static int tests = 1;
  210. #if !defined(USE_WINDOWS_API) && !defined(WOLFSSL_TIRTOS)
  211. char portNumber[8];
  212. #endif
  213. int cliTestShouldFail = 0, svrTestShouldFail = 0;
  214. /* Is Valid Cipher and Version Checks */
  215. /* build command list for the Is checks below */
  216. commandLine[0] = '\0';
  217. added = 0;
  218. for (i = 0; i < svrArgs.argc; i++) {
  219. added += XSTRLEN(svr_argv[i]) + 2;
  220. if (added >= MAX_COMMAND_SZ) {
  221. printf("server command line too long\n");
  222. break;
  223. }
  224. strcat(commandLine, svr_argv[i]);
  225. strcat(commandLine, flagSep);
  226. }
  227. if (IsValidCipherSuite(commandLine, cipherSuite) == 0) {
  228. #ifdef DEBUG_SUITE_TESTS
  229. printf("cipher suite %s not supported in build\n", cipherSuite);
  230. #endif
  231. return NOT_BUILT_IN;
  232. }
  233. if (!IsValidCert(commandLine)) {
  234. #ifdef DEBUG_SUITE_TESTS
  235. printf("certificate %s not supported in build\n", commandLine);
  236. #endif
  237. return NOT_BUILT_IN;
  238. }
  239. #ifndef WOLFSSL_ALLOW_SSLV3
  240. if (IsSslVersion(commandLine) == 1) {
  241. #ifdef DEBUG_SUITE_TESTS
  242. printf("protocol version on line %s is too old\n", commandLine);
  243. #endif
  244. return VERSION_TOO_OLD;
  245. }
  246. #endif
  247. #ifndef WOLFSSL_ALLOW_TLSV10
  248. if (IsTls10Version(commandLine) == 1) {
  249. #ifdef DEBUG_SUITE_TESTS
  250. printf("protocol version on line %s is too old\n", commandLine);
  251. #endif
  252. return VERSION_TOO_OLD;
  253. }
  254. #endif
  255. #ifdef NO_OLD_TLS
  256. if (IsOldTlsVersion(commandLine) == 1) {
  257. #ifdef DEBUG_SUITE_TESTS
  258. printf("protocol version on line %s is too old\n", commandLine);
  259. #endif
  260. return VERSION_TOO_OLD;
  261. }
  262. #endif
  263. /* Build Server Command */
  264. if (addNoVerify) {
  265. printf("repeating test with client cert request off\n");
  266. if (svrArgs.argc >= MAX_ARGS)
  267. printf("server command line too long\n");
  268. else
  269. svr_argv[svrArgs.argc++] = noVerifyFlag;
  270. }
  271. if (addNonBlocking) {
  272. printf("repeating test with non blocking on\n");
  273. if (svrArgs.argc >= MAX_ARGS)
  274. printf("server command line too long\n");
  275. else
  276. svr_argv[svrArgs.argc++] = nonblockFlag;
  277. }
  278. #if !defined(USE_WINDOWS_API) && !defined(WOLFSSL_TIRTOS)
  279. /* add port */
  280. if (svrArgs.argc + 2 > MAX_ARGS)
  281. printf("cannot add the magic port number flag to server\n");
  282. else {
  283. svr_argv[svrArgs.argc++] = portFlag;
  284. svr_argv[svrArgs.argc++] = svrPort;
  285. }
  286. #endif
  287. if (forceSrvDefCipherList) {
  288. if (svrArgs.argc + 2 > MAX_ARGS)
  289. printf("cannot add the force def cipher list flag to server\n");
  290. else {
  291. svr_argv[svrArgs.argc++] = intTestFlag;
  292. svr_argv[svrArgs.argc++] = forceDefCipherListFlag;
  293. }
  294. }
  295. #ifdef TEST_PK_PRIVKEY
  296. svr_argv[svrArgs.argc++] = (char*)"-P";
  297. #endif
  298. /* update server flags list */
  299. commandLine[0] = '\0';
  300. added = 0;
  301. for (i = 0; i < svrArgs.argc; i++) {
  302. added += XSTRLEN(svr_argv[i]) + 2;
  303. if (added >= MAX_COMMAND_SZ) {
  304. printf("server command line too long\n");
  305. break;
  306. }
  307. strcat(commandLine, svr_argv[i]);
  308. strcat(commandLine, flagSep);
  309. }
  310. printf("trying server command line[%d]: %s\n", tests, commandLine);
  311. tests++; /* test count */
  312. /* determine based on args if this test is expected to fail */
  313. if (XSTRSTR(commandLine, exitWithRetFlag) != NULL) {
  314. svrTestShouldFail = 1;
  315. }
  316. InitTcpReady(&ready);
  317. #ifdef WOLFSSL_TIRTOS
  318. fdOpenSession(Task_self());
  319. #endif
  320. /* start server */
  321. svrArgs.signal = &ready;
  322. start_thread(server_test, &svrArgs, &serverThread);
  323. wait_tcp_ready(&svrArgs);
  324. /* Build Client Command */
  325. if (addNonBlocking) {
  326. if (cliArgs.argc >= MAX_ARGS)
  327. printf("cannot add the non block flag to client\n");
  328. else
  329. cli_argv[cliArgs.argc++] = nonblockFlag;
  330. }
  331. if (addDisableEMS) {
  332. printf("repeating test without extended master secret\n");
  333. if (cliArgs.argc >= MAX_ARGS)
  334. printf("cannot add the disable EMS flag to client\n");
  335. else
  336. cli_argv[cliArgs.argc++] = disableEMSFlag;
  337. }
  338. #if !defined(USE_WINDOWS_API) && !defined(WOLFSSL_TIRTOS)
  339. if (ready.port != 0) {
  340. if (cliArgs.argc + 2 > MAX_ARGS)
  341. printf("cannot add the magic port number flag to client\n");
  342. else {
  343. snprintf(portNumber, sizeof(portNumber), "%d", ready.port);
  344. cli_argv[cliArgs.argc++] = portFlag;
  345. cli_argv[cliArgs.argc++] = portNumber;
  346. }
  347. }
  348. #endif
  349. if (forceCliDefCipherList) {
  350. if (cliArgs.argc + 2 > MAX_ARGS)
  351. printf("cannot add the force def cipher list flag to client\n");
  352. else {
  353. cli_argv[cliArgs.argc++] = intTestFlag;
  354. cli_argv[cliArgs.argc++] = forceDefCipherListFlag;
  355. }
  356. }
  357. #ifdef TEST_PK_PRIVKEY
  358. cli_argv[cliArgs.argc++] = (char*)"-P";
  359. #endif
  360. commandLine[0] = '\0';
  361. added = 0;
  362. for (i = 0; i < cliArgs.argc; i++) {
  363. added += XSTRLEN(cli_argv[i]) + 2;
  364. if (added >= MAX_COMMAND_SZ) {
  365. printf("client command line too long\n");
  366. break;
  367. }
  368. strcat(commandLine, cli_argv[i]);
  369. strcat(commandLine, flagSep);
  370. }
  371. if (!IsValidCA(commandLine)) {
  372. #ifdef DEBUG_SUITE_TESTS
  373. printf("certificate %s not supported in build\n", commandLine);
  374. #endif
  375. return NOT_BUILT_IN;
  376. }
  377. printf("trying client command line[%d]: %s\n", tests, commandLine);
  378. /* determine based on args if this test is expected to fail */
  379. if (XSTRSTR(commandLine, exitWithRetFlag) != NULL) {
  380. cliTestShouldFail = 1;
  381. }
  382. /* start client */
  383. client_test(&cliArgs);
  384. /* verify results */
  385. if ((cliArgs.return_code != 0 && cliTestShouldFail == 0) ||
  386. (cliArgs.return_code == 0 && cliTestShouldFail != 0)) {
  387. printf("client_test failed\n");
  388. XEXIT(EXIT_FAILURE);
  389. }
  390. join_thread(serverThread);
  391. if ((svrArgs.return_code != 0 && svrTestShouldFail == 0) ||
  392. (svrArgs.return_code == 0 && svrTestShouldFail != 0)) {
  393. printf("server_test failed\n");
  394. XEXIT(EXIT_FAILURE);
  395. }
  396. #ifdef WOLFSSL_TIRTOS
  397. fdCloseSession(Task_self());
  398. #endif
  399. FreeTcpReady(&ready);
  400. /* only run the first test for expected failure cases */
  401. /* the example server/client are not designed to handle expected failure in
  402. all cases, such as non-blocking, etc... */
  403. if (svrTestShouldFail || cliTestShouldFail) {
  404. return NOT_BUILT_IN;
  405. }
  406. return 0;
  407. }
  408. static void test_harness(void* vargs)
  409. {
  410. func_args* args = (func_args*)vargs;
  411. char* script;
  412. long sz, len;
  413. int cliMode = 0; /* server or client command flag, server first */
  414. int ret;
  415. FILE* file;
  416. char* svrArgs[MAX_ARGS];
  417. int svrArgsSz;
  418. char* cliArgs[MAX_ARGS];
  419. int cliArgsSz;
  420. char* cursor;
  421. char* comment;
  422. const char* fname = "tests/test.conf";
  423. const char* addArgs = NULL;
  424. if (args->argc == 1) {
  425. printf("notice: using default file %s\n", fname);
  426. }
  427. else if (args->argc == 3) {
  428. addArgs = args->argv[2];
  429. }
  430. else if (args->argc > 3) {
  431. printf("usage: harness [FILE] [ARG]\n");
  432. args->return_code = 1;
  433. return;
  434. }
  435. if (args->argc >= 2) {
  436. fname = args->argv[1];
  437. }
  438. file = fopen(fname, "rb");
  439. if (file == NULL) {
  440. fprintf(stderr, "unable to open %s\n", fname);
  441. args->return_code = 1;
  442. return;
  443. }
  444. fseek(file, 0, SEEK_END);
  445. sz = ftell(file);
  446. rewind(file);
  447. if (sz <= 0) {
  448. fprintf(stderr, "%s is empty\n", fname);
  449. fclose(file);
  450. args->return_code = 1;
  451. return;
  452. }
  453. script = (char*)malloc(sz+1);
  454. if (script == 0) {
  455. fprintf(stderr, "unable to allocate script buffer\n");
  456. fclose(file);
  457. args->return_code = 1;
  458. return;
  459. }
  460. len = fread(script, 1, sz, file);
  461. if (len != sz) {
  462. fprintf(stderr, "read error\n");
  463. fclose(file);
  464. free(script);
  465. args->return_code = 1;
  466. return;
  467. }
  468. fclose(file);
  469. script[sz] = 0;
  470. cursor = script;
  471. svrArgsSz = 1;
  472. svrArgs[0] = args->argv[0];
  473. cliArgsSz = 1;
  474. cliArgs[0] = args->argv[0];
  475. while (*cursor != 0) {
  476. int do_it = 0;
  477. switch (*cursor) {
  478. case '\n':
  479. /* A blank line triggers test case execution or switches
  480. to client mode if we don't have the client command yet */
  481. if (cliMode == 0)
  482. cliMode = 1; /* switch to client mode processing */
  483. /* skip extra newlines */
  484. else
  485. do_it = 1; /* Do It, we have server and client */
  486. cursor++;
  487. break;
  488. case '#':
  489. /* Ignore lines that start with a # */
  490. comment = XSTRSEP(&cursor, "\n");
  491. #ifdef DEBUG_SUITE_TESTS
  492. printf("%s\n", comment);
  493. #else
  494. (void)comment;
  495. #endif
  496. break;
  497. case '-':
  498. default:
  499. /* Parameters start with a -. They end in either a newline
  500. * or a space. Capture until either, save in Args list. */
  501. if (cliMode)
  502. cliArgs[cliArgsSz++] = XSTRSEP(&cursor, " \n");
  503. else
  504. svrArgs[svrArgsSz++] = XSTRSEP(&cursor, " \n");
  505. if (*cursor == '\0') /* eof */
  506. do_it = 1;
  507. break;
  508. }
  509. if (svrArgsSz == MAX_ARGS || cliArgsSz == MAX_ARGS) {
  510. fprintf(stderr, "too many arguments, forcing test run\n");
  511. do_it = 1;
  512. }
  513. if (do_it) {
  514. /* additional arguments processing */
  515. if (cliArgsSz+2 < MAX_ARGS && svrArgsSz+2 < MAX_ARGS) {
  516. if (addArgs == NULL || XSTRSTR(addArgs, "doDH") == NULL) {
  517. /* The `-2` disable DH prime check is added to all tests by default */
  518. cliArgs[cliArgsSz++] = disableDHPrimeTest;
  519. svrArgs[svrArgsSz++] = disableDHPrimeTest;
  520. }
  521. if (addArgs && XSTRSTR(addArgs, "expFail")) {
  522. /* Tests should expect to fail */
  523. cliArgs[cliArgsSz++] = intTestFlag;
  524. cliArgs[cliArgsSz++] = exitWithRetFlag;
  525. svrArgs[svrArgsSz++] = intTestFlag;
  526. svrArgs[svrArgsSz++] = exitWithRetFlag;
  527. }
  528. }
  529. ret = execute_test_case(svrArgsSz, svrArgs,
  530. cliArgsSz, cliArgs, 0, 0, 0, 0, 0);
  531. /* don't repeat if not supported in build */
  532. if (ret == 0) {
  533. /* test with default cipher list on server side */
  534. execute_test_case(svrArgsSz, svrArgs,
  535. cliArgsSz, cliArgs, 0, 0, 0, 1, 0);
  536. /* test with default cipher list on client side */
  537. execute_test_case(svrArgsSz, svrArgs,
  538. cliArgsSz, cliArgs, 0, 0, 0, 0, 1);
  539. execute_test_case(svrArgsSz, svrArgs,
  540. cliArgsSz, cliArgs, 0, 1, 0, 0, 0);
  541. execute_test_case(svrArgsSz, svrArgs,
  542. cliArgsSz, cliArgs, 1, 0, 0, 0, 0);
  543. execute_test_case(svrArgsSz, svrArgs,
  544. cliArgsSz, cliArgs, 1, 1, 0, 0, 0);
  545. #ifdef HAVE_EXTENDED_MASTER
  546. execute_test_case(svrArgsSz, svrArgs,
  547. cliArgsSz, cliArgs, 0, 0, 1, 0, 0);
  548. execute_test_case(svrArgsSz, svrArgs,
  549. cliArgsSz, cliArgs, 0, 1, 1, 0, 0);
  550. execute_test_case(svrArgsSz, svrArgs,
  551. cliArgsSz, cliArgs, 1, 0, 1, 0, 0);
  552. execute_test_case(svrArgsSz, svrArgs,
  553. cliArgsSz, cliArgs, 1, 1, 1, 0, 0);
  554. #endif
  555. }
  556. svrArgsSz = 1;
  557. cliArgsSz = 1;
  558. cliMode = 0;
  559. }
  560. }
  561. free(script);
  562. args->return_code = 0;
  563. }
  564. #endif /* !NO_WOLFSSL_SERVER && !NO_WOLFSSL_CLIENT */
  565. int SuiteTest(int argc, char** argv)
  566. {
  567. #if !defined(NO_WOLFSSL_SERVER) && !defined(NO_WOLFSSL_CLIENT)
  568. func_args args;
  569. char argv0[3][80];
  570. char* myArgv[3];
  571. printf(" Begin Cipher Suite Tests\n");
  572. /* setup */
  573. myArgv[0] = argv0[0];
  574. myArgv[1] = argv0[1];
  575. myArgv[2] = argv0[2];
  576. args.argv = myArgv;
  577. strcpy(argv0[0], "SuiteTest");
  578. #ifdef WOLFSSL_STATIC_MEMORY
  579. byte memory[200000];
  580. #endif
  581. cipherSuiteCtx = wolfSSL_CTX_new(wolfSSLv23_client_method());
  582. if (cipherSuiteCtx == NULL) {
  583. printf("can't get cipher suite ctx\n");
  584. args.return_code = EXIT_FAILURE;
  585. goto exit;
  586. }
  587. /* load in static memory buffer if enabled */
  588. #ifdef WOLFSSL_STATIC_MEMORY
  589. if (wolfSSL_CTX_load_static_memory(&cipherSuiteCtx, NULL,
  590. memory, sizeof(memory), 0, 1)
  591. != WOLFSSL_SUCCESS) {
  592. printf("unable to load static memory and create ctx");
  593. args.return_code = EXIT_FAILURE;
  594. goto exit;
  595. }
  596. #endif
  597. #ifdef WOLFSSL_ASYNC_CRYPT
  598. if (wolfAsync_DevOpen(&devId) < 0) {
  599. printf("Async device open failed");
  600. args.return_code = EXIT_FAILURE;
  601. goto exit;
  602. }
  603. wolfSSL_CTX_UseAsync(cipherSuiteCtx, devId);
  604. #endif /* WOLFSSL_ASYNC_CRYPT */
  605. /* support for custom command line tests */
  606. if (argc > 1) {
  607. /* Examples:
  608. ./tests/unit.test tests/test-altchains.conf
  609. ./tests/unit.test tests/test-fails.conf expFail
  610. ./tests/unit.test tests/test-dhprime.conf doDH
  611. */
  612. args.argc = argc;
  613. args.argv = argv;
  614. test_harness(&args);
  615. if (args.return_code != 0) {
  616. printf("error from script %d\n", args.return_code);
  617. args.return_code = EXIT_FAILURE;
  618. }
  619. goto exit;
  620. }
  621. /* default case */
  622. args.argc = 1;
  623. printf("starting default cipher suite tests\n");
  624. test_harness(&args);
  625. if (args.return_code != 0) {
  626. printf("error from script %d\n", args.return_code);
  627. args.return_code = EXIT_FAILURE;
  628. goto exit;
  629. }
  630. /* any extra cases will need another argument */
  631. args.argc = 2;
  632. #ifdef WOLFSSL_OLDTLS_SHA2_CIPHERSUITES
  633. /* SHA-2 cipher suites in old TLS versions */
  634. strcpy(argv0[1], "tests/test-sha2.conf");
  635. printf("starting SHA-2 cipher suite in old TLS versions tests\n");
  636. test_harness(&args);
  637. if (args.return_code != 0) {
  638. printf("error from script %d\n", args.return_code);
  639. args.return_code = EXIT_FAILURE;
  640. goto exit;
  641. }
  642. #endif
  643. #ifdef WOLFSSL_TLS13
  644. /* add TLSv13 extra suites */
  645. strcpy(argv0[1], "tests/test-tls13.conf");
  646. printf("starting TLSv13 extra cipher suite tests\n");
  647. test_harness(&args);
  648. if (args.return_code != 0) {
  649. printf("error from script %d\n", args.return_code);
  650. args.return_code = EXIT_FAILURE;
  651. goto exit;
  652. }
  653. #ifdef HAVE_ECC
  654. /* add TLSv13 ECC extra suites */
  655. strcpy(argv0[1], "tests/test-tls13-ecc.conf");
  656. printf("starting TLSv13 ECC extra cipher suite tests\n");
  657. test_harness(&args);
  658. if (args.return_code != 0) {
  659. printf("error from script %d\n", args.return_code);
  660. args.return_code = EXIT_FAILURE;
  661. goto exit;
  662. }
  663. #endif
  664. #ifndef WOLFSSL_NO_TLS12
  665. /* add TLSv13 downgrade tets */
  666. strcpy(argv0[1], "tests/test-tls13-down.conf");
  667. printf("starting TLSv13 Downgrade extra tests\n");
  668. test_harness(&args);
  669. if (args.return_code != 0) {
  670. printf("error from script %d\n", args.return_code);
  671. args.return_code = EXIT_FAILURE;
  672. goto exit;
  673. }
  674. #endif
  675. #endif
  676. #if defined(HAVE_CURVE25519) && defined(HAVE_ED25519)
  677. /* add ED25519 certificate cipher suite tests */
  678. strcpy(argv0[1], "tests/test-ed25519.conf");
  679. printf("starting ED25519 extra cipher suite tests\n");
  680. test_harness(&args);
  681. if (args.return_code != 0) {
  682. printf("error from script %d\n", args.return_code);
  683. args.return_code = EXIT_FAILURE;
  684. goto exit;
  685. }
  686. #endif
  687. #ifdef WOLFSSL_DTLS
  688. /* add dtls extra suites */
  689. strcpy(argv0[1], "tests/test-dtls.conf");
  690. printf("starting dtls extra cipher suite tests\n");
  691. test_harness(&args);
  692. if (args.return_code != 0) {
  693. printf("error from script %d\n", args.return_code);
  694. args.return_code = EXIT_FAILURE;
  695. goto exit;
  696. }
  697. #ifdef WOLFSSL_OLDTLS_SHA2_CIPHERSUITES
  698. /* add dtls extra suites */
  699. strcpy(argv0[1], "tests/test-dtls-sha2.conf");
  700. printf("starting dtls extra cipher suite tests - old TLS sha-2 cs\n");
  701. test_harness(&args);
  702. if (args.return_code != 0) {
  703. printf("error from script %d\n", args.return_code);
  704. args.return_code = EXIT_FAILURE;
  705. goto exit;
  706. }
  707. #endif
  708. #endif
  709. #ifdef WOLFSSL_SCTP
  710. /* add dtls-sctp extra suites */
  711. strcpy(argv0[1], "tests/test-sctp.conf");
  712. printf("starting dtls-sctp extra cipher suite tests\n");
  713. test_harness(&args);
  714. if (args.return_code != 0) {
  715. printf("error from script %d\n", args.return_code);
  716. args.return_code = EXIT_FAILURE;
  717. goto exit;
  718. }
  719. #ifdef WOLFSSL_OLDTLS_SHA2_CIPHERSUITES
  720. /* add dtls-sctp extra suites */
  721. strcpy(argv0[1], "tests/test-sctp-sha2.conf");
  722. printf("starting dtls-sctp extra cipher suite tests - old TLS sha-2 cs\n");
  723. test_harness(&args);
  724. if (args.return_code != 0) {
  725. printf("error from script %d\n", args.return_code);
  726. args.return_code = EXIT_FAILURE;
  727. goto exit;
  728. }
  729. #endif
  730. #endif
  731. #ifndef WC_STRICT_SIG
  732. #if !defined(NO_RSA) && defined(HAVE_ECC) /* testing mixed ECC/RSA cert */
  733. /* add extra signature test suites */
  734. strcpy(argv0[1], "tests/test-sig.conf");
  735. printf("starting sig extra cipher suite tests\n");
  736. test_harness(&args);
  737. if (args.return_code != 0) {
  738. printf("error from script %d\n", args.return_code);
  739. args.return_code = EXIT_FAILURE;
  740. goto exit;
  741. }
  742. #endif /* HAVE_RSA and HAVE_ECC */
  743. #endif /* !WC_STRICT_SIG */
  744. #ifdef HAVE_QSH
  745. /* add QSH extra suites */
  746. strcpy(argv0[1], "tests/test-qsh.conf");
  747. printf("starting qsh extra cipher suite tests\n");
  748. test_harness(&args);
  749. if (args.return_code != 0) {
  750. printf("error from script %d\n", args.return_code);
  751. args.return_code = EXIT_FAILURE;
  752. goto exit;
  753. }
  754. #ifdef WOLFSSL_OLDTLS_SHA2_CIPHERSUITES
  755. strcpy(argv0[1], "tests/test-qsh-sha2.conf");
  756. printf("starting qsh extra cipher suite tests - old TLS sha-2 cs\n");
  757. test_harness(&args);
  758. if (args.return_code != 0) {
  759. printf("error from script %d\n", args.return_code);
  760. args.return_code = EXIT_FAILURE;
  761. goto exit;
  762. }
  763. #endif
  764. #endif
  765. #ifndef NO_PSK
  766. #ifndef WOLFSSL_NO_TLS12
  767. #if !defined(NO_RSA) || defined(HAVE_ECC)
  768. /* add psk cipher suites */
  769. strcpy(argv0[1], "tests/test-psk.conf");
  770. printf("starting psk cipher suite tests\n");
  771. test_harness(&args);
  772. if (args.return_code != 0) {
  773. printf("error from script %d\n", args.return_code);
  774. args.return_code = EXIT_FAILURE;
  775. goto exit;
  776. }
  777. #endif
  778. #endif
  779. #ifdef WOLFSSL_TLS13
  780. /* add psk extra suites */
  781. strcpy(argv0[1], "tests/test-tls13-psk.conf");
  782. printf("starting TLS 1.3 psk no identity extra cipher suite tests\n");
  783. test_harness(&args);
  784. if (args.return_code != 0) {
  785. printf("error from script %d\n", args.return_code);
  786. args.return_code = EXIT_FAILURE;
  787. goto exit;
  788. }
  789. #endif
  790. #endif
  791. #if defined(WOLFSSL_ENCRYPTED_KEYS) && !defined(NO_DES3)
  792. /* test encrypted keys */
  793. strcpy(argv0[1], "tests/test-enckeys.conf");
  794. printf("starting encrypted keys extra cipher suite tests\n");
  795. test_harness(&args);
  796. if (args.return_code != 0) {
  797. printf("error from script %d\n", args.return_code);
  798. args.return_code = EXIT_FAILURE;
  799. goto exit;
  800. }
  801. #endif
  802. #ifdef HAVE_MAX_FRAGMENT
  803. /* Max fragment cipher suite tests */
  804. strcpy(argv0[1], "tests/test-maxfrag.conf");
  805. printf("starting max fragment cipher suite tests\n");
  806. test_harness(&args);
  807. if (args.return_code != 0) {
  808. printf("error from script %d\n", args.return_code);
  809. args.return_code = EXIT_FAILURE;
  810. goto exit;
  811. }
  812. #ifdef WOLFSSL_DTLS
  813. strcpy(argv0[1], "tests/test-maxfrag-dtls.conf");
  814. printf("starting dtls max fragment cipher suite tests\n");
  815. test_harness(&args);
  816. if (args.return_code != 0) {
  817. printf("error from script %d\n", args.return_code);
  818. args.return_code = EXIT_FAILURE;
  819. goto exit;
  820. }
  821. #endif
  822. #endif
  823. #ifdef WOLFSSL_ALT_CERT_CHAINS
  824. /* tests for alt chains */
  825. strcpy(argv0[1], "tests/test-altchains.conf");
  826. printf("starting certificate alternate chain cipher suite tests\n");
  827. test_harness(&args);
  828. if (args.return_code != 0) {
  829. printf("error from script %d\n", args.return_code);
  830. args.return_code = EXIT_FAILURE;
  831. goto exit;
  832. }
  833. #else
  834. /* tests for chains */
  835. strcpy(argv0[1], "tests/test-chains.conf");
  836. printf("starting certificate chain cipher suite tests\n");
  837. test_harness(&args);
  838. if (args.return_code != 0) {
  839. printf("error from script %d\n", args.return_code);
  840. args.return_code = EXIT_FAILURE;
  841. goto exit;
  842. }
  843. #endif
  844. #ifdef WOLFSSL_TRUST_PEER_CERT
  845. /* tests for trusted peer cert */
  846. strcpy(argv0[1], "tests/test-trustpeer.conf");
  847. printf("starting trusted peer certificate cipher suite tests\n");
  848. test_harness(&args);
  849. if (args.return_code != 0) {
  850. printf("error from script %d\n", args.return_code);
  851. args.return_code = EXIT_FAILURE;
  852. goto exit;
  853. }
  854. #endif
  855. /* tests for dh prime */
  856. args.argc = 3;
  857. strcpy(argv0[1], "tests/test-dhprime.conf");
  858. strcpy(argv0[2], "doDH"); /* add DH prime flag */
  859. printf("starting tests that expect failure\n");
  860. test_harness(&args);
  861. if (args.return_code != 0) {
  862. printf("error from script %d\n", args.return_code);
  863. args.return_code = EXIT_FAILURE;
  864. goto exit;
  865. }
  866. /* failure tests */
  867. args.argc = 3;
  868. strcpy(argv0[1], "tests/test-fails.conf");
  869. strcpy(argv0[2], "expFail"); /* tests are expected to fail */
  870. printf("starting tests that expect failure\n");
  871. test_harness(&args);
  872. if (args.return_code != 0) {
  873. printf("error from script %d\n", args.return_code);
  874. args.return_code = EXIT_FAILURE;
  875. goto exit;
  876. }
  877. exit:
  878. printf(" End Cipher Suite Tests\n");
  879. wolfSSL_CTX_free(cipherSuiteCtx);
  880. wolfSSL_Cleanup();
  881. #ifdef WOLFSSL_ASYNC_CRYPT
  882. wolfAsync_DevClose(&devId);
  883. #endif
  884. return args.return_code;
  885. #else
  886. return NOT_COMPILED_IN;
  887. #endif /* !NO_WOLFSSL_SERVER && !NO_WOLFSSL_CLIENT */
  888. (void)argc;
  889. (void)argv;
  890. }