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

Annotation of src/usr.bin/ssh/kexecdhs.c, Revision 1.21

1.21    ! djm         1: /* $OpenBSD: kexecdhs.c,v 1.20 2019/01/21 09:55:52 djm Exp $ */
1.1       djm         2: /*
                      3:  * Copyright (c) 2001 Markus Friedl.  All rights reserved.
                      4:  * Copyright (c) 2010 Damien Miller.  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:
                     27: #include <sys/types.h>
                     28: #include <string.h>
                     29: #include <signal.h>
                     30:
                     31: #include <openssl/ecdh.h>
                     32:
1.12      markus     33: #include "sshkey.h"
1.1       djm        34: #include "cipher.h"
1.12      markus     35: #include "digest.h"
1.1       djm        36: #include "kex.h"
                     37: #include "log.h"
                     38: #include "packet.h"
                     39: #include "ssh2.h"
                     40:
1.12      markus     41: #include "dispatch.h"
                     42: #include "compat.h"
                     43: #include "ssherr.h"
                     44: #include "sshbuf.h"
                     45:
1.16      markus     46: static int input_kex_ecdh_init(int, u_int32_t, struct ssh *);
1.12      markus     47:
                     48: int
                     49: kexecdh_server(struct ssh *ssh)
1.1       djm        50: {
1.12      markus     51:        debug("expecting SSH2_MSG_KEX_ECDH_INIT");
                     52:        ssh_dispatch_set(ssh, SSH2_MSG_KEX_ECDH_INIT, &input_kex_ecdh_init);
                     53:        return 0;
                     54: }
                     55:
                     56: static int
1.16      markus     57: input_kex_ecdh_init(int type, u_int32_t seq, struct ssh *ssh)
1.12      markus     58: {
                     59:        struct kex *kex = ssh->kex;
1.1       djm        60:        EC_POINT *client_public;
1.12      markus     61:        EC_KEY *server_key = NULL;
1.1       djm        62:        const EC_GROUP *group;
1.12      markus     63:        const EC_POINT *public_key;
                     64:        BIGNUM *shared_secret = NULL;
                     65:        struct sshkey *server_host_private, *server_host_public;
1.1       djm        66:        u_char *server_host_key_blob = NULL, *signature = NULL;
1.12      markus     67:        u_char *kbuf = NULL;
                     68:        u_char hash[SSH_DIGEST_MAX_LENGTH];
                     69:        size_t slen, sbloblen;
                     70:        size_t klen = 0, hashlen;
                     71:        int r;
                     72:
                     73:        if ((server_key = EC_KEY_new_by_curve_name(kex->ec_nid)) == NULL) {
                     74:                r = SSH_ERR_ALLOC_FAIL;
                     75:                goto out;
                     76:        }
                     77:        if (EC_KEY_generate_key(server_key) != 1) {
                     78:                r = SSH_ERR_LIBCRYPTO_ERROR;
                     79:                goto out;
                     80:        }
1.1       djm        81:        group = EC_KEY_get0_group(server_key);
                     82:
                     83: #ifdef DEBUG_KEXECDH
                     84:        fputs("server private key:\n", stderr);
1.12      markus     85:        sshkey_dump_ec_key(server_key);
1.1       djm        86: #endif
                     87:
1.21    ! djm        88:        if ((r = kex_load_hostkey(ssh, &server_host_private,
        !            89:            &server_host_public)) != 0)
1.12      markus     90:                goto out;
                     91:        if ((client_public = EC_POINT_new(group)) == NULL) {
                     92:                r = SSH_ERR_ALLOC_FAIL;
                     93:                goto out;
                     94:        }
                     95:        if ((r = sshpkt_get_ec(ssh, client_public, group)) != 0 ||
                     96:            (r = sshpkt_get_end(ssh)) != 0)
                     97:                goto out;
1.1       djm        98:
                     99: #ifdef DEBUG_KEXECDH
                    100:        fputs("client public key:\n", stderr);
1.12      markus    101:        sshkey_dump_ec_point(group, client_public);
1.1       djm       102: #endif
1.12      markus    103:        if (sshkey_ec_validate_public(group, client_public) != 0) {
                    104:                sshpkt_disconnect(ssh, "invalid client public key");
                    105:                r = SSH_ERR_MESSAGE_INCOMPLETE;
                    106:                goto out;
                    107:        }
1.1       djm       108:
                    109:        /* Calculate shared_secret */
                    110:        klen = (EC_GROUP_get_degree(group) + 7) / 8;
