[BACK]Return to ssh_api.c CVS log [TXT][DIR] Up to [local] / src / usr.bin / ssh

Annotation of src/usr.bin/ssh/ssh_api.c, Revision 1.6

1.6     ! djm         1: /* $OpenBSD: ssh_api.c,v 1.5 2015/12/04 16:41:28 markus Exp $ */
1.1       markus      2: /*
                      3:  * Copyright (c) 2012 Markus Friedl.  All rights reserved.
                      4:  *
                      5:  * Permission to use, copy, modify, and distribute this software for any
                      6:  * purpose with or without fee is hereby granted, provided that the above
                      7:  * copyright notice and this permission notice appear in all copies.
                      8:  *
                      9:  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
                     10:  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
                     11:  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
                     12:  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
                     13:  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
                     14:  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
                     15:  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
                     16:  */
                     17:
                     18: #include "ssh1.h" /* For SSH_MSG_NONE */
                     19: #include "ssh_api.h"
                     20: #include "compat.h"
                     21: #include "log.h"
                     22: #include "authfile.h"
                     23: #include "sshkey.h"
                     24: #include "misc.h"
                     25: #include "ssh1.h"
                     26: #include "ssh2.h"
                     27: #include "version.h"
                     28: #include "myproposal.h"
                     29: #include "ssherr.h"
                     30: #include "sshbuf.h"
                     31:
                     32: #include <string.h>
                     33:
                     34: int    _ssh_exchange_banner(struct ssh *);
                     35: int    _ssh_send_banner(struct ssh *, char **);
                     36: int    _ssh_read_banner(struct ssh *, char **);
                     37: int    _ssh_order_hostkeyalgs(struct ssh *);
                     38: int    _ssh_verify_host_key(struct sshkey *, struct ssh *);
1.2       djm        39: struct sshkey *_ssh_host_public_key(int, int, struct ssh *);
                     40: struct sshkey *_ssh_host_private_key(int, int, struct ssh *);
1.5       markus     41: int    _ssh_host_key_sign(struct sshkey *, struct sshkey *,
                     42:     u_char **, size_t *, const u_char *, size_t, const char *, u_int);
