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

1.74    ! djm         1: /* $OpenBSD: sshconnect1.c,v 1.73 2014/01/27 19:18:54 markus Exp $ */
1.1       markus      2: /*
                      3:  * Author: Tatu Ylonen <ylo@cs.hut.fi>
                      4:  * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
                      5:  *                    All rights reserved
                      6:  * Code to connect to a remote host, and to perform the client side of the
                      7:  * login (authentication) dialog.
                      8:  *
1.6       deraadt     9:  * As far as I am concerned, the code I have written for this software
                     10:  * can be used freely for any purpose.  Any derived versions of this
                     11:  * software must be clearly marked as such, and if the derived work is
                     12:  * incompatible with the protocol description in the RFC file, it must be
                     13:  * called by a name other than "ssh" or "Secure Shell".
1.1       markus     14:  */
                     15:
1.69      deraadt    16: #include <sys/types.h>
                     17: #include <sys/socket.h>
1.1       markus     18:
                     19: #include <openssl/bn.h>
1.66      stevesk    20:
1.68      stevesk    21: #include <stdio.h>
1.67      stevesk    22: #include <stdlib.h>
1.66      stevesk    23: #include <string.h>
1.69      deraadt    24: #include <signal.h>
                     25: #include <pwd.h>
1.1       markus     26:
1.69      deraadt    27: #include "xmalloc.h"
1.18      markus     28: #include "ssh.h"
                     29: #include "ssh1.h"
1.1       markus     30: #include "rsa.h"
                     31: #include "buffer.h"
                     32: #include "packet.h"
1.69      deraadt    33: #include "key.h"
                     34: #include "cipher.h"
1.58      djm        35: #include "kex.h"
1.1       markus     36: #include "uidswap.h"
1.18      markus     37: #include "log.h"
1.1       markus     38: #include "readconf.h"
1.4       markus     39: #include "authfd.h"
1.1       markus     40: #include "sshconnect.h"
                     41: #include "authfile.h"
1.57      djm        42: #include "misc.h"
1.18      markus     43: #include "canohost.h"
1.69      deraadt    44: #include "hostfile.h"
1.37      dugsong    45: #include "auth.h"
1.73      markus     46: #include "digest.h"
1.1       markus     47:
                     48: /* Session id for the current session. */
1.13      markus     49: u_char session_id[16];
                     50: u_int supported_authentications = 0;
1.1       markus     51:
                     52: extern Options options;
                     53: extern char *__progname;
                     54:
                     55: /*
                     56:  * Checks if the user has an authentication agent, and if so, tries to
                     57:  * authenticate using the agent.
                     58:  */
