cli-kex.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408
  1. /*
  2. * Dropbear - a SSH2 server
  3. *
  4. * Copyright (c) 2002-2004 Matt Johnston
  5. * Copyright (c) 2004 by Mihnea Stoenescu
  6. * All rights reserved.
  7. *
  8. * Permission is hereby granted, free of charge, to any person obtaining a copy
  9. * of this software and associated documentation files (the "Software"), to deal
  10. * in the Software without restriction, including without limitation the rights
  11. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  12. * copies of the Software, and to permit persons to whom the Software is
  13. * furnished to do so, subject to the following conditions:
  14. *
  15. * The above copyright notice and this permission notice shall be included in
  16. * all copies or substantial portions of the Software.
  17. *
  18. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  19. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  20. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  21. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  22. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  23. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  24. * SOFTWARE. */
  25. #include "includes.h"
  26. #include "session.h"
  27. #include "dbutil.h"
  28. #include "algo.h"
  29. #include "buffer.h"
  30. #include "session.h"
  31. #include "kex.h"
  32. #include "ssh.h"
  33. #include "packet.h"
  34. #include "bignum.h"
  35. #include "dbrandom.h"
  36. #include "runopts.h"
  37. #include "signkey.h"
  38. #include "ecc.h"
  39. static void checkhostkey(unsigned char* keyblob, unsigned int keybloblen);
  40. #define MAX_KNOWNHOSTS_LINE 4500
  41. void send_msg_kexdh_init() {
  42. TRACE(("send_msg_kexdh_init()"))
  43. CHECKCLEARTOWRITE();
  44. buf_putbyte(ses.writepayload, SSH_MSG_KEXDH_INIT);
  45. switch (ses.newkeys->algo_kex->mode) {
  46. case DROPBEAR_KEX_NORMAL_DH:
  47. if (ses.newkeys->algo_kex != cli_ses.param_kex_algo
  48. || !cli_ses.dh_param) {
  49. if (cli_ses.dh_param) {
  50. free_kexdh_param(cli_ses.dh_param);
  51. }
  52. cli_ses.dh_param = gen_kexdh_param();
  53. }
  54. buf_putmpint(ses.writepayload, &cli_ses.dh_param->pub);
  55. break;
  56. case DROPBEAR_KEX_ECDH:
  57. #ifdef DROPBEAR_ECDH
  58. if (ses.newkeys->algo_kex != cli_ses.param_kex_algo
  59. || !cli_ses.ecdh_param) {
  60. if (cli_ses.ecdh_param) {
  61. free_kexecdh_param(cli_ses.ecdh_param);
  62. }
  63. cli_ses.ecdh_param = gen_kexecdh_param();
  64. }
  65. buf_put_ecc_raw_pubkey_string(ses.writepayload, &cli_ses.ecdh_param->key);
  66. #endif
  67. break;
  68. #ifdef DROPBEAR_CURVE25519
  69. case DROPBEAR_KEX_CURVE25519:
  70. if (ses.newkeys->algo_kex != cli_ses.param_kex_algo
  71. || !cli_ses.curve25519_param) {
  72. if (cli_ses.curve25519_param) {
  73. free_kexcurve25519_param(cli_ses.curve25519_param);
  74. }
  75. cli_ses.curve25519_param = gen_kexcurve25519_param();
  76. }
  77. buf_putstring(ses.writepayload, (const char*)cli_ses.curve25519_param->pub, CURVE25519_LEN);
  78. #endif
  79. break;
  80. }
  81. cli_ses.param_kex_algo = ses.newkeys->algo_kex;
  82. encrypt_packet();
  83. }
  84. /* Handle a diffie-hellman key exchange reply. */
  85. void recv_msg_kexdh_reply() {
  86. sign_key *hostkey = NULL;
  87. unsigned int type, keybloblen;
  88. unsigned char* keyblob = NULL;
  89. TRACE(("enter recv_msg_kexdh_reply"))
  90. if (cli_ses.kex_state != KEXDH_INIT_SENT) {
  91. dropbear_exit("Received out-of-order kexdhreply");
  92. }
  93. type = ses.newkeys->algo_hostkey;
  94. TRACE(("type is %d", type))
  95. hostkey = new_sign_key();
  96. keybloblen = buf_getint(ses.payload);
  97. keyblob = buf_getptr(ses.payload, keybloblen);
  98. if (!ses.kexstate.donefirstkex) {
  99. /* Only makes sense the first time */
  100. checkhostkey(keyblob, keybloblen);
  101. }
  102. if (buf_get_pub_key(ses.payload, hostkey, &type) != DROPBEAR_SUCCESS) {
  103. TRACE(("failed getting pubkey"))
  104. dropbear_exit("Bad KEX packet");
  105. }
  106. switch (ses.newkeys->algo_kex->mode) {
  107. case DROPBEAR_KEX_NORMAL_DH:
  108. {
  109. DEF_MP_INT(dh_f);
  110. m_mp_init(&dh_f);
  111. if (buf_getmpint(ses.payload, &dh_f) != DROPBEAR_SUCCESS) {
  112. TRACE(("failed getting mpint"))
  113. dropbear_exit("Bad KEX packet");
  114. }
  115. kexdh_comb_key(cli_ses.dh_param, &dh_f, hostkey);
  116. mp_clear(&dh_f);
  117. }
  118. break;
  119. case DROPBEAR_KEX_ECDH:
  120. #ifdef DROPBEAR_ECDH
  121. {
  122. buffer *ecdh_qs = buf_getstringbuf(ses.payload);
  123. kexecdh_comb_key(cli_ses.ecdh_param, ecdh_qs, hostkey);
  124. buf_free(ecdh_qs);
  125. }
  126. #endif
  127. break;
  128. #ifdef DROPBEAR_CURVE25519
  129. case DROPBEAR_KEX_CURVE25519:
  130. {
  131. buffer *ecdh_qs = buf_getstringbuf(ses.payload);
  132. kexcurve25519_comb_key(cli_ses.curve25519_param, ecdh_qs, hostkey);
  133. buf_free(ecdh_qs);
  134. }
  135. #endif
  136. break;
  137. }
  138. if (cli_ses.dh_param) {
  139. free_kexdh_param(cli_ses.dh_param);
  140. cli_ses.dh_param = NULL;
  141. }
  142. #ifdef DROPBEAR_ECDH
  143. if (cli_ses.ecdh_param) {
  144. free_kexecdh_param(cli_ses.ecdh_param);
  145. cli_ses.ecdh_param = NULL;
  146. }
  147. #endif
  148. #ifdef DROPBEAR_CURVE25519
  149. if (cli_ses.curve25519_param) {
  150. free_kexcurve25519_param(cli_ses.curve25519_param);
  151. cli_ses.curve25519_param = NULL;
  152. }
  153. #endif
  154. cli_ses.param_kex_algo = NULL;
  155. if (buf_verify(ses.payload, hostkey, ses.hash) != DROPBEAR_SUCCESS) {
  156. dropbear_exit("Bad hostkey signature");
  157. }
  158. sign_key_free(hostkey);
  159. hostkey = NULL;
  160. send_msg_newkeys();
  161. ses.requirenext = SSH_MSG_NEWKEYS;
  162. TRACE(("leave recv_msg_kexdh_init"))
  163. }
  164. static void ask_to_confirm(unsigned char* keyblob, unsigned int keybloblen,
  165. const char* algoname) {
  166. char* fp = NULL;
  167. FILE *tty = NULL;
  168. int response = 'z';
  169. fp = sign_key_fingerprint(keyblob, keybloblen);
  170. if (cli_opts.always_accept_key) {
  171. dropbear_log(LOG_INFO, "\nHost '%s' key accepted unconditionally.\n(%s fingerprint %s)\n",
  172. cli_opts.remotehost,
  173. algoname,
  174. fp);
  175. m_free(fp);
  176. return;
  177. }
  178. fprintf(stderr, "\nHost '%s' is not in the trusted hosts file.\n(%s fingerprint %s)\nDo you want to continue connecting? (y/n) ",
  179. cli_opts.remotehost,
  180. algoname,
  181. fp);
  182. m_free(fp);
  183. tty = fopen(_PATH_TTY, "r");
  184. if (tty) {
  185. response = getc(tty);
  186. fclose(tty);
  187. } else {
  188. response = getc(stdin);
  189. }
  190. if (response == 'y') {
  191. return;
  192. }
  193. dropbear_exit("Didn't validate host key");
  194. }
  195. static FILE* open_known_hosts_file(int * readonly)
  196. {
  197. FILE * hostsfile = NULL;
  198. char * filename = NULL;
  199. char * homedir = NULL;
  200. homedir = getenv("HOME");
  201. if (!homedir) {
  202. struct passwd * pw = NULL;
  203. pw = getpwuid(getuid());
  204. if (pw) {
  205. homedir = pw->pw_dir;
  206. }
  207. }
  208. if (homedir) {
  209. unsigned int len;
  210. len = strlen(homedir);
  211. filename = m_malloc(len + 18); /* "/.ssh/known_hosts" and null-terminator*/
  212. snprintf(filename, len+18, "%s/.ssh", homedir);
  213. /* Check that ~/.ssh exists - easiest way is just to mkdir */
  214. if (mkdir(filename, S_IRWXU) != 0) {
  215. if (errno != EEXIST) {
  216. dropbear_log(LOG_INFO, "Warning: failed creating %s/.ssh: %s",
  217. homedir, strerror(errno));
  218. TRACE(("mkdir didn't work: %s", strerror(errno)))
  219. goto out;
  220. }
  221. }
  222. snprintf(filename, len+18, "%s/.ssh/known_hosts", homedir);
  223. hostsfile = fopen(filename, "a+");
  224. if (hostsfile != NULL) {
  225. *readonly = 0;
  226. fseek(hostsfile, 0, SEEK_SET);
  227. } else {
  228. /* We mightn't have been able to open it if it was read-only */
  229. if (errno == EACCES || errno == EROFS) {
  230. TRACE(("trying readonly: %s", strerror(errno)))
  231. *readonly = 1;
  232. hostsfile = fopen(filename, "r");
  233. }
  234. }
  235. }
  236. if (hostsfile == NULL) {
  237. TRACE(("hostsfile didn't open: %s", strerror(errno)))
  238. dropbear_log(LOG_WARNING, "Failed to open %s/.ssh/known_hosts",
  239. homedir);
  240. goto out;
  241. }
  242. out:
  243. m_free(filename);
  244. return hostsfile;
  245. }
  246. static void checkhostkey(unsigned char* keyblob, unsigned int keybloblen) {
  247. FILE *hostsfile = NULL;
  248. int readonly = 0;
  249. unsigned int hostlen, algolen;
  250. unsigned long len;
  251. const char *algoname = NULL;
  252. char * fingerprint = NULL;
  253. buffer * line = NULL;
  254. int ret;
  255. if (cli_opts.no_hostkey_check) {
  256. dropbear_log(LOG_INFO, "Caution, skipping hostkey check for %s\n", cli_opts.remotehost);
  257. return;
  258. }
  259. algoname = signkey_name_from_type(ses.newkeys->algo_hostkey, &algolen);
  260. hostsfile = open_known_hosts_file(&readonly);
  261. if (!hostsfile) {
  262. ask_to_confirm(keyblob, keybloblen, algoname);
  263. /* ask_to_confirm will exit upon failure */
  264. return;
  265. }
  266. line = buf_new(MAX_KNOWNHOSTS_LINE);
  267. hostlen = strlen(cli_opts.remotehost);
  268. do {
  269. if (buf_getline(line, hostsfile) == DROPBEAR_FAILURE) {
  270. TRACE(("failed reading line: prob EOF"))
  271. break;
  272. }
  273. /* The line is too short to be sensible */
  274. /* "30" is 'enough to hold ssh-dss plus the spaces, ie so we don't
  275. * buf_getfoo() past the end and die horribly - the base64 parsing
  276. * code is what tiptoes up to the end nicely */
  277. if (line->len < (hostlen+30) ) {
  278. TRACE(("line is too short to be sensible"))
  279. continue;
  280. }
  281. /* Compare hostnames */
  282. if (strncmp(cli_opts.remotehost, (const char *) buf_getptr(line, hostlen),
  283. hostlen) != 0) {
  284. continue;
  285. }
  286. buf_incrpos(line, hostlen);
  287. if (buf_getbyte(line) != ' ') {
  288. /* there wasn't a space after the hostname, something dodgy */
  289. TRACE(("missing space afte matching hostname"))
  290. continue;
  291. }
  292. if (strncmp((const char *) buf_getptr(line, algolen), algoname, algolen) != 0) {
  293. TRACE(("algo doesn't match"))
  294. continue;
  295. }
  296. buf_incrpos(line, algolen);
  297. if (buf_getbyte(line) != ' ') {
  298. TRACE(("missing space after algo"))
  299. continue;
  300. }
  301. /* Now we're at the interesting hostkey */
  302. ret = cmp_base64_key(keyblob, keybloblen, (const unsigned char *) algoname, algolen,
  303. line, &fingerprint);
  304. if (ret == DROPBEAR_SUCCESS) {
  305. /* Good matching key */
  306. TRACE(("good matching key"))
  307. goto out;
  308. }
  309. /* The keys didn't match. eep. Note that we're "leaking"
  310. the fingerprint strings here, but we're exiting anyway */
  311. dropbear_exit("\n\n%s host key mismatch for %s !\n"
  312. "Fingerprint is %s\n"
  313. "Expected %s\n"
  314. "If you know that the host key is correct you can\nremove the bad entry from ~/.ssh/known_hosts",
  315. algoname,
  316. cli_opts.remotehost,
  317. sign_key_fingerprint(keyblob, keybloblen),
  318. fingerprint ? fingerprint : "UNKNOWN");
  319. } while (1); /* keep going 'til something happens */
  320. /* Key doesn't exist yet */
  321. ask_to_confirm(keyblob, keybloblen, algoname);
  322. /* If we get here, they said yes */
  323. if (readonly) {
  324. TRACE(("readonly"))
  325. goto out;
  326. }
  327. if (!cli_opts.always_accept_key) {
  328. /* put the new entry in the file */
  329. fseek(hostsfile, 0, SEEK_END); /* In case it wasn't opened append */
  330. buf_setpos(line, 0);
  331. buf_setlen(line, 0);
  332. buf_putbytes(line, (const unsigned char *) cli_opts.remotehost, hostlen);
  333. buf_putbyte(line, ' ');
  334. buf_putbytes(line, (const unsigned char *) algoname, algolen);
  335. buf_putbyte(line, ' ');
  336. len = line->size - line->pos;
  337. /* The only failure with base64 is buffer_overflow, but buf_getwriteptr
  338. * will die horribly in the case anyway */
  339. base64_encode(keyblob, keybloblen, buf_getwriteptr(line, len), &len);
  340. buf_incrwritepos(line, len);
  341. buf_putbyte(line, '\n');
  342. buf_setpos(line, 0);
  343. fwrite(buf_getptr(line, line->len), line->len, 1, hostsfile);
  344. /* We ignore errors, since there's not much we can do about them */
  345. }
  346. out:
  347. if (hostsfile != NULL) {
  348. fclose(hostsfile);
  349. }
  350. if (line != NULL) {
  351. buf_free(line);
  352. }
  353. m_free(fingerprint);
  354. }