[BACK]Return to kexgexc.c CVS log [TXT][DIR] Up to [local] / src / usr.bin / ssh

Annotation of src/usr.bin/ssh/kexgexc.c, Revision 1.27

1.27    ! jsing       1: /* $OpenBSD: kexgexc.c,v 1.26 2017/12/18 02:25:15 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.9       deraadt    27: #include <sys/types.h>
1.12      djm        28:
                     29: #include <openssl/dh.h>
1.7       stevesk    30:
1.8       stevesk    31: #include <stdio.h>
1.7       stevesk    32: #include <string.h>
1.9       deraadt    33: #include <signal.h>
1.1       markus     34:
1.19      markus     35: #include "sshkey.h"
1.9       deraadt    36: #include "cipher.h"
1.19      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.19      markus     44: #include "dispatch.h"
                     45: #include "ssherr.h"
                     46: #include "sshbuf.h"
1.23      deraadt    47: #include "misc.h"
1.1       markus     48:
1.25      markus     49: static int input_kex_dh_gex_group(int, u_int32_t, struct ssh *);
                     50: static int input_kex_dh_gex_reply(int, u_int32_t, struct ssh *);
1.19      markus     51:
                     52: int
                     53: kexgex_client(struct ssh *ssh)
1.1       markus     54: {
1.19      markus     55:        struct kex *kex = ssh->kex;
                     56:        int r;
                     57:        u_int nbits;
1.1       markus     58:
1.16      dtucker    59:        nbits = dh_estimate(kex->dh_need * 8);
1.1       markus     60:
1.19      markus     61:        kex->min = DH_GRP_MIN;
                     62:        kex->max = DH_GRP_MAX;
                     63:        kex->nbits = nbits;
1.22      dtucker    64:        if (datafellows & SSH_BUG_DHGEX_LARGE)
1.23      deraadt    65:                kex->nbits = MINIMUM(kex->nbits, 4096);
1.21      djm        66:        /* New GEX request */
                     67:        if ((r = sshpkt_start(ssh, SSH2_MSG_KEX_DH_GEX_REQUEST)) != 0 ||
                     68:            (r = sshpkt_put_u32(ssh, kex->min)) != 0 ||
                     69:            (r = sshpkt_put_u32(ssh, kex->nbits)) != 0 ||
                     70:            (r = sshpkt_put_u32(ssh, kex->max)) != 0 ||
                     71:            (r = sshpkt_send(ssh)) != 0)
                     72:                goto out;
                     73:        debug("SSH2_MSG_KEX_DH_GEX_REQUEST(%u<%u<%u) sent",
                     74:            kex->min, kex->nbits, kex->max);
1.1       markus     75: #ifdef DEBUG_KEXDH
                     76:        fprintf(stderr, "\nmin = %d, nbits = %d, max = %d\n",
1.19      markus     77:            kex->min, kex->nbits, kex->max);
1.1       markus     78: #endif
1.19      markus     79:        ssh_dispatch_set(ssh, SSH2_MSG_KEX_DH_GEX_GROUP,
                     80:            &input_kex_dh_gex_group);
                     81:        r = 0;
                     82:  out:
                     83:        return r;
                     84: }
