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

1.8     ! deraadt     1: /* $OpenBSD: kexgexs.c,v 1.7 2006/08/01 23:22:47 stevesk 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:
1.6       stevesk    27: #include <sys/param.h>
1.5       stevesk    28:
1.7       stevesk    29: #include <stdio.h>
1.5       stevesk    30: #include <string.h>
1.8     ! deraadt    31: #include <signal.h>
1.1       markus     32:
                     33: #include "xmalloc.h"
1.8     ! deraadt    34: #include "buffer.h"
1.1       markus     35: #include "key.h"
1.8     ! deraadt    36: #include "cipher.h"
1.1       markus     37: #include "kex.h"
                     38: #include "log.h"
                     39: #include "packet.h"
                     40: #include "dh.h"
                     41: #include "ssh2.h"
                     42: #include "compat.h"
1.8     ! deraadt    43: #ifdef GSSAPI
        !            44: #include "ssh-gss.h"
        !            45: #endif
1.1       markus     46: #include "monitor_wrap.h"
                     47:
                     48: void
                     49: kexgex_server(Kex *kex)
                     50: {
                     51:        BIGNUM *shared_secret = NULL, *dh_client_pub = NULL;
                     52:        Key *server_host_key;
                     53:        DH *dh;
                     54:        u_char *kbuf, *hash, *signature = NULL, *server_host_key_blob = NULL;
1.2       djm        55:        u_int sbloblen, klen, kout, slen, hashlen;
1.1       markus     56:        int min = -1, max = -1, nbits = -1, type;
                     57:
                     58:        if (kex->load_host_key == NULL)
                     59:                fatal("Cannot load hostkey");
                     60:        server_host_key = kex->load_host_key(kex->hostkey_type);
                     61:        if (server_host_key == NULL)
                     62:                fatal("Unsupported hostkey type %d", kex->hostkey_type);
                     63:
                     64:        type = packet_read();
                     65:        switch (type) {
                     66:        case SSH2_MSG_KEX_DH_GEX_REQUEST:
                     67:                debug("SSH2_MSG_KEX_DH_GEX_REQUEST received");
                     68:                min = packet_get_int();
                     69:                nbits = packet_get_int();
                     70:                max = packet_get_int();
                     71:                min = MAX(DH_GRP_MIN, min);
                     72:                max = MIN(DH_GRP_MAX, max);
                     73:                break;
                     74:        case SSH2_MSG_KEX_DH_GEX_REQUEST_OLD:
                     75:                debug("SSH2_MSG_KEX_DH_GEX_REQUEST_OLD received");
                     76:                nbits = packet_get_int();
                     77:                min = DH_GRP_MIN;
                     78:                max = DH_GRP_MAX;
                     79:                /* unused for old GEX */
                     80:                break;
                     81:        default:
                     82:                fatal("protocol error during kex, no DH_GEX_REQUEST: %d", type);
                     83:        }
                     84:        packet_check_eom();
                     85:
                     86:        if (max < min || nbits < min || max < nbits)
                     87:                fatal("DH_GEX_REQUEST, bad parameters: %d !< %d !< %d",
                     88:                    min, nbits, max);
                     89:
                     90:        /* Contact privileged parent */
                     91:        dh = PRIVSEP(choose_dh(min, nbits, max));
                     92:        if (dh == NULL)
                     93:                packet_disconnect("Protocol error: no matching DH grp found");
                     94:
                     95:        debug("SSH2_MSG_KEX_DH_GEX_GROUP sent");
                     96:        packet_start(SSH2_MSG_KEX_DH_GEX_GROUP);
                     97:        packet_put_bignum2(dh->p);
                     98:        packet_put_bignum2(dh->g);
                     99:        packet_send();
                    100:
                    101:        /* flush */
                    102:        packet_write_wait();
                    103:
                    104:        /* Compute our exchange value in parallel with the client */
                    105:        dh_gen_key(dh, kex->we_need * 8);
                    106:
                    107:        debug("expecting SSH2_MSG_KEX_DH_GEX_INIT");
                    108:        packet_read_expect(SSH2_MSG_KEX_DH_GEX_INIT);
                    109:
                    110:        /* key, cert */
                    111:        if ((dh_client_pub = BN_new()) == NULL)
                    112:                fatal("dh_client_pub == NULL");
                    113:        packet_get_bignum2(dh_client_pub);
                    114:        packet_check_eom();
                    115:
                    116: #ifdef DEBUG_KEXDH
                    117:        fprintf(stderr, "dh_client_pub= ");
                    118:        BN_print_fp(stderr, dh_client_pub);
                    119:        fprintf(stderr, "\n");
                    120:        debug("bits %d", BN_num_bits(dh_client_pub));
                    121: #endif
                    122:
                    123: #ifdef DEBUG_KEXDH
                    124:        DHparams_print_fp(stderr, dh);
                    125:        fprintf(stderr, "pub= ");
                    126:        BN_print_fp(stderr, dh->pub_key);
                    127:        fprintf(stderr, "\n");
                    128: #endif
                    129:        if (!dh_pub_is_valid(dh, dh_client_pub))
                    130:                packet_disconnect("bad client public DH value");
                    131:
                    132:        klen = DH_size(dh);
                    133:        kbuf = xmalloc(klen);
                    134:        kout = DH_compute_key(kbuf, dh_client_pub, dh);
                    135: #ifdef DEBUG_KEXDH
                    136:        dump_digest("shared secret", kbuf, kout);
                    137: #endif
                    138:        if ((shared_secret = BN_new()) == NULL)
                    139:                fatal("kexgex_server: BN_new failed");
                    140:        BN_bin2bn(kbuf, kout, shared_secret);
                    141:        memset(kbuf, 0, klen);
                    142:        xfree(kbuf);
                    143:
                    144:        key_to_blob(server_host_key, &server_host_key_blob, &sbloblen);
                    145:
                    146:        if (type == SSH2_MSG_KEX_DH_GEX_REQUEST_OLD)
                    147:                min = max = -1;
                    148:
