shell.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580
  1. /*shell.c
  2. *
  3. * Copyright (C) 2006-2023 wolfSSL Inc.
  4. *
  5. * This file is part of wolfSSL.
  6. *
  7. * wolfSSL is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * wolfSSL is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA
  20. */
  21. /*** tiny Shell for wolfSSL apps ***/
  22. #ifdef HAVE_CONFIG_H
  23. #include <config.h>
  24. #endif
  25. #include <wolfssl/wolfcrypt/logging.h>
  26. #include <stdio.h>
  27. #if defined(WOLFSSL_MDK_ARM)
  28. #include <stdio.h>
  29. #include <string.h>
  30. #include <stdlib.h>
  31. #include <rtl.h>
  32. #include "wolfssl_MDK_ARM.h"
  33. #endif
  34. #ifdef WOLFSSL_KEIL_NET
  35. #include "wolfassl/test.h"
  36. #else
  37. typedef struct func_args {
  38. int argc;
  39. char** argv;
  40. int return_code;
  41. } func_args;
  42. #endif
  43. #ifdef NO_ECHOCLIENT
  44. #define echoclient_test command_not_found
  45. #endif
  46. #ifdef NO_ECHOSERVER
  47. #define echoserver_test command_not_found
  48. #endif
  49. #ifdef NO_SIMPLE_CLIENT
  50. #define client_test command_not_found
  51. #endif
  52. #ifdef NO_SIMPLE_SERVER
  53. #define server_test command_not_found
  54. #endif
  55. #ifdef NO_CRYPT_BENCHMARK
  56. #define benchmark_test command_not_found
  57. #endif
  58. #ifdef NO_CRYPT_TEST
  59. #define ctaocrypt_test command_not_found
  60. #endif
  61. #ifndef WOLFSSL_KEIL_NET
  62. #define ipaddr_comm command_not_found
  63. #endif
  64. #if !defined(HAVE_KEIL_RTX)
  65. #define stack_comm command_not_found
  66. #endif
  67. #if !defined(DEBUG_WOLFSSL)
  68. #define dbg_comm command_not_found
  69. #endif
  70. void command_not_found(void *argv) {
  71. printf("Command not found\n") ;
  72. }
  73. extern void echoclient_test(void *args) ;
  74. extern void echoserver_test(void *args) ;
  75. extern void benchmark_test(void *args) ;
  76. extern void wolfcrypt_test(void *args) ;
  77. extern void client_test(void *args) ;
  78. extern void server_test(void *args) ;
  79. extern void kill_task(void *args) ;
  80. extern void ipaddr_comm(void *args) ;
  81. extern void stack_comm(void *args) ;
  82. extern void for_command(void *args) ;
  83. extern void dbg_comm(void *arg) ;
  84. extern void help_comm(void *arg) ;
  85. #if !defined(NO_CRYPT_TEST)
  86. #ifndef NO_MD5
  87. extern void md5_test(void *arg) ;
  88. #endif
  89. #ifdef WOLFSSL_MD2
  90. extern void md2_test(void *arg) ;
  91. #endif
  92. #ifndef NO_MD4
  93. extern void md4_test(void *arg) ;
  94. #endif
  95. extern void sha_test(void *arg) ;
  96. #ifndef NO_SHA256
  97. extern void sha256_test(void *arg) ;
  98. #endif
  99. #ifdef WOLFSSL_SHA384
  100. extern void sha384_test(void *arg) ;
  101. #endif
  102. #ifdef WOLFSSL_SHA512
  103. extern void sha512_test(void *arg) ;
  104. #endif
  105. #ifdef WOLFSSL_RIPEMD
  106. extern void ripemd_test(void *arg) ;
  107. #endif
  108. #ifndef NO_HMAC
  109. #ifndef NO_MD5
  110. extern void hmac_md5_test(void *arg) ;
  111. #endif
  112. extern void hmac_sha_test(void *arg) ;
  113. #ifndef NO_SHA256
  114. extern void hmac_sha256_test(void *arg) ;
  115. #endif
  116. #ifdef WOLFSSL_SHA384
  117. extern void hmac_sha384_test(void *arg) ;
  118. #endif
  119. #endif
  120. #ifndef NO_RC4
  121. extern void arc4_test(void *arg) ;
  122. #endif
  123. #ifndef NO_DES3
  124. extern void des_test(void *arg) ;
  125. extern void des3_test(void *arg) ;
  126. #endif
  127. #ifndef NO_AES
  128. extern void aes_test(void *arg) ;
  129. #ifdef HAVE_AESGCM
  130. extern void aesgcm_test(void *arg) ;
  131. #endif
  132. #ifdef HAVE_AESCCM
  133. extern void aesccm_test(void *arg) ;
  134. #endif
  135. #endif
  136. #ifdef HAVE_CAMELLIA
  137. extern void camellia_test(void *arg) ;
  138. #endif
  139. extern void random_test(void *arg) ;
  140. #ifndef NO_RSA
  141. extern void rsa_test(void *arg) ;
  142. #endif
  143. #ifndef NO_DH
  144. extern void dh_test(void *arg) ;
  145. #endif
  146. #ifndef NO_DSA
  147. extern void dsa_test(void *arg) ;
  148. #endif
  149. #ifndef NO_PWDBASED
  150. extern void pwdbased_test(void *arg) ;
  151. #endif
  152. #ifdef HAVE_ECC
  153. extern void openssl_test(void *arg) ;
  154. #endif
  155. #ifdef HAVE_ECC
  156. extern void ecc_test(void *arg) ;
  157. #endif
  158. #endif /* NO_CRYPT_TEST */
  159. static struct {
  160. const char *command ;
  161. void (*func)(void *args) ;
  162. } commandTable[] = {
  163. "echoclient", echoclient_test,
  164. "echoserver", echoserver_test,
  165. "benchmark", benchmark_test,
  166. "test", wolfcrypt_test,
  167. "client", client_test,
  168. "server", server_test,
  169. "ipaddr", ipaddr_comm, /* TBD */
  170. "stack", stack_comm, /* On/Off check stack size */
  171. "for", for_command, /* iterate next command X times */
  172. "debug", dbg_comm, /* On/Off debug message */
  173. "help", help_comm, /* Brief description about the commands */
  174. /** short name **/
  175. "ec", echoclient_test,
  176. "es", echoserver_test,
  177. "bm", benchmark_test,
  178. "te", wolfcrypt_test,
  179. "cl", client_test,
  180. "sv", server_test,
  181. "ip", ipaddr_comm,
  182. "st", stack_comm,
  183. "dbg", dbg_comm,
  184. "?", help_comm,
  185. /*** test suites ****/
  186. #if !defined(NO_CRYPT_TEST)
  187. #ifndef NO_MD5
  188. "md5", md5_test,
  189. #endif
  190. #ifdef WOLFSSL_MD2
  191. "md2", md2_test,
  192. #endif
  193. #ifndef NO_MD4
  194. "md4", md4_test,
  195. #endif
  196. "sha", sha_test,
  197. #ifndef NO_SHA256
  198. "sha256", sha256_test,
  199. #endif
  200. #ifdef WOLFSSL_SHA384
  201. "sha384", sha384_test,
  202. #endif
  203. #ifdef WOLFSSL_SHA512
  204. "sha512", sha512_test,
  205. #endif
  206. #ifdef WOLFSSL_RIPEMD
  207. "ripemd", ripemd_test,
  208. #endif
  209. #ifndef NO_HMAC
  210. #ifndef NO_MD5
  211. "hmac_md5", hmac_md5_test,
  212. #endif
  213. "hmac_sha", hmac_sha_test,
  214. #ifndef NO_SHA256
  215. "hmac_sha256", hmac_sha256_test,
  216. #endif
  217. #ifdef WOLFSSL_SHA384
  218. "hmac_sha384", hmac_sha384_test,
  219. #endif
  220. #endif
  221. #ifndef NO_RC4
  222. "arc4", arc4_test,
  223. #endif
  224. #ifndef NO_DES3
  225. "des", des_test,
  226. "des3", des3_test,
  227. #endif
  228. #ifndef NO_AES
  229. "aes", aes_test,
  230. #ifdef HAVE_AESGCM
  231. "aesgcm", aesgcm_test,
  232. #endif
  233. #ifdef HAVE_AESCCM
  234. "aesccm", aesccm_test,
  235. #endif
  236. #endif
  237. #ifdef HAVE_CAMELLIA
  238. "camellia", camellia_test,
  239. #endif
  240. "random", random_test,
  241. #ifndef NO_RSA
  242. "rsa", rsa_test,
  243. #endif
  244. #ifndef NO_DH
  245. "dh", dh_test,
  246. #endif
  247. #ifndef NO_DSA
  248. "dsa", dsa_test,
  249. #endif
  250. #ifndef NO_PWDBASED
  251. "pwdbased", pwdbased_test,
  252. #endif
  253. #ifdef OPENSSL_EXTRA
  254. "openssl", openssl_test,
  255. #endif
  256. #ifdef HAVE_ECC
  257. "ecc", ecc_test,
  258. #endif
  259. #endif /* NO_CRYPT_TEST */
  260. "", NULL
  261. } ;
  262. enum jobtype { FORGROUND, BACKGROUND } ;
  263. #define IF_DELIMITER(ch) ((ch) == ' ' || (ch) == '\n')
  264. /******* Get Command Line *****************************/
  265. static int getline(char * line, int sz, func_args *args, int*bf_flg)
  266. {
  267. char * ret ;
  268. int i ;
  269. #define MAXARGS 10
  270. #define MAXARGLEN 30
  271. static char *argv[MAXARGS] ;
  272. args->argv = argv ;
  273. putchar('>') ;
  274. fflush(stdout) ;
  275. ret = fgets(line, sz, stdin) ;
  276. #define SHELL_ERROR_FGETS -102
  277. if(ret != line) return(SHELL_ERROR_FGETS) ;
  278. if(line[strlen(line)-2] == '&') {
  279. (*bf_flg) = BACKGROUND ;
  280. line[strlen(line)-2] = '\n' ;
  281. } else {
  282. (*bf_flg) = FORGROUND ;
  283. }
  284. args->argc = 0 ;
  285. for(i=0; i<sz; i++) {
  286. args->argv[args->argc] = &(line[i]) ;
  287. while(!IF_DELIMITER(line[i])) i++ ;
  288. args->argc++ ;
  289. if(line[i] == '\n') {
  290. line[i] = '\0' ;
  291. break ;
  292. } else {
  293. line[i] = '\0' ;
  294. }
  295. }
  296. return i ;
  297. }
  298. static int BackGround = 0 ; /* 1: background job is running */
  299. /************* Embedded Shell Commands **********************************/
  300. #define IP_SIZE 16
  301. #ifdef WOLFSSL_KEIL_NET
  302. static void ipaddr_comm(void *args)
  303. {
  304. if(((func_args *)args)->argc == 1) {
  305. printf("IP addr: %s, port %d\n", wolfSSLIP, wolfSSLPort) ;
  306. } else {
  307. if(BackGround != 0) {
  308. printf("Cannot change IP addr while background server is running\n") ;
  309. } else if(((func_args *)args)->argc == 3 &&
  310. ((func_args *)args)->argv[1][0] == '-'&&
  311. ((func_args *)args)->argv[1][1] == 'a' ) {
  312. /* strcpy(wolfSSLIP, ((func_args *)args)->argv[2]) ; */
  313. } else if(((func_args *)args)->argc == 3 &&
  314. ((func_args *)args)->argv[1][0] == '-' &&
  315. ((func_args *)args)->argv[1][1] == 'p' ) {
  316. /* yasslPort = atoi(((func_args *)args)->argv[2]) ; */
  317. } else printf("Invalid argument\n") ;
  318. }
  319. }
  320. #endif
  321. #if defined(HAVE_KEIL_RTX)
  322. static int stack_ck = 0 ;
  323. static void stack_comm(void *args)
  324. {
  325. if(stack_ck) {
  326. printf("Stack Check: Off\n") ;
  327. stack_ck = 0 ;
  328. } else {
  329. printf("Stack Check: On\n") ;
  330. stack_ck = 1 ;
  331. }
  332. }
  333. #define FILL_PATTERN 0xa596695a
  334. void stack_fill(char * stack, int size)
  335. {
  336. int i ;
  337. if(stack_ck == 0)return ;
  338. for(i=1; i<size/4-10; i++)
  339. ((int *)stack)[i] = FILL_PATTERN ;
  340. }
  341. void stack_check(char * stack, int size)
  342. {
  343. int i ;
  344. if(stack_ck == 0)return ;
  345. if(stack_ck == 1) {
  346. stack_ck ++ ; return ;
  347. }
  348. for(i=1; i<size/4 ; i++) {
  349. if(((int *)stack)[i] != FILL_PATTERN) break ;
  350. }
  351. if(i < size/4) {
  352. printf("Stack is used %d bytes out of %d\n", size - i*4, size) ;
  353. } else {
  354. printf("Stack overflow. Stack size: %d\n", size) ;
  355. }
  356. }
  357. #endif /* HAVE_KEIL_RTX */
  358. static int for_iteration = 1 ;
  359. static void for_command(void *args)
  360. {
  361. if( args == NULL || ((func_args *)args)->argc == 1) {
  362. printf("For %d times\n", for_iteration) ;
  363. } else if(((func_args *)args)->argc == 2) {
  364. for_iteration = atoi(((func_args *)args)->argv[1]) ;
  365. } else printf("Invalid argument\n") ;
  366. }
  367. #if defined(DEBUG_WOLFSSL)
  368. static int wolfsslDebug = 1 ;
  369. static void dbg_comm(void *args)
  370. {
  371. if(wolfsslDebug == 1) {
  372. wolfsslDebug = 0 ;
  373. printf("Turning OFF Debug message\n") ;
  374. wolfSSL_Debugging_OFF() ;
  375. } else {
  376. wolfsslDebug = 1 ;
  377. printf("Turning ON Debug message\n") ;
  378. wolfSSL_Debugging_ON() ;
  379. }
  380. }
  381. #endif
  382. static void help_comm(void *args)
  383. {
  384. }
  385. #define BG_JOB_STACK_SIZE 16000
  386. #if (!defined(NO_SIMPLE_SERVER) && !defined(NO_ECHOSERVER)) && \
  387. defined(HAVE_KEIL_RTX)
  388. static char bg_job_stack[BG_JOB_STACK_SIZE] ;
  389. #endif
  390. #define COMMAND_STACK_SIZE 16000
  391. #if defined(HAVE_KEIL_RTX)
  392. static char command_stack[COMMAND_STACK_SIZE] ;
  393. #endif
  394. #ifdef HAVE_KEIL_RTX
  395. static wolfSSL_Mutex command_mutex ;
  396. #endif
  397. /*********** Invoke Foreground Command *********************/
  398. static void command_invoke(void *args)
  399. {
  400. void (*func)(void * ) ;
  401. int i,iteration ;
  402. func = (void(*)(void *))((func_args *)args)->argv[0] ;
  403. #ifdef HAVE_KEIL_RTX
  404. wc_LockMutex((wolfSSL_Mutex *)&command_mutex) ;
  405. #endif
  406. iteration = for_iteration ;
  407. for(i=0; i< iteration; i++) {
  408. if(iteration > 1) printf("--- Start for %d ---->\n", i) ;
  409. #if defined(HAVE_KEIL_RTX)
  410. stack_fill(command_stack, COMMAND_STACK_SIZE) ;
  411. #endif
  412. func(args) ; /* invoke command */
  413. #if defined(HAVE_KEIL_RTX)
  414. stack_check(command_stack, COMMAND_STACK_SIZE) ;
  415. #endif
  416. }
  417. if(iteration > 1)
  418. for_iteration = 1 ;
  419. #ifdef HAVE_KEIL_RTX
  420. wc_UnLockMutex((wolfSSL_Mutex *)&command_mutex) ;
  421. os_tsk_delete_self() ;
  422. #endif
  423. }
  424. #if (!defined(NO_SIMPLE_SERVER) && !defined(NO_ECHOSERVER)) && \
  425. defined(HAVE_KEIL_RTX)
  426. /******* Invoke Background Job *******************************/
  427. static void bg_job_invoke(void *args)
  428. {
  429. void (*func)(void * ) ;
  430. BackGround = 1 ;
  431. stack_fill(bg_job_stack, BG_JOB_STACK_SIZE) ;
  432. func = (void(*)(void *))((func_args *)args)->argv[0] ;
  433. func(args) ; /* invoke command */
  434. stack_check(bg_job_stack, BG_JOB_STACK_SIZE) ;
  435. #ifdef WOLFSSL_KEIL_NET
  436. init_TcpNet ();
  437. #endif
  438. BackGround = 0 ;
  439. os_tsk_delete_self() ; ;
  440. }
  441. #endif
  442. #define LINESIZE 100
  443. static char line[LINESIZE] ;
  444. /********* SHEULL MAIN LOOP ***********************************/
  445. void shell_main(void) {
  446. int i ;
  447. func_args args ;
  448. int bf_flg ;
  449. i = BackGround ;
  450. /* Dummy for avoiding warning: BackGround is defined but not used. */
  451. #if defined(HAVE_KEIL_RTX)
  452. wc_InitMutex(&command_mutex) ;
  453. #endif
  454. printf("Starting Shell\n") ;
  455. while(1) {
  456. if(getline(line, LINESIZE, &args, &bf_flg) > 0) {
  457. for(i=0; commandTable[i].func != NULL; i++) {
  458. if(strcmp(commandTable[i].command, args.argv[0]) == 0) {
  459. args.argv[0] = (char *) commandTable[i].func ;
  460. if(bf_flg == FORGROUND) {
  461. #ifdef HAVE_KEIL_RTX
  462. wc_UnLockMutex((wolfSSL_Mutex *)&command_mutex) ;
  463. os_tsk_create_user_ex( (void(*)(void *))&command_invoke, 7,
  464. command_stack, COMMAND_STACK_SIZE, &args) ;
  465. #else
  466. command_invoke(&args) ;
  467. #endif
  468. #ifdef HAVE_KEIL_RTX
  469. wc_LockMutex((wolfSSL_Mutex *)&command_mutex) ;
  470. #endif
  471. } else {
  472. #if (!defined(NO_SIMPLE_SERVER) && \
  473. !defined(NO_ECHOSERVER)) && \
  474. defined(HAVE_KEIL_RTX)
  475. if(BackGround != 0) {
  476. printf("Multiple background servers not supported.\n") ;
  477. } else {
  478. printf("\"%s\" is running with the background mode.\n",
  479. commandTable[i].command) ;
  480. os_tsk_create_user_ex( (void(*)(void *))&bg_job_invoke,
  481. 6, bg_job_stack, BG_JOB_STACK_SIZE, &args) ;
  482. }
  483. #else
  484. printf("Invalid Command: no background job\n") ;
  485. #endif
  486. }
  487. break ;
  488. }
  489. }
  490. if(commandTable[i].func == NULL)
  491. printf("Command not found\n") ;
  492. }
  493. }
  494. }