suites.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468
  1. /* suites.c
  2. *
  3. * Copyright (C) 2006-2014 wolfSSL Inc.
  4. *
  5. * This file is part of CyaSSL.
  6. *
  7. * CyaSSL 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. * CyaSSL 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-1301, USA
  20. */
  21. #ifdef HAVE_CONFIG_H
  22. #include <config.h>
  23. #endif
  24. #include <cyassl/ctaocrypt/settings.h>
  25. #include <stdlib.h>
  26. #include <stdio.h>
  27. #include <string.h>
  28. #include <cyassl/ssl.h>
  29. #include <tests/unit.h>
  30. #define MAX_ARGS 40
  31. #define MAX_COMMAND_SZ 240
  32. #define MAX_SUITE_SZ 80
  33. #define NOT_BUILT_IN -123
  34. #ifdef NO_OLD_TLS
  35. #define VERSION_TOO_OLD -124
  36. #endif
  37. #include "examples/client/client.h"
  38. #include "examples/server/server.h"
  39. static CYASSL_CTX* cipherSuiteCtx = NULL;
  40. static char nonblockFlag[] = "-N";
  41. static char noVerifyFlag[] = "-d";
  42. static char portFlag[] = "-p";
  43. static char flagSep[] = " ";
  44. static char svrPort[] = "0";
  45. #ifdef NO_OLD_TLS
  46. /* if the protocol version is less than tls 1.2 return 1, else 0 */
  47. static int IsOldTlsVersion(const char* line)
  48. {
  49. const char* find = "-v ";
  50. char* begin = strstr(line, find);
  51. if (begin) {
  52. int version = -1;
  53. begin += 3;
  54. version = atoi(begin);
  55. if (version < 3)
  56. return 1;
  57. }
  58. return 0;
  59. }
  60. #endif /* NO_OLD_TLS */
  61. /* if the cipher suite on line is valid store in suite and return 1, else 0 */
  62. static int IsValidCipherSuite(const char* line, char* suite)
  63. {
  64. int found = 0;
  65. int valid = 0;
  66. const char* find = "-l ";
  67. const char* begin = strstr(line, find);
  68. const char* end;
  69. suite[0] = '\0';
  70. if (begin) {
  71. begin += 3;
  72. end = strstr(begin, " ");
  73. if (end) {
  74. long len = end - begin;
  75. if (len > MAX_SUITE_SZ) {
  76. printf("suite too long!\n");
  77. return 0;
  78. }
  79. memcpy(suite, begin, len);
  80. suite[len] = '\0';
  81. }
  82. else
  83. strncpy(suite, begin, MAX_SUITE_SZ);
  84. suite[MAX_SUITE_SZ] = '\0';
  85. found = 1;
  86. }
  87. if (found) {
  88. if (CyaSSL_CTX_set_cipher_list(cipherSuiteCtx, suite) == SSL_SUCCESS)
  89. valid = 1;
  90. }
  91. return valid;
  92. }
  93. static int execute_test_case(int svr_argc, char** svr_argv,
  94. int cli_argc, char** cli_argv,
  95. int addNoVerify, int addNonBlocking)
  96. {
  97. #ifdef CYASSL_TIRTOS
  98. func_args cliArgs = {0};
  99. func_args svrArgs = {0};
  100. cliArgs.argc = cli_argc;
  101. cliArgs.argv = cli_argv;
  102. svrArgs.argc = svr_argc;
  103. svrArgs.argv = svr_argv;
  104. #else
  105. func_args cliArgs = {cli_argc, cli_argv, 0, NULL, NULL};
  106. func_args svrArgs = {svr_argc, svr_argv, 0, NULL, NULL};
  107. #endif
  108. tcp_ready ready;
  109. THREAD_TYPE serverThread;
  110. char commandLine[MAX_COMMAND_SZ];
  111. char cipherSuite[MAX_SUITE_SZ+1];
  112. int i;
  113. size_t added = 0;
  114. static int tests = 1;
  115. commandLine[0] = '\0';
  116. for (i = 0; i < svr_argc; i++) {
  117. added += strlen(svr_argv[i]) + 2;
  118. if (added >= MAX_COMMAND_SZ) {
  119. printf("server command line too long\n");
  120. break;
  121. }
  122. strcat(commandLine, svr_argv[i]);
  123. strcat(commandLine, flagSep);
  124. }
  125. if (IsValidCipherSuite(commandLine, cipherSuite) == 0) {
  126. #ifdef DEBUG_SUITE_TESTS
  127. printf("cipher suite %s not supported in build\n", cipherSuite);
  128. #endif
  129. return NOT_BUILT_IN;
  130. }
  131. #ifdef NO_OLD_TLS
  132. if (IsOldTlsVersion(commandLine) == 1) {
  133. #ifdef DEBUG_SUITE_TESTS
  134. printf("protocol version on line %s is too old\n", commandLine);
  135. #endif
  136. return VERSION_TOO_OLD;
  137. }
  138. #endif
  139. if (addNoVerify) {
  140. printf("repeating test with client cert request off\n");
  141. added += 4; /* -d plus space plus terminator */
  142. if (added >= MAX_COMMAND_SZ || svr_argc >= MAX_ARGS)
  143. printf("server command line too long\n");
  144. else {
  145. svr_argv[svr_argc++] = noVerifyFlag;
  146. svrArgs.argc = svr_argc;
  147. strcat(commandLine, noVerifyFlag);
  148. strcat(commandLine, flagSep);
  149. }
  150. }
  151. if (addNonBlocking) {
  152. printf("repeating test with non blocking on\n");
  153. added += 4; /* -N plus terminator */
  154. if (added >= MAX_COMMAND_SZ || svr_argc >= MAX_ARGS)
  155. printf("server command line too long\n");
  156. else {
  157. svr_argv[svr_argc++] = nonblockFlag;
  158. svrArgs.argc = svr_argc;
  159. strcat(commandLine, nonblockFlag);
  160. strcat(commandLine, flagSep);
  161. }
  162. }
  163. #if !defined(USE_WINDOWS_API) && !defined(CYASSL_TIRTOS)
  164. /* add port 0 */
  165. if (svr_argc + 2 > MAX_ARGS)
  166. printf("cannot add the magic port number flag to server\n");
  167. else
  168. {
  169. svr_argv[svr_argc++] = portFlag;
  170. svr_argv[svr_argc++] = svrPort;
  171. svrArgs.argc = svr_argc;
  172. }
  173. #endif
  174. printf("trying server command line[%d]: %s\n", tests, commandLine);
  175. commandLine[0] = '\0';
  176. added = 0;
  177. for (i = 0; i < cli_argc; i++) {
  178. added += strlen(cli_argv[i]) + 2;
  179. if (added >= MAX_COMMAND_SZ) {
  180. printf("client command line too long\n");
  181. break;
  182. }
  183. strcat(commandLine, cli_argv[i]);
  184. strcat(commandLine, flagSep);
  185. }
  186. if (addNonBlocking) {
  187. added += 4; /* -N plus space plus terminator */
  188. if (added >= MAX_COMMAND_SZ)
  189. printf("client command line too long\n");
  190. else {
  191. cli_argv[cli_argc++] = nonblockFlag;
  192. strcat(commandLine, nonblockFlag);
  193. strcat(commandLine, flagSep);
  194. cliArgs.argc = cli_argc;
  195. }
  196. }
  197. printf("trying client command line[%d]: %s\n", tests++, commandLine);
  198. InitTcpReady(&ready);
  199. #ifdef CYASSL_TIRTOS
  200. fdOpenSession(Task_self());
  201. #endif
  202. /* start server */
  203. svrArgs.signal = &ready;
  204. start_thread(server_test, &svrArgs, &serverThread);
  205. wait_tcp_ready(&svrArgs);
  206. #if !defined(USE_WINDOWS_API) && !defined(CYASSL_TIRTOS)
  207. if (ready.port != 0)
  208. {
  209. if (cli_argc + 2 > MAX_ARGS)
  210. printf("cannot add the magic port number flag to client\n");
  211. else {
  212. char portNumber[8];
  213. snprintf(portNumber, sizeof(portNumber), "%d", ready.port);
  214. cli_argv[cli_argc++] = portFlag;
  215. cli_argv[cli_argc++] = portNumber;
  216. cliArgs.argc = cli_argc;
  217. }
  218. }
  219. #endif
  220. /* start client */
  221. client_test(&cliArgs);
  222. /* verify results */
  223. if (cliArgs.return_code != 0) {
  224. printf("client_test failed\n");
  225. exit(EXIT_FAILURE);
  226. }
  227. join_thread(serverThread);
  228. if (svrArgs.return_code != 0) {
  229. printf("server_test failed\n");
  230. exit(EXIT_FAILURE);
  231. }
  232. #ifdef CYASSL_TIRTOS
  233. fdCloseSession(Task_self());
  234. #endif
  235. FreeTcpReady(&ready);
  236. return 0;
  237. }
  238. static void test_harness(void* vargs)
  239. {
  240. func_args* args = (func_args*)vargs;
  241. char* script;
  242. long sz, len;
  243. int cliMode = 0; /* server or client command flag, server first */
  244. int ret;
  245. FILE* file;
  246. char* svrArgs[MAX_ARGS];
  247. int svrArgsSz;
  248. char* cliArgs[MAX_ARGS];
  249. int cliArgsSz;
  250. char* cursor;
  251. char* comment;
  252. const char* fname = "tests/test.conf";
  253. if (args->argc == 1) {
  254. printf("notice: using default file %s\n", fname);
  255. }
  256. else if(args->argc != 2) {
  257. printf("usage: harness [FILE]\n");
  258. args->return_code = 1;
  259. return;
  260. }
  261. else {
  262. fname = args->argv[1];
  263. }
  264. file = fopen(fname, "r");
  265. if (file == NULL) {
  266. fprintf(stderr, "unable to open %s\n", fname);
  267. args->return_code = 1;
  268. return;
  269. }
  270. fseek(file, 0, SEEK_END);
  271. sz = ftell(file);
  272. rewind(file);
  273. if (sz <= 0) {
  274. fprintf(stderr, "%s is empty\n", fname);
  275. fclose(file);
  276. args->return_code = 1;
  277. return;
  278. }
  279. script = (char*)malloc(sz+1);
  280. if (script == 0) {
  281. fprintf(stderr, "unable to allocte script buffer\n");
  282. fclose(file);
  283. args->return_code = 1;
  284. return;
  285. }
  286. len = fread(script, 1, sz, file);
  287. if (len != sz) {
  288. fprintf(stderr, "read error\n");
  289. fclose(file);
  290. free(script);
  291. args->return_code = 1;
  292. return;
  293. }
  294. fclose(file);
  295. script[sz] = 0;
  296. cursor = script;
  297. svrArgsSz = 1;
  298. svrArgs[0] = args->argv[0];
  299. cliArgsSz = 1;
  300. cliArgs[0] = args->argv[0];
  301. while (*cursor != 0) {
  302. int do_it = 0;
  303. switch (*cursor) {
  304. case '\n':
  305. /* A blank line triggers test case execution or switches
  306. to client mode if we don't have the client command yet */
  307. if (cliMode == 0)
  308. cliMode = 1; /* switch to client mode processing */
  309. else
  310. do_it = 1; /* Do It, we have server and client */
  311. cursor++;
  312. break;
  313. case '#':
  314. /* Ignore lines that start with a #. */
  315. comment = strsep(&cursor, "\n");
  316. #ifdef DEBUG_SUITE_TESTS
  317. printf("%s\n", comment);
  318. #else
  319. (void)comment;
  320. #endif
  321. break;
  322. case '-':
  323. /* Parameters start with a -. They end in either a newline
  324. * or a space. Capture until either, save in Args list. */
  325. if (cliMode)
  326. cliArgs[cliArgsSz++] = strsep(&cursor, " \n");
  327. else
  328. svrArgs[svrArgsSz++] = strsep(&cursor, " \n");
  329. break;
  330. default:
  331. /* Anything from cursor until end of line that isn't the above
  332. * is data for a paramter. Just up until the next newline in
  333. * the Args list. */
  334. if (cliMode)
  335. cliArgs[cliArgsSz++] = strsep(&cursor, "\n");
  336. else
  337. svrArgs[svrArgsSz++] = strsep(&cursor, "\n");
  338. if (*cursor == 0) /* eof */
  339. do_it = 1;
  340. }
  341. if (svrArgsSz == MAX_ARGS || cliArgsSz == MAX_ARGS) {
  342. fprintf(stderr, "too many arguments, forcing test run\n");
  343. do_it = 1;
  344. }
  345. if (do_it) {
  346. ret = execute_test_case(svrArgsSz, svrArgs, cliArgsSz, cliArgs,0,0);
  347. /* don't repeat if not supported in build */
  348. if (ret == 0) {
  349. execute_test_case(svrArgsSz, svrArgs, cliArgsSz, cliArgs, 0, 1);
  350. execute_test_case(svrArgsSz, svrArgs, cliArgsSz, cliArgs, 1, 0);
  351. execute_test_case(svrArgsSz, svrArgs, cliArgsSz, cliArgs, 1, 1);
  352. }
  353. svrArgsSz = 1;
  354. cliArgsSz = 1;
  355. cliMode = 0;
  356. }
  357. }
  358. free(script);
  359. args->return_code = 0;
  360. }
  361. int SuiteTest(void)
  362. {
  363. func_args args;
  364. char argv0[2][80];
  365. char* myArgv[2];
  366. printf(" Begin Cipher Suite Tests\n");
  367. /* setup */
  368. myArgv[0] = argv0[0];
  369. myArgv[1] = argv0[1];
  370. args.argv = myArgv;
  371. strcpy(argv0[0], "SuiteTest");
  372. (void)test_harness;
  373. cipherSuiteCtx = CyaSSL_CTX_new(CyaTLSv1_2_client_method());
  374. if (cipherSuiteCtx == NULL) {
  375. printf("can't get cipher suite ctx\n");
  376. exit(EXIT_FAILURE);
  377. }
  378. /* default case */
  379. args.argc = 1;
  380. printf("starting default cipher suite tests\n");
  381. test_harness(&args);
  382. if (args.return_code != 0) {
  383. printf("error from script %d\n", args.return_code);
  384. exit(EXIT_FAILURE);
  385. }
  386. /* any extra cases will need another argument */
  387. args.argc = 2;
  388. #ifdef CYASSL_DTLS
  389. /* add dtls extra suites */
  390. strcpy(argv0[1], "tests/test-dtls.conf");
  391. printf("starting dtls extra cipher suite tests\n");
  392. test_harness(&args);
  393. if (args.return_code != 0) {
  394. printf("error from script %d\n", args.return_code);
  395. exit(EXIT_FAILURE);
  396. }
  397. #endif
  398. printf(" End Cipher Suite Tests\n");
  399. CyaSSL_CTX_free(cipherSuiteCtx);
  400. CyaSSL_Cleanup();
  401. return args.return_code;
  402. }