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

Annotation of src/usr.bin/ssh/sshconnect2.c, Revision 1.365

1.365   ! guenther    1: /* $OpenBSD: sshconnect2.c,v 1.364 2023/03/06 12:14:48 dtucker Exp $ */
1.1       markus      2: /*
                      3:  * Copyright (c) 2000 Markus Friedl.  All rights reserved.
1.170     djm         4:  * Copyright (c) 2008 Damien Miller.  All rights reserved.
1.1       markus      5:  *
                      6:  * Redistribution and use in source and binary forms, with or without
                      7:  * modification, are permitted provided that the following conditions
                      8:  * are met:
                      9:  * 1. Redistributions of source code must retain the above copyright
                     10:  *    notice, this list of conditions and the following disclaimer.
                     11:  * 2. Redistributions in binary form must reproduce the above copyright
                     12:  *    notice, this list of conditions and the following disclaimer in the
                     13:  *    documentation and/or other materials provided with the distribution.
                     14:  *
                     15:  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
                     16:  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
                     17:  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
                     18:  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
                     19:  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
                     20:  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
                     21:  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
                     22:  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
                     23:  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
                     24:  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
                     25:  */
                     26:
1.145     stevesk    27: #include <sys/types.h>
1.160     deraadt    28: #include <sys/socket.h>
1.145     stevesk    29: #include <sys/wait.h>
1.144     stevesk    30: #include <sys/queue.h>
1.146     stevesk    31: #include <sys/stat.h>
1.156     stevesk    32:
                     33: #include <errno.h>
1.174     dtucker    34: #include <fcntl.h>
1.336     djm        35: #include <limits.h>
1.164     jolan      36: #include <netdb.h>
1.159     stevesk    37: #include <stdio.h>
1.158     stevesk    38: #include <string.h>
1.313     deraadt    39: #include <stdarg.h>
1.160     deraadt    40: #include <signal.h>
                     41: #include <pwd.h>
1.157     stevesk    42: #include <unistd.h>
1.166     djm        43: #include <vis.h>
1.1       markus     44:
1.160     deraadt    45: #include "xmalloc.h"
1.1       markus     46: #include "ssh.h"
1.37      markus     47: #include "ssh2.h"
1.278     markus     48: #include "sshbuf.h"
1.1       markus     49: #include "packet.h"
                     50: #include "compat.h"
1.37      markus     51: #include "cipher.h"
1.278     markus     52: #include "sshkey.h"
1.1       markus     53: #include "kex.h"
                     54: #include "sshconnect.h"
                     55: #include "authfile.h"
1.57      provos     56: #include "dh.h"
1.17      markus     57: #include "authfd.h"
1.37      markus     58: #include "log.h"
1.210     millert    59: #include "misc.h"
1.37      markus     60: #include "readconf.h"
1.53      markus     61: #include "match.h"
1.61      markus     62: #include "dispatch.h"
1.68      markus     63: #include "canohost.h"
1.100     markus     64: #include "msg.h"
                     65: #include "pathnames.h"
1.154     markus     66: #include "uidswap.h"
1.186     djm        67: #include "hostfile.h"
1.214     djm        68: #include "ssherr.h"
1.246     djm        69: #include "utf8.h"
1.309     djm        70: #include "ssh-sk.h"
1.312     djm        71: #include "sk-api.h"
1.22      provos     72:
1.121     markus     73: #ifdef GSSAPI
                     74: #include "ssh-gss.h"
                     75: #endif
1.293     djm        76:
1.1       markus     77: /* import */
                     78: extern char *client_version_string;
                     79: extern char *server_version_string;
                     80: extern Options options;
                     81:
                     82: /*
                     83:  * SSH2 key exchange
                     84:  */
                     85:
1.338     djm        86: static char *xxx_host;
                     87: static struct sockaddr *xxx_hostaddr;
                     88: static const struct ssh_conn_info *xxx_conn_info;
1.61      markus     89:
1.76      itojun     90: static int
1.259     markus     91: verify_host_key_callback(struct sshkey *hostkey, struct ssh *ssh)
1.61      markus     92: {
1.361     djm        93:        int r;
                     94:
                     95:        if ((r = sshkey_check_rsa_length(hostkey,
                     96:            options.required_rsa_size)) != 0)
                     97:                fatal_r(r, "Bad server host key");
1.338     djm        98:        if (verify_host_key(xxx_host, xxx_hostaddr, hostkey,
                     99:            xxx_conn_info) == -1)
1.83      markus    100:                fatal("Host key verification failed.");
1.61      markus    101:        return 0;
                    102: }
                    103:
1.326     djm       104: /* Returns the first item from a comma-separated algorithm list */
                    105: static char *
                    106: first_alg(const char *algs)
                    107: {
                    108:        char *ret, *cp;
                    109:
                    110:        ret = xstrdup(algs);
                    111:        if ((cp = strchr(ret, ',')) != NULL)
                    112:                *cp = '\0';
                    113:        return ret;
                    114: }
                    115:
1.186     djm       116: static char *
1.338     djm       117: order_hostkeyalgs(char *host, struct sockaddr *hostaddr, u_short port,
                    118:     const struct ssh_conn_info *cinfo)
1.186     djm       119: {
1.326     djm       120:        char *oavail = NULL, *avail = NULL, *first = NULL, *last = NULL;
                    121:        char *alg = NULL, *hostname = NULL, *ret = NULL, *best = NULL;
1.186     djm       122:        size_t maxlen;
1.326     djm       123:        struct hostkeys *hostkeys = NULL;
1.186     djm       124:        int ktype;
1.188     djm       125:        u_int i;
1.186     djm       126:
                    127:        /* Find all hostkeys for this hostname */
                    128:        get_hostfile_hostname_ipaddr(host, hostaddr, port, &hostname, NULL);
                    129:        hostkeys = init_hostkeys();
1.188     djm       130:        for (i = 0; i < options.num_user_hostfiles; i++)
1.337     djm       131:                load_hostkeys(hostkeys, hostname, options.user_hostfiles[i], 0);
                    132:        for (i = 0; i < options.num_system_hostfiles; i++) {
                    133:                load_hostkeys(hostkeys, hostname,
                    134:                    options.system_hostfiles[i], 0);
                    135:        }
1.339     djm       136:        if (options.known_hosts_command != NULL) {
                    137:                load_hostkeys_command(hostkeys, options.known_hosts_command,
                    138:                    "ORDER", cinfo, NULL, host);
                    139:        }
1.326     djm       140:        /*
                    141:         * If a plain public key exists that matches the type of the best
                    142:         * preference HostkeyAlgorithms, then use the whole list as is.
                    143:         * Note that we ignore whether the best preference algorithm is a
                    144:         * certificate type, as sshconnect.c will downgrade certs to
                    145:         * plain keys if necessary.
                    146:         */
                    147:        best = first_alg(options.hostkeyalgorithms);
                    148:        if (lookup_key_in_hostkeys_by_type(hostkeys,
1.328     djm       149:            sshkey_type_plain(sshkey_type_from_name(best)),
                    150:            sshkey_ecdsa_nid_from_name(best), NULL)) {
1.331     djm       151:                debug3_f("have matching best-preference key type %s, "
                    152:                    "using HostkeyAlgorithms verbatim", best);
1.326     djm       153:                ret = xstrdup(options.hostkeyalgorithms);
                    154:                goto out;
                    155:        }
                    156:
                    157:        /*
                    158:         * Otherwise, prefer the host key algorithms that match known keys
                    159:         * while keeping the ordering of HostkeyAlgorithms as much as possible.
                    160:         */
1.320     djm       161:        oavail = avail = xstrdup(options.hostkeyalgorithms);
1.186     djm       162:        maxlen = strlen(avail) + 1;
                    163:        first = xmalloc(maxlen);
                    164:        last = xmalloc(maxlen);
                    165:        *first = *last = '\0';
                    166:
                    167: #define ALG_APPEND(to, from) \
                    168:        do { \
                    169:                if (*to != '\0') \
                    170:                        strlcat(to, ",", maxlen); \
                    171:                strlcat(to, from, maxlen); \
                    172:        } while (0)
                    173:
                    174:        while ((alg = strsep(&avail, ",")) && *alg != '\0') {
1.214     djm       175:                if ((ktype = sshkey_type_from_name(alg)) == KEY_UNSPEC)
1.331     djm       176:                        fatal_f("unknown alg %s", alg);
1.322     djm       177:                /*
                    178:                 * If we have a @cert-authority marker in known_hosts then
                    179:                 * prefer all certificate algorithms.
                    180:                 */
                    181:                if (sshkey_type_is_cert(ktype) &&
                    182:                    lookup_marker_in_hostkeys(hostkeys, MRK_CA)) {
                    183:                        ALG_APPEND(first, alg);
                    184:                        continue;
                    185:                }
                    186:                /* If the key appears in known_hosts then prefer it */
1.186     djm       187:                if (lookup_key_in_hostkeys_by_type(hostkeys,
1.328     djm       188:                    sshkey_type_plain(ktype),
                    189:                    sshkey_ecdsa_nid_from_name(alg), NULL)) {
1.186     djm       190:                        ALG_APPEND(first, alg);
1.322     djm       191:                        continue;
                    192:                }
                    193:                /* Otherwise, put it last */
                    194:                ALG_APPEND(last, alg);
1.186     djm       195:        }
                    196: #undef ALG_APPEND
1.216     djm       197:        xasprintf(&ret, "%s%s%s", first,
                    198:            (*first == '\0' || *last == '\0') ? "" : ",", last);
1.186     djm       199:        if (*first != '\0')
1.331     djm       200:                debug3_f("prefer hostkeyalgs: %s", first);
1.339     djm       201:        else
                    202:                debug3_f("no algorithms matched; accept original");
1.326     djm       203:  out:
                    204:        free(best);
1.197     djm       205:        free(first);
                    206:        free(last);
                    207:        free(hostname);
                    208:        free(oavail);
1.186     djm       209:        free_hostkeys(hostkeys);
                    210:
                    211:        return ret;
                    212: }
                    213:
1.1       markus    214: void
1.338     djm       215: ssh_kex2(struct ssh *ssh, char *host, struct sockaddr *hostaddr, u_short port,
                    216:     const struct ssh_conn_info *cinfo)
1.22      provos    217: {
1.364     dtucker   218:        char *myproposal[PROPOSAL_MAX];
                    219:        char *s, *all_key, *hkalgs = NULL;
                    220:        int r;
1.61      markus    221:
                    222:        xxx_host = host;
                    223:        xxx_hostaddr = hostaddr;
1.338     djm       224:        xxx_conn_info = cinfo;
1.22      provos    225:
1.364     dtucker   226:        if (options.rekey_limit || options.rekey_interval)
                    227:                ssh_packet_set_rekey_limits(ssh, options.rekey_limit,
                    228:                    options.rekey_interval);
1.320     djm       229:
                    230:        /* Expand or fill in HostkeyAlgorithms */
                    231:        all_key = sshkey_alg_list(0, 0, 1, ',');
1.333     djm       232:        if ((r = kex_assemble_names(&options.hostkeyalgorithms,
                    233:            kex_default_pk_alg(), all_key)) != 0)
                    234:                fatal_fr(r, "kex_assemble_namelist");
1.320     djm       235:        free(all_key);
                    236:
1.231     markus    237:        if ((s = kex_names_cat(options.kex_algorithms, "ext-info-c")) == NULL)
1.331     djm       238:                fatal_f("kex_names_cat");
1.115     markus    239:
1.364     dtucker   240:        /*
                    241:         * If the user has not specified HostkeyAlgorithms, or has only
                    242:         * appended or removed algorithms from that list then prefer algorithms
                    243:         * that are in the list that are supported by known_hosts keys.
                    244:         */
                    245:        if (options.hostkeyalgorithms == NULL ||
                    246:            options.hostkeyalgorithms[0] == '-' ||
                    247:            options.hostkeyalgorithms[0] == '+')
                    248:                hkalgs = order_hostkeyalgs(host, hostaddr, port, cinfo);
                    249:
                    250:        kex_proposal_populate_entries(ssh, myproposal, s, options.ciphers,
                    251:            options.macs, compression_alg_list(options.compression),
                    252:            hkalgs ? hkalgs : options.hostkeyalgorithms);
                    253:
                    254:        free(hkalgs);
1.22      provos    255:
1.65      markus    256:        /* start key exchange */
1.291     djm       257:        if ((r = kex_setup(ssh, myproposal)) != 0)
1.331     djm       258:                fatal_r(r, "kex_setup");
1.207     markus    259: #ifdef WITH_OPENSSL
1.301     djm       260:        ssh->kex->kex[KEX_DH_GRP1_SHA1] = kex_gen_client;
                    261:        ssh->kex->kex[KEX_DH_GRP14_SHA1] = kex_gen_client;
                    262:        ssh->kex->kex[KEX_DH_GRP14_SHA256] = kex_gen_client;
                    263:        ssh->kex->kex[KEX_DH_GRP16_SHA512] = kex_gen_client;
                    264:        ssh->kex->kex[KEX_DH_GRP18_SHA512] = kex_gen_client;
1.291     djm       265:        ssh->kex->kex[KEX_DH_GEX_SHA1] = kexgex_client;
                    266:        ssh->kex->kex[KEX_DH_GEX_SHA256] = kexgex_client;
1.301     djm       267:        ssh->kex->kex[KEX_ECDH_SHA2] = kex_gen_client;
1.207     markus    268: #endif
1.301     djm       269:        ssh->kex->kex[KEX_C25519_SHA256] = kex_gen_client;
1.340     djm       270:        ssh->kex->kex[KEX_KEM_SNTRUP761X25519_SHA512] = kex_gen_client;
1.291     djm       271:        ssh->kex->verify_host_key=&verify_host_key_callback;
1.22      provos    272:
1.291     djm       273:        ssh_dispatch_run_fatal(ssh, DISPATCH_BLOCK, &ssh->kex->done);
1.173     andreas   274:
1.231     markus    275:        /* remove ext-info from the KEX proposals for rekeying */
1.364     dtucker   276:        free(myproposal[PROPOSAL_KEX_ALGS]);
1.231     markus    277:        myproposal[PROPOSAL_KEX_ALGS] =
1.345     djm       278:            compat_kex_proposal(ssh, options.kex_algorithms);
1.291     djm       279:        if ((r = kex_prop2buf(ssh->kex->my, myproposal)) != 0)
1.331     djm       280:                fatal_r(r, "kex_prop2buf");
1.62      markus    281:
1.22      provos    282: #ifdef DEBUG_KEXDH
                    283:        /* send 1st encrypted/maced/compressed message */
1.278     markus    284:        if ((r = sshpkt_start(ssh, SSH2_MSG_IGNORE)) != 0 ||
                    285:            (r = sshpkt_put_cstring(ssh, "markus")) != 0 ||
1.280     markus    286:            (r = sshpkt_send(ssh)) != 0 ||
                    287:            (r = ssh_packet_write_wait(ssh)) != 0)
1.331     djm       288:                fatal_fr(r, "send packet");
1.22      provos    289: #endif
1.364     dtucker   290:        kex_proposal_free_entries(myproposal);
1.1       markus    291: }
1.11      markus    292:
1.1       markus    293: /*
                    294:  * Authenticate user
                    295:  */
1.20      markus    296:
1.214     djm       297: typedef struct cauthctxt Authctxt;
                    298: typedef struct cauthmethod Authmethod;
1.117     markus    299: typedef struct identity Identity;
                    300: typedef struct idlist Idlist;
1.20      markus    301:
1.117     markus    302: struct identity {
                    303:        TAILQ_ENTRY(identity) next;
1.214     djm       304:        int     agent_fd;               /* >=0 if agent supports key */
                    305:        struct sshkey   *key;           /* public/private key */
1.117     markus    306:        char    *filename;              /* comment for agent-only keys */
                    307:        int     tried;
                    308:        int     isprivate;              /* key points to the private key */
1.191     dtucker   309:        int     userprovided;
1.117     markus    310: };
                    311: TAILQ_HEAD(idlist, identity);
