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

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