2004-06-01 02:46:09 +00:00
|
|
|
/*
|
|
|
|
* Dropbear - a SSH2 server
|
|
|
|
*
|
|
|
|
* Copyright (c) 2002,2003 Matt Johnston
|
2004-08-14 17:54:20 +00:00
|
|
|
* Copyright (c) 2004 by Mihnea Stoenescu
|
2004-06-01 02:46:09 +00:00
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
* of this software and associated documentation files (the "Software"), to deal
|
|
|
|
* in the Software without restriction, including without limitation the rights
|
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
* copies of the Software, and to permit persons to whom the Software is
|
|
|
|
* furnished to do so, subject to the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice shall be included in
|
|
|
|
* all copies or substantial portions of the Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
|
|
* SOFTWARE. */
|
|
|
|
|
|
|
|
#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 "random.h"
|
2004-07-20 12:05:00 +00:00
|
|
|
#include "runopts.h"
|
2013-04-06 17:36:42 +00:00
|
|
|
#include "ecc.h"
|
2004-06-01 02:46:09 +00:00
|
|
|
|
2013-03-25 17:35:22 +00:00
|
|
|
static void send_msg_kexdh_reply(mp_int *dh_e, buffer *ecdh_qs);
|
2004-06-01 02:46:09 +00:00
|
|
|
|
|
|
|
/* Handle a diffie-hellman key exchange initialisation. This involves
|
|
|
|
* calculating a session key reply value, and corresponding hash. These
|
|
|
|
* are carried out by send_msg_kexdh_reply(). recv_msg_kexdh_init() calls
|
|
|
|
* that function, then brings the new keys into use */
|
|
|
|
void recv_msg_kexdh_init() {
|
|
|
|
|
2004-08-17 10:20:20 +00:00
|
|
|
DEF_MP_INT(dh_e);
|
2013-03-25 17:35:22 +00:00
|
|
|
buffer *ecdh_qs = NULL;
|
2004-06-01 02:46:09 +00:00
|
|
|
|
2005-01-02 20:25:56 +00:00
|
|
|
TRACE(("enter recv_msg_kexdh_init"))
|
2004-06-01 02:46:09 +00:00
|
|
|
if (!ses.kexstate.recvkexinit) {
|
|
|
|
dropbear_exit("Premature kexdh_init message received");
|
|
|
|
}
|
|
|
|
|
2013-11-08 15:11:43 +00:00
|
|
|
switch (ses.newkeys->algo_kex->mode) {
|
|
|
|
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;
|
|
|
|
case DROPBEAR_KEX_ECDH:
|
|
|
|
case DROPBEAR_KEX_CURVE25519:
|
|
|
|
#if defined(DROPBEAR_ECDH) || defined(DROPBEAR_CURVE25519)
|
|
|
|
ecdh_qs = buf_getstringbuf(ses.payload);
|
|
|
|
if (ses.payload->pos != ses.payload->len) {
|
|
|
|
dropbear_exit("Bad kex value");
|
|
|
|
}
|
2013-03-25 17:35:22 +00:00
|
|
|
#endif
|
2013-11-08 15:11:43 +00:00
|
|
|
break;
|
2006-07-07 09:17:18 +00:00
|
|
|
}
|
2004-06-01 02:46:09 +00:00
|
|
|
|
2013-03-25 17:35:22 +00:00
|
|
|
send_msg_kexdh_reply(&dh_e, ecdh_qs);
|
2004-06-01 02:46:09 +00:00
|
|
|
|
|
|
|
mp_clear(&dh_e);
|
2013-03-25 17:35:22 +00:00
|
|
|
if (ecdh_qs) {
|
|
|
|
buf_free(ecdh_qs);
|
|
|
|
}
|
2004-06-01 02:46:09 +00:00
|
|
|
|
|
|
|
send_msg_newkeys();
|
2013-04-14 15:16:16 +00:00
|
|
|
ses.requirenext[0] = SSH_MSG_NEWKEYS;
|
|
|
|
ses.requirenext[1] = 0;
|
2005-01-02 20:25:56 +00:00
|
|
|
TRACE(("leave recv_msg_kexdh_init"))
|
2004-06-01 02:46:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Generate our side of the diffie-hellman key exchange value (dh_f), and
|
|
|
|
* calculate the session key using the diffie-hellman algorithm. Following
|
|
|
|
* that, the session hash is calculated, and signed with RSA or DSS. The
|
|
|
|
* result is sent to the client.
|
|
|
|
*
|
2013-03-25 17:35:22 +00:00
|
|
|
* See the transport RFC4253 section 8 for details
|
|
|
|
* or RFC5656 section 4 for elliptic curve variant. */
|
|
|
|
static void send_msg_kexdh_reply(mp_int *dh_e, buffer *ecdh_qs) {
|
2005-01-02 20:25:56 +00:00
|
|
|
TRACE(("enter send_msg_kexdh_reply"))
|
2004-06-01 02:46:09 +00:00
|
|
|
|
|
|
|
/* we can start creating the kexdh_reply packet */
|
|
|
|
CHECKCLEARTOWRITE();
|
|
|
|
buf_putbyte(ses.writepayload, SSH_MSG_KEXDH_REPLY);
|
2004-07-20 12:05:00 +00:00
|
|
|
buf_put_pub_key(ses.writepayload, svr_opts.hostkey,
|
2004-06-01 02:46:09 +00:00
|
|
|
ses.newkeys->algo_hostkey);
|
|
|
|
|
2013-11-08 15:11:43 +00:00
|
|
|
switch (ses.newkeys->algo_kex->mode) {
|
|
|
|
case DROPBEAR_KEX_NORMAL_DH:
|
|
|
|
{
|
|
|
|
struct kex_dh_param * dh_param = gen_kexdh_param();
|
|
|
|
kexdh_comb_key(dh_param, dh_e, svr_opts.hostkey);
|
|
|
|
|
|
|
|
/* put f */
|
|
|
|
buf_putmpint(ses.writepayload, &dh_param->pub);
|
|
|
|
free_kexdh_param(dh_param);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case DROPBEAR_KEX_ECDH:
|
2013-03-25 17:35:22 +00:00
|
|
|
#ifdef DROPBEAR_ECDH
|
2013-11-08 15:11:43 +00:00
|
|
|
{
|
|
|
|
struct kex_ecdh_param *ecdh_param = gen_kexecdh_param();
|
|
|
|
kexecdh_comb_key(ecdh_param, ecdh_qs, svr_opts.hostkey);
|
2013-03-25 17:35:22 +00:00
|
|
|
|
2013-11-08 15:11:43 +00:00
|
|
|
buf_put_ecc_raw_pubkey_string(ses.writepayload, &ecdh_param->key);
|
|
|
|
free_kexecdh_param(ecdh_param);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
break;
|
|
|
|
case DROPBEAR_KEX_CURVE25519:
|
|
|
|
#ifdef DROPBEAR_CURVE25519
|
|
|
|
{
|
|
|
|
struct kex_curve25519_param *param = gen_kexecdh_param();
|
|
|
|
kexcurve25519_comb_key(param, ecdh_qs, svr_opts.hostkey);
|
|
|
|
buf_putstring(ses.writepayload, param->priv, CURVE25519_LEN);
|
|
|
|
free_kexcurve25519_param(param);
|
|
|
|
}
|
2013-03-25 17:35:22 +00:00
|
|
|
#endif
|
2013-11-08 15:11:43 +00:00
|
|
|
break;
|
2013-03-25 17:35:22 +00:00
|
|
|
}
|
2004-06-01 02:46:09 +00:00
|
|
|
|
|
|
|
/* calc the signature */
|
2004-07-20 12:05:00 +00:00
|
|
|
buf_put_sign(ses.writepayload, svr_opts.hostkey,
|
2013-04-06 17:36:42 +00:00
|
|
|
ses.newkeys->algo_hostkey, ses.hash);
|
2004-06-01 02:46:09 +00:00
|
|
|
|
|
|
|
/* the SSH_MSG_KEXDH_REPLY is done */
|
|
|
|
encrypt_packet();
|
|
|
|
|
2005-01-02 20:25:56 +00:00
|
|
|
TRACE(("leave send_msg_kexdh_reply"))
|
2004-06-01 02:46:09 +00:00
|
|
|
}
|
|
|
|
|