[BACK]Return to kex.c CVS log [TXT][DIR] Up to [local] / src / usr.bin / ssh

Annotation of src/usr.bin/ssh/kex.c, Revision 1.113

1.113   ! markus      1: /* $OpenBSD: kex.c,v 1.112 2015/11/13 04:39:35 djm Exp $ */
1.1       markus      2: /*
1.36      markus      3:  * Copyright (c) 2000, 2001 Markus Friedl.  All rights reserved.
1.1       markus      4:  *
                      5:  * Redistribution and use in source and binary forms, with or without
                      6:  * modification, are permitted provided that the following conditions
                      7:  * are met:
                      8:  * 1. Redistributions of source code must retain the above copyright
                      9:  *    notice, this list of conditions and the following disclaimer.
                     10:  * 2. Redistributions in binary form must reproduce the above copyright
                     11:  *    notice, this list of conditions and the following disclaimer in the
                     12:  *    documentation and/or other materials provided with the distribution.
                     13:  *
                     14:  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
                     15:  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
                     16:  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
                     17:  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
                     18:  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
                     19:  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
                     20:  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
                     21:  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
                     22:  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
                     23:  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
                     24:  */
                     25:
1.103     deraadt    26: #include <sys/param.h> /* MAX roundup */
1.18      markus     27:
1.76      deraadt    28: #include <signal.h>
1.75      stevesk    29: #include <stdio.h>
1.74      stevesk    30: #include <stdlib.h>
1.72      stevesk    31: #include <string.h>
1.1       markus     32:
1.99      markus     33: #ifdef WITH_OPENSSL
1.76      deraadt    34: #include <openssl/crypto.h>
1.99      markus     35: #endif
1.76      deraadt    36:
1.1       markus     37: #include "ssh2.h"
1.7       markus     38: #include "packet.h"
1.1       markus     39: #include "compat.h"
1.18      markus     40: #include "cipher.h"
1.102     markus     41: #include "sshkey.h"
1.1       markus     42: #include "kex.h"
1.18      markus     43: #include "log.h"
1.21      markus     44: #include "mac.h"
1.23      markus     45: #include "match.h"
1.102     markus     46: #include "misc.h"
1.26      markus     47: #include "dispatch.h"
1.48      provos     48: #include "monitor.h"
1.82      andreas    49: #include "roaming.h"
1.102     markus     50:
                     51: #include "ssherr.h"
                     52: #include "sshbuf.h"
1.94      djm        53: #include "digest.h"
1.48      provos     54:
1.35      itojun     55: /* prototype */
1.102     markus     56: static int kex_choose_conf(struct ssh *);
                     57: static int kex_input_newkeys(int, u_int32_t, void *);
1.86      djm        58:
1.110     djm        59: static const char *proposal_names[PROPOSAL_MAX] = {
                     60:        "KEX algorithms",
                     61:        "host key algorithms",
                     62:        "ciphers ctos",
                     63:        "ciphers stoc",
                     64:        "MACs ctos",
                     65:        "MACs stoc",
                     66:        "compression ctos",
                     67:        "compression stoc",
                     68:        "languages ctos",
                     69:        "languages stoc",
                     70: };
                     71:
1.89      djm        72: struct kexalg {
                     73:        char *name;
1.102     markus     74:        u_int type;
1.89      djm        75:        int ec_nid;
1.94      djm        76:        int hash_alg;
1.89      djm        77: };
                     78: static const struct kexalg kexalgs[] = {
1.99      markus     79: #ifdef WITH_OPENSSL
1.94      djm        80:        { KEX_DH1, KEX_DH_GRP1_SHA1, 0, SSH_DIGEST_SHA1 },
                     81:        { KEX_DH14, KEX_DH_GRP14_SHA1, 0, SSH_DIGEST_SHA1 },
                     82:        { KEX_DHGEX_SHA1, KEX_DH_GEX_SHA1, 0, SSH_DIGEST_SHA1 },
                     83:        { KEX_DHGEX_SHA256, KEX_DH_GEX_SHA256, 0, SSH_DIGEST_SHA256 },
                     84:        { KEX_ECDH_SHA2_NISTP256, KEX_ECDH_SHA2,
                     85:            NID_X9_62_prime256v1, SSH_DIGEST_SHA256 },
                     86:        { KEX_ECDH_SHA2_NISTP384, KEX_ECDH_SHA2, NID_secp384r1,
                     87:            SSH_DIGEST_SHA384 },
                     88:        { KEX_ECDH_SHA2_NISTP521, KEX_ECDH_SHA2, NID_secp521r1,
                     89:            SSH_DIGEST_SHA512 },
1.99      markus     90: #endif
1.94      djm        91:        { KEX_CURVE25519_SHA256, KEX_C25519_SHA256, 0, SSH_DIGEST_SHA256 },
                     92:        { NULL, -1, -1, -1},
1.89      djm        93: };
                     94:
                     95: char *
1.93      dtucker    96: kex_alg_list(char sep)
1.89      djm        97: {
1.102     markus     98:        char *ret = NULL, *tmp;
1.89      djm        99:        size_t nlen, rlen = 0;
                    100:        const struct kexalg *k;
                    101:
                    102:        for (k = kexalgs; k->name != NULL; k++) {
                    103:                if (ret != NULL)
1.93      dtucker   104:                        ret[rlen++] = sep;
1.89      djm       105:                nlen = strlen(k->name);
1.102     markus    106:                if ((tmp = realloc(ret, rlen + nlen + 2)) == NULL) {
                    107:                        free(ret);
                    108:                        return NULL;
                    109:                }
                    110:                ret = tmp;
1.89      djm       111:                memcpy(ret + rlen, k->name, nlen + 1);
                    112:                rlen += nlen;
                    113:        }
                    114:        return ret;
                    115: }
                    116:
                    117: static const struct kexalg *
                    118: kex_alg_by_name(const char *name)
                    119: {
                    120:        const struct kexalg *k;
                    121:
                    122:        for (k = kexalgs; k->name != NULL; k++) {
                    123:                if (strcmp(k->name, name) == 0)
                    124:                        return k;
                    125:        }
                    126:        return NULL;
                    127: }
                    128:
1.86      djm       129: /* Validate KEX method name list */
                    130: int
                    131: kex_names_valid(const char *names)
                    132: {
                    133:        char *s, *cp, *p;
                    134:
                    135:        if (names == NULL || strcmp(names, "") == 0)
                    136:                return 0;
1.102     markus    137:        if ((s = cp = strdup(names)) == NULL)
                    138:                return 0;
1.86      djm       139:        for ((p = strsep(&cp, ",")); p && *p != '\0';
                    140:            (p = strsep(&cp, ","))) {
1.89      djm       141:                if (kex_alg_by_name(p) == NULL) {
1.86      djm       142:                        error("Unsupported KEX algorithm \"%.100s\"", p);
1.91      djm       143:                        free(s);
1.86      djm       144:                        return 0;
                    145:                }
                    146:        }
                    147:        debug3("kex names ok: [%s]", names);
1.91      djm       148:        free(s);
1.86      djm       149:        return 1;
1.109     djm       150: }
                    151:
                    152: /*
                    153:  * Concatenate algorithm names, avoiding duplicates in the process.
                    154:  * Caller must free returned string.
                    155:  */
                    156: char *
                    157: kex_names_cat(const char *a, const char *b)
                    158: {
                    159:        char *ret = NULL, *tmp = NULL, *cp, *p;
                    160:        size_t len;
                    161:
                    162:        if (a == NULL || *a == '\0')
                    163:                return NULL;
                    164:        if (b == NULL || *b == '\0')
                    165:                return strdup(a);
                    166:        if (strlen(b) > 1024*1024)
                    167:                return NULL;
                    168:        len = strlen(a) + strlen(b) + 2;
                    169:        if ((tmp = cp = strdup(b)) == NULL ||
                    170:            (ret = calloc(1, len)) == NULL) {
                    171:                free(tmp);
                    172:                return NULL;
                    173:        }
                    174:        strlcpy(ret, a, len);
                    175:        for ((p = strsep(&cp, ",")); p && *p != '\0'; (p = strsep(&cp, ","))) {
                    176:                if (match_list(ret, p, NULL) != NULL)
                    177:                        continue; /* Algorithm already present */
                    178:                if (strlcat(ret, ",", len) >= len ||
                    179:                    strlcat(ret, p, len) >= len) {
                    180:                        free(tmp);
                    181:                        free(ret);
                    182:                        return NULL; /* Shouldn't happen */
                    183:                }
                    184:        }
                    185:        free(tmp);
                    186:        return ret;
                    187: }
                    188:
                    189: /*
                    190:  * Assemble a list of algorithms from a default list and a string from a
                    191:  * configuration file. The user-provided string may begin with '+' to
                    192:  * indicate that it should be appended to the default.
                    193:  */
                    194: int
                    195: kex_assemble_names(const char *def, char **list)
                    196: {
                    197:        char *ret;
                    198:
                    199:        if (list == NULL || *list == NULL || **list == '\0') {
                    200:                *list = strdup(def);
                    201:                return 0;
                    202:        }
                    203:        if (**list != '+') {
                    204:                return 0;
                    205:        }
                    206:
                    207:        if ((ret = kex_names_cat(def, *list + 1)) == NULL)
                    208:                return SSH_ERR_ALLOC_FAIL;
                    209:        free(*list);
                    210:        *list = ret;
                    211:        return 0;
1.86      djm       212: }
1.26      markus    213:
                    214: /* put algorithm proposal into buffer */
1.102     markus    215: int
                    216: kex_prop2buf(struct sshbuf *b, char *proposal[PROPOSAL_MAX])
1.1       markus    217: {
1.61      djm       218:        u_int i;
1.102     markus    219:        int r;
                    220:
                    221:        sshbuf_reset(b);
1.26      markus    222:
1.49      markus    223:        /*
                    224:         * add a dummy cookie, the cookie will be overwritten by
                    225:         * kex_send_kexinit(), each time a kexinit is set
                    226:         */
1.102     markus    227:        for (i = 0; i < KEX_COOKIE_LEN; i++) {
                    228:                if ((r = sshbuf_put_u8(b, 0)) != 0)
                    229:                        return r;
                    230:        }
                    231:        for (i = 0; i < PROPOSAL_MAX; i++) {
                    232:                if ((r = sshbuf_put_cstring(b, proposal[i])) != 0)
                    233:                        return r;
                    234:        }
                    235:        if ((r = sshbuf_put_u8(b, 0)) != 0 ||   /* first_kex_packet_follows */
                    236:            (r = sshbuf_put_u32(b, 0)) != 0)    /* uint32 reserved */
                    237:                return r;
                    238:        return 0;
1.1       markus    239: }
                    240:
1.26      markus    241: /* parse buffer and return algorithm proposal */
1.102     markus    242: int
                    243: kex_buf2prop(struct sshbuf *raw, int *first_kex_follows, char ***propp)
1.7       markus    244: {
1.102     markus    245:        struct sshbuf *b = NULL;
                    246:        u_char v;
1.78      djm       247:        u_int i;
1.102     markus    248:        char **proposal = NULL;
                    249:        int r;
1.7       markus    250:
1.102     markus    251:        *propp = NULL;
                    252:        if ((proposal = calloc(PROPOSAL_MAX, sizeof(char *))) == NULL)
                    253:                return SSH_ERR_ALLOC_FAIL;
                    254:        if ((b = sshbuf_fromb(raw)) == NULL) {
                    255:                r = SSH_ERR_ALLOC_FAIL;
                    256:                goto out;
                    257:        }
                    258:        if ((r = sshbuf_consume(b, KEX_COOKIE_LEN)) != 0) /* skip cookie */
                    259:                goto out;
1.7       markus    260:        /* extract kex init proposal strings */
                    261:        for (i = 0; i < PROPOSAL_MAX; i++) {
1.102     markus    262:                if ((r = sshbuf_get_cstring(b, &(proposal[i]), NULL)) != 0)
                    263:                        goto out;
1.110     djm       264:                debug2("%s: %s", proposal_names[i], proposal[i]);
1.7       markus    265:        }
1.26      markus    266:        /* first kex follows / reserved */
1.102     markus    267:        if ((r = sshbuf_get_u8(b, &v)) != 0 ||
                    268:            (r = sshbuf_get_u32(b, &i)) != 0)
                    269:                goto out;
1.53      markus    270:        if (first_kex_follows != NULL)
                    271:                *first_kex_follows = i;
1.110     djm       272:        debug2("first_kex_follows %d ", v);
                    273:        debug2("reserved %u ", i);
1.102     markus    274:        r = 0;
                    275:        *propp = proposal;
                    276:  out:
                    277:        if (r != 0 && proposal != NULL)
                    278:                kex_prop_free(proposal);
                    279:        sshbuf_free(b);
                    280:        return r;
1.1       markus    281: }
                    282:
1.102     markus    283: void
1.26      markus    284: kex_prop_free(char **proposal)
1.1       markus    285: {
1.61      djm       286:        u_int i;
1.26      markus    287:
1.106     djm       288:        if (proposal == NULL)
                    289:                return;
1.26      markus    290:        for (i = 0; i < PROPOSAL_MAX; i++)
1.91      djm       291:                free(proposal[i]);
                    292:        free(proposal);
1.1       markus    293: }
                    294:
1.78      djm       295: /* ARGSUSED */
1.101     markus    296: static int
1.41      markus    297: kex_protocol_error(int type, u_int32_t seq, void *ctxt)
1.1       markus    298: {
1.112     djm       299:        struct ssh *ssh = active_state; /* XXX */
                    300:        int r;
                    301:
                    302:        error("kex protocol error: type %d seq %u", type, seq);
                    303:        if ((r = sshpkt_start(ssh, SSH2_MSG_UNIMPLEMENTED)) != 0 ||
                    304:            (r = sshpkt_put_u32(ssh, seq)) != 0 ||
                    305:            (r = sshpkt_send(ssh)) != 0)
                    306:                return r;
1.101     markus    307:        return 0;
1.26      markus    308: }
1.1       markus    309:
1.35      itojun    310: static void
1.102     markus    311: kex_reset_dispatch(struct ssh *ssh)
1.29      markus    312: {
1.102     markus    313:        ssh_dispatch_range(ssh, SSH2_MSG_TRANSPORT_MIN,
1.42      markus    314:            SSH2_MSG_TRANSPORT_MAX, &kex_protocol_error);
1.102     markus    315:        ssh_dispatch_set(ssh, SSH2_MSG_KEXINIT, &kex_input_kexinit);
1.29      markus    316: }
                    317:
1.113   ! markus    318: static int
        !           319: kex_send_ext_info(struct ssh *ssh)
        !           320: {
        !           321:        int r;
        !           322:
        !           323:        if ((r = sshpkt_start(ssh, SSH2_MSG_EXT_INFO)) != 0 ||
        !           324:            (r = sshpkt_put_u32(ssh, 1)) != 0 ||
        !           325:            (r = sshpkt_put_cstring(ssh, "server-sig-algs")) != 0 ||
        !           326:            (r = sshpkt_put_cstring(ssh, "rsa-sha2-256,rsa-sha2-512")) != 0 ||
        !           327:            (r = sshpkt_send(ssh)) != 0)
        !           328:                return r;
        !           329:        return 0;
        !           330: }
        !           331:
1.102     markus    332: int
                    333: kex_send_newkeys(struct ssh *ssh)
1.26      markus    334: {
1.102     markus    335:        int r;
1.28      markus    336:
1.102     markus    337:        kex_reset_dispatch(ssh);
                    338:        if ((r = sshpkt_start(ssh, SSH2_MSG_NEWKEYS)) != 0 ||
                    339:            (r = sshpkt_send(ssh)) != 0)
                    340:                return r;
1.26      markus    341:        debug("SSH2_MSG_NEWKEYS sent");
1.102     markus    342:        debug("expecting SSH2_MSG_NEWKEYS");
                    343:        ssh_dispatch_set(ssh, SSH2_MSG_NEWKEYS, &kex_input_newkeys);
1.113   ! markus    344:        if (ssh->kex->ext_info_c)
        !           345:                if ((r = kex_send_ext_info(ssh)) != 0)
        !           346:                        return r;
1.102     markus    347:        return 0;
                    348: }
                    349:
1.113   ! markus    350: int
        !           351: kex_input_ext_info(int type, u_int32_t seq, void *ctxt)
        !           352: {
        !           353:        struct ssh *ssh = ctxt;
        !           354:        struct kex *kex = ssh->kex;
        !           355:        u_int32_t i, ninfo;
        !           356:        char *name, *val, *found;
        !           357:        int r;
        !           358:
        !           359:        debug("SSH2_MSG_EXT_INFO received");
        !           360:        ssh_dispatch_set(ssh, SSH2_MSG_EXT_INFO, &kex_protocol_error);
        !           361:        if ((r = sshpkt_get_u32(ssh, &ninfo)) != 0)
        !           362:                return r;
        !           363:        for (i = 0; i < ninfo; i++) {
        !           364:                if ((r = sshpkt_get_cstring(ssh, &name, NULL)) != 0)
        !           365:                        return r;
        !           366:                if ((r = sshpkt_get_cstring(ssh, &val, NULL)) != 0) {
        !           367:                        free(name);
        !           368:                        return r;
        !           369:                }
        !           370:                debug("%s: %s=<%s>", __func__, name, val);
        !           371:                if (strcmp(name, "server-sig-algs") == 0) {
        !           372:                        found = match_list("rsa-sha2-256", val, NULL);
        !           373:                        if (found) {
        !           374:                                kex->rsa_sha2 = 256;
        !           375:                                free(found);
        !           376:                        }
        !           377:                        found = match_list("rsa-sha2-512", val, NULL);
        !           378:                        if (found) {
        !           379:                                kex->rsa_sha2 = 512;
        !           380:                                free(found);
        !           381:                        }
        !           382:                }
        !           383:                free(name);
        !           384:                free(val);
        !           385:        }
        !           386:        return sshpkt_get_end(ssh);
        !           387: }
        !           388:
1.102     markus    389: static int
                    390: kex_input_newkeys(int type, u_int32_t seq, void *ctxt)
                    391: {
                    392:        struct ssh *ssh = ctxt;
                    393:        struct kex *kex = ssh->kex;
                    394:        int r;
1.19      stevesk   395:
1.26      markus    396:        debug("SSH2_MSG_NEWKEYS received");
1.102     markus    397:        ssh_dispatch_set(ssh, SSH2_MSG_NEWKEYS, &kex_protocol_error);
                    398:        if ((r = sshpkt_get_end(ssh)) != 0)
                    399:                return r;
1.30      markus    400:        kex->done = 1;
1.102     markus    401:        sshbuf_reset(kex->peer);
                    402:        /* sshbuf_reset(kex->my); */
1.26      markus    403:        kex->flags &= ~KEX_INIT_SENT;
1.91      djm       404:        free(kex->name);
1.32      markus    405:        kex->name = NULL;
1.102     markus    406:        return 0;
1.26      markus    407: }
1.1       markus    408:
1.102     markus    409: int
                    410: kex_send_kexinit(struct ssh *ssh)
