server.c 11 KB

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