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

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