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

1.178.4.1! bluhm       1: /* $OpenBSD: kex.c,v 1.178 2023/03/12 10:40:39 dtucker Exp $ */
1.1       markus      2: /*
1.36      markus      3:  * Copyright (c) 2000, 2001 Markus Friedl.  All rights reserved.
1.1       markus      4:  *
                      5:  * Redistribution and use in source and binary forms, with or without
                      6:  * modification, are permitted provided that the following conditions
                      7:  * are met:
                      8:  * 1. Redistributions of source code must retain the above copyright
                      9:  *    notice, this list of conditions and the following disclaimer.
                     10:  * 2. Redistributions in binary form must reproduce the above copyright
                     11:  *    notice, this list of conditions and the following disclaimer in the
                     12:  *    documentation and/or other materials provided with the distribution.
                     13:  *
                     14:  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
                     15:  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
                     16:  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
                     17:  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
                     18:  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
                     19:  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
                     20:  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
                     21:  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
                     22:  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
                     23:  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
                     24:  */
                     25:
1.18      markus     26:
1.143     djm        27: #include <sys/types.h>
                     28: #include <errno.h>
1.76      deraadt    29: #include <signal.h>
1.75      stevesk    30: #include <stdio.h>
1.74      stevesk    31: #include <stdlib.h>
1.72      stevesk    32: #include <string.h>
1.143     djm        33: #include <unistd.h>
                     34: #include <poll.h>
1.1       markus     35:
1.99      markus     36: #ifdef WITH_OPENSSL
1.76      deraadt    37: #include <openssl/crypto.h>
1.99      markus     38: #endif
1.76      deraadt    39:
1.143     djm        40: #include "ssh.h"
1.1       markus     41: #include "ssh2.h"
1.143     djm        42: #include "atomicio.h"
                     43: #include "version.h"
1.7       markus     44: #include "packet.h"
1.1       markus     45: #include "compat.h"
1.18      markus     46: #include "cipher.h"
1.102     markus     47: #include "sshkey.h"
1.1       markus     48: #include "kex.h"
1.18      markus     49: #include "log.h"
1.21      markus     50: #include "mac.h"
1.23      markus     51: #include "match.h"
1.102     markus     52: #include "misc.h"
1.26      markus     53: #include "dispatch.h"
1.48      provos     54: #include "monitor.h"
1.176     dtucker    55: #include "myproposal.h"
1.102     markus     56:
                     57: #include "ssherr.h"
                     58: #include "sshbuf.h"
1.94      djm        59: #include "digest.h"
1.176     dtucker    60: #include "xmalloc.h"
1.48      provos     61:
1.35      itojun     62: /* prototype */
1.178.4.1! bluhm      63: static int kex_choose_conf(struct ssh *, uint32_t seq);
1.133     markus     64: static int kex_input_newkeys(int, u_int32_t, struct ssh *);
1.86      djm        65:
1.172     djm        66: static const char * const proposal_names[PROPOSAL_MAX] = {
1.110     djm        67:        "KEX algorithms",
                     68:        "host key algorithms",
                     69:        "ciphers ctos",
                     70:        "ciphers stoc",
                     71:        "MACs ctos",
                     72:        "MACs stoc",
                     73:        "compression ctos",
                     74:        "compression stoc",
                     75:        "languages ctos",
                     76:        "languages stoc",
                     77: };
                     78:
1.89      djm        79: struct kexalg {
                     80:        char *name;
1.102     markus     81:        u_int type;
1.89      djm        82:        int ec_nid;
1.94      djm        83:        int hash_alg;
1.89      djm        84: };
                     85: static const struct kexalg kexalgs[] = {
1.99      markus     86: #ifdef WITH_OPENSSL
1.94      djm        87:        { KEX_DH1, KEX_DH_GRP1_SHA1, 0, SSH_DIGEST_SHA1 },
1.118     djm        88:        { KEX_DH14_SHA1, KEX_DH_GRP14_SHA1, 0, SSH_DIGEST_SHA1 },
                     89:        { KEX_DH14_SHA256, KEX_DH_GRP14_SHA256, 0, SSH_DIGEST_SHA256 },
                     90:        { KEX_DH16_SHA512, KEX_DH_GRP16_SHA512, 0, SSH_DIGEST_SHA512 },
                     91:        { KEX_DH18_SHA512, KEX_DH_GRP18_SHA512, 0, SSH_DIGEST_SHA512 },
1.94      djm        92:        { KEX_DHGEX_SHA1, KEX_DH_GEX_SHA1, 0, SSH_DIGEST_SHA1 },
                     93:        { KEX_DHGEX_SHA256, KEX_DH_GEX_SHA256, 0, SSH_DIGEST_SHA256 },
                     94:        { KEX_ECDH_SHA2_NISTP256, KEX_ECDH_SHA2,
                     95:            NID_X9_62_prime256v1, SSH_DIGEST_SHA256 },
                     96:        { KEX_ECDH_SHA2_NISTP384, KEX_ECDH_SHA2, NID_secp384r1,
                     97:            SSH_DIGEST_SHA384 },
                     98:        { KEX_ECDH_SHA2_NISTP521, KEX_ECDH_SHA2, NID_secp521r1,
                     99:            SSH_DIGEST_SHA512 },
1.99      markus    100: #endif
1.94      djm       101:        { KEX_CURVE25519_SHA256, KEX_C25519_SHA256, 0, SSH_DIGEST_SHA256 },
1.124     djm       102:        { KEX_CURVE25519_SHA256_OLD, KEX_C25519_SHA256, 0, SSH_DIGEST_SHA256 },
1.163     djm       103:        { KEX_SNTRUP761X25519_SHA512, KEX_KEM_SNTRUP761X25519_SHA512, 0,
1.147     djm       104:            SSH_DIGEST_SHA512 },
1.155     dtucker   105:        { NULL, 0, -1, -1},
1.89      djm       106: };
                    107:
                    108: char *
1.93      dtucker   109: kex_alg_list(char sep)
1.89      djm       110: {
1.102     markus    111:        char *ret = NULL, *tmp;
1.89      djm       112:        size_t nlen, rlen = 0;
                    113:        const struct kexalg *k;
                    114:
                    115:        for (k = kexalgs; k->name != NULL; k++) {
                    116:                if (ret != NULL)
1.93      dtucker   117:                        ret[rlen++] = sep;
1.89      djm       118:                nlen = strlen(k->name);
1.102     markus    119:                if ((tmp = realloc(ret, rlen + nlen + 2)) == NULL) {
                    120:                        free(ret);
                    121:                        return NULL;
                    122:                }
                    123:                ret = tmp;
1.89      djm       124:                memcpy(ret + rlen, k->name, nlen + 1);
                    125:                rlen += nlen;
                    126:        }
                    127:        return ret;
                    128: }
                    129:
                    130: static const struct kexalg *
                    131: kex_alg_by_name(const char *name)
                    132: {
                    133:        const struct kexalg *k;
                    134:
                    135:        for (k = kexalgs; k->name != NULL; k++) {
                    136:                if (strcmp(k->name, name) == 0)
                    137:                        return k;
                    138:        }
                    139:        return NULL;
                    140: }
                    141:
1.86      djm       142: /* Validate KEX method name list */
                    143: int
                    144: kex_names_valid(const char *names)
                    145: {
                    146:        char *s, *cp, *p;
                    147:
                    148:        if (names == NULL || strcmp(names, "") == 0)
                    149:                return 0;
1.102     markus    150:        if ((s = cp = strdup(names)) == NULL)
                    151:                return 0;
1.86      djm       152:        for ((p = strsep(&cp, ",")); p && *p != '\0';
                    153:            (p = strsep(&cp, ","))) {
1.89      djm       154:                if (kex_alg_by_name(p) == NULL) {
1.86      djm       155:                        error("Unsupported KEX algorithm \"%.100s\"", p);
1.91      djm       156:                        free(s);
1.86      djm       157:                        return 0;
                    158:                }
                    159:        }
                    160:        debug3("kex names ok: [%s]", names);
1.91      djm       161:        free(s);
1.86      djm       162:        return 1;
1.109     djm       163: }
                    164:
1.178.4.1! bluhm     165: /* returns non-zero if proposal contains any algorithm from algs */
        !           166: static int
        !           167: has_any_alg(const char *proposal, const char *algs)
        !           168: {
        !           169:        char *cp;
        !           170:
        !           171:        if ((cp = match_list(proposal, algs, NULL)) == NULL)
        !           172:                return 0;
        !           173:        free(cp);
        !           174:        return 1;
        !           175: }
        !           176:
1.109     djm       177: /*
                    178:  * Concatenate algorithm names, avoiding duplicates in the process.
                    179:  * Caller must free returned string.
                    180:  */
                    181: char *
                    182: kex_names_cat(const char *a, const char *b)
                    183: {
1.178.4.1! bluhm     184:        char *ret = NULL, *tmp = NULL, *cp, *p;
1.109     djm       185:        size_t len;
                    186:
                    187:        if (a == NULL || *a == '\0')
1.138     djm       188:                return strdup(b);
1.109     djm       189:        if (b == NULL || *b == '\0')
                    190:                return strdup(a);
                    191:        if (strlen(b) > 1024*1024)
                    192:                return NULL;
                    193:        len = strlen(a) + strlen(b) + 2;
                    194:        if ((tmp = cp = strdup(b)) == NULL ||
                    195:            (ret = calloc(1, len)) == NULL) {
                    196:                free(tmp);
                    197:                return NULL;
                    198:        }
                    199:        strlcpy(ret, a, len);
                    200:        for ((p = strsep(&cp, ",")); p && *p != '\0'; (p = strsep(&cp, ","))) {
1.178.4.1! bluhm     201:                if (has_any_alg(ret, p))
1.109     djm       202:                        continue; /* Algorithm already present */
                    203:                if (strlcat(ret, ",", len) >= len ||
                    204:                    strlcat(ret, p, len) >= len) {
                    205:                        free(tmp);
                    206:                        free(ret);
                    207:                        return NULL; /* Shouldn't happen */
                    208:                }
                    209:        }
                    210:        free(tmp);
                    211:        return ret;
                    212: }
                    213:
                    214: /*
                    215:  * Assemble a list of algorithms from a default list and a string from a
                    216:  * configuration file. The user-provided string may begin with '+' to
1.154     naddy     217:  * indicate that it should be appended to the default, '-' that the
                    218:  * specified names should be removed, or '^' that they should be placed
                    219:  * at the head.
1.109     djm       220:  */
                    221: int
