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

1.28    ! djm         1: /* $OpenBSD: ssh_api.c,v 1.27 2021/04/03 06:18:41 djm 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:  */
1.16      djm        17:
                     18: #include <sys/types.h>
                     19:
                     20: #include <stdio.h>
                     21: #include <stdlib.h>
1.1       markus     22:
                     23: #include "ssh_api.h"
                     24: #include "compat.h"
                     25: #include "log.h"
                     26: #include "authfile.h"
                     27: #include "sshkey.h"
                     28: #include "misc.h"
                     29: #include "ssh2.h"
                     30: #include "version.h"
                     31: #include "myproposal.h"
                     32: #include "ssherr.h"
                     33: #include "sshbuf.h"
                     34:
                     35: #include <string.h>
                     36:
                     37: int    _ssh_exchange_banner(struct ssh *);
1.9       djm        38: int    _ssh_send_banner(struct ssh *, struct sshbuf *);
                     39: int    _ssh_read_banner(struct ssh *, struct sshbuf *);
1.1       markus     40: int    _ssh_order_hostkeyalgs(struct ssh *);
                     41: int    _ssh_verify_host_key(struct sshkey *, struct ssh *);
1.2       djm        42: struct sshkey *_ssh_host_public_key(int, int, struct ssh *);
                     43: struct sshkey *_ssh_host_private_key(int, int, struct ssh *);
1.10      djm        44: int    _ssh_host_key_sign(struct ssh *, struct sshkey *, struct sshkey *,
                     45:     u_char **, size_t *, const u_char *, size_t, const char *);
1.1       markus     46:
                     47: /*
                     48:  * stubs for the server side implementation of kex.
                     49:  * disable privsep so our stubs will never be called.
                     50:  */
                     51: int    use_privsep = 0;
                     52: int    mm_sshkey_sign(struct sshkey *, u_char **, u_int *,
1.21      djm        53:     const u_char *, u_int, const char *, const char *, const char *, u_int);
1.17      djm        54:
                     55: #ifdef WITH_OPENSSL
1.1       markus     56: DH     *mm_choose_dh(int, int, int);
1.17      djm        57: #endif
1.1       markus     58:
                     59: int
                     60: mm_sshkey_sign(struct sshkey *key, u_char **sigp, u_int *lenp,
1.21      djm        61:     const u_char *data, u_int datalen, const char *alg,
                     62:     const char *sk_provider, const char *sk_pin, u_int compat)
1.1       markus     63: {
                     64:        return (-1);
                     65: }
                     66:
1.17      djm        67: #ifdef WITH_OPENSSL
1.1       markus     68: DH *
                     69: mm_choose_dh(int min, int nbits, int max)
                     70: {
                     71:        return (NULL);
                     72: }
