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

1.13    ! djm         1: /* $OpenBSD: kexgexs.c,v 1.12 2009/06/21 07:37:15 dtucker 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;
1.13    ! djm        52:        Key *server_host_public, *server_host_private;
1.1       markus     53:        DH *dh;
                     54:        u_char *kbuf, *hash, *signature = NULL, *server_host_key_blob = NULL;
1.9       markus     55:        u_int sbloblen, klen, slen, hashlen;
1.11      djm        56:        int omin = -1, min = -1, omax = -1, max = -1, onbits = -1, nbits = -1;
                     57:        int type, kout;
1.1       markus     58:
1.13    ! djm        59:        if (kex->load_host_public_key == NULL ||
        !            60:            kex->load_host_private_key == NULL)
1.1       markus     61:                fatal("Cannot load hostkey");
1.13    ! djm        62:        server_host_public = kex->load_host_public_key(kex->hostkey_type);
        !            63:        if (server_host_public == NULL)
1.1       markus     64:                fatal("Unsupported hostkey type %d", kex->hostkey_type);
1.13    ! djm        65:        server_host_private = kex->load_host_private_key(kex->hostkey_type);
        !            66:        if (server_host_private == NULL)
        !            67:                fatal("Missing private key for hostkey type %d",
        !            68:                    kex->hostkey_type);
        !            69:
1.1       markus     70:
                     71:        type = packet_read();
                     72:        switch (type) {
                     73:        case SSH2_MSG_KEX_DH_GEX_REQUEST:
                     74:                debug("SSH2_MSG_KEX_DH_GEX_REQUEST received");
1.11      djm        75:                omin = min = packet_get_int();
                     76:                onbits = nbits = packet_get_int();
                     77:                omax = max = packet_get_int();
1.1       markus     78:                min = MAX(DH_GRP_MIN, min);
                     79:                max = MIN(DH_GRP_MAX, max);
1.11      djm        80:                nbits = MAX(DH_GRP_MIN, nbits);
                     81:                nbits = MIN(DH_GRP_MAX, nbits);
1.1       markus     82:                break;
                     83:        case SSH2_MSG_KEX_DH_GEX_REQUEST_OLD:
                     84:                debug("SSH2_MSG_KEX_DH_GEX_REQUEST_OLD received");
1.11      djm        85:                onbits = nbits = packet_get_int();
1.1       markus     86:                /* unused for old GEX */
1.11      djm        87:                omin = min = DH_GRP_MIN;
                     88:                omax = max = DH_GRP_MAX;
1.1       markus     89:                break;
                     90:        default:
                     91:                fatal("protocol error during kex, no DH_GEX_REQUEST: %d", type);
                     92:        }
                     93:        packet_check_eom();
                     94:
1.11      djm        95:        if (omax < omin || onbits < omin || omax < onbits)
1.1       markus     96:                fatal("DH_GEX_REQUEST, bad parameters: %d !< %d !< %d",
1.11      djm        97:                    omin, onbits, omax);
1.1       markus     98:
                     99:        /* Contact privileged parent */
                    100:        dh = PRIVSEP(choose_dh(min, nbits, max));
                    101:        if (dh == NULL)
                    102:                packet_disconnect("Protocol error: no matching DH grp found");
                    103:
                    104:        debug("SSH2_MSG_KEX_DH_GEX_GROUP sent");
                    105:        packet_start(SSH2_MSG_KEX_DH_GEX_GROUP);
                    106:        packet_put_bignum2(dh->p);
                    107:        packet_put_bignum2(dh->g);
                    108:        packet_send();
                    109:
                    110:        /* flush */
                    111:        packet_write_wait();
                    112:
                    113:        /* Compute our exchange value in parallel with the client */
                    114:        dh_gen_key(dh, kex->we_need * 8);
                    115:
                    116:        debug("expecting SSH2_MSG_KEX_DH_GEX_INIT");
                    117:        packet_read_expect(SSH2_MSG_KEX_DH_GEX_INIT);
                    118:
                    119:        /* key, cert */
                    120:        if ((dh_client_pub = BN_new()) == NULL)
                    121:                fatal("dh_client_pub == NULL");
                    122:        packet_get_bignum2(dh_client_pub);
                    123:        packet_check_eom();
                    124:
                    125: #ifdef DEBUG_KEXDH
                    126:        fprintf(stderr, "dh_client_pub= ");
                    127:        BN_print_fp(stderr, dh_client_pub);
                    128:        fprintf(stderr, "\n");
                    129:        debug("bits %d", BN_num_bits(dh_client_pub));
                    130: #endif
                    131:
                    132: #ifdef DEBUG_KEXDH
                    133:        DHparams_print_fp(stderr, dh);
                    134:        fprintf(stderr, "pub= ");
                    135:        BN_print_fp(stderr, dh->pub_key);
                    136:        fprintf(stderr, "\n");
                    137: #endif
                    138:        if (!dh_pub_is_valid(dh, dh_client_pub))
                    139:                packet_disconnect("bad client public DH value");
                    140:
                    141:        klen = DH_size(dh);
                    142:        kbuf = xmalloc(klen);
