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

1.9     ! djm         1: /* $OpenBSD: kexecdhs.c,v 1.8 2014/01/09 23:20:00 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:
                     33: #include "xmalloc.h"
                     34: #include "buffer.h"
                     35: #include "key.h"
                     36: #include "cipher.h"
                     37: #include "kex.h"
                     38: #include "log.h"
                     39: #include "packet.h"
                     40: #include "ssh2.h"
                     41:
                     42: void
                     43: kexecdh_server(Kex *kex)
                     44: {
                     45:        EC_POINT *client_public;
                     46:        EC_KEY *server_key;
                     47:        const EC_GROUP *group;
                     48:        BIGNUM *shared_secret;
                     49:        Key *server_host_private, *server_host_public;
                     50:        u_char *server_host_key_blob = NULL, *signature = NULL;
                     51:        u_char *kbuf, *hash;
                     52:        u_int klen, slen, sbloblen, hashlen;
                     53:
1.3       djm        54:        if ((server_key = EC_KEY_new_by_curve_name(kex->ec_nid)) == NULL)
1.1       djm        55:                fatal("%s: EC_KEY_new_by_curve_name failed", __func__);
                     56:        if (EC_KEY_generate_key(server_key) != 1)
                     57:                fatal("%s: EC_KEY_generate_key failed", __func__);
                     58:        group = EC_KEY_get0_group(server_key);
                     59:
                     60: #ifdef DEBUG_KEXECDH
                     61:        fputs("server private key:\n", stderr);
                     62:        key_dump_ec_key(server_key);
                     63: #endif
                     64:
                     65:        if (kex->load_host_public_key == NULL ||
                     66:            kex->load_host_private_key == NULL)
                     67:                fatal("Cannot load hostkey");
                     68:        server_host_public = kex->load_host_public_key(kex->hostkey_type);
                     69:        if (server_host_public == NULL)
                     70:                fatal("Unsupported hostkey type %d", kex->hostkey_type);
                     71:        server_host_private = kex->load_host_private_key(kex->hostkey_type);
                     72:
                     73:        debug("expecting SSH2_MSG_KEX_ECDH_INIT");
                     74:        packet_read_expect(SSH2_MSG_KEX_ECDH_INIT);
                     75:        if ((client_public = EC_POINT_new(group)) == NULL)
                     76:                fatal("%s: EC_POINT_new failed", __func__);
                     77:        packet_get_ecpoint(group, client_public);
                     78:        packet_check_eom();
                     79:
                     80:        if (key_ec_validate_public(group, client_public) != 0)
                     81:                fatal("%s: invalid client public key", __func__);
                     82:
                     83: #ifdef DEBUG_KEXECDH
                     84:        fputs("client public key:\n", stderr);
                     85:        key_dump_ec_point(group, client_public);
                     86: #endif
                     87:
                     88:        /* Calculate shared_secret */
                     89:        klen = (EC_GROUP_get_degree(group) + 7) / 8;
                     90:        kbuf = xmalloc(klen);
                     91:        if (ECDH_compute_key(kbuf, klen, client_public,
                     92:            server_key, NULL) != (int)klen)
                     93:                fatal("%s: ECDH_compute_key failed", __func__);
                     94:
                     95: #ifdef DEBUG_KEXDH
                     96:        dump_digest("shared secret", kbuf, klen);
                     97: #endif
                     98:        if ((shared_secret = BN_new()) == NULL)
                     99:                fatal("%s: BN_new failed", __func__);
                    100:        if (BN_bin2bn(kbuf, klen, shared_secret) == NULL)
                    101:                fatal("%s: BN_bin2bn failed", __func__);
                    102:        memset(kbuf, 0, klen);
1.4       djm       103:        free(kbuf);
1.1       djm       104:
                    105:        /* calc H */
                    106:        key_to_blob(server_host_public, &server_host_key_blob, &sbloblen);
                    107:        kex_ecdh_hash(
1.8       djm       108:            kex->hash_alg,
1.1       djm       109:            group,
                    110:            kex->client_version_string,
                    111:            kex->server_version_string,
                    112:            buffer_ptr(&kex->peer), buffer_len(&kex->peer),
                    113:            buffer_ptr(&kex->my), buffer_len(&kex->my),
                    114:            server_host_key_blob, sbloblen,
                    115:            client_public,
                    116:            EC_KEY_get0_public_key(server_key),
                    117:            shared_secret,
                    118:            &hash, &hashlen
                    119:        );
                    120:        EC_POINT_clear_free(client_public);
                    121:
                    122:        /* save session id := H */
                    123:        if (kex->session_id == NULL) {
                    124:                kex->session_id_len = hashlen;
                    125:                kex->session_id = xmalloc(kex->session_id_len);
                    126:                memcpy(kex->session_id, hash, kex->session_id_len);
                    127:        }
                    128:
                    129:        /* sign H */
1.5       markus    130:        kex->sign(server_host_private, server_host_public, &signature, &slen,
                    131:            hash, hashlen);
1.1       djm       132:
                    133:        /* destroy_sensitive_data(); */
                    134:
                    135:        /* send server hostkey, ECDH pubkey 'Q_S' and signed H */
                    136:        packet_start(SSH2_MSG_KEX_ECDH_REPLY);
                    137:        packet_put_string(server_host_key_blob, sbloblen);
                    138:        packet_put_ecpoint(group, EC_KEY_get0_public_key(server_key));
                    139:        packet_put_string(signature, slen);
                    140:        packet_send();
                    141:
1.4       djm       142:        free(signature);
                    143:        free(server_host_key_blob);
1.1       djm       144:        /* have keys, free server key */
                    145:        EC_KEY_free(server_key);
                    146:
1.9     ! djm       147:        kex_derive_keys_bn(kex, hash, hashlen, shared_secret);
1.1       djm       148:        BN_clear_free(shared_secret);
                    149:        kex_finish(kex);
                    150: }