1.17      djm        73: #endif
1.1       markus     74:
                     75: /* API */
                     76:
                     77: int
                     78: ssh_init(struct ssh **sshp, int is_server, struct kex_params *kex_params)
                     79: {
1.27      djm        80:        char *myproposal[PROPOSAL_MAX] = { KEX_CLIENT };
1.28    ! djm        81:        char *populated[PROPOSAL_MAX];
1.1       markus     82:        struct ssh *ssh;
                     83:        char **proposal;
                     84:        static int called;
                     85:        int r;
                     86:
                     87:        if (!called) {
1.17      djm        88: #ifdef WITH_OPENSSL
1.1       markus     89:                OpenSSL_add_all_algorithms();
1.17      djm        90: #endif
1.1       markus     91:                called = 1;
                     92:        }
                     93:
1.3       djm        94:        if ((ssh = ssh_packet_set_connection(NULL, -1, -1)) == NULL)
                     95:                return SSH_ERR_ALLOC_FAIL;
1.1       markus     96:        if (is_server)
                     97:                ssh_packet_set_server(ssh);
                     98:
                     99:        /* Initialize key exchange */
                    100:        proposal = kex_params ? kex_params->proposal : myproposal;
1.28    ! djm       101:        kex_proposal_populate_entries(ssh, populated,
        !           102:            proposal[PROPOSAL_KEX_ALGS],
        !           103:            proposal[PROPOSAL_ENC_ALGS_CTOS],
        !           104:            proposal[PROPOSAL_MAC_ALGS_CTOS],
        !           105:            proposal[PROPOSAL_COMP_ALGS_CTOS],
        !           106:            proposal[PROPOSAL_SERVER_HOST_KEY_ALGS]);
        !           107:        r = kex_ready(ssh, populated);
        !           108:        kex_proposal_free_entries(populated);
        !           109:        if (r != 0) {
1.1       markus    110:                ssh_free(ssh);
                    111:                return r;
                    112:        }
1.28    ! djm       113:
1.1       markus    114:        ssh->kex->server = is_server;
                    115:        if (is_server) {
                    116: #ifdef WITH_OPENSSL
1.15      djm       117:                ssh->kex->kex[KEX_DH_GRP1_SHA1] = kex_gen_server;
                    118:                ssh->kex->kex[KEX_DH_GRP14_SHA1] = kex_gen_server;
                    119:                ssh->kex->kex[KEX_DH_GRP14_SHA256] = kex_gen_server;
                    120:                ssh->kex->kex[KEX_DH_GRP16_SHA512] = kex_gen_server;
                    121:                ssh->kex->kex[KEX_DH_GRP18_SHA512] = kex_gen_server;
1.1       markus    122:                ssh->kex->kex[KEX_DH_GEX_SHA1] = kexgex_server;
                    123:                ssh->kex->kex[KEX_DH_GEX_SHA256] = kexgex_server;
1.15      djm       124:                ssh->kex->kex[KEX_ECDH_SHA2] = kex_gen_server;
1.1       markus    125: #endif /* WITH_OPENSSL */
1.15      djm       126:                ssh->kex->kex[KEX_C25519_SHA256] = kex_gen_server;
1.24      djm       127:                ssh->kex->kex[KEX_KEM_SNTRUP761X25519_SHA512] = kex_gen_server;
1.1       markus    128:                ssh->kex->load_host_public_key=&_ssh_host_public_key;
                    129:                ssh->kex->load_host_private_key=&_ssh_host_private_key;
                    130:                ssh->kex->sign=&_ssh_host_key_sign;
                    131:        } else {
                    132: #ifdef WITH_OPENSSL
1.15      djm       133:                ssh->kex->kex[KEX_DH_GRP1_SHA1] = kex_gen_client;
                    134:                ssh->kex->kex[KEX_DH_GRP14_SHA1] = kex_gen_client;
                    135:                ssh->kex->kex[KEX_DH_GRP14_SHA256] = kex_gen_client;
                    136:                ssh->kex->kex[KEX_DH_GRP16_SHA512] = kex_gen_client;
                    137:                ssh->kex->kex[KEX_DH_GRP18_SHA512] = kex_gen_client;
1.1       markus    138:                ssh->kex->kex[KEX_DH_GEX_SHA1] = kexgex_client;
                    139:                ssh->kex->kex[KEX_DH_GEX_SHA256] = kexgex_client;
1.15      djm       140:                ssh->kex->kex[KEX_ECDH_SHA2] = kex_gen_client;
1.1       markus    141: #endif /* WITH_OPENSSL */
1.15      djm       142:                ssh->kex->kex[KEX_C25519_SHA256] = kex_gen_client;
1.24      djm       143:                ssh->kex->kex[KEX_KEM_SNTRUP761X25519_SHA512] = kex_gen_client;
1.1       markus    144:                ssh->kex->verify_host_key =&_ssh_verify_host_key;
                    145:        }
                    146:        *sshp = ssh;
                    147:        return 0;
                    148: }
                    149:
                    150: void
                    151: ssh_free(struct ssh *ssh)
                    152: {
                    153:        struct key_entry *k;
1.23      djm       154:
                    155:        if (ssh == NULL)
                    156:                return;
1.1       markus    157:
                    158:        /*
                    159:         * we've only created the public keys variants in case we
                    160:         * are a acting as a server.
                    161:         */
                    162:        while ((k = TAILQ_FIRST(&ssh->public_keys)) != NULL) {
                    163:                TAILQ_REMOVE(&ssh->public_keys, k, next);
                    164:                if (ssh->kex && ssh->kex->server)
                    165:                        sshkey_free(k->key);
                    166:                free(k);
                    167:        }
                    168:        while ((k = TAILQ_FIRST(&ssh->private_keys)) != NULL) {
                    169:                TAILQ_REMOVE(&ssh->private_keys, k, next);
                    170:                free(k);
                    171:        }
1.20      markus    172:        ssh_packet_close(ssh);
1.1       markus    173:        free(ssh);
                    174: }
                    175:
                    176: void
                    177: ssh_set_app_data(struct ssh *ssh, void *app_data)
                    178: {
                    179:        ssh->app_data = app_data;
                    180: }
                    181:
                    182: void *
                    183: ssh_get_app_data(struct ssh *ssh)
                    184: {
                    185:        return ssh->app_data;
                    186: }
                    187:
                    188: /* Returns < 0 on error, 0 otherwise */
                    189: int
                    190: ssh_add_hostkey(struct ssh *ssh, struct sshkey *key)
                    191: {
                    192:        struct sshkey *pubkey = NULL;
                    193:        struct key_entry *k = NULL, *k_prv = NULL;
                    194:        int r;
                    195:
                    196:        if (ssh->kex->server) {
                    197:                if ((r = sshkey_from_private(key, &pubkey)) != 0)
                    198:                        return r;
                    199:                if ((k = malloc(sizeof(*k))) == NULL ||
                    200:                    (k_prv = malloc(sizeof(*k_prv))) == NULL) {
                    201:                        free(k);
                    202:                        sshkey_free(pubkey);
                    203:                        return SSH_ERR_ALLOC_FAIL;
                    204:                }
                    205:                k_prv->key = key;
                    206:                TAILQ_INSERT_TAIL(&ssh->private_keys, k_prv, next);
                    207:
                    208:                /* add the public key, too */
                    209:                k->key = pubkey;
                    210:                TAILQ_INSERT_TAIL(&ssh->public_keys, k, next);
                    211:                r = 0;
                    212:        } else {
                    213:                if ((k = malloc(sizeof(*k))) == NULL)
                    214:                        return SSH_ERR_ALLOC_FAIL;
                    215:                k->key = key;
                    216:                TAILQ_INSERT_TAIL(&ssh->public_keys, k, next);
                    217:                r = 0;
                    218:        }
                    219:
                    220:        return r;
                    221: }
                    222:
                    223: int
                    224: ssh_set_verify_host_key_callback(struct ssh *ssh,
                    225:     int (*cb)(struct sshkey *, struct ssh *))
                    226: {
                    227:        if (cb == NULL || ssh->kex == NULL)
                    228:                return SSH_ERR_INVALID_ARGUMENT;
                    229:
                    230:        ssh->kex->verify_host_key = cb;
                    231:
                    232:        return 0;
                    233: }
                    234:
                    235: int
                    236: ssh_input_append(struct ssh *ssh, const u_char *data, size_t len)
                    237: {
                    238:        return sshbuf_put(ssh_packet_get_input(ssh), data, len);
                    239: }
                    240:
                    241: int
                    242: ssh_packet_next(struct ssh *ssh, u_char *typep)
                    243: {
                    244:        int r;
                    245:        u_int32_t seqnr;
                    246:        u_char type;
                    247:
                    248:        /*
                    249:         * Try to read a packet. Return SSH_MSG_NONE if no packet or not
                    250:         * enough data.
                    251:         */
                    252:        *typep = SSH_MSG_NONE;
1.9       djm       253:        if (sshbuf_len(ssh->kex->client_version) == 0 ||
                    254:            sshbuf_len(ssh->kex->server_version) == 0)
1.1       markus    255:                return _ssh_exchange_banner(ssh);
                    256:        /*
                    257:         * If we enough data and a dispatch function then
                    258:         * call the function and get the next packet.
                    259:         * Otherwise return the packet type to the caller so it
                    260:         * can decide how to go on.
                    261:         *
                    262:         * We will only call the dispatch function for:
                    263:         *     20-29    Algorithm negotiation
                    264:         *     30-49    Key exchange method specific (numbers can be reused for
                    265:         *              different authentication methods)
                    266:         */
                    267:        for (;;) {
                    268:                if ((r = ssh_packet_read_poll2(ssh, &type, &seqnr)) != 0)
                    269:                        return r;
                    270:                if (type > 0 && type < DISPATCH_MAX &&
                    271:                    type >= SSH2_MSG_KEXINIT && type <= SSH2_MSG_TRANSPORT_MAX &&
                    272:                    ssh->dispatch[type] != NULL) {
                    273:                        if ((r = (*ssh->dispatch[type])(type, seqnr, ssh)) != 0)
                    274:                                return r;
                    275:                } else {
                    276:                        *typep = type;
                    277:                        return 0;
                    278:                }
                    279:        }
                    280: }
                    281:
                    282: const u_char *
                    283: ssh_packet_payload(struct ssh *ssh, size_t *lenp)
                    284: {
                    285:        return sshpkt_ptr(ssh, lenp);
                    286: }
                    287:
                    288: int
                    289: ssh_packet_put(struct ssh *ssh, int type, const u_char *data, size_t len)
                    290: {
                    291:        int r;
                    292:
                    293:        if ((r = sshpkt_start(ssh, type)) != 0 ||
                    294:            (r = sshpkt_put(ssh, data, len)) != 0 ||
                    295:            (r = sshpkt_send(ssh)) != 0)
                    296:                return r;
                    297:        return 0;
                    298: }
                    299:
                    300: const u_char *
                    301: ssh_output_ptr(struct ssh *ssh, size_t *len)
                    302: {
                    303:        struct sshbuf *output = ssh_packet_get_output(ssh);
                    304:
                    305:        *len = sshbuf_len(output);
                    306:        return sshbuf_ptr(output);
                    307: }
                    308:
                    309: int
                    310: ssh_output_consume(struct ssh *ssh, size_t len)
                    311: {
                    312:        return sshbuf_consume(ssh_packet_get_output(ssh), len);
                    313: }
                    314:
                    315: int
                    316: ssh_output_space(struct ssh *ssh, size_t len)
                    317: {
                    318:        return (0 == sshbuf_check_reserve(ssh_packet_get_output(ssh), len));
                    319: }
                    320:
                    321: int
                    322: ssh_input_space(struct ssh *ssh, size_t len)
                    323: {
                    324:        return (0 == sshbuf_check_reserve(ssh_packet_get_input(ssh), len));
                    325: }
                    326:
                    327: /* Read other side's version identification. */
                    328: int
