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

1.22    ! djm         1: /* $OpenBSD: kexgexs.c,v 1.21 2015/01/19 20:16:15 markus 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:
1.21      markus     35: #include "sshkey.h"
1.8       deraadt    36: #include "cipher.h"
1.21      markus     37: #include "digest.h"
1.1       markus     38: #include "kex.h"
                     39: #include "log.h"
                     40: #include "packet.h"
                     41: #include "dh.h"
                     42: #include "ssh2.h"
                     43: #include "compat.h"
1.8       deraadt    44: #ifdef GSSAPI
                     45: #include "ssh-gss.h"
                     46: #endif
1.1       markus     47: #include "monitor_wrap.h"
1.21      markus     48: #include "dispatch.h"
                     49: #include "ssherr.h"
                     50: #include "sshbuf.h"
1.1       markus     51:
1.21      markus     52: static int input_kex_dh_gex_request(int, u_int32_t, void *);
                     53: static int input_kex_dh_gex_init(int, u_int32_t, void *);
                     54:
                     55: int
                     56: kexgex_server(struct ssh *ssh)
1.1       markus     57: {
1.21      markus     58:        ssh_dispatch_set(ssh, SSH2_MSG_KEX_DH_GEX_REQUEST_OLD,
                     59:            &input_kex_dh_gex_request);
                     60:        ssh_dispatch_set(ssh, SSH2_MSG_KEX_DH_GEX_REQUEST,
                     61:            &input_kex_dh_gex_request);
                     62:        debug("expecting SSH2_MSG_KEX_DH_GEX_REQUEST");
                     63:        return 0;
                     64: }
1.1       markus     65:
1.21      markus     66: static int
                     67: input_kex_dh_gex_request(int type, u_int32_t seq, void *ctxt)
                     68: {
                     69:        struct ssh *ssh = ctxt;
                     70:        struct kex *kex = ssh->kex;
                     71:        int r;
                     72:        u_int min = 0, max = 0, nbits = 0;
1.1       markus     73:
                     74:        switch (type) {
                     75:        case SSH2_MSG_KEX_DH_GEX_REQUEST:
                     76:                debug("SSH2_MSG_KEX_DH_GEX_REQUEST received");
1.21      markus     77:                if ((r = sshpkt_get_u32(ssh, &min)) != 0 ||
                     78:                    (r = sshpkt_get_u32(ssh, &nbits)) != 0 ||
                     79:                    (r = sshpkt_get_u32(ssh, &max)) != 0 ||
                     80:                    (r = sshpkt_get_end(ssh)) != 0)
                     81:                        goto out;
                     82:                kex->nbits = nbits;
                     83:                kex->min = min;
                     84:                kex->max = max;
1.1       markus     85:                min = MAX(DH_GRP_MIN, min);
                     86:                max = MIN(DH_GRP_MAX, max);
1.11      djm        87:                nbits = MAX(DH_GRP_MIN, nbits);
                     88:                nbits = MIN(DH_GRP_MAX, nbits);
1.1       markus     89:                break;
                     90:        case SSH2_MSG_KEX_DH_GEX_REQUEST_OLD:
                     91:                debug("SSH2_MSG_KEX_DH_GEX_REQUEST_OLD received");
1.21      markus     92:                if ((r = sshpkt_get_u32(ssh, &nbits)) != 0 ||
                     93:                    (r = sshpkt_get_end(ssh)) != 0)
                     94:                        goto out;
                     95:                kex->nbits = nbits;
1.1       markus     96:                /* unused for old GEX */
1.21      markus     97:                kex->min = min = DH_GRP_MIN;
                     98:                kex->max = max = DH_GRP_MAX;
1.1       markus     99:                break;
                    100:        default:
1.21      markus    101:                r = SSH_ERR_INVALID_ARGUMENT;
                    102:                goto out;
1.1       markus    103:        }
                    104:
1.21      markus    105:        if (kex->max < kex->min || kex->nbits < kex->min ||
                    106:            kex->max < kex->nbits) {
                    107:                r = SSH_ERR_DH_GEX_OUT_OF_RANGE;
                    108:                goto out;
                    109:        }
1.1       markus    110:
                    111:        /* Contact privileged parent */
