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

1.89    ! djm         1: /* $OpenBSD: kex.c,v 1.88 2013/01/08 18:49:04 markus 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.73      stevesk    26: #include <sys/param.h>
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.76      deraadt    33: #include <openssl/crypto.h>
                     34:
                     35: #include "xmalloc.h"
1.1       markus     36: #include "ssh2.h"
                     37: #include "buffer.h"
1.7       markus     38: #include "packet.h"
1.1       markus     39: #include "compat.h"
1.18      markus     40: #include "cipher.h"
1.76      deraadt    41: #include "key.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.26      markus     46: #include "dispatch.h"
1.48      provos     47: #include "monitor.h"
1.82      andreas    48: #include "roaming.h"
1.48      provos     49:
1.35      itojun     50: /* prototype */
                     51: static void kex_kexinit_finish(Kex *);
                     52: static void kex_choose_conf(Kex *);
1.86      djm        53:
1.89    ! djm        54: struct kexalg {
        !            55:        char *name;
        !            56:        int type;
        !            57:        int ec_nid;
        !            58:        const EVP_MD *(*mdfunc)(void);
        !            59: };
        !            60: static const struct kexalg kexalgs[] = {
        !            61:        { KEX_DH1, KEX_DH_GRP1_SHA1, 0, EVP_sha1 },
        !            62:        { KEX_DH14, KEX_DH_GRP14_SHA1, 0, EVP_sha1 },
        !            63:        { KEX_DHGEX_SHA1, KEX_DH_GEX_SHA1, 0, EVP_sha1 },
        !            64:        { KEX_DHGEX_SHA256, KEX_DH_GEX_SHA256, 0, EVP_sha256 },
        !            65:        { KEX_DH1, KEX_DH_GRP1_SHA1, 0, EVP_sha1 },
        !            66:        { KEX_ECDH_SHA2_NISTP256, KEX_ECDH_SHA2, NID_X9_62_prime256v1, EVP_sha256 },
        !            67:        { KEX_ECDH_SHA2_NISTP384, KEX_ECDH_SHA2, NID_secp384r1, EVP_sha384 },
        !            68:        { KEX_ECDH_SHA2_NISTP521, KEX_ECDH_SHA2, NID_secp521r1, EVP_sha512 },
        !            69:        { NULL, -1, -1, NULL},
        !            70: };
        !            71:
        !            72: char *
        !            73: kex_alg_list(void)
        !            74: {
        !            75:        char *ret = NULL;
        !            76:        size_t nlen, rlen = 0;
        !            77:        const struct kexalg *k;
        !            78:
        !            79:        for (k = kexalgs; k->name != NULL; k++) {
        !            80:                if (ret != NULL)
        !            81:                        ret[rlen++] = '\n';
        !            82:                nlen = strlen(k->name);
        !            83:                ret = xrealloc(ret, 1, rlen + nlen + 2);
        !            84:                memcpy(ret + rlen, k->name, nlen + 1);
        !            85:                rlen += nlen;
        !            86:        }
        !            87:        return ret;
        !            88: }
        !            89:
        !            90: static const struct kexalg *
        !            91: kex_alg_by_name(const char *name)
        !            92: {
        !            93:        const struct kexalg *k;
        !            94:
        !            95:        for (k = kexalgs; k->name != NULL; k++) {
        !            96:                if (strcmp(k->name, name) == 0)
        !            97:                        return k;
        !            98:        }
        !            99:        return NULL;
        !           100: }
        !           101:
1.86      djm       102: /* Validate KEX method name list */
                    103: int
                    104: kex_names_valid(const char *names)
                    105: {
                    106:        char *s, *cp, *p;
                    107:
                    108:        if (names == NULL || strcmp(names, "") == 0)
                    109:                return 0;
                    110:        s = cp = xstrdup(names);
                    111:        for ((p = strsep(&cp, ",")); p && *p != '\0';
                    112:            (p = strsep(&cp, ","))) {
1.89    ! djm       113:                if (kex_alg_by_name(p) == NULL) {
1.86      djm       114:                        error("Unsupported KEX algorithm \"%.100s\"", p);
                    115:                        xfree(s);
                    116:                        return 0;
                    117:                }
                    118:        }
                    119:        debug3("kex names ok: [%s]", names);
                    120:        xfree(s);
                    121:        return 1;
                    122: }
