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

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