1.26      markus    411: {
1.49      markus    412:        u_char *cookie;
1.102     markus    413:        struct kex *kex = ssh->kex;
                    414:        int r;
1.49      markus    415:
1.102     markus    416:        if (kex == NULL)
                    417:                return SSH_ERR_INTERNAL_ERROR;
                    418:        if (kex->flags & KEX_INIT_SENT)
                    419:                return 0;
1.30      markus    420:        kex->done = 0;
1.49      markus    421:
                    422:        /* generate a random cookie */
1.102     markus    423:        if (sshbuf_len(kex->my) < KEX_COOKIE_LEN)
                    424:                return SSH_ERR_INVALID_FORMAT;
                    425:        if ((cookie = sshbuf_mutable_ptr(kex->my)) == NULL)
                    426:                return SSH_ERR_INTERNAL_ERROR;
                    427:        arc4random_buf(cookie, KEX_COOKIE_LEN);
                    428:
                    429:        if ((r = sshpkt_start(ssh, SSH2_MSG_KEXINIT)) != 0 ||
                    430:            (r = sshpkt_putb(ssh, kex->my)) != 0 ||
                    431:            (r = sshpkt_send(ssh)) != 0)
                    432:                return r;
1.26      markus    433:        debug("SSH2_MSG_KEXINIT sent");
                    434:        kex->flags |= KEX_INIT_SENT;
1.102     markus    435:        return 0;
1.1       markus    436: }
                    437:
1.78      djm       438: /* ARGSUSED */
1.101     markus    439: int
1.41      markus    440: kex_input_kexinit(int type, u_int32_t seq, void *ctxt)
1.11      provos    441: {
1.102     markus    442:        struct ssh *ssh = ctxt;
                    443:        struct kex *kex = ssh->kex;
                    444:        const u_char *ptr;
1.100     markus    445:        u_int i;
                    446:        size_t dlen;
1.102     markus    447:        int r;
1.11      provos    448:
1.26      markus    449:        debug("SSH2_MSG_KEXINIT received");
1.29      markus    450:        if (kex == NULL)
1.102     markus    451:                return SSH_ERR_INVALID_ARGUMENT;
1.11      provos    452:
1.102     markus    453:        ptr = sshpkt_ptr(ssh, &dlen);
                    454:        if ((r = sshbuf_put(kex->peer, ptr, dlen)) != 0)
                    455:                return r;
1.31      markus    456:
                    457:        /* discard packet */
                    458:        for (i = 0; i < KEX_COOKIE_LEN; i++)
1.102     markus    459:                if ((r = sshpkt_get_u8(ssh, NULL)) != 0)
                    460:                        return r;
1.31      markus    461:        for (i = 0; i < PROPOSAL_MAX; i++)
1.102     markus    462:                if ((r = sshpkt_get_string(ssh, NULL, NULL)) != 0)
                    463:                        return r;
1.87      djm       464:        /*
                    465:         * XXX RFC4253 sec 7: "each side MAY guess" - currently no supported
                    466:         * KEX method has the server move first, but a server might be using
                    467:         * a custom method or one that we otherwise don't support. We should
                    468:         * be prepared to remember first_kex_follows here so we can eat a
                    469:         * packet later.
                    470:         * XXX2 - RFC4253 is kind of ambiguous on what first_kex_follows means
                    471:         * for cases where the server *doesn't* go first. I guess we should
                    472:         * ignore it when it is set for these cases, which is what we do now.
                    473:         */
1.102     markus    474:        if ((r = sshpkt_get_u8(ssh, NULL)) != 0 ||      /* first_kex_follows */
                    475:            (r = sshpkt_get_u32(ssh, NULL)) != 0 ||     /* reserved */
                    476:            (r = sshpkt_get_end(ssh)) != 0)
                    477:                        return r;
                    478:
                    479:        if (!(kex->flags & KEX_INIT_SENT))
                    480:                if ((r = kex_send_kexinit(ssh)) != 0)
                    481:                        return r;
                    482:        if ((r = kex_choose_conf(ssh)) != 0)
                    483:                return r;
1.19      stevesk   484:
1.102     markus    485:        if (kex->kex_type < KEX_MAX && kex->kex[kex->kex_type] != NULL)
                    486:                return (kex->kex[kex->kex_type])(ssh);
                    487:
                    488:        return SSH_ERR_INTERNAL_ERROR;
                    489: }
                    490:
                    491: int
                    492: kex_new(struct ssh *ssh, char *proposal[PROPOSAL_MAX], struct kex **kexp)
                    493: {
                    494:        struct kex *kex;
                    495:        int r;
                    496:
                    497:        *kexp = NULL;
                    498:        if ((kex = calloc(1, sizeof(*kex))) == NULL)
                    499:                return SSH_ERR_ALLOC_FAIL;
                    500:        if ((kex->peer = sshbuf_new()) == NULL ||
                    501:            (kex->my = sshbuf_new()) == NULL) {
                    502:                r = SSH_ERR_ALLOC_FAIL;
                    503:                goto out;
                    504:        }
                    505:        if ((r = kex_prop2buf(kex->my, proposal)) != 0)
                    506:                goto out;
                    507:        kex->done = 0;
                    508:        kex_reset_dispatch(ssh);
                    509:        r = 0;
                    510:        *kexp = kex;
                    511:  out:
                    512:        if (r != 0)
                    513:                kex_free(kex);
                    514:        return r;
1.26      markus    515: }
1.11      provos    516:
1.100     markus    517: void
                    518: kex_free_newkeys(struct newkeys *newkeys)
                    519: {
                    520:        if (newkeys == NULL)
                    521:                return;
                    522:        if (newkeys->enc.key) {
                    523:                explicit_bzero(newkeys->enc.key, newkeys->enc.key_len);
                    524:                free(newkeys->enc.key);
                    525:                newkeys->enc.key = NULL;
                    526:        }
                    527:        if (newkeys->enc.iv) {
1.111     djm       528:                explicit_bzero(newkeys->enc.iv, newkeys->enc.iv_len);
1.100     markus    529:                free(newkeys->enc.iv);
                    530:                newkeys->enc.iv = NULL;
                    531:        }
                    532:        free(newkeys->enc.name);
                    533:        explicit_bzero(&newkeys->enc, sizeof(newkeys->enc));
                    534:        free(newkeys->comp.name);
                    535:        explicit_bzero(&newkeys->comp, sizeof(newkeys->comp));
                    536:        mac_clear(&newkeys->mac);
                    537:        if (newkeys->mac.key) {
                    538:                explicit_bzero(newkeys->mac.key, newkeys->mac.key_len);
                    539:                free(newkeys->mac.key);
                    540:                newkeys->mac.key = NULL;
                    541:        }
                    542:        free(newkeys->mac.name);
                    543:        explicit_bzero(&newkeys->mac, sizeof(newkeys->mac));
                    544:        explicit_bzero(newkeys, sizeof(*newkeys));
                    545:        free(newkeys);
                    546: }
                    547:
