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

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