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

1.23    ! djm         1: /* $OpenBSD: kexdhc.c,v 1.22 2018/02/07 02:06:51 jsing 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.1       markus     37: #include "kex.h"
                     38: #include "log.h"
                     39: #include "packet.h"
                     40: #include "dh.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.2       djm        57:        switch (kex->kex_type) {
                     58:        case KEX_DH_GRP1_SHA1:
1.17      markus     59:                kex->dh = dh_new_group1();
1.2       djm        60:                break;
                     61:        case KEX_DH_GRP14_SHA1:
1.19      djm        62:        case KEX_DH_GRP14_SHA256:
1.17      markus     63:                kex->dh = dh_new_group14();
1.2       djm        64:                break;
1.19      djm        65:        case KEX_DH_GRP16_SHA512:
                     66:                kex->dh = dh_new_group16();
                     67:                break;
                     68:        case KEX_DH_GRP18_SHA512:
                     69:                kex->dh = dh_new_group18();
                     70:                break;
1.2       djm        71:        default:
1.17      markus     72:                r = SSH_ERR_INVALID_ARGUMENT;
                     73:                goto out;
                     74:        }
                     75:        if (kex->dh == NULL) {
                     76:                r = SSH_ERR_ALLOC_FAIL;
                     77:                goto out;
1.2       djm        78:        }
1.1       markus     79:        debug("sending SSH2_MSG_KEXDH_INIT");
1.23    ! djm        80:        if ((r = dh_gen_key(kex->dh, kex->we_need * 8)) != 0)
        !            81:                goto out;
        !            82:        DH_get0_key(kex->dh, &pub_key, NULL);
        !            83:        if ((r = sshpkt_start(ssh, SSH2_MSG_KEXDH_INIT)) != 0 ||
        !            84:            (r = sshpkt_put_bignum2(ssh, pub_key)) != 0 ||
1.17      markus     85:            (r = sshpkt_send(ssh)) != 0)
                     86:                goto out;
1.1       markus     87: #ifdef DEBUG_KEXDH
1.17      markus     88:        DHparams_print_fp(stderr, kex->dh);
1.1       markus     89:        fprintf(stderr, "pub= ");
1.23    ! djm        90:        BN_print_fp(stderr, pub_key);
1.1       markus     91:        fprintf(stderr, "\n");
                     92: #endif
                     93:        debug("expecting SSH2_MSG_KEXDH_REPLY");
1.17      markus     94:        ssh_dispatch_set(ssh, SSH2_MSG_KEXDH_REPLY, &input_kex_dh);
                     95:        r = 0;
                     96:  out:
                     97:        return r;
                     98: }
1.1       markus     99:
1.17      markus    100: static int
1.20      markus    101: input_kex_dh(int type, u_int32_t seq, struct ssh *ssh)
1.17      markus    102: {
                    103:        struct kex *kex = ssh->kex;
                    104:        BIGNUM *dh_server_pub = NULL, *shared_secret = NULL;
1.23    ! djm       105:        const BIGNUM *pub_key;
1.17      markus    106:        struct sshkey *server_host_key = NULL;
                    107:        u_char *kbuf = NULL, *server_host_key_blob = NULL, *signature = NULL;
                    108:        u_char hash[SSH_DIGEST_MAX_LENGTH];
                    109:        size_t klen = 0, slen, sbloblen, hashlen;
                    110:        int kout, r;
                    111:
                    112:        if (kex->verify_host_key == NULL) {
                    113:                r = SSH_ERR_INVALID_ARGUMENT;
                    114:                goto out;
                    115:        }
1.1       markus    116:        /* key, cert */
1.17      markus    117:        if ((r = sshpkt_get_string(ssh, &server_host_key_blob,
                    118:            &sbloblen)) != 0 ||
                    119:            (r = sshkey_from_blob(server_host_key_blob, sbloblen,
                    120:            &server_host_key)) != 0)
                    121:                goto out;
1.18      djm       122:        if (server_host_key->type != kex->hostkey_type ||
                    123:            (kex->hostkey_type == KEY_ECDSA &&
                    124:            server_host_key->ecdsa_nid != kex->hostkey_nid)) {
1.17      markus    125:                r = SSH_ERR_KEY_TYPE_MISMATCH;
                    126:                goto out;
                    127:        }
                    128:        if (kex->verify_host_key(server_host_key, ssh) == -1) {
                    129:                r = SSH_ERR_SIGNATURE_INVALID;
                    130:                goto out;
                    131:        }
