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

Annotation of src/usr.bin/ssh/kexc25519c.c, Revision 1.6

1.6     ! markus      1: /* $OpenBSD: kexc25519c.c,v 1.5 2015/01/19 19:52:16 markus Exp $ */
1.1       markus      2: /*
                      3:  * Copyright (c) 2001 Markus Friedl.  All rights reserved.
                      4:  * Copyright (c) 2010 Damien Miller.  All rights reserved.
                      5:  * Copyright (c) 2013 Aris Adamantiadis.  All rights reserved.
                      6:  *
                      7:  * Redistribution and use in source and binary forms, with or without
                      8:  * modification, are permitted provided that the following conditions
                      9:  * are met:
                     10:  * 1. Redistributions of source code must retain the above copyright
                     11:  *    notice, this list of conditions and the following disclaimer.
                     12:  * 2. Redistributions in binary form must reproduce the above copyright
                     13:  *    notice, this list of conditions and the following disclaimer in the
                     14:  *    documentation and/or other materials provided with the distribution.
                     15:  *
                     16:  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
                     17:  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
                     18:  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
                     19:  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
                     20:  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
                     21:  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
                     22:  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
                     23:  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
                     24:  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
                     25:  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
                     26:  */
                     27:
                     28: #include <sys/types.h>
                     29:
                     30: #include <stdio.h>
                     31: #include <string.h>
                     32: #include <signal.h>
                     33:
1.6     ! markus     34: #include "sshkey.h"
1.1       markus     35: #include "cipher.h"
                     36: #include "kex.h"
                     37: #include "log.h"
                     38: #include "packet.h"
                     39: #include "ssh2.h"
1.6     ! markus     40: #include "sshbuf.h"
        !            41: #include "digest.h"
        !            42: #include "ssherr.h"
1.1       markus     43:
1.6     ! markus     44: static int
        !            45: input_kex_c25519_reply(int type, u_int32_t seq, void *ctxt);
        !            46:
        !            47: int
        !            48: kexc25519_client(struct ssh *ssh)
