common-kex.c 30 KB

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