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

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