server.c 10 KB

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