1.2       djm       149:        /* calc H */
                    150:        kexgex_hash(
                    151:            kex->evp_md,
1.1       markus    152:            kex->client_version_string,
                    153:            kex->server_version_string,
                    154:            buffer_ptr(&kex->peer), buffer_len(&kex->peer),
                    155:            buffer_ptr(&kex->my), buffer_len(&kex->my),
                    156:            server_host_key_blob, sbloblen,
                    157:            min, nbits, max,
                    158:            dh->p, dh->g,
                    159:            dh_client_pub,
                    160:            dh->pub_key,
1.2       djm       161:            shared_secret,
                    162:            &hash, &hashlen
1.1       markus    163:        );
                    164:        BN_clear_free(dh_client_pub);
                    165:
                    166:        /* save session id := H */
                    167:        if (kex->session_id == NULL) {
1.2       djm       168:                kex->session_id_len = hashlen;
1.1       markus    169:                kex->session_id = xmalloc(kex->session_id_len);
                    170:                memcpy(kex->session_id, hash, kex->session_id_len);
                    171:        }
                    172:
                    173:        /* sign H */
1.2       djm       174:        PRIVSEP(key_sign(server_host_key, &signature, &slen, hash, hashlen));
1.1       markus    175:
                    176:        /* destroy_sensitive_data(); */
                    177:
                    178:        /* send server hostkey, DH pubkey 'f' and singed H */
                    179:        debug("SSH2_MSG_KEX_DH_GEX_REPLY sent");
                    180:        packet_start(SSH2_MSG_KEX_DH_GEX_REPLY);
                    181:        packet_put_string(server_host_key_blob, sbloblen);
                    182:        packet_put_bignum2(dh->pub_key);        /* f */
                    183:        packet_put_string(signature, slen);
                    184:        packet_send();
                    185:
                    186:        xfree(signature);
                    187:        xfree(server_host_key_blob);
                    188:        /* have keys, free DH */
                    189:        DH_free(dh);
                    190:
1.2       djm       191:        kex_derive_keys(kex, hash, hashlen, shared_secret);
1.1       markus    192:        BN_clear_free(shared_secret);
                    193:
                    194:        kex_finish(kex);
                    195: }