Mercurial > dropbear
view svr-kex.c @ 1306:34e6127ef02e
merge fixes from PuTTY import.c
toint() from misc.c
(revids are from hggit conversion)
changeset: 4620:60a336a6c85c
user: Simon Tatham <[email protected]>
date: Thu Feb 25 20:26:33 2016 +0000
files: import.c
description:
Fix potential segfaults in reading OpenSSH's ASN.1 key format.
The length coming back from ber_read_id_len might have overflowed, so
treat it as potentially negative. Also, while I'm here, accumulate it
inside ber_read_id_len as an unsigned, so as to avoid undefined
behaviour on integer overflow, and toint() it before return.
Thanks to Hanno Böck for spotting this, with the aid of AFL.
(cherry picked from commit 5b7833cd474a24ec098654dcba8cb9509f3bf2c1)
Conflicts:
import.c
(cherry-picker's note: resolving the conflict involved removing an
entire section of the original commit which fixed ECDSA code not
present on this branch)
changeset: 4619:9c6c638d98d8
user: Simon Tatham <[email protected]>
date: Sun Jul 14 10:45:54 2013 +0000
files: import.c ssh.c sshdss.c sshpubk.c sshrsa.c
description:
Tighten up a lot of casts from unsigned to int which are read by one
of the GET_32BIT macros and then used as length fields. Missing bounds
checks against zero have been added, and also I've introduced a helper
function toint() which casts from unsigned to int in such a way as to
avoid C undefined behaviour, since I'm not sure I trust compilers any
more to do the obviously sensible thing.
[originally from svn r9918]
changeset: 4618:3957829f24d3
user: Simon Tatham <[email protected]>
date: Mon Jul 08 22:36:04 2013 +0000
files: import.c sshdss.c sshrsa.c
description:
Add an assortment of extra safety checks.
[originally from svn r9896]
changeset: 4617:2cddee0bce12
user: Jacob Nevins <[email protected]>
date: Wed Dec 07 00:24:45 2005 +0000
files: import.c
description:
Institutional failure to memset() things pointed at rather than pointers.
Things should now be zeroed and memory not leaked. Spotted by Brant Thomsen.
[originally from svn r6476]
changeset: 4616:24ac78a9c71d
user: Simon Tatham <[email protected]>
date: Wed Feb 11 13:58:27 2004 +0000
files: import.c
description:
Jacob's last-minute testing found a couple of trivial bugs in
import.c, and my attempts to reproduce them in cmdgen found another
one there :-)
[originally from svn r3847]
changeset: 4615:088d39a73db0
user: Simon Tatham <[email protected]>
date: Thu Jan 22 18:52:49 2004 +0000
files: import.c
description:
Placate some gcc warnings.
[originally from svn r3761]
changeset: 4614:e4288bad4d93
parent: 1758:108b8924593d
user: Simon Tatham <[email protected]>
date: Fri Oct 03 21:21:23 2003 +0000
files: import.c
description:
My ASN.1 decoder returned wrong IDs for anything above 0x1E! Good
job it's never had to yet. Ahem.
[originally from svn r3479]
author | Matt Johnston <matt@ucc.asn.au> |
---|---|
date | Tue, 12 Jul 2016 23:00:01 +0800 |
parents | c8f52c19e949 |
children | 56aba7dedbea efad433418c4 |
line wrap: on
line source
/* * Dropbear - a SSH2 server * * Copyright (c) 2002,2003 Matt Johnston * Copyright (c) 2004 by Mihnea Stoenescu * 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 "dbrandom.h" #include "runopts.h" #include "ecc.h" #include "gensignkey.h" static void send_msg_kexdh_reply(mp_int *dh_e, buffer *ecdh_qs); /* 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() { 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) { 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); #endif break; } 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(); ses.requirenext = SSH_MSG_NEWKEYS; TRACE(("leave recv_msg_kexdh_init")) } #ifdef DROPBEAR_DELAY_HOSTKEY static void fsync_parent_dir(const char* fn) { #ifdef HAVE_LIBGEN_H char *fn_dir = m_strdup(fn); char *dir = dirname(fn_dir); int dirfd = open(dir, O_RDONLY); if (dirfd != -1) { if (fsync(dirfd) != 0) { TRACE(("fsync of directory %s failed: %s", dir, strerror(errno))) } m_close(dirfd); } else { TRACE(("error opening directory %s for fsync: %s", dir, strerror(errno))) } free(fn_dir); #endif } static void svr_ensure_hostkey() { const char* fn = NULL; char *fn_temp = 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) { #ifdef DROPBEAR_RSA case DROPBEAR_SIGNKEY_RSA: fn = RSA_PRIV_FILENAME; break; #endif #ifdef DROPBEAR_DSS case DROPBEAR_SIGNKEY_DSS: fn = DSS_PRIV_FILENAME; break; #endif #ifdef DROPBEAR_ECDSA case DROPBEAR_SIGNKEY_ECDSA_NISTP256: case DROPBEAR_SIGNKEY_ECDSA_NISTP384: case DROPBEAR_SIGNKEY_ECDSA_NISTP521: fn = ECDSA_PRIV_FILENAME; break; #endif default: dropbear_assert(0); } if (readhostkey(fn, svr_opts.hostkey, &type) == DROPBEAR_SUCCESS) { return; } fn_temp = m_malloc(strlen(fn) + 20); snprintf(fn_temp, strlen(fn)+20, "%s.tmp%d", fn, getpid()); if (signkey_generate(type, 0, fn_temp) == DROPBEAR_FAILURE) { goto out; } if (link(fn_temp, fn) < 0) { /* It's OK to get EEXIST - we probably just lost a race with another connection to generate the key */ if (errno != EEXIST) { dropbear_log(LOG_ERR, "Failed moving key file to %s: %s", fn, strerror(errno)); /* XXX fallback to non-atomic copy for some filesystems? */ goto out; } } /* ensure directory update is flushed to disk, otherwise we can end up with zero-byte hostkey files if the power goes off */ fsync_parent_dir(fn); ret = readhostkey(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", fn, fp); m_free(fp); buf_free(key_buf); } out: if (fn_temp) { unlink(fn_temp); m_free(fn_temp); } if (ret == DROPBEAR_FAILURE) { dropbear_exit("Couldn't read or generate hostkey %s", fn); } } #endif /* 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. * * 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) { TRACE(("enter send_msg_kexdh_reply")) /* we can start creating the kexdh_reply packet */ CHECKCLEARTOWRITE(); #ifdef DROPBEAR_DELAY_HOSTKEY if (svr_opts.delay_hostkey) { svr_ensure_hostkey(); } #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) { 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: #ifdef DROPBEAR_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); } #endif break; case DROPBEAR_KEX_CURVE25519: #ifdef DROPBEAR_CURVE25519 { struct kex_curve25519_param *param = gen_kexcurve25519_param(); kexcurve25519_comb_key(param, ecdh_qs, svr_opts.hostkey); buf_putstring(ses.writepayload, (const char*)param->pub, CURVE25519_LEN); free_kexcurve25519_param(param); } #endif break; } /* calc the signature */ buf_put_sign(ses.writepayload, svr_opts.hostkey, ses.newkeys->algo_hostkey, ses.hash); /* the SSH_MSG_KEXDH_REPLY is done */ encrypt_packet(); TRACE(("leave send_msg_kexdh_reply")) }