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

1.44    ! djm         1: /* $OpenBSD: kexgexs.c,v 1.43 2021/01/31 22:55:29 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.5       stevesk    27:
1.7       stevesk    28: #include <stdio.h>
1.5       stevesk    29: #include <string.h>
1.8       deraadt    30: #include <signal.h>
1.14      djm        31:
                     32: #include <openssl/dh.h>
1.1       markus     33:
1.21      markus     34: #include "sshkey.h"
1.8       deraadt    35: #include "cipher.h"
1.21      markus     36: #include "digest.h"
1.1       markus     37: #include "kex.h"
                     38: #include "log.h"
                     39: #include "packet.h"
                     40: #include "dh.h"
                     41: #include "ssh2.h"
                     42: #include "compat.h"
1.8       deraadt    43: #ifdef GSSAPI
                     44: #include "ssh-gss.h"
                     45: #endif
1.1       markus     46: #include "monitor_wrap.h"
1.21      markus     47: #include "dispatch.h"
                     48: #include "ssherr.h"
                     49: #include "sshbuf.h"
1.30      deraadt    50: #include "misc.h"
1.1       markus     51:
1.31      markus     52: static int input_kex_dh_gex_request(int, u_int32_t, struct ssh *);
                     53: static int input_kex_dh_gex_init(int, u_int32_t, struct ssh *);
1.21      markus     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,
                     59:            &input_kex_dh_gex_request);
                     60:        debug("expecting SSH2_MSG_KEX_DH_GEX_REQUEST");
                     61:        return 0;
                     62: }
1.1       markus     63:
1.21      markus     64: static int
1.31      markus     65: input_kex_dh_gex_request(int type, u_int32_t seq, struct ssh *ssh)
1.21      markus     66: {
                     67:        struct kex *kex = ssh->kex;
                     68:        int r;
                     69:        u_int min = 0, max = 0, nbits = 0;
1.34      djm        70:        const BIGNUM *dh_p, *dh_g;
1.1       markus     71:
1.25      djm        72:        debug("SSH2_MSG_KEX_DH_GEX_REQUEST received");
1.43      djm        73:        ssh_dispatch_set(ssh, SSH2_MSG_KEX_DH_GEX_REQUEST, &kex_protocol_error);
                     74:
1.25      djm        75:        if ((r = sshpkt_get_u32(ssh, &min)) != 0 ||
                     76:            (r = sshpkt_get_u32(ssh, &nbits)) != 0 ||
                     77:            (r = sshpkt_get_u32(ssh, &max)) != 0 ||
                     78:            (r = sshpkt_get_end(ssh)) != 0)
1.21      markus     79:                goto out;
1.25      djm        80:        kex->nbits = nbits;
                     81:        kex->min = min;
                     82:        kex->max = max;
1.30      deraadt    83:        min = MAXIMUM(DH_GRP_MIN, min);
                     84:        max = MINIMUM(DH_GRP_MAX, max);
                     85:        nbits = MAXIMUM(DH_GRP_MIN, nbits);
                     86:        nbits = MINIMUM(DH_GRP_MAX, nbits);
1.29      dtucker    87:
1.21      markus     88:        if (kex->max < kex->min || kex->nbits < kex->min ||
1.29      dtucker    89:            kex->max < kex->nbits || kex->max < DH_GRP_MIN) {
1.21      markus     90:                r = SSH_ERR_DH_GEX_OUT_OF_RANGE;
                     91:                goto out;
                     92:        }
1.1       markus     93:
                     94:        /* Contact privileged parent */
1.29      dtucker    95:        kex->dh = PRIVSEP(choose_dh(min, nbits, max));
1.21      markus     96:        if (kex->dh == NULL) {
1.29      dtucker    97:                sshpkt_disconnect(ssh, "no matching DH grp found");
1.21      markus     98:                r = SSH_ERR_ALLOC_FAIL;
                     99:                goto out;
                    100:        }
1.1       markus    101:        debug("SSH2_MSG_KEX_DH_GEX_GROUP sent");
1.34      djm       102:        DH_get0_pqg(kex->dh, &dh_p, NULL, &dh_g);
1.21      markus    103:        if ((r = sshpkt_start(ssh, SSH2_MSG_KEX_DH_GEX_GROUP)) != 0 ||
1.34      djm       104:            (r = sshpkt_put_bignum2(ssh, dh_p)) != 0 ||
                    105:            (r = sshpkt_put_bignum2(ssh, dh_g)) != 0 ||
1.21      markus    106:            (r = sshpkt_send(ssh)) != 0)
                    107:                goto out;
1.1       markus    108:
1.21      markus    109:        /* Compute our exchange value in parallel with the client */
                    110:        if ((r = dh_gen_key(kex->dh, kex->we_need * 8)) != 0)
                    111:                goto out;
1.1       markus    112:
                    113:        debug("expecting SSH2_MSG_KEX_DH_GEX_INIT");
1.21      markus    114:        ssh_dispatch_set(ssh, SSH2_MSG_KEX_DH_GEX_INIT, &input_kex_dh_gex_init);
                    115:        r = 0;
                    116:  out:
                    117:        return r;
                    118: }
                    119:
                    120: static int
