server.c 20 KB

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