1.12      markus    111:        if ((kbuf = malloc(klen)) == NULL ||
                    112:            (shared_secret = BN_new()) == NULL) {
                    113:                r = SSH_ERR_ALLOC_FAIL;
                    114:                goto out;
                    115:        }
1.1       djm       116:        if (ECDH_compute_key(kbuf, klen, client_public,
1.12      markus    117:            server_key, NULL) != (int)klen ||
                    118:            BN_bin2bn(kbuf, klen, shared_secret) == NULL) {
                    119:                r = SSH_ERR_LIBCRYPTO_ERROR;
                    120:                goto out;
                    121:        }
1.1       djm       122:
1.12      markus    123: #ifdef DEBUG_KEXECDH
1.1       djm       124:        dump_digest("shared secret", kbuf, klen);
                    125: #endif
                    126:        /* calc H */
1.12      markus    127:        if ((r = sshkey_to_blob(server_host_public, &server_host_key_blob,
                    128:            &sbloblen)) != 0)
                    129:                goto out;
                    130:        hashlen = sizeof(hash);
                    131:        if ((r = kex_ecdh_hash(
1.8       djm       132:            kex->hash_alg,
1.1       djm       133:            group,
1.18      djm       134:            kex->client_version,
                    135:            kex->server_version,
1.12      markus    136:            sshbuf_ptr(kex->peer), sshbuf_len(kex->peer),
                    137:            sshbuf_ptr(kex->my), sshbuf_len(kex->my),
1.1       djm       138:            server_host_key_blob, sbloblen,
                    139:            client_public,
                    140:            EC_KEY_get0_public_key(server_key),
                    141:            shared_secret,
1.12      markus    142:            hash, &hashlen)) != 0)
                    143:                goto out;
1.1       djm       144:
                    145:        /* sign H */
1.19      djm       146:        if ((r = kex->sign(ssh, server_host_private, server_host_public,
                    147:            &signature, &slen, hash, hashlen, kex->hostkey_alg)) < 0)
1.12      markus    148:                goto out;
1.1       djm       149:
                    150:        /* destroy_sensitive_data(); */
                    151:
1.12      markus    152:        public_key = EC_KEY_get0_public_key(server_key);
1.1       djm       153:        /* send server hostkey, ECDH pubkey 'Q_S' and signed H */
1.12      markus    154:        if ((r = sshpkt_start(ssh, SSH2_MSG_KEX_ECDH_REPLY)) != 0 ||
                    155:            (r = sshpkt_put_string(ssh, server_host_key_blob, sbloblen)) != 0 ||
                    156:            (r = sshpkt_put_ec(ssh, public_key, group)) != 0 ||
                    157:            (r = sshpkt_put_string(ssh, signature, slen)) != 0 ||
                    158:            (r = sshpkt_send(ssh)) != 0)
                    159:                goto out;
                    160:
                    161:        if ((r = kex_derive_keys_bn(ssh, hash, hashlen, shared_secret)) == 0)
                    162:                r = kex_send_newkeys(ssh);
                    163:  out:
                    164:        explicit_bzero(hash, sizeof(hash));
1.17      jsing     165:        EC_KEY_free(kex->ec_client_key);
                    166:        kex->ec_client_key = NULL;
                    167:        EC_KEY_free(server_key);
1.12      markus    168:        if (kbuf) {
                    169:                explicit_bzero(kbuf, klen);
                    170:                free(kbuf);
                    171:        }
1.17      jsing     172:        BN_clear_free(shared_secret);
1.12      markus    173:        free(server_host_key_blob);
1.4       djm       174:        free(signature);
1.12      markus    175:        return r;
1.1       djm       176: }