1.102     markus    548: void
                    549: kex_free(struct kex *kex)
1.26      markus    550: {
1.102     markus    551:        u_int mode;
1.11      provos    552:
1.102     markus    553: #ifdef WITH_OPENSSL
                    554:        if (kex->dh)
                    555:                DH_free(kex->dh);
                    556:        if (kex->ec_client_key)
                    557:                EC_KEY_free(kex->ec_client_key);
                    558: #endif
                    559:        for (mode = 0; mode < MODE_MAX; mode++) {
                    560:                kex_free_newkeys(kex->newkeys[mode]);
                    561:                kex->newkeys[mode] = NULL;
1.100     markus    562:        }
1.102     markus    563:        sshbuf_free(kex->peer);
                    564:        sshbuf_free(kex->my);
                    565:        free(kex->session_id);
                    566:        free(kex->client_version_string);
                    567:        free(kex->server_version_string);
1.107     djm       568:        free(kex->failed_choice);
1.113   ! markus    569:        free(kex->hostkey_alg);
        !           570:        free(kex->name);
1.102     markus    571:        free(kex);
1.11      provos    572: }
                    573:
1.102     markus    574: int
                    575: kex_setup(struct ssh *ssh, char *proposal[PROPOSAL_MAX])
1.1       markus    576: {
1.102     markus    577:        int r;
1.1       markus    578:
1.102     markus    579:        if ((r = kex_new(ssh, proposal, &ssh->kex)) != 0)
                    580:                return r;
                    581:        if ((r = kex_send_kexinit(ssh)) != 0) {         /* we start */
                    582:                kex_free(ssh->kex);
                    583:                ssh->kex = NULL;
                    584:                return r;
1.1       markus    585:        }
1.102     markus    586:        return 0;
1.1       markus    587: }
                    588:
1.102     markus    589: static int
                    590: choose_enc(struct sshenc *enc, char *client, char *server)
1.1       markus    591: {
1.23      markus    592:        char *name = match_list(client, server, NULL);
1.102     markus    593:
1.1       markus    594:        if (name == NULL)
1.102     markus    595:                return SSH_ERR_NO_CIPHER_ALG_MATCH;
1.45      markus    596:        if ((enc->cipher = cipher_by_name(name)) == NULL)
1.102     markus    597:                return SSH_ERR_INTERNAL_ERROR;
1.1       markus    598:        enc->name = name;
                    599:        enc->enabled = 0;
                    600:        enc->iv = NULL;
1.88      markus    601:        enc->iv_len = cipher_ivlen(enc->cipher);
1.1       markus    602:        enc->key = NULL;
1.45      markus    603:        enc->key_len = cipher_keylen(enc->cipher);
                    604:        enc->block_size = cipher_blocksize(enc->cipher);
1.102     markus    605:        return 0;
1.1       markus    606: }
1.69      deraadt   607:
1.102     markus    608: static int
                    609: choose_mac(struct ssh *ssh, struct sshmac *mac, char *client, char *server)
1.1       markus    610: {
1.23      markus    611:        char *name = match_list(client, server, NULL);
1.102     markus    612:
1.1       markus    613:        if (name == NULL)
1.102     markus    614:                return SSH_ERR_NO_MAC_ALG_MATCH;
1.79      djm       615:        if (mac_setup(mac, name) < 0)
1.102     markus    616:                return SSH_ERR_INTERNAL_ERROR;
1.21      markus    617:        /* truncate the key */
1.102     markus    618:        if (ssh->compat & SSH_BUG_HMAC)
1.21      markus    619:                mac->key_len = 16;
1.1       markus    620:        mac->name = name;
                    621:        mac->key = NULL;
                    622:        mac->enabled = 0;
1.102     markus    623:        return 0;
1.1       markus    624: }
1.69      deraadt   625:
1.102     markus    626: static int
                    627: choose_comp(struct sshcomp *comp, char *client, char *server)
1.1       markus    628: {
1.23      markus    629:        char *name = match_list(client, server, NULL);
1.102     markus    630:
1.1       markus    631:        if (name == NULL)
1.102     markus    632:                return SSH_ERR_NO_COMPRESS_ALG_MATCH;
1.64      markus    633:        if (strcmp(name, "zlib@openssh.com") == 0) {
                    634:                comp->type = COMP_DELAYED;
                    635:        } else if (strcmp(name, "zlib") == 0) {
                    636:                comp->type = COMP_ZLIB;
1.1       markus    637:        } else if (strcmp(name, "none") == 0) {
1.64      markus    638:                comp->type = COMP_NONE;
1.1       markus    639:        } else {
1.102     markus    640:                return SSH_ERR_INTERNAL_ERROR;
1.1       markus    641:        }
                    642:        comp->name = name;
1.102     markus    643:        return 0;
1.1       markus    644: }
1.69      deraadt   645:
1.102     markus    646: static int
                    647: choose_kex(struct kex *k, char *client, char *server)