1.20      markus    312:
1.214     djm       313: struct cauthctxt {
1.20      markus    314:        const char *server_user;
1.68      markus    315:        const char *local_user;
1.20      markus    316:        const char *host;
                    317:        const char *service;
1.214     djm       318:        struct cauthmethod *method;
1.183     djm       319:        sig_atomic_t success;
1.51      markus    320:        char *authlist;
1.302     djm       321: #ifdef GSSAPI
                    322:        /* gssapi */
                    323:        gss_OID_set gss_supported_mechs;
                    324:        u_int mech_tried;
                    325: #endif
1.68      markus    326:        /* pubkey */
1.214     djm       327:        struct idlist keys;
                    328:        int agent_fd;
1.68      markus    329:        /* hostbased */
1.100     markus    330:        Sensitive *sensitive;
1.223     djm       331:        char *oktypes, *ktypes;
                    332:        const char *active_ktype;
1.82      markus    333:        /* kbd-interactive */
                    334:        int info_req_seen;
1.292     djm       335:        int attempt_kbdint;
                    336:        /* password */
                    337:        int attempt_passwd;
1.121     markus    338:        /* generic */
                    339:        void *methoddata;
1.20      markus    340: };
1.214     djm       341:
                    342: struct cauthmethod {
1.20      markus    343:        char    *name;          /* string to compare against server's list */
1.295     djm       344:        int     (*userauth)(struct ssh *ssh);
                    345:        void    (*cleanup)(struct ssh *ssh);
1.20      markus    346:        int     *enabled;       /* flag in option struct that enables method */
                    347:        int     *batch_flag;    /* flag in option struct that disables method */
                    348: };
                    349:
1.302     djm       350: static int input_userauth_service_accept(int, u_int32_t, struct ssh *);
                    351: static int input_userauth_ext_info(int, u_int32_t, struct ssh *);
                    352: static int input_userauth_success(int, u_int32_t, struct ssh *);
                    353: static int input_userauth_failure(int, u_int32_t, struct ssh *);
                    354: static int input_userauth_banner(int, u_int32_t, struct ssh *);
                    355: static int input_userauth_error(int, u_int32_t, struct ssh *);
                    356: static int input_userauth_info_req(int, u_int32_t, struct ssh *);
                    357: static int input_userauth_pk_ok(int, u_int32_t, struct ssh *);
                    358: static int input_userauth_passwd_changereq(int, u_int32_t, struct ssh *);
                    359:
                    360: static int userauth_none(struct ssh *);
                    361: static int userauth_pubkey(struct ssh *);
                    362: static int userauth_passwd(struct ssh *);
                    363: static int userauth_kbdint(struct ssh *);
                    364: static int userauth_hostbased(struct ssh *);
1.20      markus    365:
1.121     markus    366: #ifdef GSSAPI
1.302     djm       367: static int userauth_gssapi(struct ssh *);
                    368: static void userauth_gssapi_cleanup(struct ssh *);
                    369: static int input_gssapi_response(int type, u_int32_t, struct ssh *);
                    370: static int input_gssapi_token(int type, u_int32_t, struct ssh *);
                    371: static int input_gssapi_error(int, u_int32_t, struct ssh *);
                    372: static int input_gssapi_errtok(int, u_int32_t, struct ssh *);
1.121     markus    373: #endif
                    374:
1.295     djm       375: void   userauth(struct ssh *, char *);
1.51      markus    376:
1.303     djm       377: static void pubkey_cleanup(struct ssh *);
1.295     djm       378: static int sign_and_send_pubkey(struct ssh *ssh, Identity *);
1.352     djm       379: static void pubkey_prepare(struct ssh *, Authctxt *);
1.251     djm       380: static void pubkey_reset(Authctxt *);
1.259     markus    381: static struct sshkey *load_identity_file(Identity *);
1.76      itojun    382:
                    383: static Authmethod *authmethod_get(char *authlist);
                    384: static Authmethod *authmethod_lookup(const char *name);
                    385: static char *authmethods_get(void);
1.20      markus    386:
                    387: Authmethod authmethods[] = {
1.121     markus    388: #ifdef GSSAPI
1.132     markus    389:        {"gssapi-with-mic",
1.121     markus    390:                userauth_gssapi,
1.302     djm       391:                userauth_gssapi_cleanup,
1.121     markus    392:                &options.gss_authentication,
                    393:                NULL},
                    394: #endif
1.81      markus    395:        {"hostbased",
                    396:                userauth_hostbased,
1.170     djm       397:                NULL,
1.81      markus    398:                &options.hostbased_authentication,
                    399:                NULL},
1.20      markus    400:        {"publickey",
                    401:                userauth_pubkey,
1.303     djm       402:                NULL,
1.28      markus    403:                &options.pubkey_authentication,
1.20      markus    404:                NULL},
1.81      markus    405:        {"keyboard-interactive",
                    406:                userauth_kbdint,
1.170     djm       407:                NULL,
1.81      markus    408:                &options.kbd_interactive_authentication,
                    409:                &options.batch_mode},
1.20      markus    410:        {"password",
                    411:                userauth_passwd,
1.170     djm       412:                NULL,
1.20      markus    413:                &options.password_authentication,
1.23      markus    414:                &options.batch_mode},
                    415:        {"none",
                    416:                userauth_none,
                    417:                NULL,
1.170     djm       418:                NULL,
1.23      markus    419:                NULL},
1.170     djm       420:        {NULL, NULL, NULL, NULL, NULL}
1.20      markus    421: };
                    422:
                    423: void
1.291     djm       424: ssh_userauth2(struct ssh *ssh, const char *local_user,
                    425:     const char *server_user, char *host, Sensitive *sensitive)
1.20      markus    426: {
                    427:        Authctxt authctxt;
1.231     markus    428:        int r;
1.39      markus    429:
1.53      markus    430:        if (options.preferred_authentications == NULL)
                    431:                options.preferred_authentications = authmethods_get();
                    432:
1.20      markus    433:        /* setup authentication context */
1.82      markus    434:        memset(&authctxt, 0, sizeof(authctxt));
1.20      markus    435:        authctxt.server_user = server_user;
1.68      markus    436:        authctxt.local_user = local_user;
1.20      markus    437:        authctxt.host = host;
                    438:        authctxt.service = "ssh-connection";            /* service name */
                    439:        authctxt.success = 0;
1.23      markus    440:        authctxt.method = authmethod_lookup("none");
1.51      markus    441:        authctxt.authlist = NULL;
1.121     markus    442:        authctxt.methoddata = NULL;
1.100     markus    443:        authctxt.sensitive = sensitive;
1.223     djm       444:        authctxt.active_ktype = authctxt.oktypes = authctxt.ktypes = NULL;
1.82      markus    445:        authctxt.info_req_seen = 0;
1.292     djm       446:        authctxt.attempt_kbdint = 0;
                    447:        authctxt.attempt_passwd = 0;
1.302     djm       448: #if GSSAPI
                    449:        authctxt.gss_supported_mechs = NULL;
                    450:        authctxt.mech_tried = 0;
                    451: #endif
1.215     djm       452:        authctxt.agent_fd = -1;
1.352     djm       453:        pubkey_prepare(ssh, &authctxt);
1.291     djm       454:        if (authctxt.method == NULL) {
1.331     djm       455:                fatal_f("internal error: cannot send userauth none request");
1.291     djm       456:        }
1.20      markus    457:
1.231     markus    458:        if ((r = sshpkt_start(ssh, SSH2_MSG_SERVICE_REQUEST)) != 0 ||
                    459:            (r = sshpkt_put_cstring(ssh, "ssh-userauth")) != 0 ||
                    460:            (r = sshpkt_send(ssh)) != 0)
1.331     djm       461:                fatal_fr(r, "send packet");
1.231     markus    462:
1.260     markus    463:        ssh->authctxt = &authctxt;
1.231     markus    464:        ssh_dispatch_init(ssh, &input_userauth_error);
                    465:        ssh_dispatch_set(ssh, SSH2_MSG_EXT_INFO, &input_userauth_ext_info);
                    466:        ssh_dispatch_set(ssh, SSH2_MSG_SERVICE_ACCEPT, &input_userauth_service_accept);
1.263     markus    467:        ssh_dispatch_run_fatal(ssh, DISPATCH_BLOCK, &authctxt.success); /* loop until success */
1.303     djm       468:        pubkey_cleanup(ssh);
1.260     markus    469:        ssh->authctxt = NULL;
1.20      markus    470:
1.231     markus    471:        ssh_dispatch_range(ssh, SSH2_MSG_USERAUTH_MIN, SSH2_MSG_USERAUTH_MAX, NULL);
1.119     markus    472:
1.248     dtucker   473:        if (!authctxt.success)
                    474:                fatal("Authentication failed.");
1.351     djm       475:        if (ssh_packet_connection_is_on_socket(ssh)) {
                    476:                verbose("Authenticated to %s ([%s]:%d) using \"%s\".", host,
                    477:                    ssh_remote_ipaddr(ssh), ssh_remote_port(ssh),
                    478:                    authctxt.method->name);
                    479:        } else {
                    480:                verbose("Authenticated to %s (via proxy) using \"%s\".", host,
                    481:                    authctxt.method->name);
                    482:        }
1.20      markus    483: }
1.119     markus    484:
1.302     djm       485: static int
1.261     markus    486: input_userauth_service_accept(int type, u_int32_t seq, struct ssh *ssh)
1.231     markus    487: {
                    488:        int r;
                    489:
                    490:        if (ssh_packet_remaining(ssh) > 0) {
                    491:                char *reply;
                    492:
                    493:                if ((r = sshpkt_get_cstring(ssh, &reply, NULL)) != 0)
                    494:                        goto out;
                    495:                debug2("service_accept: %s", reply);
                    496:                free(reply);
                    497:        } else {
                    498:                debug2("buggy server: service_accept w/o service");
                    499:        }
                    500:        if ((r = sshpkt_get_end(ssh)) != 0)
                    501:                goto out;
                    502:        debug("SSH2_MSG_SERVICE_ACCEPT received");
                    503:
                    504:        /* initial userauth request */
1.295     djm       505:        userauth_none(ssh);
1.231     markus    506:
                    507:        ssh_dispatch_set(ssh, SSH2_MSG_EXT_INFO, &input_userauth_error);
                    508:        ssh_dispatch_set(ssh, SSH2_MSG_USERAUTH_SUCCESS, &input_userauth_success);
                    509:        ssh_dispatch_set(ssh, SSH2_MSG_USERAUTH_FAILURE, &input_userauth_failure);
                    510:        ssh_dispatch_set(ssh, SSH2_MSG_USERAUTH_BANNER, &input_userauth_banner);
                    511:        r = 0;
                    512:  out:
                    513:        return r;
                    514: }
                    515:
1.302     djm       516: static int
1.261     markus    517: input_userauth_ext_info(int type, u_int32_t seqnr, struct ssh *ssh)
1.231     markus    518: {
1.261     markus    519:        return kex_input_ext_info(type, seqnr, ssh);
1.231     markus    520: }
                    521:
1.20      markus    522: void
1.295     djm       523: userauth(struct ssh *ssh, char *authlist)
1.51      markus    524: {
1.295     djm       525:        Authctxt *authctxt = (Authctxt *)ssh->authctxt;
1.278     markus    526:
1.170     djm       527:        if (authctxt->method != NULL && authctxt->method->cleanup != NULL)
1.295     djm       528:                authctxt->method->cleanup(ssh);
1.170     djm       529:
1.197     djm       530:        free(authctxt->methoddata);
                    531:        authctxt->methoddata = NULL;
1.51      markus    532:        if (authlist == NULL) {
                    533:                authlist = authctxt->authlist;
                    534:        } else {
1.197     djm       535:                free(authctxt->authlist);
1.51      markus    536:                authctxt->authlist = authlist;
                    537:        }
                    538:        for (;;) {
                    539:                Authmethod *method = authmethod_get(authlist);
                    540:                if (method == NULL)
1.264     dtucker   541:                        fatal("%s@%s: Permission denied (%s).",
                    542:                            authctxt->server_user, authctxt->host, authlist);
1.51      markus    543:                authctxt->method = method;
1.119     markus    544:
                    545:                /* reset the per method handler */
1.278     markus    546:                ssh_dispatch_range(ssh, SSH2_MSG_USERAUTH_PER_METHOD_MIN,
1.119     markus    547:                    SSH2_MSG_USERAUTH_PER_METHOD_MAX, NULL);
                    548:
                    549:                /* and try new method */
1.295     djm       550:                if (method->userauth(ssh) != 0) {
1.51      markus    551:                        debug2("we sent a %s packet, wait for reply", method->name);
                    552:                        break;
                    553:                } else {
                    554:                        debug2("we did not send a packet, disable method");
                    555:                        method->enabled = NULL;
                    556:                }
                    557:        }
                    558: }
1.105     deraadt   559:
1.302     djm       560: static int
1.261     markus    561: input_userauth_error(int type, u_int32_t seq, struct ssh *ssh)
1.20      markus    562: {
1.331     djm       563:        fatal_f("bad message during authentication: type %d", type);
1.218     markus    564:        return 0;
1.35      markus    565: }
1.105     deraadt   566:
1.302     djm       567: static int
1.261     markus    568: input_userauth_banner(int type, u_int32_t seq, struct ssh *ssh)
1.35      markus    569: {
1.295     djm       570:        char *msg = NULL;
1.294     djm       571:        size_t len;
                    572:        int r;
1.126     deraadt   573:
1.331     djm       574:        debug3_f("entering");
1.294     djm       575:        if ((r = sshpkt_get_cstring(ssh, &msg, &len)) != 0 ||
1.295     djm       576:            (r = sshpkt_get_cstring(ssh, NULL, NULL)) != 0)
1.294     djm       577:                goto out;
1.246     djm       578:        if (len > 0 && options.log_level >= SYSLOG_LEVEL_INFO)
                    579:                fmprintf(stderr, "%s", msg);
1.294     djm       580:        r = 0;
                    581:  out:
1.246     djm       582:        free(msg);
1.294     djm       583:        return r;
1.20      markus    584: }
1.105     deraadt   585:
1.302     djm       586: static int
1.261     markus    587: input_userauth_success(int type, u_int32_t seq, struct ssh *ssh)
1.20      markus    588: {
1.260     markus    589:        Authctxt *authctxt = ssh->authctxt;
1.172     djm       590:
1.20      markus    591:        if (authctxt == NULL)
1.331     djm       592:                fatal_f("no authentication context");
1.197     djm       593:        free(authctxt->authlist);
                    594:        authctxt->authlist = NULL;
1.172     djm       595:        if (authctxt->method != NULL && authctxt->method->cleanup != NULL)
1.295     djm       596:                authctxt->method->cleanup(ssh);
1.197     djm       597:        free(authctxt->methoddata);
                    598:        authctxt->methoddata = NULL;
1.20      markus    599:        authctxt->success = 1;                  /* break out */
1.218     markus    600:        return 0;
1.20      markus    601: }
1.105     deraadt   602:
1.302     djm       603: #if 0
                    604: static int
