server.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425
  1. /* server.c
  2. *
  3. * Copyright (C) 2014-2019 wolfSSL Inc.
  4. *
  5. * This file is part of wolfSSH.
  6. *
  7. * wolfSSH 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 3 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * wolfSSH 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 wolfSSH. If not, see <http://www.gnu.org/licenses/>.
  19. */
  20. #pragma GCC diagnostic error "-Wall"
  21. #define WOLFSSH_TEST_SERVER
  22. #define WOLFSSH_TEST_THREADING
  23. #include <stdbool.h>
  24. #include "FreeRTOS.h"
  25. #include "task.h"
  26. #include "wolfssh_test.h"
  27. #include "cli.h"
  28. #include "CLI_Commands.h"
  29. #include "settings_api.h"
  30. #ifdef WOLFSSL_USER_SETTINGS
  31. #include <wolfssl/wolfcrypt/settings.h>
  32. #else
  33. #include <wolfssl/options.h>
  34. #endif
  35. #include <wolfssl/wolfcrypt/sha256.h>
  36. #include <wolfssl/wolfcrypt/coding.h>
  37. #include <wolfssh/ssh.h>
  38. //#include <wolfssh/test.h>
  39. #include <wolfssl/wolfcrypt/ecc.h>
  40. #include "examples/server/server.h"
  41. #ifdef NO_FILESYSTEM
  42. #include <wolfssh/certs_test.h>
  43. #endif
  44. static const char serverBanner[] = "BT-6709 command server\n";
  45. typedef struct {
  46. WOLFSSH* ssh;
  47. SOCKET_T fd;
  48. word32 id;
  49. char nonBlock;
  50. } thread_ctx_t;
  51. #ifndef EXAMPLE_HIGHWATER_MARK
  52. #define EXAMPLE_HIGHWATER_MARK 0x3FFF8000 /* 1GB - 32kB */
  53. #endif
  54. #ifndef EXAMPLE_BUFFER_SZ
  55. #define EXAMPLE_BUFFER_SZ 256
  56. #endif
  57. #define SCRATCH_BUFFER_SZ 1200
  58. /*
  59. static int dump_stats(thread_ctx_t* ctx)
  60. {
  61. char stats[1024];
  62. word32 statsSz;
  63. word32 txCount, rxCount, seq, peerSeq;
  64. wolfSSH_GetStats(ctx->ssh, &txCount, &rxCount, &seq, &peerSeq);
  65. WSNPRINTF(stats, sizeof(stats),
  66. "Statistics for Thread #%u:\r\n"
  67. " txCount = %u\r\n rxCount = %u\r\n"
  68. " seq = %u\r\n peerSeq = %u\r\n",
  69. ctx->id, txCount, rxCount, seq, peerSeq);
  70. statsSz = (word32)strlen(stats);
  71. fprintf(stderr, "%s", stats);
  72. return wolfSSH_stream_send(ctx->ssh, (byte*)stats, statsSz);
  73. }
  74. */
  75. static int NonBlockSSH_accept(WOLFSSH* ssh)
  76. {
  77. int ret;
  78. int error;
  79. SOCKET_T sockfd;
  80. int select_ret = 0;
  81. ret = wolfSSH_accept(ssh);
  82. error = wolfSSH_get_error(ssh);
  83. sockfd = (SOCKET_T)wolfSSH_get_fd(ssh);
  84. while (ret != WS_SUCCESS &&
  85. (error == WS_WANT_READ || error == WS_WANT_WRITE))
  86. {
  87. if (error == WS_WANT_READ)
  88. printf("... client would read block\n");
  89. else if (error == WS_WANT_WRITE)
  90. printf("... client would write block\n");
  91. select_ret = tcp_select(sockfd, 1);
  92. if (select_ret == WS_SELECT_RECV_READY ||
  93. select_ret == WS_SELECT_ERROR_READY ||
  94. error == WS_WANT_WRITE)
  95. {
  96. ret = wolfSSH_accept(ssh);
  97. error = wolfSSH_get_error(ssh);
  98. }
  99. else if (select_ret == WS_SELECT_TIMEOUT)
  100. error = WS_WANT_READ;
  101. else
  102. error = WS_FATAL_ERROR;
  103. }
  104. return ret;
  105. }
  106. static void cli_send(intptr_t fd, const char *str, unsigned len)
  107. {
  108. wolfSSH_stream_send((WOLFSSH *)fd, str, len);
  109. }
  110. static void *server_worker(void* vArgs)
  111. {
  112. int ret;
  113. thread_ctx_t* threadCtx = (thread_ctx_t*)vArgs;
  114. user_level_t user_id;
  115. wolfSSH_SetUserAuthCtx(threadCtx->ssh, &user_id);
  116. if (!threadCtx->nonBlock)
  117. ret = wolfSSH_accept(threadCtx->ssh);
  118. else
  119. ret = NonBlockSSH_accept(threadCtx->ssh);
  120. cli_state_t *cli_state;
  121. if (!sSettings.sSSH.SSHEnable) {
  122. // kick the client out
  123. } else if (ret == WS_SUCCESS) {
  124. if ((cli_state = alloc_state())) {
  125. // create the new CLI contex
  126. cli_state->num_connect = threadCtx->ssh;
  127. cli_state->input_state = CLI_CMD;
  128. cli_state->send = cli_send;
  129. cli_hello(cli_state);
  130. bool stop = false;
  131. do {
  132. uint8_t buf[EXAMPLE_BUFFER_SZ];
  133. int rxSz = 0;
  134. do {
  135. rxSz = wolfSSH_stream_read(threadCtx->ssh, buf, sizeof(buf));
  136. if (rxSz <= 0) {
  137. rxSz = wolfSSH_get_error(threadCtx->ssh);
  138. }
  139. } while (rxSz == WS_WANT_READ || rxSz == WS_WANT_WRITE);
  140. if (rxSz > 0) {
  141. cli_getchar(cli_state, buf[0], true); // TODO handle rxSz > 1
  142. if (buf[0] == 3 || buf[0] == 4 || cli_state->state == STATE_CLOSE) {
  143. stop = 1;
  144. }
  145. } else {
  146. stop = 1;
  147. }
  148. } while (!stop);
  149. free_state(cli_state);
  150. } else {
  151. cli_send(threadCtx->ssh, pcWarningMessage, pcWarningMessageLen);
  152. }
  153. } else if (ret == WS_SCP_COMPLETE) {
  154. printf("scp file transfer completed\n");
  155. } else if (ret == WS_SFTP_COMPLETE) {
  156. printf("Use example/echoserver/echoserver for SFTP\n");
  157. }
  158. wolfSSH_stream_exit(threadCtx->ssh, 0);
  159. WCLOSESOCKET(threadCtx->fd);
  160. wolfSSH_free(threadCtx->ssh);
  161. free(threadCtx);
  162. return 0;
  163. }
  164. #ifndef NO_FILESYSTEM
  165. static int load_file(const char* fileName, byte* buf, word32 bufSz)
  166. {
  167. FILE* file;
  168. word32 fileSz;
  169. word32 readSz;
  170. if (fileName == NULL) return 0;
  171. if (WFOPEN(&file, fileName, "rb") != 0)
  172. return 0;
  173. fseek(file, 0, SEEK_END);
  174. fileSz = (word32)ftell(file);
  175. rewind(file);
  176. if (fileSz > bufSz) {
  177. fclose(file);
  178. return 0;
  179. }
  180. readSz = (word32)fread(buf, 1, fileSz, file);
  181. if (readSz < fileSz) {
  182. fclose(file);
  183. return 0;
  184. }
  185. fclose(file);
  186. return fileSz;
  187. }
  188. #endif /* !NO_FILESYSTEM */
  189. /* returns buffer size on success */
  190. static int load_key(byte isEcc, byte* buf, word32 bufSz)
  191. {
  192. word32 sz = 0;
  193. #ifndef NO_FILESYSTEM
  194. const char* bufName;
  195. bufName = isEcc ? "./keys/server-key-ecc.der" :
  196. "./keys/server-key-rsa.der" ;
  197. sz = load_file(bufName, buf, bufSz);
  198. #else
  199. /* using buffers instead */
  200. if (isEcc) {
  201. if (sizeof_ecc_key_der_256 > bufSz) {
  202. return 0;
  203. }
  204. WMEMCPY(buf, ecc_key_der_256, sizeof_ecc_key_der_256);
  205. sz = sizeof_ecc_key_der_256;
  206. }
  207. else {
  208. if (sizeof_rsa_key_der_2048 > bufSz) {
  209. return 0;
  210. }
  211. WMEMCPY(buf, rsa_key_der_2048, sizeof_rsa_key_der_2048);
  212. sz = sizeof_rsa_key_der_2048;
  213. }
  214. #endif
  215. return sz;
  216. }
  217. static INLINE void c32toa(word32 u32, byte* c)
  218. {
  219. c[0] = (u32 >> 24) & 0xff;
  220. c[1] = (u32 >> 16) & 0xff;
  221. c[2] = (u32 >> 8) & 0xff;
  222. c[3] = u32 & 0xff;
  223. }
  224. static int wsUserAuth(byte authType, WS_UserAuthData* authData, void* ctx)
  225. {
  226. user_level_t *user_id = ctx;
  227. if (ctx == NULL) {
  228. printf("wsUserAuth: ctx not set");
  229. return WOLFSSH_USERAUTH_FAILURE;
  230. }
  231. if (authType != WOLFSSH_USERAUTH_PASSWORD) {
  232. return WOLFSSH_USERAUTH_FAILURE;
  233. }
  234. // the incoming password is not zero-terminated
  235. char password[MAX_WEB_PASSWD_LEN];
  236. strncpy(password, (char *)authData->sf.password.password, sizeof(password));
  237. password[min(MAX_WEB_PASSWD_LEN - 1, authData->sf.password.passwordSz)] = 0;
  238. *user_id = cli_auth_user((const char *)authData->username, password, LOG_LOGIN_SSH);
  239. if (*user_id != MAX_USER_LEVELS) {
  240. return WOLFSSH_USERAUTH_SUCCESS;
  241. }
  242. return WOLFSSH_USERAUTH_INVALID_USER;
  243. }
  244. static void ssh_server(void *arg)
  245. {
  246. (void)arg;
  247. WSTARTTCP();
  248. #ifdef DEBUG_WOLFSSH
  249. wolfSSH_Debugging_ON();
  250. #endif
  251. wolfSSH_Init();
  252. WOLFSSH_CTX* ctx = NULL;
  253. static SOCKET_T ssh_listen_fd = 0;
  254. word32 defaultHighwater = EXAMPLE_HIGHWATER_MARK;
  255. word32 threadCount = 0;
  256. const char multipleConnections = 0;
  257. char useEcc = 1;
  258. char nonBlock = 0;
  259. if (wolfSSH_Init() != WS_SUCCESS) {
  260. printf("Couldn't initialize wolfSSH.\n");
  261. exit(EXIT_FAILURE);
  262. }
  263. ctx = wolfSSH_CTX_new(WOLFSSH_ENDPOINT_SERVER, NULL);
  264. if (ctx == NULL) {
  265. printf("Couldn't allocate SSH CTX data.\n");
  266. exit(EXIT_FAILURE);
  267. }
  268. wolfSSH_SetUserAuth(ctx, wsUserAuth);
  269. wolfSSH_CTX_SetBanner(ctx, serverBanner);
  270. {
  271. byte buf[SCRATCH_BUFFER_SZ];
  272. word32 bufSz;
  273. bufSz = load_key(useEcc, buf, SCRATCH_BUFFER_SZ);
  274. if (bufSz == 0) {
  275. printf("Couldn't load key.\n");
  276. exit(EXIT_FAILURE);
  277. }
  278. if (wolfSSH_CTX_UsePrivateKey_buffer(ctx, buf, bufSz,
  279. WOLFSSH_FORMAT_ASN1) < 0) {
  280. printf("Couldn't use key buffer.\n");
  281. exit(EXIT_FAILURE);
  282. }
  283. }
  284. while (sSettings.sSSH.SSHEnable) {
  285. word16 port = sSettings.sSSH.port;
  286. tcp_listen(&ssh_listen_fd, &port, 1, false, false);
  287. do {
  288. SOCKET_T clientFd = 0;
  289. SOCKADDR_IN_T clientAddr;
  290. socklen_t clientAddrSz = sizeof(clientAddr);
  291. #ifndef SINGLE_THREADED
  292. THREAD_TYPE thread;
  293. #endif
  294. WOLFSSH* ssh;
  295. thread_ctx_t* threadCtx;
  296. threadCtx = (thread_ctx_t*)malloc(sizeof(thread_ctx_t));
  297. if (threadCtx == NULL) {
  298. printf("Couldn't allocate thread context data.\n");
  299. exit(EXIT_FAILURE);
  300. }
  301. ssh = wolfSSH_new(ctx);
  302. if (ssh == NULL) {
  303. printf("Couldn't allocate SSH data.\n");
  304. exit(EXIT_FAILURE);
  305. }
  306. /* Use the session object for its own highwater callback ctx */
  307. if (defaultHighwater > 0) {
  308. wolfSSH_SetHighwaterCtx(ssh, (void*)ssh);
  309. wolfSSH_SetHighwater(ssh, defaultHighwater);
  310. }
  311. clientFd = accept(ssh_listen_fd, (struct sockaddr*)&clientAddr,
  312. &clientAddrSz);
  313. if (clientFd == -1)
  314. err_sys("tcp accept failed");
  315. if (nonBlock)
  316. tcp_set_nonblocking(&clientFd);
  317. wolfSSH_set_fd(ssh, (int)clientFd);
  318. threadCtx->ssh = ssh;
  319. threadCtx->fd = clientFd;
  320. threadCtx->id = threadCount++;
  321. threadCtx->nonBlock = nonBlock;
  322. #ifndef SINGLE_THREADED
  323. ThreadStart(server_worker, threadCtx, &thread);
  324. if (multipleConnections)
  325. ThreadDetach(thread);
  326. else
  327. ThreadJoin(thread);
  328. #else
  329. server_worker(threadCtx);
  330. #endif /* SINGLE_THREADED */
  331. } while (multipleConnections);
  332. if (ssh_listen_fd) {
  333. WCLOSESOCKET(ssh_listen_fd);
  334. ssh_listen_fd = 0;
  335. }
  336. }
  337. wolfSSH_CTX_free(ctx);
  338. if (wolfSSH_Cleanup() != WS_SUCCESS) {
  339. printf("Couldn't clean up wolfSSH.\n");
  340. exit(EXIT_FAILURE);
  341. }
  342. #if defined(HAVE_ECC) && defined(FP_ECC) && defined(HAVE_THREAD_LS)
  343. wc_ecc_fp_free(); /* free per thread cache */
  344. #endif
  345. vTaskDelete(NULL);
  346. }
  347. void ssh_server_init(void)
  348. {
  349. xTaskCreate(ssh_server, ( char * ) "ssh_server", 24*configMINIMAL_STACK_SIZE + EXAMPLE_BUFFER_SZ, NULL, tskIDLE_PRIORITY + 1, NULL);
  350. }
  351. void ssh_server_restart(void)
  352. {
  353. for (unsigned i = 0; i < array_len(cli_states); ++i) {
  354. if (cli_states[i].state != STATE_UNUSED && cli_states[i].send == cli_send) {
  355. wolfSSH_shutdown(cli_states[i].num_connect);
  356. }
  357. }
  358. }