server.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714
  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. #ifdef WOLFSSL_USER_SETTINGS
  27. #include <wolfssl/wolfcrypt/settings.h>
  28. #else
  29. #include <wolfssl/options.h>
  30. #endif
  31. #include <wolfssl/wolfcrypt/sha256.h>
  32. #include <wolfssl/wolfcrypt/coding.h>
  33. #include <wolfssh/ssh.h>
  34. //#include <wolfssh/test.h>
  35. #include <wolfssl/wolfcrypt/ecc.h>
  36. #include "examples/server/server.h"
  37. #ifdef NO_FILESYSTEM
  38. #include <wolfssh/certs_test.h>
  39. #endif
  40. static const char serverBanner[] = "wolfSSH Example Server\n";
  41. typedef struct {
  42. WOLFSSH* ssh;
  43. SOCKET_T fd;
  44. word32 id;
  45. char nonBlock;
  46. } thread_ctx_t;
  47. #ifndef EXAMPLE_HIGHWATER_MARK
  48. #define EXAMPLE_HIGHWATER_MARK 0x3FFF8000 /* 1GB - 32kB */
  49. #endif
  50. #ifndef EXAMPLE_BUFFER_SZ
  51. #define EXAMPLE_BUFFER_SZ 4096
  52. #endif
  53. #define SCRATCH_BUFFER_SZ 1200
  54. static byte find_char(const byte* str, const byte* buf, word32 bufSz)
  55. {
  56. const byte* cur;
  57. while (bufSz) {
  58. cur = str;
  59. while (*cur != '\0') {
  60. if (*cur == *buf)
  61. return *cur;
  62. cur++;
  63. }
  64. buf++;
  65. bufSz--;
  66. }
  67. return 0;
  68. }
  69. /*
  70. static int dump_stats(thread_ctx_t* ctx)
  71. {
  72. char stats[1024];
  73. word32 statsSz;
  74. word32 txCount, rxCount, seq, peerSeq;
  75. wolfSSH_GetStats(ctx->ssh, &txCount, &rxCount, &seq, &peerSeq);
  76. WSNPRINTF(stats, sizeof(stats),
  77. "Statistics for Thread #%u:\r\n"
  78. " txCount = %u\r\n rxCount = %u\r\n"
  79. " seq = %u\r\n peerSeq = %u\r\n",
  80. ctx->id, txCount, rxCount, seq, peerSeq);
  81. statsSz = (word32)strlen(stats);
  82. fprintf(stderr, "%s", stats);
  83. return wolfSSH_stream_send(ctx->ssh, (byte*)stats, statsSz);
  84. }
  85. */
  86. static int NonBlockSSH_accept(WOLFSSH* ssh)
  87. {
  88. int ret;
  89. int error;
  90. SOCKET_T sockfd;
  91. int select_ret = 0;
  92. ret = wolfSSH_accept(ssh);
  93. error = wolfSSH_get_error(ssh);
  94. sockfd = (SOCKET_T)wolfSSH_get_fd(ssh);
  95. while (ret != WS_SUCCESS &&
  96. (error == WS_WANT_READ || error == WS_WANT_WRITE))
  97. {
  98. if (error == WS_WANT_READ)
  99. printf("... client would read block\n");
  100. else if (error == WS_WANT_WRITE)
  101. printf("... client would write block\n");
  102. select_ret = tcp_select(sockfd, 1);
  103. if (select_ret == WS_SELECT_RECV_READY ||
  104. select_ret == WS_SELECT_ERROR_READY ||
  105. error == WS_WANT_WRITE)
  106. {
  107. ret = wolfSSH_accept(ssh);
  108. error = wolfSSH_get_error(ssh);
  109. }
  110. else if (select_ret == WS_SELECT_TIMEOUT)
  111. error = WS_WANT_READ;
  112. else
  113. error = WS_FATAL_ERROR;
  114. }
  115. return ret;
  116. }
  117. static void *server_worker(void* vArgs)
  118. {
  119. int ret;
  120. thread_ctx_t* threadCtx = (thread_ctx_t*)vArgs;
  121. if (!threadCtx->nonBlock)
  122. ret = wolfSSH_accept(threadCtx->ssh);
  123. else
  124. ret = NonBlockSSH_accept(threadCtx->ssh);
  125. if (ret == WS_SUCCESS) {
  126. byte* buf = NULL;
  127. byte* tmpBuf;
  128. int bufSz, backlogSz = 0, rxSz, txSz, stop = 0, txSum;
  129. do {
  130. bufSz = EXAMPLE_BUFFER_SZ + backlogSz;
  131. tmpBuf = (byte*)realloc(buf, bufSz);
  132. if (tmpBuf == NULL)
  133. stop = 1;
  134. else
  135. buf = tmpBuf;
  136. if (!stop) {
  137. do {
  138. rxSz = wolfSSH_stream_read(threadCtx->ssh,
  139. buf + backlogSz,
  140. EXAMPLE_BUFFER_SZ);
  141. if (rxSz <= 0)
  142. rxSz = wolfSSH_get_error(threadCtx->ssh);
  143. } while (rxSz == WS_WANT_READ || rxSz == WS_WANT_WRITE);
  144. if (rxSz > 0) {
  145. backlogSz += rxSz;
  146. txSum = 0;
  147. txSz = 0;
  148. while (backlogSz != txSum && txSz >= 0 && !stop) {
  149. txSz = wolfSSH_stream_send(threadCtx->ssh,
  150. buf + txSum,
  151. backlogSz - txSum);
  152. if (txSz > 0) {
  153. byte c;
  154. const byte matches[] = { 0x03, 0x05, 0x06, 0x00 };
  155. c = find_char(matches, buf + txSum, txSz);
  156. switch (c) {
  157. case 0x03:
  158. stop = 1;
  159. break;
  160. case 0x06:
  161. if (wolfSSH_TriggerKeyExchange(threadCtx->ssh)
  162. != WS_SUCCESS)
  163. stop = 1;
  164. break;
  165. /*
  166. case 0x05:
  167. if (dump_stats(threadCtx) <= 0)
  168. stop = 1;
  169. break;
  170. */
  171. }
  172. txSum += txSz;
  173. }
  174. else if (txSz != WS_REKEYING)
  175. stop = 1;
  176. }
  177. if (txSum < backlogSz)
  178. memmove(buf, buf + txSum, backlogSz - txSum);
  179. backlogSz -= txSum;
  180. }
  181. else
  182. stop = 1;
  183. }
  184. } while (!stop);
  185. free(buf);
  186. } else if (ret == WS_SCP_COMPLETE) {
  187. printf("scp file transfer completed\n");
  188. } else if (ret == WS_SFTP_COMPLETE) {
  189. printf("Use example/echoserver/echoserver for SFTP\n");
  190. }
  191. wolfSSH_stream_exit(threadCtx->ssh, 0);
  192. WCLOSESOCKET(threadCtx->fd);
  193. wolfSSH_free(threadCtx->ssh);
  194. free(threadCtx);
  195. return 0;
  196. }
  197. #ifndef NO_FILESYSTEM
  198. static int load_file(const char* fileName, byte* buf, word32 bufSz)
  199. {
  200. FILE* file;
  201. word32 fileSz;
  202. word32 readSz;
  203. if (fileName == NULL) return 0;
  204. if (WFOPEN(&file, fileName, "rb") != 0)
  205. return 0;
  206. fseek(file, 0, SEEK_END);
  207. fileSz = (word32)ftell(file);
  208. rewind(file);
  209. if (fileSz > bufSz) {
  210. fclose(file);
  211. return 0;
  212. }
  213. readSz = (word32)fread(buf, 1, fileSz, file);
  214. if (readSz < fileSz) {
  215. fclose(file);
  216. return 0;
  217. }
  218. fclose(file);
  219. return fileSz;
  220. }
  221. #endif /* !NO_FILESYSTEM */
  222. /* returns buffer size on success */
  223. static int load_key(byte isEcc, byte* buf, word32 bufSz)
  224. {
  225. word32 sz = 0;
  226. #ifndef NO_FILESYSTEM
  227. const char* bufName;
  228. bufName = isEcc ? "./keys/server-key-ecc.der" :
  229. "./keys/server-key-rsa.der" ;
  230. sz = load_file(bufName, buf, bufSz);
  231. #else
  232. /* using buffers instead */
  233. if (isEcc) {
  234. if (sizeof_ecc_key_der_256 > bufSz) {
  235. return 0;
  236. }
  237. WMEMCPY(buf, ecc_key_der_256, sizeof_ecc_key_der_256);
  238. sz = sizeof_ecc_key_der_256;
  239. }
  240. else {
  241. if (sizeof_rsa_key_der_2048 > bufSz) {
  242. return 0;
  243. }
  244. WMEMCPY(buf, rsa_key_der_2048, sizeof_rsa_key_der_2048);
  245. sz = sizeof_rsa_key_der_2048;
  246. }
  247. #endif
  248. return sz;
  249. }
  250. static INLINE void c32toa(word32 u32, byte* c)
  251. {
  252. c[0] = (u32 >> 24) & 0xff;
  253. c[1] = (u32 >> 16) & 0xff;
  254. c[2] = (u32 >> 8) & 0xff;
  255. c[3] = u32 & 0xff;
  256. }
  257. /* Map user names to passwords */
  258. /* Use arrays for username and p. The password or public key can
  259. * be hashed and the hash stored here. Then I won't need the type. */
  260. typedef struct PwMap {
  261. byte type;
  262. byte username[32];
  263. word32 usernameSz;
  264. byte p[SHA256_DIGEST_SIZE];
  265. struct PwMap* next;
  266. } PwMap;
  267. typedef struct PwMapList {
  268. PwMap* head;
  269. } PwMapList;
  270. static PwMap* PwMapNew(PwMapList* list, byte type, const byte* username,
  271. word32 usernameSz, const byte* p, word32 pSz)
  272. {
  273. PwMap* map;
  274. map = (PwMap*)malloc(sizeof(PwMap));
  275. if (map != NULL) {
  276. Sha256 sha;
  277. byte flatSz[4];
  278. map->type = type;
  279. if (usernameSz >= sizeof(map->username))
  280. usernameSz = sizeof(map->username) - 1;
  281. memcpy(map->username, username, usernameSz + 1);
  282. map->username[usernameSz] = 0;
  283. map->usernameSz = usernameSz;
  284. wc_InitSha256(&sha);
  285. c32toa(pSz, flatSz);
  286. wc_Sha256Update(&sha, flatSz, sizeof(flatSz));
  287. wc_Sha256Update(&sha, p, pSz);
  288. wc_Sha256Final(&sha, map->p);
  289. map->next = list->head;
  290. list->head = map;
  291. }
  292. return map;
  293. }
  294. static void PwMapListDelete(PwMapList* list)
  295. {
  296. if (list != NULL) {
  297. PwMap* head = list->head;
  298. while (head != NULL) {
  299. PwMap* cur = head;
  300. head = head->next;
  301. memset(cur, 0, sizeof(PwMap));
  302. free(cur);
  303. }
  304. }
  305. }
  306. static const char samplePasswordBuffer[] =
  307. "jill:upthehill\n"
  308. "jack:fetchapail\n";
  309. static const char samplePublicKeyEccBuffer[] =
  310. "ecdsa-sha2-nistp256 AAAAE2VjZHNhLXNoYTItbmlzdHAyNTYAAAAIbmlzdHAyNTYAAA"
  311. "BBBNkI5JTP6D0lF42tbxX19cE87hztUS6FSDoGvPfiU0CgeNSbI+aFdKIzTP5CQEJSvm25"
  312. "qUzgDtH7oyaQROUnNvk= hansel\n"
  313. "ecdsa-sha2-nistp256 AAAAE2VjZHNhLXNoYTItbmlzdHAyNTYAAAAIbmlzdHAyNTYAAA"
  314. "BBBKAtH8cqaDbtJFjtviLobHBmjCtG56DMkP6A4M2H9zX2/YCg1h9bYS7WHd9UQDwXO1Hh"
  315. "IZzRYecXh7SG9P4GhRY= gretel\n";
  316. static const char samplePublicKeyRsaBuffer[] =
  317. "ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQC9P3ZFowOsONXHD5MwWiCciXytBRZGho"
  318. "MNiisWSgUs5HdHcACuHYPi2W6Z1PBFmBWT9odOrGRjoZXJfDDoPi+j8SSfDGsc/hsCmc3G"
  319. "p2yEhUZUEkDhtOXyqjns1ickC9Gh4u80aSVtwHRnJZh9xPhSq5tLOhId4eP61s+a5pwjTj"
  320. "nEhBaIPUJO2C/M0pFnnbZxKgJlX7t1Doy7h5eXxviymOIvaCZKU+x5OopfzM/wFkey0EPW"
  321. "NmzI5y/+pzU5afsdeEWdiQDIQc80H6Pz8fsoFPvYSG+s4/wz0duu7yeeV1Ypoho65Zr+pE"
  322. "nIf7dO0B8EblgWt+ud+JI8wrAhfE4x hansel\n"
  323. "ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQCqDwRVTRVk/wjPhoo66+Mztrc31KsxDZ"
  324. "+kAV0139PHQ+wsueNpba6jNn5o6mUTEOrxrz0LMsDJOBM7CmG0983kF4gRIihECpQ0rcjO"
  325. "P6BSfbVTE9mfIK5IsUiZGd8SoE9kSV2pJ2FvZeBQENoAxEFk0zZL9tchPS+OCUGbK4SDjz"
  326. "uNZl/30Mczs73N3MBzi6J1oPo7sFlqzB6ecBjK2Kpjus4Y1rYFphJnUxtKvB0s+hoaadru"
  327. "biE57dK6BrH5iZwVLTQKux31uCJLPhiktI3iLbdlGZEctJkTasfVSsUizwVIyRjhVKmbdI"
  328. "RGwkU38D043AR1h0mUoGCPIKuqcFMf gretel\n";
  329. static int LoadPasswordBuffer(byte* buf, word32 bufSz, PwMapList* list)
  330. {
  331. char* str = (char*)buf;
  332. char* delimiter;
  333. char* username;
  334. char* password;
  335. /* Each line of passwd.txt is in the format
  336. * username:password\n
  337. * This function modifies the passed-in buffer. */
  338. if (list == NULL)
  339. return -1;
  340. if (buf == NULL || bufSz == 0)
  341. return 0;
  342. while (*str != 0) {
  343. delimiter = strchr(str, ':');
  344. if (delimiter == NULL) {
  345. return -1;
  346. }
  347. username = str;
  348. *delimiter = 0;
  349. password = delimiter + 1;
  350. str = strchr(password, '\n');
  351. if (str == NULL) {
  352. return -1;
  353. }
  354. *str = 0;
  355. str++;
  356. if (PwMapNew(list, WOLFSSH_USERAUTH_PASSWORD,
  357. (byte*)username, (word32)strlen(username),
  358. (byte*)password, (word32)strlen(password)) == NULL ) {
  359. return -1;
  360. }
  361. }
  362. return 0;
  363. }
  364. static int LoadPublicKeyBuffer(byte* buf, word32 bufSz, PwMapList* list)
  365. {
  366. char* str = (char*)buf;
  367. char* delimiter;
  368. byte* publicKey64;
  369. word32 publicKey64Sz;
  370. byte* username;
  371. word32 usernameSz;
  372. byte publicKey[300];
  373. word32 publicKeySz;
  374. /* Each line of passwd.txt is in the format
  375. * ssh-rsa AAAB3BASE64ENCODEDPUBLICKEYBLOB username\n
  376. * This function modifies the passed-in buffer. */
  377. if (list == NULL)
  378. return -1;
  379. if (buf == NULL || bufSz == 0)
  380. return 0;
  381. while (*str != 0) {
  382. /* Skip the public key type. This example will always be ssh-rsa. */
  383. delimiter = strchr(str, ' ');
  384. if (delimiter == NULL) {
  385. return -1;
  386. }
  387. str = delimiter + 1;
  388. delimiter = strchr(str, ' ');
  389. if (delimiter == NULL) {
  390. return -1;
  391. }
  392. publicKey64 = (byte*)str;
  393. *delimiter = 0;
  394. publicKey64Sz = (word32)(delimiter - str);
  395. str = delimiter + 1;
  396. delimiter = strchr(str, '\n');
  397. if (delimiter == NULL) {
  398. return -1;
  399. }
  400. username = (byte*)str;
  401. *delimiter = 0;
  402. usernameSz = (word32)(delimiter - str);
  403. str = delimiter + 1;
  404. publicKeySz = sizeof(publicKey);
  405. if (Base64_Decode(publicKey64, publicKey64Sz,
  406. publicKey, &publicKeySz) != 0) {
  407. return -1;
  408. }
  409. if (PwMapNew(list, WOLFSSH_USERAUTH_PUBLICKEY,
  410. username, usernameSz,
  411. publicKey, publicKeySz) == NULL ) {
  412. return -1;
  413. }
  414. }
  415. return 0;
  416. }
  417. static int wsUserAuth(byte authType,
  418. WS_UserAuthData* authData,
  419. void* ctx)
  420. {
  421. PwMapList* list;
  422. PwMap* map;
  423. byte authHash[SHA256_DIGEST_SIZE];
  424. if (ctx == NULL) {
  425. printf("wsUserAuth: ctx not set");
  426. return WOLFSSH_USERAUTH_FAILURE;
  427. }
  428. if (authType != WOLFSSH_USERAUTH_PASSWORD &&
  429. authType != WOLFSSH_USERAUTH_PUBLICKEY) {
  430. return WOLFSSH_USERAUTH_FAILURE;
  431. }
  432. /* Hash the password or public key with its length. */
  433. {
  434. Sha256 sha;
  435. byte flatSz[4];
  436. wc_InitSha256(&sha);
  437. if (authType == WOLFSSH_USERAUTH_PASSWORD) {
  438. c32toa(authData->sf.password.passwordSz, flatSz);
  439. wc_Sha256Update(&sha, flatSz, sizeof(flatSz));
  440. wc_Sha256Update(&sha,
  441. authData->sf.password.password,
  442. authData->sf.password.passwordSz);
  443. }
  444. else if (authType == WOLFSSH_USERAUTH_PUBLICKEY) {
  445. c32toa(authData->sf.publicKey.publicKeySz, flatSz);
  446. wc_Sha256Update(&sha, flatSz, sizeof(flatSz));
  447. wc_Sha256Update(&sha,
  448. authData->sf.publicKey.publicKey,
  449. authData->sf.publicKey.publicKeySz);
  450. }
  451. wc_Sha256Final(&sha, authHash);
  452. }
  453. list = (PwMapList*)ctx;
  454. map = list->head;
  455. while (map != NULL) {
  456. if (authData->usernameSz == map->usernameSz &&
  457. memcmp(authData->username, map->username, map->usernameSz) == 0) {
  458. if (authData->type == map->type) {
  459. if (memcmp(map->p, authHash, SHA256_DIGEST_SIZE) == 0) {
  460. return WOLFSSH_USERAUTH_SUCCESS;
  461. }
  462. else {
  463. return (authType == WOLFSSH_USERAUTH_PASSWORD ?
  464. WOLFSSH_USERAUTH_INVALID_PASSWORD :
  465. WOLFSSH_USERAUTH_INVALID_PUBLICKEY);
  466. }
  467. }
  468. else {
  469. return WOLFSSH_USERAUTH_INVALID_AUTHTYPE;
  470. }
  471. }
  472. map = map->next;
  473. }
  474. return WOLFSSH_USERAUTH_INVALID_USER;
  475. }
  476. static void ssh_server(void *arg)
  477. {
  478. (void)arg;
  479. WSTARTTCP();
  480. #ifdef DEBUG_WOLFSSH
  481. wolfSSH_Debugging_ON();
  482. #endif
  483. wolfSSH_Init();
  484. WOLFSSH_CTX* ctx = NULL;
  485. PwMapList pwMapList;
  486. SOCKET_T listenFd = 0;
  487. word32 defaultHighwater = EXAMPLE_HIGHWATER_MARK;
  488. word32 threadCount = 0;
  489. word16 port = 22;
  490. const char multipleConnections = 1;
  491. char useEcc = 0;
  492. int ch;
  493. char nonBlock = 0;
  494. if (wolfSSH_Init() != WS_SUCCESS) {
  495. printf("Couldn't initialize wolfSSH.\n");
  496. exit(EXIT_FAILURE);
  497. }
  498. ctx = wolfSSH_CTX_new(WOLFSSH_ENDPOINT_SERVER, NULL);
  499. if (ctx == NULL) {
  500. printf("Couldn't allocate SSH CTX data.\n");
  501. exit(EXIT_FAILURE);
  502. }
  503. memset(&pwMapList, 0, sizeof(pwMapList));
  504. wolfSSH_SetUserAuth(ctx, wsUserAuth);
  505. wolfSSH_CTX_SetBanner(ctx, serverBanner);
  506. {
  507. const char* bufName;
  508. byte buf[SCRATCH_BUFFER_SZ];
  509. word32 bufSz;
  510. bufSz = load_key(useEcc, buf, SCRATCH_BUFFER_SZ);
  511. if (bufSz == 0) {
  512. printf("Couldn't load key.\n");
  513. exit(EXIT_FAILURE);
  514. }
  515. if (wolfSSH_CTX_UsePrivateKey_buffer(ctx, buf, bufSz,
  516. WOLFSSH_FORMAT_ASN1) < 0) {
  517. printf("Couldn't use key buffer.\n");
  518. exit(EXIT_FAILURE);
  519. }
  520. bufSz = (word32)strlen(samplePasswordBuffer);
  521. memcpy(buf, samplePasswordBuffer, bufSz);
  522. buf[bufSz] = 0;
  523. LoadPasswordBuffer(buf, bufSz, &pwMapList);
  524. bufName = useEcc ? samplePublicKeyEccBuffer :
  525. samplePublicKeyRsaBuffer;
  526. bufSz = (word32)strlen(bufName);
  527. memcpy(buf, bufName, bufSz);
  528. buf[bufSz] = 0;
  529. LoadPublicKeyBuffer(buf, bufSz, &pwMapList);
  530. }
  531. tcp_listen(&listenFd, &port, 1, false, false);
  532. do {
  533. SOCKET_T clientFd = 0;
  534. SOCKADDR_IN_T clientAddr;
  535. socklen_t clientAddrSz = sizeof(clientAddr);
  536. #ifndef SINGLE_THREADED
  537. THREAD_TYPE thread;
  538. #endif
  539. WOLFSSH* ssh;
  540. thread_ctx_t* threadCtx;
  541. threadCtx = (thread_ctx_t*)malloc(sizeof(thread_ctx_t));
  542. if (threadCtx == NULL) {
  543. printf("Couldn't allocate thread context data.\n");
  544. exit(EXIT_FAILURE);
  545. }
  546. ssh = wolfSSH_new(ctx);
  547. if (ssh == NULL) {
  548. printf("Couldn't allocate SSH data.\n");
  549. exit(EXIT_FAILURE);
  550. }
  551. wolfSSH_SetUserAuthCtx(ssh, &pwMapList);
  552. /* Use the session object for its own highwater callback ctx */
  553. if (defaultHighwater > 0) {
  554. wolfSSH_SetHighwaterCtx(ssh, (void*)ssh);
  555. wolfSSH_SetHighwater(ssh, defaultHighwater);
  556. }
  557. clientFd = accept(listenFd, (struct sockaddr*)&clientAddr,
  558. &clientAddrSz);
  559. if (clientFd == -1)
  560. err_sys("tcp accept failed");
  561. if (nonBlock)
  562. tcp_set_nonblocking(&clientFd);
  563. wolfSSH_set_fd(ssh, (int)clientFd);
  564. threadCtx->ssh = ssh;
  565. threadCtx->fd = clientFd;
  566. threadCtx->id = threadCount++;
  567. threadCtx->nonBlock = nonBlock;
  568. #ifndef SINGLE_THREADED
  569. ThreadStart(server_worker, threadCtx, &thread);
  570. if (multipleConnections)
  571. ThreadDetach(thread);
  572. else
  573. ThreadJoin(thread);
  574. #else
  575. server_worker(threadCtx);
  576. #endif /* SINGLE_THREADED */
  577. } while (multipleConnections);
  578. PwMapListDelete(&pwMapList);
  579. wolfSSH_CTX_free(ctx);
  580. if (wolfSSH_Cleanup() != WS_SUCCESS) {
  581. printf("Couldn't clean up wolfSSH.\n");
  582. exit(EXIT_FAILURE);
  583. }
  584. #if defined(HAVE_ECC) && defined(FP_ECC) && defined(HAVE_THREAD_LS)
  585. wc_ecc_fp_free(); /* free per thread cache */
  586. #endif
  587. return 0;
  588. }
  589. void ssh_server_init(void)
  590. {
  591. xTaskCreate(ssh_server, ( char * ) "ssh_server", 8*configMINIMAL_STACK_SIZE , NULL, tskIDLE_PRIORITY + 1, NULL);
  592. }