[BACK]Return to sshconnect1.c CVS log [TXT][DIR] Up to [local] / src / usr.bin / ssh

Annotation of src/usr.bin/ssh/sshconnect1.c, Revision 1.43

1.1       markus      1: /*
                      2:  * Author: Tatu Ylonen <ylo@cs.hut.fi>
                      3:  * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
                      4:  *                    All rights reserved
                      5:  * Code to connect to a remote host, and to perform the client side of the
                      6:  * login (authentication) dialog.
                      7:  *
1.6       deraadt     8:  * As far as I am concerned, the code I have written for this software
                      9:  * can be used freely for any purpose.  Any derived versions of this
                     10:  * software must be clearly marked as such, and if the derived work is
                     11:  * incompatible with the protocol description in the RFC file, it must be
                     12:  * called by a name other than "ssh" or "Secure Shell".
1.1       markus     13:  */
                     14:
                     15: #include "includes.h"
1.43    ! markus     16: RCSID("$OpenBSD: sshconnect1.c,v 1.42 2001/12/19 07:18:56 deraadt Exp $");
1.1       markus     17:
                     18: #include <openssl/bn.h>
                     19: #include <openssl/evp.h>
                     20:
1.18      markus     21: #ifdef KRB4
                     22: #include <krb.h>
                     23: #endif
1.37      dugsong    24: #ifdef KRB5
                     25: #include <krb5.h>
                     26: #endif
1.18      markus     27: #ifdef AFS
                     28: #include <kafs.h>
1.19      markus     29: #include "radix.h"
1.18      markus     30: #endif
                     31:
                     32: #include "ssh.h"
                     33: #include "ssh1.h"
1.1       markus     34: #include "xmalloc.h"
                     35: #include "rsa.h"
                     36: #include "buffer.h"
                     37: #include "packet.h"
                     38: #include "mpaux.h"
                     39: #include "uidswap.h"
1.18      markus     40: #include "log.h"
1.1       markus     41: #include "readconf.h"
                     42: #include "key.h"
1.4       markus     43: #include "authfd.h"
1.1       markus     44: #include "sshconnect.h"
                     45: #include "authfile.h"
1.18      markus     46: #include "readpass.h"
                     47: #include "cipher.h"
                     48: #include "canohost.h"
1.37      dugsong    49: #include "auth.h"
1.1       markus     50:
                     51: /* Session id for the current session. */
1.13      markus     52: u_char session_id[16];
                     53: u_int supported_authentications = 0;
1.1       markus     54:
                     55: extern Options options;
                     56: extern char *__progname;
                     57:
                     58: /*
                     59:  * Checks if the user has an authentication agent, and if so, tries to
                     60:  * authenticate using the agent.
                     61:  */
1.35      itojun     62: static int
1.24      itojun     63: try_agent_authentication(void)
1.1       markus     64: {
1.5       markus     65:        int type;
1.1       markus     66:        char *comment;
                     67:        AuthenticationConnection *auth;
1.13      markus     68:        u_char response[16];
                     69:        u_int i;
1.5       markus     70:        int plen, clen;
                     71:        Key *key;
                     72:        BIGNUM *challenge;
1.1       markus     73:
                     74:        /* Get connection to the agent. */
                     75:        auth = ssh_get_authentication_connection();
                     76:        if (!auth)
                     77:                return 0;
                     78:
1.43    ! markus     79:        if ((challenge = BN_new()) == NULL)
        !            80:                fatal("try_agent_authentication: BN_new failed");
1.1       markus     81:        /* Loop through identities served by the agent. */
1.5       markus     82:        for (key = ssh_get_first_identity(auth, &comment, 1);
1.42      deraadt    83:            key != NULL;
                     84:            key = ssh_get_next_identity(auth, &comment, 1)) {
1.1       markus     85:
                     86:                /* Try this identity. */
                     87:                debug("Trying RSA authentication via agent with '%.100s'", comment);
                     88:                xfree(comment);
                     89:
                     90:                /* Tell the server that we are willing to authenticate using this key. */
                     91:                packet_start(SSH_CMSG_AUTH_RSA);
1.5       markus     92:                packet_put_bignum(key->rsa->n);
1.1       markus     93:                packet_send();
                     94:                packet_write_wait();
                     95:
                     96:                /* Wait for server's response. */
                     97:                type = packet_read(&plen);
                     98:
                     99:                /* The server sends failure if it doesn\'t like our key or
                    100:                   does not support RSA authentication. */
                    101:                if (type == SSH_SMSG_FAILURE) {
                    102:                        debug("Server refused our key.");
1.5       markus    103:                        key_free(key);
1.1       markus    104:                        continue;
                    105:                }
                    106:                /* Otherwise it should have sent a challenge. */
                    107:                if (type != SSH_SMSG_AUTH_RSA_CHALLENGE)
                    108:                        packet_disconnect("Protocol error during RSA authentication: %d",
                    109:                                          type);
                    110:
                    111:                packet_get_bignum(challenge, &clen);
                    112:
                    113:                packet_integrity_check(plen, clen, type);
                    114:
                    115:                debug("Received RSA challenge from server.");
                    116:
                    117:                /* Ask the agent to decrypt the challenge. */
1.5       markus    118:                if (!ssh_decrypt_challenge(auth, key, challenge, session_id, 1, response)) {
                    119:                        /*
                    120:                         * The agent failed to authenticate this identifier
                    121:                         * although it advertised it supports this.  Just
                    122:                         * return a wrong value.
                    123:                         */
1.1       markus    124:                        log("Authentication agent failed to decrypt challenge.");
                    125:                        memset(response, 0, sizeof(response));
                    126:                }
1.5       markus    127:                key_free(key);
1.1       markus    128:                debug("Sending response to RSA challenge.");
                    129:
                    130:                /* Send the decrypted challenge back to the server. */
                    131:                packet_start(SSH_CMSG_AUTH_RSA_RESPONSE);
                    132:                for (i = 0; i < 16; i++)
                    133:                        packet_put_char(response[i]);
                    134:                packet_send();
                    135:                packet_write_wait();
                    136:
                    137:                /* Wait for response from the server. */
                    138:                type = packet_read(&plen);
                    139:
                    140:                /* The server returns success if it accepted the authentication. */
                    141:                if (type == SSH_SMSG_SUCCESS) {
1.14      markus    142:                        ssh_close_authentication_connection(auth);
1.5       markus    143:                        BN_clear_free(challenge);
1.1       markus    144:                        debug("RSA authentication accepted by server.");
                    145:                        return 1;
                    146:                }
                    147:                /* Otherwise it should return failure. */
                    148:                if (type != SSH_SMSG_FAILURE)
                    149:                        packet_disconnect("Protocol error waiting RSA auth response: %d",
                    150:                                          type);
                    151:        }
1.14      markus    152:        ssh_close_authentication_connection(auth);
1.1       markus    153:        BN_clear_free(challenge);
                    154:        debug("RSA authentication using agent refused.");
                    155:        return 0;
                    156: }
                    157:
                    158: /*
                    159:  * Computes the proper response to a RSA challenge, and sends the response to
                    160:  * the server.
                    161:  */
