[BACK]Return to kexgexs.c CVS log [TXT][DIR] Up to [cvs.NetBSD.org] / src / crypto / external / bsd / openssh / dist

Annotation of src/crypto/external/bsd/openssh/dist/kexgexs.c, Revision 1.1

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

CVSweb <webmaster@jp.NetBSD.org>