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

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