1.35      itojun    162: static void
1.1       markus    163: respond_to_rsa_challenge(BIGNUM * challenge, RSA * prv)
                    164: {
1.13      markus    165:        u_char buf[32], response[16];
1.1       markus    166:        MD5_CTX md;
                    167:        int i, len;
                    168:
                    169:        /* Decrypt the challenge using the private key. */
1.21      markus    170:        /* XXX think about Bleichenbacher, too */
                    171:        if (rsa_private_decrypt(challenge, challenge, prv) <= 0)
                    172:                packet_disconnect(
                    173:                    "respond_to_rsa_challenge: rsa_private_decrypt failed");
1.1       markus    174:
                    175:        /* Compute the response. */
                    176:        /* The response is MD5 of decrypted challenge plus session id. */
                    177:        len = BN_num_bytes(challenge);
                    178:        if (len <= 0 || len > sizeof(buf))
1.21      markus    179:                packet_disconnect(
                    180:                    "respond_to_rsa_challenge: bad challenge length %d", len);
1.1       markus    181:
                    182:        memset(buf, 0, sizeof(buf));
                    183:        BN_bn2bin(challenge, buf + sizeof(buf) - len);
                    184:        MD5_Init(&md);
                    185:        MD5_Update(&md, buf, 32);
                    186:        MD5_Update(&md, session_id, 16);
                    187:        MD5_Final(response, &md);
                    188:
                    189:        debug("Sending response to host key RSA challenge.");
                    190:
                    191:        /* Send the response back to the server. */
                    192:        packet_start(SSH_CMSG_AUTH_RSA_RESPONSE);
                    193:        for (i = 0; i < 16; i++)
                    194:                packet_put_char(response[i]);
                    195:        packet_send();
                    196:        packet_write_wait();
                    197:
                    198:        memset(buf, 0, sizeof(buf));
                    199:        memset(response, 0, sizeof(response));
                    200:        memset(&md, 0, sizeof(md));
                    201: }
                    202:
                    203: /*
                    204:  * Checks if the user has authentication file, and if so, tries to authenticate
                    205:  * the user using it.
                    206:  */
1.35      itojun    207: static int
1.38      markus    208: try_rsa_authentication(int idx)
1.1       markus    209: {
                    210:        BIGNUM *challenge;
1.36      markus    211:        Key *public, *private;
1.38      markus    212:        char buf[300], *passphrase, *comment, *authfile;
1.36      markus    213:        int i, type, quit, plen, clen;
1.1       markus    214:
1.38      markus    215:        public = options.identity_keys[idx];
                    216:        authfile = options.identity_files[idx];
                    217:        comment = xstrdup(authfile);
                    218:
1.1       markus    219:        debug("Trying RSA authentication with key '%.100s'", comment);
                    220:
                    221:        /* Tell the server that we are willing to authenticate using this key. */
                    222:        packet_start(SSH_CMSG_AUTH_RSA);
                    223:        packet_put_bignum(public->rsa->n);
                    224:        packet_send();
                    225:        packet_write_wait();
                    226:
                    227:        /* Wait for server's response. */
                    228:        type = packet_read(&plen);
                    229:
                    230:        /*
                    231:         * The server responds with failure if it doesn\'t like our key or
                    232:         * doesn\'t support RSA authentication.
                    233:         */
                    234:        if (type == SSH_SMSG_FAILURE) {
                    235:                debug("Server refused our key.");
                    236:                xfree(comment);
                    237:                return 0;
                    238:        }
                    239:        /* Otherwise, the server should respond with a challenge. */
                    240:        if (type != SSH_SMSG_AUTH_RSA_CHALLENGE)
                    241:                packet_disconnect("Protocol error during RSA authentication: %d", type);
                    242:
                    243:        /* Get the challenge from the packet. */
1.43    ! markus    244:        if ((challenge = BN_new()) == NULL)
        !           245:                fatal("try_rsa_authentication: BN_new failed");
1.1       markus    246:        packet_get_bignum(challenge, &clen);
                    247:
                    248:        packet_integrity_check(plen, clen, type);
                    249:
                    250:        debug("Received RSA challenge from server.");
                    251:
                    252:        /*
1.38      markus    253:         * If the key is not stored in external hardware, we have to
                    254:         * load the private key.  Try first with empty passphrase; if it
1.1       markus    255:         * fails, ask for a passphrase.
                    256:         */
1.38      markus    257:        if (public->flags && KEY_FLAG_EXT)
                    258:                private = public;
                    259:        else
                    260:                private = key_load_private_type(KEY_RSA1, authfile, "", NULL);
1.36      markus    261:        if (private == NULL && !options.batch_mode) {
                    262:                snprintf(buf, sizeof(buf),
                    263:                    "Enter passphrase for RSA key '%.100s': ", comment);
                    264:                for (i = 0; i < options.number_of_password_prompts; i++) {
1.1       markus    265:                        passphrase = read_passphrase(buf, 0);
1.36      markus    266:                        if (strcmp(passphrase, "") != 0) {
                    267:                                private = key_load_private_type(KEY_RSA1,
                    268:                                    authfile, passphrase, NULL);
                    269:                                quit = 0;
                    270:                        } else {
                    271:                                debug2("no passphrase given, try next key");
                    272:                                quit = 1;
                    273:                        }
1.1       markus    274:                        memset(passphrase, 0, strlen(passphrase));
                    275:                        xfree(passphrase);
1.36      markus    276:                        if (private != NULL || quit)
                    277:                                break;
                    278:                        debug2("bad passphrase given, try again...");
1.1       markus    279:                }
                    280:        }
                    281:        /* We no longer need the comment. */
                    282:        xfree(comment);
1.36      markus    283:
                    284:        if (private == NULL) {
                    285:                if (!options.batch_mode)
                    286:                        error("Bad passphrase.");
                    287:
                    288:                /* Send a dummy response packet to avoid protocol error. */
                    289:                packet_start(SSH_CMSG_AUTH_RSA_RESPONSE);
                    290:                for (i = 0; i < 16; i++)
                    291:                        packet_put_char(0);
                    292:                packet_send();
                    293:                packet_write_wait();
                    294:
                    295:                /* Expect the server to reject it... */
                    296:                packet_read_expect(&plen, SSH_SMSG_FAILURE);
                    297:                BN_clear_free(challenge);
                    298:                return 0;
                    299:        }
1.1       markus    300:
                    301:        /* Compute and send a response to the challenge. */
                    302:        respond_to_rsa_challenge(challenge, private->rsa);
                    303:
1.38      markus    304:        /* Destroy the private key unless it in external hardware. */
                    305:        if (!(private->flags & KEY_FLAG_EXT))
                    306:                key_free(private);
1.1       markus    307:
                    308:        /* We no longer need the challenge. */
                    309:        BN_clear_free(challenge);
                    310:
                    311:        /* Wait for response from the server. */
                    312:        type = packet_read(&plen);
                    313:        if (type == SSH_SMSG_SUCCESS) {
                    314:                debug("RSA authentication accepted by server.");
                    315:                return 1;
                    316:        }
                    317:        if (type != SSH_SMSG_FAILURE)
                    318:                packet_disconnect("Protocol error waiting RSA auth response: %d", type);
                    319:        debug("RSA authentication refused.");
                    320:        return 0;
                    321: }
                    322:
                    323: /*
                    324:  * Tries to authenticate the user using combined rhosts or /etc/hosts.equiv
                    325:  * authentication and RSA host authentication.
                    326:  */