1.1       markus    648: {
1.89      djm       649:        const struct kexalg *kexalg;
                    650:
1.23      markus    651:        k->name = match_list(client, server, NULL);
1.102     markus    652:
1.110     djm       653:        debug("kex: algorithm: %s", k->name ? k->name : "(no match)");
1.1       markus    654:        if (k->name == NULL)
1.102     markus    655:                return SSH_ERR_NO_KEX_ALG_MATCH;
1.89      djm       656:        if ((kexalg = kex_alg_by_name(k->name)) == NULL)
1.102     markus    657:                return SSH_ERR_INTERNAL_ERROR;
1.89      djm       658:        k->kex_type = kexalg->type;
1.94      djm       659:        k->hash_alg = kexalg->hash_alg;
1.89      djm       660:        k->ec_nid = kexalg->ec_nid;
1.102     markus    661:        return 0;
1.1       markus    662: }
1.65      djm       663:
1.102     markus    664: static int
                    665: choose_hostkeyalg(struct kex *k, char *client, char *server)
1.1       markus    666: {
1.113   ! markus    667:        k->hostkey_alg = match_list(client, server, NULL);
1.102     markus    668:
1.110     djm       669:        debug("kex: host key algorithm: %s",
1.113   ! markus    670:            k->hostkey_alg ? k->hostkey_alg : "(no match)");
        !           671:        if (k->hostkey_alg == NULL)
1.102     markus    672:                return SSH_ERR_NO_HOSTKEY_ALG_MATCH;
1.113   ! markus    673:        k->hostkey_type = sshkey_type_from_name(k->hostkey_alg);
1.13      markus    674:        if (k->hostkey_type == KEY_UNSPEC)
1.102     markus    675:                return SSH_ERR_INTERNAL_ERROR;
1.113   ! markus    676:        k->hostkey_nid = sshkey_ecdsa_nid_from_name(k->hostkey_alg);
1.102     markus    677:        return 0;
1.1       markus    678: }
                    679:
1.56      djm       680: static int
1.53      markus    681: proposals_match(char *my[PROPOSAL_MAX], char *peer[PROPOSAL_MAX])
                    682: {
                    683:        static int check[] = {
                    684:                PROPOSAL_KEX_ALGS, PROPOSAL_SERVER_HOST_KEY_ALGS, -1
                    685:        };
                    686:        int *idx;
                    687:        char *p;
                    688:
                    689:        for (idx = &check[0]; *idx != -1; idx++) {
                    690:                if ((p = strchr(my[*idx], ',')) != NULL)
                    691:                        *p = '\0';
                    692:                if ((p = strchr(peer[*idx], ',')) != NULL)
                    693:                        *p = '\0';
                    694:                if (strcmp(my[*idx], peer[*idx]) != 0) {
                    695:                        debug2("proposal mismatch: my %s peer %s",
                    696:                            my[*idx], peer[*idx]);
                    697:                        return (0);
                    698:                }
                    699:        }
                    700:        debug2("proposals match");
                    701:        return (1);
                    702: }
                    703:
1.102     markus    704: static int
                    705: kex_choose_conf(struct ssh *ssh)