1.31      markus    121: input_kex_dh_gex_init(int type, u_int32_t seq, struct ssh *ssh)
1.21      markus    122: {
                    123:        struct kex *kex = ssh->kex;
1.40      djm       124:        BIGNUM *dh_client_pub = NULL;
1.34      djm       125:        const BIGNUM *pub_key, *dh_p, *dh_g;
1.40      djm       126:        struct sshbuf *shared_secret = NULL;
1.42      djm       127:        struct sshbuf *server_host_key_blob = NULL;
1.21      markus    128:        struct sshkey *server_host_public, *server_host_private;
1.42      djm       129:        u_char *signature = NULL;
1.21      markus    130:        u_char hash[SSH_DIGEST_MAX_LENGTH];
1.42      djm       131:        size_t slen, hashlen;
1.40      djm       132:        int r;
1.43      djm       133:
                    134:        debug("SSH2_MSG_KEX_DH_GEX_INIT received");
                    135:        ssh_dispatch_set(ssh, SSH2_MSG_KEX_DH_GEX_INIT, &kex_protocol_error);
1.21      markus    136:
1.41      djm       137:        if ((r = kex_load_hostkey(ssh, &server_host_private,
                    138:            &server_host_public)) != 0)
1.21      markus    139:                goto out;
1.1       markus    140:
                    141:        /* key, cert */
1.38      djm       142:        if ((r = sshpkt_get_bignum2(ssh, &dh_client_pub)) != 0 ||
1.21      markus    143:            (r = sshpkt_get_end(ssh)) != 0)
                    144:                goto out;
1.40      djm       145:        if ((shared_secret = sshbuf_new()) == NULL) {
1.21      markus    146:                r = SSH_ERR_ALLOC_FAIL;
                    147:                goto out;
                    148:        }
1.40      djm       149:        if ((r = kex_dh_compute_key(kex, dh_client_pub, shared_secret)) != 0)
1.21      markus    150:                goto out;
1.42      djm       151:        if ((server_host_key_blob = sshbuf_new()) == NULL) {
                    152:                r = SSH_ERR_ALLOC_FAIL;
                    153:                goto out;
                    154:        }
                    155:        if ((r = sshkey_putb(server_host_public, server_host_key_blob)) != 0)
1.21      markus    156:                goto out;
1.40      djm       157:
1.2       djm       158:        /* calc H */
1.40      djm       159:        DH_get0_key(kex->dh, &pub_key, NULL);
                    160:        DH_get0_pqg(kex->dh, &dh_p, NULL, &dh_g);
1.21      markus    161:        hashlen = sizeof(hash);
                    162:        if ((r = kexgex_hash(
1.17      djm       163:            kex->hash_alg,
1.36      djm       164:            kex->client_version,
                    165:            kex->server_version,
1.42      djm       166:            kex->peer,
                    167:            kex->my,
                    168:            server_host_key_blob,
1.21      markus    169:            kex->min, kex->nbits, kex->max,
1.34      djm       170:            dh_p, dh_g,
1.1       markus    171:            dh_client_pub,
1.34      djm       172:            pub_key,
1.40      djm       173:            sshbuf_ptr(shared_secret), sshbuf_len(shared_secret),
1.21      markus    174:            hash, &hashlen)) != 0)
                    175:                goto out;
1.1       markus    176:
                    177:        /* sign H */
1.37      djm       178:        if ((r = kex->sign(ssh, server_host_private, server_host_public,
                    179:            &signature, &slen, hash, hashlen, kex->hostkey_alg)) < 0)
1.21      markus    180:                goto out;
1.1       markus    181:
1.33      djm       182:        /* send server hostkey, DH pubkey 'f' and signed H */
1.21      markus    183:        if ((r = sshpkt_start(ssh, SSH2_MSG_KEX_DH_GEX_REPLY)) != 0 ||
1.42      djm       184:            (r = sshpkt_put_stringb(ssh, server_host_key_blob)) != 0 ||
1.34      djm       185:            (r = sshpkt_put_bignum2(ssh, pub_key)) != 0 ||     /* f */
1.21      markus    186:            (r = sshpkt_put_string(ssh, signature, slen)) != 0 ||
                    187:            (r = sshpkt_send(ssh)) != 0)
                    188:                goto out;
                    189:
1.44    ! djm       190:        if ((r = kex_derive_keys(ssh, hash, hashlen, shared_secret)) != 0 ||
        !           191:            (r = kex_send_newkeys(ssh)) != 0)
        !           192:                goto out;
        !           193:
        !           194:        /* retain copy of hostkey used at initial KEX */
        !           195:        if (kex->initial_hostkey == NULL &&
        !           196:            (r = sshkey_from_private(server_host_public,
        !           197:            &kex->initial_hostkey)) != 0)
        !           198:                goto out;
        !           199:        /* success */
1.21      markus    200:  out:
1.35      djm       201:        explicit_bzero(hash, sizeof(hash));
1.21      markus    202:        DH_free(kex->dh);
                    203:        kex->dh = NULL;
1.32      jsing     204:        BN_clear_free(dh_client_pub);
1.40      djm       205:        sshbuf_free(shared_secret);
1.42      djm       206:        sshbuf_free(server_host_key_blob);
1.15      djm       207:        free(signature);
1.21      markus    208:        return r;
1.1       markus    209: }