1.9       djm       329: _ssh_read_banner(struct ssh *ssh, struct sshbuf *banner)
1.1       markus    330: {
1.9       djm       331:        struct sshbuf *input = ssh_packet_get_input(ssh);
1.1       markus    332:        const char *mismatch = "Protocol mismatch.\r\n";
1.9       djm       333:        const u_char *s = sshbuf_ptr(input);
                    334:        u_char c;
1.18      dtucker   335:        char *cp = NULL, *remote_version = NULL;
                    336:        int r = 0, remote_major, remote_minor, expect_nl;
1.9       djm       337:        size_t n, j;
1.1       markus    338:
                    339:        for (j = n = 0;;) {
1.9       djm       340:                sshbuf_reset(banner);
                    341:                expect_nl = 0;
                    342:                for (;;) {
                    343:                        if (j >= sshbuf_len(input))
                    344:                                return 0; /* insufficient data in input buf */
                    345:                        c = s[j++];
                    346:                        if (c == '\r') {
                    347:                                expect_nl = 1;
                    348:                                continue;
1.1       markus    349:                        }
1.9       djm       350:                        if (c == '\n')
1.1       markus    351:                                break;
1.9       djm       352:                        if (expect_nl)
                    353:                                goto bad;
                    354:                        if ((r = sshbuf_put_u8(banner, c)) != 0)
                    355:                                return r;
                    356:                        if (sshbuf_len(banner) > SSH_MAX_BANNER_LEN)
                    357:                                goto bad;
1.1       markus    358:                }
1.9       djm       359:                if (sshbuf_len(banner) >= 4 &&
                    360:                    memcmp(sshbuf_ptr(banner), "SSH-", 4) == 0)
1.1       markus    361:                        break;
1.22      djm       362:                debug_f("%.*s", (int)sshbuf_len(banner),
1.18      dtucker   363:                    sshbuf_ptr(banner));
1.9       djm       364:                /* Accept lines before banner only on client */
                    365:                if (ssh->kex->server || ++n > SSH_MAX_PRE_BANNER_LINES) {
                    366:   bad:
1.1       markus    367:                        if ((r = sshbuf_put(ssh_packet_get_output(ssh),
1.27      djm       368:                            mismatch, strlen(mismatch))) != 0)
1.1       markus    369:                                return r;
                    370:                        return SSH_ERR_NO_PROTOCOL_VERSION;
                    371:                }
                    372:        }
                    373:        if ((r = sshbuf_consume(input, j)) != 0)
                    374:                return r;
                    375:
1.9       djm       376:        /* XXX remote version must be the same size as banner for sscanf */
1.18      dtucker   377:        if ((cp = sshbuf_dup_string(banner)) == NULL ||
                    378:            (remote_version = calloc(1, sshbuf_len(banner))) == NULL) {
                    379:                r = SSH_ERR_ALLOC_FAIL;
                    380:                goto out;
                    381:        }
1.9       djm       382:
1.1       markus    383:        /*
                    384:         * Check that the versions match.  In future this might accept
                    385:         * several versions and set appropriate flags to handle them.
                    386:         */
1.9       djm       387:        if (sscanf(cp, "SSH-%d.%d-%[^\n]\n",
1.18      dtucker   388:            &remote_major, &remote_minor, remote_version) != 3) {
                    389:                r = SSH_ERR_INVALID_FORMAT;
                    390:                goto out;
                    391:        }
1.1       markus    392:        debug("Remote protocol version %d.%d, remote software version %.100s",
                    393:            remote_major, remote_minor, remote_version);
                    394:
1.25      djm       395:        compat_banner(ssh, remote_version);
1.1       markus    396:        if  (remote_major == 1 && remote_minor == 99) {
                    397:                remote_major = 2;
                    398:                remote_minor = 0;
                    399:        }
                    400:        if (remote_major != 2)
1.18      dtucker   401:                r = SSH_ERR_PROTOCOL_MISMATCH;
                    402:
1.9       djm       403:        debug("Remote version string %.100s", cp);
1.18      dtucker   404:  out:
1.9       djm       405:        free(cp);
1.18      dtucker   406:        free(remote_version);
                    407:        return r;
1.1       markus    408: }
                    409:
                    410: /* Send our own protocol version identification. */
                    411: int