1.1       markus     85:
1.19      markus     86: static int
1.25      markus     87: input_kex_dh_gex_group(int type, u_int32_t seq, struct ssh *ssh)
1.19      markus     88: {
                     89:        struct kex *kex = ssh->kex;
                     90:        BIGNUM *p = NULL, *g = NULL;
                     91:        int r, bits;
1.1       markus     92:
1.19      markus     93:        debug("got SSH2_MSG_KEX_DH_GEX_GROUP");
1.1       markus     94:
1.19      markus     95:        if ((p = BN_new()) == NULL ||
                     96:            (g = BN_new()) == NULL) {
                     97:                r = SSH_ERR_ALLOC_FAIL;
                     98:                goto out;
                     99:        }
                    100:        if ((r = sshpkt_get_bignum2(ssh, p)) != 0 ||
                    101:            (r = sshpkt_get_bignum2(ssh, g)) != 0 ||
                    102:            (r = sshpkt_get_end(ssh)) != 0)
                    103:                goto out;
                    104:        if ((bits = BN_num_bits(p)) < 0 ||
                    105:            (u_int)bits < kex->min || (u_int)bits > kex->max) {
                    106:                r = SSH_ERR_DH_GEX_OUT_OF_RANGE;
                    107:                goto out;
                    108:        }
                    109:        if ((kex->dh = dh_new_group(g, p)) == NULL) {
                    110:                r = SSH_ERR_ALLOC_FAIL;
                    111:                goto out;
                    112:        }
                    113:        p = g = NULL; /* belong to kex->dh now */
1.1       markus    114:
1.19      markus    115:        /* generate and send 'e', client DH public key */
                    116:        if ((r = dh_gen_key(kex->dh, kex->we_need * 8)) != 0 ||
                    117:            (r = sshpkt_start(ssh, SSH2_MSG_KEX_DH_GEX_INIT)) != 0 ||
                    118:            (r = sshpkt_put_bignum2(ssh, kex->dh->pub_key)) != 0 ||
                    119:            (r = sshpkt_send(ssh)) != 0)
                    120:                goto out;
                    121:        debug("SSH2_MSG_KEX_DH_GEX_INIT sent");
1.1       markus    122: #ifdef DEBUG_KEXDH
1.19      markus    123:        DHparams_print_fp(stderr, kex->dh);
1.1       markus    124:        fprintf(stderr, "pub= ");
1.19      markus    125:        BN_print_fp(stderr, kex->dh->pub_key);
1.1       markus    126:        fprintf(stderr, "\n");
                    127: #endif
1.19      markus    128:        ssh_dispatch_set(ssh, SSH2_MSG_KEX_DH_GEX_GROUP, NULL);
                    129:        ssh_dispatch_set(ssh, SSH2_MSG_KEX_DH_GEX_REPLY, &input_kex_dh_gex_reply);
                    130:        r = 0;
                    131: out:
1.27    ! jsing     132:        BN_clear_free(p);
        !           133:        BN_clear_free(g);
1.19      markus    134:        return r;
                    135: }
