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

1.9     ! markus      1: /* $OpenBSD: kexecdhc.c,v 1.8 2015/01/19 19:52:16 markus 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:
1.9     ! markus     35: #include "sshkey.h"
1.1       djm        36: #include "cipher.h"
1.9     ! markus     37: #include "digest.h"
1.1       djm        38: #include "kex.h"
                     39: #include "log.h"
                     40: #include "packet.h"
                     41: #include "dh.h"
                     42: #include "ssh2.h"
1.9     ! markus     43: #include "dispatch.h"
        !            44: #include "compat.h"
        !            45: #include "ssherr.h"
        !            46: #include "sshbuf.h"
1.1       djm        47:
1.9     ! markus     48: static int input_kex_ecdh_reply(int, u_int32_t, void *);
        !            49:
        !            50: int
        !            51: kexecdh_client(struct ssh *ssh)
1.1       djm        52: {
1.9     ! markus     53:        struct kex *kex = ssh->kex;
        !            54:        EC_KEY *client_key = NULL;
1.1       djm        55:        const EC_GROUP *group;
1.9     ! markus     56:        const EC_POINT *public_key;
        !            57:        int r;
1.1       djm        58:
1.9     ! markus     59:        if ((client_key = EC_KEY_new_by_curve_name(kex->ec_nid)) == NULL) {
        !            60:                r = SSH_ERR_ALLOC_FAIL;
        !            61:                goto out;
        !            62:        }
        !            63:        if (EC_KEY_generate_key(client_key) != 1) {
        !            64:                r = SSH_ERR_LIBCRYPTO_ERROR;
        !            65:                goto out;
        !            66:        }
1.1       djm        67:        group = EC_KEY_get0_group(client_key);
1.9     ! markus     68:        public_key = EC_KEY_get0_public_key(client_key);
1.1       djm        69:
1.9     ! markus     70:        if ((r = sshpkt_start(ssh, SSH2_MSG_KEX_ECDH_INIT)) != 0 ||
        !            71:            (r = sshpkt_put_ec(ssh, public_key, group)) != 0 ||
        !            72:            (r = sshpkt_send(ssh)) != 0)
        !            73:                goto out;
1.1       djm        74:        debug("sending SSH2_MSG_KEX_ECDH_INIT");
                     75:
                     76: #ifdef DEBUG_KEXECDH
                     77:        fputs("client private key:\n", stderr);
1.9     ! markus     78:        sshkey_dump_ec_key(client_key);
1.1       djm        79: #endif
1.9     ! markus     80:        kex->ec_client_key = client_key;
        !            81:        kex->ec_group = group;
        !            82:        client_key = NULL;      /* owned by the kex */
1.1       djm        83:
                     84:        debug("expecting SSH2_MSG_KEX_ECDH_REPLY");
