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

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