[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.117.2.1

1.117.2.1! markus      1: /* $OpenBSD: kex.c,v 1.117 2016/02/08 10:57:07 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.102     markus     49:
                     50: #include "ssherr.h"
                     51: #include "sshbuf.h"
1.94      djm        52: #include "digest.h"
1.48      provos     53:
1.35      itojun     54: /* prototype */
1.102     markus     55: static int kex_choose_conf(struct ssh *);
                     56: static int kex_input_newkeys(int, u_int32_t, void *);
1.86      djm        57:
1.110     djm        58: static const char *proposal_names[PROPOSAL_MAX] = {
                     59:        "KEX algorithms",
                     60:        "host key algorithms",
                     61:        "ciphers ctos",
                     62:        "ciphers stoc",
                     63:        "MACs ctos",
                     64:        "MACs stoc",
                     65:        "compression ctos",
                     66:        "compression stoc",
                     67:        "languages ctos",
                     68:        "languages stoc",
                     69: };
                     70:
1.89      djm        71: struct kexalg {
                     72:        char *name;
1.102     markus     73:        u_int type;
1.89      djm        74:        int ec_nid;
1.94      djm        75:        int hash_alg;
1.89      djm        76: };
                     77: static const struct kexalg kexalgs[] = {
1.99      markus     78: #ifdef WITH_OPENSSL
1.94      djm        79:        { KEX_DH1, KEX_DH_GRP1_SHA1, 0, SSH_DIGEST_SHA1 },
                     80:        { KEX_DH14, KEX_DH_GRP14_SHA1, 0, SSH_DIGEST_SHA1 },
                     81:        { KEX_DHGEX_SHA1, KEX_DH_GEX_SHA1, 0, SSH_DIGEST_SHA1 },
                     82:        { KEX_DHGEX_SHA256, KEX_DH_GEX_SHA256, 0, SSH_DIGEST_SHA256 },
                     83:        { KEX_ECDH_SHA2_NISTP256, KEX_ECDH_SHA2,
                     84:            NID_X9_62_prime256v1, SSH_DIGEST_SHA256 },
                     85:        { KEX_ECDH_SHA2_NISTP384, KEX_ECDH_SHA2, NID_secp384r1,
                     86:            SSH_DIGEST_SHA384 },
                     87:        { KEX_ECDH_SHA2_NISTP521, KEX_ECDH_SHA2, NID_secp521r1,
                     88:            SSH_DIGEST_SHA512 },
1.99      markus     89: #endif
1.94      djm        90:        { KEX_CURVE25519_SHA256, KEX_C25519_SHA256, 0, SSH_DIGEST_SHA256 },
                     91:        { NULL, -1, -1, -1},
1.89      djm        92: };
                     93:
                     94: char *
1.93      dtucker    95: kex_alg_list(char sep)
1.89      djm        96: {
1.102     markus     97:        char *ret = NULL, *tmp;
1.89      djm        98:        size_t nlen, rlen = 0;
                     99:        const struct kexalg *k;
                    100:
                    101:        for (k = kexalgs; k->name != NULL; k++) {
                    102:                if (ret != NULL)
1.93      dtucker   103:                        ret[rlen++] = sep;
1.89      djm       104:                nlen = strlen(k->name);
1.102     markus    105:                if ((tmp = realloc(ret, rlen + nlen + 2)) == NULL) {
                    106:                        free(ret);
                    107:                        return NULL;
                    108:                }
                    109:                ret = tmp;
1.89      djm       110:                memcpy(ret + rlen, k->name, nlen + 1);
                    111:                rlen += nlen;
                    112:        }
                    113:        return ret;
                    114: }
                    115:
                    116: static const struct kexalg *
                    117: kex_alg_by_name(const char *name)
                    118: {
                    119:        const struct kexalg *k;
                    120:
                    121:        for (k = kexalgs; k->name != NULL; k++) {
                    122:                if (strcmp(k->name, name) == 0)
                    123:                        return k;
                    124:        }
                    125:        return NULL;
                    126: }
                    127:
1.86      djm       128: /* Validate KEX method name list */
                    129: int
                    130: kex_names_valid(const char *names)
                    131: {
                    132:        char *s, *cp, *p;
                    133:
                    134:        if (names == NULL || strcmp(names, "") == 0)
                    135:                return 0;
1.102     markus    136:        if ((s = cp = strdup(names)) == NULL)
                    137:                return 0;
1.86      djm       138:        for ((p = strsep(&cp, ",")); p && *p != '\0';
                    139:            (p = strsep(&cp, ","))) {
1.89      djm       140:                if (kex_alg_by_name(p) == NULL) {
1.86      djm       141:                        error("Unsupported KEX algorithm \"%.100s\"", p);
1.91      djm       142:                        free(s);
1.86      djm       143:                        return 0;
                    144:                }
                    145:        }
                    146:        debug3("kex names ok: [%s]", names);
1.91      djm       147:        free(s);
1.86      djm       148:        return 1;
1.109     djm       149: }
                    150:
                    151: /*
                    152:  * Concatenate algorithm names, avoiding duplicates in the process.
                    153:  * Caller must free returned string.
                    154:  */
                    155: char *
                    156: kex_names_cat(const char *a, const char *b)
                    157: {
                    158:        char *ret = NULL, *tmp = NULL, *cp, *p;
                    159:        size_t len;
                    160:
                    161:        if (a == NULL || *a == '\0')
                    162:                return NULL;
                    163:        if (b == NULL || *b == '\0')
                    164:                return strdup(a);
                    165:        if (strlen(b) > 1024*1024)
                    166:                return NULL;
                    167:        len = strlen(a) + strlen(b) + 2;
                    168:        if ((tmp = cp = strdup(b)) == NULL ||
                    169:            (ret = calloc(1, len)) == NULL) {
                    170:                free(tmp);
                    171:                return NULL;
                    172:        }
                    173:        strlcpy(ret, a, len);
                    174:        for ((p = strsep(&cp, ",")); p && *p != '\0'; (p = strsep(&cp, ","))) {
                    175:                if (match_list(ret, p, NULL) != NULL)
                    176:                        continue; /* Algorithm already present */
                    177:                if (strlcat(ret, ",", len) >= len ||
                    178:                    strlcat(ret, p, len) >= len) {
                    179:                        free(tmp);
                    180:                        free(ret);
                    181:                        return NULL; /* Shouldn't happen */
                    182:                }
                    183:        }
                    184:        free(tmp);
                    185:        return ret;
                    186: }
                    187:
                    188: /*
                    189:  * Assemble a list of algorithms from a default list and a string from a
                    190:  * configuration file. The user-provided string may begin with '+' to
                    191:  * indicate that it should be appended to the default.
                    192:  */
                    193: int
                    194: kex_assemble_names(const char *def, char **list)
                    195: {
                    196:        char *ret;
                    197:
                    198:        if (list == NULL || *list == NULL || **list == '\0') {
                    199:                *list = strdup(def);
                    200:                return 0;
                    201:        }
                    202:        if (**list != '+') {
                    203:                return 0;
                    204:        }
                    205:
                    206:        if ((ret = kex_names_cat(def, *list + 1)) == NULL)
                    207:                return SSH_ERR_ALLOC_FAIL;
                    208:        free(*list);
                    209:        *list = ret;
                    210:        return 0;
1.86      djm       211: }
1.26      markus    212:
                    213: /* put algorithm proposal into buffer */
1.102     markus    214: int
                    215: kex_prop2buf(struct sshbuf *b, char *proposal[PROPOSAL_MAX])
1.1       markus    216: {
1.61      djm       217:        u_int i;
1.102     markus    218:        int r;
                    219:
                    220:        sshbuf_reset(b);
1.26      markus    221:
1.49      markus    222:        /*
                    223:         * add a dummy cookie, the cookie will be overwritten by
                    224:         * kex_send_kexinit(), each time a kexinit is set
                    225:         */
1.102     markus    226:        for (i = 0; i < KEX_COOKIE_LEN; i++) {
                    227:                if ((r = sshbuf_put_u8(b, 0)) != 0)
                    228:                        return r;
                    229:        }
                    230:        for (i = 0; i < PROPOSAL_MAX; i++) {
                    231:                if ((r = sshbuf_put_cstring(b, proposal[i])) != 0)
                    232:                        return r;
                    233:        }
                    234:        if ((r = sshbuf_put_u8(b, 0)) != 0 ||   /* first_kex_packet_follows */
                    235:            (r = sshbuf_put_u32(b, 0)) != 0)    /* uint32 reserved */
                    236:                return r;
                    237:        return 0;
1.1       markus    238: }
                    239:
1.26      markus    240: /* parse buffer and return algorithm proposal */
1.102     markus    241: int
                    242: kex_buf2prop(struct sshbuf *raw, int *first_kex_follows, char ***propp)
1.7       markus    243: {
1.102     markus    244:        struct sshbuf *b = NULL;
                    245:        u_char v;
1.78      djm       246:        u_int i;
1.102     markus    247:        char **proposal = NULL;
                    248:        int r;
1.7       markus    249:
1.102     markus    250:        *propp = NULL;
                    251:        if ((proposal = calloc(PROPOSAL_MAX, sizeof(char *))) == NULL)
                    252:                return SSH_ERR_ALLOC_FAIL;
                    253:        if ((b = sshbuf_fromb(raw)) == NULL) {
                    254:                r = SSH_ERR_ALLOC_FAIL;
                    255:                goto out;
                    256:        }
                    257:        if ((r = sshbuf_consume(b, KEX_COOKIE_LEN)) != 0) /* skip cookie */
                    258:                goto out;
1.7       markus    259:        /* extract kex init proposal strings */
                    260:        for (i = 0; i < PROPOSAL_MAX; i++) {
1.102     markus    261:                if ((r = sshbuf_get_cstring(b, &(proposal[i]), NULL)) != 0)
                    262:                        goto out;
1.110     djm       263:                debug2("%s: %s", proposal_names[i], proposal[i]);
1.7       markus    264:        }
1.26      markus    265:        /* first kex follows / reserved */
1.115     djm       266:        if ((r = sshbuf_get_u8(b, &v)) != 0 ||  /* first_kex_follows */
                    267:            (r = sshbuf_get_u32(b, &i)) != 0)   /* reserved */
1.102     markus    268:                goto out;
1.53      markus    269:        if (first_kex_follows != NULL)
1.115     djm       270:                *first_kex_follows = v;
1.110     djm       271:        debug2("first_kex_follows %d ", v);
                    272:        debug2("reserved %u ", i);
1.102     markus    273:        r = 0;
                    274:        *propp = proposal;
                    275:  out:
                    276:        if (r != 0 && proposal != NULL)
                    277:                kex_prop_free(proposal);
                    278:        sshbuf_free(b);
                    279:        return r;
1.1       markus    280: }
                    281:
1.102     markus    282: void
1.26      markus    283: kex_prop_free(char **proposal)
1.1       markus    284: {
1.61      djm       285:        u_int i;
1.26      markus    286:
1.106     djm       287:        if (proposal == NULL)
                    288:                return;
1.26      markus    289:        for (i = 0; i < PROPOSAL_MAX; i++)
1.91      djm       290:                free(proposal[i]);
                    291:        free(proposal);
1.1       markus    292: }
                    293:
1.78      djm       294: /* ARGSUSED */
1.101     markus    295: static int
1.41      markus    296: kex_protocol_error(int type, u_int32_t seq, void *ctxt)
1.1       markus    297: {
1.112     djm       298:        struct ssh *ssh = active_state; /* XXX */
                    299:        int r;
                    300:
                    301:        error("kex protocol error: type %d seq %u", type, seq);
                    302:        if ((r = sshpkt_start(ssh, SSH2_MSG_UNIMPLEMENTED)) != 0 ||
                    303:            (r = sshpkt_put_u32(ssh, seq)) != 0 ||
                    304:            (r = sshpkt_send(ssh)) != 0)
                    305:                return r;
1.101     markus    306:        return 0;
1.26      markus    307: }
1.1       markus    308:
1.35      itojun    309: static void
1.102     markus    310: kex_reset_dispatch(struct ssh *ssh)
1.29      markus    311: {
1.102     markus    312:        ssh_dispatch_range(ssh, SSH2_MSG_TRANSPORT_MIN,
1.42      markus    313:            SSH2_MSG_TRANSPORT_MAX, &kex_protocol_error);
1.102     markus    314:        ssh_dispatch_set(ssh, SSH2_MSG_KEXINIT, &kex_input_kexinit);
1.29      markus    315: }
                    316:
1.113     markus    317: static int
                    318: kex_send_ext_info(struct ssh *ssh)
                    319: {
                    320:        int r;
                    321:
                    322:        if ((r = sshpkt_start(ssh, SSH2_MSG_EXT_INFO)) != 0 ||
                    323:            (r = sshpkt_put_u32(ssh, 1)) != 0 ||
                    324:            (r = sshpkt_put_cstring(ssh, "server-sig-algs")) != 0 ||
                    325:            (r = sshpkt_put_cstring(ssh, "rsa-sha2-256,rsa-sha2-512")) != 0 ||
                    326:            (r = sshpkt_send(ssh)) != 0)
                    327:                return r;
                    328:        return 0;
                    329: }
                    330:
1.102     markus    331: int
                    332: kex_send_newkeys(struct ssh *ssh)
1.26      markus    333: {
1.102     markus    334:        int r;
1.28      markus    335:
1.102     markus    336:        kex_reset_dispatch(ssh);
                    337:        if ((r = sshpkt_start(ssh, SSH2_MSG_NEWKEYS)) != 0 ||
                    338:            (r = sshpkt_send(ssh)) != 0)
                    339:                return r;
1.26      markus    340:        debug("SSH2_MSG_NEWKEYS sent");
1.102     markus    341:        debug("expecting SSH2_MSG_NEWKEYS");
                    342:        ssh_dispatch_set(ssh, SSH2_MSG_NEWKEYS, &kex_input_newkeys);
1.113     markus    343:        if (ssh->kex->ext_info_c)
                    344:                if ((r = kex_send_ext_info(ssh)) != 0)
                    345:                        return r;
1.102     markus    346:        return 0;
                    347: }
                    348:
1.113     markus    349: int
                    350: kex_input_ext_info(int type, u_int32_t seq, void *ctxt)
                    351: {
                    352:        struct ssh *ssh = ctxt;
                    353:        struct kex *kex = ssh->kex;
                    354:        u_int32_t i, ninfo;
                    355:        char *name, *val, *found;
                    356:        int r;
                    357:
                    358:        debug("SSH2_MSG_EXT_INFO received");
                    359:        ssh_dispatch_set(ssh, SSH2_MSG_EXT_INFO, &kex_protocol_error);
                    360:        if ((r = sshpkt_get_u32(ssh, &ninfo)) != 0)
                    361:                return r;
                    362:        for (i = 0; i < ninfo; i++) {
                    363:                if ((r = sshpkt_get_cstring(ssh, &name, NULL)) != 0)
                    364:                        return r;
                    365:                if ((r = sshpkt_get_cstring(ssh, &val, NULL)) != 0) {
                    366:                        free(name);
                    367:                        return r;
                    368:                }
                    369:                debug("%s: %s=<%s>", __func__, name, val);
                    370:                if (strcmp(name, "server-sig-algs") == 0) {
                    371:                        found = match_list("rsa-sha2-256", val, NULL);
                    372:                        if (found) {
                    373:                                kex->rsa_sha2 = 256;
                    374:                                free(found);
                    375:                        }
                    376:                        found = match_list("rsa-sha2-512", val, NULL);
                    377:                        if (found) {
                    378:                                kex->rsa_sha2 = 512;
                    379:                                free(found);
                    380:                        }
                    381:                }
                    382:                free(name);
                    383:                free(val);
                    384:        }
                    385:        return sshpkt_get_end(ssh);
                    386: }
                    387:
1.102     markus    388: static int
                    389: kex_input_newkeys(int type, u_int32_t seq, void *ctxt)
                    390: {
                    391:        struct ssh *ssh = ctxt;
                    392:        struct kex *kex = ssh->kex;
                    393:        int r;
1.19      stevesk   394:
1.26      markus    395:        debug("SSH2_MSG_NEWKEYS received");
1.102     markus    396:        ssh_dispatch_set(ssh, SSH2_MSG_NEWKEYS, &kex_protocol_error);
                    397:        if ((r = sshpkt_get_end(ssh)) != 0)
                    398:                return r;
1.30      markus    399:        kex->done = 1;
1.102     markus    400:        sshbuf_reset(kex->peer);
                    401:        /* sshbuf_reset(kex->my); */
1.26      markus    402:        kex->flags &= ~KEX_INIT_SENT;
1.91      djm       403:        free(kex->name);
1.32      markus    404:        kex->name = NULL;
1.102     markus    405:        return 0;
1.26      markus    406: }
1.1       markus    407:
1.102     markus    408: int
                    409: kex_send_kexinit(struct ssh *ssh)
1.26      markus    410: {
1.49      markus    411:        u_char *cookie;
1.102     markus    412:        struct kex *kex = ssh->kex;
                    413:        int r;
1.49      markus    414:
1.102     markus    415:        if (kex == NULL)
                    416:                return SSH_ERR_INTERNAL_ERROR;
                    417:        if (kex->flags & KEX_INIT_SENT)
                    418:                return 0;
1.30      markus    419:        kex->done = 0;
1.49      markus    420:
                    421:        /* generate a random cookie */
1.102     markus    422:        if (sshbuf_len(kex->my) < KEX_COOKIE_LEN)
                    423:                return SSH_ERR_INVALID_FORMAT;
                    424:        if ((cookie = sshbuf_mutable_ptr(kex->my)) == NULL)
                    425:                return SSH_ERR_INTERNAL_ERROR;
                    426:        arc4random_buf(cookie, KEX_COOKIE_LEN);
                    427:
                    428:        if ((r = sshpkt_start(ssh, SSH2_MSG_KEXINIT)) != 0 ||
                    429:            (r = sshpkt_putb(ssh, kex->my)) != 0 ||
                    430:            (r = sshpkt_send(ssh)) != 0)
                    431:                return r;
1.26      markus    432:        debug("SSH2_MSG_KEXINIT sent");
                    433:        kex->flags |= KEX_INIT_SENT;
1.102     markus    434:        return 0;
1.1       markus    435: }
                    436:
1.78      djm       437: /* ARGSUSED */
1.101     markus    438: int
1.41      markus    439: kex_input_kexinit(int type, u_int32_t seq, void *ctxt)
1.11      provos    440: {
1.102     markus    441:        struct ssh *ssh = ctxt;
                    442:        struct kex *kex = ssh->kex;
                    443:        const u_char *ptr;
1.100     markus    444:        u_int i;
                    445:        size_t dlen;
1.102     markus    446:        int r;
1.11      provos    447:
1.26      markus    448:        debug("SSH2_MSG_KEXINIT received");
1.29      markus    449:        if (kex == NULL)
1.102     markus    450:                return SSH_ERR_INVALID_ARGUMENT;
1.11      provos    451:
1.117.2.1! markus    452:        ssh_dispatch_set(ssh, SSH2_MSG_KEXINIT, NULL);
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.117     djm       587: }
                    588:
                    589: /*
                    590:  * Request key re-exchange, returns 0 on success or a ssherr.h error
                    591:  * code otherwise. Must not be called if KEX is incomplete or in-progress.
                    592:  */
                    593: int
                    594: kex_start_rekex(struct ssh *ssh)
                    595: {
                    596:        if (ssh->kex == NULL) {
                    597:                error("%s: no kex", __func__);
                    598:                return SSH_ERR_INTERNAL_ERROR;
                    599:        }
                    600:        if (ssh->kex->done == 0) {
                    601:                error("%s: requested twice", __func__);
                    602:                return SSH_ERR_INTERNAL_ERROR;
                    603:        }
                    604:        ssh->kex->done = 0;
                    605:        return kex_send_kexinit(ssh);
1.1       markus    606: }
                    607:
1.102     markus    608: static int
                    609: choose_enc(struct sshenc *enc, 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_CIPHER_ALG_MATCH;
1.45      markus    615:        if ((enc->cipher = cipher_by_name(name)) == NULL)
1.102     markus    616:                return SSH_ERR_INTERNAL_ERROR;
1.1       markus    617:        enc->name = name;
                    618:        enc->enabled = 0;
                    619:        enc->iv = NULL;
1.88      markus    620:        enc->iv_len = cipher_ivlen(enc->cipher);
1.1       markus    621:        enc->key = NULL;
1.45      markus    622:        enc->key_len = cipher_keylen(enc->cipher);
                    623:        enc->block_size = cipher_blocksize(enc->cipher);
1.102     markus    624:        return 0;
1.1       markus    625: }
1.69      deraadt   626:
1.102     markus    627: static int
                    628: choose_mac(struct ssh *ssh, struct sshmac *mac, char *client, char *server)
1.1       markus    629: {
1.23      markus    630:        char *name = match_list(client, server, NULL);
1.102     markus    631:
1.1       markus    632:        if (name == NULL)
1.102     markus    633:                return SSH_ERR_NO_MAC_ALG_MATCH;
1.79      djm       634:        if (mac_setup(mac, name) < 0)
1.102     markus    635:                return SSH_ERR_INTERNAL_ERROR;
1.21      markus    636:        /* truncate the key */
1.102     markus    637:        if (ssh->compat & SSH_BUG_HMAC)
1.21      markus    638:                mac->key_len = 16;
1.1       markus    639:        mac->name = name;
                    640:        mac->key = NULL;
                    641:        mac->enabled = 0;
1.102     markus    642:        return 0;
1.1       markus    643: }
1.69      deraadt   644:
1.102     markus    645: static int
                    646: choose_comp(struct sshcomp *comp, char *client, char *server)
1.1       markus    647: {
1.23      markus    648:        char *name = match_list(client, server, NULL);
1.102     markus    649:
1.1       markus    650:        if (name == NULL)
1.102     markus    651:                return SSH_ERR_NO_COMPRESS_ALG_MATCH;
1.64      markus    652:        if (strcmp(name, "zlib@openssh.com") == 0) {
                    653:                comp->type = COMP_DELAYED;
                    654:        } else if (strcmp(name, "zlib") == 0) {
                    655:                comp->type = COMP_ZLIB;
1.1       markus    656:        } else if (strcmp(name, "none") == 0) {
1.64      markus    657:                comp->type = COMP_NONE;
1.1       markus    658:        } else {
1.102     markus    659:                return SSH_ERR_INTERNAL_ERROR;
1.1       markus    660:        }
                    661:        comp->name = name;
1.102     markus    662:        return 0;
1.1       markus    663: }
1.69      deraadt   664:
1.102     markus    665: static int
                    666: choose_kex(struct kex *k, char *client, char *server)
1.1       markus    667: {
1.89      djm       668:        const struct kexalg *kexalg;
                    669:
1.23      markus    670:        k->name = match_list(client, server, NULL);
1.102     markus    671:
1.110     djm       672:        debug("kex: algorithm: %s", k->name ? k->name : "(no match)");
1.1       markus    673:        if (k->name == NULL)
1.102     markus    674:                return SSH_ERR_NO_KEX_ALG_MATCH;
1.89      djm       675:        if ((kexalg = kex_alg_by_name(k->name)) == NULL)
1.102     markus    676:                return SSH_ERR_INTERNAL_ERROR;
1.89      djm       677:        k->kex_type = kexalg->type;
1.94      djm       678:        k->hash_alg = kexalg->hash_alg;
1.89      djm       679:        k->ec_nid = kexalg->ec_nid;
1.102     markus    680:        return 0;
1.1       markus    681: }
1.65      djm       682:
1.102     markus    683: static int
                    684: choose_hostkeyalg(struct kex *k, char *client, char *server)
1.1       markus    685: {
1.113     markus    686:        k->hostkey_alg = match_list(client, server, NULL);
1.102     markus    687:
1.110     djm       688:        debug("kex: host key algorithm: %s",
1.113     markus    689:            k->hostkey_alg ? k->hostkey_alg : "(no match)");
                    690:        if (k->hostkey_alg == NULL)
1.102     markus    691:                return SSH_ERR_NO_HOSTKEY_ALG_MATCH;
1.113     markus    692:        k->hostkey_type = sshkey_type_from_name(k->hostkey_alg);
1.13      markus    693:        if (k->hostkey_type == KEY_UNSPEC)
1.102     markus    694:                return SSH_ERR_INTERNAL_ERROR;
1.113     markus    695:        k->hostkey_nid = sshkey_ecdsa_nid_from_name(k->hostkey_alg);
1.102     markus    696:        return 0;
1.1       markus    697: }
                    698:
1.56      djm       699: static int
1.53      markus    700: proposals_match(char *my[PROPOSAL_MAX], char *peer[PROPOSAL_MAX])
                    701: {
                    702:        static int check[] = {
                    703:                PROPOSAL_KEX_ALGS, PROPOSAL_SERVER_HOST_KEY_ALGS, -1
                    704:        };
                    705:        int *idx;
                    706:        char *p;
                    707:
                    708:        for (idx = &check[0]; *idx != -1; idx++) {
                    709:                if ((p = strchr(my[*idx], ',')) != NULL)
                    710:                        *p = '\0';
                    711:                if ((p = strchr(peer[*idx], ',')) != NULL)
                    712:                        *p = '\0';
                    713:                if (strcmp(my[*idx], peer[*idx]) != 0) {
                    714:                        debug2("proposal mismatch: my %s peer %s",
                    715:                            my[*idx], peer[*idx]);
                    716:                        return (0);
                    717:                }
                    718:        }
                    719:        debug2("proposals match");
                    720:        return (1);
                    721: }
                    722:
1.102     markus    723: static int
                    724: kex_choose_conf(struct ssh *ssh)
1.1       markus    725: {
1.102     markus    726:        struct kex *kex = ssh->kex;
                    727:        struct newkeys *newkeys;
                    728:        char **my = NULL, **peer = NULL;
1.26      markus    729:        char **cprop, **sprop;
1.27      markus    730:        int nenc, nmac, ncomp;
1.96      dtucker   731:        u_int mode, ctos, need, dh_need, authlen;
1.102     markus    732:        int r, first_kex_follows;
1.1       markus    733:
1.110     djm       734:        debug2("local %s KEXINIT proposal", kex->server ? "server" : "client");
                    735:        if ((r = kex_buf2prop(kex->my, NULL, &my)) != 0)
                    736:                goto out;
                    737:        debug2("peer %s KEXINIT proposal", kex->server ? "client" : "server");
                    738:        if ((r = kex_buf2prop(kex->peer, &first_kex_follows, &peer)) != 0)
1.102     markus    739:                goto out;
1.26      markus    740:
1.27      markus    741:        if (kex->server) {
1.26      markus    742:                cprop=peer;
                    743:                sprop=my;
                    744:        } else {
                    745:                cprop=my;
                    746:                sprop=peer;
1.113     markus    747:        }
                    748:
                    749:        /* Check whether client supports ext_info_c */
                    750:        if (kex->server) {
                    751:                char *ext;
                    752:
                    753:                ext = match_list("ext-info-c", peer[PROPOSAL_KEX_ALGS], NULL);
                    754:                if (ext) {
                    755:                        kex->ext_info_c = 1;
                    756:                        free(ext);
1.82      andreas   757:                }
1.26      markus    758:        }
1.1       markus    759:
1.30      markus    760:        /* Algorithm Negotiation */
1.110     djm       761:        if ((r = choose_kex(kex, cprop[PROPOSAL_KEX_ALGS],
                    762:            sprop[PROPOSAL_KEX_ALGS])) != 0) {
                    763:                kex->failed_choice = peer[PROPOSAL_KEX_ALGS];
                    764:                peer[PROPOSAL_KEX_ALGS] = NULL;
                    765:                goto out;
                    766:        }
                    767:        if ((r = choose_hostkeyalg(kex, cprop[PROPOSAL_SERVER_HOST_KEY_ALGS],
                    768:            sprop[PROPOSAL_SERVER_HOST_KEY_ALGS])) != 0) {
                    769:                kex->failed_choice = peer[PROPOSAL_SERVER_HOST_KEY_ALGS];
                    770:                peer[PROPOSAL_SERVER_HOST_KEY_ALGS] = NULL;
                    771:                goto out;
                    772:        }
1.1       markus    773:        for (mode = 0; mode < MODE_MAX; mode++) {
1.102     markus    774:                if ((newkeys = calloc(1, sizeof(*newkeys))) == NULL) {
                    775:                        r = SSH_ERR_ALLOC_FAIL;
                    776:                        goto out;
                    777:                }
1.30      markus    778:                kex->newkeys[mode] = newkeys;
1.78      djm       779:                ctos = (!kex->server && mode == MODE_OUT) ||
                    780:                    (kex->server && mode == MODE_IN);
1.1       markus    781:                nenc  = ctos ? PROPOSAL_ENC_ALGS_CTOS  : PROPOSAL_ENC_ALGS_STOC;
                    782:                nmac  = ctos ? PROPOSAL_MAC_ALGS_CTOS  : PROPOSAL_MAC_ALGS_STOC;
                    783:                ncomp = ctos ? PROPOSAL_COMP_ALGS_CTOS : PROPOSAL_COMP_ALGS_STOC;
1.102     markus    784:                if ((r = choose_enc(&newkeys->enc, cprop[nenc],
1.107     djm       785:                    sprop[nenc])) != 0) {
                    786:                        kex->failed_choice = peer[nenc];
                    787:                        peer[nenc] = NULL;
1.102     markus    788:                        goto out;
1.107     djm       789:                }
1.102     markus    790:                authlen = cipher_authlen(newkeys->enc.cipher);
1.88      markus    791:                /* ignore mac for authenticated encryption */
1.102     markus    792:                if (authlen == 0 &&
                    793:                    (r = choose_mac(ssh, &newkeys->mac, cprop[nmac],
1.107     djm       794:                    sprop[nmac])) != 0) {
                    795:                        kex->failed_choice = peer[nmac];
                    796:                        peer[nmac] = NULL;
1.102     markus    797:                        goto out;
1.107     djm       798:                }
1.102     markus    799:                if ((r = choose_comp(&newkeys->comp, cprop[ncomp],
1.107     djm       800:                    sprop[ncomp])) != 0) {
                    801:                        kex->failed_choice = peer[ncomp];
                    802:                        peer[ncomp] = NULL;
1.102     markus    803:                        goto out;
1.107     djm       804:                }
1.110     djm       805:                debug("kex: %s cipher: %s MAC: %s compression: %s",
1.1       markus    806:                    ctos ? "client->server" : "server->client",
1.27      markus    807:                    newkeys->enc.name,
1.88      markus    808:                    authlen == 0 ? newkeys->mac.name : "<implicit>",
1.27      markus    809:                    newkeys->comp.name);
1.107     djm       810:        }
1.96      dtucker   811:        need = dh_need = 0;
1.1       markus    812:        for (mode = 0; mode < MODE_MAX; mode++) {
1.30      markus    813:                newkeys = kex->newkeys[mode];
1.97      markus    814:                need = MAX(need, newkeys->enc.key_len);
                    815:                need = MAX(need, newkeys->enc.block_size);
                    816:                need = MAX(need, newkeys->enc.iv_len);
                    817:                need = MAX(need, newkeys->mac.key_len);
                    818:                dh_need = MAX(dh_need, cipher_seclen(newkeys->enc.cipher));
                    819:                dh_need = MAX(dh_need, newkeys->enc.block_size);
                    820:                dh_need = MAX(dh_need, newkeys->enc.iv_len);
                    821:                dh_need = MAX(dh_need, newkeys->mac.key_len);
1.1       markus    822:        }
1.7       markus    823:        /* XXX need runden? */
1.27      markus    824:        kex->we_need = need;
1.96      dtucker   825:        kex->dh_need = dh_need;
1.53      markus    826:
                    827:        /* ignore the next message if the proposals do not match */
1.56      djm       828:        if (first_kex_follows && !proposals_match(my, peer) &&
1.102     markus    829:            !(ssh->compat & SSH_BUG_FIRSTKEX))
                    830:                ssh->dispatch_skip_packets = 1;
                    831:        r = 0;
                    832:  out:
1.26      markus    833:        kex_prop_free(my);
                    834:        kex_prop_free(peer);
1.102     markus    835:        return r;
1.26      markus    836: }
                    837:
1.102     markus    838: static int
                    839: derive_key(struct ssh *ssh, int id, u_int need, u_char *hash, u_int hashlen,
                    840:     const struct sshbuf *shared_secret, u_char **keyp)
1.26      markus    841: {
1.102     markus    842:        struct kex *kex = ssh->kex;
                    843:        struct ssh_digest_ctx *hashctx = NULL;
1.26      markus    844:        char c = id;
1.61      djm       845:        u_int have;
1.94      djm       846:        size_t mdsz;
1.61      djm       847:        u_char *digest;
1.102     markus    848:        int r;
1.62      djm       849:
1.94      djm       850:        if ((mdsz = ssh_digest_bytes(kex->hash_alg)) == 0)
1.102     markus    851:                return SSH_ERR_INVALID_ARGUMENT;
                    852:        if ((digest = calloc(1, roundup(need, mdsz))) == NULL) {
                    853:                r = SSH_ERR_ALLOC_FAIL;
                    854:                goto out;
                    855:        }
1.26      markus    856:
1.30      markus    857:        /* K1 = HASH(K || H || "A" || session_id) */
1.102     markus    858:        if ((hashctx = ssh_digest_start(kex->hash_alg)) == NULL ||
                    859:            ssh_digest_update_buffer(hashctx, shared_secret) != 0 ||
1.94      djm       860:            ssh_digest_update(hashctx, hash, hashlen) != 0 ||
                    861:            ssh_digest_update(hashctx, &c, 1) != 0 ||
                    862:            ssh_digest_update(hashctx, kex->session_id,
1.102     markus    863:            kex->session_id_len) != 0 ||
                    864:            ssh_digest_final(hashctx, digest, mdsz) != 0) {
                    865:                r = SSH_ERR_LIBCRYPTO_ERROR;
                    866:                goto out;
                    867:        }
1.94      djm       868:        ssh_digest_free(hashctx);
1.102     markus    869:        hashctx = NULL;
1.26      markus    870:
1.30      markus    871:        /*
                    872:         * expand key:
                    873:         * Kn = HASH(K || H || K1 || K2 || ... || Kn-1)
                    874:         * Key = K1 || K2 || ... || Kn
                    875:         */
1.26      markus    876:        for (have = mdsz; need > have; have += mdsz) {
1.102     markus    877:                if ((hashctx = ssh_digest_start(kex->hash_alg)) == NULL ||
                    878:                    ssh_digest_update_buffer(hashctx, shared_secret) != 0 ||
1.94      djm       879:                    ssh_digest_update(hashctx, hash, hashlen) != 0 ||
1.102     markus    880:                    ssh_digest_update(hashctx, digest, have) != 0 ||
                    881:                    ssh_digest_final(hashctx, digest + have, mdsz) != 0) {
                    882:                        r = SSH_ERR_LIBCRYPTO_ERROR;
                    883:                        goto out;
                    884:                }
1.94      djm       885:                ssh_digest_free(hashctx);
1.102     markus    886:                hashctx = NULL;
1.26      markus    887:        }
                    888: #ifdef DEBUG_KEX
                    889:        fprintf(stderr, "key '%c'== ", c);
                    890:        dump_digest("key", digest, need);
                    891: #endif
1.102     markus    892:        *keyp = digest;
                    893:        digest = NULL;
                    894:        r = 0;
                    895:  out:
1.114     mmcc      896:        free(digest);
1.102     markus    897:        ssh_digest_free(hashctx);
                    898:        return r;
1.1       markus    899: }
                    900:
1.23      markus    901: #define NKEYS  6
1.102     markus    902: int
                    903: kex_derive_keys(struct ssh *ssh, u_char *hash, u_int hashlen,
                    904:     const struct sshbuf *shared_secret)
1.1       markus    905: {
1.102     markus    906:        struct kex *kex = ssh->kex;
1.15      markus    907:        u_char *keys[NKEYS];
1.102     markus    908:        u_int i, j, mode, ctos;
                    909:        int r;
1.1       markus    910:
1.65      djm       911:        for (i = 0; i < NKEYS; i++) {
1.102     markus    912:                if ((r = derive_key(ssh, 'A'+i, kex->we_need, hash, hashlen,
                    913:                    shared_secret, &keys[i])) != 0) {
                    914:                        for (j = 0; j < i; j++)
                    915:                                free(keys[j]);
                    916:                        return r;
                    917:                }
1.65      djm       918:        }
1.1       markus    919:        for (mode = 0; mode < MODE_MAX; mode++) {
1.69      deraadt   920:                ctos = (!kex->server && mode == MODE_OUT) ||
                    921:                    (kex->server && mode == MODE_IN);
1.100     markus    922:                kex->newkeys[mode]->enc.iv  = keys[ctos ? 0 : 1];
                    923:                kex->newkeys[mode]->enc.key = keys[ctos ? 2 : 3];
                    924:                kex->newkeys[mode]->mac.key = keys[ctos ? 4 : 5];
1.1       markus    925:        }
1.102     markus    926:        return 0;
1.95      djm       927: }
                    928:
1.99      markus    929: #ifdef WITH_OPENSSL
1.102     markus    930: int
                    931: kex_derive_keys_bn(struct ssh *ssh, u_char *hash, u_int hashlen,
                    932:     const BIGNUM *secret)
1.95      djm       933: {
1.102     markus    934:        struct sshbuf *shared_secret;
                    935:        int r;
1.95      djm       936:
1.102     markus    937:        if ((shared_secret = sshbuf_new()) == NULL)
                    938:                return SSH_ERR_ALLOC_FAIL;
                    939:        if ((r = sshbuf_put_bignum2(shared_secret, secret)) == 0)
                    940:                r = kex_derive_keys(ssh, hash, hashlen, shared_secret);
                    941:        sshbuf_free(shared_secret);
                    942:        return r;
1.27      markus    943: }
1.99      markus    944: #endif
1.57      djm       945:
1.99      markus    946: #ifdef WITH_SSH1
1.102     markus    947: int
1.57      djm       948: derive_ssh1_session_id(BIGNUM *host_modulus, BIGNUM *server_modulus,
                    949:     u_int8_t cookie[8], u_int8_t id[16])
                    950: {
1.105     djm       951:        u_int8_t hbuf[2048], sbuf[2048], obuf[SSH_DIGEST_MAX_LENGTH];
1.102     markus    952:        struct ssh_digest_ctx *hashctx = NULL;
1.105     djm       953:        size_t hlen, slen;
1.102     markus    954:        int r;
1.57      djm       955:
1.105     djm       956:        hlen = BN_num_bytes(host_modulus);
                    957:        slen = BN_num_bytes(server_modulus);
                    958:        if (hlen < (512 / 8) || (u_int)hlen > sizeof(hbuf) ||
                    959:            slen < (512 / 8) || (u_int)slen > sizeof(sbuf))
1.102     markus    960:                return SSH_ERR_KEY_BITS_MISMATCH;
1.105     djm       961:        if (BN_bn2bin(host_modulus, hbuf) <= 0 ||
                    962:            BN_bn2bin(server_modulus, sbuf) <= 0) {
                    963:                r = SSH_ERR_LIBCRYPTO_ERROR;
                    964:                goto out;
                    965:        }
                    966:        if ((hashctx = ssh_digest_start(SSH_DIGEST_MD5)) == NULL) {
                    967:                r = SSH_ERR_ALLOC_FAIL;
                    968:                goto out;
                    969:        }
                    970:        if (ssh_digest_update(hashctx, hbuf, hlen) != 0 ||
                    971:            ssh_digest_update(hashctx, sbuf, slen) != 0 ||
1.102     markus    972:            ssh_digest_update(hashctx, cookie, 8) != 0 ||
                    973:            ssh_digest_final(hashctx, obuf, sizeof(obuf)) != 0) {
                    974:                r = SSH_ERR_LIBCRYPTO_ERROR;
                    975:                goto out;
                    976:        }
1.94      djm       977:        memcpy(id, obuf, ssh_digest_bytes(SSH_DIGEST_MD5));
1.102     markus    978:        r = 0;
                    979:  out:
                    980:        ssh_digest_free(hashctx);
1.105     djm       981:        explicit_bzero(hbuf, sizeof(hbuf));
                    982:        explicit_bzero(sbuf, sizeof(sbuf));
1.98      djm       983:        explicit_bzero(obuf, sizeof(obuf));
1.102     markus    984:        return r;
1.1       markus    985: }
1.99      markus    986: #endif
1.26      markus    987:
1.84      djm       988: #if defined(DEBUG_KEX) || defined(DEBUG_KEXDH) || defined(DEBUG_KEXECDH)
1.26      markus    989: void
                    990: dump_digest(char *msg, u_char *digest, int len)
                    991: {
                    992:        fprintf(stderr, "%s\n", msg);
1.102     markus    993:        sshbuf_dump_data(digest, len, stderr);
1.26      markus    994: }
                    995: #endif