1.6       miod      132:        /* DH parameter f, server public DH key */
1.17      markus    133:        if ((dh_server_pub = BN_new()) == NULL) {
                    134:                r = SSH_ERR_ALLOC_FAIL;
                    135:                goto out;
                    136:        }
                    137:        /* signed H */
                    138:        if ((r = sshpkt_get_bignum2(ssh, dh_server_pub)) != 0 ||
                    139:            (r = sshpkt_get_string(ssh, &signature, &slen)) != 0 ||
                    140:            (r = sshpkt_get_end(ssh)) != 0)
                    141:                goto out;
1.1       markus    142: #ifdef DEBUG_KEXDH
                    143:        fprintf(stderr, "dh_server_pub= ");
                    144:        BN_print_fp(stderr, dh_server_pub);
                    145:        fprintf(stderr, "\n");
                    146:        debug("bits %d", BN_num_bits(dh_server_pub));
                    147: #endif
1.17      markus    148:        if (!dh_pub_is_valid(kex->dh, dh_server_pub)) {
                    149:                sshpkt_disconnect(ssh, "bad server public DH value");
                    150:                r = SSH_ERR_MESSAGE_INCOMPLETE;
                    151:                goto out;
                    152:        }
1.1       markus    153:
1.17      markus    154:        klen = DH_size(kex->dh);
                    155:        if ((kbuf = malloc(klen)) == NULL ||
                    156:            (shared_secret = BN_new()) == NULL) {
                    157:                r = SSH_ERR_ALLOC_FAIL;
                    158:                goto out;
                    159:        }
                    160:        if ((kout = DH_compute_key(kbuf, dh_server_pub, kex->dh)) < 0 ||
                    161:            BN_bin2bn(kbuf, kout, shared_secret) == NULL) {
                    162:                r = SSH_ERR_LIBCRYPTO_ERROR;
                    163:                goto out;
                    164:        }
1.1       markus    165: #ifdef DEBUG_KEXDH
                    166:        dump_digest("shared secret", kbuf, kout);
                    167: #endif
                    168:
                    169:        /* calc and verify H */
1.23    ! djm       170:        DH_get0_key(kex->dh, &pub_key, NULL);
1.17      markus    171:        hashlen = sizeof(hash);
                    172:        if ((r = kex_dh_hash(
1.19      djm       173:            kex->hash_alg,
1.1       markus    174:            kex->client_version_string,
                    175:            kex->server_version_string,
1.17      markus    176:            sshbuf_ptr(kex->my), sshbuf_len(kex->my),
                    177:            sshbuf_ptr(kex->peer), sshbuf_len(kex->peer),
1.1       markus    178:            server_host_key_blob, sbloblen,
1.23    ! djm       179:            pub_key,
1.1       markus    180:            dh_server_pub,
1.3       djm       181:            shared_secret,
1.17      markus    182:            hash, &hashlen)) != 0)
                    183:                goto out;
1.1       markus    184:
1.17      markus    185:        if ((r = sshkey_verify(server_host_key, signature, slen, hash, hashlen,
1.21      djm       186:            kex->hostkey_alg, ssh->compat)) != 0)
1.17      markus    187:                goto out;
1.1       markus    188:
                    189:        /* save session id */
                    190:        if (kex->session_id == NULL) {
1.3       djm       191:                kex->session_id_len = hashlen;
1.17      markus    192:                kex->session_id = malloc(kex->session_id_len);
                    193:                if (kex->session_id == NULL) {
                    194:                        r = SSH_ERR_ALLOC_FAIL;
                    195:                        goto out;
                    196:                }
1.1       markus    197:                memcpy(kex->session_id, hash, kex->session_id_len);
                    198:        }
                    199:
1.17      markus    200:        if ((r = kex_derive_keys_bn(ssh, hash, hashlen, shared_secret)) == 0)
                    201:                r = kex_send_newkeys(ssh);
                    202:  out:
                    203:        explicit_bzero(hash, sizeof(hash));
                    204:        DH_free(kex->dh);
                    205:        kex->dh = NULL;
1.22      jsing     206:        BN_clear_free(dh_server_pub);
1.17      markus    207:        if (kbuf) {
                    208:                explicit_bzero(kbuf, klen);
                    209:                free(kbuf);
                    210:        }
1.22      jsing     211:        BN_clear_free(shared_secret);
1.17      markus    212:        sshkey_free(server_host_key);
                    213:        free(server_host_key_blob);
                    214:        free(signature);
                    215:        return r;
1.1       markus    216: }