1.35      itojun     59: static int
1.24      itojun     60: try_agent_authentication(void)
1.1       markus     61: {
1.5       markus     62:        int type;
1.1       markus     63:        char *comment;
                     64:        AuthenticationConnection *auth;
1.13      markus     65:        u_char response[16];
                     66:        u_int i;
1.5       markus     67:        Key *key;
                     68:        BIGNUM *challenge;
1.1       markus     69:
                     70:        /* Get connection to the agent. */
                     71:        auth = ssh_get_authentication_connection();
                     72:        if (!auth)
                     73:                return 0;
                     74:
1.43      markus     75:        if ((challenge = BN_new()) == NULL)
                     76:                fatal("try_agent_authentication: BN_new failed");
1.1       markus     77:        /* Loop through identities served by the agent. */
1.5       markus     78:        for (key = ssh_get_first_identity(auth, &comment, 1);
1.42      deraadt    79:            key != NULL;
                     80:            key = ssh_get_next_identity(auth, &comment, 1)) {
1.1       markus     81:
                     82:                /* Try this identity. */
                     83:                debug("Trying RSA authentication via agent with '%.100s'", comment);
1.71      djm        84:                free(comment);
1.1       markus     85:
                     86:                /* Tell the server that we are willing to authenticate using this key. */
                     87:                packet_start(SSH_CMSG_AUTH_RSA);
1.5       markus     88:                packet_put_bignum(key->rsa->n);
1.1       markus     89:                packet_send();
                     90:                packet_write_wait();
                     91:
                     92:                /* Wait for server's response. */
1.47      markus     93:                type = packet_read();
1.1       markus     94:
1.62      djm        95:                /* The server sends failure if it doesn't like our key or
1.1       markus     96:                   does not support RSA authentication. */
                     97:                if (type == SSH_SMSG_FAILURE) {
                     98:                        debug("Server refused our key.");
1.5       markus     99:                        key_free(key);
1.1       markus    100:                        continue;
                    101:                }
                    102:                /* Otherwise it should have sent a challenge. */
                    103:                if (type != SSH_SMSG_AUTH_RSA_CHALLENGE)
                    104:                        packet_disconnect("Protocol error during RSA authentication: %d",
                    105:                                          type);
                    106:
1.46      markus    107:                packet_get_bignum(challenge);
1.45      markus    108:                packet_check_eom();
1.1       markus    109:
                    110:                debug("Received RSA challenge from server.");
                    111:
                    112:                /* Ask the agent to decrypt the challenge. */
1.5       markus    113:                if (!ssh_decrypt_challenge(auth, key, challenge, session_id, 1, response)) {
                    114:                        /*
                    115:                         * The agent failed to authenticate this identifier
                    116:                         * although it advertised it supports this.  Just
                    117:                         * return a wrong value.
                    118:                         */
1.53      itojun    119:                        logit("Authentication agent failed to decrypt challenge.");
1.74    ! djm       120:                        explicit_bzero(response, sizeof(response));
1.1       markus    121:                }
1.5       markus    122:                key_free(key);
1.1       markus    123:                debug("Sending response to RSA challenge.");
                    124:
                    125:                /* Send the decrypted challenge back to the server. */
                    126:                packet_start(SSH_CMSG_AUTH_RSA_RESPONSE);
                    127:                for (i = 0; i < 16; i++)
                    128:                        packet_put_char(response[i]);
                    129:                packet_send();
                    130:                packet_write_wait();
                    131:
                    132:                /* Wait for response from the server. */
1.47      markus    133:                type = packet_read();
1.1       markus    134:
                    135:                /* The server returns success if it accepted the authentication. */
                    136:                if (type == SSH_SMSG_SUCCESS) {
1.14      markus    137:                        ssh_close_authentication_connection(auth);
1.5       markus    138:                        BN_clear_free(challenge);
1.1       markus    139:                        debug("RSA authentication accepted by server.");
                    140:                        return 1;
                    141:                }
                    142:                /* Otherwise it should return failure. */
                    143:                if (type != SSH_SMSG_FAILURE)
                    144:                        packet_disconnect("Protocol error waiting RSA auth response: %d",
                    145:                                          type);
                    146:        }
1.14      markus    147:        ssh_close_authentication_connection(auth);
1.1       markus    148:        BN_clear_free(challenge);
                    149:        debug("RSA authentication using agent refused.");
                    150:        return 0;
                    151: }
                    152:
                    153: /*
                    154:  * Computes the proper response to a RSA challenge, and sends the response to
                    155:  * the server.
                    156:  */
1.35      itojun    157: static void
1.1       markus    158: respond_to_rsa_challenge(BIGNUM * challenge, RSA * prv)
                    159: {
1.13      markus    160:        u_char buf[32], response[16];
1.73      markus    161:        struct ssh_digest_ctx *md;
1.1       markus    162:        int i, len;
                    163:
                    164:        /* Decrypt the challenge using the private key. */
1.21      markus    165:        /* XXX think about Bleichenbacher, too */
                    166:        if (rsa_private_decrypt(challenge, challenge, prv) <= 0)
                    167:                packet_disconnect(
                    168:                    "respond_to_rsa_challenge: rsa_private_decrypt failed");
1.1       markus    169:
                    170:        /* Compute the response. */
                    171:        /* The response is MD5 of decrypted challenge plus session id. */
                    172:        len = BN_num_bytes(challenge);
1.61      djm       173:        if (len <= 0 || (u_int)len > sizeof(buf))
1.21      markus    174:                packet_disconnect(
                    175:                    "respond_to_rsa_challenge: bad challenge length %d", len);
1.1       markus    176:
                    177:        memset(buf, 0, sizeof(buf));
                    178:        BN_bn2bin(challenge, buf + sizeof(buf) - len);
1.73      markus    179:        if ((md = ssh_digest_start(SSH_DIGEST_MD5)) == NULL ||
                    180:            ssh_digest_update(md, buf, 32) < 0 ||
                    181:            ssh_digest_update(md, session_id, 16) < 0 ||
                    182:            ssh_digest_final(md, response, sizeof(response)) < 0)
                    183:                fatal("%s: md5 failed", __func__);
                    184:        ssh_digest_free(md);
1.1       markus    185:
                    186:        debug("Sending response to host key RSA challenge.");
                    187:
                    188:        /* Send the response back to the server. */
                    189:        packet_start(SSH_CMSG_AUTH_RSA_RESPONSE);
                    190:        for (i = 0; i < 16; i++)
                    191:                packet_put_char(response[i]);
                    192:        packet_send();
                    193:        packet_write_wait();
                    194:
1.74    ! djm       195:        explicit_bzero(buf, sizeof(buf));
        !           196:        explicit_bzero(response, sizeof(response));
        !           197:        explicit_bzero(&md, sizeof(md));
1.1       markus    198: }
                    199:
                    200: /*
                    201:  * Checks if the user has authentication file, and if so, tries to authenticate
                    202:  * the user using it.
                    203:  */