1.261     markus    605: input_userauth_success_unexpected(int type, u_int32_t seq, struct ssh *ssh)
1.172     djm       606: {
1.260     markus    607:        Authctxt *authctxt = ssh->authctxt;
1.172     djm       608:
                    609:        if (authctxt == NULL)
1.331     djm       610:                fatal_f("no authentication context");
1.172     djm       611:
                    612:        fatal("Unexpected authentication success during %s.",
                    613:            authctxt->method->name);
1.218     markus    614:        return 0;
1.172     djm       615: }
1.302     djm       616: #endif
1.172     djm       617:
1.302     djm       618: static int
1.261     markus    619: input_userauth_failure(int type, u_int32_t seq, struct ssh *ssh)
1.20      markus    620: {
1.260     markus    621:        Authctxt *authctxt = ssh->authctxt;
1.20      markus    622:        char *authlist = NULL;
1.278     markus    623:        u_char partial;
1.20      markus    624:
                    625:        if (authctxt == NULL)
                    626:                fatal("input_userauth_failure: no authentication context");
                    627:
1.307     dtucker   628:        if (sshpkt_get_cstring(ssh, &authlist, NULL) != 0 ||
                    629:            sshpkt_get_u8(ssh, &partial) != 0 ||
                    630:            sshpkt_get_end(ssh) != 0)
1.278     markus    631:                goto out;
1.20      markus    632:
1.193     markus    633:        if (partial != 0) {
1.351     djm       634:                verbose("Authenticated using \"%s\" with partial success.",
                    635:                    authctxt->method->name);
1.193     markus    636:                /* reset state */
1.251     djm       637:                pubkey_reset(authctxt);
1.193     markus    638:        }
1.109     markus    639:        debug("Authentications that can continue: %s", authlist);
1.20      markus    640:
1.295     djm       641:        userauth(ssh, authlist);
1.278     markus    642:        authlist = NULL;
                    643:  out:
                    644:        free(authlist);
1.218     markus    645:        return 0;
1.51      markus    646: }
1.169     djm       647:
1.287     djm       648: /*
                    649:  * Format an identity for logging including filename, key type, fingerprint
                    650:  * and location (agent, etc.). Caller must free.
                    651:  */
                    652: static char *
                    653: format_identity(Identity *id)
                    654: {
                    655:        char *fp = NULL, *ret = NULL;
1.309     djm       656:        const char *note = "";
1.287     djm       657:
                    658:        if (id->key != NULL) {
1.347     djm       659:                fp = sshkey_fingerprint(id->key, options.fingerprint_hash,
1.287     djm       660:                    SSH_FP_DEFAULT);
                    661:        }
1.309     djm       662:        if (id->key) {
                    663:                if ((id->key->flags & SSHKEY_FLAG_EXT) != 0)
                    664:                        note = " token";
1.311     markus    665:                else if (sshkey_is_sk(id->key))
1.319     naddy     666:                        note = " authenticator";
1.309     djm       667:        }
1.287     djm       668:        xasprintf(&ret, "%s %s%s%s%s%s%s",
                    669:            id->filename,
                    670:            id->key ? sshkey_type(id->key) : "", id->key ? " " : "",
                    671:            fp ? fp : "",
1.309     djm       672:            id->userprovided ? " explicit" : "", note,
1.287     djm       673:            id->agent_fd != -1 ? " agent" : "");
                    674:        free(fp);
                    675:        return ret;
                    676: }
                    677:
1.302     djm       678: static int
1.261     markus    679: input_userauth_pk_ok(int type, u_int32_t seq, struct ssh *ssh)
1.51      markus    680: {
1.260     markus    681:        Authctxt *authctxt = ssh->authctxt;
1.259     markus    682:        struct sshkey *key = NULL;
1.117     markus    683:        Identity *id = NULL;
1.287     djm       684:        int pktype, found = 0, sent = 0;
1.278     markus    685:        size_t blen;
1.287     djm       686:        char *pkalg = NULL, *fp = NULL, *ident = NULL;
1.278     markus    687:        u_char *pkblob = NULL;
                    688:        int r;
1.51      markus    689:
                    690:        if (authctxt == NULL)
                    691:                fatal("input_userauth_pk_ok: no authentication context");
1.267     djm       692:
1.278     markus    693:        if ((r = sshpkt_get_cstring(ssh, &pkalg, NULL)) != 0 ||
                    694:            (r = sshpkt_get_string(ssh, &pkblob, &blen)) != 0 ||
                    695:            (r = sshpkt_get_end(ssh)) != 0)
                    696:                goto done;
1.51      markus    697:
1.278     markus    698:        if ((pktype = sshkey_type_from_name(pkalg)) == KEY_UNSPEC) {
1.331     djm       699:                debug_f("server sent unknown pkalg %s", pkalg);
1.117     markus    700:                goto done;
                    701:        }
1.278     markus    702:        if ((r = sshkey_from_blob(pkblob, blen, &key)) != 0) {
1.331     djm       703:                debug_r(r, "no key from blob. pkalg %s", pkalg);
1.117     markus    704:                goto done;
                    705:        }
                    706:        if (key->type != pktype) {
                    707:                error("input_userauth_pk_ok: type mismatch "
                    708:                    "for decoded key (received %d, expected %d)",
                    709:                    key->type, pktype);
                    710:                goto done;
                    711:        }
                    712:
1.127     markus    713:        /*
                    714:         * search keys in the reverse order, because last candidate has been
                    715:         * moved to the end of the queue.  this also avoids confusion by
                    716:         * duplicate keys
                    717:         */
1.136     henning   718:        TAILQ_FOREACH_REVERSE(id, &authctxt->keys, idlist, next) {
1.278     markus    719:                if (sshkey_equal(key, id->key)) {
1.287     djm       720:                        found = 1;
1.51      markus    721:                        break;
                    722:                }
1.117     markus    723:        }
1.287     djm       724:        if (!found || id == NULL) {
                    725:                fp = sshkey_fingerprint(key, options.fingerprint_hash,
                    726:                    SSH_FP_DEFAULT);
1.331     djm       727:                error_f("server replied with unknown key: %s %s",
1.287     djm       728:                    sshkey_type(key), fp == NULL ? "<ERROR>" : fp);
                    729:                goto done;
                    730:        }
                    731:        ident = format_identity(id);
                    732:        debug("Server accepts key: %s", ident);
1.295     djm       733:        sent = sign_and_send_pubkey(ssh, id);
1.278     markus    734:        r = 0;
                    735:  done:
                    736:        sshkey_free(key);
1.287     djm       737:        free(ident);
                    738:        free(fp);
1.197     djm       739:        free(pkalg);
                    740:        free(pkblob);
1.51      markus    741:
1.106     deraadt   742:        /* try another method if we did not send a packet */
1.278     markus    743:        if (r == 0 && sent == 0)
1.295     djm       744:                userauth(ssh, NULL);
1.278     markus    745:        return r;
1.20      markus    746: }
1.121     markus    747:
                    748: #ifdef GSSAPI
1.302     djm       749: static int
1.295     djm       750: userauth_gssapi(struct ssh *ssh)
1.121     markus    751: {
1.295     djm       752:        Authctxt *authctxt = (Authctxt *)ssh->authctxt;
1.121     markus    753:        Gssctxt *gssctxt = NULL;
                    754:        OM_uint32 min;
1.278     markus    755:        int r, ok = 0;
1.302     djm       756:        gss_OID mech = NULL;
1.121     markus    757:
                    758:        /* Try one GSSAPI method at a time, rather than sending them all at
                    759:         * once. */
                    760:
1.302     djm       761:        if (authctxt->gss_supported_mechs == NULL)
                    762:                gss_indicate_mechs(&min, &authctxt->gss_supported_mechs);
1.121     markus    763:
1.302     djm       764:        /* Check to see whether the mechanism is usable before we offer it */
                    765:        while (authctxt->mech_tried < authctxt->gss_supported_mechs->count &&
                    766:            !ok) {
                    767:                mech = &authctxt->gss_supported_mechs->
                    768:                    elements[authctxt->mech_tried];
1.121     markus    769:                /* My DER encoding requires length<128 */
1.302     djm       770:                if (mech->length < 128 && ssh_gssapi_check_mechanism(&gssctxt,
                    771:                    mech, authctxt->host)) {
1.121     markus    772:                        ok = 1; /* Mechanism works */
                    773:                } else {
1.302     djm       774:                        authctxt->mech_tried++;
1.121     markus    775:                }
                    776:        }
                    777:
1.302     djm       778:        if (!ok || mech == NULL)
1.139     djm       779:                return 0;
1.121     markus    780:
                    781:        authctxt->methoddata=(void *)gssctxt;
                    782:
1.278     markus    783:        if ((r = sshpkt_start(ssh, SSH2_MSG_USERAUTH_REQUEST)) != 0 ||
                    784:            (r = sshpkt_put_cstring(ssh, authctxt->server_user)) != 0 ||
                    785:            (r = sshpkt_put_cstring(ssh, authctxt->service)) != 0 ||
                    786:            (r = sshpkt_put_cstring(ssh, authctxt->method->name)) != 0 ||
                    787:            (r = sshpkt_put_u32(ssh, 1)) != 0 ||
1.302     djm       788:            (r = sshpkt_put_u32(ssh, (mech->length) + 2)) != 0 ||
1.278     markus    789:            (r = sshpkt_put_u8(ssh, SSH_GSS_OIDTYPE)) != 0 ||
1.302     djm       790:            (r = sshpkt_put_u8(ssh, mech->length)) != 0 ||
                    791:            (r = sshpkt_put(ssh, mech->elements, mech->length)) != 0 ||
1.278     markus    792:            (r = sshpkt_send(ssh)) != 0)
1.331     djm       793:                fatal_fr(r, "send packet");
1.278     markus    794:
                    795:        ssh_dispatch_set(ssh, SSH2_MSG_USERAUTH_GSSAPI_RESPONSE, &input_gssapi_response);
                    796:        ssh_dispatch_set(ssh, SSH2_MSG_USERAUTH_GSSAPI_TOKEN, &input_gssapi_token);
                    797:        ssh_dispatch_set(ssh, SSH2_MSG_USERAUTH_GSSAPI_ERROR, &input_gssapi_error);
                    798:        ssh_dispatch_set(ssh, SSH2_MSG_USERAUTH_GSSAPI_ERRTOK, &input_gssapi_errtok);
1.121     markus    799:
1.302     djm       800:        authctxt->mech_tried++; /* Move along to next candidate */
1.121     markus    801:
                    802:        return 1;
                    803: }
                    804:
1.302     djm       805: static void
                    806: userauth_gssapi_cleanup(struct ssh *ssh)
                    807: {
                    808:        Authctxt *authctxt = (Authctxt *)ssh->authctxt;
                    809:        Gssctxt *gssctxt = (Gssctxt *)authctxt->methoddata;
                    810:
                    811:        ssh_gssapi_delete_ctx(&gssctxt);
                    812:        authctxt->methoddata = NULL;
                    813:
                    814:        free(authctxt->gss_supported_mechs);
                    815:        authctxt->gss_supported_mechs = NULL;
                    816: }
                    817:
1.130     markus    818: static OM_uint32
1.262     djm       819: process_gssapi_token(struct ssh *ssh, gss_buffer_t recv_tok)
1.130     markus    820: {
1.260     markus    821:        Authctxt *authctxt = ssh->authctxt;
1.130     markus    822:        Gssctxt *gssctxt = authctxt->methoddata;
                    823:        gss_buffer_desc send_tok = GSS_C_EMPTY_BUFFER;
1.142     djm       824:        gss_buffer_desc mic = GSS_C_EMPTY_BUFFER;
                    825:        gss_buffer_desc gssbuf;
1.132     markus    826:        OM_uint32 status, ms, flags;
1.278     markus    827:        int r;
1.133     djm       828:
1.130     markus    829:        status = ssh_gssapi_init_ctx(gssctxt, options.gss_deleg_creds,
1.132     markus    830:            recv_tok, &send_tok, &flags);
1.130     markus    831:
                    832:        if (send_tok.length > 0) {
1.278     markus    833:                u_char type = GSS_ERROR(status) ?
                    834:                    SSH2_MSG_USERAUTH_GSSAPI_ERRTOK :
                    835:                    SSH2_MSG_USERAUTH_GSSAPI_TOKEN;
                    836:
                    837:                if ((r = sshpkt_start(ssh, type)) != 0 ||
                    838:                    (r = sshpkt_put_string(ssh, send_tok.value,
                    839:                    send_tok.length)) != 0 ||
                    840:                    (r = sshpkt_send(ssh)) != 0)
1.331     djm       841:                        fatal_fr(r, "send %u packet", type);
1.133     djm       842:
1.130     markus    843:                gss_release_buffer(&ms, &send_tok);
                    844:        }
1.133     djm       845:
1.130     markus    846:        if (status == GSS_S_COMPLETE) {
1.132     markus    847:                /* send either complete or MIC, depending on mechanism */
                    848:                if (!(flags & GSS_C_INTEG_FLAG)) {
1.278     markus    849:                        if ((r = sshpkt_start(ssh,
                    850:                            SSH2_MSG_USERAUTH_GSSAPI_EXCHANGE_COMPLETE)) != 0 ||
                    851:                            (r = sshpkt_send(ssh)) != 0)
1.331     djm       852:                                fatal_fr(r, "send completion");
1.132     markus    853:                } else {
1.278     markus    854:                        struct sshbuf *b;
                    855:
                    856:                        if ((b = sshbuf_new()) == NULL)
1.331     djm       857:                                fatal_f("sshbuf_new failed");
1.278     markus    858:                        ssh_gssapi_buildmic(b, authctxt->server_user,
1.346     djm       859:                            authctxt->service, "gssapi-with-mic",
                    860:                            ssh->kex->session_id);
1.132     markus    861:
1.278     markus    862:                        if ((gssbuf.value = sshbuf_mutable_ptr(b)) == NULL)
1.331     djm       863:                                fatal_f("sshbuf_mutable_ptr failed");
1.278     markus    864:                        gssbuf.length = sshbuf_len(b);
1.133     djm       865:
1.132     markus    866:                        status = ssh_gssapi_sign(gssctxt, &gssbuf, &mic);
1.133     djm       867:
1.132     markus    868:                        if (!GSS_ERROR(status)) {
1.278     markus    869:                                if ((r = sshpkt_start(ssh,
                    870:                                    SSH2_MSG_USERAUTH_GSSAPI_MIC)) != 0 ||
                    871:                                    (r = sshpkt_put_string(ssh, mic.value,
                    872:                                    mic.length)) != 0 ||
                    873:                                    (r = sshpkt_send(ssh)) != 0)
1.331     djm       874:                                        fatal_fr(r, "send MIC");
1.132     markus    875:                        }
1.133     djm       876:
1.278     markus    877:                        sshbuf_free(b);
1.132     markus    878:                        gss_release_buffer(&ms, &mic);
1.133     djm       879:                }
1.130     markus    880:        }
1.133     djm       881:
1.130     markus    882:        return status;
                    883: }
                    884:
1.302     djm       885: static int
1.261     markus    886: input_gssapi_response(int type, u_int32_t plen, struct ssh *ssh)
1.121     markus    887: {
1.260     markus    888:        Authctxt *authctxt = ssh->authctxt;
1.121     markus    889:        Gssctxt *gssctxt;
1.278     markus    890:        size_t oidlen;
                    891:        u_char *oidv = NULL;
                    892:        int r;
1.121     markus    893:
                    894:        if (authctxt == NULL)
                    895:                fatal("input_gssapi_response: no authentication context");
                    896:        gssctxt = authctxt->methoddata;
                    897:
                    898:        /* Setup our OID */
1.278     markus    899:        if ((r = sshpkt_get_string(ssh, &oidv, &oidlen)) != 0)
                    900:                goto done;
1.121     markus    901:
1.129     markus    902:        if (oidlen <= 2 ||
                    903:            oidv[0] != SSH_GSS_OIDTYPE ||
                    904:            oidv[1] != oidlen - 2) {
                    905:                debug("Badly encoded mechanism OID received");
1.296     djm       906:                userauth(ssh, NULL);
1.278     markus    907:                goto ok;
1.121     markus    908:        }
1.129     markus    909:
                    910:        if (!ssh_gssapi_check_oid(gssctxt, oidv + 2, oidlen - 2))
                    911:                fatal("Server returned different OID than expected");
1.121     markus    912:
1.278     markus    913:        if ((r = sshpkt_get_end(ssh)) != 0)
                    914:                goto done;
1.121     markus    915:
1.262     djm       916:        if (GSS_ERROR(process_gssapi_token(ssh, GSS_C_NO_BUFFER))) {
1.121     markus    917:                /* Start again with next method on list */
                    918:                debug("Trying to start again");
1.296     djm       919:                userauth(ssh, NULL);
1.278     markus    920:                goto ok;
1.121     markus    921:        }
1.278     markus    922:  ok:
                    923:        r = 0;
                    924:  done:
                    925:        free(oidv);
                    926:        return r;
1.121     markus    927: }
                    928:
