suites.c 31 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031
  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. /* determine based on args if this test is expected to fail */
  404. if (XSTRSTR(commandLine, exitWithRetFlag) != NULL) {
  405. cliTestShouldFail = 1;
  406. }
  407. /* start client */
  408. client_test(&cliArgs);
  409. /* verify results */
  410. if ((cliArgs.return_code != 0 && cliTestShouldFail == 0) ||
  411. (cliArgs.return_code == 0 && cliTestShouldFail != 0)) {
  412. printf("client_test failed\n");
  413. XEXIT(EXIT_FAILURE);
  414. }
  415. join_thread(serverThread);
  416. if ((svrArgs.return_code != 0 && svrTestShouldFail == 0) ||
  417. (svrArgs.return_code == 0 && svrTestShouldFail != 0)) {
  418. printf("server_test failed\n");
  419. XEXIT(EXIT_FAILURE);
  420. }
  421. #ifdef WOLFSSL_TIRTOS
  422. fdCloseSession(Task_self());
  423. #endif
  424. FreeTcpReady(&ready);
  425. /* only run the first test for expected failure cases */
  426. /* the example server/client are not designed to handle expected failure in
  427. all cases, such as non-blocking, etc... */
  428. if (svrTestShouldFail || cliTestShouldFail) {
  429. return NOT_BUILT_IN;
  430. }
  431. return 0;
  432. }
  433. static void test_harness(void* vargs)
  434. {
  435. func_args* args = (func_args*)vargs;
  436. char* script;
  437. long sz, len;
  438. int cliMode = 0; /* server or client command flag, server first */
  439. int ret;
  440. FILE* file;
  441. char* svrArgs[MAX_ARGS];
  442. int svrArgsSz;
  443. char* cliArgs[MAX_ARGS];
  444. int cliArgsSz;
  445. char* cursor;
  446. char* comment;
  447. const char* fname = "tests/test.conf";
  448. const char* addArgs = NULL;
  449. if (args->argc == 1) {
  450. printf("notice: using default file %s\n", fname);
  451. }
  452. else if (args->argc == 3) {
  453. addArgs = args->argv[2];
  454. }
  455. else if (args->argc > 3) {
  456. printf("usage: harness [FILE] [ARG]\n");
  457. args->return_code = 1;
  458. return;
  459. }
  460. if (args->argc >= 2) {
  461. fname = args->argv[1];
  462. }
  463. file = fopen(fname, "rb");
  464. if (file == NULL) {
  465. fprintf(stderr, "unable to open %s\n", fname);
  466. args->return_code = 1;
  467. return;
  468. }
  469. fseek(file, 0, SEEK_END);
  470. sz = ftell(file);
  471. rewind(file);
  472. if (sz <= 0) {
  473. fprintf(stderr, "%s is empty\n", fname);
  474. fclose(file);
  475. args->return_code = 1;
  476. return;
  477. }
  478. script = (char*)malloc(sz+1);
  479. if (script == 0) {
  480. fprintf(stderr, "unable to allocate script buffer\n");
  481. fclose(file);
  482. args->return_code = 1;
  483. return;
  484. }
  485. len = fread(script, 1, sz, file);
  486. if (len != sz) {
  487. fprintf(stderr, "read error\n");
  488. fclose(file);
  489. free(script);
  490. args->return_code = 1;
  491. return;
  492. }
  493. fclose(file);
  494. script[sz] = 0;
  495. cursor = script;
  496. svrArgsSz = 1;
  497. svrArgs[0] = args->argv[0];
  498. cliArgsSz = 1;
  499. cliArgs[0] = args->argv[0];
  500. while (*cursor != 0) {
  501. int do_it = 0;
  502. switch (*cursor) {
  503. case '\n':
  504. /* A blank line triggers test case execution or switches
  505. to client mode if we don't have the client command yet */
  506. if (cliMode == 0)
  507. cliMode = 1; /* switch to client mode processing */
  508. /* skip extra newlines */
  509. else
  510. do_it = 1; /* Do It, we have server and client */
  511. cursor++;
  512. break;
  513. case '#':
  514. /* Ignore lines that start with a # */
  515. comment = XSTRSEP(&cursor, "\n");
  516. #ifdef DEBUG_SUITE_TESTS
  517. printf("%s\n", comment);
  518. #else
  519. (void)comment;
  520. #endif
  521. break;
  522. case '-':
  523. default:
  524. /* Parameters start with a -. They end in either a newline
  525. * or a space. Capture until either, save in Args list. */
  526. if (cliMode)
  527. cliArgs[cliArgsSz++] = XSTRSEP(&cursor, " \n");
  528. else
  529. svrArgs[svrArgsSz++] = XSTRSEP(&cursor, " \n");
  530. if (*cursor == '\0') /* eof */
  531. do_it = 1;
  532. break;
  533. }
  534. if (svrArgsSz == MAX_ARGS || cliArgsSz == MAX_ARGS) {
  535. fprintf(stderr, "too many arguments, forcing test run\n");
  536. do_it = 1;
  537. }
  538. if (do_it) {
  539. /* additional arguments processing */
  540. if (cliArgsSz+2 < MAX_ARGS && svrArgsSz+2 < MAX_ARGS) {
  541. if (addArgs == NULL || XSTRSTR(addArgs, "doDH") == NULL) {
  542. /* The `-2` disable DH prime check is added to all tests by default */
  543. cliArgs[cliArgsSz++] = disableDHPrimeTest;
  544. svrArgs[svrArgsSz++] = disableDHPrimeTest;
  545. }
  546. if (addArgs && XSTRSTR(addArgs, "expFail")) {
  547. /* Tests should expect to fail */
  548. cliArgs[cliArgsSz++] = intTestFlag;
  549. cliArgs[cliArgsSz++] = exitWithRetFlag;
  550. svrArgs[svrArgsSz++] = intTestFlag;
  551. svrArgs[svrArgsSz++] = exitWithRetFlag;
  552. }
  553. }
  554. ret = execute_test_case(svrArgsSz, svrArgs,
  555. cliArgsSz, cliArgs, 0, 0, 0, 0, 0);
  556. /* don't repeat if not supported in build */
  557. if (ret == 0) {
  558. /* test with default cipher list on server side */
  559. execute_test_case(svrArgsSz, svrArgs,
  560. cliArgsSz, cliArgs, 0, 0, 0, 1, 0);
  561. /* test with default cipher list on client side */
  562. execute_test_case(svrArgsSz, svrArgs,
  563. cliArgsSz, cliArgs, 0, 0, 0, 0, 1);
  564. execute_test_case(svrArgsSz, svrArgs,
  565. cliArgsSz, cliArgs, 0, 1, 0, 0, 0);
  566. execute_test_case(svrArgsSz, svrArgs,
  567. cliArgsSz, cliArgs, 1, 0, 0, 0, 0);
  568. execute_test_case(svrArgsSz, svrArgs,
  569. cliArgsSz, cliArgs, 1, 1, 0, 0, 0);
  570. #ifdef HAVE_EXTENDED_MASTER
  571. execute_test_case(svrArgsSz, svrArgs,
  572. cliArgsSz, cliArgs, 0, 0, 1, 0, 0);
  573. execute_test_case(svrArgsSz, svrArgs,
  574. cliArgsSz, cliArgs, 0, 1, 1, 0, 0);
  575. execute_test_case(svrArgsSz, svrArgs,
  576. cliArgsSz, cliArgs, 1, 0, 1, 0, 0);
  577. execute_test_case(svrArgsSz, svrArgs,
  578. cliArgsSz, cliArgs, 1, 1, 1, 0, 0);
  579. #endif
  580. }
  581. svrArgsSz = 1;
  582. cliArgsSz = 1;
  583. cliMode = 0;
  584. }
  585. }
  586. free(script);
  587. args->return_code = 0;
  588. }
  589. #endif /* !NO_WOLFSSL_SERVER && !NO_WOLFSSL_CLIENT */
  590. int SuiteTest(int argc, char** argv)
  591. {
  592. #if !defined(NO_WOLFSSL_SERVER) && !defined(NO_WOLFSSL_CLIENT)
  593. func_args args;
  594. char argv0[3][80];
  595. char* myArgv[3];
  596. printf(" Begin Cipher Suite Tests\n");
  597. /* setup */
  598. myArgv[0] = argv0[0];
  599. myArgv[1] = argv0[1];
  600. myArgv[2] = argv0[2];
  601. args.argv = myArgv;
  602. strcpy(argv0[0], "SuiteTest");
  603. #ifdef WOLFSSL_STATIC_MEMORY
  604. byte memory[200000];
  605. #endif
  606. cipherSuiteCtx = wolfSSL_CTX_new(wolfSSLv23_client_method());
  607. if (cipherSuiteCtx == NULL) {
  608. printf("can't get cipher suite ctx\n");
  609. args.return_code = EXIT_FAILURE;
  610. goto exit;
  611. }
  612. /* load in static memory buffer if enabled */
  613. #ifdef WOLFSSL_STATIC_MEMORY
  614. if (wolfSSL_CTX_load_static_memory(&cipherSuiteCtx, NULL,
  615. memory, sizeof(memory), 0, 1)
  616. != WOLFSSL_SUCCESS) {
  617. printf("unable to load static memory and create ctx");
  618. args.return_code = EXIT_FAILURE;
  619. goto exit;
  620. }
  621. #endif
  622. #ifdef WOLFSSL_ASYNC_CRYPT
  623. if (wolfAsync_DevOpen(&devId) < 0) {
  624. printf("Async device open failed");
  625. args.return_code = EXIT_FAILURE;
  626. goto exit;
  627. }
  628. wolfSSL_CTX_UseAsync(cipherSuiteCtx, devId);
  629. #endif /* WOLFSSL_ASYNC_CRYPT */
  630. /* support for custom command line tests */
  631. if (argc > 1) {
  632. /* Examples:
  633. ./tests/unit.test tests/test-altchains.conf
  634. ./tests/unit.test tests/test-fails.conf expFail
  635. ./tests/unit.test tests/test-dhprime.conf doDH
  636. */
  637. args.argc = argc;
  638. args.argv = argv;
  639. test_harness(&args);
  640. if (args.return_code != 0) {
  641. printf("error from script %d\n", args.return_code);
  642. args.return_code = EXIT_FAILURE;
  643. }
  644. goto exit;
  645. }
  646. /* default case */
  647. args.argc = 1;
  648. printf("starting default cipher suite tests\n");
  649. test_harness(&args);
  650. if (args.return_code != 0) {
  651. printf("error from script %d\n", args.return_code);
  652. args.return_code = EXIT_FAILURE;
  653. goto exit;
  654. }
  655. /* any extra cases will need another argument */
  656. args.argc = 2;
  657. #ifdef WOLFSSL_OLDTLS_SHA2_CIPHERSUITES
  658. /* SHA-2 cipher suites in old TLS versions */
  659. strcpy(argv0[1], "tests/test-sha2.conf");
  660. printf("starting SHA-2 cipher suite in old TLS versions tests\n");
  661. test_harness(&args);
  662. if (args.return_code != 0) {
  663. printf("error from script %d\n", args.return_code);
  664. args.return_code = EXIT_FAILURE;
  665. goto exit;
  666. }
  667. #endif
  668. #ifdef WOLFSSL_TLS13
  669. /* add TLSv13 extra suites */
  670. strcpy(argv0[1], "tests/test-tls13.conf");
  671. printf("starting TLSv13 extra cipher suite tests\n");
  672. test_harness(&args);
  673. if (args.return_code != 0) {
  674. printf("error from script %d\n", args.return_code);
  675. args.return_code = EXIT_FAILURE;
  676. goto exit;
  677. }
  678. #ifdef HAVE_ECC
  679. /* add TLSv13 ECC extra suites */
  680. strcpy(argv0[1], "tests/test-tls13-ecc.conf");
  681. printf("starting TLSv13 ECC extra cipher suite tests\n");
  682. test_harness(&args);
  683. if (args.return_code != 0) {
  684. printf("error from script %d\n", args.return_code);
  685. args.return_code = EXIT_FAILURE;
  686. goto exit;
  687. }
  688. #endif
  689. #ifndef WOLFSSL_NO_TLS12
  690. /* add TLSv13 downgrade tets */
  691. strcpy(argv0[1], "tests/test-tls13-down.conf");
  692. printf("starting TLSv13 Downgrade extra tests\n");
  693. test_harness(&args);
  694. if (args.return_code != 0) {
  695. printf("error from script %d\n", args.return_code);
  696. args.return_code = EXIT_FAILURE;
  697. goto exit;
  698. }
  699. #endif
  700. #endif
  701. #if defined(HAVE_CURVE25519) && defined(HAVE_ED25519)
  702. /* add ED25519 certificate cipher suite tests */
  703. strcpy(argv0[1], "tests/test-ed25519.conf");
  704. printf("starting ED25519 extra cipher suite tests\n");
  705. test_harness(&args);
  706. if (args.return_code != 0) {
  707. printf("error from script %d\n", args.return_code);
  708. args.return_code = EXIT_FAILURE;
  709. goto exit;
  710. }
  711. #endif
  712. #if defined(HAVE_CURVE448) && defined(HAVE_ED448)
  713. /* add ED448 certificate cipher suite tests */
  714. strcpy(argv0[1], "tests/test-ed448.conf");
  715. printf("starting ED448 extra cipher suite tests\n");
  716. test_harness(&args);
  717. if (args.return_code != 0) {
  718. printf("error from script %d\n", args.return_code);
  719. args.return_code = EXIT_FAILURE;
  720. goto exit;
  721. }
  722. #endif
  723. #ifdef WOLFSSL_DTLS
  724. /* add dtls extra suites */
  725. strcpy(argv0[1], "tests/test-dtls.conf");
  726. printf("starting dtls extra cipher suite tests\n");
  727. test_harness(&args);
  728. if (args.return_code != 0) {
  729. printf("error from script %d\n", args.return_code);
  730. args.return_code = EXIT_FAILURE;
  731. goto exit;
  732. }
  733. #ifdef WOLFSSL_OLDTLS_SHA2_CIPHERSUITES
  734. /* add dtls extra suites */
  735. strcpy(argv0[1], "tests/test-dtls-sha2.conf");
  736. printf("starting dtls extra cipher suite tests - old TLS sha-2 cs\n");
  737. test_harness(&args);
  738. if (args.return_code != 0) {
  739. printf("error from script %d\n", args.return_code);
  740. args.return_code = EXIT_FAILURE;
  741. goto exit;
  742. }
  743. #endif
  744. #endif
  745. #ifdef WOLFSSL_SCTP
  746. /* add dtls-sctp extra suites */
  747. strcpy(argv0[1], "tests/test-sctp.conf");
  748. printf("starting dtls-sctp extra cipher suite tests\n");
  749. test_harness(&args);
  750. if (args.return_code != 0) {
  751. printf("error from script %d\n", args.return_code);
  752. args.return_code = EXIT_FAILURE;
  753. goto exit;
  754. }
  755. #ifdef WOLFSSL_OLDTLS_SHA2_CIPHERSUITES
  756. /* add dtls-sctp extra suites */
  757. strcpy(argv0[1], "tests/test-sctp-sha2.conf");
  758. printf("starting dtls-sctp extra cipher suite tests - old TLS sha-2 cs\n");
  759. test_harness(&args);
  760. if (args.return_code != 0) {
  761. printf("error from script %d\n", args.return_code);
  762. args.return_code = EXIT_FAILURE;
  763. goto exit;
  764. }
  765. #endif
  766. #endif
  767. #ifndef WC_STRICT_SIG
  768. #if !defined(NO_RSA) && defined(HAVE_ECC) /* testing mixed ECC/RSA cert */
  769. /* add extra signature test suites */
  770. strcpy(argv0[1], "tests/test-sig.conf");
  771. printf("starting sig extra cipher suite tests\n");
  772. test_harness(&args);
  773. if (args.return_code != 0) {
  774. printf("error from script %d\n", args.return_code);
  775. args.return_code = EXIT_FAILURE;
  776. goto exit;
  777. }
  778. #endif /* HAVE_RSA and HAVE_ECC */
  779. #endif /* !WC_STRICT_SIG */
  780. #ifdef HAVE_QSH
  781. /* add QSH extra suites */
  782. strcpy(argv0[1], "tests/test-qsh.conf");
  783. printf("starting qsh extra cipher suite tests\n");
  784. test_harness(&args);
  785. if (args.return_code != 0) {
  786. printf("error from script %d\n", args.return_code);
  787. args.return_code = EXIT_FAILURE;
  788. goto exit;
  789. }
  790. #ifdef WOLFSSL_OLDTLS_SHA2_CIPHERSUITES
  791. strcpy(argv0[1], "tests/test-qsh-sha2.conf");
  792. printf("starting qsh extra cipher suite tests - old TLS sha-2 cs\n");
  793. test_harness(&args);
  794. if (args.return_code != 0) {
  795. printf("error from script %d\n", args.return_code);
  796. args.return_code = EXIT_FAILURE;
  797. goto exit;
  798. }
  799. #endif
  800. #endif
  801. #ifndef NO_PSK
  802. #ifndef WOLFSSL_NO_TLS12
  803. #if !defined(NO_RSA) || defined(HAVE_ECC)
  804. /* add psk cipher suites */
  805. strcpy(argv0[1], "tests/test-psk.conf");
  806. printf("starting psk cipher suite tests\n");
  807. test_harness(&args);
  808. if (args.return_code != 0) {
  809. printf("error from script %d\n", args.return_code);
  810. args.return_code = EXIT_FAILURE;
  811. goto exit;
  812. }
  813. #endif
  814. #endif
  815. #ifdef WOLFSSL_TLS13
  816. /* add psk extra suites */
  817. strcpy(argv0[1], "tests/test-tls13-psk.conf");
  818. printf("starting TLS 1.3 psk no identity extra cipher suite tests\n");
  819. test_harness(&args);
  820. if (args.return_code != 0) {
  821. printf("error from script %d\n", args.return_code);
  822. args.return_code = EXIT_FAILURE;
  823. goto exit;
  824. }
  825. #endif
  826. #endif
  827. #if defined(WOLFSSL_ENCRYPTED_KEYS) && !defined(NO_DES3) && !defined(NO_MD5) &&\
  828. !defined(NO_SHA)
  829. /* test encrypted keys */
  830. strcpy(argv0[1], "tests/test-enckeys.conf");
  831. printf("starting encrypted keys extra cipher suite tests\n");
  832. test_harness(&args);
  833. if (args.return_code != 0) {
  834. printf("error from script %d\n", args.return_code);
  835. args.return_code = EXIT_FAILURE;
  836. goto exit;
  837. }
  838. #endif
  839. #ifdef HAVE_MAX_FRAGMENT
  840. /* Max fragment cipher suite tests */
  841. strcpy(argv0[1], "tests/test-maxfrag.conf");
  842. printf("starting max fragment cipher suite tests\n");
  843. test_harness(&args);
  844. if (args.return_code != 0) {
  845. printf("error from script %d\n", args.return_code);
  846. args.return_code = EXIT_FAILURE;
  847. goto exit;
  848. }
  849. #ifdef WOLFSSL_DTLS
  850. strcpy(argv0[1], "tests/test-maxfrag-dtls.conf");
  851. printf("starting dtls max fragment cipher suite tests\n");
  852. test_harness(&args);
  853. if (args.return_code != 0) {
  854. printf("error from script %d\n", args.return_code);
  855. args.return_code = EXIT_FAILURE;
  856. goto exit;
  857. }
  858. #endif
  859. #endif
  860. #ifdef WOLFSSL_ALT_CERT_CHAINS
  861. /* tests for alt chains */
  862. strcpy(argv0[1], "tests/test-altchains.conf");
  863. printf("starting certificate alternate chain cipher suite tests\n");
  864. test_harness(&args);
  865. if (args.return_code != 0) {
  866. printf("error from script %d\n", args.return_code);
  867. args.return_code = EXIT_FAILURE;
  868. goto exit;
  869. }
  870. #else
  871. /* tests for chains */
  872. strcpy(argv0[1], "tests/test-chains.conf");
  873. printf("starting certificate chain cipher suite tests\n");
  874. test_harness(&args);
  875. if (args.return_code != 0) {
  876. printf("error from script %d\n", args.return_code);
  877. args.return_code = EXIT_FAILURE;
  878. goto exit;
  879. }
  880. #endif
  881. #ifdef WOLFSSL_TRUST_PEER_CERT
  882. /* tests for trusted peer cert */
  883. strcpy(argv0[1], "tests/test-trustpeer.conf");
  884. printf("starting trusted peer certificate 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. /* tests for dh prime */
  893. args.argc = 3;
  894. strcpy(argv0[1], "tests/test-dhprime.conf");
  895. strcpy(argv0[2], "doDH"); /* add DH prime flag */
  896. printf("starting tests that expect failure\n");
  897. test_harness(&args);
  898. if (args.return_code != 0) {
  899. printf("error from script %d\n", args.return_code);
  900. args.return_code = EXIT_FAILURE;
  901. goto exit;
  902. }
  903. /* failure tests */
  904. args.argc = 3;
  905. strcpy(argv0[1], "tests/test-fails.conf");
  906. strcpy(argv0[2], "expFail"); /* tests are expected to fail */
  907. printf("starting tests that expect failure\n");
  908. test_harness(&args);
  909. if (args.return_code != 0) {
  910. printf("error from script %d\n", args.return_code);
  911. args.return_code = EXIT_FAILURE;
  912. goto exit;
  913. }
  914. exit:
  915. printf(" End Cipher Suite Tests\n");
  916. wolfSSL_CTX_free(cipherSuiteCtx);
  917. wolfSSL_Cleanup();
  918. #if defined(HAVE_ECC) && defined(FP_ECC) && defined(HAVE_THREAD_LS) \
  919. && (defined(NO_MAIN_DRIVER) || defined(HAVE_STACK_SIZE))
  920. wc_ecc_fp_free(); /* free per thread cache */
  921. #endif
  922. #ifdef WOLFSSL_ASYNC_CRYPT
  923. wolfAsync_DevClose(&devId);
  924. #endif
  925. return args.return_code;
  926. #else
  927. return NOT_COMPILED_IN;
  928. #endif /* !NO_WOLFSSL_SERVER && !NO_WOLFSSL_CLIENT */
  929. (void)argc;
  930. (void)argv;
  931. }