1.35      itojun    204: static int
1.38      markus    205: try_rsa_authentication(int idx)
1.1       markus    206: {
                    207:        BIGNUM *challenge;
1.36      markus    208:        Key *public, *private;
1.38      markus    209:        char buf[300], *passphrase, *comment, *authfile;
1.65      dtucker   210:        int i, perm_ok = 1, type, quit;
1.1       markus    211:
1.38      markus    212:        public = options.identity_keys[idx];
                    213:        authfile = options.identity_files[idx];
                    214:        comment = xstrdup(authfile);
                    215:
1.1       markus    216:        debug("Trying RSA authentication with key '%.100s'", comment);
                    217:
                    218:        /* Tell the server that we are willing to authenticate using this key. */
                    219:        packet_start(SSH_CMSG_AUTH_RSA);
                    220:        packet_put_bignum(public->rsa->n);
                    221:        packet_send();
                    222:        packet_write_wait();
                    223:
                    224:        /* Wait for server's response. */
1.47      markus    225:        type = packet_read();
1.1       markus    226:
                    227:        /*
1.62      djm       228:         * The server responds with failure if it doesn't like our key or
                    229:         * doesn't support RSA authentication.
1.1       markus    230:         */
                    231:        if (type == SSH_SMSG_FAILURE) {
                    232:                debug("Server refused our key.");
1.71      djm       233:                free(comment);
1.1       markus    234:                return 0;
                    235:        }
                    236:        /* Otherwise, the server should respond with a challenge. */
                    237:        if (type != SSH_SMSG_AUTH_RSA_CHALLENGE)
                    238:                packet_disconnect("Protocol error during RSA authentication: %d", type);
                    239:
                    240:        /* Get the challenge from the packet. */
1.43      markus    241:        if ((challenge = BN_new()) == NULL)
                    242:                fatal("try_rsa_authentication: BN_new failed");
1.46      markus    243:        packet_get_bignum(challenge);
1.45      markus    244:        packet_check_eom();
1.1       markus    245:
                    246:        debug("Received RSA challenge from server.");
                    247:
                    248:        /*
1.38      markus    249:         * If the key is not stored in external hardware, we have to
                    250:         * load the private key.  Try first with empty passphrase; if it
1.1       markus    251:         * fails, ask for a passphrase.
                    252:         */
1.52      aaron     253:        if (public->flags & KEY_FLAG_EXT)
1.38      markus    254:                private = public;
                    255:        else
1.65      dtucker   256:                private = key_load_private_type(KEY_RSA1, authfile, "", NULL,
                    257:                    &perm_ok);
                    258:        if (private == NULL && !options.batch_mode && perm_ok) {
1.36      markus    259:                snprintf(buf, sizeof(buf),
                    260:                    "Enter passphrase for RSA key '%.100s': ", comment);
                    261:                for (i = 0; i < options.number_of_password_prompts; i++) {
1.1       markus    262:                        passphrase = read_passphrase(buf, 0);
1.36      markus    263:                        if (strcmp(passphrase, "") != 0) {
                    264:                                private = key_load_private_type(KEY_RSA1,
1.65      dtucker   265:                                    authfile, passphrase, NULL, NULL);
1.36      markus    266:                                quit = 0;
                    267:                        } else {
                    268:                                debug2("no passphrase given, try next key");
                    269:                                quit = 1;
                    270:                        }
1.74    ! djm       271:                        explicit_bzero(passphrase, strlen(passphrase));
1.71      djm       272:                        free(passphrase);
1.36      markus    273:                        if (private != NULL || quit)
                    274:                                break;
                    275:                        debug2("bad passphrase given, try again...");
1.1       markus    276:                }
                    277:        }
                    278:        /* We no longer need the comment. */
1.71      djm       279:        free(comment);
1.36      markus    280:
                    281:        if (private == NULL) {
1.65      dtucker   282:                if (!options.batch_mode && perm_ok)
1.36      markus    283:                        error("Bad passphrase.");
                    284:
                    285:                /* Send a dummy response packet to avoid protocol error. */
                    286:                packet_start(SSH_CMSG_AUTH_RSA_RESPONSE);
                    287:                for (i = 0; i < 16; i++)
                    288:                        packet_put_char(0);
                    289:                packet_send();
                    290:                packet_write_wait();
                    291:
                    292:                /* Expect the server to reject it... */
1.47      markus    293:                packet_read_expect(SSH_SMSG_FAILURE);
1.36      markus    294:                BN_clear_free(challenge);
                    295:                return 0;
                    296:        }
1.1       markus    297:
                    298:        /* Compute and send a response to the challenge. */
                    299:        respond_to_rsa_challenge(challenge, private->rsa);
                    300:
1.38      markus    301:        /* Destroy the private key unless it in external hardware. */
                    302:        if (!(private->flags & KEY_FLAG_EXT))
                    303:                key_free(private);
1.1       markus    304:
                    305:        /* We no longer need the challenge. */
                    306:        BN_clear_free(challenge);
                    307:
                    308:        /* Wait for response from the server. */
1.47      markus    309:        type = packet_read();
1.1       markus    310:        if (type == SSH_SMSG_SUCCESS) {
                    311:                debug("RSA authentication accepted by server.");
                    312:                return 1;
                    313:        }
                    314:        if (type != SSH_SMSG_FAILURE)
                    315:                packet_disconnect("Protocol error waiting RSA auth response: %d", type);
                    316:        debug("RSA authentication refused.");
                    317:        return 0;
                    318: }
                    319:
                    320: /*
                    321:  * Tries to authenticate the user using combined rhosts or /etc/hosts.equiv
                    322:  * authentication and RSA host authentication.
                    323:  */