1.302     djm       929: static int
1.261     markus    930: input_gssapi_token(int type, u_int32_t plen, struct ssh *ssh)
1.121     markus    931: {
1.260     markus    932:        Authctxt *authctxt = ssh->authctxt;
1.121     markus    933:        gss_buffer_desc recv_tok;
1.278     markus    934:        u_char *p = NULL;
                    935:        size_t len;
1.130     markus    936:        OM_uint32 status;
1.278     markus    937:        int r;
1.121     markus    938:
                    939:        if (authctxt == NULL)
                    940:                fatal("input_gssapi_response: no authentication context");
                    941:
1.278     markus    942:        if ((r = sshpkt_get_string(ssh, &p, &len)) != 0 ||
                    943:            (r = sshpkt_get_end(ssh)) != 0)
                    944:                goto out;
1.121     markus    945:
1.278     markus    946:        recv_tok.value = p;
                    947:        recv_tok.length = len;
1.262     djm       948:        status = process_gssapi_token(ssh, &recv_tok);
1.121     markus    949:
1.278     markus    950:        /* Start again with the next method in the list */
1.121     markus    951:        if (GSS_ERROR(status)) {
1.296     djm       952:                userauth(ssh, NULL);
1.278     markus    953:                /* ok */
1.121     markus    954:        }
1.278     markus    955:        r = 0;
                    956:  out:
                    957:        free(p);
                    958:        return r;
1.121     markus    959: }
                    960:
1.302     djm       961: static int
1.261     markus    962: input_gssapi_errtok(int type, u_int32_t plen, struct ssh *ssh)
1.121     markus    963: {
1.260     markus    964:        Authctxt *authctxt = ssh->authctxt;
1.121     markus    965:        Gssctxt *gssctxt;
                    966:        gss_buffer_desc send_tok = GSS_C_EMPTY_BUFFER;
                    967:        gss_buffer_desc recv_tok;
1.194     djm       968:        OM_uint32 ms;
1.278     markus    969:        u_char *p = NULL;
                    970:        size_t len;
                    971:        int r;
1.121     markus    972:
                    973:        if (authctxt == NULL)
                    974:                fatal("input_gssapi_response: no authentication context");
                    975:        gssctxt = authctxt->methoddata;
                    976:
1.278     markus    977:        if ((r = sshpkt_get_string(ssh, &p, &len)) != 0 ||
                    978:            (r = sshpkt_get_end(ssh)) != 0) {
                    979:                free(p);
                    980:                return r;
                    981:        }
1.121     markus    982:
                    983:        /* Stick it into GSSAPI and see what it says */
1.278     markus    984:        recv_tok.value = p;
                    985:        recv_tok.length = len;
1.194     djm       986:        (void)ssh_gssapi_init_ctx(gssctxt, options.gss_deleg_creds,
1.140     djm       987:            &recv_tok, &send_tok, NULL);
1.278     markus    988:        free(p);
1.121     markus    989:        gss_release_buffer(&ms, &send_tok);
                    990:
                    991:        /* Server will be returning a failed packet after this one */
1.220     djm       992:        return 0;
1.121     markus    993: }
                    994:
1.302     djm       995: static int
1.261     markus    996: input_gssapi_error(int type, u_int32_t plen, struct ssh *ssh)
1.121     markus    997: {
1.278     markus    998:        char *msg = NULL;
                    999:        char *lang = NULL;
                   1000:        int r;
1.121     markus   1001:
1.278     markus   1002:        if ((r = sshpkt_get_u32(ssh, NULL)) != 0 ||     /* maj */
                   1003:            (r = sshpkt_get_u32(ssh, NULL)) != 0 ||     /* min */
                   1004:            (r = sshpkt_get_cstring(ssh, &msg, NULL)) != 0 ||
                   1005:            (r = sshpkt_get_cstring(ssh, &lang, NULL)) != 0)
                   1006:                goto out;
                   1007:        r = sshpkt_get_end(ssh);
1.143     stevesk  1008:        debug("Server GSSAPI Error:\n%s", msg);
1.278     markus   1009:  out:
1.197     djm      1010:        free(msg);
                   1011:        free(lang);
1.278     markus   1012:        return r;
1.121     markus   1013: }
                   1014: #endif /* GSSAPI */
1.20      markus   1015:
1.302     djm      1016: static int
1.295     djm      1017: userauth_none(struct ssh *ssh)
1.23      markus   1018: {
1.295     djm      1019:        Authctxt *authctxt = (Authctxt *)ssh->authctxt;
1.278     markus   1020:        int r;
                   1021:
1.23      markus   1022:        /* initial userauth request */
1.278     markus   1023:        if ((r = sshpkt_start(ssh, SSH2_MSG_USERAUTH_REQUEST)) != 0 ||
                   1024:            (r = sshpkt_put_cstring(ssh, authctxt->server_user)) != 0 ||
                   1025:            (r = sshpkt_put_cstring(ssh, authctxt->service)) != 0 ||
                   1026:            (r = sshpkt_put_cstring(ssh, authctxt->method->name)) != 0 ||
                   1027:            (r = sshpkt_send(ssh)) != 0)
1.331     djm      1028:                fatal_fr(r, "send packet");
1.23      markus   1029:        return 1;
                   1030: }
                   1031:
1.302     djm      1032: static int
1.295     djm      1033: userauth_passwd(struct ssh *ssh)
1.1       markus   1034: {
1.295     djm      1035:        Authctxt *authctxt = (Authctxt *)ssh->authctxt;
1.290     djm      1036:        char *password, *prompt = NULL;
1.175     dtucker  1037:        const char *host = options.host_key_alias ?  options.host_key_alias :
                   1038:            authctxt->host;
1.278     markus   1039:        int r;
1.6       markus   1040:
1.292     djm      1041:        if (authctxt->attempt_passwd++ >= options.number_of_password_prompts)
1.6       markus   1042:                return 0;
1.13      todd     1043:
1.292     djm      1044:        if (authctxt->attempt_passwd != 1)
1.13      todd     1045:                error("Permission denied, please try again.");
1.1       markus   1046:
1.290     djm      1047:        xasprintf(&prompt, "%s@%s's password: ", authctxt->server_user, host);
1.1       markus   1048:        password = read_passphrase(prompt, 0);
1.278     markus   1049:        if ((r = sshpkt_start(ssh, SSH2_MSG_USERAUTH_REQUEST)) != 0 ||
                   1050:            (r = sshpkt_put_cstring(ssh, authctxt->server_user)) != 0 ||
                   1051:            (r = sshpkt_put_cstring(ssh, authctxt->service)) != 0 ||
                   1052:            (r = sshpkt_put_cstring(ssh, authctxt->method->name)) != 0 ||
                   1053:            (r = sshpkt_put_u8(ssh, 0)) != 0 ||
                   1054:            (r = sshpkt_put_cstring(ssh, password)) != 0 ||
                   1055:            (r = sshpkt_add_padding(ssh, 64)) != 0 ||
                   1056:            (r = sshpkt_send(ssh)) != 0)
1.331     djm      1057:                fatal_fr(r, "send packet");
1.99      markus   1058:
1.290     djm      1059:        free(prompt);
                   1060:        if (password != NULL)
1.278     markus   1061:                freezero(password, strlen(password));
                   1062:
                   1063:        ssh_dispatch_set(ssh, SSH2_MSG_USERAUTH_PASSWD_CHANGEREQ,
1.99      markus   1064:            &input_userauth_passwd_changereq);
                   1065:
1.1       markus   1066:        return 1;
                   1067: }
1.169     djm      1068:
1.99      markus   1069: /*
                   1070:  * parse PASSWD_CHANGEREQ, prompt user and send SSH2_MSG_USERAUTH_REQUEST
                   1071:  */
1.302     djm      1072: static int
1.261     markus   1073: input_userauth_passwd_changereq(int type, u_int32_t seqnr, struct ssh *ssh)
1.99      markus   1074: {
1.260     markus   1075:        Authctxt *authctxt = ssh->authctxt;
1.278     markus   1076:        char *info = NULL, *lang = NULL, *password = NULL, *retype = NULL;
1.266     dtucker  1077:        char prompt[256];
1.252     djm      1078:        const char *host;
1.278     markus   1079:        int r;
1.99      markus   1080:
                   1081:        debug2("input_userauth_passwd_changereq");
                   1082:
                   1083:        if (authctxt == NULL)
                   1084:                fatal("input_userauth_passwd_changereq: "
                   1085:                    "no authentication context");
1.252     djm      1086:        host = options.host_key_alias ? options.host_key_alias : authctxt->host;
1.99      markus   1087:
1.278     markus   1088:        if ((r = sshpkt_get_cstring(ssh, &info, NULL)) != 0 ||
                   1089:            (r = sshpkt_get_cstring(ssh, &lang, NULL)) != 0)
                   1090:                goto out;
1.99      markus   1091:        if (strlen(info) > 0)
1.116     itojun   1092:                logit("%s", info);
1.278     markus   1093:        if ((r = sshpkt_start(ssh, SSH2_MSG_USERAUTH_REQUEST)) != 0 ||
                   1094:            (r = sshpkt_put_cstring(ssh, authctxt->server_user)) != 0 ||
                   1095:            (r = sshpkt_put_cstring(ssh, authctxt->service)) != 0 ||
                   1096:            (r = sshpkt_put_cstring(ssh, authctxt->method->name)) != 0 ||
                   1097:            (r = sshpkt_put_u8(ssh, 1)) != 0)   /* additional info */
                   1098:                goto out;
                   1099:
1.104     deraadt  1100:        snprintf(prompt, sizeof(prompt),
1.99      markus   1101:            "Enter %.30s@%.128s's old password: ",
1.175     dtucker  1102:            authctxt->server_user, host);
1.99      markus   1103:        password = read_passphrase(prompt, 0);
1.278     markus   1104:        if ((r = sshpkt_put_cstring(ssh, password)) != 0)
                   1105:                goto out;
                   1106:
                   1107:        freezero(password, strlen(password));
1.99      markus   1108:        password = NULL;
                   1109:        while (password == NULL) {
1.104     deraadt  1110:                snprintf(prompt, sizeof(prompt),
1.99      markus   1111:                    "Enter %.30s@%.128s's new password: ",
1.175     dtucker  1112:                    authctxt->server_user, host);
1.99      markus   1113:                password = read_passphrase(prompt, RP_ALLOW_EOF);
                   1114:                if (password == NULL) {
                   1115:                        /* bail out */
1.278     markus   1116:                        r = 0;
                   1117:                        goto out;
1.99      markus   1118:                }
1.104     deraadt  1119:                snprintf(prompt, sizeof(prompt),
1.99      markus   1120:                    "Retype %.30s@%.128s's new password: ",
1.175     dtucker  1121:                    authctxt->server_user, host);
1.99      markus   1122:                retype = read_passphrase(prompt, 0);
                   1123:                if (strcmp(password, retype) != 0) {
1.278     markus   1124:                        freezero(password, strlen(password));
1.116     itojun   1125:                        logit("Mismatch; try again, EOF to quit.");
1.99      markus   1126:                        password = NULL;
                   1127:                }
1.278     markus   1128:                freezero(retype, strlen(retype));
1.99      markus   1129:        }
1.278     markus   1130:        if ((r = sshpkt_put_cstring(ssh, password)) != 0 ||
                   1131:            (r = sshpkt_add_padding(ssh, 64)) != 0 ||
                   1132:            (r = sshpkt_send(ssh)) != 0)
                   1133:                goto out;
1.104     deraadt  1134:
1.278     markus   1135:        ssh_dispatch_set(ssh, SSH2_MSG_USERAUTH_PASSWD_CHANGEREQ,
1.99      markus   1136:            &input_userauth_passwd_changereq);
1.278     markus   1137:        r = 0;
                   1138:  out:
                   1139:        if (password)
                   1140:                freezero(password, strlen(password));
                   1141:        free(info);
                   1142:        free(lang);
                   1143:        return r;
1.117     markus   1144: }
                   1145:
1.272     djm      1146: /*
                   1147:  * Select an algorithm for publickey signatures.
                   1148:  * Returns algorithm (caller must free) or NULL if no mutual algorithm found.
                   1149:  *
                   1150:  * Call with ssh==NULL to ignore server-sig-algs extension list and
                   1151:  * only attempt with the key's base signature type.
                   1152:  */
                   1153: static char *
                   1154: key_sig_algorithm(struct ssh *ssh, const struct sshkey *key)
1.231     markus   1155: {
1.274     djm      1156:        char *allowed, *oallowed, *cp, *tmp, *alg = NULL;
1.348     djm      1157:        const char *server_sig_algs;
1.231     markus   1158:
1.272     djm      1159:        /*
                   1160:         * The signature algorithm will only differ from the key algorithm
                   1161:         * for RSA keys/certs and when the server advertises support for
                   1162:         * newer (SHA2) algorithms.
                   1163:         */
                   1164:        if (ssh == NULL || ssh->kex->server_sig_algs == NULL ||
1.288     djm      1165:            (key->type != KEY_RSA && key->type != KEY_RSA_CERT) ||
1.345     djm      1166:            (key->type == KEY_RSA_CERT && (ssh->compat & SSH_BUG_SIGTYPE))) {
1.272     djm      1167:                /* Filter base key signature alg against our configuration */
1.279     markus   1168:                return match_list(sshkey_ssh_name(key),
1.342     dtucker  1169:                    options.pubkey_accepted_algos, NULL);
1.231     markus   1170:        }
                   1171:
1.272     djm      1172:        /*
1.348     djm      1173:         * Workaround OpenSSH 7.4 bug: this version supports RSA/SHA-2 but
                   1174:         * fails to advertise it via SSH2_MSG_EXT_INFO.
                   1175:         */
                   1176:        server_sig_algs = ssh->kex->server_sig_algs;
                   1177:        if (key->type == KEY_RSA && (ssh->compat & SSH_BUG_SIGTYPE74))
                   1178:                server_sig_algs = "rsa-sha2-256,rsa-sha2-512";
                   1179:
                   1180:        /*
1.272     djm      1181:         * For RSA keys/certs, since these might have a different sig type:
1.342     dtucker  1182:         * find the first entry in PubkeyAcceptedAlgorithms of the right type
1.272     djm      1183:         * that also appears in the supported signature algorithms list from
                   1184:         * the server.
                   1185:         */
1.342     dtucker  1186:        oallowed = allowed = xstrdup(options.pubkey_accepted_algos);
1.272     djm      1187:        while ((cp = strsep(&allowed, ",")) != NULL) {
                   1188:                if (sshkey_type_from_name(cp) != key->type)
                   1189:                        continue;
1.323     djm      1190:                tmp = match_list(sshkey_sigalg_by_name(cp),
1.348     djm      1191:                    server_sig_algs, NULL);
1.274     djm      1192:                if (tmp != NULL)
                   1193:                        alg = xstrdup(cp);
                   1194:                free(tmp);
1.272     djm      1195:                if (alg != NULL)
                   1196:                        break;
1.269     djm      1197:        }
1.272     djm      1198:        free(oallowed);
                   1199:        return alg;
1.269     djm      1200: }
                   1201:
1.117     markus   1202: static int
1.214     djm      1203: identity_sign(struct identity *id, u_char **sigp, size_t *lenp,
1.272     djm      1204:     const u_char *data, size_t datalen, u_int compat, const char *alg)
1.117     markus   1205: {
1.309     djm      1206:        struct sshkey *sign_key = NULL, *prv = NULL;
1.360     djm      1207:        int is_agent = 0, retried = 0, r = SSH_ERR_INTERNAL_ERROR;
1.312     djm      1208:        struct notifier_ctx *notifier = NULL;
1.325     djm      1209:        char *fp = NULL, *pin = NULL, *prompt = NULL;
1.309     djm      1210:
                   1211:        *sigp = NULL;
                   1212:        *lenp = 0;
1.99      markus   1213:
1.272     djm      1214:        /* The agent supports this key. */
1.269     djm      1215:        if (id->key != NULL && id->agent_fd != -1) {
1.272     djm      1216:                return ssh_agent_sign(id->agent_fd, id->key, sigp, lenp,
                   1217:                    data, datalen, alg, compat);
1.269     djm      1218:        }
1.214     djm      1219:
1.117     markus   1220:        /*
1.272     djm      1221:         * We have already loaded the private key or the private key is
                   1222:         * stored in external hardware.
1.117     markus   1223:         */
1.255     djm      1224:        if (id->key != NULL &&
1.272     djm      1225:            (id->isprivate || (id->key->flags & SSHKEY_FLAG_EXT))) {
1.309     djm      1226:                sign_key = id->key;
1.360     djm      1227:                is_agent = 1;
1.309     djm      1228:        } else {
                   1229:                /* Load the private key from the file. */
                   1230:                if ((prv = load_identity_file(id)) == NULL)
                   1231:                        return SSH_ERR_KEY_NOT_FOUND;
                   1232:                if (id->key != NULL && !sshkey_equal_public(prv, id->key)) {
1.331     djm      1233:                        error_f("private key %s contents do not match public",
1.347     djm      1234:                            id->filename);
1.309     djm      1235:                        r = SSH_ERR_KEY_NOT_FOUND;
                   1236:                        goto out;
                   1237:                }
                   1238:                sign_key = prv;
1.360     djm      1239:        }
1.341     djm      1240:  retry_pin:
1.360     djm      1241:        /* Prompt for touch for non-agent FIDO keys that request UP */
                   1242:        if (!is_agent && sshkey_is_sk(sign_key) &&
                   1243:            (sign_key->sk_flags & SSH_SK_USER_PRESENCE_REQD)) {
                   1244:                /* XXX should batch mode just skip these? */
                   1245:                if ((fp = sshkey_fingerprint(sign_key,
                   1246:                    options.fingerprint_hash, SSH_FP_DEFAULT)) == NULL)
                   1247:                        fatal_f("fingerprint failed");
                   1248:                notifier = notify_start(options.batch_mode,
                   1249:                    "Confirm user presence for key %s %s",
                   1250:                    sshkey_type(sign_key), fp);
                   1251:                free(fp);
1.272     djm      1252:        }
1.310     djm      1253:        if ((r = sshkey_sign(sign_key, sigp, lenp, data, datalen,
1.325     djm      1254:            alg, options.sk_provider, pin, compat)) != 0) {
1.331     djm      1255:                debug_fr(r, "sshkey_sign");
1.360     djm      1256:                if (!retried && pin == NULL && !is_agent &&
                   1257:                    sshkey_is_sk(sign_key) &&
1.341     djm      1258:                    r == SSH_ERR_KEY_WRONG_PASSPHRASE) {
                   1259:                        notify_complete(notifier, NULL);
                   1260:                        notifier = NULL;
1.360     djm      1261:                        xasprintf(&prompt, "Enter PIN for %s key %s: ",
                   1262:                            sshkey_type(sign_key), id->filename);
                   1263:                        pin = read_passphrase(prompt, 0);
1.341     djm      1264:                        retried = 1;
                   1265:                        goto retry_pin;
                   1266:                }
1.309     djm      1267:                goto out;
                   1268:        }
1.341     djm      1269:
1.309     djm      1270:        /*
                   1271:         * PKCS#11 tokens may not support all signature algorithms,
                   1272:         * so check what we get back.
                   1273:         */
                   1274:        if ((r = sshkey_check_sigtype(*sigp, *lenp, alg)) != 0) {
1.331     djm      1275:                debug_fr(r, "sshkey_check_sigtype");
1.309     djm      1276:                goto out;
1.265     djm      1277:        }
1.309     djm      1278:        /* success */
                   1279:        r = 0;
                   1280:  out:
1.325     djm      1281:        free(prompt);
                   1282:        if (pin != NULL)
                   1283:                freezero(pin, strlen(pin));
1.341     djm      1284:        notify_complete(notifier, r == 0 ? "User presence confirmed" : NULL);
1.214     djm      1285:        sshkey_free(prv);
1.269     djm      1286:        return r;
1.51      markus   1287: }
                   1288:
1.76      itojun   1289: static int
1.255     djm      1290: id_filename_matches(Identity *id, Identity *private_id)
                   1291: {
1.356     djm      1292:        static const char * const suffixes[] = { ".pub", "-cert.pub", NULL };
1.255     djm      1293:        size_t len = strlen(id->filename), plen = strlen(private_id->filename);
                   1294:        size_t i, slen;
                   1295:
                   1296:        if (strcmp(id->filename, private_id->filename) == 0)
                   1297:                return 1;
                   1298:        for (i = 0; suffixes[i]; i++) {
                   1299:                slen = strlen(suffixes[i]);
                   1300:                if (len > slen && plen == len - slen &&
                   1301:                    strcmp(id->filename + (len - slen), suffixes[i]) == 0 &&
                   1302:                    memcmp(id->filename, private_id->filename, plen) == 0)
                   1303:                        return 1;
                   1304:        }
                   1305:        return 0;
                   1306: }
                   1307:
                   1308: static int
1.295     djm      1309: sign_and_send_pubkey(struct ssh *ssh, Identity *id)
1.1       markus   1310: {
1.295     djm      1311:        Authctxt *authctxt = (Authctxt *)ssh->authctxt;
1.272     djm      1312:        struct sshbuf *b = NULL;
                   1313:        Identity *private_id, *sign_id = NULL;
                   1314:        u_char *signature = NULL;
                   1315:        size_t slen = 0, skip = 0;
                   1316:        int r, fallback_sigtype, sent = 0;
                   1317:        char *alg = NULL, *fp = NULL;
1.353     djm      1318:        const char *loc = "", *method = "publickey";
1.354     djm      1319:        int hostbound = 0;
1.353     djm      1320:
                   1321:        /* prefer host-bound pubkey signatures if supported by server */
1.354     djm      1322:        if ((ssh->kex->flags & KEX_HAS_PUBKEY_HOSTBOUND) != 0 &&
                   1323:            (options.pubkey_authentication & SSH_PUBKEY_AUTH_HBOUND) != 0) {
                   1324:                hostbound = 1;
1.353     djm      1325:                method = "publickey-hostbound-v00@openssh.com";
1.354     djm      1326:        }
1.1       markus   1327:
1.222     djm      1328:        if ((fp = sshkey_fingerprint(id->key, options.fingerprint_hash,
                   1329:            SSH_FP_DEFAULT)) == NULL)
                   1330:                return 0;
1.51      markus   1331:
1.354     djm      1332:        debug3_f("using %s with %s %s", method, sshkey_type(id->key), fp);
1.1       markus   1333:
1.227     djm      1334:        /*
                   1335:         * If the key is an certificate, try to find a matching private key
                   1336:         * and use it to complete the signature.
1.241     djm      1337:         * If no such private key exists, fall back to trying the certificate
                   1338:         * key itself in case it has a private half already loaded.
1.272     djm      1339:         * This will try to set sign_id to the private key that will perform
                   1340:         * the signature.
1.227     djm      1341:         */
1.272     djm      1342:        if (sshkey_is_cert(id->key)) {
1.227     djm      1343:                TAILQ_FOREACH(private_id, &authctxt->keys, next) {
                   1344:                        if (sshkey_equal_public(id->key, private_id->key) &&
                   1345:                            id->key->type != private_id->key->type) {
1.272     djm      1346:                                sign_id = private_id;
1.227     djm      1347:                                break;
                   1348:                        }
                   1349:                }
1.255     djm      1350:                /*
                   1351:                 * Exact key matches are preferred, but also allow
                   1352:                 * filename matches for non-PKCS#11/agent keys that
                   1353:                 * didn't load public keys. This supports the case
                   1354:                 * of keeping just a private key file and public
                   1355:                 * certificate on disk.
                   1356:                 */
1.272     djm      1357:                if (sign_id == NULL &&
                   1358:                    !id->isprivate && id->agent_fd == -1 &&
1.255     djm      1359:                    (id->key->flags & SSHKEY_FLAG_EXT) == 0) {
                   1360:                        TAILQ_FOREACH(private_id, &authctxt->keys, next) {
                   1361:                                if (private_id->key == NULL &&
                   1362:                                    id_filename_matches(id, private_id)) {
1.272     djm      1363:                                        sign_id = private_id;
1.255     djm      1364:                                        break;
                   1365:                                }
                   1366:                        }
                   1367:                }
1.272     djm      1368:                if (sign_id != NULL) {
1.331     djm      1369:                        debug2_f("using private key \"%s\"%s for "
1.349     djm      1370:                            "certificate", sign_id->filename,
                   1371:                            sign_id->agent_fd != -1 ? " from agent" : "");
1.227     djm      1372:                } else {
1.331     djm      1373:                        debug_f("no separate private key for certificate "
                   1374:                            "\"%s\"", id->filename);
1.227     djm      1375:                }
                   1376:        }
                   1377:
1.272     djm      1378:        /*
                   1379:         * If the above didn't select another identity to do the signing
                   1380:         * then default to the one we started with.
                   1381:         */
                   1382:        if (sign_id == NULL)
                   1383:                sign_id = id;
                   1384:
                   1385:        /* assemble and sign data */
                   1386:        for (fallback_sigtype = 0; fallback_sigtype <= 1; fallback_sigtype++) {
                   1387:                free(alg);
                   1388:                slen = 0;
                   1389:                signature = NULL;
                   1390:                if ((alg = key_sig_algorithm(fallback_sigtype ? NULL : ssh,
                   1391:                    id->key)) == NULL) {
1.331     djm      1392:                        error_f("no mutual signature supported");
1.272     djm      1393:                        goto out;
                   1394:                }
1.331     djm      1395:                debug3_f("signing using %s %s", alg, fp);
1.272     djm      1396:
                   1397:                sshbuf_free(b);
                   1398:                if ((b = sshbuf_new()) == NULL)
1.331     djm      1399:                        fatal_f("sshbuf_new failed");
1.345     djm      1400:                if (ssh->compat & SSH_OLD_SESSIONID) {
1.346     djm      1401:                        if ((r = sshbuf_putb(b, ssh->kex->session_id)) != 0)
                   1402:                                fatal_fr(r, "sshbuf_putb");
1.272     djm      1403:                } else {
1.346     djm      1404:                        if ((r = sshbuf_put_stringb(b,
                   1405:                            ssh->kex->session_id)) != 0)
                   1406:                                fatal_fr(r, "sshbuf_put_stringb");
1.272     djm      1407:                }
1.278     markus   1408:                skip = sshbuf_len(b);
1.272     djm      1409:                if ((r = sshbuf_put_u8(b, SSH2_MSG_USERAUTH_REQUEST)) != 0 ||
                   1410:                    (r = sshbuf_put_cstring(b, authctxt->server_user)) != 0 ||
                   1411:                    (r = sshbuf_put_cstring(b, authctxt->service)) != 0 ||
1.353     djm      1412:                    (r = sshbuf_put_cstring(b, method)) != 0 ||
1.272     djm      1413:                    (r = sshbuf_put_u8(b, 1)) != 0 ||
                   1414:                    (r = sshbuf_put_cstring(b, alg)) != 0 ||
                   1415:                    (r = sshkey_puts(id->key, b)) != 0) {
1.331     djm      1416:                        fatal_fr(r, "assemble signed data");
1.272     djm      1417:                }
1.354     djm      1418:                if (hostbound) {
1.353     djm      1419:                        if (ssh->kex->initial_hostkey == NULL) {
                   1420:                                fatal_f("internal error: initial hostkey "
                   1421:                                    "not recorded");
                   1422:                        }
                   1423:                        if ((r = sshkey_puts(ssh->kex->initial_hostkey, b)) != 0)
                   1424:                                fatal_fr(r, "assemble %s hostkey", method);
                   1425:                }
1.272     djm      1426:                /* generate signature */
                   1427:                r = identity_sign(sign_id, &signature, &slen,
1.345     djm      1428:                    sshbuf_ptr(b), sshbuf_len(b), ssh->compat, alg);
1.272     djm      1429:                if (r == 0)
                   1430:                        break;
                   1431:                else if (r == SSH_ERR_KEY_NOT_FOUND)
                   1432:                        goto out; /* soft failure */
                   1433:                else if (r == SSH_ERR_SIGN_ALG_UNSUPPORTED &&
                   1434:                    !fallback_sigtype) {
                   1435:                        if (sign_id->agent_fd != -1)
                   1436:                                loc = "agent ";
                   1437:                        else if ((sign_id->key->flags & SSHKEY_FLAG_EXT) != 0)
                   1438:                                loc = "token ";
                   1439:                        logit("%skey %s %s returned incorrect signature type",
                   1440:                            loc, sshkey_type(id->key), fp);
                   1441:                        continue;
                   1442:                }
1.331     djm      1443:                error_fr(r, "signing failed for %s \"%s\"%s",
1.315     djm      1444:                    sshkey_type(sign_id->key), sign_id->filename,
1.331     djm      1445:                    id->agent_fd != -1 ? " from agent" : "");
1.272     djm      1446:                goto out;
1.17      markus   1447:        }
1.272     djm      1448:        if (slen == 0 || signature == NULL) /* shouldn't happen */
1.331     djm      1449:                fatal_f("no signature");
1.51      markus   1450:
1.1       markus   1451:        /* append signature */
1.272     djm      1452:        if ((r = sshbuf_put_string(b, signature, slen)) != 0)
1.331     djm      1453:                fatal_fr(r, "append signature");
1.1       markus   1454:
1.272     djm      1455: #ifdef DEBUG_PK
                   1456:        sshbuf_dump(b, stderr);
                   1457: #endif
1.1       markus   1458:        /* skip session id and packet type */
1.272     djm      1459:        if ((r = sshbuf_consume(b, skip + 1)) != 0)
1.331     djm      1460:                fatal_fr(r, "consume");
1.1       markus   1461:
                   1462:        /* put remaining data from buffer into packet */
1.272     djm      1463:        if ((r = sshpkt_start(ssh, SSH2_MSG_USERAUTH_REQUEST)) != 0 ||
                   1464:            (r = sshpkt_putb(ssh, b)) != 0 ||
                   1465:            (r = sshpkt_send(ssh)) != 0)
1.331     djm      1466:                fatal_fr(r, "enqueue request");
1.272     djm      1467:
                   1468:        /* success */
                   1469:        sent = 1;
1.17      markus   1470:
1.272     djm      1471:  out:
                   1472:        free(fp);
                   1473:        free(alg);
                   1474:        sshbuf_free(b);
                   1475:        freezero(signature, slen);
                   1476:        return sent;
1.16      markus   1477: }
                   1478:
1.76      itojun   1479: static int
1.295     djm      1480: send_pubkey_test(struct ssh *ssh, Identity *id)
1.20      markus   1481: {
1.295     djm      1482:        Authctxt *authctxt = (Authctxt *)ssh->authctxt;
1.272     djm      1483:        u_char *blob = NULL;
                   1484:        char *alg = NULL;
1.278     markus   1485:        size_t bloblen;
                   1486:        u_int have_sig = 0;
                   1487:        int sent = 0, r;
1.20      markus   1488:
1.272     djm      1489:        if ((alg = key_sig_algorithm(ssh, id->key)) == NULL) {
1.331     djm      1490:                debug_f("no mutual signature algorithm");
1.272     djm      1491:                goto out;
                   1492:        }
1.16      markus   1493:
1.278     markus   1494:        if ((r = sshkey_to_blob(id->key, &blob, &bloblen)) != 0) {
1.51      markus   1495:                /* we cannot handle this key */
1.331     djm      1496:                debug3_f("cannot handle key");
1.272     djm      1497:                goto out;
1.16      markus   1498:        }
1.51      markus   1499:        /* register callback for USERAUTH_PK_OK message */
1.278     markus   1500:        ssh_dispatch_set(ssh, SSH2_MSG_USERAUTH_PK_OK, &input_userauth_pk_ok);
1.51      markus   1501:
1.278     markus   1502:        if ((r = sshpkt_start(ssh, SSH2_MSG_USERAUTH_REQUEST)) != 0 ||
                   1503:            (r = sshpkt_put_cstring(ssh, authctxt->server_user)) != 0 ||
                   1504:            (r = sshpkt_put_cstring(ssh, authctxt->service)) != 0 ||
                   1505:            (r = sshpkt_put_cstring(ssh, authctxt->method->name)) != 0 ||
                   1506:            (r = sshpkt_put_u8(ssh, have_sig)) != 0 ||
                   1507:            (r = sshpkt_put_cstring(ssh, alg)) != 0 ||
                   1508:            (r = sshpkt_put_string(ssh, blob, bloblen)) != 0 ||
                   1509:            (r = sshpkt_send(ssh)) != 0)
1.331     djm      1510:                fatal_fr(r, "send packet");
1.272     djm      1511:        sent = 1;
1.278     markus   1512:
                   1513:  out:
1.272     djm      1514:        free(alg);
1.197     djm      1515:        free(blob);
1.272     djm      1516:        return sent;
1.51      markus   1517: }
                   1518:
1.259     markus   1519: static struct sshkey *
1.229     jcs      1520: load_identity_file(Identity *id)
1.51      markus   1521: {
1.259     markus   1522:        struct sshkey *private = NULL;
1.229     jcs      1523:        char prompt[300], *passphrase, *comment;
1.308     dtucker  1524:        int r, quit = 0, i;
1.52      markus   1525:        struct stat st;
1.16      markus   1526:
1.306     deraadt  1527:        if (stat(id->filename, &st) == -1) {
1.330     djm      1528:                do_log2(id->userprovided ?
                   1529:                    SYSLOG_LEVEL_INFO : SYSLOG_LEVEL_DEBUG3,
                   1530:                    "no such identity: %s: %s", id->filename, strerror(errno));
1.52      markus   1531:                return NULL;
                   1532:        }
1.214     djm      1533:        snprintf(prompt, sizeof prompt,
1.229     jcs      1534:            "Enter passphrase for key '%.100s': ", id->filename);
1.214     djm      1535:        for (i = 0; i <= options.number_of_password_prompts; i++) {
                   1536:                if (i == 0)
                   1537:                        passphrase = "";
                   1538:                else {
1.20      markus   1539:                        passphrase = read_passphrase(prompt, 0);
1.214     djm      1540:                        if (*passphrase == '\0') {
1.20      markus   1541:                                debug2("no passphrase given, try next key");
1.214     djm      1542:                                free(passphrase);
                   1543:                                break;
                   1544:                        }
                   1545:                }
1.229     jcs      1546:                switch ((r = sshkey_load_private_type(KEY_UNSPEC, id->filename,
1.308     dtucker  1547:                    passphrase, &private, &comment))) {
1.214     djm      1548:                case 0:
                   1549:                        break;
                   1550:                case SSH_ERR_KEY_WRONG_PASSPHRASE:
                   1551:                        if (options.batch_mode) {
                   1552:                                quit = 1;
                   1553:                                break;
                   1554:                        }
1.215     djm      1555:                        if (i != 0)
                   1556:                                debug2("bad passphrase given, try again...");
1.214     djm      1557:                        break;
                   1558:                case SSH_ERR_SYSTEM_ERROR:
                   1559:                        if (errno == ENOENT) {
1.331     djm      1560:                                debug2_r(r, "Load key \"%s\"", id->filename);
1.51      markus   1561:                                quit = 1;
1.214     djm      1562:                                break;
1.20      markus   1563:                        }
1.214     djm      1564:                        /* FALLTHROUGH */
                   1565:                default:
1.331     djm      1566:                        error_r(r, "Load key \"%s\"", id->filename);
1.214     djm      1567:                        quit = 1;
                   1568:                        break;
                   1569:                }
1.311     markus   1570:                if (private != NULL && sshkey_is_sk(private) &&
1.309     djm      1571:                    options.sk_provider == NULL) {
1.319     naddy    1572:                        debug("key \"%s\" is an authenticator-hosted key, "
                   1573:                            "but no provider specified", id->filename);
1.309     djm      1574:                        sshkey_free(private);
                   1575:                        private = NULL;
                   1576:                        quit = 1;
                   1577:                }
1.361     djm      1578:                if (!quit && (r = sshkey_check_rsa_length(private,
                   1579:                    options.required_rsa_size)) != 0) {
                   1580:                        debug_fr(r, "Skipping key %s", id->filename);
                   1581:                        sshkey_free(private);
                   1582:                        private = NULL;
                   1583:                        quit = 1;
                   1584:                }
1.230     djm      1585:                if (!quit && private != NULL && id->agent_fd == -1 &&
1.229     jcs      1586:                    !(id->key && id->isprivate))
                   1587:                        maybe_add_key_to_agent(id->filename, private, comment,
                   1588:                            passphrase);
1.278     markus   1589:                if (i > 0)
                   1590:                        freezero(passphrase, strlen(passphrase));
1.232     mmcc     1591:                free(comment);
1.214     djm      1592:                if (private != NULL || quit)
                   1593:                        break;
1.16      markus   1594:        }
1.51      markus   1595:        return private;
                   1596: }
                   1597:
1.272     djm      1598: static int
                   1599: key_type_allowed_by_config(struct sshkey *key)
                   1600: {
                   1601:        if (match_pattern_list(sshkey_ssh_name(key),
1.342     dtucker  1602:            options.pubkey_accepted_algos, 0) == 1)
1.272     djm      1603:                return 1;
                   1604:
                   1605:        /* RSA keys/certs might be allowed by alternate signature types */
                   1606:        switch (key->type) {
                   1607:        case KEY_RSA:
                   1608:                if (match_pattern_list("rsa-sha2-512",
1.342     dtucker  1609:                    options.pubkey_accepted_algos, 0) == 1)
1.272     djm      1610:                        return 1;
                   1611:                if (match_pattern_list("rsa-sha2-256",
1.342     dtucker  1612:                    options.pubkey_accepted_algos, 0) == 1)
1.272     djm      1613:                        return 1;
                   1614:                break;
                   1615:        case KEY_RSA_CERT:
                   1616:                if (match_pattern_list("rsa-sha2-512-cert-v01@openssh.com",
1.342     dtucker  1617:                    options.pubkey_accepted_algos, 0) == 1)
1.272     djm      1618:                        return 1;
                   1619:                if (match_pattern_list("rsa-sha2-256-cert-v01@openssh.com",
1.342     dtucker  1620:                    options.pubkey_accepted_algos, 0) == 1)
1.272     djm      1621:                        return 1;
                   1622:                break;
                   1623:        }
                   1624:        return 0;
                   1625: }
                   1626:
1.352     djm      1627: /* obtain a list of keys from the agent */
                   1628: static int
                   1629: get_agent_identities(struct ssh *ssh, int *agent_fdp,
                   1630:     struct ssh_identitylist **idlistp)
                   1631: {
                   1632:        int r, agent_fd;
                   1633:        struct ssh_identitylist *idlist;
                   1634:
                   1635:        if ((r = ssh_get_authentication_socket(&agent_fd)) != 0) {
                   1636:                if (r != SSH_ERR_AGENT_NOT_PRESENT)
                   1637:                        debug_fr(r, "ssh_get_authentication_socket");
                   1638:                return r;
                   1639:        }
                   1640:        if ((r = ssh_agent_bind_hostkey(agent_fd, ssh->kex->initial_hostkey,
                   1641:            ssh->kex->session_id, ssh->kex->initial_sig, 0)) == 0)
                   1642:                debug_f("bound agent to hostkey");
                   1643:        else
                   1644:                debug2_fr(r, "ssh_agent_bind_hostkey");
                   1645:
                   1646:        if ((r = ssh_fetch_identitylist(agent_fd, &idlist)) != 0) {
                   1647:                debug_fr(r, "ssh_fetch_identitylist");
                   1648:                close(agent_fd);
                   1649:                return r;
                   1650:        }
                   1651:        /* success */
                   1652:        *agent_fdp = agent_fd;
                   1653:        *idlistp = idlist;
                   1654:        debug_f("agent returned %zu keys", idlist->nkeys);
                   1655:        return 0;
                   1656: }
1.272     djm      1657:
1.117     markus   1658: /*
                   1659:  * try keys in the following order:
1.332     djm      1660:  *     1. certificates listed in the config file
                   1661:  *     2. other input certificates
1.227     djm      1662:  *     3. agent keys that are found in the config file
                   1663:  *     4. other agent keys
                   1664:  *     5. keys that are only listed in the config file
1.117     markus   1665:  */
                   1666: static void
1.352     djm      1667: pubkey_prepare(struct ssh *ssh, Authctxt *authctxt)
1.51      markus   1668: {
1.214     djm      1669:        struct identity *id, *id2, *tmp;
                   1670:        struct idlist agent, files, *preferred;
                   1671:        struct sshkey *key;
1.230     djm      1672:        int agent_fd = -1, i, r, found;
1.214     djm      1673:        size_t j;
                   1674:        struct ssh_identitylist *idlist;
1.287     djm      1675:        char *ident;
1.51      markus   1676:
1.117     markus   1677:        TAILQ_INIT(&agent);     /* keys from the agent */
                   1678:        TAILQ_INIT(&files);     /* keys from the config file */
                   1679:        preferred = &authctxt->keys;
                   1680:        TAILQ_INIT(preferred);  /* preferred order of keys */
                   1681:
1.190     djm      1682:        /* list of keys stored in the filesystem and PKCS#11 */
1.117     markus   1683:        for (i = 0; i < options.num_identity_files; i++) {
                   1684:                key = options.identity_keys[i];
1.309     djm      1685:                if (key && key->cert &&
                   1686:                    key->cert->type != SSH2_CERT_TYPE_USER) {
1.331     djm      1687:                        debug_f("ignoring certificate %s: not a user "
                   1688:                            "certificate", options.identity_files[i]);
1.309     djm      1689:                        continue;
                   1690:                }
1.311     markus   1691:                if (key && sshkey_is_sk(key) && options.sk_provider == NULL) {
1.331     djm      1692:                        debug_f("ignoring authenticator-hosted key %s as no "
1.309     djm      1693:                            "SecurityKeyProvider has been specified",
1.331     djm      1694:                            options.identity_files[i]);
1.117     markus   1695:                        continue;
1.309     djm      1696:                }
1.117     markus   1697:                options.identity_keys[i] = NULL;
1.150     djm      1698:                id = xcalloc(1, sizeof(*id));
1.230     djm      1699:                id->agent_fd = -1;
1.117     markus   1700:                id->key = key;
                   1701:                id->filename = xstrdup(options.identity_files[i]);
1.192     dtucker  1702:                id->userprovided = options.identity_file_userprovided[i];
1.117     markus   1703:                TAILQ_INSERT_TAIL(&files, id, next);
1.190     djm      1704:        }
1.227     djm      1705:        /* list of certificates specified by user */
                   1706:        for (i = 0; i < options.num_certificate_files; i++) {
                   1707:                key = options.certificates[i];
1.278     markus   1708:                if (!sshkey_is_cert(key) || key->cert == NULL ||
1.309     djm      1709:                    key->cert->type != SSH2_CERT_TYPE_USER) {
1.331     djm      1710:                        debug_f("ignoring certificate %s: not a user "
                   1711:                            "certificate", options.identity_files[i]);
1.227     djm      1712:                        continue;
1.309     djm      1713:                }
1.311     markus   1714:                if (key && sshkey_is_sk(key) && options.sk_provider == NULL) {
1.331     djm      1715:                        debug_f("ignoring authenticator-hosted key "
1.319     naddy    1716:                            "certificate %s as no "
1.309     djm      1717:                            "SecurityKeyProvider has been specified",
1.331     djm      1718:                            options.identity_files[i]);
1.309     djm      1719:                        continue;
                   1720:                }
1.227     djm      1721:                id = xcalloc(1, sizeof(*id));
1.230     djm      1722:                id->agent_fd = -1;
1.227     djm      1723:                id->key = key;
                   1724:                id->filename = xstrdup(options.certificate_files[i]);
                   1725:                id->userprovided = options.certificate_file_userprovided[i];
                   1726:                TAILQ_INSERT_TAIL(preferred, id, next);
1.117     markus   1727:        }
                   1728:        /* list of keys supported by the agent */
1.352     djm      1729:        if ((r = get_agent_identities(ssh, &agent_fd, &idlist)) == 0) {
1.214     djm      1730:                for (j = 0; j < idlist->nkeys; j++) {
1.361     djm      1731:                        if ((r = sshkey_check_rsa_length(idlist->keys[j],
                   1732:                            options.required_rsa_size)) != 0) {
                   1733:                                debug_fr(r, "ignoring %s agent key",
                   1734:                                    sshkey_ssh_name(idlist->keys[j]));
                   1735:                                continue;
                   1736:                        }
1.117     markus   1737:                        found = 0;
                   1738:                        TAILQ_FOREACH(id, &files, next) {
1.214     djm      1739:                                /*
                   1740:                                 * agent keys from the config file are
                   1741:                                 * preferred
                   1742:                                 */
                   1743:                                if (sshkey_equal(idlist->keys[j], id->key)) {
1.117     markus   1744:                                        TAILQ_REMOVE(&files, id, next);
                   1745:                                        TAILQ_INSERT_TAIL(preferred, id, next);
1.214     djm      1746:                                        id->agent_fd = agent_fd;
1.117     markus   1747:                                        found = 1;
                   1748:                                        break;
                   1749:                                }
                   1750:                        }
1.135     markus   1751:                        if (!found && !options.identities_only) {
1.150     djm      1752:                                id = xcalloc(1, sizeof(*id));
1.214     djm      1753:                                /* XXX "steals" key/comment from idlist */
                   1754:                                id->key = idlist->keys[j];
                   1755:                                id->filename = idlist->comments[j];
                   1756:                                idlist->keys[j] = NULL;
                   1757:                                idlist->comments[j] = NULL;
                   1758:                                id->agent_fd = agent_fd;
1.117     markus   1759:                                TAILQ_INSERT_TAIL(&agent, id, next);
                   1760:                        }
                   1761:                }
1.214     djm      1762:                ssh_free_identitylist(idlist);
1.117     markus   1763:                /* append remaining agent keys */
1.324     bket     1764:                TAILQ_CONCAT(preferred, &agent, next);
1.214     djm      1765:                authctxt->agent_fd = agent_fd;
1.244     djm      1766:        }
                   1767:        /* Prefer PKCS11 keys that are explicitly listed */
                   1768:        TAILQ_FOREACH_SAFE(id, &files, next, tmp) {
                   1769:                if (id->key == NULL || (id->key->flags & SSHKEY_FLAG_EXT) == 0)
                   1770:                        continue;
                   1771:                found = 0;
                   1772:                TAILQ_FOREACH(id2, &files, next) {
                   1773:                        if (id2->key == NULL ||
1.321     djm      1774:                            (id2->key->flags & SSHKEY_FLAG_EXT) != 0)
1.244     djm      1775:                                continue;
                   1776:                        if (sshkey_equal(id->key, id2->key)) {
                   1777:                                TAILQ_REMOVE(&files, id, next);
                   1778:                                TAILQ_INSERT_TAIL(preferred, id, next);
                   1779:                                found = 1;
                   1780:                                break;
                   1781:                        }
                   1782:                }
                   1783:                /* If IdentitiesOnly set and key not found then don't use it */
                   1784:                if (!found && options.identities_only) {
                   1785:                        TAILQ_REMOVE(&files, id, next);
1.278     markus   1786:                        freezero(id, sizeof(*id));
1.244     djm      1787:                }
1.117     markus   1788:        }
                   1789:        /* append remaining keys from the config file */
1.324     bket     1790:        TAILQ_CONCAT(preferred, &files, next);
1.342     dtucker  1791:        /* finally, filter by PubkeyAcceptedAlgorithms */
1.228     djm      1792:        TAILQ_FOREACH_SAFE(id, preferred, next, id2) {
1.273     djm      1793:                if (id->key != NULL && !key_type_allowed_by_config(id->key)) {
1.228     djm      1794:                        debug("Skipping %s key %s - "
1.342     dtucker  1795:                            "corresponding algo not in PubkeyAcceptedAlgorithms",
1.228     djm      1796:                            sshkey_ssh_name(id->key), id->filename);
                   1797:                        TAILQ_REMOVE(preferred, id, next);
                   1798:                        sshkey_free(id->key);
                   1799:                        free(id->filename);
                   1800:                        memset(id, 0, sizeof(*id));
                   1801:                        continue;
                   1802:                }
1.117     markus   1803:        }
1.287     djm      1804:        /* List the keys we plan on using */
                   1805:        TAILQ_FOREACH_SAFE(id, preferred, next, id2) {
                   1806:                ident = format_identity(id);
                   1807:                debug("Will attempt key: %s", ident);
                   1808:                free(ident);
                   1809:        }
1.331     djm      1810:        debug2_f("done");
1.17      markus   1811: }
                   1812:
1.117     markus   1813: static void
1.303     djm      1814: pubkey_cleanup(struct ssh *ssh)
1.51      markus   1815: {
1.302     djm      1816:        Authctxt *authctxt = (Authctxt *)ssh->authctxt;
1.117     markus   1817:        Identity *id;
1.51      markus   1818:
1.289     djm      1819:        if (authctxt->agent_fd != -1) {
1.214     djm      1820:                ssh_close_authentication_socket(authctxt->agent_fd);
1.289     djm      1821:                authctxt->agent_fd = -1;
                   1822:        }
1.117     markus   1823:        for (id = TAILQ_FIRST(&authctxt->keys); id;
                   1824:            id = TAILQ_FIRST(&authctxt->keys)) {
                   1825:                TAILQ_REMOVE(&authctxt->keys, id, next);
1.235     mmcc     1826:                sshkey_free(id->key);
1.197     djm      1827:                free(id->filename);
                   1828:                free(id);
1.117     markus   1829:        }
1.1       markus   1830: }
                   1831:
1.251     djm      1832: static void
                   1833: pubkey_reset(Authctxt *authctxt)
                   1834: {
                   1835:        Identity *id;
                   1836:
                   1837:        TAILQ_FOREACH(id, &authctxt->keys, next)
                   1838:                id->tried = 0;
                   1839: }
                   1840:
1.225     markus   1841: static int
1.295     djm      1842: userauth_pubkey(struct ssh *ssh)
1.20      markus   1843: {
1.295     djm      1844:        Authctxt *authctxt = (Authctxt *)ssh->authctxt;
1.117     markus   1845:        Identity *id;
1.20      markus   1846:        int sent = 0;
1.287     djm      1847:        char *ident;
1.20      markus   1848:
1.117     markus   1849:        while ((id = TAILQ_FIRST(&authctxt->keys))) {
                   1850:                if (id->tried++)
                   1851:                        return (0);
1.127     markus   1852:                /* move key to the end of the queue */
1.117     markus   1853:                TAILQ_REMOVE(&authctxt->keys, id, next);
                   1854:                TAILQ_INSERT_TAIL(&authctxt->keys, id, next);
                   1855:                /*
                   1856:                 * send a test message if we have the public key. for
                   1857:                 * encrypted keys we cannot do this and have to load the
                   1858:                 * private key instead
                   1859:                 */
1.200     djm      1860:                if (id->key != NULL) {
1.362     dtucker  1861:                        if (id->key != NULL) {
1.287     djm      1862:                                ident = format_identity(id);
                   1863:                                debug("Offering public key: %s", ident);
                   1864:                                free(ident);
1.295     djm      1865:                                sent = send_pubkey_test(ssh, id);
1.200     djm      1866:                        }
                   1867:                } else {
1.117     markus   1868:                        debug("Trying private key: %s", id->filename);
1.229     jcs      1869:                        id->key = load_identity_file(id);
1.117     markus   1870:                        if (id->key != NULL) {
1.362     dtucker  1871:                                if (id->key != NULL) {
1.225     markus   1872:                                        id->isprivate = 1;
1.295     djm      1873:                                        sent = sign_and_send_pubkey(ssh, id);
1.200     djm      1874:                                }
1.278     markus   1875:                                sshkey_free(id->key);
1.117     markus   1876:                                id->key = NULL;
1.251     djm      1877:                                id->isprivate = 0;
1.51      markus   1878:                        }
                   1879:                }
1.117     markus   1880:                if (sent)
                   1881:                        return (sent);
1.28      markus   1882:        }
1.117     markus   1883:        return (0);
1.20      markus   1884: }
                   1885:
1.23      markus   1886: /*
                   1887:  * Send userauth request message specifying keyboard-interactive method.
                   1888:  */
1.302     djm      1889: static int
1.295     djm      1890: userauth_kbdint(struct ssh *ssh)
1.23      markus   1891: {
1.295     djm      1892:        Authctxt *authctxt = (Authctxt *)ssh->authctxt;
1.278     markus   1893:        int r;
1.23      markus   1894:
1.292     djm      1895:        if (authctxt->attempt_kbdint++ >= options.number_of_password_prompts)
1.23      markus   1896:                return 0;
1.82      markus   1897:        /* disable if no SSH2_MSG_USERAUTH_INFO_REQUEST has been seen */
1.292     djm      1898:        if (authctxt->attempt_kbdint > 1 && !authctxt->info_req_seen) {
1.82      markus   1899:                debug3("userauth_kbdint: disable: no info_req_seen");
1.278     markus   1900:                ssh_dispatch_set(ssh, SSH2_MSG_USERAUTH_INFO_REQUEST, NULL);
1.82      markus   1901:                return 0;
                   1902:        }
1.23      markus   1903:
                   1904:        debug2("userauth_kbdint");
1.278     markus   1905:        if ((r = sshpkt_start(ssh, SSH2_MSG_USERAUTH_REQUEST)) != 0 ||
                   1906:            (r = sshpkt_put_cstring(ssh, authctxt->server_user)) != 0 ||
                   1907:            (r = sshpkt_put_cstring(ssh, authctxt->service)) != 0 ||
                   1908:            (r = sshpkt_put_cstring(ssh, authctxt->method->name)) != 0 ||
                   1909:            (r = sshpkt_put_cstring(ssh, "")) != 0 ||           /* lang */
                   1910:            (r = sshpkt_put_cstring(ssh, options.kbd_interactive_devices ?
                   1911:            options.kbd_interactive_devices : "")) != 0 ||
                   1912:            (r = sshpkt_send(ssh)) != 0)
1.331     djm      1913:                fatal_fr(r, "send packet");
1.23      markus   1914:
1.278     markus   1915:        ssh_dispatch_set(ssh, SSH2_MSG_USERAUTH_INFO_REQUEST, &input_userauth_info_req);
1.23      markus   1916:        return 1;
                   1917: }
                   1918:
                   1919: /*
1.46      markus   1920:  * parse INFO_REQUEST, prompt user and send INFO_RESPONSE
1.23      markus   1921:  */
1.302     djm      1922: static int
1.261     markus   1923: input_userauth_info_req(int type, u_int32_t seq, struct ssh *ssh)
1.23      markus   1924: {
1.260     markus   1925:        Authctxt *authctxt = ssh->authctxt;
1.278     markus   1926:        char *name = NULL, *inst = NULL, *lang = NULL, *prompt = NULL;
1.335     djm      1927:        char *display_prompt = NULL, *response = NULL;
1.278     markus   1928:        u_char echo = 0;
1.32      markus   1929:        u_int num_prompts, i;
1.278     markus   1930:        int r;
1.23      markus   1931:
1.335     djm      1932:        debug2_f("entering");
1.23      markus   1933:
                   1934:        if (authctxt == NULL)
1.335     djm      1935:                fatal_f("no authentication context");
1.82      markus   1936:
                   1937:        authctxt->info_req_seen = 1;
1.23      markus   1938:
1.278     markus   1939:        if ((r = sshpkt_get_cstring(ssh, &name, NULL)) != 0 ||
                   1940:            (r = sshpkt_get_cstring(ssh, &inst, NULL)) != 0 ||
                   1941:            (r = sshpkt_get_cstring(ssh, &lang, NULL)) != 0)
                   1942:                goto out;
1.23      markus   1943:        if (strlen(name) > 0)
1.116     itojun   1944:                logit("%s", name);
1.23      markus   1945:        if (strlen(inst) > 0)
1.116     itojun   1946:                logit("%s", inst);
1.23      markus   1947:
1.278     markus   1948:        if ((r = sshpkt_get_u32(ssh, &num_prompts)) != 0)
                   1949:                goto out;
1.23      markus   1950:        /*
                   1951:         * Begin to build info response packet based on prompts requested.
                   1952:         * We commit to providing the correct number of responses, so if
                   1953:         * further on we run into a problem that prevents this, we have to
                   1954:         * be sure and clean this up and send a correct error response.
                   1955:         */
1.278     markus   1956:        if ((r = sshpkt_start(ssh, SSH2_MSG_USERAUTH_INFO_RESPONSE)) != 0 ||
                   1957:            (r = sshpkt_put_u32(ssh, num_prompts)) != 0)
                   1958:                goto out;
1.23      markus   1959:
1.335     djm      1960:        debug2_f("num_prompts %d", num_prompts);
1.23      markus   1961:        for (i = 0; i < num_prompts; i++) {
1.278     markus   1962:                if ((r = sshpkt_get_cstring(ssh, &prompt, NULL)) != 0 ||
                   1963:                    (r = sshpkt_get_u8(ssh, &echo)) != 0)
                   1964:                        goto out;
1.336     djm      1965:                if (asmprintf(&display_prompt, INT_MAX, NULL, "(%s@%s) %s",
1.335     djm      1966:                    authctxt->server_user, options.host_key_alias ?
1.336     djm      1967:                    options.host_key_alias : authctxt->host, prompt) == -1)
                   1968:                        fatal_f("asmprintf failed");
1.335     djm      1969:                response = read_passphrase(display_prompt, echo ? RP_ECHO : 0);
1.278     markus   1970:                if ((r = sshpkt_put_cstring(ssh, response)) != 0)
                   1971:                        goto out;
                   1972:                freezero(response, strlen(response));
1.197     djm      1973:                free(prompt);
1.335     djm      1974:                free(display_prompt);
                   1975:                display_prompt = response = prompt = NULL;
1.23      markus   1976:        }
1.278     markus   1977:        /* done with parsing incoming message. */
                   1978:        if ((r = sshpkt_get_end(ssh)) != 0 ||
                   1979:            (r = sshpkt_add_padding(ssh, 64)) != 0)
                   1980:                goto out;
                   1981:        r = sshpkt_send(ssh);
                   1982:  out:
                   1983:        if (response)
                   1984:                freezero(response, strlen(response));
                   1985:        free(prompt);
1.335     djm      1986:        free(display_prompt);
1.278     markus   1987:        free(name);
                   1988:        free(inst);
                   1989:        free(lang);
                   1990:        return r;
1.68      markus   1991: }
                   1992:
1.100     markus   1993: static int
1.294     djm      1994: ssh_keysign(struct ssh *ssh, struct sshkey *key, u_char **sigp, size_t *lenp,
1.223     djm      1995:     const u_char *data, size_t datalen)
1.100     markus   1996: {
1.223     djm      1997:        struct sshbuf *b;
1.101     markus   1998:        struct stat st;
1.100     markus   1999:        pid_t pid;
1.304     deraadt  2000:        int r, to[2], from[2], status;
1.295     djm      2001:        int sock = ssh_packet_get_connection_in(ssh);
1.223     djm      2002:        u_char rversion = 0, version = 2;
                   2003:        void (*osigchld)(int);
1.100     markus   2004:
1.223     djm      2005:        *sigp = NULL;
                   2006:        *lenp = 0;
1.100     markus   2007:
1.306     deraadt  2008:        if (stat(_PATH_SSH_KEY_SIGN, &st) == -1) {
1.331     djm      2009:                error_f("not installed: %s", strerror(errno));
1.223     djm      2010:                return -1;
                   2011:        }
                   2012:        if (fflush(stdout) != 0) {
1.331     djm      2013:                error_f("fflush: %s", strerror(errno));
1.101     markus   2014:                return -1;
                   2015:        }
1.306     deraadt  2016:        if (pipe(to) == -1) {
1.331     djm      2017:                error_f("pipe: %s", strerror(errno));
1.100     markus   2018:                return -1;
                   2019:        }
1.306     deraadt  2020:        if (pipe(from) == -1) {
1.331     djm      2021:                error_f("pipe: %s", strerror(errno));
1.100     markus   2022:                return -1;
                   2023:        }
1.306     deraadt  2024:        if ((pid = fork()) == -1) {
1.331     djm      2025:                error_f("fork: %s", strerror(errno));
1.100     markus   2026:                return -1;
                   2027:        }
1.317     dtucker  2028:        osigchld = ssh_signal(SIGCHLD, SIG_DFL);
1.100     markus   2029:        if (pid == 0) {
                   2030:                close(from[0]);
1.306     deraadt  2031:                if (dup2(from[1], STDOUT_FILENO) == -1)
1.331     djm      2032:                        fatal_f("dup2: %s", strerror(errno));
1.100     markus   2033:                close(to[1]);
1.306     deraadt  2034:                if (dup2(to[0], STDIN_FILENO) == -1)
1.331     djm      2035:                        fatal_f("dup2: %s", strerror(errno));
1.103     markus   2036:                close(from[1]);
                   2037:                close(to[0]);
1.304     deraadt  2038:
1.306     deraadt  2039:                if (dup2(sock, STDERR_FILENO + 1) == -1)
1.331     djm      2040:                        fatal_f("dup2: %s", strerror(errno));
1.304     deraadt  2041:                sock = STDERR_FILENO + 1;
1.363     dtucker  2042:                if (fcntl(sock, F_SETFD, 0) == -1) /* keep the socket on exec */
                   2043:                        debug3_f("fcntl F_SETFD: %s", strerror(errno));
1.223     djm      2044:                closefrom(sock + 1);
1.304     deraadt  2045:
1.331     djm      2046:                debug3_f("[child] pid=%ld, exec %s",
                   2047:                    (long)getpid(), _PATH_SSH_KEY_SIGN);
1.233     mmcc     2048:                execl(_PATH_SSH_KEY_SIGN, _PATH_SSH_KEY_SIGN, (char *)NULL);
1.331     djm      2049:                fatal_f("exec(%s): %s", _PATH_SSH_KEY_SIGN,
1.100     markus   2050:                    strerror(errno));
                   2051:        }
                   2052:        close(from[1]);
                   2053:        close(to[0]);
1.305     djm      2054:        sock = STDERR_FILENO + 1;
1.100     markus   2055:
1.223     djm      2056:        if ((b = sshbuf_new()) == NULL)
1.331     djm      2057:                fatal_f("sshbuf_new failed");
1.223     djm      2058:        /* send # of sock, data to be signed */
1.253     djm      2059:        if ((r = sshbuf_put_u32(b, sock)) != 0 ||
1.223     djm      2060:            (r = sshbuf_put_string(b, data, datalen)) != 0)
1.331     djm      2061:                fatal_fr(r, "buffer error");
1.223     djm      2062:        if (ssh_msg_send(to[1], version, b) == -1)
1.331     djm      2063:                fatal_f("couldn't send request");
1.223     djm      2064:        sshbuf_reset(b);
                   2065:        r = ssh_msg_recv(from[0], b);
1.101     markus   2066:        close(from[0]);
                   2067:        close(to[1]);
1.223     djm      2068:        if (r < 0) {
1.331     djm      2069:                error_f("no reply");
1.223     djm      2070:                goto fail;
                   2071:        }
1.101     markus   2072:
1.223     djm      2073:        errno = 0;
1.306     deraadt  2074:        while (waitpid(pid, &status, 0) == -1) {
1.223     djm      2075:                if (errno != EINTR) {
1.331     djm      2076:                        error_f("waitpid %ld: %s", (long)pid, strerror(errno));
1.223     djm      2077:                        goto fail;
                   2078:                }
                   2079:        }
                   2080:        if (!WIFEXITED(status)) {
1.331     djm      2081:                error_f("exited abnormally");
1.223     djm      2082:                goto fail;
                   2083:        }
                   2084:        if (WEXITSTATUS(status) != 0) {
1.331     djm      2085:                error_f("exited with status %d", WEXITSTATUS(status));
1.223     djm      2086:                goto fail;
                   2087:        }
                   2088:        if ((r = sshbuf_get_u8(b, &rversion)) != 0) {
1.331     djm      2089:                error_fr(r, "buffer error");
1.223     djm      2090:                goto fail;
                   2091:        }
                   2092:        if (rversion != version) {
1.331     djm      2093:                error_f("bad version");
1.223     djm      2094:                goto fail;
                   2095:        }
                   2096:        if ((r = sshbuf_get_string(b, sigp, lenp)) != 0) {
1.331     djm      2097:                error_fr(r, "buffer error");
1.223     djm      2098:  fail:
1.317     dtucker  2099:                ssh_signal(SIGCHLD, osigchld);
1.223     djm      2100:                sshbuf_free(b);
1.100     markus   2101:                return -1;
                   2102:        }
1.317     dtucker  2103:        ssh_signal(SIGCHLD, osigchld);
1.223     djm      2104:        sshbuf_free(b);
1.100     markus   2105:
                   2106:        return 0;
                   2107: }
                   2108:
1.302     djm      2109: static int
1.295     djm      2110: userauth_hostbased(struct ssh *ssh)
1.68      markus   2111: {
1.295     djm      2112:        Authctxt *authctxt = (Authctxt *)ssh->authctxt;
1.223     djm      2113:        struct sshkey *private = NULL;
                   2114:        struct sshbuf *b = NULL;
                   2115:        u_char *sig = NULL, *keyblob = NULL;
                   2116:        char *fp = NULL, *chost = NULL, *lname = NULL;
                   2117:        size_t siglen = 0, keylen = 0;
                   2118:        int i, r, success = 0;
                   2119:
                   2120:        if (authctxt->ktypes == NULL) {
1.344     dtucker  2121:                authctxt->oktypes = xstrdup(options.hostbased_accepted_algos);
1.223     djm      2122:                authctxt->ktypes = authctxt->oktypes;
                   2123:        }
1.213     djm      2124:
1.223     djm      2125:        /*
1.344     dtucker  2126:         * Work through each listed type pattern in HostbasedAcceptedAlgorithms,
1.223     djm      2127:         * trying each hostkey that matches the type in turn.
                   2128:         */
                   2129:        for (;;) {
                   2130:                if (authctxt->active_ktype == NULL)
                   2131:                        authctxt->active_ktype = strsep(&authctxt->ktypes, ",");
                   2132:                if (authctxt->active_ktype == NULL ||
                   2133:                    *authctxt->active_ktype == '\0')
                   2134:                        break;
1.331     djm      2135:                debug3_f("trying key type %s", authctxt->active_ktype);
1.68      markus   2136:
1.223     djm      2137:                /* check for a useful key */
                   2138:                private = NULL;
                   2139:                for (i = 0; i < authctxt->sensitive->nkeys; i++) {
                   2140:                        if (authctxt->sensitive->keys[i] == NULL ||
                   2141:                            authctxt->sensitive->keys[i]->type == KEY_UNSPEC)
                   2142:                                continue;
1.355     djm      2143:                        if (!sshkey_match_keyname_to_sigalgs(
1.223     djm      2144:                            sshkey_ssh_name(authctxt->sensitive->keys[i]),
1.355     djm      2145:                            authctxt->active_ktype))
1.223     djm      2146:                                continue;
1.68      markus   2147:                        /* we take and free the key */
1.223     djm      2148:                        private = authctxt->sensitive->keys[i];
                   2149:                        authctxt->sensitive->keys[i] = NULL;
1.68      markus   2150:                        break;
                   2151:                }
1.223     djm      2152:                /* Found one */
                   2153:                if (private != NULL)
                   2154:                        break;
                   2155:                /* No more keys of this type; advance */
                   2156:                authctxt->active_ktype = NULL;
1.68      markus   2157:        }
1.223     djm      2158:        if (private == NULL) {
                   2159:                free(authctxt->oktypes);
                   2160:                authctxt->oktypes = authctxt->ktypes = NULL;
                   2161:                authctxt->active_ktype = NULL;
1.109     markus   2162:                debug("No more client hostkeys for hostbased authentication.");
1.223     djm      2163:                goto out;
1.68      markus   2164:        }
1.211     djm      2165:
1.223     djm      2166:        if ((fp = sshkey_fingerprint(private, options.fingerprint_hash,
                   2167:            SSH_FP_DEFAULT)) == NULL) {
1.331     djm      2168:                error_f("sshkey_fingerprint failed");
1.223     djm      2169:                goto out;
1.68      markus   2170:        }
1.355     djm      2171:        debug_f("trying hostkey %s %s using sigalg %s",
                   2172:            sshkey_ssh_name(private), fp, authctxt->active_ktype);
1.211     djm      2173:
1.84      markus   2174:        /* figure out a name for the client host */
1.295     djm      2175:        lname = get_local_name(ssh_packet_get_connection_in(ssh));
                   2176:        if (lname == NULL) {
1.331     djm      2177:                error_f("cannot get local ipaddr/name");
1.223     djm      2178:                goto out;
1.84      markus   2179:        }
1.223     djm      2180:
                   2181:        /* XXX sshbuf_put_stringf? */
                   2182:        xasprintf(&chost, "%s.", lname);
1.331     djm      2183:        debug2_f("chost %s", chost);
1.84      markus   2184:
1.68      markus   2185:        /* construct data */
1.223     djm      2186:        if ((b = sshbuf_new()) == NULL) {
1.331     djm      2187:                error_f("sshbuf_new failed");
1.223     djm      2188:                goto out;
                   2189:        }
                   2190:        if ((r = sshkey_to_blob(private, &keyblob, &keylen)) != 0) {
1.331     djm      2191:                error_fr(r, "sshkey_to_blob");
1.223     djm      2192:                goto out;
                   2193:        }
1.346     djm      2194:        if ((r = sshbuf_put_stringb(b, ssh->kex->session_id)) != 0 ||
1.223     djm      2195:            (r = sshbuf_put_u8(b, SSH2_MSG_USERAUTH_REQUEST)) != 0 ||
                   2196:            (r = sshbuf_put_cstring(b, authctxt->server_user)) != 0 ||
1.267     djm      2197:            (r = sshbuf_put_cstring(b, authctxt->service)) != 0 ||
1.223     djm      2198:            (r = sshbuf_put_cstring(b, authctxt->method->name)) != 0 ||
1.343     djm      2199:            (r = sshbuf_put_cstring(b, authctxt->active_ktype)) != 0 ||
1.223     djm      2200:            (r = sshbuf_put_string(b, keyblob, keylen)) != 0 ||
                   2201:            (r = sshbuf_put_cstring(b, chost)) != 0 ||
                   2202:            (r = sshbuf_put_cstring(b, authctxt->local_user)) != 0) {
1.331     djm      2203:                error_fr(r, "buffer error");
1.223     djm      2204:                goto out;
                   2205:        }
                   2206:
1.68      markus   2207: #ifdef DEBUG_PK
1.223     djm      2208:        sshbuf_dump(b, stderr);
1.68      markus   2209: #endif
1.294     djm      2210:        if ((r = ssh_keysign(ssh, private, &sig, &siglen,
                   2211:            sshbuf_ptr(b), sshbuf_len(b))) != 0) {
1.223     djm      2212:                error("sign using hostkey %s %s failed",
                   2213:                    sshkey_ssh_name(private), fp);
                   2214:                goto out;
                   2215:        }
                   2216:        if ((r = sshpkt_start(ssh, SSH2_MSG_USERAUTH_REQUEST)) != 0 ||
                   2217:            (r = sshpkt_put_cstring(ssh, authctxt->server_user)) != 0 ||
                   2218:            (r = sshpkt_put_cstring(ssh, authctxt->service)) != 0 ||
                   2219:            (r = sshpkt_put_cstring(ssh, authctxt->method->name)) != 0 ||
1.343     djm      2220:            (r = sshpkt_put_cstring(ssh, authctxt->active_ktype)) != 0 ||
1.223     djm      2221:            (r = sshpkt_put_string(ssh, keyblob, keylen)) != 0 ||
                   2222:            (r = sshpkt_put_cstring(ssh, chost)) != 0 ||
                   2223:            (r = sshpkt_put_cstring(ssh, authctxt->local_user)) != 0 ||
                   2224:            (r = sshpkt_put_string(ssh, sig, siglen)) != 0 ||
                   2225:            (r = sshpkt_send(ssh)) != 0) {
1.331     djm      2226:                error_fr(r, "packet error");
1.223     djm      2227:                goto out;
                   2228:        }
                   2229:        success = 1;
                   2230:
                   2231:  out:
1.278     markus   2232:        if (sig != NULL)
                   2233:                freezero(sig, siglen);
1.223     djm      2234:        free(keyblob);
                   2235:        free(lname);
                   2236:        free(fp);
1.197     djm      2237:        free(chost);
1.223     djm      2238:        sshkey_free(private);
                   2239:        sshbuf_free(b);
1.68      markus   2240:
1.223     djm      2241:        return success;
1.23      markus   2242: }
1.170     djm      2243:
1.20      markus   2244: /* find auth method */
                   2245:
                   2246: /*
                   2247:  * given auth method name, if configurable options permit this method fill
                   2248:  * in auth_ident field and return true, otherwise return false.
                   2249:  */
1.76      itojun   2250: static int
1.20      markus   2251: authmethod_is_enabled(Authmethod *method)
                   2252: {
                   2253:        if (method == NULL)
                   2254:                return 0;
                   2255:        /* return false if options indicate this method is disabled */
                   2256:        if  (method->enabled == NULL || *method->enabled == 0)
                   2257:                return 0;
                   2258:        /* return false if batch mode is enabled but method needs interactive mode */
                   2259:        if  (method->batch_flag != NULL && *method->batch_flag != 0)
                   2260:                return 0;
                   2261:        return 1;
                   2262: }
                   2263:
1.76      itojun   2264: static Authmethod *
1.20      markus   2265: authmethod_lookup(const char *name)
1.1       markus   2266: {
1.20      markus   2267:        Authmethod *method = NULL;
                   2268:        if (name != NULL)
                   2269:                for (method = authmethods; method->name != NULL; method++)
                   2270:                        if (strcmp(name, method->name) == 0)
                   2271:                                return method;
                   2272:        debug2("Unrecognized authentication method name: %s", name ? name : "NULL");
                   2273:        return NULL;
                   2274: }
1.1       markus   2275:
1.53      markus   2276: /* XXX internal state */
                   2277: static Authmethod *current = NULL;
                   2278: static char *supported = NULL;
                   2279: static char *preferred = NULL;
1.105     deraadt  2280:
1.20      markus   2281: /*
                   2282:  * Given the authentication method list sent by the server, return the
                   2283:  * next method we should try.  If the server initially sends a nil list,
1.67      markus   2284:  * use a built-in default list.
1.41      stevesk  2285:  */
1.76      itojun   2286: static Authmethod *
1.20      markus   2287: authmethod_get(char *authlist)
                   2288: {
1.53      markus   2289:        char *name = NULL;
1.97      markus   2290:        u_int next;
1.41      stevesk  2291:
1.20      markus   2292:        /* Use a suitable default if we're passed a nil list.  */
                   2293:        if (authlist == NULL || strlen(authlist) == 0)
1.53      markus   2294:                authlist = options.preferred_authentications;
1.20      markus   2295:
1.53      markus   2296:        if (supported == NULL || strcmp(authlist, supported) != 0) {
                   2297:                debug3("start over, passed a different list %s", authlist);
1.197     djm      2298:                free(supported);
1.53      markus   2299:                supported = xstrdup(authlist);
                   2300:                preferred = options.preferred_authentications;
                   2301:                debug3("preferred %s", preferred);
                   2302:                current = NULL;
                   2303:        } else if (current != NULL && authmethod_is_enabled(current))
                   2304:                return current;
1.20      markus   2305:
1.53      markus   2306:        for (;;) {
                   2307:                if ((name = match_list(preferred, supported, &next)) == NULL) {
1.109     markus   2308:                        debug("No more authentication methods to try.");
1.53      markus   2309:                        current = NULL;
                   2310:                        return NULL;
                   2311:                }
                   2312:                preferred += next;
1.23      markus   2313:                debug3("authmethod_lookup %s", name);
1.53      markus   2314:                debug3("remaining preferred: %s", preferred);
                   2315:                if ((current = authmethod_lookup(name)) != NULL &&
                   2316:                    authmethod_is_enabled(current)) {
1.23      markus   2317:                        debug3("authmethod_is_enabled %s", name);
1.109     markus   2318:                        debug("Next authentication method: %s", name);
1.197     djm      2319:                        free(name);
1.53      markus   2320:                        return current;
1.23      markus   2321:                }
1.198     dtucker  2322:                free(name);
1.1       markus   2323:        }
1.53      markus   2324: }
1.1       markus   2325:
1.79      stevesk  2326: static char *
1.53      markus   2327: authmethods_get(void)
                   2328: {
                   2329:        Authmethod *method = NULL;
1.278     markus   2330:        struct sshbuf *b;
1.93      markus   2331:        char *list;
1.278     markus   2332:        int r;
1.27      provos   2333:
1.278     markus   2334:        if ((b = sshbuf_new()) == NULL)
1.331     djm      2335:                fatal_f("sshbuf_new failed");
1.53      markus   2336:        for (method = authmethods; method->name != NULL; method++) {
                   2337:                if (authmethod_is_enabled(method)) {
1.278     markus   2338:                        if ((r = sshbuf_putf(b, "%s%s",
                   2339:                            sshbuf_len(b) ? "," : "", method->name)) != 0)
1.331     djm      2340:                                fatal_fr(r, "buffer error");
1.53      markus   2341:                }
                   2342:        }
1.278     markus   2343:        if ((list = sshbuf_dup_string(b)) == NULL)
1.331     djm      2344:                fatal_f("sshbuf_dup_string failed");
1.278     markus   2345:        sshbuf_free(b);
1.93      markus   2346:        return list;
1.1       markus   2347: }