1.1       markus     49: {
1.6     ! markus     50:        struct kex *kex = ssh->kex;
        !            51:        int r;
1.1       markus     52:
1.6     ! markus     53:        kexc25519_keygen(kex->c25519_client_key, kex->c25519_client_pubkey);
1.1       markus     54: #ifdef DEBUG_KEXECDH
1.6     ! markus     55:        dump_digest("client private key:", kex->c25519_client_key,
        !            56:            sizeof(kex->c25519_client_key));
1.1       markus     57: #endif
1.6     ! markus     58:        if ((r = sshpkt_start(ssh, SSH2_MSG_KEX_ECDH_INIT)) != 0 ||
        !            59:            (r = sshpkt_put_string(ssh, kex->c25519_client_pubkey,
        !            60:            sizeof(kex->c25519_client_pubkey))) != 0 ||
        !            61:            (r = sshpkt_send(ssh)) != 0)
        !            62:                return r;
1.1       markus     63:
                     64:        debug("expecting SSH2_MSG_KEX_ECDH_REPLY");
1.6     ! markus     65:        ssh_dispatch_set(ssh, SSH2_MSG_KEX_ECDH_REPLY, &input_kex_c25519_reply);
        !            66:        return 0;
        !            67: }
        !            68:
        !            69: static int
        !            70: input_kex_c25519_reply(int type, u_int32_t seq, void *ctxt)
        !            71: {
        !            72:        struct ssh *ssh = ctxt;
        !            73:        struct kex *kex = ssh->kex;
        !            74:        struct sshkey *server_host_key = NULL;
        !            75:        struct sshbuf *shared_secret = NULL;
        !            76:        u_char *server_pubkey = NULL;
        !            77:        u_char *server_host_key_blob = NULL, *signature = NULL;
        !            78:        u_char hash[SSH_DIGEST_MAX_LENGTH];
        !            79:        size_t slen, pklen, sbloblen, hashlen;
        !            80:        int r;
        !            81:
        !            82:        if (kex->verify_host_key == NULL) {
        !            83:                r = SSH_ERR_INVALID_ARGUMENT;
        !            84:                goto out;
        !            85:        }
1.1       markus     86:
                     87:        /* hostkey */
1.6     ! markus     88:        if ((r = sshpkt_get_string(ssh, &server_host_key_blob,
        !            89:            &sbloblen)) != 0 ||
        !            90:            (r = sshkey_from_blob(server_host_key_blob, sbloblen,
        !            91:            &server_host_key)) != 0)
        !            92:                goto out;
        !            93:        if (server_host_key->type != kex->hostkey_type) {
        !            94:                r = SSH_ERR_KEY_TYPE_MISMATCH;
        !            95:                goto out;
        !            96:        }
        !            97:        if (kex->verify_host_key(server_host_key, ssh) == -1) {
        !            98:                r = SSH_ERR_SIGNATURE_INVALID;
        !            99:                goto out;
        !           100:        }
1.1       markus    101:
                    102:        /* Q_S, server public key */
1.6     ! markus    103:        /* signed H */
        !           104:        if ((r = sshpkt_get_string(ssh, &server_pubkey, &pklen)) != 0 ||
        !           105:            (r = sshpkt_get_string(ssh, &signature, &slen)) != 0 ||
        !           106:            (r = sshpkt_get_end(ssh)) != 0)
        !           107:                goto out;
        !           108:        if (pklen != CURVE25519_SIZE) {
        !           109:                r = SSH_ERR_SIGNATURE_INVALID;
        !           110:                goto out;
        !           111:        }
1.1       markus    112:
                    113: #ifdef DEBUG_KEXECDH
                    114:        dump_digest("server public key:", server_pubkey, CURVE25519_SIZE);
                    115: #endif
                    116:
1.6     ! markus    117:        if ((shared_secret = sshbuf_new()) == NULL) {
        !           118:                r = SSH_ERR_ALLOC_FAIL;
        !           119:                goto out;
        !           120:        }
        !           121:        if ((r = kexc25519_shared_key(kex->c25519_client_key, server_pubkey,
        !           122:            shared_secret)) < 0)
        !           123:                goto out;
1.1       markus    124:
                    125:        /* calc and verify H */
1.6     ! markus    126:        hashlen = sizeof(hash);
        !           127:        if ((r = kex_c25519_hash(
1.3       djm       128:            kex->hash_alg,
1.1       markus    129:            kex->client_version_string,
                    130:            kex->server_version_string,
1.6     ! markus    131:            sshbuf_ptr(kex->my), sshbuf_len(kex->my),
        !           132:            sshbuf_ptr(kex->peer), sshbuf_len(kex->peer),
1.1       markus    133:            server_host_key_blob, sbloblen,
1.6     ! markus    134:            kex->c25519_client_pubkey,
1.1       markus    135:            server_pubkey,
1.6     ! markus    136:            sshbuf_ptr(shared_secret), sshbuf_len(shared_secret),
        !           137:            hash, &hashlen)) < 0)
        !           138:                goto out;
        !           139:
        !           140:        if ((r = sshkey_verify(server_host_key, signature, slen, hash, hashlen,
        !           141:            ssh->compat)) != 0)
        !           142:                goto out;
1.1       markus    143:
                    144:        /* save session id */
                    145:        if (kex->session_id == NULL) {
                    146:                kex->session_id_len = hashlen;
1.6     ! markus    147:                kex->session_id = malloc(kex->session_id_len);
        !           148:                if (kex->session_id == NULL) {
        !           149:                        r = SSH_ERR_ALLOC_FAIL;
        !           150:                        goto out;
        !           151:                }
1.1       markus    152:                memcpy(kex->session_id, hash, kex->session_id_len);
                    153:        }
1.6     ! markus    154:
        !           155:        if ((r = kex_derive_keys(ssh, hash, hashlen, shared_secret)) == 0)
        !           156:                r = kex_send_newkeys(ssh);
        !           157: out:
        !           158:        explicit_bzero(hash, sizeof(hash));
        !           159:        explicit_bzero(kex->c25519_client_key, sizeof(kex->c25519_client_key));
        !           160:        free(server_host_key_blob);
        !           161:        free(server_pubkey);
        !           162:        free(signature);
        !           163:        sshkey_free(server_host_key);
        !           164:        sshbuf_free(shared_secret);
        !           165:        return r;
1.1       markus    166: }