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

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