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

Annotation of src/usr.bin/ssh/sshconnect2.c, Revision 1.19

1.1       markus      1: /*
                      2:  * Copyright (c) 2000 Markus Friedl.  All rights reserved.
                      3:  *
                      4:  * Redistribution and use in source and binary forms, with or without
                      5:  * modification, are permitted provided that the following conditions
                      6:  * are met:
                      7:  * 1. Redistributions of source code must retain the above copyright
                      8:  *    notice, this list of conditions and the following disclaimer.
                      9:  * 2. Redistributions in binary form must reproduce the above copyright
                     10:  *    notice, this list of conditions and the following disclaimer in the
                     11:  *    documentation and/or other materials provided with the distribution.
                     12:  *
                     13:  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
                     14:  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
                     15:  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
                     16:  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
                     17:  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
                     18:  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
                     19:  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
                     20:  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
                     21:  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
                     22:  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
                     23:  */
                     24:
                     25: #include "includes.h"
1.19    ! markus     26: RCSID("$OpenBSD: sshconnect2.c,v 1.18 2000/09/07 20:27:55 deraadt Exp $");
1.1       markus     27:
                     28: #include <openssl/bn.h>
                     29: #include <openssl/rsa.h>
                     30: #include <openssl/dsa.h>
                     31: #include <openssl/md5.h>
                     32: #include <openssl/dh.h>
                     33: #include <openssl/hmac.h>
                     34:
                     35: #include "ssh.h"
                     36: #include "xmalloc.h"
                     37: #include "rsa.h"
                     38: #include "buffer.h"
                     39: #include "packet.h"
                     40: #include "cipher.h"
                     41: #include "uidswap.h"
                     42: #include "compat.h"
                     43: #include "readconf.h"
                     44: #include "bufaux.h"
                     45: #include "ssh2.h"
                     46: #include "kex.h"
                     47: #include "myproposal.h"
                     48: #include "key.h"
                     49: #include "dsa.h"
                     50: #include "sshconnect.h"
                     51: #include "authfile.h"
1.17      markus     52: #include "authfd.h"
1.1       markus     53:
                     54: /* import */
                     55: extern char *client_version_string;
                     56: extern char *server_version_string;
                     57: extern Options options;
                     58:
                     59: /*
                     60:  * SSH2 key exchange
                     61:  */
                     62:
                     63: unsigned char *session_id2 = NULL;
                     64: int session_id2_len = 0;
                     65:
                     66: void
1.11      markus     67: ssh_kex_dh(Kex *kex, char *host, struct sockaddr *hostaddr,
                     68:     Buffer *client_kexinit, Buffer *server_kexinit)
