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

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