1.9       djm       412: _ssh_send_banner(struct ssh *ssh, struct sshbuf *banner)
1.1       markus    413: {
1.9       djm       414:        char *cp;
1.1       markus    415:        int r;
                    416:
1.9       djm       417:        if ((r = sshbuf_putf(banner, "SSH-2.0-%.100s\r\n", SSH_VERSION)) != 0)
                    418:                return r;
                    419:        if ((r = sshbuf_putb(ssh_packet_get_output(ssh), banner)) != 0)
                    420:                return r;
                    421:        /* Remove trailing \r\n */
                    422:        if ((r = sshbuf_consume_end(banner, 2)) != 0)
1.1       markus    423:                return r;
1.9       djm       424:        if ((cp = sshbuf_dup_string(banner)) == NULL)
1.1       markus    425:                return SSH_ERR_ALLOC_FAIL;
1.9       djm       426:        debug("Local version string %.100s", cp);
                    427:        free(cp);
1.1       markus    428:        return 0;
                    429: }
                    430:
                    431: int
                    432: _ssh_exchange_banner(struct ssh *ssh)
                    433: {
                    434:        struct kex *kex = ssh->kex;
                    435:        int r;
                    436:
                    437:        /*
                    438:         * if _ssh_read_banner() cannot parse a full version string
                    439:         * it will return NULL and we end up calling it again.
                    440:         */
                    441:
                    442:        r = 0;
                    443:        if (kex->server) {
1.9       djm       444:                if (sshbuf_len(ssh->kex->server_version) == 0)
                    445:                        r = _ssh_send_banner(ssh, ssh->kex->server_version);
1.1       markus    446:                if (r == 0 &&
1.9       djm       447:                    sshbuf_len(ssh->kex->server_version) != 0 &&
                    448:                    sshbuf_len(ssh->kex->client_version) == 0)
                    449:                        r = _ssh_read_banner(ssh, ssh->kex->client_version);
1.1       markus    450:        } else {
1.9       djm       451:                if (sshbuf_len(ssh->kex->server_version) == 0)
                    452:                        r = _ssh_read_banner(ssh, ssh->kex->server_version);
1.1       markus    453:                if (r == 0 &&
1.9       djm       454:                    sshbuf_len(ssh->kex->server_version) != 0 &&
                    455:                    sshbuf_len(ssh->kex->client_version) == 0)
                    456:                        r = _ssh_send_banner(ssh, ssh->kex->client_version);
1.1       markus    457:        }
                    458:        if (r != 0)
                    459:                return r;
                    460:        /* start initial kex as soon as we have exchanged the banners */
1.9       djm       461:        if (sshbuf_len(ssh->kex->server_version) != 0 &&
                    462:            sshbuf_len(ssh->kex->client_version) != 0) {
1.1       markus    463:                if ((r = _ssh_order_hostkeyalgs(ssh)) != 0 ||
                    464:                    (r = kex_send_kexinit(ssh)) != 0)
                    465:                        return r;
                    466:        }
                    467:        return 0;
                    468: }
                    469:
                    470: struct sshkey *