1.1       markus     69: {
1.19    ! markus     70: #ifdef DEBUG_KEXDH
        !            71:        int i;
        !            72: #endif
1.11      markus     73:        int plen, dlen;
1.1       markus     74:        unsigned int klen, kout;
                     75:        char *signature = NULL;
                     76:        unsigned int slen;
                     77:        char *server_host_key_blob = NULL;
                     78:        Key *server_host_key;
                     79:        unsigned int sbloblen;
                     80:        DH *dh;
                     81:        BIGNUM *dh_server_pub = 0;
                     82:        BIGNUM *shared_secret = 0;
                     83:        unsigned char *kbuf;
                     84:        unsigned char *hash;
                     85:
                     86:        debug("Sending SSH2_MSG_KEXDH_INIT.");
                     87:        /* generate and send 'e', client DH public key */
                     88:        dh = dh_new_group1();
                     89:        packet_start(SSH2_MSG_KEXDH_INIT);
                     90:        packet_put_bignum2(dh->pub_key);
                     91:        packet_send();
                     92:        packet_write_wait();
                     93:
                     94: #ifdef DEBUG_KEXDH
                     95:        fprintf(stderr, "\np= ");
1.19    ! markus     96:        BN_print_fp(stderr, dh->p);
1.1       markus     97:        fprintf(stderr, "\ng= ");
1.19    ! markus     98:        BN_print_fp(stderr, dh->g);
1.1       markus     99:        fprintf(stderr, "\npub= ");
1.19    ! markus    100:        BN_print_fp(stderr, dh->pub_key);
1.1       markus    101:        fprintf(stderr, "\n");
                    102:        DHparams_print_fp(stderr, dh);
                    103: #endif
                    104:
                    105:        debug("Wait SSH2_MSG_KEXDH_REPLY.");
                    106:
1.11      markus    107:        packet_read_expect(&plen, SSH2_MSG_KEXDH_REPLY);
1.1       markus    108:
                    109:        debug("Got SSH2_MSG_KEXDH_REPLY.");
                    110:
                    111:        /* key, cert */
                    112:        server_host_key_blob = packet_get_string(&sbloblen);
                    113:        server_host_key = dsa_key_from_blob(server_host_key_blob, sbloblen);
                    114:        if (server_host_key == NULL)
                    115:                fatal("cannot decode server_host_key_blob");
                    116:
                    117:        check_host_key(host, hostaddr, server_host_key,
                    118:            options.user_hostfile2, options.system_hostfile2);
                    119:
                    120:        /* DH paramter f, server public DH key */
                    121:        dh_server_pub = BN_new();
                    122:        if (dh_server_pub == NULL)
                    123:                fatal("dh_server_pub == NULL");
                    124:        packet_get_bignum2(dh_server_pub, &dlen);
                    125:
                    126: #ifdef DEBUG_KEXDH
                    127:        fprintf(stderr, "\ndh_server_pub= ");
1.19    ! markus    128:        BN_print_fp(stderr, dh_server_pub);
1.1       markus    129:        fprintf(stderr, "\n");
                    130:        debug("bits %d", BN_num_bits(dh_server_pub));
                    131: #endif
                    132:
                    133:        /* signed H */
                    134:        signature = packet_get_string(&slen);
                    135:        packet_done();
                    136:
                    137:        if (!dh_pub_is_valid(dh, dh_server_pub))
                    138:                packet_disconnect("bad server public DH value");
                    139:
                    140:        klen = DH_size(dh);
                    141:        kbuf = xmalloc(klen);
                    142:        kout = DH_compute_key(kbuf, dh_server_pub, dh);
                    143: #ifdef DEBUG_KEXDH
                    144:        debug("shared secret: len %d/%d", klen, kout);
                    145:        fprintf(stderr, "shared secret == ");
                    146:        for (i = 0; i< kout; i++)
                    147:                fprintf(stderr, "%02x", (kbuf[i])&0xff);
                    148:        fprintf(stderr, "\n");
                    149: #endif
                    150:        shared_secret = BN_new();
                    151:
                    152:        BN_bin2bn(kbuf, kout, shared_secret);
                    153:        memset(kbuf, 0, klen);
                    154:        xfree(kbuf);
                    155:
                    156:        /* calc and verify H */
                    157:        hash = kex_hash(
                    158:            client_version_string,
                    159:            server_version_string,
                    160:            buffer_ptr(client_kexinit), buffer_len(client_kexinit),
                    161:            buffer_ptr(server_kexinit), buffer_len(server_kexinit),
                    162:            server_host_key_blob, sbloblen,
                    163:            dh->pub_key,
                    164:            dh_server_pub,
                    165:            shared_secret
                    166:        );
1.3       markus    167:        xfree(server_host_key_blob);
1.11      markus    168:        DH_free(dh);
1.1       markus    169: #ifdef DEBUG_KEXDH
                    170:        fprintf(stderr, "hash == ");
                    171:        for (i = 0; i< 20; i++)
                    172:                fprintf(stderr, "%02x", (hash[i])&0xff);
                    173:        fprintf(stderr, "\n");
                    174: #endif
                    175:        if (dsa_verify(server_host_key, (unsigned char *)signature, slen, hash, 20) != 1)
                    176:                fatal("dsa_verify failed for server_host_key");
                    177:        key_free(server_host_key);
                    178:
                    179:        kex_derive_keys(kex, hash, shared_secret);
                    180:        packet_set_kex(kex);
                    181:
                    182:        /* save session id */
                    183:        session_id2_len = 20;
                    184:        session_id2 = xmalloc(session_id2_len);
                    185:        memcpy(session_id2, hash, session_id2_len);
1.11      markus    186: }
                    187:
                    188: void
                    189: ssh_kex2(char *host, struct sockaddr *hostaddr)
                    190: {
                    191:        int i, plen;
                    192:        Kex *kex;
                    193:        Buffer *client_kexinit, *server_kexinit;
                    194:        char *sprop[PROPOSAL_MAX];
                    195:
                    196:        if (options.ciphers != NULL) {
                    197:                myproposal[PROPOSAL_ENC_ALGS_CTOS] =
                    198:                myproposal[PROPOSAL_ENC_ALGS_STOC] = options.ciphers;
                    199:        } else if (options.cipher == SSH_CIPHER_3DES) {
                    200:                myproposal[PROPOSAL_ENC_ALGS_CTOS] =
                    201:                myproposal[PROPOSAL_ENC_ALGS_STOC] =
                    202:                    (char *) cipher_name(SSH_CIPHER_3DES_CBC);
                    203:        } else if (options.cipher == SSH_CIPHER_BLOWFISH) {
                    204:                myproposal[PROPOSAL_ENC_ALGS_CTOS] =
                    205:                myproposal[PROPOSAL_ENC_ALGS_STOC] =
                    206:                    (char *) cipher_name(SSH_CIPHER_BLOWFISH_CBC);
                    207:        }
                    208:        if (options.compression) {
                    209:                myproposal[PROPOSAL_COMP_ALGS_CTOS] = "zlib";
                    210:                myproposal[PROPOSAL_COMP_ALGS_STOC] = "zlib";
                    211:        } else {
                    212:                myproposal[PROPOSAL_COMP_ALGS_CTOS] = "none";
                    213:                myproposal[PROPOSAL_COMP_ALGS_STOC] = "none";
                    214:        }
                    215:
                    216:        /* buffers with raw kexinit messages */
                    217:        server_kexinit = xmalloc(sizeof(*server_kexinit));
                    218:        buffer_init(server_kexinit);
                    219:        client_kexinit = kex_init(myproposal);
                    220:
                    221:        /* algorithm negotiation */
                    222:        kex_exchange_kexinit(client_kexinit, server_kexinit, sprop);
                    223:        kex = kex_choose_conf(myproposal, sprop, 0);
                    224:        for (i = 0; i < PROPOSAL_MAX; i++)
                    225:                xfree(sprop[i]);
                    226:
                    227:        /* server authentication and session key agreement */
                    228:        ssh_kex_dh(kex, host, hostaddr, client_kexinit, server_kexinit);
                    229:
                    230:        buffer_free(client_kexinit);
                    231:        buffer_free(server_kexinit);
                    232:        xfree(client_kexinit);
                    233:        xfree(server_kexinit);
1.1       markus    234:
                    235:        debug("Wait SSH2_MSG_NEWKEYS.");
1.11      markus    236:        packet_read_expect(&plen, SSH2_MSG_NEWKEYS);
1.1       markus    237:        packet_done();
                    238:        debug("GOT SSH2_MSG_NEWKEYS.");
                    239:
                    240:        debug("send SSH2_MSG_NEWKEYS.");
                    241:        packet_start(SSH2_MSG_NEWKEYS);
                    242:        packet_send();
                    243:        packet_write_wait();
                    244:        debug("done: send SSH2_MSG_NEWKEYS.");
                    245:
                    246: #ifdef DEBUG_KEXDH
                    247:        /* send 1st encrypted/maced/compressed message */
                    248:        packet_start(SSH2_MSG_IGNORE);
                    249:        packet_put_cstring("markus");
                    250:        packet_send();
                    251:        packet_write_wait();
                    252: #endif
                    253:        debug("done: KEX2.");
                    254: }