1.1       markus    136:
1.19      markus    137: static int
1.25      markus    138: input_kex_dh_gex_reply(int type, u_int32_t seq, struct ssh *ssh)
1.19      markus    139: {
                    140:        struct kex *kex = ssh->kex;
                    141:        BIGNUM *dh_server_pub = NULL, *shared_secret = NULL;
                    142:        struct sshkey *server_host_key = NULL;
                    143:        u_char *kbuf = NULL, *signature = NULL, *server_host_key_blob = NULL;
                    144:        u_char hash[SSH_DIGEST_MAX_LENGTH];
                    145:        size_t klen = 0, slen, sbloblen, hashlen;
                    146:        int kout, r;
                    147:
                    148:        debug("got SSH2_MSG_KEX_DH_GEX_REPLY");
                    149:        if (kex->verify_host_key == NULL) {
                    150:                r = SSH_ERR_INVALID_ARGUMENT;
                    151:                goto out;
                    152:        }
1.1       markus    153:        /* key, cert */
1.19      markus    154:        if ((r = sshpkt_get_string(ssh, &server_host_key_blob,
                    155:            &sbloblen)) != 0 ||
                    156:            (r = sshkey_from_blob(server_host_key_blob, sbloblen,
                    157:            &server_host_key)) != 0)
                    158:                goto out;
1.20      djm       159:        if (server_host_key->type != kex->hostkey_type ||
                    160:            (kex->hostkey_type == KEY_ECDSA &&
                    161:            server_host_key->ecdsa_nid != kex->hostkey_nid)) {
1.19      markus    162:                r = SSH_ERR_KEY_TYPE_MISMATCH;
                    163:                goto out;
                    164:        }
                    165:        if (kex->verify_host_key(server_host_key, ssh) == -1) {
                    166:                r = SSH_ERR_SIGNATURE_INVALID;
                    167:                goto out;
                    168:        }
1.6       miod      169:        /* DH parameter f, server public DH key */
1.19      markus    170:        if ((dh_server_pub = BN_new()) == NULL) {
                    171:                r = SSH_ERR_ALLOC_FAIL;
                    172:                goto out;
                    173:        }
                    174:        /* signed H */
                    175:        if ((r = sshpkt_get_bignum2(ssh, dh_server_pub)) != 0 ||
                    176:            (r = sshpkt_get_string(ssh, &signature, &slen)) != 0 ||
                    177:            (r = sshpkt_get_end(ssh)) != 0)
                    178:                goto out;
1.1       markus    179: #ifdef DEBUG_KEXDH
                    180:        fprintf(stderr, "dh_server_pub= ");
                    181:        BN_print_fp(stderr, dh_server_pub);
                    182:        fprintf(stderr, "\n");
                    183:        debug("bits %d", BN_num_bits(dh_server_pub));
                    184: #endif
1.19      markus    185:        if (!dh_pub_is_valid(kex->dh, dh_server_pub)) {
                    186:                sshpkt_disconnect(ssh, "bad server public DH value");
                    187:                r = SSH_ERR_MESSAGE_INCOMPLETE;
                    188:                goto out;
                    189:        }
1.1       markus    190:
1.19      markus    191:        klen = DH_size(kex->dh);
                    192:        if ((kbuf = malloc(klen)) == NULL ||
                    193:            (shared_secret = BN_new()) == NULL) {
                    194:                r = SSH_ERR_ALLOC_FAIL;
                    195:                goto out;
                    196:        }
                    197:        if ((kout = DH_compute_key(kbuf, dh_server_pub, kex->dh)) < 0 ||
                    198:            BN_bin2bn(kbuf, kout, shared_secret) == NULL) {
                    199:                r = SSH_ERR_LIBCRYPTO_ERROR;
                    200:                goto out;
                    201:        }
1.1       markus    202: #ifdef DEBUG_KEXDH
                    203:        dump_digest("shared secret", kbuf, kout);
                    204: #endif
1.19      markus    205:        if (ssh->compat & SSH_OLD_DHGEX)
                    206:                kex->min = kex->max = -1;
1.1       markus    207:
                    208:        /* calc and verify H */
1.19      markus    209:        hashlen = sizeof(hash);
                    210:        if ((r = kexgex_hash(
1.14      djm       211:            kex->hash_alg,
1.1       markus    212:            kex->client_version_string,
                    213:            kex->server_version_string,
1.19      markus    214:            sshbuf_ptr(kex->my), sshbuf_len(kex->my),
                    215:            sshbuf_ptr(kex->peer), sshbuf_len(kex->peer),
1.1       markus    216:            server_host_key_blob, sbloblen,
1.19      markus    217:            kex->min, kex->nbits, kex->max,
                    218:            kex->dh->p, kex->dh->g,
                    219:            kex->dh->pub_key,
1.1       markus    220:            dh_server_pub,
1.3       djm       221:            shared_secret,
1.19      markus    222:            hash, &hashlen)) != 0)
                    223:                goto out;
1.3       djm       224:
1.19      markus    225:        if ((r = sshkey_verify(server_host_key, signature, slen, hash,
1.26      djm       226:            hashlen, kex->hostkey_alg, ssh->compat)) != 0)
1.19      markus    227:                goto out;
1.1       markus    228:
                    229:        /* save session id */
                    230:        if (kex->session_id == NULL) {
1.3       djm       231:                kex->session_id_len = hashlen;
1.19      markus    232:                kex->session_id = malloc(kex->session_id_len);
                    233:                if (kex->session_id == NULL) {
                    234:                        r = SSH_ERR_ALLOC_FAIL;
                    235:                        goto out;
                    236:                }
1.1       markus    237:                memcpy(kex->session_id, hash, kex->session_id_len);
                    238:        }
                    239:
1.19      markus    240:        if ((r = kex_derive_keys_bn(ssh, hash, hashlen, shared_secret)) == 0)
                    241:                r = kex_send_newkeys(ssh);
                    242:  out:
                    243:        explicit_bzero(hash, sizeof(hash));
                    244:        DH_free(kex->dh);
                    245:        kex->dh = NULL;
1.27    ! jsing     246:        BN_clear_free(dh_server_pub);
1.19      markus    247:        if (kbuf) {
                    248:                explicit_bzero(kbuf, klen);
                    249:                free(kbuf);
                    250:        }
1.27    ! jsing     251:        BN_clear_free(shared_secret);
1.19      markus    252:        sshkey_free(server_host_key);
                    253:        free(server_host_key_blob);
                    254:        free(signature);
                    255:        return r;
1.1       markus    256: }