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

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