123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276 |
- #include "includes.h"
- #include "dbutil.h"
- #include "algo.h"
- #include "buffer.h"
- #include "session.h"
- #include "kex.h"
- #include "ssh.h"
- #include "packet.h"
- #include "bignum.h"
- #include "dbrandom.h"
- #include "runopts.h"
- #include "ecc.h"
- #include "gensignkey.h"
- static void send_msg_kexdh_reply(mp_int *dh_e, buffer *ecdh_qs);
- #if DROPBEAR_EXT_INFO
- static void send_msg_ext_info(void);
- #endif
- void recv_msg_kexdh_init() {
- DEF_MP_INT(dh_e);
- buffer *ecdh_qs = NULL;
- TRACE(("enter recv_msg_kexdh_init"))
- if (!ses.kexstate.recvkexinit) {
- dropbear_exit("Premature kexdh_init message received");
- }
- switch (ses.newkeys->algo_kex->mode) {
- #if DROPBEAR_NORMAL_DH
- case DROPBEAR_KEX_NORMAL_DH:
- m_mp_init(&dh_e);
- if (buf_getmpint(ses.payload, &dh_e) != DROPBEAR_SUCCESS) {
- dropbear_exit("Bad kex value");
- }
- break;
- #endif
- #if DROPBEAR_ECDH
- case DROPBEAR_KEX_ECDH:
- #endif
- #if DROPBEAR_CURVE25519
- case DROPBEAR_KEX_CURVE25519:
- #endif
- #if DROPBEAR_ECDH || DROPBEAR_CURVE25519
- ecdh_qs = buf_getstringbuf(ses.payload);
- break;
- #endif
- }
- if (ses.payload->pos != ses.payload->len) {
- dropbear_exit("Bad kex value");
- }
- send_msg_kexdh_reply(&dh_e, ecdh_qs);
- mp_clear(&dh_e);
- if (ecdh_qs) {
- buf_free(ecdh_qs);
- ecdh_qs = NULL;
- }
- send_msg_newkeys();
- #if DROPBEAR_EXT_INFO
-
- if (!ses.kexstate.donesecondkex && ses.allow_ext_info) {
- send_msg_ext_info();
- }
- #endif
- ses.requirenext = SSH_MSG_NEWKEYS;
- TRACE(("leave recv_msg_kexdh_init"))
- }
- #if DROPBEAR_DELAY_HOSTKEY
- static void svr_ensure_hostkey() {
- const char* fn = NULL;
- char *expand_fn = NULL;
- enum signkey_type type = ses.newkeys->algo_hostkey;
- void **hostkey = signkey_key_ptr(svr_opts.hostkey, type);
- int ret = DROPBEAR_FAILURE;
- if (hostkey && *hostkey) {
- return;
- }
- switch (type)
- {
- #if DROPBEAR_RSA
- case DROPBEAR_SIGNKEY_RSA:
- fn = RSA_PRIV_FILENAME;
- break;
- #endif
- #if DROPBEAR_DSS
- case DROPBEAR_SIGNKEY_DSS:
- fn = DSS_PRIV_FILENAME;
- break;
- #endif
- #if DROPBEAR_ECDSA
- case DROPBEAR_SIGNKEY_ECDSA_NISTP256:
- case DROPBEAR_SIGNKEY_ECDSA_NISTP384:
- case DROPBEAR_SIGNKEY_ECDSA_NISTP521:
- fn = ECDSA_PRIV_FILENAME;
- break;
- #endif
- #if DROPBEAR_ED25519
- case DROPBEAR_SIGNKEY_ED25519:
- fn = ED25519_PRIV_FILENAME;
- break;
- #endif
- default:
- dropbear_assert(0);
- }
- expand_fn = expand_homedir_path(fn);
- ret = readhostkey(expand_fn, svr_opts.hostkey, &type);
- if (ret == DROPBEAR_SUCCESS) {
- goto out;
- }
- if (signkey_generate(type, 0, expand_fn, 1) == DROPBEAR_FAILURE) {
- goto out;
- }
-
-
- ret = readhostkey(expand_fn, svr_opts.hostkey, &type);
- if (ret == DROPBEAR_SUCCESS) {
- char *fp = NULL;
- unsigned int len;
- buffer *key_buf = buf_new(MAX_PUBKEY_SIZE);
- buf_put_pub_key(key_buf, svr_opts.hostkey, type);
- buf_setpos(key_buf, 4);
- len = key_buf->len - key_buf->pos;
- fp = sign_key_fingerprint(buf_getptr(key_buf, len), len);
- dropbear_log(LOG_INFO, "Generated hostkey %s, fingerprint is %s",
- expand_fn, fp);
- m_free(fp);
- buf_free(key_buf);
- }
- out:
- if (ret == DROPBEAR_FAILURE) {
- dropbear_exit("Couldn't read or generate hostkey %s", expand_fn);
- }
- m_free(expand_fn);
- }
- #endif
-
- static void send_msg_kexdh_reply(mp_int *dh_e, buffer *ecdh_qs) {
- TRACE(("enter send_msg_kexdh_reply"))
-
- CHECKCLEARTOWRITE();
- #if DROPBEAR_DELAY_HOSTKEY
- if (svr_opts.delay_hostkey)
- {
- svr_ensure_hostkey();
- }
- #endif
- #if DROPBEAR_FUZZ
- if (fuzz.fuzzing && fuzz.skip_kexmaths) {
- fuzz_fake_send_kexdh_reply();
- return;
- }
- #endif
- buf_putbyte(ses.writepayload, SSH_MSG_KEXDH_REPLY);
- buf_put_pub_key(ses.writepayload, svr_opts.hostkey,
- ses.newkeys->algo_hostkey);
- switch (ses.newkeys->algo_kex->mode) {
- #if DROPBEAR_NORMAL_DH
- case DROPBEAR_KEX_NORMAL_DH:
- {
- struct kex_dh_param * dh_param = gen_kexdh_param();
- kexdh_comb_key(dh_param, dh_e, svr_opts.hostkey);
-
- buf_putmpint(ses.writepayload, &dh_param->pub);
- free_kexdh_param(dh_param);
- }
- break;
- #endif
- #if DROPBEAR_ECDH
- case DROPBEAR_KEX_ECDH:
- {
- struct kex_ecdh_param *ecdh_param = gen_kexecdh_param();
- kexecdh_comb_key(ecdh_param, ecdh_qs, svr_opts.hostkey);
- buf_put_ecc_raw_pubkey_string(ses.writepayload, &ecdh_param->key);
- free_kexecdh_param(ecdh_param);
- }
- break;
- #endif
- #if DROPBEAR_CURVE25519
- case DROPBEAR_KEX_CURVE25519:
- {
- struct kex_curve25519_param *param = gen_kexcurve25519_param();
- kexcurve25519_comb_key(param, ecdh_qs, svr_opts.hostkey);
- buf_putstring(ses.writepayload, param->pub, CURVE25519_LEN);
- free_kexcurve25519_param(param);
- }
- break;
- #endif
- }
-
- buf_put_sign(ses.writepayload, svr_opts.hostkey,
- ses.newkeys->algo_signature, ses.hash);
-
- encrypt_packet();
- TRACE(("leave send_msg_kexdh_reply"))
- }
- #if DROPBEAR_EXT_INFO
- static void send_msg_ext_info(void) {
- TRACE(("enter send_msg_ext_info"))
- buf_putbyte(ses.writepayload, SSH_MSG_EXT_INFO);
-
- buf_putint(ses.writepayload, 1);
- buf_putstring(ses.writepayload, SSH_SERVER_SIG_ALGS, strlen(SSH_SERVER_SIG_ALGS));
- buf_put_algolist_all(ses.writepayload, sigalgs, 1);
-
- encrypt_packet();
- TRACE(("leave send_msg_ext_info"))
- }
- #endif
|