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

1.14    ! djm         1: /* $OpenBSD: kexecdhs.c,v 1.13 2015/01/20 07:55:33 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:
                     46: static int input_kex_ecdh_init(int, u_int32_t, void *);
                     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
                     57: input_kex_ecdh_init(int type, u_int32_t seq, void *ctxt)
                     58: {
                     59:        struct ssh *ssh = ctxt;
                     60:        struct kex *kex = ssh->kex;
1.1       djm        61:        EC_POINT *client_public;
1.12      markus     62:        EC_KEY *server_key = NULL;
1.1       djm        63:        const EC_GROUP *group;
1.12      markus     64:        const EC_POINT *public_key;
                     65:        BIGNUM *shared_secret = NULL;
                     66:        struct sshkey *server_host_private, *server_host_public;
1.1       djm        67:        u_char *server_host_key_blob = NULL, *signature = NULL;
1.12      markus     68:        u_char *kbuf = NULL;
                     69:        u_char hash[SSH_DIGEST_MAX_LENGTH];
                     70:        size_t slen, sbloblen;
                     71:        size_t klen = 0, hashlen;
                     72:        int r;
                     73:
                     74:        if ((server_key = EC_KEY_new_by_curve_name(kex->ec_nid)) == NULL) {
                     75:                r = SSH_ERR_ALLOC_FAIL;
                     76:                goto out;
                     77:        }
                     78:        if (EC_KEY_generate_key(server_key) != 1) {
                     79:                r = SSH_ERR_LIBCRYPTO_ERROR;
                     80:                goto out;
                     81:        }
1.1       djm        82:        group = EC_KEY_get0_group(server_key);
                     83:
                     84: #ifdef DEBUG_KEXECDH
                     85:        fputs("server private key:\n", stderr);
1.12      markus     86:        sshkey_dump_ec_key(server_key);
1.1       djm        87: #endif
                     88:
                     89:        if (kex->load_host_public_key == NULL ||
1.12      markus     90:            kex->load_host_private_key == NULL) {
                     91:                r = SSH_ERR_INVALID_ARGUMENT;
                     92:                goto out;
                     93:        }
1.14    ! djm        94:        server_host_public = kex->load_host_public_key(kex->hostkey_type,
        !            95:            kex->hostkey_nid, ssh);
        !            96:        server_host_private = kex->load_host_private_key(kex->hostkey_type,
        !            97:            kex->hostkey_nid, ssh);
1.13      djm        98:        if (server_host_public == NULL) {
1.12      markus     99:                r = SSH_ERR_NO_HOSTKEY_LOADED;
                    100:                goto out;
                    101:        }
                    102:        if ((client_public = EC_POINT_new(group)) == NULL) {
                    103:                r = SSH_ERR_ALLOC_FAIL;
                    104:                goto out;
                    105:        }
                    106:        if ((r = sshpkt_get_ec(ssh, client_public, group)) != 0 ||
                    107:            (r = sshpkt_get_end(ssh)) != 0)
                    108:                goto out;
1.1       djm       109:
                    110: #ifdef DEBUG_KEXECDH
                    111:        fputs("client public key:\n", stderr);
1.12      markus    112:        sshkey_dump_ec_point(group, client_public);
1.1       djm       113: #endif
1.12      markus    114:        if (sshkey_ec_validate_public(group, client_public) != 0) {
                    115:                sshpkt_disconnect(ssh, "invalid client public key");
                    116:                r = SSH_ERR_MESSAGE_INCOMPLETE;
                    117:                goto out;
                    118:        }
1.1       djm       119:
                    120:        /* Calculate shared_secret */
                    121:        klen = (EC_GROUP_get_degree(group) + 7) / 8;
1.12      markus    122:        if ((kbuf = malloc(klen)) == NULL ||
                    123:            (shared_secret = BN_new()) == NULL) {
                    124:                r = SSH_ERR_ALLOC_FAIL;
                    125:                goto out;
                    126:        }
