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

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

1.27    ! djm         1: /* $OpenBSD: kexdhc.c,v 1.26 2019/01/21 09:55:52 djm Exp $ */
1.1       markus      2: /*
                      3:  * Copyright (c) 2001 Markus Friedl.  All rights reserved.
                      4:  *
                      5:  * Redistribution and use in source and binary forms, with or without
                      6:  * modification, are permitted provided that the following conditions
                      7:  * are met:
                      8:  * 1. Redistributions of source code must retain the above copyright
                      9:  *    notice, this list of conditions and the following disclaimer.
                     10:  * 2. Redistributions in binary form must reproduce the above copyright
                     11:  *    notice, this list of conditions and the following disclaimer in the
                     12:  *    documentation and/or other materials provided with the distribution.
                     13:  *
                     14:  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
                     15:  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
                     16:  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
                     17:  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
                     18:  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
                     19:  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
                     20:  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
                     21:  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
                     22:  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
                     23:  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
                     24:  */
                     25:
1.9       deraadt    26: #include <sys/types.h>
1.12      djm        27:
                     28: #include <openssl/dh.h>
1.7       stevesk    29:
1.8       stevesk    30: #include <stdio.h>
1.7       stevesk    31: #include <string.h>
1.9       deraadt    32: #include <signal.h>
1.1       markus     33:
1.17      markus     34: #include "sshkey.h"
1.9       deraadt    35: #include "cipher.h"
1.17      markus     36: #include "digest.h"
1.27    ! djm        37: #include "dh.h"
1.1       markus     38: #include "kex.h"
                     39: #include "log.h"
                     40: #include "packet.h"
                     41: #include "ssh2.h"
1.17      markus     42: #include "dispatch.h"
                     43: #include "compat.h"
                     44: #include "ssherr.h"
                     45: #include "sshbuf.h"
1.1       markus     46:
1.20      markus     47: static int input_kex_dh(int, u_int32_t, struct ssh *);
1.17      markus     48:
                     49: int
                     50: kexdh_client(struct ssh *ssh)
1.1       markus     51: {
1.17      markus     52:        struct kex *kex = ssh->kex;
                     53:        int r;
1.23      djm        54:        const BIGNUM *pub_key;
1.1       markus     55:
                     56:        /* generate and send 'e', client DH public key */
1.27    ! djm        57:        if ((r = kex_dh_keygen(kex)) != 0)
1.17      markus     58:                goto out;
1.1       markus     59:        debug("sending SSH2_MSG_KEXDH_INIT");
1.23      djm        60:        DH_get0_key(kex->dh, &pub_key, NULL);
                     61:        if ((r = sshpkt_start(ssh, SSH2_MSG_KEXDH_INIT)) != 0 ||
                     62:            (r = sshpkt_put_bignum2(ssh, pub_key)) != 0 ||
1.17      markus     63:            (r = sshpkt_send(ssh)) != 0)
                     64:                goto out;
1.1       markus     65: #ifdef DEBUG_KEXDH
1.17      markus     66:        DHparams_print_fp(stderr, kex->dh);
1.1       markus     67:        fprintf(stderr, "pub= ");
1.23      djm        68:        BN_print_fp(stderr, pub_key);
1.1       markus     69:        fprintf(stderr, "\n");
                     70: #endif
                     71:        debug("expecting SSH2_MSG_KEXDH_REPLY");
1.17      markus     72:        ssh_dispatch_set(ssh, SSH2_MSG_KEXDH_REPLY, &input_kex_dh);
                     73:        r = 0;
                     74:  out:
                     75:        return r;
                     76: }
