server.c 12 KB

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