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

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