1.35      itojun    327: static int
1.29      markus    328: try_rhosts_rsa_authentication(const char *local_user, Key * host_key)
1.1       markus    329: {
                    330:        int type;
                    331:        BIGNUM *challenge;
                    332:        int plen, clen;
                    333:
                    334:        debug("Trying rhosts or /etc/hosts.equiv with RSA host authentication.");
                    335:
                    336:        /* Tell the server that we are willing to authenticate using this key. */
                    337:        packet_start(SSH_CMSG_AUTH_RHOSTS_RSA);
1.33      markus    338:        packet_put_cstring(local_user);
1.29      markus    339:        packet_put_int(BN_num_bits(host_key->rsa->n));
                    340:        packet_put_bignum(host_key->rsa->e);
                    341:        packet_put_bignum(host_key->rsa->n);
1.1       markus    342:        packet_send();
                    343:        packet_write_wait();
                    344:
                    345:        /* Wait for server's response. */
                    346:        type = packet_read(&plen);
                    347:
                    348:        /* The server responds with failure if it doesn't admit our
                    349:           .rhosts authentication or doesn't know our host key. */
                    350:        if (type == SSH_SMSG_FAILURE) {
                    351:                debug("Server refused our rhosts authentication or host key.");
                    352:                return 0;
                    353:        }
                    354:        /* Otherwise, the server should respond with a challenge. */
                    355:        if (type != SSH_SMSG_AUTH_RSA_CHALLENGE)
                    356:                packet_disconnect("Protocol error during RSA authentication: %d", type);
                    357:
                    358:        /* Get the challenge from the packet. */
1.43    ! markus    359:        if ((challenge = BN_new()) == NULL)
        !           360:                fatal("try_rhosts_rsa_authentication: BN_new failed");
1.1       markus    361:        packet_get_bignum(challenge, &clen);
                    362:
                    363:        packet_integrity_check(plen, clen, type);
                    364:
                    365:        debug("Received RSA challenge for host key from server.");
                    366:
                    367:        /* Compute a response to the challenge. */
1.29      markus    368:        respond_to_rsa_challenge(challenge, host_key->rsa);
1.1       markus    369:
                    370:        /* We no longer need the challenge. */
                    371:        BN_clear_free(challenge);
                    372:
                    373:        /* Wait for response from the server. */
                    374:        type = packet_read(&plen);
                    375:        if (type == SSH_SMSG_SUCCESS) {
                    376:                debug("Rhosts or /etc/hosts.equiv with RSA host authentication accepted by server.");
                    377:                return 1;
                    378:        }
                    379:        if (type != SSH_SMSG_FAILURE)
                    380:                packet_disconnect("Protocol error waiting RSA auth response: %d", type);
                    381:        debug("Rhosts or /etc/hosts.equiv with RSA host authentication refused.");
                    382:        return 0;
                    383: }
                    384:
                    385: #ifdef KRB4
1.35      itojun    386: static int
1.37      dugsong   387: try_krb4_authentication(void)
1.1       markus    388: {
                    389:        KTEXT_ST auth;          /* Kerberos data */
                    390:        char *reply;
                    391:        char inst[INST_SZ];
                    392:        char *realm;
                    393:        CREDENTIALS cred;
                    394:        int r, type, plen;
                    395:        socklen_t slen;
                    396:        Key_schedule schedule;
                    397:        u_long checksum, cksum;
                    398:        MSG_DAT msg_data;
                    399:        struct sockaddr_in local, foreign;
                    400:        struct stat st;
                    401:
                    402:        /* Don't do anything if we don't have any tickets. */
                    403:        if (stat(tkt_string(), &st) < 0)
                    404:                return 0;
1.42      deraadt   405:
1.37      dugsong   406:        strlcpy(inst, (char *)krb_get_phost(get_canonical_hostname(1)),
                    407:            INST_SZ);
1.42      deraadt   408:
1.37      dugsong   409:        realm = (char *)krb_realmofhost(get_canonical_hostname(1));
1.1       markus    410:        if (!realm) {
1.37      dugsong   411:                debug("Kerberos v4: no realm for %s", get_canonical_hostname(1));
1.1       markus    412:                return 0;
                    413:        }
                    414:        /* This can really be anything. */
1.37      dugsong   415:        checksum = (u_long)getpid();
1.42      deraadt   416:
1.1       markus    417:        r = krb_mk_req(&auth, KRB4_SERVICE_NAME, inst, realm, checksum);
                    418:        if (r != KSUCCESS) {
1.37      dugsong   419:                debug("Kerberos v4 krb_mk_req failed: %s", krb_err_txt[r]);
1.1       markus    420:                return 0;
                    421:        }
                    422:        /* Get session key to decrypt the server's reply with. */
                    423:        r = krb_get_cred(KRB4_SERVICE_NAME, inst, realm, &cred);
                    424:        if (r != KSUCCESS) {
                    425:                debug("get_cred failed: %s", krb_err_txt[r]);
                    426:                return 0;
                    427:        }
                    428:        des_key_sched((des_cblock *) cred.session, schedule);
1.42      deraadt   429:
1.1       markus    430:        /* Send authentication info to server. */
                    431:        packet_start(SSH_CMSG_AUTH_KERBEROS);
                    432:        packet_put_string((char *) auth.dat, auth.length);
                    433:        packet_send();
                    434:        packet_write_wait();
1.42      deraadt   435:
1.1       markus    436:        /* Zero the buffer. */
                    437:        (void) memset(auth.dat, 0, MAX_KTXT_LEN);
1.42      deraadt   438:
1.1       markus    439:        slen = sizeof(local);
                    440:        memset(&local, 0, sizeof(local));
                    441:        if (getsockname(packet_get_connection_in(),
1.37      dugsong   442:            (struct sockaddr *)&local, &slen) < 0)
1.1       markus    443:                debug("getsockname failed: %s", strerror(errno));
1.42      deraadt   444:
1.1       markus    445:        slen = sizeof(foreign);
                    446:        memset(&foreign, 0, sizeof(foreign));
                    447:        if (getpeername(packet_get_connection_in(),
1.37      dugsong   448:            (struct sockaddr *)&foreign, &slen) < 0) {
1.1       markus    449:                debug("getpeername failed: %s", strerror(errno));
                    450:                fatal_cleanup();
                    451:        }
                    452:        /* Get server reply. */
                    453:        type = packet_read(&plen);
                    454:        switch (type) {
                    455:        case SSH_SMSG_FAILURE:
                    456:                /* Should really be SSH_SMSG_AUTH_KERBEROS_FAILURE */
1.37      dugsong   457:                debug("Kerberos v4 authentication failed.");
1.1       markus    458:                return 0;
                    459:                break;
1.42      deraadt   460:
1.1       markus    461:        case SSH_SMSG_AUTH_KERBEROS_RESPONSE:
                    462:                /* SSH_SMSG_AUTH_KERBEROS_SUCCESS */
1.37      dugsong   463:                debug("Kerberos v4 authentication accepted.");
1.42      deraadt   464:
1.1       markus    465:                /* Get server's response. */
1.13      markus    466:                reply = packet_get_string((u_int *) &auth.length);
1.1       markus    467:                memcpy(auth.dat, reply, auth.length);
                    468:                xfree(reply);
1.42      deraadt   469:
1.1       markus    470:                packet_integrity_check(plen, 4 + auth.length, type);
1.42      deraadt   471:
1.1       markus    472:                /*
                    473:                 * If his response isn't properly encrypted with the session
                    474:                 * key, and the decrypted checksum fails to match, he's
                    475:                 * bogus. Bail out.
                    476:                 */
                    477:                r = krb_rd_priv(auth.dat, auth.length, schedule, &cred.session,
1.37      dugsong   478:                    &foreign, &local, &msg_data);
1.1       markus    479:                if (r != KSUCCESS) {
1.37      dugsong   480:                        debug("Kerberos v4 krb_rd_priv failed: %s",
                    481:                            krb_err_txt[r]);
                    482:                        packet_disconnect("Kerberos v4 challenge failed!");
1.1       markus    483:                }
                    484:                /* Fetch the (incremented) checksum that we supplied in the request. */
1.37      dugsong   485:                memcpy((char *)&cksum, (char *)msg_data.app_data,
                    486:                    sizeof(cksum));
1.1       markus    487:                cksum = ntohl(cksum);
1.42      deraadt   488:
1.1       markus    489:                /* If it matches, we're golden. */
                    490:                if (cksum == checksum + 1) {
1.37      dugsong   491:                        debug("Kerberos v4 challenge successful.");
1.1       markus    492:                        return 1;
                    493:                } else
1.37      dugsong   494:                        packet_disconnect("Kerberos v4 challenge failed!");
1.1       markus    495:                break;
1.42      deraadt   496:
1.1       markus    497:        default:
1.37      dugsong   498:                packet_disconnect("Protocol error on Kerberos v4 response: %d", type);
1.1       markus    499:        }
                    500:        return 0;
                    501: }
                    502:
                    503: #endif /* KRB4 */
                    504:
1.37      dugsong   505: #ifdef KRB5
                    506: static int
                    507: try_krb5_authentication(krb5_context *context, krb5_auth_context *auth_context)
                    508: {
                    509:        krb5_error_code problem;
                    510:        const char *tkfile;
                    511:        struct stat buf;
                    512:        krb5_ccache ccache = NULL;
                    513:        const char *remotehost;
                    514:        krb5_data ap;
                    515:        int type, payload_len;
                    516:        krb5_ap_rep_enc_part *reply = NULL;
                    517:        int ret;
1.42      deraadt   518:
1.37      dugsong   519:        memset(&ap, 0, sizeof(ap));
1.42      deraadt   520:
1.37      dugsong   521:        problem = krb5_init_context(context);
                    522:        if (problem) {
                    523:                debug("Kerberos v5: krb5_init_context failed");
                    524:                ret = 0;
                    525:                goto out;
                    526:        }
1.42      deraadt   527:
1.37      dugsong   528:        tkfile = krb5_cc_default_name(*context);
                    529:        if (strncmp(tkfile, "FILE:", 5) == 0)
                    530:                tkfile += 5;
1.42      deraadt   531:
1.37      dugsong   532:        if (stat(tkfile, &buf) == 0 && getuid() != buf.st_uid) {
                    533:                debug("Kerberos v5: could not get default ccache (permission denied).");
                    534:                ret = 0;
                    535:                goto out;
                    536:        }
1.42      deraadt   537:
1.37      dugsong   538:        problem = krb5_cc_default(*context, &ccache);
                    539:        if (problem) {
                    540:                debug("Kerberos v5: krb5_cc_default failed: %s",
                    541:                    krb5_get_err_text(*context, problem));
                    542:                ret = 0;
                    543:                goto out;
                    544:        }
1.42      deraadt   545:
1.37      dugsong   546:        remotehost = get_canonical_hostname(1);
1.42      deraadt   547:
1.37      dugsong   548:        problem = krb5_mk_req(*context, auth_context, AP_OPTS_MUTUAL_REQUIRED,
                    549:            "host", remotehost, NULL, ccache, &ap);
                    550:        if (problem) {
                    551:                debug("Kerberos v5: krb5_mk_req failed: %s",
                    552:                    krb5_get_err_text(*context, problem));
                    553:                ret = 0;
                    554:                goto out;
                    555:        }
1.42      deraadt   556:
1.37      dugsong   557:        packet_start(SSH_CMSG_AUTH_KERBEROS);
                    558:        packet_put_string((char *) ap.data, ap.length);
                    559:        packet_send();
                    560:        packet_write_wait();
1.42      deraadt   561:
1.37      dugsong   562:        xfree(ap.data);
                    563:        ap.length = 0;
1.42      deraadt   564:
1.37      dugsong   565:        type = packet_read(&payload_len);
                    566:        switch (type) {
1.42      deraadt   567:        case SSH_SMSG_FAILURE:
                    568:                /* Should really be SSH_SMSG_AUTH_KERBEROS_FAILURE */
                    569:                debug("Kerberos v5 authentication failed.");
                    570:                ret = 0;
                    571:                break;
                    572:
1.37      dugsong   573:        case SSH_SMSG_AUTH_KERBEROS_RESPONSE:
1.42      deraadt   574:                /* SSH_SMSG_AUTH_KERBEROS_SUCCESS */
                    575:                debug("Kerberos v5 authentication accepted.");
                    576:
                    577:                /* Get server's response. */
                    578:                ap.data = packet_get_string((unsigned int *) &ap.length);
                    579:
                    580:                packet_integrity_check(payload_len, 4 + ap.length, type);
                    581:                /* XXX je to dobre? */
                    582:
                    583:                problem = krb5_rd_rep(*context, *auth_context, &ap, &reply);
                    584:                if (problem) {
1.37      dugsong   585:                        ret = 0;
                    586:                }
                    587:                ret = 1;
                    588:                break;
1.42      deraadt   589:
1.37      dugsong   590:        default:
                    591:                packet_disconnect("Protocol error on Kerberos v5 response: %d",
                    592:                    type);
                    593:                ret = 0;
                    594:                break;
1.42      deraadt   595:
1.37      dugsong   596:        }
1.42      deraadt   597:
1.37      dugsong   598:  out:
                    599:        if (ccache != NULL)
                    600:                krb5_cc_close(*context, ccache);
                    601:        if (reply != NULL)
                    602:                krb5_free_ap_rep_enc_part(*context, reply);
                    603:        if (ap.length > 0)
                    604:                krb5_data_free(&ap);
1.42      deraadt   605:
1.37      dugsong   606:        return (ret);
                    607: }
                    608:
                    609: static void
                    610: send_krb5_tgt(krb5_context context, krb5_auth_context auth_context)
                    611: {
                    612:        int fd, type, payload_len;
                    613:        krb5_error_code problem;
                    614:        krb5_data outbuf;
                    615:        krb5_ccache ccache = NULL;
                    616:        krb5_creds creds;
                    617:        krb5_kdc_flags flags;
                    618:        const char *remotehost;
1.42      deraadt   619:
1.37      dugsong   620:        memset(&creds, 0, sizeof(creds));
                    621:        memset(&outbuf, 0, sizeof(outbuf));
1.42      deraadt   622:
1.37      dugsong   623:        fd = packet_get_connection_in();
1.42      deraadt   624:
1.37      dugsong   625:        problem = krb5_auth_con_setaddrs_from_fd(context, auth_context, &fd);
                    626:        if (problem)
                    627:                goto out;
1.42      deraadt   628:
1.37      dugsong   629:        problem = krb5_cc_default(context, &ccache);
                    630:        if (problem)
                    631:                goto out;
1.42      deraadt   632:
1.37      dugsong   633:        problem = krb5_cc_get_principal(context, ccache, &creds.client);
                    634:        if (problem)
                    635:                goto out;
1.42      deraadt   636:
1.37      dugsong   637:        problem = krb5_build_principal(context, &creds.server,
                    638:            strlen(creds.client->realm), creds.client->realm,
                    639:            "krbtgt", creds.client->realm, NULL);
                    640:        if (problem)
                    641:                goto out;
1.42      deraadt   642:
1.37      dugsong   643:        creds.times.endtime = 0;
1.42      deraadt   644:
1.37      dugsong   645:        flags.i = 0;
                    646:        flags.b.forwarded = 1;
                    647:        flags.b.forwardable = krb5_config_get_bool(context,  NULL,
                    648:            "libdefaults", "forwardable", NULL);
1.42      deraadt   649:
1.37      dugsong   650:        remotehost = get_canonical_hostname(1);
1.42      deraadt   651:
1.37      dugsong   652:        problem = krb5_get_forwarded_creds(context, auth_context,
                    653:            ccache, flags.i, remotehost, &creds, &outbuf);
                    654:        if (problem)
                    655:                goto out;
1.42      deraadt   656:
1.37      dugsong   657:        packet_start(SSH_CMSG_HAVE_KERBEROS_TGT);
                    658:        packet_put_string((char *)outbuf.data, outbuf.length);
                    659:        packet_send();
                    660:        packet_write_wait();
1.42      deraadt   661:
1.37      dugsong   662:        type = packet_read(&payload_len);
1.42      deraadt   663:
1.37      dugsong   664:        if (type == SSH_SMSG_SUCCESS) {
                    665:                char *pname;
1.42      deraadt   666:
1.37      dugsong   667:                krb5_unparse_name(context, creds.client, &pname);
                    668:                debug("Kerberos v5 TGT forwarded (%s).", pname);
                    669:                xfree(pname);
                    670:        } else
                    671:                debug("Kerberos v5 TGT forwarding failed.");
1.42      deraadt   672:
1.37      dugsong   673:        return;
1.42      deraadt   674:
1.37      dugsong   675:  out:
                    676:        if (problem)
                    677:                debug("Kerberos v5 TGT forwarding failed: %s",
                    678:                    krb5_get_err_text(context, problem));
                    679:        if (creds.client)
                    680:                krb5_free_principal(context, creds.client);
                    681:        if (creds.server)
                    682:                krb5_free_principal(context, creds.server);
                    683:        if (ccache)
                    684:                krb5_cc_close(context, ccache);
                    685:        if (outbuf.data)
                    686:                xfree(outbuf.data);
                    687: }
                    688: #endif /* KRB5 */
                    689:
1.1       markus    690: #ifdef AFS
1.37      dugsong   691: static void
                    692: send_krb4_tgt(void)
1.1       markus    693: {
                    694:        CREDENTIALS *creds;
                    695:        struct stat st;
1.37      dugsong   696:        char buffer[4096], pname[ANAME_SZ], pinst[INST_SZ], prealm[REALM_SZ];
                    697:        int problem, type, len;
1.42      deraadt   698:
1.1       markus    699:        /* Don't do anything if we don't have any tickets. */
                    700:        if (stat(tkt_string(), &st) < 0)
1.37      dugsong   701:                return;
1.42      deraadt   702:
1.1       markus    703:        creds = xmalloc(sizeof(*creds));
1.42      deraadt   704:
1.37      dugsong   705:        problem = krb_get_tf_fullname(TKT_FILE, pname, pinst, prealm);
                    706:        if (problem)
                    707:                goto out;
1.42      deraadt   708:
1.37      dugsong   709:        problem = krb_get_cred("krbtgt", prealm, prealm, creds);
                    710:        if (problem)
                    711:                goto out;
1.42      deraadt   712:
1.1       markus    713:        if (time(0) > krb_life_to_time(creds->issue_date, creds->lifetime)) {
1.37      dugsong   714:                problem = RD_AP_EXP;
                    715:                goto out;
1.1       markus    716:        }
1.37      dugsong   717:        creds_to_radix(creds, (u_char *)buffer, sizeof(buffer));
1.42      deraadt   718:
1.1       markus    719:        packet_start(SSH_CMSG_HAVE_KERBEROS_TGT);
1.33      markus    720:        packet_put_cstring(buffer);
1.1       markus    721:        packet_send();
                    722:        packet_write_wait();
1.42      deraadt   723:
1.37      dugsong   724:        type = packet_read(&len);
1.42      deraadt   725:
1.37      dugsong   726:        if (type == SSH_SMSG_SUCCESS)
                    727:                debug("Kerberos v4 TGT forwarded (%s%s%s@%s).",
                    728:                    creds->pname, creds->pinst[0] ? "." : "",
                    729:                    creds->pinst, creds->realm);
                    730:        else
                    731:                debug("Kerberos v4 TGT rejected.");
1.42      deraadt   732:
1.37      dugsong   733:        xfree(creds);
                    734:        return;
1.42      deraadt   735:
1.37      dugsong   736:  out:
                    737:        debug("Kerberos v4 TGT passing failed: %s", krb_err_txt[problem]);
                    738:        xfree(creds);
1.1       markus    739: }
                    740:
1.35      itojun    741: static void
1.1       markus    742: send_afs_tokens(void)
                    743: {
                    744:        CREDENTIALS creds;
                    745:        struct ViceIoctl parms;
                    746:        struct ClearToken ct;
1.37      dugsong   747:        int i, type, len;
1.1       markus    748:        char buf[2048], *p, *server_cell;
                    749:        char buffer[8192];
1.42      deraadt   750:
1.1       markus    751:        /* Move over ktc_GetToken, here's something leaner. */
                    752:        for (i = 0; i < 100; i++) {     /* just in case */
                    753:                parms.in = (char *) &i;
                    754:                parms.in_size = sizeof(i);
                    755:                parms.out = buf;
                    756:                parms.out_size = sizeof(buf);
                    757:                if (k_pioctl(0, VIOCGETTOK, &parms, 0) != 0)
                    758:                        break;
                    759:                p = buf;
1.42      deraadt   760:
1.1       markus    761:                /* Get secret token. */
1.13      markus    762:                memcpy(&creds.ticket_st.length, p, sizeof(u_int));
1.1       markus    763:                if (creds.ticket_st.length > MAX_KTXT_LEN)
                    764:                        break;
1.13      markus    765:                p += sizeof(u_int);
1.1       markus    766:                memcpy(creds.ticket_st.dat, p, creds.ticket_st.length);
                    767:                p += creds.ticket_st.length;
1.42      deraadt   768:
1.1       markus    769:                /* Get clear token. */
                    770:                memcpy(&len, p, sizeof(len));
                    771:                if (len != sizeof(struct ClearToken))
                    772:                        break;
                    773:                p += sizeof(len);
                    774:                memcpy(&ct, p, len);
                    775:                p += len;
                    776:                p += sizeof(len);       /* primary flag */
                    777:                server_cell = p;
1.42      deraadt   778:
1.1       markus    779:                /* Flesh out our credentials. */
1.37      dugsong   780:                strlcpy(creds.service, "afs", sizeof(creds.service));
1.1       markus    781:                creds.instance[0] = '\0';
                    782:                strlcpy(creds.realm, server_cell, REALM_SZ);
                    783:                memcpy(creds.session, ct.HandShakeKey, DES_KEY_SZ);
                    784:                creds.issue_date = ct.BeginTimestamp;
1.37      dugsong   785:                creds.lifetime = krb_time_to_life(creds.issue_date,
                    786:                    ct.EndTimestamp);
1.1       markus    787:                creds.kvno = ct.AuthHandle;
                    788:                snprintf(creds.pname, sizeof(creds.pname), "AFS ID %d", ct.ViceId);
                    789:                creds.pinst[0] = '\0';
1.42      deraadt   790:
1.1       markus    791:                /* Encode token, ship it off. */
1.37      dugsong   792:                if (creds_to_radix(&creds, (u_char *)buffer,
                    793:                    sizeof(buffer)) <= 0)
1.1       markus    794:                        break;
                    795:                packet_start(SSH_CMSG_HAVE_AFS_TOKEN);
1.33      markus    796:                packet_put_cstring(buffer);
1.1       markus    797:                packet_send();
                    798:                packet_write_wait();
                    799:
                    800:                /* Roger, Roger. Clearance, Clarence. What's your vector,
                    801:                   Victor? */
1.37      dugsong   802:                type = packet_read(&len);
1.42      deraadt   803:
1.1       markus    804:                if (type == SSH_SMSG_FAILURE)
                    805:                        debug("AFS token for cell %s rejected.", server_cell);
                    806:                else if (type != SSH_SMSG_SUCCESS)
                    807:                        packet_disconnect("Protocol error on AFS token response: %d", type);
                    808:        }
                    809: }
                    810:
                    811: #endif /* AFS */
                    812:
                    813: /*
                    814:  * Tries to authenticate with any string-based challenge/response system.
                    815:  * Note that the client code is not tied to s/key or TIS.
                    816:  */