1.26      markus    123:
                    124: /* put algorithm proposal into buffer */
1.35      itojun    125: static void
1.26      markus    126: kex_prop2buf(Buffer *b, char *proposal[PROPOSAL_MAX])
1.1       markus    127: {
1.61      djm       128:        u_int i;
1.26      markus    129:
                    130:        buffer_clear(b);
1.49      markus    131:        /*
                    132:         * add a dummy cookie, the cookie will be overwritten by
                    133:         * kex_send_kexinit(), each time a kexinit is set
                    134:         */
                    135:        for (i = 0; i < KEX_COOKIE_LEN; i++)
                    136:                buffer_put_char(b, 0);
1.1       markus    137:        for (i = 0; i < PROPOSAL_MAX; i++)
1.26      markus    138:                buffer_put_cstring(b, proposal[i]);
                    139:        buffer_put_char(b, 0);                  /* first_kex_packet_follows */
                    140:        buffer_put_int(b, 0);                   /* uint32 reserved */
1.1       markus    141: }
                    142:
1.26      markus    143: /* parse buffer and return algorithm proposal */
1.35      itojun    144: static char **
1.53      markus    145: kex_buf2prop(Buffer *raw, int *first_kex_follows)
1.7       markus    146: {
1.26      markus    147:        Buffer b;
1.78      djm       148:        u_int i;
1.26      markus    149:        char **proposal;
1.7       markus    150:
1.70      djm       151:        proposal = xcalloc(PROPOSAL_MAX, sizeof(char *));
1.7       markus    152:
1.26      markus    153:        buffer_init(&b);
                    154:        buffer_append(&b, buffer_ptr(raw), buffer_len(raw));
1.7       markus    155:        /* skip cookie */
                    156:        for (i = 0; i < KEX_COOKIE_LEN; i++)
1.26      markus    157:                buffer_get_char(&b);
1.7       markus    158:        /* extract kex init proposal strings */
                    159:        for (i = 0; i < PROPOSAL_MAX; i++) {
1.83      djm       160:                proposal[i] = buffer_get_cstring(&b,NULL);
1.26      markus    161:                debug2("kex_parse_kexinit: %s", proposal[i]);
1.7       markus    162:        }
1.26      markus    163:        /* first kex follows / reserved */
                    164:        i = buffer_get_char(&b);
1.53      markus    165:        if (first_kex_follows != NULL)
                    166:                *first_kex_follows = i;
1.26      markus    167:        debug2("kex_parse_kexinit: first_kex_follows %d ", i);
                    168:        i = buffer_get_int(&b);
1.78      djm       169:        debug2("kex_parse_kexinit: reserved %u ", i);
1.26      markus    170:        buffer_free(&b);
                    171:        return proposal;
1.1       markus    172: }
                    173:
1.35      itojun    174: static void
1.26      markus    175: kex_prop_free(char **proposal)
1.1       markus    176: {
1.61      djm       177:        u_int i;
1.26      markus    178:
                    179:        for (i = 0; i < PROPOSAL_MAX; i++)
                    180:                xfree(proposal[i]);
                    181:        xfree(proposal);
1.1       markus    182: }
                    183:
1.78      djm       184: /* ARGSUSED */
1.35      itojun    185: static void
1.41      markus    186: kex_protocol_error(int type, u_int32_t seq, void *ctxt)
1.1       markus    187: {
1.41      markus    188:        error("Hm, kex protocol error: type %d seq %u", type, seq);
1.26      markus    189: }
1.1       markus    190:
1.35      itojun    191: static void
1.44      markus    192: kex_reset_dispatch(void)
1.29      markus    193: {
1.42      markus    194:        dispatch_range(SSH2_MSG_TRANSPORT_MIN,
                    195:            SSH2_MSG_TRANSPORT_MAX, &kex_protocol_error);
1.44      markus    196:        dispatch_set(SSH2_MSG_KEXINIT, &kex_input_kexinit);
1.29      markus    197: }
                    198:
                    199: void
