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

1.34    ! djm         1: /* $OpenBSD: kexgexs.c,v 1.33 2018/04/10 00:10:49 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");
                     73:        if ((r = sshpkt_get_u32(ssh, &min)) != 0 ||
                     74:            (r = sshpkt_get_u32(ssh, &nbits)) != 0 ||
                     75:            (r = sshpkt_get_u32(ssh, &max)) != 0 ||
                     76:            (r = sshpkt_get_end(ssh)) != 0)
1.21      markus     77:                goto out;
1.25      djm        78:        kex->nbits = nbits;
                     79:        kex->min = min;
                     80:        kex->max = max;
1.30      deraadt    81:        min = MAXIMUM(DH_GRP_MIN, min);
                     82:        max = MINIMUM(DH_GRP_MAX, max);
                     83:        nbits = MAXIMUM(DH_GRP_MIN, nbits);
                     84:        nbits = MINIMUM(DH_GRP_MAX, nbits);
1.29      dtucker    85:
1.21      markus     86:        if (kex->max < kex->min || kex->nbits < kex->min ||
1.29      dtucker    87:            kex->max < kex->nbits || kex->max < DH_GRP_MIN) {
1.21      markus     88:                r = SSH_ERR_DH_GEX_OUT_OF_RANGE;
                     89:                goto out;
                     90:        }
1.1       markus     91:
                     92:        /* Contact privileged parent */
1.29      dtucker    93:        kex->dh = PRIVSEP(choose_dh(min, nbits, max));
1.21      markus     94:        if (kex->dh == NULL) {
1.29      dtucker    95:                sshpkt_disconnect(ssh, "no matching DH grp found");
1.21      markus     96:                r = SSH_ERR_ALLOC_FAIL;
                     97:                goto out;
                     98:        }
1.1       markus     99:        debug("SSH2_MSG_KEX_DH_GEX_GROUP sent");
1.34    ! djm       100:        DH_get0_pqg(kex->dh, &dh_p, NULL, &dh_g);
1.21      markus    101:        if ((r = sshpkt_start(ssh, SSH2_MSG_KEX_DH_GEX_GROUP)) != 0 ||
1.34    ! djm       102:            (r = sshpkt_put_bignum2(ssh, dh_p)) != 0 ||
        !           103:            (r = sshpkt_put_bignum2(ssh, dh_g)) != 0 ||
1.21      markus    104:            (r = sshpkt_send(ssh)) != 0)
                    105:                goto out;
1.1       markus    106:
1.21      markus    107:        /* Compute our exchange value in parallel with the client */
                    108:        if ((r = dh_gen_key(kex->dh, kex->we_need * 8)) != 0)
                    109:                goto out;
1.1       markus    110:
                    111:        debug("expecting SSH2_MSG_KEX_DH_GEX_INIT");
1.21      markus    112:        ssh_dispatch_set(ssh, SSH2_MSG_KEX_DH_GEX_INIT, &input_kex_dh_gex_init);
                    113:        r = 0;
                    114:  out:
                    115:        return r;
                    116: }
                    117:
                    118: static int