1.35      itojun    324: static int
1.29      markus    325: try_rhosts_rsa_authentication(const char *local_user, Key * host_key)
1.1       markus    326: {
                    327:        int type;
                    328:        BIGNUM *challenge;
                    329:
                    330:        debug("Trying rhosts or /etc/hosts.equiv with RSA host authentication.");
                    331:
                    332:        /* Tell the server that we are willing to authenticate using this key. */
                    333:        packet_start(SSH_CMSG_AUTH_RHOSTS_RSA);
1.33      markus    334:        packet_put_cstring(local_user);
1.29      markus    335:        packet_put_int(BN_num_bits(host_key->rsa->n));
                    336:        packet_put_bignum(host_key->rsa->e);
                    337:        packet_put_bignum(host_key->rsa->n);
1.1       markus    338:        packet_send();
                    339:        packet_write_wait();
                    340:
                    341:        /* Wait for server's response. */
1.47      markus    342:        type = packet_read();
1.1       markus    343:
                    344:        /* The server responds with failure if it doesn't admit our
                    345:           .rhosts authentication or doesn't know our host key. */
                    346:        if (type == SSH_SMSG_FAILURE) {
                    347:                debug("Server refused our rhosts authentication or host key.");
                    348:                return 0;
                    349:        }
                    350:        /* Otherwise, the server should respond with a challenge. */
                    351:        if (type != SSH_SMSG_AUTH_RSA_CHALLENGE)
                    352:                packet_disconnect("Protocol error during RSA authentication: %d", type);
                    353:
                    354:        /* Get the challenge from the packet. */
1.43      markus    355:        if ((challenge = BN_new()) == NULL)
                    356:                fatal("try_rhosts_rsa_authentication: BN_new failed");
1.46      markus    357:        packet_get_bignum(challenge);
1.45      markus    358:        packet_check_eom();
1.1       markus    359:
                    360:        debug("Received RSA challenge for host key from server.");
                    361:
                    362:        /* Compute a response to the challenge. */
1.29      markus    363:        respond_to_rsa_challenge(challenge, host_key->rsa);
1.1       markus    364:
                    365:        /* We no longer need the challenge. */
                    366:        BN_clear_free(challenge);
                    367:
                    368:        /* Wait for response from the server. */
1.47      markus    369:        type = packet_read();
1.1       markus    370:        if (type == SSH_SMSG_SUCCESS) {
                    371:                debug("Rhosts or /etc/hosts.equiv with RSA host authentication accepted by server.");
                    372:                return 1;
                    373:        }
                    374:        if (type != SSH_SMSG_FAILURE)
                    375:                packet_disconnect("Protocol error waiting RSA auth response: %d", type);
                    376:        debug("Rhosts or /etc/hosts.equiv with RSA host authentication refused.");
                    377:        return 0;
                    378: }
                    379:
                    380: /*
                    381:  * Tries to authenticate with any string-based challenge/response system.
                    382:  * Note that the client code is not tied to s/key or TIS.
                    383:  */