1.138     djm       222: kex_assemble_names(char **listp, const char *def, const char *all)
1.109     djm       223: {
1.138     djm       224:        char *cp, *tmp, *patterns;
                    225:        char *list = NULL, *ret = NULL, *matching = NULL, *opatterns = NULL;
                    226:        int r = SSH_ERR_INTERNAL_ERROR;
                    227:
1.153     djm       228:        if (listp == NULL || def == NULL || all == NULL)
                    229:                return SSH_ERR_INVALID_ARGUMENT;
                    230:
                    231:        if (*listp == NULL || **listp == '\0') {
1.138     djm       232:                if ((*listp = strdup(def)) == NULL)
                    233:                        return SSH_ERR_ALLOC_FAIL;
                    234:                return 0;
                    235:        }
1.109     djm       236:
1.138     djm       237:        list = *listp;
                    238:        *listp = NULL;
                    239:        if (*list == '+') {
                    240:                /* Append names to default list */
                    241:                if ((tmp = kex_names_cat(def, list + 1)) == NULL) {
                    242:                        r = SSH_ERR_ALLOC_FAIL;
                    243:                        goto fail;
                    244:                }
                    245:                free(list);
                    246:                list = tmp;
                    247:        } else if (*list == '-') {
                    248:                /* Remove names from default list */
1.159     djm       249:                if ((*listp = match_filter_denylist(def, list + 1)) == NULL) {
1.138     djm       250:                        r = SSH_ERR_ALLOC_FAIL;
                    251:                        goto fail;
                    252:                }
                    253:                free(list);
                    254:                /* filtering has already been done */
1.109     djm       255:                return 0;
1.154     naddy     256:        } else if (*list == '^') {
                    257:                /* Place names at head of default list */
                    258:                if ((tmp = kex_names_cat(list + 1, def)) == NULL) {
                    259:                        r = SSH_ERR_ALLOC_FAIL;
                    260:                        goto fail;
                    261:                }
                    262:                free(list);
                    263:                list = tmp;
1.138     djm       264:        } else {
                    265:                /* Explicit list, overrides default - just use "list" as is */
                    266:        }
                    267:
                    268:        /*
                    269:         * The supplied names may be a pattern-list. For the -list case,
                    270:         * the patterns are applied above. For the +list and explicit list
                    271:         * cases we need to do it now.
                    272:         */
                    273:        ret = NULL;
                    274:        if ((patterns = opatterns = strdup(list)) == NULL) {
                    275:                r = SSH_ERR_ALLOC_FAIL;
                    276:                goto fail;
                    277:        }
                    278:        /* Apply positive (i.e. non-negated) patterns from the list */
                    279:        while ((cp = strsep(&patterns, ",")) != NULL) {
                    280:                if (*cp == '!') {
                    281:                        /* negated matches are not supported here */
                    282:                        r = SSH_ERR_INVALID_ARGUMENT;
                    283:                        goto fail;
                    284:                }
                    285:                free(matching);
1.159     djm       286:                if ((matching = match_filter_allowlist(all, cp)) == NULL) {
1.138     djm       287:                        r = SSH_ERR_ALLOC_FAIL;
                    288:                        goto fail;
                    289:                }
                    290:                if ((tmp = kex_names_cat(ret, matching)) == NULL) {
                    291:                        r = SSH_ERR_ALLOC_FAIL;
                    292:                        goto fail;
                    293:                }
                    294:                free(ret);
                    295:                ret = tmp;
1.109     djm       296:        }
1.138     djm       297:        if (ret == NULL || *ret == '\0') {
                    298:                /* An empty name-list is an error */
                    299:                /* XXX better error code? */
                    300:                r = SSH_ERR_INVALID_ARGUMENT;
                    301:                goto fail;
1.109     djm       302:        }
                    303:
1.138     djm       304:        /* success */
                    305:        *listp = ret;
                    306:        ret = NULL;
                    307:        r = 0;
                    308:
                    309:  fail:
                    310:        free(matching);
                    311:        free(opatterns);
                    312:        free(list);
                    313:        free(ret);
                    314:        return r;
1.176     dtucker   315: }
                    316:
                    317: /*
                    318:  * Fill out a proposal array with dynamically allocated values, which may
                    319:  * be modified as required for compatibility reasons.
                    320:  * Any of the options may be NULL, in which case the default is used.
                    321:  * Array contents must be freed by calling kex_proposal_free_entries.
                    322:  */
                    323: void
                    324: kex_proposal_populate_entries(struct ssh *ssh, char *prop[PROPOSAL_MAX],
                    325:     const char *kexalgos, const char *ciphers, const char *macs,
                    326:     const char *comp, const char *hkalgs)
                    327: {
                    328:        const char *defpropserver[PROPOSAL_MAX] = { KEX_SERVER };
                    329:        const char *defpropclient[PROPOSAL_MAX] = { KEX_CLIENT };
                    330:        const char **defprop = ssh->kex->server ? defpropserver : defpropclient;
                    331:        u_int i;
1.178.4.1! bluhm     332:        char *cp;
1.176     dtucker   333:
                    334:        if (prop == NULL)
                    335:                fatal_f("proposal missing");
                    336:
1.178.4.1! bluhm     337:        /* Append EXT_INFO signalling to KexAlgorithms */
        !           338:        if (kexalgos == NULL)
        !           339:                kexalgos = defprop[PROPOSAL_KEX_ALGS];
        !           340:        if ((cp = kex_names_cat(kexalgos, ssh->kex->server ?
        !           341:            "kex-strict-s-v00@openssh.com" :
        !           342:            "ext-info-c,kex-strict-c-v00@openssh.com")) == NULL)
        !           343:                fatal_f("kex_names_cat");
        !           344:
1.176     dtucker   345:        for (i = 0; i < PROPOSAL_MAX; i++) {
                    346:                switch(i) {
                    347:                case PROPOSAL_KEX_ALGS:
1.178.4.1! bluhm     348:                        prop[i] = compat_kex_proposal(ssh, cp);
1.176     dtucker   349:                        break;
                    350:                case PROPOSAL_ENC_ALGS_CTOS:
                    351:                case PROPOSAL_ENC_ALGS_STOC:
                    352:                        prop[i] = xstrdup(ciphers ? ciphers : defprop[i]);
                    353:                        break;
                    354:                case PROPOSAL_MAC_ALGS_CTOS:
                    355:                case PROPOSAL_MAC_ALGS_STOC:
                    356:                        prop[i]  = xstrdup(macs ? macs : defprop[i]);
                    357:                        break;
                    358:                case PROPOSAL_COMP_ALGS_CTOS:
                    359:                case PROPOSAL_COMP_ALGS_STOC:
                    360:                        prop[i] = xstrdup(comp ? comp : defprop[i]);
                    361:                        break;
                    362:                case PROPOSAL_SERVER_HOST_KEY_ALGS:
                    363:                        prop[i] = xstrdup(hkalgs ? hkalgs : defprop[i]);
                    364:                        break;
                    365:                default:
                    366:                        prop[i] = xstrdup(defprop[i]);
                    367:                }
                    368:        }
1.178.4.1! bluhm     369:        free(cp);
1.176     dtucker   370: }
                    371:
                    372: void
                    373: kex_proposal_free_entries(char *prop[PROPOSAL_MAX])
                    374: {
                    375:        u_int i;
                    376:
                    377:        for (i = 0; i < PROPOSAL_MAX; i++)
                    378:                free(prop[i]);
1.86      djm       379: }
1.26      markus    380:
                    381: /* put algorithm proposal into buffer */
1.102     markus    382: int
                    383: kex_prop2buf(struct sshbuf *b, char *proposal[PROPOSAL_MAX])
1.1       markus    384: {
1.61      djm       385:        u_int i;
1.102     markus    386:        int r;
                    387:
                    388:        sshbuf_reset(b);
1.26      markus    389:
1.49      markus    390:        /*
                    391:         * add a dummy cookie, the cookie will be overwritten by
                    392:         * kex_send_kexinit(), each time a kexinit is set
                    393:         */
1.102     markus    394:        for (i = 0; i < KEX_COOKIE_LEN; i++) {
                    395:                if ((r = sshbuf_put_u8(b, 0)) != 0)
                    396:                        return r;
                    397:        }
                    398:        for (i = 0; i < PROPOSAL_MAX; i++) {
                    399:                if ((r = sshbuf_put_cstring(b, proposal[i])) != 0)
                    400:                        return r;
                    401:        }
                    402:        if ((r = sshbuf_put_u8(b, 0)) != 0 ||   /* first_kex_packet_follows */
                    403:            (r = sshbuf_put_u32(b, 0)) != 0)    /* uint32 reserved */
                    404:                return r;
                    405:        return 0;
1.1       markus    406: }
                    407:
1.26      markus    408: /* parse buffer and return algorithm proposal */
1.102     markus    409: int
                    410: kex_buf2prop(struct sshbuf *raw, int *first_kex_follows, char ***propp)
