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

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