1.35      itojun    384: static int
1.32      markus    385: try_challenge_response_authentication(void)
1.1       markus    386: {
                    387:        int type, i;
1.13      markus    388:        u_int clen;
1.12      markus    389:        char prompt[1024];
1.1       markus    390:        char *challenge, *response;
1.40      markus    391:
                    392:        debug("Doing challenge response authentication.");
                    393:
1.12      markus    394:        for (i = 0; i < options.number_of_password_prompts; i++) {
                    395:                /* request a challenge */
                    396:                packet_start(SSH_CMSG_AUTH_TIS);
                    397:                packet_send();
                    398:                packet_write_wait();
1.1       markus    399:
1.47      markus    400:                type = packet_read();
1.12      markus    401:                if (type != SSH_SMSG_FAILURE &&
                    402:                    type != SSH_SMSG_AUTH_TIS_CHALLENGE) {
                    403:                        packet_disconnect("Protocol error: got %d in response "
1.20      markus    404:                            "to SSH_CMSG_AUTH_TIS", type);
1.12      markus    405:                }
                    406:                if (type != SSH_SMSG_AUTH_TIS_CHALLENGE) {
1.20      markus    407:                        debug("No challenge.");
1.12      markus    408:                        return 0;
                    409:                }
                    410:                challenge = packet_get_string(&clen);
1.45      markus    411:                packet_check_eom();
1.16      markus    412:                snprintf(prompt, sizeof prompt, "%s%s", challenge,
1.42      deraadt   413:                    strchr(challenge, '\n') ? "" : "\nResponse: ");
1.71      djm       414:                free(challenge);
1.1       markus    415:                if (i != 0)
                    416:                        error("Permission denied, please try again.");
1.12      markus    417:                if (options.cipher == SSH_CIPHER_NONE)
1.53      itojun    418:                        logit("WARNING: Encryption is disabled! "
1.50      stevesk   419:                            "Response will be transmitted in clear text.");
1.12      markus    420:                response = read_passphrase(prompt, 0);
                    421:                if (strcmp(response, "") == 0) {
1.71      djm       422:                        free(response);
1.12      markus    423:                        break;
                    424:                }
1.1       markus    425:                packet_start(SSH_CMSG_AUTH_TIS_RESPONSE);
1.27      markus    426:                ssh_put_password(response);
1.74    ! djm       427:                explicit_bzero(response, strlen(response));
1.71      djm       428:                free(response);
1.1       markus    429:                packet_send();
                    430:                packet_write_wait();
1.47      markus    431:                type = packet_read();
1.1       markus    432:                if (type == SSH_SMSG_SUCCESS)
                    433:                        return 1;
                    434:                if (type != SSH_SMSG_FAILURE)
                    435:                        packet_disconnect("Protocol error: got %d in response "
1.20      markus    436:                            "to SSH_CMSG_AUTH_TIS_RESPONSE", type);
1.1       markus    437:        }
                    438:        /* failure */
                    439:        return 0;
                    440: }
                    441:
                    442: /*
                    443:  * Tries to authenticate with plain passwd authentication.
                    444:  */