1.7       markus    411: {
1.102     markus    412:        struct sshbuf *b = NULL;
                    413:        u_char v;
1.78      djm       414:        u_int i;
1.102     markus    415:        char **proposal = NULL;
                    416:        int r;
1.7       markus    417:
1.102     markus    418:        *propp = NULL;
                    419:        if ((proposal = calloc(PROPOSAL_MAX, sizeof(char *))) == NULL)
                    420:                return SSH_ERR_ALLOC_FAIL;
                    421:        if ((b = sshbuf_fromb(raw)) == NULL) {
                    422:                r = SSH_ERR_ALLOC_FAIL;
                    423:                goto out;
                    424:        }
1.152     djm       425:        if ((r = sshbuf_consume(b, KEX_COOKIE_LEN)) != 0) { /* skip cookie */
1.160     djm       426:                error_fr(r, "consume cookie");
1.102     markus    427:                goto out;
1.152     djm       428:        }
1.7       markus    429:        /* extract kex init proposal strings */
                    430:        for (i = 0; i < PROPOSAL_MAX; i++) {
1.152     djm       431:                if ((r = sshbuf_get_cstring(b, &(proposal[i]), NULL)) != 0) {
1.160     djm       432:                        error_fr(r, "parse proposal %u", i);
1.102     markus    433:                        goto out;
1.152     djm       434:                }
1.110     djm       435:                debug2("%s: %s", proposal_names[i], proposal[i]);
1.7       markus    436:        }
1.26      markus    437:        /* first kex follows / reserved */
1.115     djm       438:        if ((r = sshbuf_get_u8(b, &v)) != 0 ||  /* first_kex_follows */
1.152     djm       439:            (r = sshbuf_get_u32(b, &i)) != 0) { /* reserved */
1.160     djm       440:                error_fr(r, "parse");
1.102     markus    441:                goto out;
1.152     djm       442:        }
1.53      markus    443:        if (first_kex_follows != NULL)
1.115     djm       444:                *first_kex_follows = v;
1.110     djm       445:        debug2("first_kex_follows %d ", v);
                    446:        debug2("reserved %u ", i);
1.102     markus    447:        r = 0;
                    448:        *propp = proposal;
                    449:  out:
                    450:        if (r != 0 && proposal != NULL)
                    451:                kex_prop_free(proposal);
                    452:        sshbuf_free(b);
                    453:        return r;
1.1       markus    454: }
                    455:
1.102     markus    456: void
1.26      markus    457: kex_prop_free(char **proposal)
1.1       markus    458: {
1.61      djm       459:        u_int i;
1.26      markus    460:
1.106     djm       461:        if (proposal == NULL)
                    462:                return;
1.26      markus    463:        for (i = 0; i < PROPOSAL_MAX; i++)
1.91      djm       464:                free(proposal[i]);
                    465:        free(proposal);
1.1       markus    466: }
                    467:
1.167     djm       468: int
1.133     markus    469: kex_protocol_error(int type, u_int32_t seq, struct ssh *ssh)
1.1       markus    470: {
1.112     djm       471:        int r;
                    472:
1.178.4.1! bluhm     473:        /* If in strict mode, any unexpected message is an error */
        !           474:        if ((ssh->kex->flags & KEX_INITIAL) && ssh->kex->kex_strict) {
        !           475:                ssh_packet_disconnect(ssh, "strict KEX violation: "
        !           476:                    "unexpected packet type %u (seqnr %u)", type, seq);
        !           477:        }
        !           478:        error_f("type %u seq %u", type, seq);
1.112     djm       479:        if ((r = sshpkt_start(ssh, SSH2_MSG_UNIMPLEMENTED)) != 0 ||
                    480:            (r = sshpkt_put_u32(ssh, seq)) != 0 ||
                    481:            (r = sshpkt_send(ssh)) != 0)
                    482:                return r;
1.101     markus    483:        return 0;
1.26      markus    484: }
1.1       markus    485:
1.35      itojun    486: static void
1.102     markus    487: kex_reset_dispatch(struct ssh *ssh)
1.29      markus    488: {
1.102     markus    489:        ssh_dispatch_range(ssh, SSH2_MSG_TRANSPORT_MIN,
1.42      markus    490:            SSH2_MSG_TRANSPORT_MAX, &kex_protocol_error);
1.29      markus    491: }
                    492:
1.113     markus    493: static int
                    494: kex_send_ext_info(struct ssh *ssh)
                    495: {
                    496:        int r;
1.121     djm       497:        char *algs;
1.113     markus    498:
1.151     djm       499:        debug("Sending SSH2_MSG_EXT_INFO");
1.130     djm       500:        if ((algs = sshkey_alg_list(0, 1, 1, ',')) == NULL)
1.121     djm       501:                return SSH_ERR_ALLOC_FAIL;
1.137     djm       502:        /* XXX filter algs list by allowed pubkey/hostbased types */
1.113     markus    503:        if ((r = sshpkt_start(ssh, SSH2_MSG_EXT_INFO)) != 0 ||
1.170     djm       504:            (r = sshpkt_put_u32(ssh, 2)) != 0 ||
1.113     markus    505:            (r = sshpkt_put_cstring(ssh, "server-sig-algs")) != 0 ||
1.121     djm       506:            (r = sshpkt_put_cstring(ssh, algs)) != 0 ||
1.170     djm       507:            (r = sshpkt_put_cstring(ssh,
                    508:            "publickey-hostbound@openssh.com")) != 0 ||
                    509:            (r = sshpkt_put_cstring(ssh, "0")) != 0 ||
1.152     djm       510:            (r = sshpkt_send(ssh)) != 0) {
1.160     djm       511:                error_fr(r, "compose");
1.121     djm       512:                goto out;
1.152     djm       513:        }
1.121     djm       514:        /* success */
                    515:        r = 0;
                    516:  out:
                    517:        free(algs);
1.123     djm       518:        return r;
1.113     markus    519: }
                    520:
1.102     markus    521: int
                    522: kex_send_newkeys(struct ssh *ssh)
1.26      markus    523: {
1.102     markus    524:        int r;
1.28      markus    525:
1.102     markus    526:        kex_reset_dispatch(ssh);
                    527:        if ((r = sshpkt_start(ssh, SSH2_MSG_NEWKEYS)) != 0 ||
                    528:            (r = sshpkt_send(ssh)) != 0)
                    529:                return r;
1.26      markus    530:        debug("SSH2_MSG_NEWKEYS sent");
1.102     markus    531:        ssh_dispatch_set(ssh, SSH2_MSG_NEWKEYS, &kex_input_newkeys);
1.151     djm       532:        if (ssh->kex->ext_info_c && (ssh->kex->flags & KEX_INITIAL) != 0)
1.113     markus    533:                if ((r = kex_send_ext_info(ssh)) != 0)
                    534:                        return r;
1.151     djm       535:        debug("expecting SSH2_MSG_NEWKEYS");
1.102     markus    536:        return 0;
                    537: }
                    538:
1.113     markus    539: int
1.133     markus    540: kex_input_ext_info(int type, u_int32_t seq, struct ssh *ssh)
1.113     markus    541: {
                    542:        struct kex *kex = ssh->kex;
                    543:        u_int32_t i, ninfo;
1.137     djm       544:        char *name;
1.134     djm       545:        u_char *val;
                    546:        size_t vlen;
1.113     markus    547:        int r;
                    548:
                    549:        debug("SSH2_MSG_EXT_INFO received");
                    550:        ssh_dispatch_set(ssh, SSH2_MSG_EXT_INFO, &kex_protocol_error);
                    551:        if ((r = sshpkt_get_u32(ssh, &ninfo)) != 0)
                    552:                return r;
1.178     dtucker   553:        if (ninfo >= 1024) {
                    554:                error("SSH2_MSG_EXT_INFO with too many entries, expected "
                    555:                    "<=1024, received %u", ninfo);
1.178.4.1! bluhm     556:                return dispatch_protocol_error(type, seq, ssh);
1.178     dtucker   557:        }
1.113     markus    558:        for (i = 0; i < ninfo; i++) {
                    559:                if ((r = sshpkt_get_cstring(ssh, &name, NULL)) != 0)
                    560:                        return r;
1.134     djm       561:                if ((r = sshpkt_get_string(ssh, &val, &vlen)) != 0) {
1.113     markus    562:                        free(name);
                    563:                        return r;
                    564:                }
                    565:                if (strcmp(name, "server-sig-algs") == 0) {
1.134     djm       566:                        /* Ensure no \0 lurking in value */
                    567:                        if (memchr(val, '\0', vlen) != NULL) {
1.160     djm       568:                                error_f("nul byte in %s", name);
1.134     djm       569:                                return SSH_ERR_INVALID_FORMAT;
                    570:                        }
1.160     djm       571:                        debug_f("%s=<%s>", name, val);
1.137     djm       572:                        kex->server_sig_algs = val;
                    573:                        val = NULL;
1.170     djm       574:                } else if (strcmp(name,
                    575:                    "publickey-hostbound@openssh.com") == 0) {
                    576:                        /* XXX refactor */
                    577:                        /* Ensure no \0 lurking in value */
                    578:                        if (memchr(val, '\0', vlen) != NULL) {
                    579:                                error_f("nul byte in %s", name);
                    580:                                return SSH_ERR_INVALID_FORMAT;
                    581:                        }
                    582:                        debug_f("%s=<%s>", name, val);
                    583:                        if (strcmp(val, "0") == 0)
                    584:                                kex->flags |= KEX_HAS_PUBKEY_HOSTBOUND;
                    585:                        else {
                    586:                                debug_f("unsupported version of %s extension",
                    587:                                    name);
                    588:                        }
1.134     djm       589:                } else
1.160     djm       590:                        debug_f("%s (unrecognised)", name);
1.113     markus    591:                free(name);
                    592:                free(val);
                    593:        }
                    594:        return sshpkt_get_end(ssh);
                    595: }
                    596:
1.102     markus    597: static int
1.133     markus    598: kex_input_newkeys(int type, u_int32_t seq, struct ssh *ssh)
1.102     markus    599: {
                    600:        struct kex *kex = ssh->kex;
                    601:        int r;
1.19      stevesk   602:
1.26      markus    603:        debug("SSH2_MSG_NEWKEYS received");
1.102     markus    604:        ssh_dispatch_set(ssh, SSH2_MSG_NEWKEYS, &kex_protocol_error);
1.131     markus    605:        ssh_dispatch_set(ssh, SSH2_MSG_KEXINIT, &kex_input_kexinit);
1.102     markus    606:        if ((r = sshpkt_get_end(ssh)) != 0)
1.122     markus    607:                return r;
                    608:        if ((r = ssh_set_newkeys(ssh, MODE_IN)) != 0)
1.102     markus    609:                return r;
1.30      markus    610:        kex->done = 1;
1.142     djm       611:        kex->flags &= ~KEX_INITIAL;
1.102     markus    612:        sshbuf_reset(kex->peer);
                    613:        /* sshbuf_reset(kex->my); */
1.26      markus    614:        kex->flags &= ~KEX_INIT_SENT;
1.91      djm       615:        free(kex->name);
1.32      markus    616:        kex->name = NULL;
1.102     markus    617:        return 0;
1.26      markus    618: }
1.1       markus    619:
1.102     markus    620: int
                    621: kex_send_kexinit(struct ssh *ssh)
1.26      markus    622: {
1.49      markus    623:        u_char *cookie;
1.102     markus    624:        struct kex *kex = ssh->kex;
                    625:        int r;
1.49      markus    626:
1.152     djm       627:        if (kex == NULL) {
1.161     djm       628:                error_f("no kex");
1.102     markus    629:                return SSH_ERR_INTERNAL_ERROR;
1.152     djm       630:        }
1.102     markus    631:        if (kex->flags & KEX_INIT_SENT)
                    632:                return 0;
1.30      markus    633:        kex->done = 0;
1.49      markus    634:
                    635:        /* generate a random cookie */
1.152     djm       636:        if (sshbuf_len(kex->my) < KEX_COOKIE_LEN) {
1.160     djm       637:                error_f("bad kex length: %zu < %d",
1.152     djm       638:                    sshbuf_len(kex->my), KEX_COOKIE_LEN);
1.102     markus    639:                return SSH_ERR_INVALID_FORMAT;
1.152     djm       640:        }
                    641:        if ((cookie = sshbuf_mutable_ptr(kex->my)) == NULL) {
1.160     djm       642:                error_f("buffer error");
1.102     markus    643:                return SSH_ERR_INTERNAL_ERROR;
1.152     djm       644:        }
1.102     markus    645:        arc4random_buf(cookie, KEX_COOKIE_LEN);
                    646:
                    647:        if ((r = sshpkt_start(ssh, SSH2_MSG_KEXINIT)) != 0 ||
                    648:            (r = sshpkt_putb(ssh, kex->my)) != 0 ||
1.152     djm       649:            (r = sshpkt_send(ssh)) != 0) {
1.160     djm       650:                error_fr(r, "compose reply");
1.102     markus    651:                return r;
1.152     djm       652:        }
1.26      markus    653:        debug("SSH2_MSG_KEXINIT sent");
                    654:        kex->flags |= KEX_INIT_SENT;
1.102     markus    655:        return 0;
1.1       markus    656: }
                    657:
1.101     markus    658: int
1.133     markus    659: kex_input_kexinit(int type, u_int32_t seq, struct ssh *ssh)
1.11      provos    660: {
1.102     markus    661:        struct kex *kex = ssh->kex;
                    662:        const u_char *ptr;
1.100     markus    663:        u_int i;
                    664:        size_t dlen;
1.102     markus    665:        int r;
1.11      provos    666:
1.26      markus    667:        debug("SSH2_MSG_KEXINIT received");
1.152     djm       668:        if (kex == NULL) {
1.161     djm       669:                error_f("no kex");
1.152     djm       670:                return SSH_ERR_INTERNAL_ERROR;
                    671:        }
1.178.4.1! bluhm     672:        ssh_dispatch_set(ssh, SSH2_MSG_KEXINIT, &kex_protocol_error);
1.102     markus    673:        ptr = sshpkt_ptr(ssh, &dlen);
                    674:        if ((r = sshbuf_put(kex->peer, ptr, dlen)) != 0)
                    675:                return r;
1.31      markus    676:
                    677:        /* discard packet */
1.152     djm       678:        for (i = 0; i < KEX_COOKIE_LEN; i++) {
                    679:                if ((r = sshpkt_get_u8(ssh, NULL)) != 0) {
1.160     djm       680:                        error_fr(r, "discard cookie");
1.102     markus    681:                        return r;
1.152     djm       682:                }
                    683:        }
                    684:        for (i = 0; i < PROPOSAL_MAX; i++) {
                    685:                if ((r = sshpkt_get_string(ssh, NULL, NULL)) != 0) {
1.160     djm       686:                        error_fr(r, "discard proposal");
1.102     markus    687:                        return r;
1.152     djm       688:                }
                    689:        }
1.87      djm       690:        /*
                    691:         * XXX RFC4253 sec 7: "each side MAY guess" - currently no supported
                    692:         * KEX method has the server move first, but a server might be using
                    693:         * a custom method or one that we otherwise don't support. We should
                    694:         * be prepared to remember first_kex_follows here so we can eat a
                    695:         * packet later.
                    696:         * XXX2 - RFC4253 is kind of ambiguous on what first_kex_follows means
                    697:         * for cases where the server *doesn't* go first. I guess we should
                    698:         * ignore it when it is set for these cases, which is what we do now.
                    699:         */
1.102     markus    700:        if ((r = sshpkt_get_u8(ssh, NULL)) != 0 ||      /* first_kex_follows */
                    701:            (r = sshpkt_get_u32(ssh, NULL)) != 0 ||     /* reserved */
                    702:            (r = sshpkt_get_end(ssh)) != 0)
                    703:                        return r;
                    704:
                    705:        if (!(kex->flags & KEX_INIT_SENT))
                    706:                if ((r = kex_send_kexinit(ssh)) != 0)
                    707:                        return r;
1.178.4.1! bluhm     708:        if ((r = kex_choose_conf(ssh, seq)) != 0)
1.102     markus    709:                return r;
1.19      stevesk   710:
1.102     markus    711:        if (kex->kex_type < KEX_MAX && kex->kex[kex->kex_type] != NULL)
                    712:                return (kex->kex[kex->kex_type])(ssh);
                    713:
1.160     djm       714:        error_f("unknown kex type %u", kex->kex_type);
1.102     markus    715:        return SSH_ERR_INTERNAL_ERROR;
                    716: }
                    717:
1.143     djm       718: struct kex *
                    719: kex_new(void)
1.102     markus    720: {
                    721:        struct kex *kex;
                    722:
1.143     djm       723:        if ((kex = calloc(1, sizeof(*kex))) == NULL ||
                    724:            (kex->peer = sshbuf_new()) == NULL ||
                    725:            (kex->my = sshbuf_new()) == NULL ||
                    726:            (kex->client_version = sshbuf_new()) == NULL ||
1.165     djm       727:            (kex->server_version = sshbuf_new()) == NULL ||
                    728:            (kex->session_id = sshbuf_new()) == NULL) {
1.143     djm       729:                kex_free(kex);
                    730:                return NULL;
1.102     markus    731:        }
1.143     djm       732:        return kex;
1.26      markus    733: }
1.11      provos    734:
1.100     markus    735: void
                    736: kex_free_newkeys(struct newkeys *newkeys)
                    737: {
                    738:        if (newkeys == NULL)
                    739:                return;
                    740:        if (newkeys->enc.key) {
                    741:                explicit_bzero(newkeys->enc.key, newkeys->enc.key_len);
                    742:                free(newkeys->enc.key);
                    743:                newkeys->enc.key = NULL;
                    744:        }
                    745:        if (newkeys->enc.iv) {
1.111     djm       746:                explicit_bzero(newkeys->enc.iv, newkeys->enc.iv_len);
1.100     markus    747:                free(newkeys->enc.iv);
                    748:                newkeys->enc.iv = NULL;
                    749:        }
                    750:        free(newkeys->enc.name);
                    751:        explicit_bzero(&newkeys->enc, sizeof(newkeys->enc));
                    752:        free(newkeys->comp.name);
                    753:        explicit_bzero(&newkeys->comp, sizeof(newkeys->comp));
                    754:        mac_clear(&newkeys->mac);
                    755:        if (newkeys->mac.key) {
                    756:                explicit_bzero(newkeys->mac.key, newkeys->mac.key_len);
                    757:                free(newkeys->mac.key);
                    758:                newkeys->mac.key = NULL;
                    759:        }
                    760:        free(newkeys->mac.name);
                    761:        explicit_bzero(&newkeys->mac, sizeof(newkeys->mac));
1.157     jsg       762:        freezero(newkeys, sizeof(*newkeys));
1.100     markus    763: }
                    764:
1.102     markus    765: void
                    766: kex_free(struct kex *kex)
