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

Annotation of src/usr.bin/ssh/kexecdhc.c, Revision 1.2

1.2     ! djm         1: /* $OpenBSD: kexecdhc.c,v 1.1 2010/08/31 11:54:45 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:
                     29: #include <stdio.h>
                     30: #include <string.h>
                     31: #include <signal.h>
                     32:
                     33: #include <openssl/ecdh.h>
                     34:
                     35: #include "xmalloc.h"
                     36: #include "buffer.h"
                     37: #include "key.h"
                     38: #include "cipher.h"
                     39: #include "kex.h"
                     40: #include "log.h"
                     41: #include "packet.h"
                     42: #include "dh.h"
                     43: #include "ssh2.h"
                     44:
                     45: void
                     46: kexecdh_client(Kex *kex)
                     47: {
                     48:        EC_KEY *client_key;
                     49:        EC_POINT *server_public;
                     50:        const EC_GROUP *group;
                     51:        BIGNUM *shared_secret;
                     52:        Key *server_host_key;
                     53:        u_char *server_host_key_blob = NULL, *signature = NULL;
                     54:        u_char *kbuf, *hash;
                     55:        u_int klen, slen, sbloblen, hashlen;
                     56:        int curve_nid;
                     57:
1.2     ! djm        58:        if ((curve_nid = kex_ecdh_name_to_nid(kex->name)) == -1)
        !            59:                fatal("%s: unsupported ECDH curve \"%s\"", __func__, kex->name);
1.1       djm        60:        if ((client_key = EC_KEY_new_by_curve_name(curve_nid)) == NULL)
                     61:                fatal("%s: EC_KEY_new_by_curve_name failed", __func__);
                     62:        if (EC_KEY_generate_key(client_key) != 1)
                     63:                fatal("%s: EC_KEY_generate_key failed", __func__);
                     64:        group = EC_KEY_get0_group(client_key);
                     65:
                     66:        packet_start(SSH2_MSG_KEX_ECDH_INIT);
                     67:        packet_put_ecpoint(group, EC_KEY_get0_public_key(client_key));
                     68:        packet_send();
                     69:        debug("sending SSH2_MSG_KEX_ECDH_INIT");
                     70:
                     71: #ifdef DEBUG_KEXECDH
                     72:        fputs("client private key:\n", stderr);
                     73:        key_dump_ec_key(client_key);
                     74: #endif
                     75:
                     76:        debug("expecting SSH2_MSG_KEX_ECDH_REPLY");
                     77:        packet_read_expect(SSH2_MSG_KEX_ECDH_REPLY);
                     78:
                     79:        /* hostkey */
                     80:        server_host_key_blob = packet_get_string(&sbloblen);
                     81:        server_host_key = key_from_blob(server_host_key_blob, sbloblen);
                     82:        if (server_host_key == NULL)
                     83:                fatal("cannot decode server_host_key_blob");
                     84:        if (server_host_key->type != kex->hostkey_type)
                     85:                fatal("type mismatch for decoded server_host_key_blob");
                     86:        if (kex->verify_host_key == NULL)
                     87:                fatal("cannot verify server_host_key");
                     88:        if (kex->verify_host_key(server_host_key) == -1)
                     89:                fatal("server_host_key verification failed");
                     90:
                     91:        /* Q_S, server public key */
                     92:        if ((server_public = EC_POINT_new(group)) == NULL)
                     93:                fatal("%s: EC_POINT_new failed", __func__);
                     94:        packet_get_ecpoint(group, server_public);
                     95:
                     96:        if (key_ec_validate_public(group, server_public) != 0)
                     97:                fatal("%s: invalid server public key", __func__);
                     98:
                     99: #ifdef DEBUG_KEXECDH
                    100:        fputs("server public key:\n", stderr);
                    101:        key_dump_ec_point(group, server_public);
                    102: #endif
                    103:
                    104:        /* signed H */
                    105:        signature = packet_get_string(&slen);
                    106:        packet_check_eom();
                    107:
                    108:        klen = (EC_GROUP_get_degree(group) + 7) / 8;
                    109:        kbuf = xmalloc(klen);
                    110:        if (ECDH_compute_key(kbuf, klen, server_public,
                    111:            client_key, NULL) != (int)klen)
                    112:                fatal("%s: ECDH_compute_key failed", __func__);
                    113:
                    114: #ifdef DEBUG_KEXECDH
                    115:        dump_digest("shared secret", kbuf, klen);
                    116: #endif
                    117:        if ((shared_secret = BN_new()) == NULL)
                    118:                fatal("%s: BN_new failed", __func__);
                    119:        if (BN_bin2bn(kbuf, klen, shared_secret) == NULL)
                    120:                fatal("%s: BN_bin2bn failed", __func__);
                    121:        memset(kbuf, 0, klen);
                    122:        xfree(kbuf);
                    123:
                    124:        /* calc and verify H */
                    125:        kex_ecdh_hash(
                    126:            kex->evp_md,
                    127:            group,
                    128:            kex->client_version_string,
                    129:            kex->server_version_string,
                    130:            buffer_ptr(&kex->my), buffer_len(&kex->my),
                    131:            buffer_ptr(&kex->peer), buffer_len(&kex->peer),
                    132:            server_host_key_blob, sbloblen,
                    133:            EC_KEY_get0_public_key(client_key),
                    134:            server_public,
                    135:            shared_secret,
                    136:            &hash, &hashlen
                    137:        );
                    138:        xfree(server_host_key_blob);
                    139:        EC_POINT_clear_free(server_public);
                    140:        EC_KEY_free(client_key);
                    141:
                    142:        if (key_verify(server_host_key, signature, slen, hash, hashlen) != 1)
                    143:                fatal("key_verify failed for server_host_key");
                    144:        key_free(server_host_key);
                    145:        xfree(signature);
                    146:
                    147:        /* save session id */
                    148:        if (kex->session_id == NULL) {
                    149:                kex->session_id_len = hashlen;
                    150:                kex->session_id = xmalloc(kex->session_id_len);
                    151:                memcpy(kex->session_id, hash, kex->session_id_len);
                    152:        }
                    153:
                    154:        kex_derive_keys(kex, hash, hashlen, shared_secret);
                    155:        BN_clear_free(shared_secret);
                    156:        kex_finish(kex);
                    157: }