1.1       markus     43:
                     44: /*
                     45:  * stubs for the server side implementation of kex.
                     46:  * disable privsep so our stubs will never be called.
                     47:  */
                     48: int    use_privsep = 0;
                     49: int    mm_sshkey_sign(struct sshkey *, u_char **, u_int *,
1.5       markus     50:     u_char *, u_int, char *, u_int);
1.1       markus     51: DH     *mm_choose_dh(int, int, int);
                     52:
                     53: /* Define these two variables here so that they are part of the library */
                     54: u_char *session_id2 = NULL;
                     55: u_int session_id2_len = 0;
                     56:
                     57: int
                     58: mm_sshkey_sign(struct sshkey *key, u_char **sigp, u_int *lenp,
1.5       markus     59:     u_char *data, u_int datalen, char *alg, u_int compat)
1.1       markus     60: {
                     61:        return (-1);
                     62: }
                     63:
                     64: DH *
                     65: mm_choose_dh(int min, int nbits, int max)
                     66: {
                     67:        return (NULL);
                     68: }
                     69:
                     70: /* API */
                     71:
                     72: int
                     73: ssh_init(struct ssh **sshp, int is_server, struct kex_params *kex_params)
                     74: {
                     75:         char *myproposal[PROPOSAL_MAX] = { KEX_CLIENT };
                     76:        struct ssh *ssh;
                     77:        char **proposal;
                     78:        static int called;
                     79:        int r;
                     80:
                     81:        if (!called) {
                     82:                OpenSSL_add_all_algorithms();
                     83:                called = 1;
                     84:        }
                     85:
1.3       djm        86:        if ((ssh = ssh_packet_set_connection(NULL, -1, -1)) == NULL)
                     87:                return SSH_ERR_ALLOC_FAIL;
1.1       markus     88:        if (is_server)
                     89:                ssh_packet_set_server(ssh);
                     90:
                     91:        /* Initialize key exchange */
                     92:        proposal = kex_params ? kex_params->proposal : myproposal;
                     93:        if ((r = kex_new(ssh, proposal, &ssh->kex)) != 0) {
                     94:                ssh_free(ssh);
                     95:                return r;
                     96:        }
                     97:        ssh->kex->server = is_server;
                     98:        if (is_server) {
                     99: #ifdef WITH_OPENSSL
                    100:                ssh->kex->kex[KEX_DH_GRP1_SHA1] = kexdh_server;
                    101:                ssh->kex->kex[KEX_DH_GRP14_SHA1] = kexdh_server;
1.6     ! djm       102:                ssh->kex->kex[KEX_DH_GRP14_SHA256] = kexdh_server;
        !           103:                ssh->kex->kex[KEX_DH_GRP16_SHA512] = kexdh_server;
        !           104:                ssh->kex->kex[KEX_DH_GRP18_SHA512] = kexdh_server;
1.1       markus    105:                ssh->kex->kex[KEX_DH_GEX_SHA1] = kexgex_server;
                    106:                ssh->kex->kex[KEX_DH_GEX_SHA256] = kexgex_server;
                    107:                ssh->kex->kex[KEX_ECDH_SHA2] = kexecdh_server;
                    108: #endif /* WITH_OPENSSL */
                    109:                ssh->kex->kex[KEX_C25519_SHA256] = kexc25519_server;
                    110:                ssh->kex->load_host_public_key=&_ssh_host_public_key;
                    111:                ssh->kex->load_host_private_key=&_ssh_host_private_key;
                    112:                ssh->kex->sign=&_ssh_host_key_sign;
                    113:        } else {
                    114: #ifdef WITH_OPENSSL
                    115:                ssh->kex->kex[KEX_DH_GRP1_SHA1] = kexdh_client;
                    116:                ssh->kex->kex[KEX_DH_GRP14_SHA1] = kexdh_client;
1.6     ! djm       117:                ssh->kex->kex[KEX_DH_GRP14_SHA256] = kexdh_client;
        !           118:                ssh->kex->kex[KEX_DH_GRP16_SHA512] = kexdh_client;
        !           119:                ssh->kex->kex[KEX_DH_GRP18_SHA512] = kexdh_client;
1.1       markus    120:                ssh->kex->kex[KEX_DH_GEX_SHA1] = kexgex_client;
                    121:                ssh->kex->kex[KEX_DH_GEX_SHA256] = kexgex_client;
                    122:                ssh->kex->kex[KEX_ECDH_SHA2] = kexecdh_client;
                    123: #endif /* WITH_OPENSSL */
                    124:                ssh->kex->kex[KEX_C25519_SHA256] = kexc25519_client;
                    125:                ssh->kex->verify_host_key =&_ssh_verify_host_key;
                    126:        }
                    127:        *sshp = ssh;
                    128:        return 0;
                    129: }
                    130:
                    131: void
                    132: ssh_free(struct ssh *ssh)
                    133: {
                    134:        struct key_entry *k;
                    135:
                    136:        ssh_packet_close(ssh);
                    137:        /*
                    138:         * we've only created the public keys variants in case we
                    139:         * are a acting as a server.
                    140:         */
                    141:        while ((k = TAILQ_FIRST(&ssh->public_keys)) != NULL) {
                    142:                TAILQ_REMOVE(&ssh->public_keys, k, next);
                    143:                if (ssh->kex && ssh->kex->server)
                    144:                        sshkey_free(k->key);
                    145:                free(k);
                    146:        }
                    147:        while ((k = TAILQ_FIRST(&ssh->private_keys)) != NULL) {
                    148:                TAILQ_REMOVE(&ssh->private_keys, k, next);
                    149:                free(k);
                    150:        }
                    151:        if (ssh->kex)
                    152:                kex_free(ssh->kex);
                    153:        free(ssh);
                    154: }
                    155:
                    156: void
                    157: ssh_set_app_data(struct ssh *ssh, void *app_data)
                    158: {
                    159:        ssh->app_data = app_data;
                    160: }
                    161:
                    162: void *
                    163: ssh_get_app_data(struct ssh *ssh)
                    164: {
                    165:        return ssh->app_data;
                    166: }
                    167:
                    168: /* Returns < 0 on error, 0 otherwise */
                    169: int
                    170: ssh_add_hostkey(struct ssh *ssh, struct sshkey *key)
                    171: {
                    172:        struct sshkey *pubkey = NULL;
                    173:        struct key_entry *k = NULL, *k_prv = NULL;
                    174:        int r;
                    175:
                    176:        if (ssh->kex->server) {
                    177:                if ((r = sshkey_from_private(key, &pubkey)) != 0)
                    178:                        return r;
                    179:                if ((k = malloc(sizeof(*k))) == NULL ||
                    180:                    (k_prv = malloc(sizeof(*k_prv))) == NULL) {
                    181:                        free(k);
                    182:                        sshkey_free(pubkey);
                    183:                        return SSH_ERR_ALLOC_FAIL;
                    184:                }
                    185:                k_prv->key = key;
                    186:                TAILQ_INSERT_TAIL(&ssh->private_keys, k_prv, next);
                    187:
                    188:                /* add the public key, too */
                    189:                k->key = pubkey;
                    190:                TAILQ_INSERT_TAIL(&ssh->public_keys, k, next);
                    191:                r = 0;
                    192:        } else {
                    193:                if ((k = malloc(sizeof(*k))) == NULL)
                    194:                        return SSH_ERR_ALLOC_FAIL;
                    195:                k->key = key;
                    196:                TAILQ_INSERT_TAIL(&ssh->public_keys, k, next);
                    197:                r = 0;
                    198:        }
                    199:
                    200:        return r;
                    201: }
                    202:
                    203: int
                    204: ssh_set_verify_host_key_callback(struct ssh *ssh,
                    205:     int (*cb)(struct sshkey *, struct ssh *))
                    206: {
                    207:        if (cb == NULL || ssh->kex == NULL)
                    208:                return SSH_ERR_INVALID_ARGUMENT;
                    209:
                    210:        ssh->kex->verify_host_key = cb;
                    211:
                    212:        return 0;
                    213: }
                    214:
                    215: int
                    216: ssh_input_append(struct ssh *ssh, const u_char *data, size_t len)
                    217: {
                    218:        return sshbuf_put(ssh_packet_get_input(ssh), data, len);
                    219: }
                    220:
                    221: int
                    222: ssh_packet_next(struct ssh *ssh, u_char *typep)
                    223: {
                    224:        int r;
                    225:        u_int32_t seqnr;
                    226:        u_char type;
                    227:
                    228:        /*
                    229:         * Try to read a packet. Return SSH_MSG_NONE if no packet or not
                    230:         * enough data.
                    231:         */
                    232:        *typep = SSH_MSG_NONE;
                    233:        if (ssh->kex->client_version_string == NULL ||
                    234:            ssh->kex->server_version_string == NULL)
                    235:                return _ssh_exchange_banner(ssh);
                    236:        /*
                    237:         * If we enough data and a dispatch function then
                    238:         * call the function and get the next packet.
                    239:         * Otherwise return the packet type to the caller so it
                    240:         * can decide how to go on.
                    241:         *
                    242:         * We will only call the dispatch function for:
                    243:         *     20-29    Algorithm negotiation
                    244:         *     30-49    Key exchange method specific (numbers can be reused for
                    245:         *              different authentication methods)
                    246:         */
                    247:        for (;;) {
                    248:                if ((r = ssh_packet_read_poll2(ssh, &type, &seqnr)) != 0)
                    249:                        return r;
                    250:                if (type > 0 && type < DISPATCH_MAX &&
                    251:                    type >= SSH2_MSG_KEXINIT && type <= SSH2_MSG_TRANSPORT_MAX &&
                    252:                    ssh->dispatch[type] != NULL) {
                    253:                        if ((r = (*ssh->dispatch[type])(type, seqnr, ssh)) != 0)
                    254:                                return r;
                    255:                } else {
                    256:                        *typep = type;
                    257:                        return 0;
                    258:                }
                    259:        }
                    260: }
                    261:
                    262: const u_char *
                    263: ssh_packet_payload(struct ssh *ssh, size_t *lenp)
                    264: {
                    265:        return sshpkt_ptr(ssh, lenp);
                    266: }
                    267:
                    268: int
                    269: ssh_packet_put(struct ssh *ssh, int type, const u_char *data, size_t len)
                    270: {
                    271:        int r;
                    272:
                    273:        if ((r = sshpkt_start(ssh, type)) != 0 ||
                    274:            (r = sshpkt_put(ssh, data, len)) != 0 ||
                    275:            (r = sshpkt_send(ssh)) != 0)
                    276:                return r;
                    277:        return 0;
                    278: }
                    279:
                    280: const u_char *
                    281: ssh_output_ptr(struct ssh *ssh, size_t *len)
                    282: {
                    283:        struct sshbuf *output = ssh_packet_get_output(ssh);
                    284:
                    285:        *len = sshbuf_len(output);
                    286:        return sshbuf_ptr(output);
                    287: }
                    288:
                    289: int
                    290: ssh_output_consume(struct ssh *ssh, size_t len)
                    291: {
                    292:        return sshbuf_consume(ssh_packet_get_output(ssh), len);
                    293: }
                    294:
                    295: int
                    296: ssh_output_space(struct ssh *ssh, size_t len)
                    297: {
                    298:        return (0 == sshbuf_check_reserve(ssh_packet_get_output(ssh), len));
                    299: }
                    300:
                    301: int
                    302: ssh_input_space(struct ssh *ssh, size_t len)
                    303: {
                    304:        return (0 == sshbuf_check_reserve(ssh_packet_get_input(ssh), len));
                    305: }
                    306:
                    307: /* Read other side's version identification. */
                    308: int
                    309: _ssh_read_banner(struct ssh *ssh, char **bannerp)
                    310: {
                    311:        struct sshbuf *input;
                    312:        const char *s;
                    313:        char buf[256], remote_version[256];     /* must be same size! */
                    314:        const char *mismatch = "Protocol mismatch.\r\n";
                    315:        int r, remote_major, remote_minor;
                    316:        size_t i, n, j, len;
                    317:
                    318:        *bannerp = NULL;
                    319:        input = ssh_packet_get_input(ssh);
                    320:        len = sshbuf_len(input);
                    321:        s = (const char *)sshbuf_ptr(input);
                    322:        for (j = n = 0;;) {
                    323:                for (i = 0; i < sizeof(buf) - 1; i++) {
                    324:                        if (j >= len)
                    325:                                return (0);
                    326:                        buf[i] = s[j++];
                    327:                        if (buf[i] == '\r') {
                    328:                                buf[i] = '\n';
                    329:                                buf[i + 1] = 0;
                    330:                                continue;               /**XXX wait for \n */
                    331:                        }
                    332:                        if (buf[i] == '\n') {
                    333:                                buf[i + 1] = 0;
                    334:                                break;
                    335:                        }
                    336:                }
                    337:                buf[sizeof(buf) - 1] = 0;
                    338:                if (strncmp(buf, "SSH-", 4) == 0)
                    339:                        break;
                    340:                debug("ssh_exchange_identification: %s", buf);
                    341:                if (ssh->kex->server || ++n > 65536) {
                    342:                        if ((r = sshbuf_put(ssh_packet_get_output(ssh),
                    343:                           mismatch, strlen(mismatch))) != 0)
                    344:                                return r;
                    345:                        return SSH_ERR_NO_PROTOCOL_VERSION;
                    346:                }
                    347:        }
                    348:        if ((r = sshbuf_consume(input, j)) != 0)
                    349:                return r;
                    350:
                    351:        /*
                    352:         * Check that the versions match.  In future this might accept
                    353:         * several versions and set appropriate flags to handle them.
                    354:         */
                    355:        if (sscanf(buf, "SSH-%d.%d-%[^\n]\n",
                    356:            &remote_major, &remote_minor, remote_version) != 3)
                    357:                return SSH_ERR_INVALID_FORMAT;
                    358:        debug("Remote protocol version %d.%d, remote software version %.100s",
                    359:            remote_major, remote_minor, remote_version);
                    360:
                    361:        ssh->compat = compat_datafellows(remote_version);
                    362:        if  (remote_major == 1 && remote_minor == 99) {
                    363:                remote_major = 2;
                    364:                remote_minor = 0;
                    365:        }
                    366:        if (remote_major != 2)
                    367:                return SSH_ERR_PROTOCOL_MISMATCH;
                    368:        enable_compat20();
                    369:        chop(buf);
                    370:        debug("Remote version string %.100s", buf);
                    371:        if ((*bannerp = strdup(buf)) == NULL)
                    372:                return SSH_ERR_ALLOC_FAIL;
                    373:        return 0;
                    374: }
                    375:
                    376: /* Send our own protocol version identification. */
                    377: int
                    378: _ssh_send_banner(struct ssh *ssh, char **bannerp)
                    379: {
                    380:        char buf[256];
                    381:        int r;
                    382:
                    383:        snprintf(buf, sizeof buf, "SSH-2.0-%.100s\r\n", SSH_VERSION);
                    384:        if ((r = sshbuf_put(ssh_packet_get_output(ssh), buf, strlen(buf))) != 0)
                    385:                return r;
                    386:        chop(buf);
                    387:        debug("Local version string %.100s", buf);
                    388:        if ((*bannerp = strdup(buf)) == NULL)
                    389:                return SSH_ERR_ALLOC_FAIL;
                    390:        return 0;
                    391: }
                    392:
                    393: int
                    394: _ssh_exchange_banner(struct ssh *ssh)
                    395: {
                    396:        struct kex *kex = ssh->kex;
                    397:        int r;
                    398:
                    399:        /*
                    400:         * if _ssh_read_banner() cannot parse a full version string
                    401:         * it will return NULL and we end up calling it again.
                    402:         */
                    403:
                    404:        r = 0;
                    405:        if (kex->server) {
                    406:                if (kex->server_version_string == NULL)
                    407:                        r = _ssh_send_banner(ssh, &kex->server_version_string);
                    408:                if (r == 0 &&
                    409:                    kex->server_version_string != NULL &&
                    410:                    kex->client_version_string == NULL)
                    411:                        r = _ssh_read_banner(ssh, &kex->client_version_string);
                    412:        } else {
                    413:                if (kex->server_version_string == NULL)
                    414:                        r = _ssh_read_banner(ssh, &kex->server_version_string);
                    415:                if (r == 0 &&
                    416:                    kex->server_version_string != NULL &&
                    417:                    kex->client_version_string == NULL)
                    418:                        r = _ssh_send_banner(ssh, &kex->client_version_string);
                    419:        }
                    420:        if (r != 0)
                    421:                return r;
                    422:        /* start initial kex as soon as we have exchanged the banners */
                    423:        if (kex->server_version_string != NULL &&
                    424:            kex->client_version_string != NULL) {
                    425:                if ((r = _ssh_order_hostkeyalgs(ssh)) != 0 ||
                    426:                    (r = kex_send_kexinit(ssh)) != 0)
                    427:                        return r;
                    428:        }
                    429:        return 0;
                    430: }
                    431:
                    432: struct sshkey *