1.26      markus    767: {
1.102     markus    768:        u_int mode;
1.11      provos    769:
1.143     djm       770:        if (kex == NULL)
                    771:                return;
                    772:
1.102     markus    773: #ifdef WITH_OPENSSL
1.136     jsing     774:        DH_free(kex->dh);
                    775:        EC_KEY_free(kex->ec_client_key);
1.102     markus    776: #endif
                    777:        for (mode = 0; mode < MODE_MAX; mode++) {
                    778:                kex_free_newkeys(kex->newkeys[mode]);
                    779:                kex->newkeys[mode] = NULL;
1.100     markus    780:        }
1.102     markus    781:        sshbuf_free(kex->peer);
                    782:        sshbuf_free(kex->my);
1.143     djm       783:        sshbuf_free(kex->client_version);
                    784:        sshbuf_free(kex->server_version);
1.149     djm       785:        sshbuf_free(kex->client_pub);
1.165     djm       786:        sshbuf_free(kex->session_id);
1.169     djm       787:        sshbuf_free(kex->initial_sig);
                    788:        sshkey_free(kex->initial_hostkey);
1.107     djm       789:        free(kex->failed_choice);
1.113     markus    790:        free(kex->hostkey_alg);
                    791:        free(kex->name);
1.102     markus    792:        free(kex);
1.11      provos    793: }
                    794:
1.102     markus    795: int
1.143     djm       796: kex_ready(struct ssh *ssh, char *proposal[PROPOSAL_MAX])
                    797: {
                    798:        int r;
                    799:
                    800:        if ((r = kex_prop2buf(ssh->kex->my, proposal)) != 0)
                    801:                return r;
                    802:        ssh->kex->flags = KEX_INITIAL;
                    803:        kex_reset_dispatch(ssh);
                    804:        ssh_dispatch_set(ssh, SSH2_MSG_KEXINIT, &kex_input_kexinit);
                    805:        return 0;
                    806: }
                    807:
                    808: int
1.102     markus    809: kex_setup(struct ssh *ssh, char *proposal[PROPOSAL_MAX])
1.1       markus    810: {
1.102     markus    811:        int r;
1.1       markus    812:
1.143     djm       813:        if ((r = kex_ready(ssh, proposal)) != 0)
1.102     markus    814:                return r;
                    815:        if ((r = kex_send_kexinit(ssh)) != 0) {         /* we start */
                    816:                kex_free(ssh->kex);
                    817:                ssh->kex = NULL;
                    818:                return r;
1.1       markus    819:        }
1.102     markus    820:        return 0;
1.117     djm       821: }
                    822:
                    823: /*
                    824:  * Request key re-exchange, returns 0 on success or a ssherr.h error
                    825:  * code otherwise. Must not be called if KEX is incomplete or in-progress.
                    826:  */
                    827: int
                    828: kex_start_rekex(struct ssh *ssh)
                    829: {
                    830:        if (ssh->kex == NULL) {
1.160     djm       831:                error_f("no kex");
1.117     djm       832:                return SSH_ERR_INTERNAL_ERROR;
                    833:        }
                    834:        if (ssh->kex->done == 0) {
1.160     djm       835:                error_f("requested twice");
1.117     djm       836:                return SSH_ERR_INTERNAL_ERROR;
                    837:        }
                    838:        ssh->kex->done = 0;
                    839:        return kex_send_kexinit(ssh);
1.1       markus    840: }
                    841:
1.102     markus    842: static int
                    843: choose_enc(struct sshenc *enc, char *client, char *server)
1.1       markus    844: {
1.23      markus    845:        char *name = match_list(client, server, NULL);
1.102     markus    846:
1.1       markus    847:        if (name == NULL)
1.102     markus    848:                return SSH_ERR_NO_CIPHER_ALG_MATCH;
1.129     dtucker   849:        if ((enc->cipher = cipher_by_name(name)) == NULL) {
1.160     djm       850:                error_f("unsupported cipher %s", name);
1.129     dtucker   851:                free(name);
1.102     markus    852:                return SSH_ERR_INTERNAL_ERROR;
1.129     dtucker   853:        }
1.1       markus    854:        enc->name = name;
                    855:        enc->enabled = 0;
                    856:        enc->iv = NULL;
1.88      markus    857:        enc->iv_len = cipher_ivlen(enc->cipher);
1.1       markus    858:        enc->key = NULL;
1.45      markus    859:        enc->key_len = cipher_keylen(enc->cipher);
                    860:        enc->block_size = cipher_blocksize(enc->cipher);
1.102     markus    861:        return 0;
1.1       markus    862: }
1.69      deraadt   863:
1.102     markus    864: static int
                    865: choose_mac(struct ssh *ssh, struct sshmac *mac, char *client, char *server)
1.1       markus    866: {
1.23      markus    867:        char *name = match_list(client, server, NULL);
1.102     markus    868:
1.1       markus    869:        if (name == NULL)
1.102     markus    870:                return SSH_ERR_NO_MAC_ALG_MATCH;
1.129     dtucker   871:        if (mac_setup(mac, name) < 0) {
1.160     djm       872:                error_f("unsupported MAC %s", name);
1.129     dtucker   873:                free(name);
1.102     markus    874:                return SSH_ERR_INTERNAL_ERROR;
1.129     dtucker   875:        }
1.1       markus    876:        mac->name = name;
                    877:        mac->key = NULL;
                    878:        mac->enabled = 0;
1.102     markus    879:        return 0;
1.1       markus    880: }
1.69      deraadt   881:
1.102     markus    882: static int
                    883: choose_comp(struct sshcomp *comp, char *client, char *server)
1.1       markus    884: {
1.23      markus    885:        char *name = match_list(client, server, NULL);
1.102     markus    886:
1.1       markus    887:        if (name == NULL)
1.102     markus    888:                return SSH_ERR_NO_COMPRESS_ALG_MATCH;
1.156     dtucker   889: #ifdef WITH_ZLIB
1.64      markus    890:        if (strcmp(name, "zlib@openssh.com") == 0) {
1.141     sf        891:                comp->type = COMP_DELAYED;
                    892:        } else if (strcmp(name, "zlib") == 0) {
1.140     sf        893:                comp->type = COMP_ZLIB;
1.156     dtucker   894:        } else
                    895: #endif /* WITH_ZLIB */
                    896:        if (strcmp(name, "none") == 0) {
1.64      markus    897:                comp->type = COMP_NONE;
1.1       markus    898:        } else {
1.160     djm       899:                error_f("unsupported compression scheme %s", name);
1.129     dtucker   900:                free(name);
1.102     markus    901:                return SSH_ERR_INTERNAL_ERROR;
1.1       markus    902:        }
                    903:        comp->name = name;
1.102     markus    904:        return 0;
1.1       markus    905: }
1.69      deraadt   906:
1.102     markus    907: static int
                    908: choose_kex(struct kex *k, char *client, char *server)
1.1       markus    909: {
1.89      djm       910:        const struct kexalg *kexalg;
                    911:
1.23      markus    912:        k->name = match_list(client, server, NULL);
1.102     markus    913:
1.110     djm       914:        debug("kex: algorithm: %s", k->name ? k->name : "(no match)");
1.1       markus    915:        if (k->name == NULL)
1.102     markus    916:                return SSH_ERR_NO_KEX_ALG_MATCH;
1.152     djm       917:        if ((kexalg = kex_alg_by_name(k->name)) == NULL) {
1.160     djm       918:                error_f("unsupported KEX method %s", k->name);
1.102     markus    919:                return SSH_ERR_INTERNAL_ERROR;
1.152     djm       920:        }
1.89      djm       921:        k->kex_type = kexalg->type;
1.94      djm       922:        k->hash_alg = kexalg->hash_alg;
1.89      djm       923:        k->ec_nid = kexalg->ec_nid;
1.102     markus    924:        return 0;
1.1       markus    925: }
1.65      djm       926:
1.102     markus    927: static int
                    928: choose_hostkeyalg(struct kex *k, char *client, char *server)
1.1       markus    929: {
1.162     djm       930:        free(k->hostkey_alg);
1.113     markus    931:        k->hostkey_alg = match_list(client, server, NULL);
1.102     markus    932:
1.110     djm       933:        debug("kex: host key algorithm: %s",
1.113     markus    934:            k->hostkey_alg ? k->hostkey_alg : "(no match)");
                    935:        if (k->hostkey_alg == NULL)
1.102     markus    936:                return SSH_ERR_NO_HOSTKEY_ALG_MATCH;
1.113     markus    937:        k->hostkey_type = sshkey_type_from_name(k->hostkey_alg);
1.152     djm       938:        if (k->hostkey_type == KEY_UNSPEC) {
1.160     djm       939:                error_f("unsupported hostkey algorithm %s", k->hostkey_alg);
1.102     markus    940:                return SSH_ERR_INTERNAL_ERROR;
1.152     djm       941:        }
1.113     markus    942:        k->hostkey_nid = sshkey_ecdsa_nid_from_name(k->hostkey_alg);
1.102     markus    943:        return 0;
1.1       markus    944: }
                    945:
1.56      djm       946: static int
1.53      markus    947: proposals_match(char *my[PROPOSAL_MAX], char *peer[PROPOSAL_MAX])
                    948: {
                    949:        static int check[] = {
                    950:                PROPOSAL_KEX_ALGS, PROPOSAL_SERVER_HOST_KEY_ALGS, -1
                    951:        };
                    952:        int *idx;
                    953:        char *p;
                    954:
                    955:        for (idx = &check[0]; *idx != -1; idx++) {
                    956:                if ((p = strchr(my[*idx], ',')) != NULL)
                    957:                        *p = '\0';
                    958:                if ((p = strchr(peer[*idx], ',')) != NULL)
                    959:                        *p = '\0';
                    960:                if (strcmp(my[*idx], peer[*idx]) != 0) {
                    961:                        debug2("proposal mismatch: my %s peer %s",
                    962:                            my[*idx], peer[*idx]);
                    963:                        return (0);
                    964:                }
                    965:        }
                    966:        debug2("proposals match");
                    967:        return (1);
                    968: }
                    969:
1.171     djm       970: static int
1.178.4.1! bluhm     971: kexalgs_contains(char **peer, const char *ext)
1.171     djm       972: {
1.178.4.1! bluhm     973:        return has_any_alg(peer[PROPOSAL_KEX_ALGS], ext);
1.171     djm       974: }
                    975:
1.102     markus    976: static int
1.178.4.1! bluhm     977: kex_choose_conf(struct ssh *ssh, uint32_t seq)
1.1       markus    978: {
1.102     markus    979:        struct kex *kex = ssh->kex;
                    980:        struct newkeys *newkeys;
                    981:        char **my = NULL, **peer = NULL;
1.26      markus    982:        char **cprop, **sprop;
1.27      markus    983:        int nenc, nmac, ncomp;
1.96      dtucker   984:        u_int mode, ctos, need, dh_need, authlen;
1.102     markus    985:        int r, first_kex_follows;
1.1       markus    986:
1.110     djm       987:        debug2("local %s KEXINIT proposal", kex->server ? "server" : "client");
                    988:        if ((r = kex_buf2prop(kex->my, NULL, &my)) != 0)
                    989:                goto out;
                    990:        debug2("peer %s KEXINIT proposal", kex->server ? "client" : "server");
                    991:        if ((r = kex_buf2prop(kex->peer, &first_kex_follows, &peer)) != 0)
1.102     markus    992:                goto out;
1.26      markus    993:
1.27      markus    994:        if (kex->server) {
1.26      markus    995:                cprop=peer;
                    996:                sprop=my;
                    997:        } else {
                    998:                cprop=my;
                    999:                sprop=peer;
1.113     markus   1000:        }
                   1001:
1.178.4.1! bluhm    1002:        /* Check whether peer supports ext_info/kex_strict */
        !          1003:        if ((kex->flags & KEX_INITIAL) != 0) {
        !          1004:                if (kex->server) {
        !          1005:                        kex->ext_info_c = kexalgs_contains(peer, "ext-info-c");
        !          1006:                        kex->kex_strict = kexalgs_contains(peer,
        !          1007:                            "kex-strict-c-v00@openssh.com");
        !          1008:                } else {
        !          1009:                        kex->kex_strict = kexalgs_contains(peer,
        !          1010:                            "kex-strict-s-v00@openssh.com");
        !          1011:                }
        !          1012:                if (kex->kex_strict) {
        !          1013:                        debug3_f("will use strict KEX ordering");
        !          1014:                        if (seq != 0)
        !          1015:                                ssh_packet_disconnect(ssh,
        !          1016:                                    "strict KEX violation: "
        !          1017:                                    "KEXINIT was not the first packet");
        !          1018:                }
1.171     djm      1019:        }
                   1020:
                   1021:        /* Check whether client supports rsa-sha2 algorithms */
                   1022:        if (kex->server && (kex->flags & KEX_INITIAL)) {
                   1023:                if (has_any_alg(peer[PROPOSAL_SERVER_HOST_KEY_ALGS],
                   1024:                    "rsa-sha2-256,rsa-sha2-256-cert-v01@openssh.com"))
                   1025:                        kex->flags |= KEX_RSA_SHA2_256_SUPPORTED;
                   1026:                if (has_any_alg(peer[PROPOSAL_SERVER_HOST_KEY_ALGS],
                   1027:                    "rsa-sha2-512,rsa-sha2-512-cert-v01@openssh.com"))
                   1028:                        kex->flags |= KEX_RSA_SHA2_512_SUPPORTED;
1.26      markus   1029:        }
1.1       markus   1030:
1.30      markus   1031:        /* Algorithm Negotiation */
1.110     djm      1032:        if ((r = choose_kex(kex, cprop[PROPOSAL_KEX_ALGS],
                   1033:            sprop[PROPOSAL_KEX_ALGS])) != 0) {
                   1034:                kex->failed_choice = peer[PROPOSAL_KEX_ALGS];
                   1035:                peer[PROPOSAL_KEX_ALGS] = NULL;
                   1036:                goto out;
                   1037:        }
                   1038:        if ((r = choose_hostkeyalg(kex, cprop[PROPOSAL_SERVER_HOST_KEY_ALGS],
                   1039:            sprop[PROPOSAL_SERVER_HOST_KEY_ALGS])) != 0) {
                   1040:                kex->failed_choice = peer[PROPOSAL_SERVER_HOST_KEY_ALGS];
                   1041:                peer[PROPOSAL_SERVER_HOST_KEY_ALGS] = NULL;
                   1042:                goto out;
                   1043:        }
1.1       markus   1044:        for (mode = 0; mode < MODE_MAX; mode++) {
1.102     markus   1045:                if ((newkeys = calloc(1, sizeof(*newkeys))) == NULL) {
                   1046:                        r = SSH_ERR_ALLOC_FAIL;
                   1047:                        goto out;
                   1048:                }
1.30      markus   1049:                kex->newkeys[mode] = newkeys;
1.78      djm      1050:                ctos = (!kex->server && mode == MODE_OUT) ||
                   1051:                    (kex->server && mode == MODE_IN);
1.1       markus   1052:                nenc  = ctos ? PROPOSAL_ENC_ALGS_CTOS  : PROPOSAL_ENC_ALGS_STOC;
                   1053:                nmac  = ctos ? PROPOSAL_MAC_ALGS_CTOS  : PROPOSAL_MAC_ALGS_STOC;
                   1054:                ncomp = ctos ? PROPOSAL_COMP_ALGS_CTOS : PROPOSAL_COMP_ALGS_STOC;
1.102     markus   1055:                if ((r = choose_enc(&newkeys->enc, cprop[nenc],
1.107     djm      1056:                    sprop[nenc])) != 0) {
                   1057:                        kex->failed_choice = peer[nenc];
                   1058:                        peer[nenc] = NULL;
1.102     markus   1059:                        goto out;
1.107     djm      1060:                }
1.102     markus   1061:                authlen = cipher_authlen(newkeys->enc.cipher);
1.88      markus   1062:                /* ignore mac for authenticated encryption */
1.102     markus   1063:                if (authlen == 0 &&
                   1064:                    (r = choose_mac(ssh, &newkeys->mac, cprop[nmac],
1.107     djm      1065:                    sprop[nmac])) != 0) {
                   1066:                        kex->failed_choice = peer[nmac];
                   1067:                        peer[nmac] = NULL;
1.102     markus   1068:                        goto out;
1.107     djm      1069:                }
1.102     markus   1070:                if ((r = choose_comp(&newkeys->comp, cprop[ncomp],
1.107     djm      1071:                    sprop[ncomp])) != 0) {
                   1072:                        kex->failed_choice = peer[ncomp];
                   1073:                        peer[ncomp] = NULL;
1.102     markus   1074:                        goto out;
1.107     djm      1075:                }
1.110     djm      1076:                debug("kex: %s cipher: %s MAC: %s compression: %s",
1.1       markus   1077:                    ctos ? "client->server" : "server->client",
1.27      markus   1078:                    newkeys->enc.name,
1.88      markus   1079:                    authlen == 0 ? newkeys->mac.name : "<implicit>",
1.27      markus   1080:                    newkeys->comp.name);
1.107     djm      1081:        }
1.96      dtucker  1082:        need = dh_need = 0;
1.1       markus   1083:        for (mode = 0; mode < MODE_MAX; mode++) {
1.30      markus   1084:                newkeys = kex->newkeys[mode];
1.120     deraadt  1085:                need = MAXIMUM(need, newkeys->enc.key_len);
                   1086:                need = MAXIMUM(need, newkeys->enc.block_size);
                   1087:                need = MAXIMUM(need, newkeys->enc.iv_len);
                   1088:                need = MAXIMUM(need, newkeys->mac.key_len);
                   1089:                dh_need = MAXIMUM(dh_need, cipher_seclen(newkeys->enc.cipher));
                   1090:                dh_need = MAXIMUM(dh_need, newkeys->enc.block_size);
                   1091:                dh_need = MAXIMUM(dh_need, newkeys->enc.iv_len);
                   1092:                dh_need = MAXIMUM(dh_need, newkeys->mac.key_len);
1.1       markus   1093:        }
1.7       markus   1094:        /* XXX need runden? */
1.27      markus   1095:        kex->we_need = need;
1.96      dtucker  1096:        kex->dh_need = dh_need;
1.53      markus   1097:
                   1098:        /* ignore the next message if the proposals do not match */
1.135     djm      1099:        if (first_kex_follows && !proposals_match(my, peer))
1.102     markus   1100:                ssh->dispatch_skip_packets = 1;
                   1101:        r = 0;
                   1102:  out:
1.26      markus   1103:        kex_prop_free(my);
                   1104:        kex_prop_free(peer);
1.102     markus   1105:        return r;
1.26      markus   1106: }
                   1107:
1.102     markus   1108: static int
                   1109: derive_key(struct ssh *ssh, int id, u_int need, u_char *hash, u_int hashlen,
                   1110:     const struct sshbuf *shared_secret, u_char **keyp)