1.11      markus    255:
1.1       markus    256: /*
                    257:  * Authenticate user
                    258:  */
                    259: int
                    260: ssh2_try_passwd(const char *server_user, const char *host, const char *service)
                    261: {
1.6       markus    262:        static int attempt = 0;
1.1       markus    263:        char prompt[80];
                    264:        char *password;
1.6       markus    265:
1.13      todd      266:        if (attempt++ >= options.number_of_password_prompts)
1.6       markus    267:                return 0;
1.13      todd      268:
                    269:        if(attempt != 1)
                    270:                error("Permission denied, please try again.");
1.1       markus    271:
                    272:        snprintf(prompt, sizeof(prompt), "%.30s@%.40s's password: ",
                    273:            server_user, host);
                    274:        password = read_passphrase(prompt, 0);
                    275:        packet_start(SSH2_MSG_USERAUTH_REQUEST);
                    276:        packet_put_cstring(server_user);
                    277:        packet_put_cstring(service);
                    278:        packet_put_cstring("password");
                    279:        packet_put_char(0);
                    280:        packet_put_cstring(password);
                    281:        memset(password, 0, strlen(password));
                    282:        xfree(password);
                    283:        packet_send();
                    284:        packet_write_wait();
                    285:        return 1;
                    286: }
                    287:
1.16      markus    288: typedef int sign_fn(
                    289:     Key *key,
                    290:     unsigned char **sigp, int *lenp,
                    291:     unsigned char *data, int datalen);
                    292:
1.17      markus    293: int
1.16      markus    294: ssh2_sign_and_send_pubkey(Key *k, sign_fn *do_sign,
1.1       markus    295:     const char *server_user, const char *host, const char *service)
                    296: {
                    297:        Buffer b;
                    298:        unsigned char *blob, *signature;
                    299:        int bloblen, slen;
1.14      markus    300:        int skip = 0;
1.17      markus    301:        int ret = -1;
1.1       markus    302:
                    303:        dsa_make_key_blob(k, &blob, &bloblen);
                    304:
                    305:        /* data to be signed */
                    306:        buffer_init(&b);
1.14      markus    307:        if (datafellows & SSH_COMPAT_SESSIONID_ENCODING) {
                    308:                buffer_put_string(&b, session_id2, session_id2_len);
                    309:                skip = buffer_len(&b);
                    310:        } else {
                    311:                buffer_append(&b, session_id2, session_id2_len);
                    312:                skip = session_id2_len;
                    313:        }
1.1       markus    314:        buffer_put_char(&b, SSH2_MSG_USERAUTH_REQUEST);
                    315:        buffer_put_cstring(&b, server_user);
1.10      markus    316:        buffer_put_cstring(&b,
                    317:            datafellows & SSH_BUG_PUBKEYAUTH ?
                    318:            "ssh-userauth" :
                    319:            service);
1.1       markus    320:        buffer_put_cstring(&b, "publickey");
                    321:        buffer_put_char(&b, 1);
                    322:        buffer_put_cstring(&b, KEX_DSS);
                    323:        buffer_put_string(&b, blob, bloblen);
                    324:
                    325:        /* generate signature */
1.17      markus    326:        ret = do_sign(k, &signature, &slen, buffer_ptr(&b), buffer_len(&b));
                    327:        if (ret == -1) {
                    328:                xfree(blob);
                    329:                buffer_free(&b);
                    330:                return 0;
                    331:        }
1.1       markus    332: #ifdef DEBUG_DSS
                    333:        buffer_dump(&b);
                    334: #endif
1.10      markus    335:        if (datafellows & SSH_BUG_PUBKEYAUTH) {
                    336:                buffer_clear(&b);
                    337:                buffer_append(&b, session_id2, session_id2_len);
                    338:                buffer_put_char(&b, SSH2_MSG_USERAUTH_REQUEST);
                    339:                buffer_put_cstring(&b, server_user);
                    340:                buffer_put_cstring(&b, service);
                    341:                buffer_put_cstring(&b, "publickey");
                    342:                buffer_put_char(&b, 1);
                    343:                buffer_put_cstring(&b, KEX_DSS);
                    344:                buffer_put_string(&b, blob, bloblen);
                    345:        }
                    346:        xfree(blob);
1.1       markus    347:        /* append signature */
                    348:        buffer_put_string(&b, signature, slen);
                    349:        xfree(signature);
                    350:
                    351:        /* skip session id and packet type */
1.14      markus    352:        if (buffer_len(&b) < skip + 1)
1.1       markus    353:                fatal("ssh2_try_pubkey: internal error");
1.14      markus    354:        buffer_consume(&b, skip + 1);
1.1       markus    355:
                    356:        /* put remaining data from buffer into packet */
                    357:        packet_start(SSH2_MSG_USERAUTH_REQUEST);
                    358:        packet_put_raw(buffer_ptr(&b), buffer_len(&b));
                    359:        buffer_free(&b);
                    360:
                    361:        /* send */
                    362:        packet_send();
                    363:        packet_write_wait();
1.17      markus    364:
                    365:        return 1;
1.16      markus    366: }
                    367:
                    368: int
                    369: ssh2_try_pubkey(char *filename,
                    370:     const char *server_user, const char *host, const char *service)
                    371: {
                    372:        Key *k;
1.17      markus    373:        int ret = 0;
1.16      markus    374:        struct stat st;
                    375:
                    376:        if (stat(filename, &st) != 0) {
                    377:                debug("key does not exist: %s", filename);
                    378:                return 0;
                    379:        }
                    380:        debug("try pubkey: %s", filename);
                    381:
                    382:        k = key_new(KEY_DSA);
                    383:        if (!load_private_key(filename, "", k, NULL)) {
                    384:                int success = 0;
                    385:                char *passphrase;
                    386:                char prompt[300];
                    387:                snprintf(prompt, sizeof prompt,
                    388:                     "Enter passphrase for DSA key '%.100s': ",
                    389:                     filename);
                    390:                passphrase = read_passphrase(prompt, 0);
                    391:                success = load_private_key(filename, passphrase, k, NULL);
                    392:                memset(passphrase, 0, strlen(passphrase));
                    393:                xfree(passphrase);
                    394:                if (!success) {
                    395:                        key_free(k);
                    396:                        return 0;
                    397:                }
                    398:        }
1.17      markus    399:        ret = ssh2_sign_and_send_pubkey(k, dsa_sign, server_user, host, service);
                    400:        key_free(k);
                    401:        return ret;
                    402: }
                    403:
                    404: int agent_sign(
                    405:     Key *key,
                    406:     unsigned char **sigp, int *lenp,
                    407:     unsigned char *data, int datalen)
                    408: {
                    409:        int ret = -1;
                    410:        AuthenticationConnection *ac = ssh_get_authentication_connection();
                    411:        if (ac != NULL) {
                    412:                ret = ssh_agent_sign(ac, key, sigp, lenp, data, datalen);
                    413:                ssh_close_authentication_connection(ac);
                    414:        }
                    415:        return ret;
                    416: }
                    417:
                    418: int
                    419: ssh2_try_agent(AuthenticationConnection *ac,
                    420:     const char *server_user, const char *host, const char *service)
                    421: {
                    422:        static int called = 0;
                    423:        char *comment;
                    424:        Key *k;
                    425:        int ret;
                    426:
                    427:        if (called == 0) {
                    428:                k = ssh_get_first_identity(ac, &comment, 2);
                    429:                called ++;
                    430:        } else {
                    431:                k = ssh_get_next_identity(ac, &comment, 2);
                    432:        }
                    433:        if (k == NULL)
                    434:                return 0;
                    435:        debug("trying DSA agent key %s", comment);
                    436:        xfree(comment);
                    437:        ret = ssh2_sign_and_send_pubkey(k, agent_sign, server_user, host, service);
                    438:        key_free(k);
                    439:        return ret;
1.1       markus    440: }
                    441:
                    442: void
                    443: ssh_userauth2(const char *server_user, char *host)
                    444: {
1.17      markus    445:        AuthenticationConnection *ac = ssh_get_authentication_connection();
1.1       markus    446:        int type;
                    447:        int plen;
                    448:        int sent;
                    449:        unsigned int dlen;
                    450:        int partial;
                    451:        int i = 0;
                    452:        char *auths;
                    453:        char *service = "ssh-connection";               /* service name */
                    454:
                    455:        debug("send SSH2_MSG_SERVICE_REQUEST");
                    456:        packet_start(SSH2_MSG_SERVICE_REQUEST);
                    457:        packet_put_cstring("ssh-userauth");
                    458:        packet_send();
                    459:        packet_write_wait();
                    460:
                    461:        type = packet_read(&plen);
                    462:        if (type != SSH2_MSG_SERVICE_ACCEPT) {
                    463:                fatal("denied SSH2_MSG_SERVICE_ACCEPT: %d", type);
                    464:        }
                    465:        if (packet_remaining() > 0) {
                    466:                char *reply = packet_get_string(&plen);
                    467:                debug("service_accept: %s", reply);
                    468:                xfree(reply);
                    469:        } else {
                    470:                /* payload empty for ssh-2.0.13 ?? */
1.8       markus    471:                debug("buggy server: service_accept w/o service");
1.1       markus    472:        }
                    473:        packet_done();
                    474:        debug("got SSH2_MSG_SERVICE_ACCEPT");
                    475:
                    476:        /* INITIAL request for auth */
                    477:        packet_start(SSH2_MSG_USERAUTH_REQUEST);
                    478:        packet_put_cstring(server_user);
                    479:        packet_put_cstring(service);
                    480:        packet_put_cstring("none");
                    481:        packet_send();
                    482:        packet_write_wait();
                    483:
                    484:        for (;;) {
                    485:                sent = 0;
                    486:                type = packet_read(&plen);
                    487:                if (type == SSH2_MSG_USERAUTH_SUCCESS)
                    488:                        break;
                    489:                if (type != SSH2_MSG_USERAUTH_FAILURE)
                    490:                        fatal("access denied: %d", type);
                    491:                /* SSH2_MSG_USERAUTH_FAILURE means: try again */
                    492:                auths = packet_get_string(&dlen);
                    493:                debug("authentications that can continue: %s", auths);
                    494:                partial = packet_get_char();
                    495:                packet_done();
                    496:                if (partial)
                    497:                        debug("partial success");
1.7       markus    498:                if (options.dsa_authentication &&
1.4       markus    499:                    strstr(auths, "publickey") != NULL) {
1.17      markus    500:                        if (ac != NULL)
                    501:                                sent = ssh2_try_agent(ac,
1.1       markus    502:                                    server_user, host, service);
1.17      markus    503:                        if (!sent) {
                    504:                                while (i < options.num_identity_files2) {
                    505:                                        sent = ssh2_try_pubkey(
                    506:                                            options.identity_files2[i++],
                    507:                                            server_user, host, service);
                    508:                                        if (sent)
                    509:                                                break;
                    510:                                }
1.1       markus    511:                        }
                    512:                }
                    513:                if (!sent) {
1.4       markus    514:                        if (options.password_authentication &&
                    515:                            !options.batch_mode &&
                    516:                            strstr(auths, "password") != NULL) {
1.1       markus    517:                                sent = ssh2_try_passwd(server_user, host, service);
                    518:                        }
                    519:                }
1.4       markus    520:                if (!sent)
                    521:                        fatal("Permission denied (%s).", auths);
1.1       markus    522:                xfree(auths);
                    523:        }
1.17      markus    524:        if (ac != NULL)
                    525:                ssh_close_authentication_connection(ac);
1.1       markus    526:        packet_done();
                    527:        debug("ssh-userauth2 successfull");
                    528: }