1.1       markus     77:
1.17      markus     78: static int
1.20      markus     79: input_kex_dh(int type, u_int32_t seq, struct ssh *ssh)
1.17      markus     80: {
                     81:        struct kex *kex = ssh->kex;
                     82:        BIGNUM *dh_server_pub = NULL, *shared_secret = NULL;
1.23      djm        83:        const BIGNUM *pub_key;
1.17      markus     84:        struct sshkey *server_host_key = NULL;
                     85:        u_char *kbuf = NULL, *server_host_key_blob = NULL, *signature = NULL;
                     86:        u_char hash[SSH_DIGEST_MAX_LENGTH];
                     87:        size_t klen = 0, slen, sbloblen, hashlen;
                     88:        int kout, r;
                     89:
                     90:        if (kex->verify_host_key == NULL) {
                     91:                r = SSH_ERR_INVALID_ARGUMENT;
                     92:                goto out;
                     93:        }
1.1       markus     94:        /* key, cert */
1.17      markus     95:        if ((r = sshpkt_get_string(ssh, &server_host_key_blob,
                     96:            &sbloblen)) != 0 ||
                     97:            (r = sshkey_from_blob(server_host_key_blob, sbloblen,
                     98:            &server_host_key)) != 0)
                     99:                goto out;
1.18      djm       100:        if (server_host_key->type != kex->hostkey_type ||
                    101:            (kex->hostkey_type == KEY_ECDSA &&
                    102:            server_host_key->ecdsa_nid != kex->hostkey_nid)) {
1.17      markus    103:                r = SSH_ERR_KEY_TYPE_MISMATCH;
                    104:                goto out;
                    105:        }
                    106:        if (kex->verify_host_key(server_host_key, ssh) == -1) {
                    107:                r = SSH_ERR_SIGNATURE_INVALID;
                    108:                goto out;
                    109:        }
1.25      djm       110:        /* DH parameter f, server public DH key, signed H */
                    111:        if ((r = sshpkt_get_bignum2(ssh, &dh_server_pub)) != 0 ||
1.17      markus    112:            (r = sshpkt_get_string(ssh, &signature, &slen)) != 0 ||
                    113:            (r = sshpkt_get_end(ssh)) != 0)
                    114:                goto out;
1.1       markus    115: #ifdef DEBUG_KEXDH
                    116:        fprintf(stderr, "dh_server_pub= ");
                    117:        BN_print_fp(stderr, dh_server_pub);
                    118:        fprintf(stderr, "\n");
                    119:        debug("bits %d", BN_num_bits(dh_server_pub));
                    120: #endif
1.17      markus    121:        if (!dh_pub_is_valid(kex->dh, dh_server_pub)) {
                    122:                sshpkt_disconnect(ssh, "bad server public DH value");
                    123:                r = SSH_ERR_MESSAGE_INCOMPLETE;
                    124:                goto out;
                    125:        }
1.1       markus    126:
1.17      markus    127:        klen = DH_size(kex->dh);
                    128:        if ((kbuf = malloc(klen)) == NULL ||
                    129:            (shared_secret = BN_new()) == NULL) {
                    130:                r = SSH_ERR_ALLOC_FAIL;
                    131:                goto out;
                    132:        }
                    133:        if ((kout = DH_compute_key(kbuf, dh_server_pub, kex->dh)) < 0 ||
                    134:            BN_bin2bn(kbuf, kout, shared_secret) == NULL) {
                    135:                r = SSH_ERR_LIBCRYPTO_ERROR;
                    136:                goto out;
                    137:        }
1.1       markus    138: #ifdef DEBUG_KEXDH
                    139:        dump_digest("shared secret", kbuf, kout);
                    140: #endif
                    141:
                    142:        /* calc and verify H */
1.23      djm       143:        DH_get0_key(kex->dh, &pub_key, NULL);
1.17      markus    144:        hashlen = sizeof(hash);
                    145:        if ((r = kex_dh_hash(
1.19      djm       146:            kex->hash_alg,
1.24      djm       147:            kex->client_version,
                    148:            kex->server_version,
1.17      markus    149:            sshbuf_ptr(kex->my), sshbuf_len(kex->my),
                    150:            sshbuf_ptr(kex->peer), sshbuf_len(kex->peer),
1.1       markus    151:            server_host_key_blob, sbloblen,
1.23      djm       152:            pub_key,
1.1       markus    153:            dh_server_pub,
1.3       djm       154:            shared_secret,
1.17      markus    155:            hash, &hashlen)) != 0)
                    156:                goto out;
1.1       markus    157:
1.17      markus    158:        if ((r = sshkey_verify(server_host_key, signature, slen, hash, hashlen,
1.21      djm       159:            kex->hostkey_alg, ssh->compat)) != 0)
1.17      markus    160:                goto out;
1.1       markus    161:
1.17      markus    162:        if ((r = kex_derive_keys_bn(ssh, hash, hashlen, shared_secret)) == 0)
                    163:                r = kex_send_newkeys(ssh);
                    164:  out:
                    165:        explicit_bzero(hash, sizeof(hash));
                    166:        DH_free(kex->dh);
                    167:        kex->dh = NULL;
1.22      jsing     168:        BN_clear_free(dh_server_pub);
1.17      markus    169:        if (kbuf) {
                    170:                explicit_bzero(kbuf, klen);
                    171:                free(kbuf);
                    172:        }
1.22      jsing     173:        BN_clear_free(shared_secret);
1.17      markus    174:        sshkey_free(server_host_key);
                    175:        free(server_host_key_blob);
                    176:        free(signature);
                    177:        return r;
1.1       markus    178: }