1.35      itojun    817: static int
1.32      markus    818: try_challenge_response_authentication(void)
1.1       markus    819: {
                    820:        int type, i;
                    821:        int payload_len;
1.13      markus    822:        u_int clen;
1.12      markus    823:        char prompt[1024];
1.1       markus    824:        char *challenge, *response;
1.40      markus    825:
                    826:        debug("Doing challenge response authentication.");
                    827:
1.12      markus    828:        for (i = 0; i < options.number_of_password_prompts; i++) {
                    829:                /* request a challenge */
                    830:                packet_start(SSH_CMSG_AUTH_TIS);
                    831:                packet_send();
                    832:                packet_write_wait();
1.1       markus    833:
1.12      markus    834:                type = packet_read(&payload_len);
                    835:                if (type != SSH_SMSG_FAILURE &&
                    836:                    type != SSH_SMSG_AUTH_TIS_CHALLENGE) {
                    837:                        packet_disconnect("Protocol error: got %d in response "
1.20      markus    838:                            "to SSH_CMSG_AUTH_TIS", type);
1.12      markus    839:                }
                    840:                if (type != SSH_SMSG_AUTH_TIS_CHALLENGE) {
1.20      markus    841:                        debug("No challenge.");
1.12      markus    842:                        return 0;
                    843:                }
                    844:                challenge = packet_get_string(&clen);
                    845:                packet_integrity_check(payload_len, (4 + clen), type);
1.16      markus    846:                snprintf(prompt, sizeof prompt, "%s%s", challenge,
1.42      deraadt   847:                    strchr(challenge, '\n') ? "" : "\nResponse: ");
1.12      markus    848:                xfree(challenge);
1.1       markus    849:                if (i != 0)
                    850:                        error("Permission denied, please try again.");
1.12      markus    851:                if (options.cipher == SSH_CIPHER_NONE)
                    852:                        log("WARNING: Encryption is disabled! "
                    853:                            "Reponse will be transmitted in clear text.");
                    854:                response = read_passphrase(prompt, 0);
                    855:                if (strcmp(response, "") == 0) {
                    856:                        xfree(response);
                    857:                        break;
                    858:                }
1.1       markus    859:                packet_start(SSH_CMSG_AUTH_TIS_RESPONSE);
1.27      markus    860:                ssh_put_password(response);
1.1       markus    861:                memset(response, 0, strlen(response));
                    862:                xfree(response);
                    863:                packet_send();
                    864:                packet_write_wait();
                    865:                type = packet_read(&payload_len);
                    866:                if (type == SSH_SMSG_SUCCESS)
                    867:                        return 1;
                    868:                if (type != SSH_SMSG_FAILURE)
                    869:                        packet_disconnect("Protocol error: got %d in response "
1.20      markus    870:                            "to SSH_CMSG_AUTH_TIS_RESPONSE", type);
1.1       markus    871:        }
                    872:        /* failure */
                    873:        return 0;
                    874: }
                    875:
                    876: /*
                    877:  * Tries to authenticate with plain passwd authentication.
                    878:  */