1.35      itojun    445: static int
1.1       markus    446: try_password_authentication(char *prompt)
                    447: {
1.47      markus    448:        int type, i;
1.1       markus    449:        char *password;
                    450:
                    451:        debug("Doing password authentication.");
                    452:        if (options.cipher == SSH_CIPHER_NONE)
1.53      itojun    453:                logit("WARNING: Encryption is disabled! Password will be transmitted in clear text.");
1.1       markus    454:        for (i = 0; i < options.number_of_password_prompts; i++) {
                    455:                if (i != 0)
                    456:                        error("Permission denied, please try again.");
                    457:                password = read_passphrase(prompt, 0);
                    458:                packet_start(SSH_CMSG_AUTH_PASSWORD);
1.27      markus    459:                ssh_put_password(password);
1.74    ! djm       460:                explicit_bzero(password, strlen(password));
1.71      djm       461:                free(password);
1.1       markus    462:                packet_send();
                    463:                packet_write_wait();
                    464:
1.47      markus    465:                type = packet_read();
1.1       markus    466:                if (type == SSH_SMSG_SUCCESS)
                    467:                        return 1;
                    468:                if (type != SSH_SMSG_FAILURE)
                    469:                        packet_disconnect("Protocol error: got %d in response to passwd auth", type);
                    470:        }
                    471:        /* failure */
                    472:        return 0;
                    473: }
                    474:
                    475: /*
                    476:  * SSH1 key exchange
                    477:  */
                    478: void
                    479: ssh_kex(char *host, struct sockaddr *hostaddr)
                    480: {
                    481:        int i;
                    482:        BIGNUM *key;
1.43      markus    483:        Key *host_key, *server_key;
1.1       markus    484:        int bits, rbits;
                    485:        int ssh_cipher_default = SSH_CIPHER_3DES;
1.13      markus    486:        u_char session_key[SSH_SESSION_KEY_LENGTH];
                    487:        u_char cookie[8];
                    488:        u_int supported_ciphers;
                    489:        u_int server_flags, client_flags;
1.59      avsm      490:        u_int32_t rnd = 0;
1.1       markus    491:
                    492:        debug("Waiting for server public key.");
                    493:
                    494:        /* Wait for a public key packet from the server. */
1.47      markus    495:        packet_read_expect(SSH_SMSG_PUBLIC_KEY);
1.1       markus    496:
                    497:        /* Get cookie from the packet. */
                    498:        for (i = 0; i < 8; i++)
                    499:                cookie[i] = packet_get_char();
                    500:
                    501:        /* Get the public key. */
1.43      markus    502:        server_key = key_new(KEY_RSA1);
                    503:        bits = packet_get_int();
1.46      markus    504:        packet_get_bignum(server_key->rsa->e);
                    505:        packet_get_bignum(server_key->rsa->n);
1.1       markus    506:
1.43      markus    507:        rbits = BN_num_bits(server_key->rsa->n);
1.1       markus    508:        if (bits != rbits) {
1.53      itojun    509:                logit("Warning: Server lies about size of server public key: "
1.1       markus    510:                    "actual size is %d bits vs. announced %d.", rbits, bits);
1.53      itojun    511:                logit("Warning: This may be due to an old implementation of ssh.");
1.1       markus    512:        }
                    513:        /* Get the host key. */
1.43      markus    514:        host_key = key_new(KEY_RSA1);
                    515:        bits = packet_get_int();
1.46      markus    516:        packet_get_bignum(host_key->rsa->e);
                    517:        packet_get_bignum(host_key->rsa->n);
1.1       markus    518:
1.43      markus    519:        rbits = BN_num_bits(host_key->rsa->n);
1.1       markus    520:        if (bits != rbits) {
1.53      itojun    521:                logit("Warning: Server lies about size of server host key: "
1.1       markus    522:                    "actual size is %d bits vs. announced %d.", rbits, bits);
1.53      itojun    523:                logit("Warning: This may be due to an old implementation of ssh.");
1.1       markus    524:        }
                    525:
                    526:        /* Get protocol flags. */
                    527:        server_flags = packet_get_int();
                    528:        packet_set_protocol_flags(server_flags);
                    529:
                    530:        supported_ciphers = packet_get_int();
                    531:        supported_authentications = packet_get_int();
1.45      markus    532:        packet_check_eom();
1.1       markus    533:
                    534:        debug("Received server public key (%d bits) and host key (%d bits).",
1.43      markus    535:            BN_num_bits(server_key->rsa->n), BN_num_bits(host_key->rsa->n));
1.1       markus    536:
1.43      markus    537:        if (verify_host_key(host, hostaddr, host_key) == -1)
1.41      markus    538:                fatal("Host key verification failed.");
1.1       markus    539:
                    540:        client_flags = SSH_PROTOFLAG_SCREEN_NUMBER | SSH_PROTOFLAG_HOST_IN_FWD_OPEN;
                    541:
1.58      djm       542:        derive_ssh1_session_id(host_key->rsa->n, server_key->rsa->n, cookie, session_id);
1.1       markus    543:
                    544:        /*
                    545:         * Generate an encryption key for the session.   The key is a 256 bit
                    546:         * random number, interpreted as a 32-byte key, with the least
                    547:         * significant 8 bits being the first byte of the key.
                    548:         */
                    549:        for (i = 0; i < 32; i++) {
                    550:                if (i % 4 == 0)
1.59      avsm      551:                        rnd = arc4random();
                    552:                session_key[i] = rnd & 0xff;
                    553:                rnd >>= 8;
1.1       markus    554:        }
                    555:
                    556:        /*
                    557:         * According to the protocol spec, the first byte of the session key
                    558:         * is the highest byte of the integer.  The session key is xored with
                    559:         * the first 16 bytes of the session id.
                    560:         */
1.43      markus    561:        if ((key = BN_new()) == NULL)
1.70      markus    562:                fatal("ssh_kex: BN_new failed");
                    563:        if (BN_set_word(key, 0) == 0)
                    564:                fatal("ssh_kex: BN_set_word failed");
1.1       markus    565:        for (i = 0; i < SSH_SESSION_KEY_LENGTH; i++) {
1.70      markus    566:                if (BN_lshift(key, key, 8) == 0)
                    567:                        fatal("ssh_kex: BN_lshift failed");
                    568:                if (i < 16) {
                    569:                        if (BN_add_word(key, session_key[i] ^ session_id[i])
                    570:                            == 0)
                    571:                                fatal("ssh_kex: BN_add_word failed");
                    572:                } else {
                    573:                        if (BN_add_word(key, session_key[i]) == 0)
                    574:                                fatal("ssh_kex: BN_add_word failed");
                    575:                }
1.1       markus    576:        }
                    577:
                    578:        /*
                    579:         * Encrypt the integer using the public key and host key of the
                    580:         * server (key with smaller modulus first).
                    581:         */
1.43      markus    582:        if (BN_cmp(server_key->rsa->n, host_key->rsa->n) < 0) {
1.1       markus    583:                /* Public key has smaller modulus. */
1.43      markus    584:                if (BN_num_bits(host_key->rsa->n) <
                    585:                    BN_num_bits(server_key->rsa->n) + SSH_KEY_BITS_RESERVED) {
                    586:                        fatal("respond_to_rsa_challenge: host_key %d < server_key %d + "
1.42      deraadt   587:                            "SSH_KEY_BITS_RESERVED %d",
1.43      markus    588:                            BN_num_bits(host_key->rsa->n),
                    589:                            BN_num_bits(server_key->rsa->n),
1.42      deraadt   590:                            SSH_KEY_BITS_RESERVED);
1.1       markus    591:                }
1.43      markus    592:                rsa_public_encrypt(key, key, server_key->rsa);
                    593:                rsa_public_encrypt(key, key, host_key->rsa);
1.1       markus    594:        } else {
                    595:                /* Host key has smaller modulus (or they are equal). */
1.43      markus    596:                if (BN_num_bits(server_key->rsa->n) <
                    597:                    BN_num_bits(host_key->rsa->n) + SSH_KEY_BITS_RESERVED) {
                    598:                        fatal("respond_to_rsa_challenge: server_key %d < host_key %d + "
1.42      deraadt   599:                            "SSH_KEY_BITS_RESERVED %d",
1.43      markus    600:                            BN_num_bits(server_key->rsa->n),
                    601:                            BN_num_bits(host_key->rsa->n),
1.42      deraadt   602:                            SSH_KEY_BITS_RESERVED);
1.1       markus    603:                }
1.43      markus    604:                rsa_public_encrypt(key, key, host_key->rsa);
                    605:                rsa_public_encrypt(key, key, server_key->rsa);
1.1       markus    606:        }
                    607:
                    608:        /* Destroy the public keys since we no longer need them. */
1.43      markus    609:        key_free(server_key);
                    610:        key_free(host_key);
1.1       markus    611:
1.11      markus    612:        if (options.cipher == SSH_CIPHER_NOT_SET) {
                    613:                if (cipher_mask_ssh1(1) & supported_ciphers & (1 << ssh_cipher_default))
                    614:                        options.cipher = ssh_cipher_default;
1.60      markus    615:        } else if (options.cipher == SSH_CIPHER_INVALID ||
1.10      markus    616:            !(cipher_mask_ssh1(1) & (1 << options.cipher))) {
1.53      itojun    617:                logit("No valid SSH1 cipher, using %.100s instead.",
1.7       markus    618:                    cipher_name(ssh_cipher_default));
                    619:                options.cipher = ssh_cipher_default;
1.1       markus    620:        }
                    621:        /* Check that the selected cipher is supported. */
                    622:        if (!(supported_ciphers & (1 << options.cipher)))
                    623:                fatal("Selected cipher type %.100s not supported by server.",
1.42      deraadt   624:                    cipher_name(options.cipher));
1.1       markus    625:
                    626:        debug("Encryption type: %.100s", cipher_name(options.cipher));
                    627:
                    628:        /* Send the encrypted session key to the server. */
                    629:        packet_start(SSH_CMSG_SESSION_KEY);
                    630:        packet_put_char(options.cipher);
                    631:
                    632:        /* Send the cookie back to the server. */
                    633:        for (i = 0; i < 8; i++)
                    634:                packet_put_char(cookie[i]);
                    635:
                    636:        /* Send and destroy the encrypted encryption key integer. */
                    637:        packet_put_bignum(key);
                    638:        BN_clear_free(key);
                    639:
                    640:        /* Send protocol flags. */
                    641:        packet_put_int(client_flags);
                    642:
                    643:        /* Send the packet now. */
                    644:        packet_send();
                    645:        packet_write_wait();
                    646:
                    647:        debug("Sent encrypted session key.");
                    648:
                    649:        /* Set the encryption key. */
                    650:        packet_set_encryption_key(session_key, SSH_SESSION_KEY_LENGTH, options.cipher);
                    651:
1.74    ! djm       652:        /*
        !           653:         * We will no longer need the session key here.
        !           654:         * Destroy any extra copies.
        !           655:         */
        !           656:        explicit_bzero(session_key, sizeof(session_key));
1.1       markus    657:
                    658:        /*
                    659:         * Expect a success message from the server.  Note that this message
                    660:         * will be received in encrypted form.
                    661:         */
1.47      markus    662:        packet_read_expect(SSH_SMSG_SUCCESS);
1.1       markus    663:
                    664:        debug("Received encrypted confirmation.");
                    665: }
                    666:
                    667: /*
                    668:  * Authenticate user
                    669:  */
                    670: void