1.9     ! markus     85:        ssh_dispatch_set(ssh, SSH2_MSG_KEX_ECDH_REPLY, &input_kex_ecdh_reply);
        !            86:        r = 0;
        !            87:  out:
        !            88:        if (client_key)
        !            89:                EC_KEY_free(client_key);
        !            90:        return r;
        !            91: }
        !            92:
        !            93: static int
        !            94: input_kex_ecdh_reply(int type, u_int32_t seq, void *ctxt)
        !            95: {
        !            96:        struct ssh *ssh = ctxt;
        !            97:        struct kex *kex = ssh->kex;
        !            98:        const EC_GROUP *group;
        !            99:        EC_POINT *server_public = NULL;
        !           100:        EC_KEY *client_key;
        !           101:        BIGNUM *shared_secret = NULL;
        !           102:        struct sshkey *server_host_key = NULL;
        !           103:        u_char *server_host_key_blob = NULL, *signature = NULL;
        !           104:        u_char *kbuf = NULL;
        !           105:        u_char hash[SSH_DIGEST_MAX_LENGTH];
        !           106:        size_t slen, sbloblen;
        !           107:        size_t klen = 0, hashlen;
        !           108:        int r;
        !           109:
        !           110:        if (kex->verify_host_key == NULL) {
        !           111:                r = SSH_ERR_INVALID_ARGUMENT;
        !           112:                goto out;
        !           113:        }
        !           114:        group = kex->ec_group;
        !           115:        client_key = kex->ec_client_key;
1.1       djm       116:
                    117:        /* hostkey */
1.9     ! markus    118:        if ((r = sshpkt_get_string(ssh, &server_host_key_blob,
        !           119:            &sbloblen)) != 0 ||
        !           120:            (r = sshkey_from_blob(server_host_key_blob, sbloblen,
        !           121:            &server_host_key)) != 0)
        !           122:                goto out;
        !           123:        if (server_host_key->type != kex->hostkey_type) {
        !           124:                r = SSH_ERR_KEY_TYPE_MISMATCH;
        !           125:                goto out;
        !           126:        }
        !           127:        if (kex->verify_host_key(server_host_key, ssh) == -1) {
        !           128:                r = SSH_ERR_SIGNATURE_INVALID;
        !           129:                goto out;
        !           130:        }
1.1       djm       131:
                    132:        /* Q_S, server public key */
1.9     ! markus    133:        /* signed H */
        !           134:        if ((server_public = EC_POINT_new(group)) == NULL) {
        !           135:                r = SSH_ERR_ALLOC_FAIL;
        !           136:                goto out;
        !           137:        }
        !           138:        if ((r = sshpkt_get_ec(ssh, server_public, group)) != 0 ||
        !           139:            (r = sshpkt_get_string(ssh, &signature, &slen)) != 0 ||
        !           140:            (r = sshpkt_get_end(ssh)) != 0)
        !           141:                goto out;
1.1       djm       142:
                    143: #ifdef DEBUG_KEXECDH
                    144:        fputs("server public key:\n", stderr);
1.9     ! markus    145:        sshkey_dump_ec_point(group, server_public);
1.1       djm       146: #endif
1.9     ! markus    147:        if (sshkey_ec_validate_public(group, server_public) != 0) {
        !           148:                sshpkt_disconnect(ssh, "invalid server public key");
        !           149:                r = SSH_ERR_MESSAGE_INCOMPLETE;
        !           150:                goto out;
        !           151:        }
1.1       djm       152:
                    153:        klen = (EC_GROUP_get_degree(group) + 7) / 8;
1.9     ! markus    154:        if ((kbuf = malloc(klen)) == NULL ||
        !           155:            (shared_secret = BN_new()) == NULL) {
        !           156:                r = SSH_ERR_ALLOC_FAIL;
        !           157:                goto out;
        !           158:        }
1.1       djm       159:        if (ECDH_compute_key(kbuf, klen, server_public,
1.9     ! markus    160:            client_key, NULL) != (int)klen ||
        !           161:            BN_bin2bn(kbuf, klen, shared_secret) == NULL) {
        !           162:                r = SSH_ERR_LIBCRYPTO_ERROR;
        !           163:                goto out;
        !           164:        }
1.1       djm       165:
                    166: #ifdef DEBUG_KEXECDH
                    167:        dump_digest("shared secret", kbuf, klen);
                    168: #endif
                    169:        /* calc and verify H */
1.9     ! markus    170:        hashlen = sizeof(hash);
        !           171:        if ((r = kex_ecdh_hash(
1.5       djm       172:            kex->hash_alg,
1.1       djm       173:            group,
                    174:            kex->client_version_string,
                    175:            kex->server_version_string,
1.9     ! markus    176:            sshbuf_ptr(kex->my), sshbuf_len(kex->my),
        !           177:            sshbuf_ptr(kex->peer), sshbuf_len(kex->peer),
1.1       djm       178:            server_host_key_blob, sbloblen,
                    179:            EC_KEY_get0_public_key(client_key),
                    180:            server_public,
                    181:            shared_secret,
1.9     ! markus    182:            hash, &hashlen)) != 0)
        !           183:                goto out;
1.1       djm       184:
1.9     ! markus    185:        if ((r = sshkey_verify(server_host_key, signature, slen, hash,
        !           186:            hashlen, ssh->compat)) != 0)
        !           187:                goto out;
1.1       djm       188:
                    189:        /* save session id */
                    190:        if (kex->session_id == NULL) {
                    191:                kex->session_id_len = hashlen;
1.9     ! markus    192:                kex->session_id = malloc(kex->session_id_len);
        !           193:                if (kex->session_id == NULL) {
        !           194:                        r = SSH_ERR_ALLOC_FAIL;
        !           195:                        goto out;
        !           196:                }
1.1       djm       197:                memcpy(kex->session_id, hash, kex->session_id_len);
                    198:        }
                    199:
1.9     ! markus    200:        if ((r = kex_derive_keys_bn(ssh, hash, hashlen, shared_secret)) == 0)
        !           201:                r = kex_send_newkeys(ssh);
        !           202:  out:
        !           203:        explicit_bzero(hash, sizeof(hash));
        !           204:        if (kex->ec_client_key) {
        !           205:                EC_KEY_free(kex->ec_client_key);
        !           206:                kex->ec_client_key = NULL;
        !           207:        }
        !           208:        if (server_public)
        !           209:                EC_POINT_clear_free(server_public);
        !           210:        if (kbuf) {
        !           211:                explicit_bzero(kbuf, klen);
        !           212:                free(kbuf);
        !           213:        }
        !           214:        if (shared_secret)
        !           215:                BN_clear_free(shared_secret);
        !           216:        sshkey_free(server_host_key);
        !           217:        free(server_host_key_blob);
        !           218:        free(signature);
        !           219:        return r;
1.1       djm       220: }