1.35      itojun    879: static int
1.1       markus    880: try_password_authentication(char *prompt)
                    881: {
                    882:        int type, i, payload_len;
                    883:        char *password;
                    884:
                    885:        debug("Doing password authentication.");
                    886:        if (options.cipher == SSH_CIPHER_NONE)
                    887:                log("WARNING: Encryption is disabled! Password will be transmitted in clear text.");
                    888:        for (i = 0; i < options.number_of_password_prompts; i++) {
                    889:                if (i != 0)
                    890:                        error("Permission denied, please try again.");
                    891:                password = read_passphrase(prompt, 0);
                    892:                packet_start(SSH_CMSG_AUTH_PASSWORD);
1.27      markus    893:                ssh_put_password(password);
1.1       markus    894:                memset(password, 0, strlen(password));
                    895:                xfree(password);
                    896:                packet_send();
                    897:                packet_write_wait();
                    898:
                    899:                type = packet_read(&payload_len);
                    900:                if (type == SSH_SMSG_SUCCESS)
                    901:                        return 1;
                    902:                if (type != SSH_SMSG_FAILURE)
                    903:                        packet_disconnect("Protocol error: got %d in response to passwd auth", type);
                    904:        }
                    905:        /* failure */
                    906:        return 0;
                    907: }
                    908:
                    909: /*
                    910:  * SSH1 key exchange
                    911:  */
                    912: void
                    913: ssh_kex(char *host, struct sockaddr *hostaddr)
                    914: {
                    915:        int i;
                    916:        BIGNUM *key;
1.43    ! markus    917:        Key *host_key, *server_key;
1.1       markus    918:        int bits, rbits;
                    919:        int ssh_cipher_default = SSH_CIPHER_3DES;
1.13      markus    920:        u_char session_key[SSH_SESSION_KEY_LENGTH];
                    921:        u_char cookie[8];
                    922:        u_int supported_ciphers;
                    923:        u_int server_flags, client_flags;
1.1       markus    924:        int payload_len, clen, sum_len = 0;
                    925:        u_int32_t rand = 0;
                    926:
                    927:        debug("Waiting for server public key.");
                    928:
                    929:        /* Wait for a public key packet from the server. */
                    930:        packet_read_expect(&payload_len, SSH_SMSG_PUBLIC_KEY);
                    931:
                    932:        /* Get cookie from the packet. */
                    933:        for (i = 0; i < 8; i++)
                    934:                cookie[i] = packet_get_char();
                    935:
                    936:        /* Get the public key. */
1.43    ! markus    937:        server_key = key_new(KEY_RSA1);
        !           938:        bits = packet_get_int();
        !           939:        packet_get_bignum(server_key->rsa->e, &clen);
1.1       markus    940:        sum_len += clen;
1.43    ! markus    941:        packet_get_bignum(server_key->rsa->n, &clen);
1.1       markus    942:        sum_len += clen;
                    943:
1.43    ! markus    944:        rbits = BN_num_bits(server_key->rsa->n);
1.1       markus    945:        if (bits != rbits) {
                    946:                log("Warning: Server lies about size of server public key: "
                    947:                    "actual size is %d bits vs. announced %d.", rbits, bits);
                    948:                log("Warning: This may be due to an old implementation of ssh.");
                    949:        }
                    950:        /* Get the host key. */
1.43    ! markus    951:        host_key = key_new(KEY_RSA1);
        !           952:        bits = packet_get_int();
        !           953:        packet_get_bignum(host_key->rsa->e, &clen);
1.1       markus    954:        sum_len += clen;
1.43    ! markus    955:        packet_get_bignum(host_key->rsa->n, &clen);
1.1       markus    956:        sum_len += clen;
                    957:
1.43    ! markus    958:        rbits = BN_num_bits(host_key->rsa->n);
1.1       markus    959:        if (bits != rbits) {
                    960:                log("Warning: Server lies about size of server host key: "
                    961:                    "actual size is %d bits vs. announced %d.", rbits, bits);
                    962:                log("Warning: This may be due to an old implementation of ssh.");
                    963:        }
                    964:
                    965:        /* Get protocol flags. */
                    966:        server_flags = packet_get_int();
                    967:        packet_set_protocol_flags(server_flags);
                    968:
                    969:        supported_ciphers = packet_get_int();
                    970:        supported_authentications = packet_get_int();
                    971:
                    972:        debug("Received server public key (%d bits) and host key (%d bits).",
1.43    ! markus    973:            BN_num_bits(server_key->rsa->n), BN_num_bits(host_key->rsa->n));
1.1       markus    974:
                    975:        packet_integrity_check(payload_len,
1.42      deraadt   976:            8 + 4 + sum_len + 0 + 4 + 0 + 0 + 4 + 4 + 4,
                    977:            SSH_SMSG_PUBLIC_KEY);
1.43    ! markus    978:        if (verify_host_key(host, hostaddr, host_key) == -1)
1.41      markus    979:                fatal("Host key verification failed.");
1.1       markus    980:
                    981:        client_flags = SSH_PROTOFLAG_SCREEN_NUMBER | SSH_PROTOFLAG_HOST_IN_FWD_OPEN;
                    982:
1.43    ! markus    983:        compute_session_id(session_id, cookie, host_key->rsa->n, server_key->rsa->n);
1.1       markus    984:
                    985:        /* Generate a session key. */
                    986:        arc4random_stir();
                    987:
                    988:        /*
                    989:         * Generate an encryption key for the session.   The key is a 256 bit
                    990:         * random number, interpreted as a 32-byte key, with the least
                    991:         * significant 8 bits being the first byte of the key.
                    992:         */
                    993:        for (i = 0; i < 32; i++) {
                    994:                if (i % 4 == 0)
                    995:                        rand = arc4random();
                    996:                session_key[i] = rand & 0xff;
                    997:                rand >>= 8;
                    998:        }
                    999:
                   1000:        /*
                   1001:         * According to the protocol spec, the first byte of the session key
                   1002:         * is the highest byte of the integer.  The session key is xored with
                   1003:         * the first 16 bytes of the session id.
                   1004:         */
1.43    ! markus   1005:        if ((key = BN_new()) == NULL)
        !          1006:                fatal("respond_to_rsa_challenge: BN_new failed");
1.1       markus   1007:        BN_set_word(key, 0);
                   1008:        for (i = 0; i < SSH_SESSION_KEY_LENGTH; i++) {
                   1009:                BN_lshift(key, key, 8);
                   1010:                if (i < 16)
                   1011:                        BN_add_word(key, session_key[i] ^ session_id[i]);
                   1012:                else
                   1013:                        BN_add_word(key, session_key[i]);
                   1014:        }
                   1015:
                   1016:        /*
                   1017:         * Encrypt the integer using the public key and host key of the
                   1018:         * server (key with smaller modulus first).
                   1019:         */
1.43    ! markus   1020:        if (BN_cmp(server_key->rsa->n, host_key->rsa->n) < 0) {
1.1       markus   1021:                /* Public key has smaller modulus. */
1.43    ! markus   1022:                if (BN_num_bits(host_key->rsa->n) <
        !          1023:                    BN_num_bits(server_key->rsa->n) + SSH_KEY_BITS_RESERVED) {
        !          1024:                        fatal("respond_to_rsa_challenge: host_key %d < server_key %d + "
1.42      deraadt  1025:                            "SSH_KEY_BITS_RESERVED %d",
1.43    ! markus   1026:                            BN_num_bits(host_key->rsa->n),
        !          1027:                            BN_num_bits(server_key->rsa->n),
1.42      deraadt  1028:                            SSH_KEY_BITS_RESERVED);
1.1       markus   1029:                }
1.43    ! markus   1030:                rsa_public_encrypt(key, key, server_key->rsa);
        !          1031:                rsa_public_encrypt(key, key, host_key->rsa);
1.1       markus   1032:        } else {
                   1033:                /* Host key has smaller modulus (or they are equal). */
1.43    ! markus   1034:                if (BN_num_bits(server_key->rsa->n) <
        !          1035:                    BN_num_bits(host_key->rsa->n) + SSH_KEY_BITS_RESERVED) {
        !          1036:                        fatal("respond_to_rsa_challenge: server_key %d < host_key %d + "
1.42      deraadt  1037:                            "SSH_KEY_BITS_RESERVED %d",
1.43    ! markus   1038:                            BN_num_bits(server_key->rsa->n),
        !          1039:                            BN_num_bits(host_key->rsa->n),
1.42      deraadt  1040:                            SSH_KEY_BITS_RESERVED);
1.1       markus   1041:                }
1.43    ! markus   1042:                rsa_public_encrypt(key, key, host_key->rsa);
        !          1043:                rsa_public_encrypt(key, key, server_key->rsa);
1.1       markus   1044:        }
                   1045:
                   1046:        /* Destroy the public keys since we no longer need them. */
1.43    ! markus   1047:        key_free(server_key);
        !          1048:        key_free(host_key);
1.1       markus   1049:
1.11      markus   1050:        if (options.cipher == SSH_CIPHER_NOT_SET) {
                   1051:                if (cipher_mask_ssh1(1) & supported_ciphers & (1 << ssh_cipher_default))
                   1052:                        options.cipher = ssh_cipher_default;
                   1053:        } else if (options.cipher == SSH_CIPHER_ILLEGAL ||
1.10      markus   1054:            !(cipher_mask_ssh1(1) & (1 << options.cipher))) {
1.3       markus   1055:                log("No valid SSH1 cipher, using %.100s instead.",
1.7       markus   1056:                    cipher_name(ssh_cipher_default));
                   1057:                options.cipher = ssh_cipher_default;
1.1       markus   1058:        }
                   1059:        /* Check that the selected cipher is supported. */
                   1060:        if (!(supported_ciphers & (1 << options.cipher)))
                   1061:                fatal("Selected cipher type %.100s not supported by server.",
1.42      deraadt  1062:                    cipher_name(options.cipher));
1.1       markus   1063:
                   1064:        debug("Encryption type: %.100s", cipher_name(options.cipher));
                   1065:
                   1066:        /* Send the encrypted session key to the server. */
                   1067:        packet_start(SSH_CMSG_SESSION_KEY);
                   1068:        packet_put_char(options.cipher);
                   1069:
                   1070:        /* Send the cookie back to the server. */
                   1071:        for (i = 0; i < 8; i++)
                   1072:                packet_put_char(cookie[i]);
                   1073:
                   1074:        /* Send and destroy the encrypted encryption key integer. */
                   1075:        packet_put_bignum(key);
                   1076:        BN_clear_free(key);
                   1077:
                   1078:        /* Send protocol flags. */
                   1079:        packet_put_int(client_flags);
                   1080:
                   1081:        /* Send the packet now. */
                   1082:        packet_send();
                   1083:        packet_write_wait();
                   1084:
                   1085:        debug("Sent encrypted session key.");
                   1086:
                   1087:        /* Set the encryption key. */
                   1088:        packet_set_encryption_key(session_key, SSH_SESSION_KEY_LENGTH, options.cipher);
                   1089:
                   1090:        /* We will no longer need the session key here.  Destroy any extra copies. */
                   1091:        memset(session_key, 0, sizeof(session_key));
                   1092:
                   1093:        /*
                   1094:         * Expect a success message from the server.  Note that this message
                   1095:         * will be received in encrypted form.
                   1096:         */
                   1097:        packet_read_expect(&payload_len, SSH_SMSG_SUCCESS);
                   1098:
                   1099:        debug("Received encrypted confirmation.");
                   1100: }
                   1101:
                   1102: /*
                   1103:  * Authenticate user
                   1104:  */
                   1105: void
1.30      markus   1106: ssh_userauth1(const char *local_user, const char *server_user, char *host,
                   1107:     Key **keys, int nkeys)
