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

Annotation of src/usr.bin/ssh/kexgexs.c, Revision 1.5

1.5     ! stevesk     1: /* $OpenBSD: kexgexs.c,v 1.4 2006/03/25 13:17:02 djm Exp $ */
1.1       markus      2: /*
                      3:  * Copyright (c) 2000 Niels Provos.  All rights reserved.
                      4:  * Copyright (c) 2001 Markus Friedl.  All rights reserved.
                      5:  *
                      6:  * Redistribution and use in source and binary forms, with or without
                      7:  * modification, are permitted provided that the following conditions
                      8:  * are met:
                      9:  * 1. Redistributions of source code must retain the above copyright
                     10:  *    notice, this list of conditions and the following disclaimer.
                     11:  * 2. Redistributions in binary form must reproduce the above copyright
                     12:  *    notice, this list of conditions and the following disclaimer in the
                     13:  *    documentation and/or other materials provided with the distribution.
                     14:  *
                     15:  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
                     16:  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
                     17:  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
                     18:  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
                     19:  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
                     20:  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
                     21:  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
                     22:  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
                     23:  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
                     24:  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
                     25:  */
                     26:
                     27: #include "includes.h"
1.5     ! stevesk    28:
        !            29: #include <string.h>
1.1       markus     30:
                     31: #include "xmalloc.h"
                     32: #include "key.h"
                     33: #include "kex.h"
                     34: #include "log.h"
                     35: #include "packet.h"
                     36: #include "dh.h"
                     37: #include "ssh2.h"
                     38: #include "compat.h"
                     39: #include "monitor_wrap.h"
                     40:
                     41: void
                     42: kexgex_server(Kex *kex)
                     43: {
                     44:        BIGNUM *shared_secret = NULL, *dh_client_pub = NULL;
                     45:        Key *server_host_key;
                     46:        DH *dh;
                     47:        u_char *kbuf, *hash, *signature = NULL, *server_host_key_blob = NULL;
1.2       djm        48:        u_int sbloblen, klen, kout, slen, hashlen;
1.1       markus     49:        int min = -1, max = -1, nbits = -1, type;
                     50:
                     51:        if (kex->load_host_key == NULL)
                     52:                fatal("Cannot load hostkey");
                     53:        server_host_key = kex->load_host_key(kex->hostkey_type);
                     54:        if (server_host_key == NULL)
                     55:                fatal("Unsupported hostkey type %d", kex->hostkey_type);
                     56:
                     57:        type = packet_read();
                     58:        switch (type) {
                     59:        case SSH2_MSG_KEX_DH_GEX_REQUEST:
                     60:                debug("SSH2_MSG_KEX_DH_GEX_REQUEST received");
                     61:                min = packet_get_int();
                     62:                nbits = packet_get_int();
                     63:                max = packet_get_int();
                     64:                min = MAX(DH_GRP_MIN, min);
                     65:                max = MIN(DH_GRP_MAX, max);
                     66:                break;
                     67:        case SSH2_MSG_KEX_DH_GEX_REQUEST_OLD:
                     68:                debug("SSH2_MSG_KEX_DH_GEX_REQUEST_OLD received");
                     69:                nbits = packet_get_int();
                     70:                min = DH_GRP_MIN;
                     71:                max = DH_GRP_MAX;
                     72:                /* unused for old GEX */
                     73:                break;
                     74:        default:
                     75:                fatal("protocol error during kex, no DH_GEX_REQUEST: %d", type);
                     76:        }
                     77:        packet_check_eom();
                     78:
                     79:        if (max < min || nbits < min || max < nbits)
                     80:                fatal("DH_GEX_REQUEST, bad parameters: %d !< %d !< %d",
                     81:                    min, nbits, max);
                     82:
                     83:        /* Contact privileged parent */
                     84:        dh = PRIVSEP(choose_dh(min, nbits, max));
                     85:        if (dh == NULL)
                     86:                packet_disconnect("Protocol error: no matching DH grp found");
                     87:
                     88:        debug("SSH2_MSG_KEX_DH_GEX_GROUP sent");
                     89:        packet_start(SSH2_MSG_KEX_DH_GEX_GROUP);
                     90:        packet_put_bignum2(dh->p);
                     91:        packet_put_bignum2(dh->g);
                     92:        packet_send();
                     93:
                     94:        /* flush */
                     95:        packet_write_wait();
                     96:
                     97:        /* Compute our exchange value in parallel with the client */
                     98:        dh_gen_key(dh, kex->we_need * 8);
                     99:
                    100:        debug("expecting SSH2_MSG_KEX_DH_GEX_INIT");
                    101:        packet_read_expect(SSH2_MSG_KEX_DH_GEX_INIT);
                    102:
                    103:        /* key, cert */
                    104:        if ((dh_client_pub = BN_new()) == NULL)
                    105:                fatal("dh_client_pub == NULL");
                    106:        packet_get_bignum2(dh_client_pub);
                    107:        packet_check_eom();
                    108:
                    109: #ifdef DEBUG_KEXDH
                    110:        fprintf(stderr, "dh_client_pub= ");
                    111:        BN_print_fp(stderr, dh_client_pub);
                    112:        fprintf(stderr, "\n");
                    113:        debug("bits %d", BN_num_bits(dh_client_pub));
                    114: #endif
                    115:
                    116: #ifdef DEBUG_KEXDH
                    117:        DHparams_print_fp(stderr, dh);
                    118:        fprintf(stderr, "pub= ");
                    119:        BN_print_fp(stderr, dh->pub_key);
                    120:        fprintf(stderr, "\n");
                    121: #endif
                    122:        if (!dh_pub_is_valid(dh, dh_client_pub))
                    123:                packet_disconnect("bad client public DH value");
                    124:
                    125:        klen = DH_size(dh);
                    126:        kbuf = xmalloc(klen);
                    127:        kout = DH_compute_key(kbuf, dh_client_pub, dh);
                    128: #ifdef DEBUG_KEXDH
                    129:        dump_digest("shared secret", kbuf, kout);
                    130: #endif
                    131:        if ((shared_secret = BN_new()) == NULL)
                    132:                fatal("kexgex_server: BN_new failed");
                    133:        BN_bin2bn(kbuf, kout, shared_secret);
                    134:        memset(kbuf, 0, klen);
                    135:        xfree(kbuf);
                    136:
                    137:        key_to_blob(server_host_key, &server_host_key_blob, &sbloblen);
                    138:
                    139:        if (type == SSH2_MSG_KEX_DH_GEX_REQUEST_OLD)
                    140:                min = max = -1;
                    141:
1.2       djm       142:        /* calc H */
                    143:        kexgex_hash(
                    144:            kex->evp_md,
1.1       markus    145:            kex->client_version_string,
                    146:            kex->server_version_string,
                    147:            buffer_ptr(&kex->peer), buffer_len(&kex->peer),
                    148:            buffer_ptr(&kex->my), buffer_len(&kex->my),
                    149:            server_host_key_blob, sbloblen,
                    150:            min, nbits, max,
                    151:            dh->p, dh->g,
                    152:            dh_client_pub,
                    153:            dh->pub_key,
1.2       djm       154:            shared_secret,
                    155:            &hash, &hashlen
1.1       markus    156:        );
                    157:        BN_clear_free(dh_client_pub);
                    158:
                    159:        /* save session id := H */
                    160:        if (kex->session_id == NULL) {
1.2       djm       161:                kex->session_id_len = hashlen;
1.1       markus    162:                kex->session_id = xmalloc(kex->session_id_len);
                    163:                memcpy(kex->session_id, hash, kex->session_id_len);
                    164:        }
                    165:
                    166:        /* sign H */
1.2       djm       167:        PRIVSEP(key_sign(server_host_key, &signature, &slen, hash, hashlen));
1.1       markus    168:
                    169:        /* destroy_sensitive_data(); */
                    170:
                    171:        /* send server hostkey, DH pubkey 'f' and singed H */
                    172:        debug("SSH2_MSG_KEX_DH_GEX_REPLY sent");
                    173:        packet_start(SSH2_MSG_KEX_DH_GEX_REPLY);
                    174:        packet_put_string(server_host_key_blob, sbloblen);
                    175:        packet_put_bignum2(dh->pub_key);        /* f */
                    176:        packet_put_string(signature, slen);
                    177:        packet_send();
                    178:
                    179:        xfree(signature);
                    180:        xfree(server_host_key_blob);
                    181:        /* have keys, free DH */
                    182:        DH_free(dh);
                    183:
1.2       djm       184:        kex_derive_keys(kex, hash, hashlen, shared_secret);
1.1       markus    185:        BN_clear_free(shared_secret);
                    186:
                    187:        kex_finish(kex);
                    188: }