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

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