1.21      markus    112:        kex->dh = PRIVSEP(choose_dh(min, nbits, max));
                    113:        if (kex->dh == NULL) {
                    114:                sshpkt_disconnect(ssh, "no matching DH grp found");
                    115:                r = SSH_ERR_ALLOC_FAIL;
                    116:                goto out;
                    117:        }
1.1       markus    118:        debug("SSH2_MSG_KEX_DH_GEX_GROUP sent");
1.21      markus    119:        if ((r = sshpkt_start(ssh, SSH2_MSG_KEX_DH_GEX_GROUP)) != 0 ||
                    120:            (r = sshpkt_put_bignum2(ssh, kex->dh->p)) != 0 ||
                    121:            (r = sshpkt_put_bignum2(ssh, kex->dh->g)) != 0 ||
                    122:            (r = sshpkt_send(ssh)) != 0)
                    123:                goto out;
1.1       markus    124:
1.21      markus    125:        /* Compute our exchange value in parallel with the client */
                    126:        if ((r = dh_gen_key(kex->dh, kex->we_need * 8)) != 0)
                    127:                goto out;
1.1       markus    128:
1.21      markus    129:        /* old KEX does not use min/max in kexgex_hash() */
                    130:        if (type == SSH2_MSG_KEX_DH_GEX_REQUEST_OLD)
                    131:                kex->min = kex->max = -1;
1.1       markus    132:
                    133:        debug("expecting SSH2_MSG_KEX_DH_GEX_INIT");
