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

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