server.c 12 KB

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