1.9       markus    143:        if ((kout = DH_compute_key(kbuf, dh_client_pub, dh)) < 0)
                    144:                fatal("DH_compute_key: failed");
1.1       markus    145: #ifdef DEBUG_KEXDH
                    146:        dump_digest("shared secret", kbuf, kout);
                    147: #endif
                    148:        if ((shared_secret = BN_new()) == NULL)
                    149:                fatal("kexgex_server: BN_new failed");
1.10      markus    150:        if (BN_bin2bn(kbuf, kout, shared_secret) == NULL)
                    151:                fatal("kexgex_server: BN_bin2bn failed");
1.1       markus    152:        memset(kbuf, 0, klen);
                    153:        xfree(kbuf);
                    154:
1.13    ! djm       155:        key_to_blob(server_host_public, &server_host_key_blob, &sbloblen);
1.1       markus    156:
                    157:        if (type == SSH2_MSG_KEX_DH_GEX_REQUEST_OLD)
1.11      djm       158:                omin = min = omax = max = -1;
1.1       markus    159:
1.2       djm       160:        /* calc H */
                    161:        kexgex_hash(
                    162:            kex->evp_md,
1.1       markus    163:            kex->client_version_string,
                    164:            kex->server_version_string,
                    165:            buffer_ptr(&kex->peer), buffer_len(&kex->peer),
                    166:            buffer_ptr(&kex->my), buffer_len(&kex->my),
                    167:            server_host_key_blob, sbloblen,
1.11      djm       168:            omin, onbits, omax,
1.1       markus    169:            dh->p, dh->g,
                    170:            dh_client_pub,
                    171:            dh->pub_key,
1.2       djm       172:            shared_secret,
                    173:            &hash, &hashlen
1.1       markus    174:        );
                    175:        BN_clear_free(dh_client_pub);
                    176:
                    177:        /* save session id := H */
                    178:        if (kex->session_id == NULL) {
1.2       djm       179:                kex->session_id_len = hashlen;
1.1       markus    180:                kex->session_id = xmalloc(kex->session_id_len);
                    181:                memcpy(kex->session_id, hash, kex->session_id_len);
                    182:        }
                    183:
                    184:        /* sign H */
1.13    ! djm       185:        if (PRIVSEP(key_sign(server_host_private, &signature, &slen, hash,
1.12      dtucker   186:            hashlen)) < 0)
                    187:                fatal("kexgex_server: key_sign failed");
1.1       markus    188:
                    189:        /* destroy_sensitive_data(); */
                    190:
                    191:        /* send server hostkey, DH pubkey 'f' and singed H */
                    192:        debug("SSH2_MSG_KEX_DH_GEX_REPLY sent");
                    193:        packet_start(SSH2_MSG_KEX_DH_GEX_REPLY);
                    194:        packet_put_string(server_host_key_blob, sbloblen);
                    195:        packet_put_bignum2(dh->pub_key);        /* f */
                    196:        packet_put_string(signature, slen);
                    197:        packet_send();
                    198:
                    199:        xfree(signature);
                    200:        xfree(server_host_key_blob);
                    201:        /* have keys, free DH */
                    202:        DH_free(dh);
                    203:
1.2       djm       204:        kex_derive_keys(kex, hash, hashlen, shared_secret);
1.1       markus    205:        BN_clear_free(shared_secret);
                    206:
                    207:        kex_finish(kex);
                    208: }