1.26      markus   1111: {
1.102     markus   1112:        struct kex *kex = ssh->kex;
                   1113:        struct ssh_digest_ctx *hashctx = NULL;
1.26      markus   1114:        char c = id;
1.61      djm      1115:        u_int have;
1.94      djm      1116:        size_t mdsz;
1.61      djm      1117:        u_char *digest;
1.102     markus   1118:        int r;
1.62      djm      1119:
1.94      djm      1120:        if ((mdsz = ssh_digest_bytes(kex->hash_alg)) == 0)
1.102     markus   1121:                return SSH_ERR_INVALID_ARGUMENT;
1.120     deraadt  1122:        if ((digest = calloc(1, ROUNDUP(need, mdsz))) == NULL) {
1.102     markus   1123:                r = SSH_ERR_ALLOC_FAIL;
                   1124:                goto out;
                   1125:        }
1.26      markus   1126:
1.30      markus   1127:        /* K1 = HASH(K || H || "A" || session_id) */
1.102     markus   1128:        if ((hashctx = ssh_digest_start(kex->hash_alg)) == NULL ||
                   1129:            ssh_digest_update_buffer(hashctx, shared_secret) != 0 ||
1.94      djm      1130:            ssh_digest_update(hashctx, hash, hashlen) != 0 ||
                   1131:            ssh_digest_update(hashctx, &c, 1) != 0 ||
1.165     djm      1132:            ssh_digest_update_buffer(hashctx, kex->session_id) != 0 ||
1.102     markus   1133:            ssh_digest_final(hashctx, digest, mdsz) != 0) {
                   1134:                r = SSH_ERR_LIBCRYPTO_ERROR;
1.160     djm      1135:                error_f("KEX hash failed");
1.102     markus   1136:                goto out;
                   1137:        }
1.94      djm      1138:        ssh_digest_free(hashctx);
1.102     markus   1139:        hashctx = NULL;
1.26      markus   1140:
1.30      markus   1141:        /*
                   1142:         * expand key:
                   1143:         * Kn = HASH(K || H || K1 || K2 || ... || Kn-1)
                   1144:         * Key = K1 || K2 || ... || Kn
                   1145:         */
1.26      markus   1146:        for (have = mdsz; need > have; have += mdsz) {
1.102     markus   1147:                if ((hashctx = ssh_digest_start(kex->hash_alg)) == NULL ||
                   1148:                    ssh_digest_update_buffer(hashctx, shared_secret) != 0 ||
1.94      djm      1149:                    ssh_digest_update(hashctx, hash, hashlen) != 0 ||
1.102     markus   1150:                    ssh_digest_update(hashctx, digest, have) != 0 ||
                   1151:                    ssh_digest_final(hashctx, digest + have, mdsz) != 0) {
1.160     djm      1152:                        error_f("KDF failed");
1.102     markus   1153:                        r = SSH_ERR_LIBCRYPTO_ERROR;
                   1154:                        goto out;
                   1155:                }
1.94      djm      1156:                ssh_digest_free(hashctx);
1.102     markus   1157:                hashctx = NULL;
1.26      markus   1158:        }
                   1159: #ifdef DEBUG_KEX
                   1160:        fprintf(stderr, "key '%c'== ", c);
                   1161:        dump_digest("key", digest, need);
                   1162: #endif
1.102     markus   1163:        *keyp = digest;
                   1164:        digest = NULL;
                   1165:        r = 0;
                   1166:  out:
1.114     mmcc     1167:        free(digest);
1.102     markus   1168:        ssh_digest_free(hashctx);
                   1169:        return r;
1.1       markus   1170: }
                   1171:
1.23      markus   1172: #define NKEYS  6
1.102     markus   1173: int
                   1174: kex_derive_keys(struct ssh *ssh, u_char *hash, u_int hashlen,
                   1175:     const struct sshbuf *shared_secret)
1.1       markus   1176: {
1.102     markus   1177:        struct kex *kex = ssh->kex;
1.15      markus   1178:        u_char *keys[NKEYS];
1.102     markus   1179:        u_int i, j, mode, ctos;
                   1180:        int r;
1.1       markus   1181:
1.144     djm      1182:        /* save initial hash as session id */
1.165     djm      1183:        if ((kex->flags & KEX_INITIAL) != 0) {
1.166     djm      1184:                if (sshbuf_len(kex->session_id) != 0) {
                   1185:                        error_f("already have session ID at kex");
                   1186:                        return SSH_ERR_INTERNAL_ERROR;
                   1187:                }
1.165     djm      1188:                if ((r = sshbuf_put(kex->session_id, hash, hashlen)) != 0)
                   1189:                        return r;
                   1190:        } else if (sshbuf_len(kex->session_id) == 0) {
                   1191:                error_f("no session ID in rekex");
1.166     djm      1192:                return SSH_ERR_INTERNAL_ERROR;
1.144     djm      1193:        }
1.65      djm      1194:        for (i = 0; i < NKEYS; i++) {
1.102     markus   1195:                if ((r = derive_key(ssh, 'A'+i, kex->we_need, hash, hashlen,
                   1196:                    shared_secret, &keys[i])) != 0) {
                   1197:                        for (j = 0; j < i; j++)
                   1198:                                free(keys[j]);
                   1199:                        return r;
                   1200:                }
1.65      djm      1201:        }
1.1       markus   1202:        for (mode = 0; mode < MODE_MAX; mode++) {
1.69      deraadt  1203:                ctos = (!kex->server && mode == MODE_OUT) ||
                   1204:                    (kex->server && mode == MODE_IN);
1.100     markus   1205:                kex->newkeys[mode]->enc.iv  = keys[ctos ? 0 : 1];
                   1206:                kex->newkeys[mode]->enc.key = keys[ctos ? 2 : 3];
                   1207:                kex->newkeys[mode]->mac.key = keys[ctos ? 4 : 5];
1.1       markus   1208:        }
1.102     markus   1209:        return 0;
1.95      djm      1210: }
1.57      djm      1211:
1.145     djm      1212: int
1.150     djm      1213: kex_load_hostkey(struct ssh *ssh, struct sshkey **prvp, struct sshkey **pubp)
1.145     djm      1214: {
                   1215:        struct kex *kex = ssh->kex;
                   1216:
                   1217:        *pubp = NULL;
                   1218:        *prvp = NULL;
                   1219:        if (kex->load_host_public_key == NULL ||
1.152     djm      1220:            kex->load_host_private_key == NULL) {
1.160     djm      1221:                error_f("missing hostkey loader");
1.145     djm      1222:                return SSH_ERR_INVALID_ARGUMENT;
1.152     djm      1223:        }
1.145     djm      1224:        *pubp = kex->load_host_public_key(kex->hostkey_type,
                   1225:            kex->hostkey_nid, ssh);
                   1226:        *prvp = kex->load_host_private_key(kex->hostkey_type,
                   1227:            kex->hostkey_nid, ssh);
                   1228:        if (*pubp == NULL)
                   1229:                return SSH_ERR_NO_HOSTKEY_LOADED;
1.146     djm      1230:        return 0;
                   1231: }
                   1232:
                   1233: int
                   1234: kex_verify_host_key(struct ssh *ssh, struct sshkey *server_host_key)
                   1235: {
                   1236:        struct kex *kex = ssh->kex;
                   1237:
1.152     djm      1238:        if (kex->verify_host_key == NULL) {
1.160     djm      1239:                error_f("missing hostkey verifier");
1.146     djm      1240:                return SSH_ERR_INVALID_ARGUMENT;
1.152     djm      1241:        }
1.146     djm      1242:        if (server_host_key->type != kex->hostkey_type ||
                   1243:            (kex->hostkey_type == KEY_ECDSA &&
                   1244:            server_host_key->ecdsa_nid != kex->hostkey_nid))
                   1245:                return SSH_ERR_KEY_TYPE_MISMATCH;
                   1246:        if (kex->verify_host_key(server_host_key, ssh) == -1)
                   1247:                return  SSH_ERR_SIGNATURE_INVALID;
1.145     djm      1248:        return 0;
                   1249: }
1.26      markus   1250:
1.84      djm      1251: #if defined(DEBUG_KEX) || defined(DEBUG_KEXDH) || defined(DEBUG_KEXECDH)
1.26      markus   1252: void
1.147     djm      1253: dump_digest(const char *msg, const u_char *digest, int len)
1.26      markus   1254: {
                   1255:        fprintf(stderr, "%s\n", msg);
1.102     markus   1256:        sshbuf_dump_data(digest, len, stderr);
1.26      markus   1257: }
                   1258: #endif
