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

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