1.28      markus    200: kex_finish(Kex *kex)
1.26      markus    201: {
1.44      markus    202:        kex_reset_dispatch();
1.28      markus    203:
1.26      markus    204:        packet_start(SSH2_MSG_NEWKEYS);
                    205:        packet_send();
                    206:        /* packet_write_wait(); */
                    207:        debug("SSH2_MSG_NEWKEYS sent");
1.19      stevesk   208:
1.52      markus    209:        debug("expecting SSH2_MSG_NEWKEYS");
1.40      markus    210:        packet_read_expect(SSH2_MSG_NEWKEYS);
1.46      markus    211:        packet_check_eom();
1.26      markus    212:        debug("SSH2_MSG_NEWKEYS received");
1.32      markus    213:
1.30      markus    214:        kex->done = 1;
1.26      markus    215:        buffer_clear(&kex->peer);
1.27      markus    216:        /* buffer_clear(&kex->my); */
1.26      markus    217:        kex->flags &= ~KEX_INIT_SENT;
1.32      markus    218:        xfree(kex->name);
                    219:        kex->name = NULL;
1.26      markus    220: }
1.1       markus    221:
1.26      markus    222: void
                    223: kex_send_kexinit(Kex *kex)
                    224: {
1.60      avsm      225:        u_int32_t rnd = 0;
1.49      markus    226:        u_char *cookie;
1.61      djm       227:        u_int i;
1.49      markus    228:
1.29      markus    229:        if (kex == NULL) {
                    230:                error("kex_send_kexinit: no kex, cannot rekey");
                    231:                return;
                    232:        }
1.28      markus    233:        if (kex->flags & KEX_INIT_SENT) {
                    234:                debug("KEX_INIT_SENT");
                    235:                return;
                    236:        }
1.30      markus    237:        kex->done = 0;
1.49      markus    238:
                    239:        /* generate a random cookie */
                    240:        if (buffer_len(&kex->my) < KEX_COOKIE_LEN)
                    241:                fatal("kex_send_kexinit: kex proposal too short");
                    242:        cookie = buffer_ptr(&kex->my);
                    243:        for (i = 0; i < KEX_COOKIE_LEN; i++) {
                    244:                if (i % 4 == 0)
1.60      avsm      245:                        rnd = arc4random();
                    246:                cookie[i] = rnd;
                    247:                rnd >>= 8;
1.49      markus    248:        }
1.26      markus    249:        packet_start(SSH2_MSG_KEXINIT);
                    250:        packet_put_raw(buffer_ptr(&kex->my), buffer_len(&kex->my));
                    251:        packet_send();
                    252:        debug("SSH2_MSG_KEXINIT sent");
                    253:        kex->flags |= KEX_INIT_SENT;
1.1       markus    254: }
                    255:
1.78      djm       256: /* ARGSUSED */
1.26      markus    257: void
1.41      markus    258: kex_input_kexinit(int type, u_int32_t seq, void *ctxt)
1.11      provos    259: {
1.26      markus    260:        char *ptr;
1.61      djm       261:        u_int i, dlen;
1.26      markus    262:        Kex *kex = (Kex *)ctxt;
1.11      provos    263:
1.26      markus    264:        debug("SSH2_MSG_KEXINIT received");
1.29      markus    265:        if (kex == NULL)
                    266:                fatal("kex_input_kexinit: no kex, cannot rekey");
1.11      provos    267:
1.26      markus    268:        ptr = packet_get_raw(&dlen);
                    269:        buffer_append(&kex->peer, ptr, dlen);
1.31      markus    270:
                    271:        /* discard packet */
                    272:        for (i = 0; i < KEX_COOKIE_LEN; i++)
                    273:                packet_get_char();
                    274:        for (i = 0; i < PROPOSAL_MAX; i++)
                    275:                xfree(packet_get_string(NULL));
1.87      djm       276:        /*
                    277:         * XXX RFC4253 sec 7: "each side MAY guess" - currently no supported
                    278:         * KEX method has the server move first, but a server might be using
                    279:         * a custom method or one that we otherwise don't support. We should
                    280:         * be prepared to remember first_kex_follows here so we can eat a
                    281:         * packet later.
                    282:         * XXX2 - RFC4253 is kind of ambiguous on what first_kex_follows means
                    283:         * for cases where the server *doesn't* go first. I guess we should
                    284:         * ignore it when it is set for these cases, which is what we do now.
                    285:         */
                    286:        (void) packet_get_char();       /* first_kex_follows */
                    287:        (void) packet_get_int();        /* reserved */
1.39      markus    288:        packet_check_eom();
1.19      stevesk   289:
1.26      markus    290:        kex_kexinit_finish(kex);
                    291: }
1.11      provos    292:
1.26      markus    293: Kex *
1.28      markus    294: kex_setup(char *proposal[PROPOSAL_MAX])
1.26      markus    295: {
                    296:        Kex *kex;
1.11      provos    297:
1.70      djm       298:        kex = xcalloc(1, sizeof(*kex));
1.26      markus    299:        buffer_init(&kex->peer);
                    300:        buffer_init(&kex->my);
                    301:        kex_prop2buf(&kex->my, proposal);
1.30      markus    302:        kex->done = 0;
1.26      markus    303:
                    304:        kex_send_kexinit(kex);                                  /* we start */
1.44      markus    305:        kex_reset_dispatch();
1.26      markus    306:
                    307:        return kex;
1.11      provos    308: }
                    309:
1.35      itojun    310: static void
1.26      markus    311: kex_kexinit_finish(Kex *kex)
1.1       markus    312: {
1.26      markus    313:        if (!(kex->flags & KEX_INIT_SENT))
                    314:                kex_send_kexinit(kex);
1.1       markus    315:
1.26      markus    316:        kex_choose_conf(kex);
1.1       markus    317:
1.54      markus    318:        if (kex->kex_type >= 0 && kex->kex_type < KEX_MAX &&
                    319:            kex->kex[kex->kex_type] != NULL) {
                    320:                (kex->kex[kex->kex_type])(kex);
                    321:        } else {
1.26      markus    322:                fatal("Unsupported key exchange %d", kex->kex_type);
1.1       markus    323:        }
                    324: }
                    325:
1.35      itojun    326: static void
1.1       markus    327: choose_enc(Enc *enc, char *client, char *server)
                    328: {
1.23      markus    329:        char *name = match_list(client, server, NULL);
1.1       markus    330:        if (name == NULL)
1.78      djm       331:                fatal("no matching cipher found: client %s server %s",
                    332:                    client, server);
1.45      markus    333:        if ((enc->cipher = cipher_by_name(name)) == NULL)
1.12      markus    334:                fatal("matching cipher is not supported: %s", name);
1.1       markus    335:        enc->name = name;
                    336:        enc->enabled = 0;
                    337:        enc->iv = NULL;
1.88      markus    338:        enc->iv_len = cipher_ivlen(enc->cipher);
1.1       markus    339:        enc->key = NULL;
1.45      markus    340:        enc->key_len = cipher_keylen(enc->cipher);
                    341:        enc->block_size = cipher_blocksize(enc->cipher);
1.1       markus    342: }
1.69      deraadt   343:
1.35      itojun    344: static void
1.1       markus    345: choose_mac(Mac *mac, char *client, char *server)
                    346: {
1.23      markus    347:        char *name = match_list(client, server, NULL);
1.1       markus    348:        if (name == NULL)
1.78      djm       349:                fatal("no matching mac found: client %s server %s",
                    350:                    client, server);
1.79      djm       351:        if (mac_setup(mac, name) < 0)
1.1       markus    352:                fatal("unsupported mac %s", name);
1.21      markus    353:        /* truncate the key */
                    354:        if (datafellows & SSH_BUG_HMAC)
                    355:                mac->key_len = 16;
1.1       markus    356:        mac->name = name;
                    357:        mac->key = NULL;
                    358:        mac->enabled = 0;
                    359: }
