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

Annotation of src/usr.bin/ssh/kexgexc.c, Revision 1.4

1.1       markus      1: /*
                      2:  * Copyright (c) 2000 Niels Provos.  All rights reserved.
                      3:  * Copyright (c) 2001 Markus Friedl.  All rights reserved.
                      4:  *
                      5:  * Redistribution and use in source and binary forms, with or without
                      6:  * modification, are permitted provided that the following conditions
                      7:  * are met:
                      8:  * 1. Redistributions of source code must retain the above copyright
                      9:  *    notice, this list of conditions and the following disclaimer.
                     10:  * 2. Redistributions in binary form must reproduce the above copyright
                     11:  *    notice, this list of conditions and the following disclaimer in the
                     12:  *    documentation and/or other materials provided with the distribution.
                     13:  *
                     14:  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
                     15:  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
                     16:  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
                     17:  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
                     18:  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
                     19:  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
                     20:  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
                     21:  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
                     22:  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
                     23:  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
                     24:  */
                     25:
                     26: #include "includes.h"
                     27:
                     28: #include "xmalloc.h"
                     29: #include "key.h"
                     30: #include "kex.h"
                     31: #include "log.h"
                     32: #include "packet.h"
                     33: #include "dh.h"
                     34: #include "ssh2.h"
                     35: #include "compat.h"
                     36:
                     37: void
                     38: kexgex_client(Kex *kex)
                     39: {
                     40:        BIGNUM *dh_server_pub = NULL, *shared_secret = NULL;
                     41:        BIGNUM *p = NULL, *g = NULL;
                     42:        Key *server_host_key;
                     43:        u_char *kbuf, *hash, *signature = NULL, *server_host_key_blob = NULL;
1.3       djm        44:        u_int klen, kout, slen, sbloblen, hashlen;
1.1       markus     45:        int min, max, nbits;
                     46:        DH *dh;
                     47:
                     48:        nbits = dh_estimate(kex->we_need * 8);
                     49:
                     50:        if (datafellows & SSH_OLD_DHGEX) {
                     51:                /* Old GEX request */
                     52:                packet_start(SSH2_MSG_KEX_DH_GEX_REQUEST_OLD);
                     53:                packet_put_int(nbits);
                     54:                min = DH_GRP_MIN;
                     55:                max = DH_GRP_MAX;
1.2       markus     56:
                     57:                debug("SSH2_MSG_KEX_DH_GEX_REQUEST_OLD(%u) sent", nbits);
1.1       markus     58:        } else {
                     59:                /* New GEX request */
                     60:                min = DH_GRP_MIN;
                     61:                max = DH_GRP_MAX;
                     62:                packet_start(SSH2_MSG_KEX_DH_GEX_REQUEST);
                     63:                packet_put_int(min);
                     64:                packet_put_int(nbits);
                     65:                packet_put_int(max);
1.2       markus     66:
                     67:                debug("SSH2_MSG_KEX_DH_GEX_REQUEST(%u<%u<%u) sent",
                     68:                    min, nbits, max);
1.1       markus     69:        }
                     70: #ifdef DEBUG_KEXDH
                     71:        fprintf(stderr, "\nmin = %d, nbits = %d, max = %d\n",
                     72:            min, nbits, max);
                     73: #endif
                     74:        packet_send();
                     75:
                     76:        debug("expecting SSH2_MSG_KEX_DH_GEX_GROUP");
                     77:        packet_read_expect(SSH2_MSG_KEX_DH_GEX_GROUP);
                     78:
                     79:        if ((p = BN_new()) == NULL)
                     80:                fatal("BN_new");
                     81:        packet_get_bignum2(p);
                     82:        if ((g = BN_new()) == NULL)
                     83:                fatal("BN_new");
                     84:        packet_get_bignum2(g);
                     85:        packet_check_eom();
                     86:
                     87:        if (BN_num_bits(p) < min || BN_num_bits(p) > max)
                     88:                fatal("DH_GEX group out of range: %d !< %d !< %d",
                     89:                    min, BN_num_bits(p), max);
                     90:
                     91:        dh = dh_new_group(g, p);
                     92:        dh_gen_key(dh, kex->we_need * 8);
                     93:
                     94: #ifdef DEBUG_KEXDH
                     95:        DHparams_print_fp(stderr, dh);
                     96:        fprintf(stderr, "pub= ");
                     97:        BN_print_fp(stderr, dh->pub_key);
                     98:        fprintf(stderr, "\n");
                     99: #endif
                    100:
                    101:        debug("SSH2_MSG_KEX_DH_GEX_INIT sent");
                    102:        /* generate and send 'e', client DH public key */
                    103:        packet_start(SSH2_MSG_KEX_DH_GEX_INIT);
                    104:        packet_put_bignum2(dh->pub_key);
                    105:        packet_send();
                    106:
                    107:        debug("expecting SSH2_MSG_KEX_DH_GEX_REPLY");
                    108:        packet_read_expect(SSH2_MSG_KEX_DH_GEX_REPLY);
                    109:
                    110:        /* key, cert */
                    111:        server_host_key_blob = packet_get_string(&sbloblen);
                    112:        server_host_key = key_from_blob(server_host_key_blob, sbloblen);
                    113:        if (server_host_key == NULL)
                    114:                fatal("cannot decode server_host_key_blob");
                    115:        if (server_host_key->type != kex->hostkey_type)
                    116:                fatal("type mismatch for decoded server_host_key_blob");
                    117:        if (kex->verify_host_key == NULL)
                    118:                fatal("cannot verify server_host_key");
                    119:        if (kex->verify_host_key(server_host_key) == -1)
                    120:                fatal("server_host_key verification failed");
                    121:
                    122:        /* DH paramter f, server public DH key */
                    123:        if ((dh_server_pub = BN_new()) == NULL)
                    124:                fatal("dh_server_pub == NULL");
                    125:        packet_get_bignum2(dh_server_pub);
                    126:
                    127: #ifdef DEBUG_KEXDH
                    128:        fprintf(stderr, "dh_server_pub= ");
                    129:        BN_print_fp(stderr, 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_check_eom();
                    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:        dump_digest("shared secret", kbuf, kout);
                    146: #endif
                    147:        if ((shared_secret = BN_new()) == NULL)
                    148:                fatal("kexgex_client: BN_new failed");
                    149:        BN_bin2bn(kbuf, kout, shared_secret);
                    150:        memset(kbuf, 0, klen);
                    151:        xfree(kbuf);
                    152:
                    153:        if (datafellows & SSH_OLD_DHGEX)
                    154:                min = max = -1;
                    155:
                    156:        /* calc and verify H */
1.3       djm       157:        kexgex_hash(
                    158:            kex->evp_md,
1.1       markus    159:            kex->client_version_string,
                    160:            kex->server_version_string,
                    161:            buffer_ptr(&kex->my), buffer_len(&kex->my),
                    162:            buffer_ptr(&kex->peer), buffer_len(&kex->peer),
                    163:            server_host_key_blob, sbloblen,
                    164:            min, nbits, max,
                    165:            dh->p, dh->g,
                    166:            dh->pub_key,
                    167:            dh_server_pub,
1.3       djm       168:            shared_secret,
                    169:            &hash, &hashlen
1.1       markus    170:        );
1.3       djm       171:
1.1       markus    172:        /* have keys, free DH */
                    173:        DH_free(dh);
                    174:        xfree(server_host_key_blob);
                    175:        BN_clear_free(dh_server_pub);
                    176:
1.3       djm       177:        if (key_verify(server_host_key, signature, slen, hash, hashlen) != 1)
1.1       markus    178:                fatal("key_verify failed for server_host_key");
                    179:        key_free(server_host_key);
                    180:        xfree(signature);
                    181:
                    182:        /* save session id */
                    183:        if (kex->session_id == NULL) {
1.3       djm       184:                kex->session_id_len = hashlen;
1.1       markus    185:                kex->session_id = xmalloc(kex->session_id_len);
                    186:                memcpy(kex->session_id, hash, kex->session_id_len);
                    187:        }
1.3       djm       188:        kex_derive_keys(kex, hash, hashlen, shared_secret);
1.1       markus    189:        BN_clear_free(shared_secret);
                    190:
                    191:        kex_finish(kex);
                    192: }