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

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