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

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