1.31      markus    119: input_kex_dh_gex_init(int type, u_int32_t seq, struct ssh *ssh)
1.21      markus    120: {
                    121:        struct kex *kex = ssh->kex;
                    122:        BIGNUM *shared_secret = NULL, *dh_client_pub = NULL;
1.34    ! djm       123:        const BIGNUM *pub_key, *dh_p, *dh_g;
1.21      markus    124:        struct sshkey *server_host_public, *server_host_private;
                    125:        u_char *kbuf = NULL, *signature = NULL, *server_host_key_blob = NULL;
                    126:        u_char hash[SSH_DIGEST_MAX_LENGTH];
                    127:        size_t sbloblen, slen;
                    128:        size_t klen = 0, hashlen;
                    129:        int kout, r;
                    130:
                    131:        if (kex->load_host_public_key == NULL ||
                    132:            kex->load_host_private_key == NULL) {
                    133:                r = SSH_ERR_INVALID_ARGUMENT;
                    134:                goto out;
                    135:        }
1.24      djm       136:        server_host_public = kex->load_host_public_key(kex->hostkey_type,
                    137:            kex->hostkey_nid, ssh);
                    138:        server_host_private = kex->load_host_private_key(kex->hostkey_type,
                    139:            kex->hostkey_nid, ssh);
1.22      djm       140:        if (server_host_public == NULL) {
1.21      markus    141:                r = SSH_ERR_NO_HOSTKEY_LOADED;
                    142:                goto out;
                    143:        }
1.1       markus    144:
                    145:        /* key, cert */
1.21      markus    146:        if ((dh_client_pub = BN_new()) == NULL) {
                    147:                r = SSH_ERR_ALLOC_FAIL;
                    148:                goto out;
                    149:        }
                    150:        if ((r = sshpkt_get_bignum2(ssh, dh_client_pub)) != 0 ||
                    151:            (r = sshpkt_get_end(ssh)) != 0)
                    152:                goto out;
1.1       markus    153:
1.34    ! djm       154:        DH_get0_key(kex->dh, &pub_key, NULL);
        !           155:        DH_get0_pqg(kex->dh, &dh_p, NULL, &dh_g);
        !           156:
1.1       markus    157: #ifdef DEBUG_KEXDH
                    158:        fprintf(stderr, "dh_client_pub= ");
                    159:        BN_print_fp(stderr, dh_client_pub);
                    160:        fprintf(stderr, "\n");
                    161:        debug("bits %d", BN_num_bits(dh_client_pub));
1.21      markus    162:        DHparams_print_fp(stderr, kex->dh);
1.1       markus    163:        fprintf(stderr, "pub= ");
1.34    ! djm       164:        BN_print_fp(stderr, pub_key);
1.1       markus    165:        fprintf(stderr, "\n");
                    166: #endif
1.21      markus    167:        if (!dh_pub_is_valid(kex->dh, dh_client_pub)) {
                    168:                sshpkt_disconnect(ssh, "bad client public DH value");
                    169:                r = SSH_ERR_MESSAGE_INCOMPLETE;
                    170:                goto out;
                    171:        }
1.1       markus    172:
1.21      markus    173:        klen = DH_size(kex->dh);
                    174:        if ((kbuf = malloc(klen)) == NULL ||
                    175:            (shared_secret = BN_new()) == NULL) {
                    176:                r = SSH_ERR_ALLOC_FAIL;
                    177:                goto out;
                    178:        }
                    179:        if ((kout = DH_compute_key(kbuf, dh_client_pub, kex->dh)) < 0 ||
                    180:            BN_bin2bn(kbuf, kout, shared_secret) == NULL) {
                    181:                r = SSH_ERR_LIBCRYPTO_ERROR;
                    182:                goto out;
                    183:        }
1.1       markus    184: #ifdef DEBUG_KEXDH
                    185:        dump_digest("shared secret", kbuf, kout);
                    186: #endif
1.21      markus    187:        if ((r = sshkey_to_blob(server_host_public, &server_host_key_blob,
                    188:            &sbloblen)) != 0)
                    189:                goto out;
1.2       djm       190:        /* calc H */
1.21      markus    191:        hashlen = sizeof(hash);
                    192:        if ((r = kexgex_hash(
1.17      djm       193:            kex->hash_alg,
1.1       markus    194:            kex->client_version_string,
                    195:            kex->server_version_string,
1.21      markus    196:            sshbuf_ptr(kex->peer), sshbuf_len(kex->peer),
                    197:            sshbuf_ptr(kex->my), sshbuf_len(kex->my),
1.1       markus    198:            server_host_key_blob, sbloblen,
1.21      markus    199:            kex->min, kex->nbits, kex->max,
1.34    ! djm       200:            dh_p, dh_g,
1.1       markus    201:            dh_client_pub,
1.34    ! djm       202:            pub_key,
1.2       djm       203:            shared_secret,
1.21      markus    204:            hash, &hashlen)) != 0)
                    205:                goto out;
1.1       markus    206:
                    207:        /* save session id := H */
                    208:        if (kex->session_id == NULL) {
1.2       djm       209:                kex->session_id_len = hashlen;
1.21      markus    210:                kex->session_id = malloc(kex->session_id_len);
                    211:                if (kex->session_id == NULL) {
                    212:                        r = SSH_ERR_ALLOC_FAIL;
                    213:                        goto out;
                    214:                }
1.1       markus    215:                memcpy(kex->session_id, hash, kex->session_id_len);
                    216:        }
                    217:
                    218:        /* sign H */
1.26      markus    219:        if ((r = kex->sign(server_host_private, server_host_public, &signature,
                    220:             &slen, hash, hashlen, kex->hostkey_alg, ssh->compat)) < 0)
1.21      markus    221:                goto out;
1.1       markus    222:
                    223:        /* destroy_sensitive_data(); */
                    224:
1.33      djm       225:        /* send server hostkey, DH pubkey 'f' and signed H */
1.21      markus    226:        if ((r = sshpkt_start(ssh, SSH2_MSG_KEX_DH_GEX_REPLY)) != 0 ||
                    227:            (r = sshpkt_put_string(ssh, server_host_key_blob, sbloblen)) != 0 ||
1.34    ! djm       228:            (r = sshpkt_put_bignum2(ssh, pub_key)) != 0 ||     /* f */
1.21      markus    229:            (r = sshpkt_put_string(ssh, signature, slen)) != 0 ||
                    230:            (r = sshpkt_send(ssh)) != 0)
                    231:                goto out;
                    232:
                    233:        if ((r = kex_derive_keys_bn(ssh, hash, hashlen, shared_secret)) == 0)
                    234:                r = kex_send_newkeys(ssh);
                    235:  out:
                    236:        DH_free(kex->dh);
                    237:        kex->dh = NULL;
1.32      jsing     238:        BN_clear_free(dh_client_pub);
1.21      markus    239:        if (kbuf) {
                    240:                explicit_bzero(kbuf, klen);
                    241:                free(kbuf);
                    242:        }
1.32      jsing     243:        BN_clear_free(shared_secret);
1.21      markus    244:        free(server_host_key_blob);
1.15      djm       245:        free(signature);
1.21      markus    246:        return r;
1.1       markus    247: }