svr-kex.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. /*
  2. * Dropbear - a SSH2 server
  3. *
  4. * Copyright (c) 2002,2003 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 "dbutil.h"
  27. #include "algo.h"
  28. #include "buffer.h"
  29. #include "session.h"
  30. #include "kex.h"
  31. #include "ssh.h"
  32. #include "packet.h"
  33. #include "bignum.h"
  34. #include "dbrandom.h"
  35. #include "runopts.h"
  36. #include "ecc.h"
  37. #include "gensignkey.h"
  38. static void send_msg_kexdh_reply(mp_int *dh_e, buffer *ecdh_qs);
  39. #if DROPBEAR_EXT_INFO
  40. static void send_msg_ext_info(void);
  41. #endif
  42. /* Handle a diffie-hellman key exchange initialisation. This involves
  43. * calculating a session key reply value, and corresponding hash. These
  44. * are carried out by send_msg_kexdh_reply(). recv_msg_kexdh_init() calls
  45. * that function, then brings the new keys into use */
  46. void recv_msg_kexdh_init() {
  47. DEF_MP_INT(dh_e);
  48. buffer *ecdh_qs = NULL;
  49. TRACE(("enter recv_msg_kexdh_init"))
  50. if (!ses.kexstate.recvkexinit) {
  51. dropbear_exit("Premature kexdh_init message received");
  52. }
  53. switch (ses.newkeys->algo_kex->mode) {
  54. #if DROPBEAR_NORMAL_DH
  55. case DROPBEAR_KEX_NORMAL_DH:
  56. m_mp_init(&dh_e);
  57. if (buf_getmpint(ses.payload, &dh_e) != DROPBEAR_SUCCESS) {
  58. dropbear_exit("Bad kex value");
  59. }
  60. break;
  61. #endif
  62. #if DROPBEAR_ECDH
  63. case DROPBEAR_KEX_ECDH:
  64. #endif
  65. #if DROPBEAR_CURVE25519
  66. case DROPBEAR_KEX_CURVE25519:
  67. #endif
  68. #if DROPBEAR_ECDH || DROPBEAR_CURVE25519
  69. ecdh_qs = buf_getstringbuf(ses.payload);
  70. break;
  71. #endif
  72. }
  73. if (ses.payload->pos != ses.payload->len) {
  74. dropbear_exit("Bad kex value");
  75. }
  76. send_msg_kexdh_reply(&dh_e, ecdh_qs);
  77. mp_clear(&dh_e);
  78. if (ecdh_qs) {
  79. buf_free(ecdh_qs);
  80. ecdh_qs = NULL;
  81. }
  82. send_msg_newkeys();
  83. #if DROPBEAR_EXT_INFO
  84. /* Only send it following the first newkeys */
  85. if (!ses.kexstate.donesecondkex && ses.allow_ext_info) {
  86. send_msg_ext_info();
  87. }
  88. #endif
  89. ses.requirenext = SSH_MSG_NEWKEYS;
  90. TRACE(("leave recv_msg_kexdh_init"))
  91. }
  92. #if DROPBEAR_DELAY_HOSTKEY
  93. static void svr_ensure_hostkey() {
  94. const char* fn = NULL;
  95. char *expand_fn = NULL;
  96. enum signkey_type type = ses.newkeys->algo_hostkey;
  97. void **hostkey = signkey_key_ptr(svr_opts.hostkey, type);
  98. int ret = DROPBEAR_FAILURE;
  99. if (hostkey && *hostkey) {
  100. return;
  101. }
  102. switch (type)
  103. {
  104. #if DROPBEAR_RSA
  105. case DROPBEAR_SIGNKEY_RSA:
  106. fn = RSA_PRIV_FILENAME;
  107. break;
  108. #endif
  109. #if DROPBEAR_DSS
  110. case DROPBEAR_SIGNKEY_DSS:
  111. fn = DSS_PRIV_FILENAME;
  112. break;
  113. #endif
  114. #if DROPBEAR_ECDSA
  115. case DROPBEAR_SIGNKEY_ECDSA_NISTP256:
  116. case DROPBEAR_SIGNKEY_ECDSA_NISTP384:
  117. case DROPBEAR_SIGNKEY_ECDSA_NISTP521:
  118. fn = ECDSA_PRIV_FILENAME;
  119. break;
  120. #endif
  121. #if DROPBEAR_ED25519
  122. case DROPBEAR_SIGNKEY_ED25519:
  123. fn = ED25519_PRIV_FILENAME;
  124. break;
  125. #endif
  126. default:
  127. dropbear_assert(0);
  128. }
  129. expand_fn = expand_homedir_path(fn);
  130. ret = readhostkey(expand_fn, svr_opts.hostkey, &type);
  131. if (ret == DROPBEAR_SUCCESS) {
  132. goto out;
  133. }
  134. if (signkey_generate(type, 0, expand_fn, 1) == DROPBEAR_FAILURE) {
  135. goto out;
  136. }
  137. /* Read what we just generated (or another process raced us) */
  138. ret = readhostkey(expand_fn, svr_opts.hostkey, &type);
  139. if (ret == DROPBEAR_SUCCESS) {
  140. char *fp = NULL;
  141. unsigned int len;
  142. buffer *key_buf = buf_new(MAX_PUBKEY_SIZE);
  143. buf_put_pub_key(key_buf, svr_opts.hostkey, type);
  144. buf_setpos(key_buf, 4);
  145. len = key_buf->len - key_buf->pos;
  146. fp = sign_key_fingerprint(buf_getptr(key_buf, len), len);
  147. dropbear_log(LOG_INFO, "Generated hostkey %s, fingerprint is %s",
  148. expand_fn, fp);
  149. m_free(fp);
  150. buf_free(key_buf);
  151. }
  152. out:
  153. if (ret == DROPBEAR_FAILURE) {
  154. dropbear_exit("Couldn't read or generate hostkey %s", expand_fn);
  155. }
  156. m_free(expand_fn);
  157. }
  158. #endif
  159. /* Generate our side of the diffie-hellman key exchange value (dh_f), and
  160. * calculate the session key using the diffie-hellman algorithm. Following
  161. * that, the session hash is calculated, and signed with RSA or DSS. The
  162. * result is sent to the client.
  163. *
  164. * See the transport RFC4253 section 8 for details
  165. * or RFC5656 section 4 for elliptic curve variant. */
  166. static void send_msg_kexdh_reply(mp_int *dh_e, buffer *ecdh_qs) {
  167. TRACE(("enter send_msg_kexdh_reply"))
  168. /* we can start creating the kexdh_reply packet */
  169. CHECKCLEARTOWRITE();
  170. #if DROPBEAR_DELAY_HOSTKEY
  171. if (svr_opts.delay_hostkey)
  172. {
  173. svr_ensure_hostkey();
  174. }
  175. #endif
  176. #if DROPBEAR_FUZZ
  177. if (fuzz.fuzzing && fuzz.skip_kexmaths) {
  178. fuzz_fake_send_kexdh_reply();
  179. return;
  180. }
  181. #endif
  182. buf_putbyte(ses.writepayload, SSH_MSG_KEXDH_REPLY);
  183. buf_put_pub_key(ses.writepayload, svr_opts.hostkey,
  184. ses.newkeys->algo_hostkey);
  185. switch (ses.newkeys->algo_kex->mode) {
  186. #if DROPBEAR_NORMAL_DH
  187. case DROPBEAR_KEX_NORMAL_DH:
  188. {
  189. struct kex_dh_param * dh_param = gen_kexdh_param();
  190. kexdh_comb_key(dh_param, dh_e, svr_opts.hostkey);
  191. /* put f */
  192. buf_putmpint(ses.writepayload, &dh_param->pub);
  193. free_kexdh_param(dh_param);
  194. }
  195. break;
  196. #endif
  197. #if DROPBEAR_ECDH
  198. case DROPBEAR_KEX_ECDH:
  199. {
  200. struct kex_ecdh_param *ecdh_param = gen_kexecdh_param();
  201. kexecdh_comb_key(ecdh_param, ecdh_qs, svr_opts.hostkey);
  202. buf_put_ecc_raw_pubkey_string(ses.writepayload, &ecdh_param->key);
  203. free_kexecdh_param(ecdh_param);
  204. }
  205. break;
  206. #endif
  207. #if DROPBEAR_CURVE25519
  208. case DROPBEAR_KEX_CURVE25519:
  209. {
  210. struct kex_curve25519_param *param = gen_kexcurve25519_param();
  211. kexcurve25519_comb_key(param, ecdh_qs, svr_opts.hostkey);
  212. buf_putstring(ses.writepayload, param->pub, CURVE25519_LEN);
  213. free_kexcurve25519_param(param);
  214. }
  215. break;
  216. #endif
  217. }
  218. /* calc the signature */
  219. buf_put_sign(ses.writepayload, svr_opts.hostkey,
  220. ses.newkeys->algo_signature, ses.hash);
  221. /* the SSH_MSG_KEXDH_REPLY is done */
  222. encrypt_packet();
  223. TRACE(("leave send_msg_kexdh_reply"))
  224. }
  225. #if DROPBEAR_EXT_INFO
  226. /* Only used for server-sig-algs on the server side */
  227. static void send_msg_ext_info(void) {
  228. TRACE(("enter send_msg_ext_info"))
  229. buf_putbyte(ses.writepayload, SSH_MSG_EXT_INFO);
  230. /* nr-extensions */
  231. buf_putint(ses.writepayload, 1);
  232. buf_putstring(ses.writepayload, SSH_SERVER_SIG_ALGS, strlen(SSH_SERVER_SIG_ALGS));
  233. buf_put_algolist_all(ses.writepayload, sigalgs, 1);
  234. encrypt_packet();
  235. TRACE(("leave send_msg_ext_info"))
  236. }
  237. #endif