1.30      markus    671: ssh_userauth1(const char *local_user, const char *server_user, char *host,
1.51      markus    672:     Sensitive *sensitive)
1.1       markus    673: {
                    674:        int i, type;
1.42      deraadt   675:
1.1       markus    676:        if (supported_authentications == 0)
1.30      markus    677:                fatal("ssh_userauth1: server supports no auth methods");
1.1       markus    678:
                    679:        /* Send the name of the user to log in as on the server. */
                    680:        packet_start(SSH_CMSG_USER);
1.33      markus    681:        packet_put_cstring(server_user);
1.1       markus    682:        packet_send();
                    683:        packet_write_wait();
                    684:
                    685:        /*
                    686:         * The server should respond with success if no authentication is
                    687:         * needed (the user has no password).  Otherwise the server responds
                    688:         * with failure.
                    689:         */
1.47      markus    690:        type = packet_read();
1.1       markus    691:
                    692:        /* check whether the connection was accepted without authentication. */
                    693:        if (type == SSH_SMSG_SUCCESS)
1.37      dugsong   694:                goto success;
1.1       markus    695:        if (type != SSH_SMSG_FAILURE)
1.37      dugsong   696:                packet_disconnect("Protocol error: got %d in response to SSH_CMSG_USER", type);
1.42      deraadt   697:
1.1       markus    698:        /*
                    699:         * Try .rhosts or /etc/hosts.equiv authentication with RSA host
                    700:         * authentication.
                    701:         */
                    702:        if ((supported_authentications & (1 << SSH_AUTH_RHOSTS_RSA)) &&
1.30      markus    703:            options.rhosts_rsa_authentication) {
1.51      markus    704:                for (i = 0; i < sensitive->nkeys; i++) {
                    705:                        if (sensitive->keys[i] != NULL &&
                    706:                            sensitive->keys[i]->type == KEY_RSA1 &&
                    707:                            try_rhosts_rsa_authentication(local_user,
                    708:                            sensitive->keys[i]))
1.37      dugsong   709:                                goto success;
1.30      markus    710:                }
1.1       markus    711:        }
                    712:        /* Try RSA authentication if the server supports it. */
                    713:        if ((supported_authentications & (1 << SSH_AUTH_RSA)) &&
                    714:            options.rsa_authentication) {
                    715:                /*
                    716:                 * Try RSA authentication using the authentication agent. The
                    717:                 * agent is tried first because no passphrase is needed for
                    718:                 * it, whereas identity files may require passphrases.
                    719:                 */
                    720:                if (try_agent_authentication())
1.37      dugsong   721:                        goto success;
1.1       markus    722:
                    723:                /* Try RSA authentication for each identity. */
                    724:                for (i = 0; i < options.num_identity_files; i++)
1.28      markus    725:                        if (options.identity_keys[i] != NULL &&
                    726:                            options.identity_keys[i]->type == KEY_RSA1 &&
1.38      markus    727:                            try_rsa_authentication(i))
1.37      dugsong   728:                                goto success;
1.1       markus    729:        }
1.20      markus    730:        /* Try challenge response authentication if the server supports it. */
1.1       markus    731:        if ((supported_authentications & (1 << SSH_AUTH_TIS)) &&
1.32      markus    732:            options.challenge_response_authentication && !options.batch_mode) {
                    733:                if (try_challenge_response_authentication())
1.37      dugsong   734:                        goto success;
1.1       markus    735:        }
                    736:        /* Try password authentication if the server supports it. */
                    737:        if ((supported_authentications & (1 << SSH_AUTH_PASSWORD)) &&
                    738:            options.password_authentication && !options.batch_mode) {
                    739:                char prompt[80];
                    740:
1.23      itojun    741:                snprintf(prompt, sizeof(prompt), "%.30s@%.128s's password: ",
1.1       markus    742:                    server_user, host);
                    743:                if (try_password_authentication(prompt))
1.37      dugsong   744:                        goto success;
1.1       markus    745:        }
                    746:        /* All authentication methods have failed.  Exit with an error message. */
                    747:        fatal("Permission denied.");
                    748:        /* NOTREACHED */
1.37      dugsong   749:
                    750:  success:
1.39      stevesk   751:        return; /* need statement after label */
1.1       markus    752: }