1.1       markus    706: {
1.102     markus    707:        struct kex *kex = ssh->kex;
                    708:        struct newkeys *newkeys;
                    709:        char **my = NULL, **peer = NULL;
1.26      markus    710:        char **cprop, **sprop;
1.27      markus    711:        int nenc, nmac, ncomp;
1.96      dtucker   712:        u_int mode, ctos, need, dh_need, authlen;
1.102     markus    713:        int r, first_kex_follows;
1.1       markus    714:
1.110     djm       715:        debug2("local %s KEXINIT proposal", kex->server ? "server" : "client");
                    716:        if ((r = kex_buf2prop(kex->my, NULL, &my)) != 0)
                    717:                goto out;
                    718:        debug2("peer %s KEXINIT proposal", kex->server ? "client" : "server");
                    719:        if ((r = kex_buf2prop(kex->peer, &first_kex_follows, &peer)) != 0)
1.102     markus    720:                goto out;
1.26      markus    721:
1.27      markus    722:        if (kex->server) {
1.26      markus    723:                cprop=peer;
                    724:                sprop=my;
                    725:        } else {
                    726:                cprop=my;
                    727:                sprop=peer;
1.82      andreas   728:        }
                    729:
                    730:        /* Check whether server offers roaming */
                    731:        if (!kex->server) {
1.102     markus    732:                char *roaming = match_list(KEX_RESUME,
                    733:                    peer[PROPOSAL_KEX_ALGS], NULL);
                    734:
1.82      andreas   735:                if (roaming) {
                    736:                        kex->roaming = 1;
1.91      djm       737:                        free(roaming);
1.113   ! markus    738:                }
        !           739:        }
        !           740:
        !           741:        /* Check whether client supports ext_info_c */
        !           742:        if (kex->server) {
        !           743:                char *ext;
        !           744:
        !           745:                ext = match_list("ext-info-c", peer[PROPOSAL_KEX_ALGS], NULL);
        !           746:                if (ext) {
        !           747:                        kex->ext_info_c = 1;
        !           748:                        free(ext);
1.82      andreas   749:                }
1.26      markus    750:        }
1.1       markus    751:
1.30      markus    752:        /* Algorithm Negotiation */
1.110     djm       753:        if ((r = choose_kex(kex, cprop[PROPOSAL_KEX_ALGS],
                    754:            sprop[PROPOSAL_KEX_ALGS])) != 0) {
                    755:                kex->failed_choice = peer[PROPOSAL_KEX_ALGS];
                    756:                peer[PROPOSAL_KEX_ALGS] = NULL;
                    757:                goto out;
                    758:        }
                    759:        if ((r = choose_hostkeyalg(kex, cprop[PROPOSAL_SERVER_HOST_KEY_ALGS],
                    760:            sprop[PROPOSAL_SERVER_HOST_KEY_ALGS])) != 0) {
                    761:                kex->failed_choice = peer[PROPOSAL_SERVER_HOST_KEY_ALGS];
                    762:                peer[PROPOSAL_SERVER_HOST_KEY_ALGS] = NULL;
                    763:                goto out;
                    764:        }
1.1       markus    765:        for (mode = 0; mode < MODE_MAX; mode++) {
1.102     markus    766:                if ((newkeys = calloc(1, sizeof(*newkeys))) == NULL) {
                    767:                        r = SSH_ERR_ALLOC_FAIL;
                    768:                        goto out;
                    769:                }
1.30      markus    770:                kex->newkeys[mode] = newkeys;
1.78      djm       771:                ctos = (!kex->server && mode == MODE_OUT) ||
                    772:                    (kex->server && mode == MODE_IN);
1.1       markus    773:                nenc  = ctos ? PROPOSAL_ENC_ALGS_CTOS  : PROPOSAL_ENC_ALGS_STOC;
                    774:                nmac  = ctos ? PROPOSAL_MAC_ALGS_CTOS  : PROPOSAL_MAC_ALGS_STOC;
                    775:                ncomp = ctos ? PROPOSAL_COMP_ALGS_CTOS : PROPOSAL_COMP_ALGS_STOC;
1.102     markus    776:                if ((r = choose_enc(&newkeys->enc, cprop[nenc],
1.107     djm       777:                    sprop[nenc])) != 0) {
                    778:                        kex->failed_choice = peer[nenc];
                    779:                        peer[nenc] = NULL;
1.102     markus    780:                        goto out;
1.107     djm       781:                }
1.102     markus    782:                authlen = cipher_authlen(newkeys->enc.cipher);
1.88      markus    783:                /* ignore mac for authenticated encryption */
1.102     markus    784:                if (authlen == 0 &&
                    785:                    (r = choose_mac(ssh, &newkeys->mac, cprop[nmac],
1.107     djm       786:                    sprop[nmac])) != 0) {
                    787:                        kex->failed_choice = peer[nmac];
                    788:                        peer[nmac] = NULL;
1.102     markus    789:                        goto out;
1.107     djm       790:                }
1.102     markus    791:                if ((r = choose_comp(&newkeys->comp, cprop[ncomp],
1.107     djm       792:                    sprop[ncomp])) != 0) {
                    793:                        kex->failed_choice = peer[ncomp];
                    794:                        peer[ncomp] = NULL;
1.102     markus    795:                        goto out;
1.107     djm       796:                }
1.110     djm       797:                debug("kex: %s cipher: %s MAC: %s compression: %s",
1.1       markus    798:                    ctos ? "client->server" : "server->client",
1.27      markus    799:                    newkeys->enc.name,
1.88      markus    800:                    authlen == 0 ? newkeys->mac.name : "<implicit>",
1.27      markus    801:                    newkeys->comp.name);
1.107     djm       802:        }
1.96      dtucker   803:        need = dh_need = 0;
1.1       markus    804:        for (mode = 0; mode < MODE_MAX; mode++) {
1.30      markus    805:                newkeys = kex->newkeys[mode];
1.97      markus    806:                need = MAX(need, newkeys->enc.key_len);
                    807:                need = MAX(need, newkeys->enc.block_size);
                    808:                need = MAX(need, newkeys->enc.iv_len);
                    809:                need = MAX(need, newkeys->mac.key_len);
                    810:                dh_need = MAX(dh_need, cipher_seclen(newkeys->enc.cipher));
                    811:                dh_need = MAX(dh_need, newkeys->enc.block_size);
                    812:                dh_need = MAX(dh_need, newkeys->enc.iv_len);
                    813:                dh_need = MAX(dh_need, newkeys->mac.key_len);
1.1       markus    814:        }
1.7       markus    815:        /* XXX need runden? */
1.27      markus    816:        kex->we_need = need;
1.96      dtucker   817:        kex->dh_need = dh_need;
1.53      markus    818:
                    819:        /* ignore the next message if the proposals do not match */
1.56      djm       820:        if (first_kex_follows && !proposals_match(my, peer) &&
1.102     markus    821:            !(ssh->compat & SSH_BUG_FIRSTKEX))
                    822:                ssh->dispatch_skip_packets = 1;
                    823:        r = 0;
                    824:  out:
1.26      markus    825:        kex_prop_free(my);
                    826:        kex_prop_free(peer);
1.102     markus    827:        return r;
1.26      markus    828: }
                    829:
1.102     markus    830: static int
                    831: derive_key(struct ssh *ssh, int id, u_int need, u_char *hash, u_int hashlen,
                    832:     const struct sshbuf *shared_secret, u_char **keyp)
1.26      markus    833: {
1.102     markus    834:        struct kex *kex = ssh->kex;
                    835:        struct ssh_digest_ctx *hashctx = NULL;
1.26      markus    836:        char c = id;
1.61      djm       837:        u_int have;
1.94      djm       838:        size_t mdsz;
1.61      djm       839:        u_char *digest;
1.102     markus    840:        int r;
1.62      djm       841:
1.94      djm       842:        if ((mdsz = ssh_digest_bytes(kex->hash_alg)) == 0)
1.102     markus    843:                return SSH_ERR_INVALID_ARGUMENT;
                    844:        if ((digest = calloc(1, roundup(need, mdsz))) == NULL) {
                    845:                r = SSH_ERR_ALLOC_FAIL;
                    846:                goto out;
                    847:        }
1.26      markus    848:
1.30      markus    849:        /* K1 = HASH(K || H || "A" || session_id) */
1.102     markus    850:        if ((hashctx = ssh_digest_start(kex->hash_alg)) == NULL ||
                    851:            ssh_digest_update_buffer(hashctx, shared_secret) != 0 ||
1.94      djm       852:            ssh_digest_update(hashctx, hash, hashlen) != 0 ||
                    853:            ssh_digest_update(hashctx, &c, 1) != 0 ||
                    854:            ssh_digest_update(hashctx, kex->session_id,
1.102     markus    855:            kex->session_id_len) != 0 ||
                    856:            ssh_digest_final(hashctx, digest, mdsz) != 0) {
                    857:                r = SSH_ERR_LIBCRYPTO_ERROR;
                    858:                goto out;
                    859:        }
1.94      djm       860:        ssh_digest_free(hashctx);
1.102     markus    861:        hashctx = NULL;
1.26      markus    862:
1.30      markus    863:        /*
                    864:         * expand key:
                    865:         * Kn = HASH(K || H || K1 || K2 || ... || Kn-1)
                    866:         * Key = K1 || K2 || ... || Kn
                    867:         */
1.26      markus    868:        for (have = mdsz; need > have; have += mdsz) {
1.102     markus    869:                if ((hashctx = ssh_digest_start(kex->hash_alg)) == NULL ||
                    870:                    ssh_digest_update_buffer(hashctx, shared_secret) != 0 ||
1.94      djm       871:                    ssh_digest_update(hashctx, hash, hashlen) != 0 ||
1.102     markus    872:                    ssh_digest_update(hashctx, digest, have) != 0 ||
                    873:                    ssh_digest_final(hashctx, digest + have, mdsz) != 0) {
                    874:                        r = SSH_ERR_LIBCRYPTO_ERROR;
                    875:                        goto out;
                    876:                }
1.94      djm       877:                ssh_digest_free(hashctx);
1.102     markus    878:                hashctx = NULL;
1.26      markus    879:        }
                    880: #ifdef DEBUG_KEX
                    881:        fprintf(stderr, "key '%c'== ", c);
                    882:        dump_digest("key", digest, need);
                    883: #endif
1.102     markus    884:        *keyp = digest;
                    885:        digest = NULL;
                    886:        r = 0;
                    887:  out:
                    888:        if (digest)
                    889:                free(digest);
                    890:        ssh_digest_free(hashctx);
                    891:        return r;
1.1       markus    892: }
                    893:
1.23      markus    894: #define NKEYS  6
1.102     markus    895: int
                    896: kex_derive_keys(struct ssh *ssh, u_char *hash, u_int hashlen,
                    897:     const struct sshbuf *shared_secret)
1.1       markus    898: {
1.102     markus    899:        struct kex *kex = ssh->kex;
1.15      markus    900:        u_char *keys[NKEYS];
1.102     markus    901:        u_int i, j, mode, ctos;
                    902:        int r;
1.1       markus    903:
1.65      djm       904:        for (i = 0; i < NKEYS; i++) {
1.102     markus    905:                if ((r = derive_key(ssh, 'A'+i, kex->we_need, hash, hashlen,
                    906:                    shared_secret, &keys[i])) != 0) {
                    907:                        for (j = 0; j < i; j++)
                    908:                                free(keys[j]);
                    909:                        return r;
                    910:                }
1.65      djm       911:        }
1.1       markus    912:        for (mode = 0; mode < MODE_MAX; mode++) {
1.69      deraadt   913:                ctos = (!kex->server && mode == MODE_OUT) ||
                    914:                    (kex->server && mode == MODE_IN);
1.100     markus    915:                kex->newkeys[mode]->enc.iv  = keys[ctos ? 0 : 1];
                    916:                kex->newkeys[mode]->enc.key = keys[ctos ? 2 : 3];
                    917:                kex->newkeys[mode]->mac.key = keys[ctos ? 4 : 5];
1.1       markus    918:        }
1.102     markus    919:        return 0;
1.95      djm       920: }
                    921:
1.99      markus    922: #ifdef WITH_OPENSSL
1.102     markus    923: int
                    924: kex_derive_keys_bn(struct ssh *ssh, u_char *hash, u_int hashlen,
                    925:     const BIGNUM *secret)
1.95      djm       926: {
1.102     markus    927:        struct sshbuf *shared_secret;
                    928:        int r;
1.95      djm       929:
1.102     markus    930:        if ((shared_secret = sshbuf_new()) == NULL)
                    931:                return SSH_ERR_ALLOC_FAIL;
                    932:        if ((r = sshbuf_put_bignum2(shared_secret, secret)) == 0)
                    933:                r = kex_derive_keys(ssh, hash, hashlen, shared_secret);
                    934:        sshbuf_free(shared_secret);
                    935:        return r;
1.27      markus    936: }
1.99      markus    937: #endif
1.57      djm       938:
1.99      markus    939: #ifdef WITH_SSH1
1.102     markus    940: int
1.57      djm       941: derive_ssh1_session_id(BIGNUM *host_modulus, BIGNUM *server_modulus,
                    942:     u_int8_t cookie[8], u_int8_t id[16])
                    943: {
1.105     djm       944:        u_int8_t hbuf[2048], sbuf[2048], obuf[SSH_DIGEST_MAX_LENGTH];
1.102     markus    945:        struct ssh_digest_ctx *hashctx = NULL;
1.105     djm       946:        size_t hlen, slen;
1.102     markus    947:        int r;
1.57      djm       948:
1.105     djm       949:        hlen = BN_num_bytes(host_modulus);
                    950:        slen = BN_num_bytes(server_modulus);
                    951:        if (hlen < (512 / 8) || (u_int)hlen > sizeof(hbuf) ||
                    952:            slen < (512 / 8) || (u_int)slen > sizeof(sbuf))
1.102     markus    953:                return SSH_ERR_KEY_BITS_MISMATCH;
1.105     djm       954:        if (BN_bn2bin(host_modulus, hbuf) <= 0 ||
                    955:            BN_bn2bin(server_modulus, sbuf) <= 0) {
                    956:                r = SSH_ERR_LIBCRYPTO_ERROR;
                    957:                goto out;
                    958:        }
                    959:        if ((hashctx = ssh_digest_start(SSH_DIGEST_MD5)) == NULL) {
                    960:                r = SSH_ERR_ALLOC_FAIL;
                    961:                goto out;
                    962:        }
                    963:        if (ssh_digest_update(hashctx, hbuf, hlen) != 0 ||
                    964:            ssh_digest_update(hashctx, sbuf, slen) != 0 ||
1.102     markus    965:            ssh_digest_update(hashctx, cookie, 8) != 0 ||
                    966:            ssh_digest_final(hashctx, obuf, sizeof(obuf)) != 0) {
                    967:                r = SSH_ERR_LIBCRYPTO_ERROR;
                    968:                goto out;
                    969:        }
1.94      djm       970:        memcpy(id, obuf, ssh_digest_bytes(SSH_DIGEST_MD5));
1.102     markus    971:        r = 0;
                    972:  out:
                    973:        ssh_digest_free(hashctx);
1.105     djm       974:        explicit_bzero(hbuf, sizeof(hbuf));
                    975:        explicit_bzero(sbuf, sizeof(sbuf));
1.98      djm       976:        explicit_bzero(obuf, sizeof(obuf));
1.102     markus    977:        return r;
1.1       markus    978: }
1.99      markus    979: #endif
1.26      markus    980:
1.84      djm       981: #if defined(DEBUG_KEX) || defined(DEBUG_KEXDH) || defined(DEBUG_KEXECDH)
1.26      markus    982: void
                    983: dump_digest(char *msg, u_char *digest, int len)
                    984: {
                    985:        fprintf(stderr, "%s\n", msg);
1.102     markus    986:        sshbuf_dump_data(digest, len, stderr);
1.26      markus    987: }
                    988: #endif