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

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