cli-kex.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410
  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. return;
  170. fp = sign_key_fingerprint(keyblob, keybloblen);
  171. if (cli_opts.always_accept_key) {
  172. dropbear_log(LOG_INFO, "\nHost '%s' key accepted unconditionally.\n(%s fingerprint %s)\n",
  173. cli_opts.remotehost,
  174. algoname,
  175. fp);
  176. m_free(fp);
  177. return;
  178. }
  179. fprintf(stderr, "\nHost '%s' is not in the trusted hosts file.\n(%s fingerprint %s)\nDo you want to continue connecting? (y/n) ",
  180. cli_opts.remotehost,
  181. algoname,
  182. fp);
  183. m_free(fp);
  184. tty = fopen(_PATH_TTY, "r");
  185. if (tty) {
  186. response = getc(tty);
  187. fclose(tty);
  188. } else {
  189. response = getc(stdin);
  190. }
  191. if (response == 'y') {
  192. return;
  193. }
  194. dropbear_exit("Didn't validate host key");
  195. }
  196. static FILE* open_known_hosts_file(int * readonly)
  197. {
  198. FILE * hostsfile = NULL;
  199. char * filename = NULL;
  200. char * homedir = NULL;
  201. homedir = getenv("HOME");
  202. if (!homedir) {
  203. struct passwd * pw = NULL;
  204. pw = getpwuid(getuid());
  205. if (pw) {
  206. homedir = pw->pw_dir;
  207. }
  208. }
  209. if (homedir) {
  210. unsigned int len;
  211. len = strlen(homedir);
  212. filename = m_malloc(len + 18); /* "/.ssh/known_hosts" and null-terminator*/
  213. snprintf(filename, len+18, "%s/.ssh", homedir);
  214. /* Check that ~/.ssh exists - easiest way is just to mkdir */
  215. if (mkdir(filename, S_IRWXU) != 0) {
  216. if (errno != EEXIST) {
  217. dropbear_log(LOG_INFO, "Warning: failed creating %s/.ssh: %s",
  218. homedir, strerror(errno));
  219. TRACE(("mkdir didn't work: %s", strerror(errno)))
  220. goto out;
  221. }
  222. }
  223. snprintf(filename, len+18, "%s/.ssh/known_hosts", homedir);
  224. hostsfile = fopen(filename, "a+");
  225. if (hostsfile != NULL) {
  226. *readonly = 0;
  227. fseek(hostsfile, 0, SEEK_SET);
  228. } else {
  229. /* We mightn't have been able to open it if it was read-only */
  230. if (errno == EACCES || errno == EROFS) {
  231. TRACE(("trying readonly: %s", strerror(errno)))
  232. *readonly = 1;
  233. hostsfile = fopen(filename, "r");
  234. }
  235. }
  236. }
  237. if (hostsfile == NULL) {
  238. TRACE(("hostsfile didn't open: %s", strerror(errno)))
  239. dropbear_log(LOG_WARNING, "Failed to open %s/.ssh/known_hosts",
  240. homedir);
  241. goto out;
  242. }
  243. out:
  244. m_free(filename);
  245. return hostsfile;
  246. }
  247. static void checkhostkey(unsigned char* keyblob, unsigned int keybloblen) {
  248. FILE *hostsfile = NULL;
  249. int readonly = 0;
  250. unsigned int hostlen, algolen;
  251. unsigned long len;
  252. const char *algoname = NULL;
  253. char * fingerprint = NULL;
  254. buffer * line = NULL;
  255. int ret;
  256. if (cli_opts.no_hostkey_check) {
  257. dropbear_log(LOG_INFO, "Caution, skipping hostkey check for %s\n", cli_opts.remotehost);
  258. return;
  259. }
  260. algoname = signkey_name_from_type(ses.newkeys->algo_hostkey, &algolen);
  261. hostsfile = open_known_hosts_file(&readonly);
  262. if (!hostsfile) {
  263. ask_to_confirm(keyblob, keybloblen, algoname);
  264. /* ask_to_confirm will exit upon failure */
  265. return;
  266. }
  267. line = buf_new(MAX_KNOWNHOSTS_LINE);
  268. hostlen = strlen(cli_opts.remotehost);
  269. do {
  270. if (buf_getline(line, hostsfile) == DROPBEAR_FAILURE) {
  271. TRACE(("failed reading line: prob EOF"))
  272. break;
  273. }
  274. /* The line is too short to be sensible */
  275. /* "30" is 'enough to hold ssh-dss plus the spaces, ie so we don't
  276. * buf_getfoo() past the end and die horribly - the base64 parsing
  277. * code is what tiptoes up to the end nicely */
  278. if (line->len < (hostlen+30) ) {
  279. TRACE(("line is too short to be sensible"))
  280. continue;
  281. }
  282. /* Compare hostnames */
  283. if (strncmp(cli_opts.remotehost, (const char *) buf_getptr(line, hostlen),
  284. hostlen) != 0) {
  285. continue;
  286. }
  287. buf_incrpos(line, hostlen);
  288. if (buf_getbyte(line) != ' ') {
  289. /* there wasn't a space after the hostname, something dodgy */
  290. TRACE(("missing space afte matching hostname"))
  291. continue;
  292. }
  293. if (strncmp((const char *) buf_getptr(line, algolen), algoname, algolen) != 0) {
  294. TRACE(("algo doesn't match"))
  295. continue;
  296. }
  297. buf_incrpos(line, algolen);
  298. if (buf_getbyte(line) != ' ') {
  299. TRACE(("missing space after algo"))
  300. continue;
  301. }
  302. /* Now we're at the interesting hostkey */
  303. ret = cmp_base64_key(keyblob, keybloblen, (const unsigned char *) algoname, algolen,
  304. line, &fingerprint);
  305. if (ret == DROPBEAR_SUCCESS) {
  306. /* Good matching key */
  307. TRACE(("good matching key"))
  308. goto out;
  309. }
  310. /* The keys didn't match. eep. Note that we're "leaking"
  311. the fingerprint strings here, but we're exiting anyway */
  312. dropbear_exit("\n\n%s host key mismatch for %s !\n"
  313. "Fingerprint is %s\n"
  314. "Expected %s\n"
  315. "If you know that the host key is correct you can\nremove the bad entry from ~/.ssh/known_hosts",
  316. algoname,
  317. cli_opts.remotehost,
  318. sign_key_fingerprint(keyblob, keybloblen),
  319. fingerprint ? fingerprint : "UNKNOWN");
  320. } while (1); /* keep going 'til something happens */
  321. /* Key doesn't exist yet */
  322. ask_to_confirm(keyblob, keybloblen, algoname);
  323. /* If we get here, they said yes */
  324. if (readonly) {
  325. TRACE(("readonly"))
  326. goto out;
  327. }
  328. if (!cli_opts.always_accept_key) {
  329. /* put the new entry in the file */
  330. fseek(hostsfile, 0, SEEK_END); /* In case it wasn't opened append */
  331. buf_setpos(line, 0);
  332. buf_setlen(line, 0);
  333. buf_putbytes(line, (const unsigned char *) cli_opts.remotehost, hostlen);
  334. buf_putbyte(line, ' ');
  335. buf_putbytes(line, (const unsigned char *) algoname, algolen);
  336. buf_putbyte(line, ' ');
  337. len = line->size - line->pos;
  338. /* The only failure with base64 is buffer_overflow, but buf_getwriteptr
  339. * will die horribly in the case anyway */
  340. base64_encode(keyblob, keybloblen, buf_getwriteptr(line, len), &len);
  341. buf_incrwritepos(line, len);
  342. buf_putbyte(line, '\n');
  343. buf_setpos(line, 0);
  344. fwrite(buf_getptr(line, line->len), line->len, 1, hostsfile);
  345. /* We ignore errors, since there's not much we can do about them */
  346. }
  347. out:
  348. if (hostsfile != NULL) {
  349. fclose(hostsfile);
  350. }
  351. if (line != NULL) {
  352. buf_free(line);
  353. }
  354. m_free(fingerprint);
  355. }