1.69      deraadt   360:
1.35      itojun    361: static void
1.1       markus    362: choose_comp(Comp *comp, char *client, char *server)
                    363: {
1.23      markus    364:        char *name = match_list(client, server, NULL);
1.1       markus    365:        if (name == NULL)
                    366:                fatal("no matching comp found: client %s server %s", client, server);
1.64      markus    367:        if (strcmp(name, "zlib@openssh.com") == 0) {
                    368:                comp->type = COMP_DELAYED;
                    369:        } else if (strcmp(name, "zlib") == 0) {
                    370:                comp->type = COMP_ZLIB;
1.1       markus    371:        } else if (strcmp(name, "none") == 0) {
1.64      markus    372:                comp->type = COMP_NONE;
1.1       markus    373:        } else {
                    374:                fatal("unsupported comp %s", name);
                    375:        }
                    376:        comp->name = name;
                    377: }
1.69      deraadt   378:
1.35      itojun    379: static void
1.1       markus    380: choose_kex(Kex *k, char *client, char *server)
                    381: {
1.89    ! djm       382:        const struct kexalg *kexalg;
        !           383:
1.23      markus    384:        k->name = match_list(client, server, NULL);
1.1       markus    385:        if (k->name == NULL)
1.78      djm       386:                fatal("Unable to negotiate a key exchange method");
1.89    ! djm       387:        if ((kexalg = kex_alg_by_name(k->name)) == NULL)
        !           388:                fatal("unsupported kex alg %s", k->name);
        !           389:        k->kex_type = kexalg->type;
        !           390:        k->evp_md = kexalg->mdfunc();
        !           391:        k->ec_nid = kexalg->ec_nid;
1.1       markus    392: }
1.65      djm       393:
1.35      itojun    394: static void
1.1       markus    395: choose_hostkeyalg(Kex *k, char *client, char *server)
                    396: {
1.23      markus    397:        char *hostkeyalg = match_list(client, server, NULL);
1.13      markus    398:        if (hostkeyalg == NULL)
1.1       markus    399:                fatal("no hostkey alg");
1.13      markus    400:        k->hostkey_type = key_type_from_name(hostkeyalg);
                    401:        if (k->hostkey_type == KEY_UNSPEC)
                    402:                fatal("bad hostkey alg '%s'", hostkeyalg);
1.17      markus    403:        xfree(hostkeyalg);
1.1       markus    404: }
                    405:
1.56      djm       406: static int
1.53      markus    407: proposals_match(char *my[PROPOSAL_MAX], char *peer[PROPOSAL_MAX])
                    408: {
                    409:        static int check[] = {
                    410:                PROPOSAL_KEX_ALGS, PROPOSAL_SERVER_HOST_KEY_ALGS, -1
                    411:        };
                    412:        int *idx;
                    413:        char *p;
                    414:
                    415:        for (idx = &check[0]; *idx != -1; idx++) {
                    416:                if ((p = strchr(my[*idx], ',')) != NULL)
                    417:                        *p = '\0';
                    418:                if ((p = strchr(peer[*idx], ',')) != NULL)
                    419:                        *p = '\0';
                    420:                if (strcmp(my[*idx], peer[*idx]) != 0) {
                    421:                        debug2("proposal mismatch: my %s peer %s",
                    422:                            my[*idx], peer[*idx]);
                    423:                        return (0);
                    424:                }
                    425:        }
                    426:        debug2("proposals match");
                    427:        return (1);
                    428: }
                    429:
1.35      itojun    430: static void
1.27      markus    431: kex_choose_conf(Kex *kex)
1.1       markus    432: {
1.27      markus    433:        Newkeys *newkeys;
1.26      markus    434:        char **my, **peer;
                    435:        char **cprop, **sprop;
1.27      markus    436:        int nenc, nmac, ncomp;
1.88      markus    437:        u_int mode, ctos, need, authlen;
1.53      markus    438:        int first_kex_follows, type;
1.1       markus    439:
1.53      markus    440:        my   = kex_buf2prop(&kex->my, NULL);
                    441:        peer = kex_buf2prop(&kex->peer, &first_kex_follows);
1.26      markus    442:
1.27      markus    443:        if (kex->server) {
1.26      markus    444:                cprop=peer;
                    445:                sprop=my;
                    446:        } else {
                    447:                cprop=my;
                    448:                sprop=peer;
1.82      andreas   449:        }
                    450:
                    451:        /* Check whether server offers roaming */
                    452:        if (!kex->server) {
                    453:                char *roaming;
                    454:                roaming = match_list(KEX_RESUME, peer[PROPOSAL_KEX_ALGS], NULL);
                    455:                if (roaming) {
                    456:                        kex->roaming = 1;
                    457:                        xfree(roaming);
                    458:                }
1.26      markus    459:        }
1.1       markus    460:
1.30      markus    461:        /* Algorithm Negotiation */
1.1       markus    462:        for (mode = 0; mode < MODE_MAX; mode++) {
1.70      djm       463:                newkeys = xcalloc(1, sizeof(*newkeys));
1.30      markus    464:                kex->newkeys[mode] = newkeys;
1.78      djm       465:                ctos = (!kex->server && mode == MODE_OUT) ||
                    466:                    (kex->server && mode == MODE_IN);
1.1       markus    467:                nenc  = ctos ? PROPOSAL_ENC_ALGS_CTOS  : PROPOSAL_ENC_ALGS_STOC;
                    468:                nmac  = ctos ? PROPOSAL_MAC_ALGS_CTOS  : PROPOSAL_MAC_ALGS_STOC;
                    469:                ncomp = ctos ? PROPOSAL_COMP_ALGS_CTOS : PROPOSAL_COMP_ALGS_STOC;
1.88      markus    470:                choose_enc(&newkeys->enc, cprop[nenc], sprop[nenc]);
                    471:                /* ignore mac for authenticated encryption */
                    472:                authlen = cipher_authlen(newkeys->enc.cipher);
                    473:                if (authlen == 0)
                    474:                        choose_mac(&newkeys->mac, cprop[nmac], sprop[nmac]);
1.27      markus    475:                choose_comp(&newkeys->comp, cprop[ncomp], sprop[ncomp]);
1.2       markus    476:                debug("kex: %s %s %s %s",
1.1       markus    477:                    ctos ? "client->server" : "server->client",
1.27      markus    478:                    newkeys->enc.name,
1.88      markus    479:                    authlen == 0 ? newkeys->mac.name : "<implicit>",
1.27      markus    480:                    newkeys->comp.name);
1.1       markus    481:        }
1.27      markus    482:        choose_kex(kex, cprop[PROPOSAL_KEX_ALGS], sprop[PROPOSAL_KEX_ALGS]);
                    483:        choose_hostkeyalg(kex, cprop[PROPOSAL_SERVER_HOST_KEY_ALGS],
1.1       markus    484:            sprop[PROPOSAL_SERVER_HOST_KEY_ALGS]);
                    485:        need = 0;
                    486:        for (mode = 0; mode < MODE_MAX; mode++) {
1.30      markus    487:                newkeys = kex->newkeys[mode];
1.45      markus    488:                if (need < newkeys->enc.key_len)
                    489:                        need = newkeys->enc.key_len;
                    490:                if (need < newkeys->enc.block_size)
                    491:                        need = newkeys->enc.block_size;
1.88      markus    492:                if (need < newkeys->enc.iv_len)
                    493:                        need = newkeys->enc.iv_len;
1.27      markus    494:                if (need < newkeys->mac.key_len)
                    495:                        need = newkeys->mac.key_len;
1.1       markus    496:        }
1.7       markus    497:        /* XXX need runden? */
1.27      markus    498:        kex->we_need = need;
1.53      markus    499:
                    500:        /* ignore the next message if the proposals do not match */
1.56      djm       501:        if (first_kex_follows && !proposals_match(my, peer) &&
1.63      djm       502:            !(datafellows & SSH_BUG_FIRSTKEX)) {
1.53      markus    503:                type = packet_read();
                    504:                debug2("skipping next packet (type %u)", type);
                    505:        }
1.26      markus    506:
                    507:        kex_prop_free(my);
                    508:        kex_prop_free(peer);
                    509: }
                    510:
1.35      itojun    511: static u_char *
1.65      djm       512: derive_key(Kex *kex, int id, u_int need, u_char *hash, u_int hashlen,
                    513:     BIGNUM *shared_secret)
1.26      markus    514: {
                    515:        Buffer b;
                    516:        EVP_MD_CTX md;
                    517:        char c = id;
1.61      djm       518:        u_int have;
1.65      djm       519:        int mdsz;
1.61      djm       520:        u_char *digest;
1.62      djm       521:
1.65      djm       522:        if ((mdsz = EVP_MD_size(kex->evp_md)) <= 0)
                    523:                fatal("bad kex md size %d", mdsz);
1.68      deraadt   524:        digest = xmalloc(roundup(need, mdsz));
1.26      markus    525:
                    526:        buffer_init(&b);
                    527:        buffer_put_bignum2(&b, shared_secret);
                    528:
1.30      markus    529:        /* K1 = HASH(K || H || "A" || session_id) */
1.65      djm       530:        EVP_DigestInit(&md, kex->evp_md);
1.34      markus    531:        if (!(datafellows & SSH_BUG_DERIVEKEY))
                    532:                EVP_DigestUpdate(&md, buffer_ptr(&b), buffer_len(&b));
1.65      djm       533:        EVP_DigestUpdate(&md, hash, hashlen);
1.30      markus    534:        EVP_DigestUpdate(&md, &c, 1);
1.27      markus    535:        EVP_DigestUpdate(&md, kex->session_id, kex->session_id_len);
1.26      markus    536:        EVP_DigestFinal(&md, digest, NULL);
                    537:
1.30      markus    538:        /*
                    539:         * expand key:
                    540:         * Kn = HASH(K || H || K1 || K2 || ... || Kn-1)
                    541:         * Key = K1 || K2 || ... || Kn
                    542:         */
1.26      markus    543:        for (have = mdsz; need > have; have += mdsz) {
1.65      djm       544:                EVP_DigestInit(&md, kex->evp_md);
1.34      markus    545:                if (!(datafellows & SSH_BUG_DERIVEKEY))
                    546:                        EVP_DigestUpdate(&md, buffer_ptr(&b), buffer_len(&b));
1.65      djm       547:                EVP_DigestUpdate(&md, hash, hashlen);
1.26      markus    548:                EVP_DigestUpdate(&md, digest, have);
                    549:                EVP_DigestFinal(&md, digest + have, NULL);
                    550:        }
                    551:        buffer_free(&b);
                    552: #ifdef DEBUG_KEX
                    553:        fprintf(stderr, "key '%c'== ", c);
                    554:        dump_digest("key", digest, need);
                    555: #endif
                    556:        return digest;
1.1       markus    557: }
                    558:
1.30      markus    559: Newkeys *current_keys[MODE_MAX];
1.27      markus    560:
1.23      markus    561: #define NKEYS  6
1.26      markus    562: void
1.65      djm       563: kex_derive_keys(Kex *kex, u_char *hash, u_int hashlen, BIGNUM *shared_secret)
1.1       markus    564: {
1.15      markus    565:        u_char *keys[NKEYS];
1.61      djm       566:        u_int i, mode, ctos;
1.1       markus    567:
1.65      djm       568:        for (i = 0; i < NKEYS; i++) {
                    569:                keys[i] = derive_key(kex, 'A'+i, kex->we_need, hash, hashlen,
                    570:                    shared_secret);
                    571:        }
1.1       markus    572:
1.52      markus    573:        debug2("kex_derive_keys");
1.1       markus    574:        for (mode = 0; mode < MODE_MAX; mode++) {
1.30      markus    575:                current_keys[mode] = kex->newkeys[mode];
                    576:                kex->newkeys[mode] = NULL;
1.69      deraadt   577:                ctos = (!kex->server && mode == MODE_OUT) ||
                    578:                    (kex->server && mode == MODE_IN);
1.30      markus    579:                current_keys[mode]->enc.iv  = keys[ctos ? 0 : 1];
                    580:                current_keys[mode]->enc.key = keys[ctos ? 2 : 3];
                    581:                current_keys[mode]->mac.key = keys[ctos ? 4 : 5];
1.1       markus    582:        }
1.27      markus    583: }
                    584:
                    585: Newkeys *
                    586: kex_get_newkeys(int mode)
                    587: {
1.30      markus    588:        Newkeys *ret;
                    589:
                    590:        ret = current_keys[mode];
                    591:        current_keys[mode] = NULL;
                    592:        return ret;
1.57      djm       593: }
                    594:
                    595: void
                    596: derive_ssh1_session_id(BIGNUM *host_modulus, BIGNUM *server_modulus,
                    597:     u_int8_t cookie[8], u_int8_t id[16])
                    598: {
                    599:        const EVP_MD *evp_md = EVP_md5();
                    600:        EVP_MD_CTX md;
                    601:        u_int8_t nbuf[2048], obuf[EVP_MAX_MD_SIZE];
                    602:        int len;
                    603:
                    604:        EVP_DigestInit(&md, evp_md);
                    605:
                    606:        len = BN_num_bytes(host_modulus);
1.61      djm       607:        if (len < (512 / 8) || (u_int)len > sizeof(nbuf))
1.57      djm       608:                fatal("%s: bad host modulus (len %d)", __func__, len);
                    609:        BN_bn2bin(host_modulus, nbuf);
                    610:        EVP_DigestUpdate(&md, nbuf, len);
                    611:
                    612:        len = BN_num_bytes(server_modulus);
1.61      djm       613:        if (len < (512 / 8) || (u_int)len > sizeof(nbuf))
1.57      djm       614:                fatal("%s: bad server modulus (len %d)", __func__, len);
                    615:        BN_bn2bin(server_modulus, nbuf);
                    616:        EVP_DigestUpdate(&md, nbuf, len);
                    617:
                    618:        EVP_DigestUpdate(&md, cookie, 8);
                    619:
1.58      djm       620:        EVP_DigestFinal(&md, obuf, NULL);
1.57      djm       621:        memcpy(id, obuf, 16);
                    622:
                    623:        memset(nbuf, 0, sizeof(nbuf));
                    624:        memset(obuf, 0, sizeof(obuf));
                    625:        memset(&md, 0, sizeof(md));
1.1       markus    626: }
1.26      markus    627:
1.84      djm       628: #if defined(DEBUG_KEX) || defined(DEBUG_KEXDH) || defined(DEBUG_KEXECDH)
1.26      markus    629: void
                    630: dump_digest(char *msg, u_char *digest, int len)
                    631: {
1.84      djm       632:        int i;
1.26      markus    633:
                    634:        fprintf(stderr, "%s\n", msg);
1.77      stevesk   635:        for (i = 0; i < len; i++) {
1.26      markus    636:                fprintf(stderr, "%02x", digest[i]);
                    637:                if (i%32 == 31)
                    638:                        fprintf(stderr, "\n");
                    639:                else if (i%8 == 7)
                    640:                        fprintf(stderr, " ");
                    641:        }
                    642:        fprintf(stderr, "\n");
                    643: }
                    644: #endif