server.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421
  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. cli_state->num_connect = threadCtx->ssh;
  126. cli_state->input_state = CLI_CMD;
  127. cli_state->send = cli_send;
  128. cli_hello(cli_state);
  129. bool stop = false;
  130. do {
  131. uint8_t buf[EXAMPLE_BUFFER_SZ];
  132. int rxSz = 0;
  133. do {
  134. rxSz = wolfSSH_stream_read(threadCtx->ssh, buf, sizeof(buf));
  135. if (rxSz <= 0) {
  136. rxSz = wolfSSH_get_error(threadCtx->ssh);
  137. }
  138. } while (rxSz == WS_WANT_READ || rxSz == WS_WANT_WRITE);
  139. if (rxSz > 0) {
  140. cli_getchar(cli_state, buf[0]); // TODO handle rxSz > 1
  141. if (buf[0] == 3 || buf[0] == 4 || cli_state->state == STATE_CLOSE) {
  142. stop = 1;
  143. }
  144. } else {
  145. stop = 1;
  146. }
  147. } while (!stop);
  148. free_state(cli_state);
  149. } else {
  150. cli_send(threadCtx->ssh, pcWarningMessage, pcWarningMessageLen);
  151. }
  152. } else if (ret == WS_SCP_COMPLETE) {
  153. printf("scp file transfer completed\n");
  154. } else if (ret == WS_SFTP_COMPLETE) {
  155. printf("Use example/echoserver/echoserver for SFTP\n");
  156. }
  157. wolfSSH_stream_exit(threadCtx->ssh, 0);
  158. WCLOSESOCKET(threadCtx->fd);
  159. wolfSSH_free(threadCtx->ssh);
  160. free(threadCtx);
  161. return 0;
  162. }
  163. #ifndef NO_FILESYSTEM
  164. static int load_file(const char* fileName, byte* buf, word32 bufSz)
  165. {
  166. FILE* file;
  167. word32 fileSz;
  168. word32 readSz;
  169. if (fileName == NULL) return 0;
  170. if (WFOPEN(&file, fileName, "rb") != 0)
  171. return 0;
  172. fseek(file, 0, SEEK_END);
  173. fileSz = (word32)ftell(file);
  174. rewind(file);
  175. if (fileSz > bufSz) {
  176. fclose(file);
  177. return 0;
  178. }
  179. readSz = (word32)fread(buf, 1, fileSz, file);
  180. if (readSz < fileSz) {
  181. fclose(file);
  182. return 0;
  183. }
  184. fclose(file);
  185. return fileSz;
  186. }
  187. #endif /* !NO_FILESYSTEM */
  188. /* returns buffer size on success */
  189. static int load_key(byte isEcc, byte* buf, word32 bufSz)
  190. {
  191. word32 sz = 0;
  192. #ifndef NO_FILESYSTEM
  193. const char* bufName;
  194. bufName = isEcc ? "./keys/server-key-ecc.der" :
  195. "./keys/server-key-rsa.der" ;
  196. sz = load_file(bufName, buf, bufSz);
  197. #else
  198. /* using buffers instead */
  199. if (isEcc) {
  200. if (sizeof_ecc_key_der_256 > bufSz) {
  201. return 0;
  202. }
  203. WMEMCPY(buf, ecc_key_der_256, sizeof_ecc_key_der_256);
  204. sz = sizeof_ecc_key_der_256;
  205. }
  206. else {
  207. if (sizeof_rsa_key_der_2048 > bufSz) {
  208. return 0;
  209. }
  210. WMEMCPY(buf, rsa_key_der_2048, sizeof_rsa_key_der_2048);
  211. sz = sizeof_rsa_key_der_2048;
  212. }
  213. #endif
  214. return sz;
  215. }
  216. static INLINE void c32toa(word32 u32, byte* c)
  217. {
  218. c[0] = (u32 >> 24) & 0xff;
  219. c[1] = (u32 >> 16) & 0xff;
  220. c[2] = (u32 >> 8) & 0xff;
  221. c[3] = u32 & 0xff;
  222. }
  223. static int wsUserAuth(byte authType, WS_UserAuthData* authData, void* ctx)
  224. {
  225. user_level_t *user_id = ctx;
  226. if (ctx == NULL) {
  227. printf("wsUserAuth: ctx not set");
  228. return WOLFSSH_USERAUTH_FAILURE;
  229. }
  230. if (authType != WOLFSSH_USERAUTH_PASSWORD) {
  231. return WOLFSSH_USERAUTH_FAILURE;
  232. }
  233. // the incoming password is not zero-terminated
  234. char password[MAX_WEB_PASSWD_LEN];
  235. strncpy(password, (char *)authData->sf.password.password, sizeof(password));
  236. password[min(MAX_WEB_PASSWD_LEN - 1, authData->sf.password.passwordSz)] = 0;
  237. *user_id = cli_auth_user((const char *)authData->username, password, LOG_LOGIN_SSH);
  238. if (*user_id != MAX_USER_LEVELS) {
  239. return WOLFSSH_USERAUTH_SUCCESS;
  240. }
  241. return WOLFSSH_USERAUTH_INVALID_USER;
  242. }
  243. static void ssh_server(void *arg)
  244. {
  245. (void)arg;
  246. WSTARTTCP();
  247. #ifdef DEBUG_WOLFSSH
  248. wolfSSH_Debugging_ON();
  249. #endif
  250. wolfSSH_Init();
  251. WOLFSSH_CTX* ctx = NULL;
  252. SOCKET_T listenFd = 0;
  253. word32 defaultHighwater = EXAMPLE_HIGHWATER_MARK;
  254. word32 threadCount = 0;
  255. const char multipleConnections = 0;
  256. char useEcc = 1;
  257. char nonBlock = 0;
  258. if (wolfSSH_Init() != WS_SUCCESS) {
  259. printf("Couldn't initialize wolfSSH.\n");
  260. exit(EXIT_FAILURE);
  261. }
  262. ctx = wolfSSH_CTX_new(WOLFSSH_ENDPOINT_SERVER, NULL);
  263. if (ctx == NULL) {
  264. printf("Couldn't allocate SSH CTX data.\n");
  265. exit(EXIT_FAILURE);
  266. }
  267. wolfSSH_SetUserAuth(ctx, wsUserAuth);
  268. wolfSSH_CTX_SetBanner(ctx, serverBanner);
  269. {
  270. byte buf[SCRATCH_BUFFER_SZ];
  271. word32 bufSz;
  272. bufSz = load_key(useEcc, buf, SCRATCH_BUFFER_SZ);
  273. if (bufSz == 0) {
  274. printf("Couldn't load key.\n");
  275. exit(EXIT_FAILURE);
  276. }
  277. if (wolfSSH_CTX_UsePrivateKey_buffer(ctx, buf, bufSz,
  278. WOLFSSH_FORMAT_ASN1) < 0) {
  279. printf("Couldn't use key buffer.\n");
  280. exit(EXIT_FAILURE);
  281. }
  282. }
  283. while (sSettings.sSSH.SSHEnable) {
  284. word16 port = sSettings.sSSH.port;
  285. tcp_listen(&listenFd, &port, 1, false, false);
  286. do {
  287. SOCKET_T clientFd = 0;
  288. SOCKADDR_IN_T clientAddr;
  289. socklen_t clientAddrSz = sizeof(clientAddr);
  290. #ifndef SINGLE_THREADED
  291. THREAD_TYPE thread;
  292. #endif
  293. WOLFSSH* ssh;
  294. thread_ctx_t* threadCtx;
  295. threadCtx = (thread_ctx_t*)malloc(sizeof(thread_ctx_t));
  296. if (threadCtx == NULL) {
  297. printf("Couldn't allocate thread context data.\n");
  298. exit(EXIT_FAILURE);
  299. }
  300. ssh = wolfSSH_new(ctx);
  301. if (ssh == NULL) {
  302. printf("Couldn't allocate SSH data.\n");
  303. exit(EXIT_FAILURE);
  304. }
  305. /* Use the session object for its own highwater callback ctx */
  306. if (defaultHighwater > 0) {
  307. wolfSSH_SetHighwaterCtx(ssh, (void*)ssh);
  308. wolfSSH_SetHighwater(ssh, defaultHighwater);
  309. }
  310. clientFd = accept(listenFd, (struct sockaddr*)&clientAddr,
  311. &clientAddrSz);
  312. if (clientFd == -1)
  313. err_sys("tcp accept failed");
  314. if (nonBlock)
  315. tcp_set_nonblocking(&clientFd);
  316. wolfSSH_set_fd(ssh, (int)clientFd);
  317. threadCtx->ssh = ssh;
  318. threadCtx->fd = clientFd;
  319. threadCtx->id = threadCount++;
  320. threadCtx->nonBlock = nonBlock;
  321. #ifndef SINGLE_THREADED
  322. ThreadStart(server_worker, threadCtx, &thread);
  323. if (multipleConnections)
  324. ThreadDetach(thread);
  325. else
  326. ThreadJoin(thread);
  327. #else
  328. server_worker(threadCtx);
  329. #endif /* SINGLE_THREADED */
  330. } while (multipleConnections);
  331. WCLOSESOCKET(listenFd);
  332. }
  333. wolfSSH_CTX_free(ctx);
  334. if (wolfSSH_Cleanup() != WS_SUCCESS) {
  335. printf("Couldn't clean up wolfSSH.\n");
  336. exit(EXIT_FAILURE);
  337. }
  338. #if defined(HAVE_ECC) && defined(FP_ECC) && defined(HAVE_THREAD_LS)
  339. wc_ecc_fp_free(); /* free per thread cache */
  340. #endif
  341. vTaskDelete(NULL);
  342. }
  343. void ssh_server_init(void)
  344. {
  345. xTaskCreate(ssh_server, ( char * ) "ssh_server", 24*configMINIMAL_STACK_SIZE + EXAMPLE_BUFFER_SZ, NULL, tskIDLE_PRIORITY + 1, NULL);
  346. }
  347. void ssh_server_restart(void)
  348. {
  349. for (unsigned i = 0; i < array_len(cli_states); ++i) {
  350. if (cli_states[i].state != STATE_UNUSED && cli_states[i].send == cli_send) {
  351. wolfSSH_shutdown(cli_states[i].num_connect);
  352. }
  353. }
  354. }