1.2       djm       433: _ssh_host_public_key(int type, int nid, struct ssh *ssh)
1.1       markus    434: {
                    435:        struct key_entry *k;
                    436:
                    437:        debug3("%s: need %d", __func__, type);
                    438:        TAILQ_FOREACH(k, &ssh->public_keys, next) {
                    439:                debug3("%s: check %s", __func__, sshkey_type(k->key));
1.2       djm       440:                if (k->key->type == type &&
                    441:                    (type != KEY_ECDSA || k->key->ecdsa_nid == nid))
1.1       markus    442:                        return (k->key);
                    443:        }
                    444:        return (NULL);
                    445: }
                    446:
                    447: struct sshkey *
1.2       djm       448: _ssh_host_private_key(int type, int nid, struct ssh *ssh)
1.1       markus    449: {
                    450:        struct key_entry *k;
                    451:
                    452:        debug3("%s: need %d", __func__, type);
                    453:        TAILQ_FOREACH(k, &ssh->private_keys, next) {
                    454:                debug3("%s: check %s", __func__, sshkey_type(k->key));
1.2       djm       455:                if (k->key->type == type &&
                    456:                    (type != KEY_ECDSA || k->key->ecdsa_nid == nid))
1.1       markus    457:                        return (k->key);
                    458:        }
                    459:        return (NULL);
                    460: }
                    461:
                    462: int
                    463: _ssh_verify_host_key(struct sshkey *hostkey, struct ssh *ssh)
                    464: {
                    465:        struct key_entry *k;
                    466:
                    467:        debug3("%s: need %s", __func__, sshkey_type(hostkey));
                    468:        TAILQ_FOREACH(k, &ssh->public_keys, next) {
                    469:                debug3("%s: check %s", __func__, sshkey_type(k->key));
                    470:                if (sshkey_equal_public(hostkey, k->key))
                    471:                        return (0);     /* ok */
                    472:        }
                    473:        return (-1);    /* failed */
                    474: }
                    475:
                    476: /* offer hostkey algorithms in kexinit depending on registered keys */
                    477: int
                    478: _ssh_order_hostkeyalgs(struct ssh *ssh)
                    479: {
                    480:        struct key_entry *k;
                    481:        char *orig, *avail, *oavail = NULL, *alg, *replace = NULL;
                    482:        char **proposal;
                    483:        size_t maxlen;
                    484:        int ktype, r;
                    485:
                    486:        /* XXX we de-serialize ssh->kex->my, modify it, and change it */
                    487:        if ((r = kex_buf2prop(ssh->kex->my, NULL, &proposal)) != 0)
                    488:                return r;
                    489:        orig = proposal[PROPOSAL_SERVER_HOST_KEY_ALGS];
                    490:        if ((oavail = avail = strdup(orig)) == NULL) {
                    491:                r = SSH_ERR_ALLOC_FAIL;
                    492:                goto out;
                    493:        }
                    494:        maxlen = strlen(avail) + 1;
                    495:        if ((replace = calloc(1, maxlen)) == NULL) {
                    496:                r = SSH_ERR_ALLOC_FAIL;
                    497:                goto out;
                    498:        }
                    499:        *replace = '\0';
                    500:        while ((alg = strsep(&avail, ",")) && *alg != '\0') {
                    501:                if ((ktype = sshkey_type_from_name(alg)) == KEY_UNSPEC)
                    502:                        continue;
                    503:                TAILQ_FOREACH(k, &ssh->public_keys, next) {
                    504:                        if (k->key->type == ktype ||
                    505:                            (sshkey_is_cert(k->key) && k->key->type ==
                    506:                            sshkey_type_plain(ktype))) {
                    507:                                if (*replace != '\0')
                    508:                                        strlcat(replace, ",", maxlen);
                    509:                                strlcat(replace, alg, maxlen);
                    510:                                break;
                    511:                        }
                    512:                }
                    513:        }
                    514:        if (*replace != '\0') {
                    515:                debug2("%s: orig/%d    %s", __func__, ssh->kex->server, orig);
                    516:                debug2("%s: replace/%d %s", __func__, ssh->kex->server, replace);
                    517:                free(orig);
                    518:                proposal[PROPOSAL_SERVER_HOST_KEY_ALGS] = replace;
                    519:                replace = NULL; /* owned by proposal */
                    520:                r = kex_prop2buf(ssh->kex->my, proposal);
                    521:        }
                    522:  out:
                    523:        free(oavail);
                    524:        free(replace);
                    525:        kex_prop_free(proposal);
                    526:        return r;
                    527: }
                    528:
                    529: int
                    530: _ssh_host_key_sign(struct sshkey *privkey, struct sshkey *pubkey,
1.5       markus    531:     u_char **signature, size_t *slen, const u_char *data, size_t dlen,
                    532:     const char *alg, u_int compat)
1.1       markus    533: {
1.5       markus    534:        return sshkey_sign(privkey, signature, slen, data, dlen, alg, compat);
1.1       markus    535: }