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

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