suites.c 33 KB

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