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

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