1.1       markus   1108: {
1.37      dugsong  1109: #ifdef KRB5
                   1110:        krb5_context context = NULL;
                   1111:        krb5_auth_context auth_context = NULL;
                   1112: #endif
1.1       markus   1113:        int i, type;
                   1114:        int payload_len;
1.42      deraadt  1115:
1.1       markus   1116:        if (supported_authentications == 0)
1.30      markus   1117:                fatal("ssh_userauth1: server supports no auth methods");
1.1       markus   1118:
                   1119:        /* Send the name of the user to log in as on the server. */
                   1120:        packet_start(SSH_CMSG_USER);
1.33      markus   1121:        packet_put_cstring(server_user);
1.1       markus   1122:        packet_send();
                   1123:        packet_write_wait();
                   1124:
                   1125:        /*
                   1126:         * The server should respond with success if no authentication is
                   1127:         * needed (the user has no password).  Otherwise the server responds
                   1128:         * with failure.
                   1129:         */
                   1130:        type = packet_read(&payload_len);
                   1131:
                   1132:        /* check whether the connection was accepted without authentication. */
                   1133:        if (type == SSH_SMSG_SUCCESS)
1.37      dugsong  1134:                goto success;
1.1       markus   1135:        if (type != SSH_SMSG_FAILURE)
1.37      dugsong  1136:                packet_disconnect("Protocol error: got %d in response to SSH_CMSG_USER", type);
1.42      deraadt  1137:
1.37      dugsong  1138: #ifdef KRB5
                   1139:        if ((supported_authentications & (1 << SSH_AUTH_KERBEROS)) &&
1.42      deraadt  1140:            options.kerberos_authentication) {
1.37      dugsong  1141:                debug("Trying Kerberos v5 authentication.");
1.42      deraadt  1142:
1.37      dugsong  1143:                if (try_krb5_authentication(&context, &auth_context)) {
                   1144:                        type = packet_read(&payload_len);
                   1145:                        if (type == SSH_SMSG_SUCCESS)
                   1146:                                goto success;
                   1147:                        if (type != SSH_SMSG_FAILURE)
                   1148:                                packet_disconnect("Protocol error: got %d in response to Kerberos v5 auth", type);
                   1149:                }
1.1       markus   1150:        }
1.37      dugsong  1151: #endif /* KRB5 */
1.42      deraadt  1152:
1.1       markus   1153: #ifdef KRB4
                   1154:        if ((supported_authentications & (1 << SSH_AUTH_KERBEROS)) &&
                   1155:            options.kerberos_authentication) {
1.37      dugsong  1156:                debug("Trying Kerberos v4 authentication.");
1.42      deraadt  1157:
1.37      dugsong  1158:                if (try_krb4_authentication()) {
1.1       markus   1159:                        type = packet_read(&payload_len);
                   1160:                        if (type == SSH_SMSG_SUCCESS)
1.37      dugsong  1161:                                goto success;
1.1       markus   1162:                        if (type != SSH_SMSG_FAILURE)
1.37      dugsong  1163:                                packet_disconnect("Protocol error: got %d in response to Kerberos v4 auth", type);
1.1       markus   1164:                }
                   1165:        }
                   1166: #endif /* KRB4 */
1.42      deraadt  1167:
1.1       markus   1168:        /*
                   1169:         * Use rhosts authentication if running in privileged socket and we
                   1170:         * do not wish to remain anonymous.
                   1171:         */
                   1172:        if ((supported_authentications & (1 << SSH_AUTH_RHOSTS)) &&
                   1173:            options.rhosts_authentication) {
                   1174:                debug("Trying rhosts authentication.");
                   1175:                packet_start(SSH_CMSG_AUTH_RHOSTS);
1.33      markus   1176:                packet_put_cstring(local_user);
1.1       markus   1177:                packet_send();
                   1178:                packet_write_wait();
                   1179:
                   1180:                /* The server should respond with success or failure. */
                   1181:                type = packet_read(&payload_len);
                   1182:                if (type == SSH_SMSG_SUCCESS)
1.37      dugsong  1183:                        goto success;
1.1       markus   1184:                if (type != SSH_SMSG_FAILURE)
                   1185:                        packet_disconnect("Protocol error: got %d in response to rhosts auth",
                   1186:                                          type);
                   1187:        }
                   1188:        /*
                   1189:         * Try .rhosts or /etc/hosts.equiv authentication with RSA host
                   1190:         * authentication.
                   1191:         */
                   1192:        if ((supported_authentications & (1 << SSH_AUTH_RHOSTS_RSA)) &&
1.30      markus   1193:            options.rhosts_rsa_authentication) {
                   1194:                for (i = 0; i < nkeys; i++) {
1.31      markus   1195:                        if (keys[i] != NULL && keys[i]->type == KEY_RSA1 &&
1.30      markus   1196:                            try_rhosts_rsa_authentication(local_user, keys[i]))
1.37      dugsong  1197:                                goto success;
1.30      markus   1198:                }
1.1       markus   1199:        }
                   1200:        /* Try RSA authentication if the server supports it. */
                   1201:        if ((supported_authentications & (1 << SSH_AUTH_RSA)) &&
                   1202:            options.rsa_authentication) {
                   1203:                /*
                   1204:                 * Try RSA authentication using the authentication agent. The
                   1205:                 * agent is tried first because no passphrase is needed for
                   1206:                 * it, whereas identity files may require passphrases.
                   1207:                 */
                   1208:                if (try_agent_authentication())
1.37      dugsong  1209:                        goto success;
1.1       markus   1210:
                   1211:                /* Try RSA authentication for each identity. */
                   1212:                for (i = 0; i < options.num_identity_files; i++)
1.28      markus   1213:                        if (options.identity_keys[i] != NULL &&
                   1214:                            options.identity_keys[i]->type == KEY_RSA1 &&
1.38      markus   1215:                            try_rsa_authentication(i))
1.37      dugsong  1216:                                goto success;
1.1       markus   1217:        }
1.20      markus   1218:        /* Try challenge response authentication if the server supports it. */
1.1       markus   1219:        if ((supported_authentications & (1 << SSH_AUTH_TIS)) &&
1.32      markus   1220:            options.challenge_response_authentication && !options.batch_mode) {
                   1221:                if (try_challenge_response_authentication())
1.37      dugsong  1222:                        goto success;
1.1       markus   1223:        }
                   1224:        /* Try password authentication if the server supports it. */
                   1225:        if ((supported_authentications & (1 << SSH_AUTH_PASSWORD)) &&
                   1226:            options.password_authentication && !options.batch_mode) {
                   1227:                char prompt[80];
                   1228:
1.23      itojun   1229:                snprintf(prompt, sizeof(prompt), "%.30s@%.128s's password: ",
1.1       markus   1230:                    server_user, host);
                   1231:                if (try_password_authentication(prompt))
1.37      dugsong  1232:                        goto success;
1.1       markus   1233:        }
                   1234:        /* All authentication methods have failed.  Exit with an error message. */
                   1235:        fatal("Permission denied.");
                   1236:        /* NOTREACHED */
1.37      dugsong  1237:
                   1238:  success:
                   1239: #ifdef KRB5
                   1240:        /* Try Kerberos v5 TGT passing. */
                   1241:        if ((supported_authentications & (1 << SSH_PASS_KERBEROS_TGT)) &&
                   1242:            options.kerberos_tgt_passing && context && auth_context) {
                   1243:                if (options.cipher == SSH_CIPHER_NONE)
                   1244:                        log("WARNING: Encryption is disabled! Ticket will be transmitted in the clear!");
                   1245:                send_krb5_tgt(context, auth_context);
                   1246:        }
                   1247:        if (auth_context)
                   1248:                krb5_auth_con_free(context, auth_context);
                   1249:        if (context)
                   1250:                krb5_free_context(context);
                   1251: #endif
1.42      deraadt  1252:
1.37      dugsong  1253: #ifdef AFS
                   1254:        /* Try Kerberos v4 TGT passing if the server supports it. */
                   1255:        if ((supported_authentications & (1 << SSH_PASS_KERBEROS_TGT)) &&
                   1256:            options.kerberos_tgt_passing) {
                   1257:                if (options.cipher == SSH_CIPHER_NONE)
                   1258:                        log("WARNING: Encryption is disabled! Ticket will be transmitted in the clear!");
                   1259:                send_krb4_tgt();
                   1260:        }
                   1261:        /* Try AFS token passing if the server supports it. */
                   1262:        if ((supported_authentications & (1 << SSH_PASS_AFS_TOKEN)) &&
                   1263:            options.afs_token_passing && k_hasafs()) {
                   1264:                if (options.cipher == SSH_CIPHER_NONE)
                   1265:                        log("WARNING: Encryption is disabled! Token will be transmitted in the clear!");
                   1266:                send_afs_tokens();
                   1267:        }
                   1268: #endif /* AFS */
1.39      stevesk  1269:
                   1270:        return; /* need statement after label */
1.1       markus   1271: }