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

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