1.143     djm      1259:
                   1260: /*
                   1261:  * Send a plaintext error message to the peer, suffixed by \r\n.
                   1262:  * Only used during banner exchange, and there only for the server.
                   1263:  */
                   1264: static void
                   1265: send_error(struct ssh *ssh, char *msg)
                   1266: {
                   1267:        char *crnl = "\r\n";
                   1268:
                   1269:        if (!ssh->kex->server)
                   1270:                return;
                   1271:
                   1272:        if (atomicio(vwrite, ssh_packet_get_connection_out(ssh),
                   1273:            msg, strlen(msg)) != strlen(msg) ||
                   1274:            atomicio(vwrite, ssh_packet_get_connection_out(ssh),
                   1275:            crnl, strlen(crnl)) != strlen(crnl))
1.160     djm      1276:                error_f("write: %.100s", strerror(errno));
1.143     djm      1277: }
                   1278:
                   1279: /*
                   1280:  * Sends our identification string and waits for the peer's. Will block for
                   1281:  * up to timeout_ms (or indefinitely if timeout_ms <= 0).
                   1282:  * Returns on 0 success or a ssherr.h code on failure.
                   1283:  */
                   1284: int
                   1285: kex_exchange_identification(struct ssh *ssh, int timeout_ms,
                   1286:     const char *version_addendum)
                   1287: {
1.158     djm      1288:        int remote_major, remote_minor, mismatch, oerrno = 0;
1.173     dtucker  1289:        size_t len, n;
1.143     djm      1290:        int r, expect_nl;
                   1291:        u_char c;
                   1292:        struct sshbuf *our_version = ssh->kex->server ?
                   1293:            ssh->kex->server_version : ssh->kex->client_version;
                   1294:        struct sshbuf *peer_version = ssh->kex->server ?
                   1295:            ssh->kex->client_version : ssh->kex->server_version;
                   1296:        char *our_version_string = NULL, *peer_version_string = NULL;
                   1297:        char *cp, *remote_version = NULL;
                   1298:
                   1299:        /* Prepare and send our banner */
                   1300:        sshbuf_reset(our_version);
                   1301:        if (version_addendum != NULL && *version_addendum == '\0')
                   1302:                version_addendum = NULL;
                   1303:        if ((r = sshbuf_putf(our_version, "SSH-%d.%d-%.100s%s%s\r\n",
1.168     djm      1304:            PROTOCOL_MAJOR_2, PROTOCOL_MINOR_2, SSH_VERSION,
1.143     djm      1305:            version_addendum == NULL ? "" : " ",
                   1306:            version_addendum == NULL ? "" : version_addendum)) != 0) {
1.158     djm      1307:                oerrno = errno;
1.160     djm      1308:                error_fr(r, "sshbuf_putf");
1.143     djm      1309:                goto out;
                   1310:        }
                   1311:
                   1312:        if (atomicio(vwrite, ssh_packet_get_connection_out(ssh),
                   1313:            sshbuf_mutable_ptr(our_version),
                   1314:            sshbuf_len(our_version)) != sshbuf_len(our_version)) {
1.158     djm      1315:                oerrno = errno;
1.160     djm      1316:                debug_f("write: %.100s", strerror(errno));
1.143     djm      1317:                r = SSH_ERR_SYSTEM_ERROR;
                   1318:                goto out;
                   1319:        }
                   1320:        if ((r = sshbuf_consume_end(our_version, 2)) != 0) { /* trim \r\n */
1.158     djm      1321:                oerrno = errno;
1.160     djm      1322:                error_fr(r, "sshbuf_consume_end");
1.143     djm      1323:                goto out;
                   1324:        }
                   1325:        our_version_string = sshbuf_dup_string(our_version);
                   1326:        if (our_version_string == NULL) {
1.160     djm      1327:                error_f("sshbuf_dup_string failed");
1.143     djm      1328:                r = SSH_ERR_ALLOC_FAIL;
                   1329:                goto out;
                   1330:        }
                   1331:        debug("Local version string %.100s", our_version_string);
                   1332:
                   1333:        /* Read other side's version identification. */
                   1334:        for (n = 0; ; n++) {
                   1335:                if (n >= SSH_MAX_PRE_BANNER_LINES) {
                   1336:                        send_error(ssh, "No SSH identification string "
                   1337:                            "received.");
1.160     djm      1338:                        error_f("No SSH version received in first %u lines "
                   1339:                            "from server", SSH_MAX_PRE_BANNER_LINES);
1.143     djm      1340:                        r = SSH_ERR_INVALID_FORMAT;
                   1341:                        goto out;
                   1342:                }
                   1343:                sshbuf_reset(peer_version);
                   1344:                expect_nl = 0;
1.173     dtucker  1345:                for (;;) {
1.143     djm      1346:                        if (timeout_ms > 0) {
                   1347:                                r = waitrfd(ssh_packet_get_connection_in(ssh),
                   1348:                                    &timeout_ms);
                   1349:                                if (r == -1 && errno == ETIMEDOUT) {
                   1350:                                        send_error(ssh, "Timed out waiting "
                   1351:                                            "for SSH identification string.");
                   1352:                                        error("Connection timed out during "
                   1353:                                            "banner exchange");
                   1354:                                        r = SSH_ERR_CONN_TIMEOUT;
                   1355:                                        goto out;
                   1356:                                } else if (r == -1) {
1.158     djm      1357:                                        oerrno = errno;
1.160     djm      1358:                                        error_f("%s", strerror(errno));
1.143     djm      1359:                                        r = SSH_ERR_SYSTEM_ERROR;
                   1360:                                        goto out;
                   1361:                                }
                   1362:                        }
                   1363:
                   1364:                        len = atomicio(read, ssh_packet_get_connection_in(ssh),
                   1365:                            &c, 1);
                   1366:                        if (len != 1 && errno == EPIPE) {
1.160     djm      1367:                                error_f("Connection closed by remote host");
1.143     djm      1368:                                r = SSH_ERR_CONN_CLOSED;
                   1369:                                goto out;
                   1370:                        } else if (len != 1) {
1.158     djm      1371:                                oerrno = errno;
1.160     djm      1372:                                error_f("read: %.100s", strerror(errno));
1.143     djm      1373:                                r = SSH_ERR_SYSTEM_ERROR;
                   1374:                                goto out;
                   1375:                        }
                   1376:                        if (c == '\r') {
                   1377:                                expect_nl = 1;
                   1378:                                continue;
                   1379:                        }
                   1380:                        if (c == '\n')
                   1381:                                break;
                   1382:                        if (c == '\0' || expect_nl) {
1.160     djm      1383:                                error_f("banner line contains invalid "
                   1384:                                    "characters");
1.143     djm      1385:                                goto invalid;
                   1386:                        }
                   1387:                        if ((r = sshbuf_put_u8(peer_version, c)) != 0) {
1.158     djm      1388:                                oerrno = errno;
1.160     djm      1389:                                error_fr(r, "sshbuf_put");
1.143     djm      1390:                                goto out;
                   1391:                        }
                   1392:                        if (sshbuf_len(peer_version) > SSH_MAX_BANNER_LEN) {
1.160     djm      1393:                                error_f("banner line too long");
1.143     djm      1394:                                goto invalid;
                   1395:                        }
                   1396:                }
                   1397:                /* Is this an actual protocol banner? */
                   1398:                if (sshbuf_len(peer_version) > 4 &&
                   1399:                    memcmp(sshbuf_ptr(peer_version), "SSH-", 4) == 0)
                   1400:                        break;
                   1401:                /* If not, then just log the line and continue */
                   1402:                if ((cp = sshbuf_dup_string(peer_version)) == NULL) {
1.160     djm      1403:                        error_f("sshbuf_dup_string failed");
1.143     djm      1404:                        r = SSH_ERR_ALLOC_FAIL;
                   1405:                        goto out;
                   1406:                }
                   1407:                /* Do not accept lines before the SSH ident from a client */
                   1408:                if (ssh->kex->server) {
1.160     djm      1409:                        error_f("client sent invalid protocol identifier "
                   1410:                            "\"%.256s\"", cp);
1.143     djm      1411:                        free(cp);
                   1412:                        goto invalid;
                   1413:                }
1.160     djm      1414:                debug_f("banner line %zu: %s", n, cp);
1.143     djm      1415:                free(cp);
                   1416:        }
                   1417:        peer_version_string = sshbuf_dup_string(peer_version);
                   1418:        if (peer_version_string == NULL)
1.175     dtucker  1419:                fatal_f("sshbuf_dup_string failed");
1.143     djm      1420:        /* XXX must be same size for sscanf */
                   1421:        if ((remote_version = calloc(1, sshbuf_len(peer_version))) == NULL) {
1.160     djm      1422:                error_f("calloc failed");
1.143     djm      1423:                r = SSH_ERR_ALLOC_FAIL;
                   1424:                goto out;
                   1425:        }
                   1426:
                   1427:        /*
                   1428:         * Check that the versions match.  In future this might accept
                   1429:         * several versions and set appropriate flags to handle them.
                   1430:         */
                   1431:        if (sscanf(peer_version_string, "SSH-%d.%d-%[^\n]\n",
                   1432:            &remote_major, &remote_minor, remote_version) != 3) {
                   1433:                error("Bad remote protocol version identification: '%.100s'",
                   1434:                    peer_version_string);
                   1435:  invalid:
                   1436:                send_error(ssh, "Invalid SSH identification string.");
                   1437:                r = SSH_ERR_INVALID_FORMAT;
                   1438:                goto out;
                   1439:        }
                   1440:        debug("Remote protocol version %d.%d, remote software version %.100s",
                   1441:            remote_major, remote_minor, remote_version);
1.164     djm      1442:        compat_banner(ssh, remote_version);
1.143     djm      1443:
                   1444:        mismatch = 0;
                   1445:        switch (remote_major) {
                   1446:        case 2:
                   1447:                break;
                   1448:        case 1:
                   1449:                if (remote_minor != 99)
                   1450:                        mismatch = 1;
                   1451:                break;
                   1452:        default:
                   1453:                mismatch = 1;
                   1454:                break;
                   1455:        }
                   1456:        if (mismatch) {
                   1457:                error("Protocol major versions differ: %d vs. %d",
                   1458:                    PROTOCOL_MAJOR_2, remote_major);
                   1459:                send_error(ssh, "Protocol major versions differ.");
                   1460:                r = SSH_ERR_NO_PROTOCOL_VERSION;
                   1461:                goto out;
                   1462:        }
                   1463:
                   1464:        if (ssh->kex->server && (ssh->compat & SSH_BUG_PROBE) != 0) {
                   1465:                logit("probed from %s port %d with %s.  Don't panic.",
                   1466:                    ssh_remote_ipaddr(ssh), ssh_remote_port(ssh),
                   1467:                    peer_version_string);
                   1468:                r = SSH_ERR_CONN_CLOSED; /* XXX */
                   1469:                goto out;
                   1470:        }
                   1471:        if (ssh->kex->server && (ssh->compat & SSH_BUG_SCANNER) != 0) {
                   1472:                logit("scanned from %s port %d with %s.  Don't panic.",
                   1473:                    ssh_remote_ipaddr(ssh), ssh_remote_port(ssh),
                   1474:                    peer_version_string);
                   1475:                r = SSH_ERR_CONN_CLOSED; /* XXX */
                   1476:                goto out;
                   1477:        }
                   1478:        /* success */
                   1479:        r = 0;
                   1480:  out:
                   1481:        free(our_version_string);
                   1482:        free(peer_version_string);
                   1483:        free(remote_version);
1.158     djm      1484:        if (r == SSH_ERR_SYSTEM_ERROR)
                   1485:                errno = oerrno;
1.143     djm      1486:        return r;
                   1487: }
                   1488: