common-kex.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957
  1. /*
  2. * Dropbear SSH
  3. *
  4. * Copyright (c) 2002-2004 Matt Johnston
  5. * Portions 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 "dbutil.h"
  27. #include "algo.h"
  28. #include "buffer.h"
  29. #include "session.h"
  30. #include "kex.h"
  31. #include "dh_groups.h"
  32. #include "ssh.h"
  33. #include "packet.h"
  34. #include "bignum.h"
  35. #include "dbrandom.h"
  36. #include "runopts.h"
  37. #include "ecc.h"
  38. #include "crypto_desc.h"
  39. static void kexinitialise(void);
  40. static void gen_new_keys(void);
  41. #ifndef DISABLE_ZLIB
  42. static void gen_new_zstream_recv(void);
  43. static void gen_new_zstream_trans(void);
  44. #endif
  45. static void read_kex_algos(void);
  46. /* helper function for gen_new_keys */
  47. static void hashkeys(unsigned char *out, unsigned int outlen,
  48. const hash_state * hs, const unsigned char X);
  49. static void finish_kexhashbuf(void);
  50. /* Send our list of algorithms we can use */
  51. void send_msg_kexinit() {
  52. CHECKCLEARTOWRITE();
  53. buf_putbyte(ses.writepayload, SSH_MSG_KEXINIT);
  54. /* cookie */
  55. genrandom(buf_getwriteptr(ses.writepayload, 16), 16);
  56. buf_incrwritepos(ses.writepayload, 16);
  57. /* kex algos */
  58. buf_put_algolist(ses.writepayload, sshkex);
  59. /* server_host_key_algorithms */
  60. buf_put_algolist(ses.writepayload, sshhostkey);
  61. /* encryption_algorithms_client_to_server */
  62. buf_put_algolist(ses.writepayload, sshciphers);
  63. /* encryption_algorithms_server_to_client */
  64. buf_put_algolist(ses.writepayload, sshciphers);
  65. /* mac_algorithms_client_to_server */
  66. buf_put_algolist(ses.writepayload, sshhashes);
  67. /* mac_algorithms_server_to_client */
  68. buf_put_algolist(ses.writepayload, sshhashes);
  69. /* compression_algorithms_client_to_server */
  70. buf_put_algolist(ses.writepayload, ses.compress_algos);
  71. /* compression_algorithms_server_to_client */
  72. buf_put_algolist(ses.writepayload, ses.compress_algos);
  73. /* languages_client_to_server */
  74. buf_putstring(ses.writepayload, "", 0);
  75. /* languages_server_to_client */
  76. buf_putstring(ses.writepayload, "", 0);
  77. /* first_kex_packet_follows */
  78. buf_putbyte(ses.writepayload, (ses.send_kex_first_guess != NULL));
  79. /* reserved unit32 */
  80. buf_putint(ses.writepayload, 0);
  81. /* set up transmitted kex packet buffer for hashing.
  82. * This is freed after the end of the kex */
  83. ses.transkexinit = buf_newcopy(ses.writepayload);
  84. encrypt_packet();
  85. ses.dataallowed = 0; /* don't send other packets during kex */
  86. ses.kexstate.sentkexinit = 1;
  87. ses.newkeys = (struct key_context*)m_malloc(sizeof(struct key_context));
  88. if (ses.send_kex_first_guess) {
  89. ses.newkeys->algo_kex = sshkex[0].data;
  90. ses.newkeys->algo_hostkey = sshhostkey[0].val;
  91. ses.send_kex_first_guess();
  92. }
  93. TRACE(("DATAALLOWED=0"))
  94. TRACE(("-> KEXINIT"))
  95. }
  96. static void switch_keys() {
  97. TRACE2(("enter switch_keys"))
  98. if (!(ses.kexstate.sentkexinit && ses.kexstate.recvkexinit)) {
  99. dropbear_exit("Unexpected newkeys message");
  100. }
  101. if (!ses.keys) {
  102. ses.keys = m_malloc(sizeof(*ses.newkeys));
  103. }
  104. if (ses.kexstate.recvnewkeys && ses.newkeys->recv.valid) {
  105. TRACE(("switch_keys recv"))
  106. #ifndef DISABLE_ZLIB
  107. gen_new_zstream_recv();
  108. #endif
  109. ses.keys->recv = ses.newkeys->recv;
  110. m_burn(&ses.newkeys->recv, sizeof(ses.newkeys->recv));
  111. ses.newkeys->recv.valid = 0;
  112. }
  113. if (ses.kexstate.sentnewkeys && ses.newkeys->trans.valid) {
  114. TRACE(("switch_keys trans"))
  115. #ifndef DISABLE_ZLIB
  116. gen_new_zstream_trans();
  117. #endif
  118. ses.keys->trans = ses.newkeys->trans;
  119. m_burn(&ses.newkeys->trans, sizeof(ses.newkeys->trans));
  120. ses.newkeys->trans.valid = 0;
  121. }
  122. if (ses.kexstate.sentnewkeys && ses.kexstate.recvnewkeys)
  123. {
  124. TRACE(("switch_keys done"))
  125. ses.keys->algo_kex = ses.newkeys->algo_kex;
  126. ses.keys->algo_hostkey = ses.newkeys->algo_hostkey;
  127. ses.keys->allow_compress = 0;
  128. m_free(ses.newkeys);
  129. ses.newkeys = NULL;
  130. kexinitialise();
  131. }
  132. TRACE2(("leave switch_keys"))
  133. }
  134. /* Bring new keys into use after a key exchange, and let the client know*/
  135. void send_msg_newkeys() {
  136. TRACE(("enter send_msg_newkeys"))
  137. /* generate the kexinit request */
  138. CHECKCLEARTOWRITE();
  139. buf_putbyte(ses.writepayload, SSH_MSG_NEWKEYS);
  140. encrypt_packet();
  141. /* set up our state */
  142. ses.kexstate.sentnewkeys = 1;
  143. ses.kexstate.donefirstkex = 1;
  144. ses.dataallowed = 1; /* we can send other packets again now */
  145. gen_new_keys();
  146. switch_keys();
  147. TRACE(("leave send_msg_newkeys"))
  148. }
  149. /* Bring the new keys into use after a key exchange */
  150. void recv_msg_newkeys() {
  151. TRACE(("enter recv_msg_newkeys"))
  152. ses.kexstate.recvnewkeys = 1;
  153. switch_keys();
  154. TRACE(("leave recv_msg_newkeys"))
  155. }
  156. /* Set up the kex for the first time */
  157. void kexfirstinitialise() {
  158. ses.kexstate.donefirstkex = 0;
  159. #ifdef DISABLE_ZLIB
  160. ses.compress_algos = ssh_nocompress;
  161. #else
  162. switch (opts.compress_mode)
  163. {
  164. case DROPBEAR_COMPRESS_DELAYED:
  165. ses.compress_algos = ssh_delaycompress;
  166. break;
  167. case DROPBEAR_COMPRESS_ON:
  168. ses.compress_algos = ssh_compress;
  169. break;
  170. case DROPBEAR_COMPRESS_OFF:
  171. ses.compress_algos = ssh_nocompress;
  172. break;
  173. }
  174. #endif
  175. kexinitialise();
  176. }
  177. /* Reset the kex state, ready for a new negotiation */
  178. static void kexinitialise() {
  179. TRACE(("kexinitialise()"))
  180. /* sent/recv'd MSG_KEXINIT */
  181. ses.kexstate.sentkexinit = 0;
  182. ses.kexstate.recvkexinit = 0;
  183. /* sent/recv'd MSG_NEWKEYS */
  184. ses.kexstate.recvnewkeys = 0;
  185. ses.kexstate.sentnewkeys = 0;
  186. /* first_packet_follows */
  187. ses.kexstate.them_firstfollows = 0;
  188. ses.kexstate.datatrans = 0;
  189. ses.kexstate.datarecv = 0;
  190. ses.kexstate.our_first_follows_matches = 0;
  191. ses.kexstate.lastkextime = monotonic_now();
  192. }
  193. /* Helper function for gen_new_keys, creates a hash. It makes a copy of the
  194. * already initialised hash_state hs, which should already have processed
  195. * the dh_K and hash, since these are common. X is the letter 'A', 'B' etc.
  196. * out must have at least min(SHA1_HASH_SIZE, outlen) bytes allocated.
  197. *
  198. * See Section 7.2 of rfc4253 (ssh transport) for details */
  199. static void hashkeys(unsigned char *out, unsigned int outlen,
  200. const hash_state * hs, const unsigned char X) {
  201. const struct ltc_hash_descriptor *hash_desc = ses.newkeys->algo_kex->hash_desc;
  202. hash_state hs2;
  203. unsigned int offset;
  204. unsigned char tmpout[MAX_HASH_SIZE];
  205. memcpy(&hs2, hs, sizeof(hash_state));
  206. hash_desc->process(&hs2, &X, 1);
  207. hash_desc->process(&hs2, ses.session_id->data, ses.session_id->len);
  208. hash_desc->done(&hs2, tmpout);
  209. memcpy(out, tmpout, MIN(hash_desc->hashsize, outlen));
  210. for (offset = hash_desc->hashsize;
  211. offset < outlen;
  212. offset += hash_desc->hashsize)
  213. {
  214. /* need to extend */
  215. memcpy(&hs2, hs, sizeof(hash_state));
  216. hash_desc->process(&hs2, out, offset);
  217. hash_desc->done(&hs2, tmpout);
  218. memcpy(&out[offset], tmpout, MIN(outlen - offset, hash_desc->hashsize));
  219. }
  220. m_burn(&hs2, sizeof(hash_state));
  221. }
  222. /* Generate the actual encryption/integrity keys, using the results of the
  223. * key exchange, as specified in section 7.2 of the transport rfc 4253.
  224. * This occurs after the DH key-exchange.
  225. *
  226. * ses.newkeys is the new set of keys which are generated, these are only
  227. * taken into use after both sides have sent a newkeys message */
  228. static void gen_new_keys() {
  229. unsigned char C2S_IV[MAX_IV_LEN];
  230. unsigned char C2S_key[MAX_KEY_LEN];
  231. unsigned char S2C_IV[MAX_IV_LEN];
  232. unsigned char S2C_key[MAX_KEY_LEN];
  233. /* unsigned char key[MAX_KEY_LEN]; */
  234. unsigned char *trans_IV, *trans_key, *recv_IV, *recv_key;
  235. hash_state hs;
  236. const struct ltc_hash_descriptor *hash_desc = ses.newkeys->algo_kex->hash_desc;
  237. char mactransletter, macrecvletter; /* Client or server specific */
  238. TRACE(("enter gen_new_keys"))
  239. /* the dh_K and hash are the start of all hashes, we make use of that */
  240. hash_desc->init(&hs);
  241. hash_process_mp(hash_desc, &hs, ses.dh_K);
  242. mp_clear(ses.dh_K);
  243. m_free(ses.dh_K);
  244. hash_desc->process(&hs, ses.hash->data, ses.hash->len);
  245. buf_burn(ses.hash);
  246. buf_free(ses.hash);
  247. ses.hash = NULL;
  248. if (IS_DROPBEAR_CLIENT) {
  249. trans_IV = C2S_IV;
  250. recv_IV = S2C_IV;
  251. trans_key = C2S_key;
  252. recv_key = S2C_key;
  253. mactransletter = 'E';
  254. macrecvletter = 'F';
  255. } else {
  256. trans_IV = S2C_IV;
  257. recv_IV = C2S_IV;
  258. trans_key = S2C_key;
  259. recv_key = C2S_key;
  260. mactransletter = 'F';
  261. macrecvletter = 'E';
  262. }
  263. hashkeys(C2S_IV, sizeof(C2S_IV), &hs, 'A');
  264. hashkeys(S2C_IV, sizeof(S2C_IV), &hs, 'B');
  265. hashkeys(C2S_key, sizeof(C2S_key), &hs, 'C');
  266. hashkeys(S2C_key, sizeof(S2C_key), &hs, 'D');
  267. if (ses.newkeys->recv.algo_crypt->cipherdesc != NULL) {
  268. int recv_cipher = find_cipher(ses.newkeys->recv.algo_crypt->cipherdesc->name);
  269. if (recv_cipher < 0)
  270. dropbear_exit("Crypto error");
  271. if (ses.newkeys->recv.crypt_mode->start(recv_cipher,
  272. recv_IV, recv_key,
  273. ses.newkeys->recv.algo_crypt->keysize, 0,
  274. &ses.newkeys->recv.cipher_state) != CRYPT_OK) {
  275. dropbear_exit("Crypto error");
  276. }
  277. }
  278. if (ses.newkeys->trans.algo_crypt->cipherdesc != NULL) {
  279. int trans_cipher = find_cipher(ses.newkeys->trans.algo_crypt->cipherdesc->name);
  280. if (trans_cipher < 0)
  281. dropbear_exit("Crypto error");
  282. if (ses.newkeys->trans.crypt_mode->start(trans_cipher,
  283. trans_IV, trans_key,
  284. ses.newkeys->trans.algo_crypt->keysize, 0,
  285. &ses.newkeys->trans.cipher_state) != CRYPT_OK) {
  286. dropbear_exit("Crypto error");
  287. }
  288. }
  289. if (ses.newkeys->trans.algo_mac->hash_desc != NULL) {
  290. hashkeys(ses.newkeys->trans.mackey,
  291. ses.newkeys->trans.algo_mac->keysize, &hs, mactransletter);
  292. ses.newkeys->trans.hash_index = find_hash(ses.newkeys->trans.algo_mac->hash_desc->name);
  293. }
  294. if (ses.newkeys->recv.algo_mac->hash_desc != NULL) {
  295. hashkeys(ses.newkeys->recv.mackey,
  296. ses.newkeys->recv.algo_mac->keysize, &hs, macrecvletter);
  297. ses.newkeys->recv.hash_index = find_hash(ses.newkeys->recv.algo_mac->hash_desc->name);
  298. }
  299. /* Ready to switch over */
  300. ses.newkeys->trans.valid = 1;
  301. ses.newkeys->recv.valid = 1;
  302. m_burn(C2S_IV, sizeof(C2S_IV));
  303. m_burn(C2S_key, sizeof(C2S_key));
  304. m_burn(S2C_IV, sizeof(S2C_IV));
  305. m_burn(S2C_key, sizeof(S2C_key));
  306. m_burn(&hs, sizeof(hash_state));
  307. TRACE(("leave gen_new_keys"))
  308. }
  309. #ifndef DISABLE_ZLIB
  310. int is_compress_trans() {
  311. return ses.keys->trans.algo_comp == DROPBEAR_COMP_ZLIB
  312. || (ses.authstate.authdone
  313. && ses.keys->trans.algo_comp == DROPBEAR_COMP_ZLIB_DELAY);
  314. }
  315. int is_compress_recv() {
  316. return ses.keys->recv.algo_comp == DROPBEAR_COMP_ZLIB
  317. || (ses.authstate.authdone
  318. && ses.keys->recv.algo_comp == DROPBEAR_COMP_ZLIB_DELAY);
  319. }
  320. /* Set up new zlib compression streams, close the old ones. Only
  321. * called from gen_new_keys() */
  322. static void gen_new_zstream_recv() {
  323. /* create new zstreams */
  324. if (ses.newkeys->recv.algo_comp == DROPBEAR_COMP_ZLIB
  325. || ses.newkeys->recv.algo_comp == DROPBEAR_COMP_ZLIB_DELAY) {
  326. ses.newkeys->recv.zstream = (z_streamp)m_malloc(sizeof(z_stream));
  327. ses.newkeys->recv.zstream->zalloc = Z_NULL;
  328. ses.newkeys->recv.zstream->zfree = Z_NULL;
  329. if (inflateInit(ses.newkeys->recv.zstream) != Z_OK) {
  330. dropbear_exit("zlib error");
  331. }
  332. } else {
  333. ses.newkeys->recv.zstream = NULL;
  334. }
  335. /* clean up old keys */
  336. if (ses.keys->recv.zstream != NULL) {
  337. if (inflateEnd(ses.keys->recv.zstream) == Z_STREAM_ERROR) {
  338. /* Z_DATA_ERROR is ok, just means that stream isn't ended */
  339. dropbear_exit("Crypto error");
  340. }
  341. m_free(ses.keys->recv.zstream);
  342. }
  343. }
  344. static void gen_new_zstream_trans() {
  345. if (ses.newkeys->trans.algo_comp == DROPBEAR_COMP_ZLIB
  346. || ses.newkeys->trans.algo_comp == DROPBEAR_COMP_ZLIB_DELAY) {
  347. ses.newkeys->trans.zstream = (z_streamp)m_malloc(sizeof(z_stream));
  348. ses.newkeys->trans.zstream->zalloc = Z_NULL;
  349. ses.newkeys->trans.zstream->zfree = Z_NULL;
  350. if (deflateInit2(ses.newkeys->trans.zstream, Z_DEFAULT_COMPRESSION,
  351. Z_DEFLATED, DROPBEAR_ZLIB_WINDOW_BITS,
  352. DROPBEAR_ZLIB_MEM_LEVEL, Z_DEFAULT_STRATEGY)
  353. != Z_OK) {
  354. dropbear_exit("zlib error");
  355. }
  356. } else {
  357. ses.newkeys->trans.zstream = NULL;
  358. }
  359. if (ses.keys->trans.zstream != NULL) {
  360. if (deflateEnd(ses.keys->trans.zstream) == Z_STREAM_ERROR) {
  361. /* Z_DATA_ERROR is ok, just means that stream isn't ended */
  362. dropbear_exit("Crypto error");
  363. }
  364. m_free(ses.keys->trans.zstream);
  365. }
  366. }
  367. #endif /* DISABLE_ZLIB */
  368. /* Executed upon receiving a kexinit message from the client to initiate
  369. * key exchange. If we haven't already done so, we send the list of our
  370. * preferred algorithms. The client's requested algorithms are processed,
  371. * and we calculate the first portion of the key-exchange-hash for used
  372. * later in the key exchange. No response is sent, as the client should
  373. * initiate the diffie-hellman key exchange */
  374. void recv_msg_kexinit() {
  375. unsigned int kexhashbuf_len = 0;
  376. unsigned int remote_ident_len = 0;
  377. unsigned int local_ident_len = 0;
  378. TRACE(("<- KEXINIT"))
  379. TRACE(("enter recv_msg_kexinit"))
  380. if (!ses.kexstate.sentkexinit) {
  381. /* we need to send a kex packet */
  382. send_msg_kexinit();
  383. TRACE(("continue recv_msg_kexinit: sent kexinit"))
  384. }
  385. /* start the kex hash */
  386. local_ident_len = strlen(LOCAL_IDENT);
  387. remote_ident_len = strlen(ses.remoteident);
  388. kexhashbuf_len = local_ident_len + remote_ident_len
  389. + ses.transkexinit->len + ses.payload->len
  390. + KEXHASHBUF_MAX_INTS;
  391. ses.kexhashbuf = buf_new(kexhashbuf_len);
  392. if (IS_DROPBEAR_CLIENT) {
  393. /* read the peer's choice of algos */
  394. read_kex_algos();
  395. /* V_C, the client's version string (CR and NL excluded) */
  396. buf_putstring(ses.kexhashbuf, LOCAL_IDENT, local_ident_len);
  397. /* V_S, the server's version string (CR and NL excluded) */
  398. buf_putstring(ses.kexhashbuf, ses.remoteident, remote_ident_len);
  399. /* I_C, the payload of the client's SSH_MSG_KEXINIT */
  400. buf_putstring(ses.kexhashbuf,
  401. (const char*)ses.transkexinit->data, ses.transkexinit->len);
  402. /* I_S, the payload of the server's SSH_MSG_KEXINIT */
  403. buf_setpos(ses.payload, ses.payload_beginning);
  404. buf_putstring(ses.kexhashbuf,
  405. (const char*)buf_getptr(ses.payload, ses.payload->len-ses.payload->pos),
  406. ses.payload->len-ses.payload->pos);
  407. ses.requirenext = SSH_MSG_KEXDH_REPLY;
  408. } else {
  409. /* SERVER */
  410. /* read the peer's choice of algos */
  411. read_kex_algos();
  412. /* V_C, the client's version string (CR and NL excluded) */
  413. buf_putstring(ses.kexhashbuf, ses.remoteident, remote_ident_len);
  414. /* V_S, the server's version string (CR and NL excluded) */
  415. buf_putstring(ses.kexhashbuf, LOCAL_IDENT, local_ident_len);
  416. /* I_C, the payload of the client's SSH_MSG_KEXINIT */
  417. buf_setpos(ses.payload, ses.payload_beginning);
  418. buf_putstring(ses.kexhashbuf,
  419. (const char*)buf_getptr(ses.payload, ses.payload->len-ses.payload->pos),
  420. ses.payload->len-ses.payload->pos);
  421. /* I_S, the payload of the server's SSH_MSG_KEXINIT */
  422. buf_putstring(ses.kexhashbuf,
  423. (const char*)ses.transkexinit->data, ses.transkexinit->len);
  424. ses.requirenext = SSH_MSG_KEXDH_INIT;
  425. }
  426. buf_free(ses.transkexinit);
  427. ses.transkexinit = NULL;
  428. /* the rest of ses.kexhashbuf will be done after DH exchange */
  429. ses.kexstate.recvkexinit = 1;
  430. TRACE(("leave recv_msg_kexinit"))
  431. }
  432. static void load_dh_p(mp_int * dh_p)
  433. {
  434. bytes_to_mp(dh_p, ses.newkeys->algo_kex->dh_p_bytes,
  435. ses.newkeys->algo_kex->dh_p_len);
  436. }
  437. /* Initialises and generate one side of the diffie-hellman key exchange values.
  438. * See the transport rfc 4253 section 8 for details */
  439. /* dh_pub and dh_priv MUST be already initialised */
  440. struct kex_dh_param *gen_kexdh_param() {
  441. struct kex_dh_param *param = NULL;
  442. DEF_MP_INT(dh_p);
  443. DEF_MP_INT(dh_q);
  444. DEF_MP_INT(dh_g);
  445. TRACE(("enter gen_kexdh_vals"))
  446. param = m_malloc(sizeof(*param));
  447. m_mp_init_multi(&param->pub, &param->priv, &dh_g, &dh_p, &dh_q, NULL);
  448. /* read the prime and generator*/
  449. load_dh_p(&dh_p);
  450. if (mp_set_int(&dh_g, DH_G_VAL) != MP_OKAY) {
  451. dropbear_exit("Diffie-Hellman error");
  452. }
  453. /* calculate q = (p-1)/2 */
  454. /* dh_priv is just a temp var here */
  455. if (mp_sub_d(&dh_p, 1, &param->priv) != MP_OKAY) {
  456. dropbear_exit("Diffie-Hellman error");
  457. }
  458. if (mp_div_2(&param->priv, &dh_q) != MP_OKAY) {
  459. dropbear_exit("Diffie-Hellman error");
  460. }
  461. /* Generate a private portion 0 < dh_priv < dh_q */
  462. gen_random_mpint(&dh_q, &param->priv);
  463. /* f = g^y mod p */
  464. if (mp_exptmod(&dh_g, &param->priv, &dh_p, &param->pub) != MP_OKAY) {
  465. dropbear_exit("Diffie-Hellman error");
  466. }
  467. mp_clear_multi(&dh_g, &dh_p, &dh_q, NULL);
  468. return param;
  469. }
  470. void free_kexdh_param(struct kex_dh_param *param)
  471. {
  472. mp_clear_multi(&param->pub, &param->priv, NULL);
  473. m_free(param);
  474. }
  475. /* This function is fairly common between client/server, with some substitution
  476. * of dh_e/dh_f etc. Hence these arguments:
  477. * dh_pub_us is 'e' for the client, 'f' for the server. dh_pub_them is
  478. * vice-versa. dh_priv is the x/y value corresponding to dh_pub_us */
  479. void kexdh_comb_key(struct kex_dh_param *param, mp_int *dh_pub_them,
  480. sign_key *hostkey) {
  481. DEF_MP_INT(dh_p);
  482. DEF_MP_INT(dh_p_min1);
  483. mp_int *dh_e = NULL, *dh_f = NULL;
  484. m_mp_init_multi(&dh_p, &dh_p_min1, NULL);
  485. load_dh_p(&dh_p);
  486. if (mp_sub_d(&dh_p, 1, &dh_p_min1) != MP_OKAY) {
  487. dropbear_exit("Diffie-Hellman error");
  488. }
  489. /* Check that dh_pub_them (dh_e or dh_f) is in the range [2, p-2] */
  490. if (mp_cmp(dh_pub_them, &dh_p_min1) != MP_LT
  491. || mp_cmp_d(dh_pub_them, 1) != MP_GT) {
  492. dropbear_exit("Diffie-Hellman error");
  493. }
  494. /* K = e^y mod p = f^x mod p */
  495. m_mp_alloc_init_multi(&ses.dh_K, NULL);
  496. if (mp_exptmod(dh_pub_them, &param->priv, &dh_p, ses.dh_K) != MP_OKAY) {
  497. dropbear_exit("Diffie-Hellman error");
  498. }
  499. /* clear no longer needed vars */
  500. mp_clear_multi(&dh_p, &dh_p_min1, NULL);
  501. /* From here on, the code needs to work with the _same_ vars on each side,
  502. * not vice-versaing for client/server */
  503. if (IS_DROPBEAR_CLIENT) {
  504. dh_e = &param->pub;
  505. dh_f = dh_pub_them;
  506. } else {
  507. dh_e = dh_pub_them;
  508. dh_f = &param->pub;
  509. }
  510. /* Create the remainder of the hash buffer, to generate the exchange hash */
  511. /* K_S, the host key */
  512. buf_put_pub_key(ses.kexhashbuf, hostkey, ses.newkeys->algo_hostkey);
  513. /* e, exchange value sent by the client */
  514. buf_putmpint(ses.kexhashbuf, dh_e);
  515. /* f, exchange value sent by the server */
  516. buf_putmpint(ses.kexhashbuf, dh_f);
  517. /* K, the shared secret */
  518. buf_putmpint(ses.kexhashbuf, ses.dh_K);
  519. /* calculate the hash H to sign */
  520. finish_kexhashbuf();
  521. }
  522. #ifdef DROPBEAR_ECDH
  523. struct kex_ecdh_param *gen_kexecdh_param() {
  524. struct kex_ecdh_param *param = m_malloc(sizeof(*param));
  525. if (ecc_make_key_ex(NULL, dropbear_ltc_prng,
  526. &param->key, ses.newkeys->algo_kex->ecc_curve->dp) != CRYPT_OK) {
  527. dropbear_exit("ECC error");
  528. }
  529. return param;
  530. }
  531. void free_kexecdh_param(struct kex_ecdh_param *param) {
  532. ecc_free(&param->key);
  533. m_free(param);
  534. }
  535. void kexecdh_comb_key(struct kex_ecdh_param *param, buffer *pub_them,
  536. sign_key *hostkey) {
  537. const struct dropbear_kex *algo_kex = ses.newkeys->algo_kex;
  538. /* public keys from client and server */
  539. ecc_key *Q_C, *Q_S, *Q_them;
  540. Q_them = buf_get_ecc_raw_pubkey(pub_them, algo_kex->ecc_curve);
  541. if (Q_them == NULL) {
  542. dropbear_exit("ECC error");
  543. }
  544. ses.dh_K = dropbear_ecc_shared_secret(Q_them, &param->key);
  545. /* Create the remainder of the hash buffer, to generate the exchange hash
  546. See RFC5656 section 4 page 7 */
  547. if (IS_DROPBEAR_CLIENT) {
  548. Q_C = &param->key;
  549. Q_S = Q_them;
  550. } else {
  551. Q_C = Q_them;
  552. Q_S = &param->key;
  553. }
  554. /* K_S, the host key */
  555. buf_put_pub_key(ses.kexhashbuf, hostkey, ses.newkeys->algo_hostkey);
  556. /* Q_C, client's ephemeral public key octet string */
  557. buf_put_ecc_raw_pubkey_string(ses.kexhashbuf, Q_C);
  558. /* Q_S, server's ephemeral public key octet string */
  559. buf_put_ecc_raw_pubkey_string(ses.kexhashbuf, Q_S);
  560. /* K, the shared secret */
  561. buf_putmpint(ses.kexhashbuf, ses.dh_K);
  562. /* calculate the hash H to sign */
  563. finish_kexhashbuf();
  564. }
  565. #endif /* DROPBEAR_ECDH */
  566. #ifdef DROPBEAR_CURVE25519
  567. struct kex_curve25519_param *gen_kexcurve25519_param () {
  568. /* Per http://cr.yp.to/ecdh.html */
  569. struct kex_curve25519_param *param = m_malloc(sizeof(*param));
  570. const unsigned char basepoint[32] = {9};
  571. genrandom(param->priv, CURVE25519_LEN);
  572. param->priv[0] &= 248;
  573. param->priv[31] &= 127;
  574. param->priv[31] |= 64;
  575. curve25519_donna(param->pub, param->priv, basepoint);
  576. return param;
  577. }
  578. void free_kexcurve25519_param(struct kex_curve25519_param *param)
  579. {
  580. m_burn(param->priv, CURVE25519_LEN);
  581. m_free(param);
  582. }
  583. void kexcurve25519_comb_key(struct kex_curve25519_param *param, buffer *buf_pub_them,
  584. sign_key *hostkey) {
  585. unsigned char out[CURVE25519_LEN];
  586. const unsigned char* Q_C = NULL;
  587. const unsigned char* Q_S = NULL;
  588. char zeroes[CURVE25519_LEN] = {0};
  589. if (buf_pub_them->len != CURVE25519_LEN)
  590. {
  591. dropbear_exit("Bad curve25519");
  592. }
  593. curve25519_donna(out, param->priv, buf_pub_them->data);
  594. if (constant_time_memcmp(zeroes, out, CURVE25519_LEN) == 0) {
  595. dropbear_exit("Bad curve25519");
  596. }
  597. m_mp_alloc_init_multi(&ses.dh_K, NULL);
  598. bytes_to_mp(ses.dh_K, out, CURVE25519_LEN);
  599. m_burn(out, sizeof(out));
  600. /* Create the remainder of the hash buffer, to generate the exchange hash.
  601. See RFC5656 section 4 page 7 */
  602. if (IS_DROPBEAR_CLIENT) {
  603. Q_C = param->pub;
  604. Q_S = buf_pub_them->data;
  605. } else {
  606. Q_S = param->pub;
  607. Q_C = buf_pub_them->data;
  608. }
  609. /* K_S, the host key */
  610. buf_put_pub_key(ses.kexhashbuf, hostkey, ses.newkeys->algo_hostkey);
  611. /* Q_C, client's ephemeral public key octet string */
  612. buf_putstring(ses.kexhashbuf, (const char*)Q_C, CURVE25519_LEN);
  613. /* Q_S, server's ephemeral public key octet string */
  614. buf_putstring(ses.kexhashbuf, (const char*)Q_S, CURVE25519_LEN);
  615. /* K, the shared secret */
  616. buf_putmpint(ses.kexhashbuf, ses.dh_K);
  617. /* calculate the hash H to sign */
  618. finish_kexhashbuf();
  619. }
  620. #endif /* DROPBEAR_CURVE25519 */
  621. static void finish_kexhashbuf(void) {
  622. hash_state hs;
  623. const struct ltc_hash_descriptor *hash_desc = ses.newkeys->algo_kex->hash_desc;
  624. hash_desc->init(&hs);
  625. buf_setpos(ses.kexhashbuf, 0);
  626. hash_desc->process(&hs, buf_getptr(ses.kexhashbuf, ses.kexhashbuf->len),
  627. ses.kexhashbuf->len);
  628. ses.hash = buf_new(hash_desc->hashsize);
  629. hash_desc->done(&hs, buf_getwriteptr(ses.hash, hash_desc->hashsize));
  630. buf_setlen(ses.hash, hash_desc->hashsize);
  631. #if defined(DEBUG_KEXHASH) && defined(DEBUG_TRACE)
  632. if (!debug_trace) {
  633. printhex("kexhashbuf", ses.kexhashbuf->data, ses.kexhashbuf->len);
  634. printhex("kexhash", ses.hash->data, ses.hash->len);
  635. }
  636. #endif
  637. buf_burn(ses.kexhashbuf);
  638. buf_free(ses.kexhashbuf);
  639. m_burn(&hs, sizeof(hash_state));
  640. ses.kexhashbuf = NULL;
  641. /* first time around, we set the session_id to H */
  642. if (ses.session_id == NULL) {
  643. /* create the session_id, this never needs freeing */
  644. ses.session_id = buf_newcopy(ses.hash);
  645. }
  646. }
  647. /* read the other side's algo list. buf_match_algo is a callback to match
  648. * algos for the client or server. */
  649. static void read_kex_algos() {
  650. /* for asymmetry */
  651. algo_type * c2s_hash_algo = NULL;
  652. algo_type * s2c_hash_algo = NULL;
  653. algo_type * c2s_cipher_algo = NULL;
  654. algo_type * s2c_cipher_algo = NULL;
  655. algo_type * c2s_comp_algo = NULL;
  656. algo_type * s2c_comp_algo = NULL;
  657. /* the generic one */
  658. algo_type * algo = NULL;
  659. /* which algo couldn't match */
  660. char * erralgo = NULL;
  661. int goodguess = 0;
  662. int allgood = 1; /* we AND this with each goodguess and see if its still
  663. true after */
  664. #ifdef USE_KEXGUESS2
  665. enum kexguess2_used kexguess2 = KEXGUESS2_LOOK;
  666. #else
  667. enum kexguess2_used kexguess2 = KEXGUESS2_NO;
  668. #endif
  669. buf_incrpos(ses.payload, 16); /* start after the cookie */
  670. memset(ses.newkeys, 0x0, sizeof(*ses.newkeys));
  671. /* kex_algorithms */
  672. algo = buf_match_algo(ses.payload, sshkex, &kexguess2, &goodguess);
  673. allgood &= goodguess;
  674. if (algo == NULL || algo->val == KEXGUESS2_ALGO_ID) {
  675. erralgo = "kex";
  676. goto error;
  677. }
  678. TRACE(("kexguess2 %d", kexguess2))
  679. TRACE(("kex algo %s", algo->name))
  680. ses.newkeys->algo_kex = algo->data;
  681. /* server_host_key_algorithms */
  682. algo = buf_match_algo(ses.payload, sshhostkey, &kexguess2, &goodguess);
  683. allgood &= goodguess;
  684. if (algo == NULL) {
  685. erralgo = "hostkey";
  686. goto error;
  687. }
  688. TRACE(("hostkey algo %s", algo->name))
  689. ses.newkeys->algo_hostkey = algo->val;
  690. /* encryption_algorithms_client_to_server */
  691. c2s_cipher_algo = buf_match_algo(ses.payload, sshciphers, NULL, NULL);
  692. if (c2s_cipher_algo == NULL) {
  693. erralgo = "enc c->s";
  694. goto error;
  695. }
  696. TRACE(("enc c2s is %s", c2s_cipher_algo->name))
  697. /* encryption_algorithms_server_to_client */
  698. s2c_cipher_algo = buf_match_algo(ses.payload, sshciphers, NULL, NULL);
  699. if (s2c_cipher_algo == NULL) {
  700. erralgo = "enc s->c";
  701. goto error;
  702. }
  703. TRACE(("enc s2c is %s", s2c_cipher_algo->name))
  704. /* mac_algorithms_client_to_server */
  705. c2s_hash_algo = buf_match_algo(ses.payload, sshhashes, NULL, NULL);
  706. if (c2s_hash_algo == NULL) {
  707. erralgo = "mac c->s";
  708. goto error;
  709. }
  710. TRACE(("hash c2s is %s", c2s_hash_algo->name))
  711. /* mac_algorithms_server_to_client */
  712. s2c_hash_algo = buf_match_algo(ses.payload, sshhashes, NULL, NULL);
  713. if (s2c_hash_algo == NULL) {
  714. erralgo = "mac s->c";
  715. goto error;
  716. }
  717. TRACE(("hash s2c is %s", s2c_hash_algo->name))
  718. /* compression_algorithms_client_to_server */
  719. c2s_comp_algo = buf_match_algo(ses.payload, ses.compress_algos, NULL, NULL);
  720. if (c2s_comp_algo == NULL) {
  721. erralgo = "comp c->s";
  722. goto error;
  723. }
  724. TRACE(("hash c2s is %s", c2s_comp_algo->name))
  725. /* compression_algorithms_server_to_client */
  726. s2c_comp_algo = buf_match_algo(ses.payload, ses.compress_algos, NULL, NULL);
  727. if (s2c_comp_algo == NULL) {
  728. erralgo = "comp s->c";
  729. goto error;
  730. }
  731. TRACE(("hash s2c is %s", s2c_comp_algo->name))
  732. /* languages_client_to_server */
  733. buf_eatstring(ses.payload);
  734. /* languages_server_to_client */
  735. buf_eatstring(ses.payload);
  736. /* their first_kex_packet_follows */
  737. if (buf_getbool(ses.payload)) {
  738. TRACE(("them kex firstfollows. allgood %d", allgood))
  739. ses.kexstate.them_firstfollows = 1;
  740. /* if the guess wasn't good, we ignore the packet sent */
  741. if (!allgood) {
  742. ses.ignorenext = 1;
  743. }
  744. }
  745. /* Handle the asymmetry */
  746. if (IS_DROPBEAR_CLIENT) {
  747. ses.newkeys->recv.algo_crypt =
  748. (struct dropbear_cipher*)s2c_cipher_algo->data;
  749. ses.newkeys->trans.algo_crypt =
  750. (struct dropbear_cipher*)c2s_cipher_algo->data;
  751. ses.newkeys->recv.crypt_mode =
  752. (struct dropbear_cipher_mode*)s2c_cipher_algo->mode;
  753. ses.newkeys->trans.crypt_mode =
  754. (struct dropbear_cipher_mode*)c2s_cipher_algo->mode;
  755. ses.newkeys->recv.algo_mac =
  756. (struct dropbear_hash*)s2c_hash_algo->data;
  757. ses.newkeys->trans.algo_mac =
  758. (struct dropbear_hash*)c2s_hash_algo->data;
  759. ses.newkeys->recv.algo_comp = s2c_comp_algo->val;
  760. ses.newkeys->trans.algo_comp = c2s_comp_algo->val;
  761. } else {
  762. /* SERVER */
  763. ses.newkeys->recv.algo_crypt =
  764. (struct dropbear_cipher*)c2s_cipher_algo->data;
  765. ses.newkeys->trans.algo_crypt =
  766. (struct dropbear_cipher*)s2c_cipher_algo->data;
  767. ses.newkeys->recv.crypt_mode =
  768. (struct dropbear_cipher_mode*)c2s_cipher_algo->mode;
  769. ses.newkeys->trans.crypt_mode =
  770. (struct dropbear_cipher_mode*)s2c_cipher_algo->mode;
  771. ses.newkeys->recv.algo_mac =
  772. (struct dropbear_hash*)c2s_hash_algo->data;
  773. ses.newkeys->trans.algo_mac =
  774. (struct dropbear_hash*)s2c_hash_algo->data;
  775. ses.newkeys->recv.algo_comp = c2s_comp_algo->val;
  776. ses.newkeys->trans.algo_comp = s2c_comp_algo->val;
  777. }
  778. /* reserved for future extensions */
  779. buf_getint(ses.payload);
  780. if (ses.send_kex_first_guess && allgood) {
  781. TRACE(("our_first_follows_matches 1"))
  782. ses.kexstate.our_first_follows_matches = 1;
  783. }
  784. return;
  785. error:
  786. dropbear_exit("No matching algo %s", erralgo);
  787. }