1.2       djm       471: _ssh_host_public_key(int type, int nid, struct ssh *ssh)
1.1       markus    472: {
                    473:        struct key_entry *k;
                    474:
1.22      djm       475:        debug3_f("need %d", type);
1.1       markus    476:        TAILQ_FOREACH(k, &ssh->public_keys, next) {
1.22      djm       477:                debug3_f("check %s", sshkey_type(k->key));
1.2       djm       478:                if (k->key->type == type &&
                    479:                    (type != KEY_ECDSA || k->key->ecdsa_nid == nid))
1.1       markus    480:                        return (k->key);
                    481:        }
                    482:        return (NULL);
                    483: }
                    484:
                    485: struct sshkey *
1.2       djm       486: _ssh_host_private_key(int type, int nid, struct ssh *ssh)
1.1       markus    487: {
                    488:        struct key_entry *k;
                    489:
1.22      djm       490:        debug3_f("need %d", type);
1.1       markus    491:        TAILQ_FOREACH(k, &ssh->private_keys, next) {
1.22      djm       492:                debug3_f("check %s", sshkey_type(k->key));
1.2       djm       493:                if (k->key->type == type &&
                    494:                    (type != KEY_ECDSA || k->key->ecdsa_nid == nid))
1.1       markus    495:                        return (k->key);
                    496:        }
                    497:        return (NULL);
                    498: }
                    499:
                    500: int
                    501: _ssh_verify_host_key(struct sshkey *hostkey, struct ssh *ssh)
                    502: {
                    503:        struct key_entry *k;
                    504:
1.22      djm       505:        debug3_f("need %s", sshkey_type(hostkey));
1.1       markus    506:        TAILQ_FOREACH(k, &ssh->public_keys, next) {
1.22      djm       507:                debug3_f("check %s", sshkey_type(k->key));
1.1       markus    508:                if (sshkey_equal_public(hostkey, k->key))
                    509:                        return (0);     /* ok */
                    510:        }
                    511:        return (-1);    /* failed */
                    512: }
                    513:
                    514: /* offer hostkey algorithms in kexinit depending on registered keys */
                    515: int
                    516: _ssh_order_hostkeyalgs(struct ssh *ssh)
                    517: {
                    518:        struct key_entry *k;
                    519:        char *orig, *avail, *oavail = NULL, *alg, *replace = NULL;
                    520:        char **proposal;
                    521:        size_t maxlen;
                    522:        int ktype, r;
                    523:
                    524:        /* XXX we de-serialize ssh->kex->my, modify it, and change it */
                    525:        if ((r = kex_buf2prop(ssh->kex->my, NULL, &proposal)) != 0)
                    526:                return r;
                    527:        orig = proposal[PROPOSAL_SERVER_HOST_KEY_ALGS];
                    528:        if ((oavail = avail = strdup(orig)) == NULL) {
                    529:                r = SSH_ERR_ALLOC_FAIL;
                    530:                goto out;
                    531:        }
                    532:        maxlen = strlen(avail) + 1;
                    533:        if ((replace = calloc(1, maxlen)) == NULL) {
                    534:                r = SSH_ERR_ALLOC_FAIL;
                    535:                goto out;
                    536:        }
                    537:        *replace = '\0';
                    538:        while ((alg = strsep(&avail, ",")) && *alg != '\0') {
                    539:                if ((ktype = sshkey_type_from_name(alg)) == KEY_UNSPEC)
                    540:                        continue;
                    541:                TAILQ_FOREACH(k, &ssh->public_keys, next) {
                    542:                        if (k->key->type == ktype ||
                    543:                            (sshkey_is_cert(k->key) && k->key->type ==
                    544:                            sshkey_type_plain(ktype))) {
                    545:                                if (*replace != '\0')
                    546:                                        strlcat(replace, ",", maxlen);
                    547:                                strlcat(replace, alg, maxlen);
                    548:                                break;
                    549:                        }
                    550:                }
                    551:        }
                    552:        if (*replace != '\0') {
1.22      djm       553:                debug2_f("orig/%d    %s", ssh->kex->server, orig);
                    554:                debug2_f("replace/%d %s", ssh->kex->server, replace);
1.1       markus    555:                free(orig);
                    556:                proposal[PROPOSAL_SERVER_HOST_KEY_ALGS] = replace;
                    557:                replace = NULL; /* owned by proposal */
                    558:                r = kex_prop2buf(ssh->kex->my, proposal);
                    559:        }
                    560:  out:
                    561:        free(oavail);
                    562:        free(replace);
                    563:        kex_prop_free(proposal);
                    564:        return r;
                    565: }
                    566:
                    567: int
1.10      djm       568: _ssh_host_key_sign(struct ssh *ssh, struct sshkey *privkey,
                    569:     struct sshkey *pubkey, u_char **signature, size_t *slen,
                    570:     const u_char *data, size_t dlen, const char *alg)
1.1       markus    571: {
1.10      djm       572:        return sshkey_sign(privkey, signature, slen, data, dlen,
1.21      djm       573:            alg, NULL, NULL, ssh->compat);
1.1       markus    574: }