1.1       djm       127:        if (ECDH_compute_key(kbuf, klen, client_public,
1.12      markus    128:            server_key, NULL) != (int)klen ||
                    129:            BN_bin2bn(kbuf, klen, shared_secret) == NULL) {
                    130:                r = SSH_ERR_LIBCRYPTO_ERROR;
                    131:                goto out;
                    132:        }
1.1       djm       133:
1.12      markus    134: #ifdef DEBUG_KEXECDH
1.1       djm       135:        dump_digest("shared secret", kbuf, klen);
                    136: #endif
                    137:        /* calc H */
1.12      markus    138:        if ((r = sshkey_to_blob(server_host_public, &server_host_key_blob,
                    139:            &sbloblen)) != 0)
                    140:                goto out;
                    141:        hashlen = sizeof(hash);
                    142:        if ((r = kex_ecdh_hash(
1.8       djm       143:            kex->hash_alg,
1.1       djm       144:            group,
                    145:            kex->client_version_string,
                    146:            kex->server_version_string,
1.12      markus    147:            sshbuf_ptr(kex->peer), sshbuf_len(kex->peer),
                    148:            sshbuf_ptr(kex->my), sshbuf_len(kex->my),
1.1       djm       149:            server_host_key_blob, sbloblen,
                    150:            client_public,
                    151:            EC_KEY_get0_public_key(server_key),
                    152:            shared_secret,
1.12      markus    153:            hash, &hashlen)) != 0)
                    154:                goto out;
1.1       djm       155:
                    156:        /* save session id := H */
                    157:        if (kex->session_id == NULL) {
                    158:                kex->session_id_len = hashlen;
1.12      markus    159:                kex->session_id = malloc(kex->session_id_len);
                    160:                if (kex->session_id == NULL) {
                    161:                        r = SSH_ERR_ALLOC_FAIL;
                    162:                        goto out;
                    163:                }
1.1       djm       164:                memcpy(kex->session_id, hash, kex->session_id_len);
                    165:        }
                    166:
                    167:        /* sign H */
1.12      markus    168:        if ((r = kex->sign(server_host_private, server_host_public,
                    169:            &signature, &slen, hash, hashlen, ssh->compat)) < 0)
                    170:                goto out;
1.1       djm       171:
                    172:        /* destroy_sensitive_data(); */
                    173:
1.12      markus    174:        public_key = EC_KEY_get0_public_key(server_key);
1.1       djm       175:        /* send server hostkey, ECDH pubkey 'Q_S' and signed H */
1.12      markus    176:        if ((r = sshpkt_start(ssh, SSH2_MSG_KEX_ECDH_REPLY)) != 0 ||
                    177:            (r = sshpkt_put_string(ssh, server_host_key_blob, sbloblen)) != 0 ||
                    178:            (r = sshpkt_put_ec(ssh, public_key, group)) != 0 ||
                    179:            (r = sshpkt_put_string(ssh, signature, slen)) != 0 ||
                    180:            (r = sshpkt_send(ssh)) != 0)
                    181:                goto out;
                    182:
                    183:        if ((r = kex_derive_keys_bn(ssh, hash, hashlen, shared_secret)) == 0)
                    184:                r = kex_send_newkeys(ssh);
                    185:  out:
                    186:        explicit_bzero(hash, sizeof(hash));
                    187:        if (kex->ec_client_key) {
                    188:                EC_KEY_free(kex->ec_client_key);
                    189:                kex->ec_client_key = NULL;
                    190:        }
                    191:        if (server_key)
                    192:                EC_KEY_free(server_key);
                    193:        if (kbuf) {
                    194:                explicit_bzero(kbuf, klen);
                    195:                free(kbuf);
                    196:        }
                    197:        if (shared_secret)
                    198:                BN_clear_free(shared_secret);
                    199:        free(server_host_key_blob);
1.4       djm       200:        free(signature);
1.12      markus    201:        return r;
1.1       djm       202: }