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

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