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

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