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

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