1.21      markus    134:        ssh_dispatch_set(ssh, SSH2_MSG_KEX_DH_GEX_INIT, &input_kex_dh_gex_init);
                    135:        r = 0;
                    136:  out:
                    137:        return r;
                    138: }
                    139:
                    140: static int
                    141: input_kex_dh_gex_init(int type, u_int32_t seq, void *ctxt)
                    142: {
                    143:        struct ssh *ssh = ctxt;
                    144:        struct kex *kex = ssh->kex;
                    145:        BIGNUM *shared_secret = NULL, *dh_client_pub = NULL;
                    146:        struct sshkey *server_host_public, *server_host_private;
                    147:        u_char *kbuf = NULL, *signature = NULL, *server_host_key_blob = NULL;
                    148:        u_char hash[SSH_DIGEST_MAX_LENGTH];
                    149:        size_t sbloblen, slen;
                    150:        size_t klen = 0, hashlen;
                    151:        int kout, r;
                    152:
                    153:        if (kex->load_host_public_key == NULL ||
                    154:            kex->load_host_private_key == NULL) {
                    155:                r = SSH_ERR_INVALID_ARGUMENT;
                    156:                goto out;
                    157:        }
1.22    ! djm       158:        server_host_public = kex->load_host_public_key(kex->hostkey_type, ssh);
        !           159:        server_host_private = kex->load_host_private_key(kex->hostkey_type, ssh);
        !           160:        if (server_host_public == NULL) {
1.21      markus    161:                r = SSH_ERR_NO_HOSTKEY_LOADED;
                    162:                goto out;
                    163:        }
1.1       markus    164:
                    165:        /* key, cert */
1.21      markus    166:        if ((dh_client_pub = BN_new()) == NULL) {
                    167:                r = SSH_ERR_ALLOC_FAIL;
                    168:                goto out;
                    169:        }
                    170:        if ((r = sshpkt_get_bignum2(ssh, dh_client_pub)) != 0 ||
                    171:            (r = sshpkt_get_end(ssh)) != 0)
                    172:                goto out;
1.1       markus    173:
                    174: #ifdef DEBUG_KEXDH
                    175:        fprintf(stderr, "dh_client_pub= ");
                    176:        BN_print_fp(stderr, dh_client_pub);
                    177:        fprintf(stderr, "\n");
                    178:        debug("bits %d", BN_num_bits(dh_client_pub));
                    179: #endif
                    180:
                    181: #ifdef DEBUG_KEXDH
1.21      markus    182:        DHparams_print_fp(stderr, kex->dh);
1.1       markus    183:        fprintf(stderr, "pub= ");
1.21      markus    184:        BN_print_fp(stderr, kex->dh->pub_key);
1.1       markus    185:        fprintf(stderr, "\n");
                    186: #endif
1.21      markus    187:        if (!dh_pub_is_valid(kex->dh, dh_client_pub)) {
                    188:                sshpkt_disconnect(ssh, "bad client public DH value");
                    189:                r = SSH_ERR_MESSAGE_INCOMPLETE;
                    190:                goto out;
                    191:        }
1.1       markus    192:
1.21      markus    193:        klen = DH_size(kex->dh);
                    194:        if ((kbuf = malloc(klen)) == NULL ||
                    195:            (shared_secret = BN_new()) == NULL) {
                    196:                r = SSH_ERR_ALLOC_FAIL;
                    197:                goto out;
                    198:        }
                    199:        if ((kout = DH_compute_key(kbuf, dh_client_pub, kex->dh)) < 0 ||
                    200:            BN_bin2bn(kbuf, kout, shared_secret) == NULL) {
                    201:                r = SSH_ERR_LIBCRYPTO_ERROR;
                    202:                goto out;
                    203:        }
1.1       markus    204: #ifdef DEBUG_KEXDH
                    205:        dump_digest("shared secret", kbuf, kout);
                    206: #endif
1.21      markus    207:        if ((r = sshkey_to_blob(server_host_public, &server_host_key_blob,
                    208:            &sbloblen)) != 0)
                    209:                goto out;
1.2       djm       210:        /* calc H */
1.21      markus    211:        hashlen = sizeof(hash);
                    212:        if ((r = kexgex_hash(
1.17      djm       213:            kex->hash_alg,
1.1       markus    214:            kex->client_version_string,
                    215:            kex->server_version_string,
1.21      markus    216:            sshbuf_ptr(kex->peer), sshbuf_len(kex->peer),
                    217:            sshbuf_ptr(kex->my), sshbuf_len(kex->my),
1.1       markus    218:            server_host_key_blob, sbloblen,
1.21      markus    219:            kex->min, kex->nbits, kex->max,
                    220:            kex->dh->p, kex->dh->g,
1.1       markus    221:            dh_client_pub,
1.21      markus    222:            kex->dh->pub_key,
1.2       djm       223:            shared_secret,
1.21      markus    224:            hash, &hashlen)) != 0)
                    225:                goto out;
1.1       markus    226:
                    227:        /* save session id := H */
                    228:        if (kex->session_id == NULL) {
1.2       djm       229:                kex->session_id_len = hashlen;
1.21      markus    230:                kex->session_id = malloc(kex->session_id_len);
                    231:                if (kex->session_id == NULL) {
                    232:                        r = SSH_ERR_ALLOC_FAIL;
                    233:                        goto out;
                    234:                }
1.1       markus    235:                memcpy(kex->session_id, hash, kex->session_id_len);
                    236:        }
                    237:
                    238:        /* sign H */
1.21      markus    239:        if ((r = kex->sign(server_host_private, server_host_public,
                    240:            &signature, &slen, hash, hashlen, ssh->compat)) < 0)
                    241:                goto out;
1.1       markus    242:
                    243:        /* destroy_sensitive_data(); */
                    244:
                    245:        /* send server hostkey, DH pubkey 'f' and singed H */
1.21      markus    246:        if ((r = sshpkt_start(ssh, SSH2_MSG_KEX_DH_GEX_REPLY)) != 0 ||
                    247:            (r = sshpkt_put_string(ssh, server_host_key_blob, sbloblen)) != 0 ||
                    248:            (r = sshpkt_put_bignum2(ssh, kex->dh->pub_key)) != 0 ||     /* f */
                    249:            (r = sshpkt_put_string(ssh, signature, slen)) != 0 ||
                    250:            (r = sshpkt_send(ssh)) != 0)
                    251:                goto out;
                    252:
                    253:        if ((r = kex_derive_keys_bn(ssh, hash, hashlen, shared_secret)) == 0)
                    254:                r = kex_send_newkeys(ssh);
                    255:  out:
                    256:        DH_free(kex->dh);
                    257:        kex->dh = NULL;
                    258:        if (dh_client_pub)
                    259:                BN_clear_free(dh_client_pub);
                    260:        if (kbuf) {
                    261:                explicit_bzero(kbuf, klen);
                    262:                free(kbuf);
                    263:        }
                    264:        if (shared_secret)
                    265:                BN_clear_free(shared_secret);
                    266:        free(server_host_key_blob);
1.15      djm       267:        free(signature);
1.21      markus    268:        return r;
1.1       markus    269: }