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

Annotation of src/usr.bin/ssh/kexc25519s.c, Revision 1.10

1.10    ! markus      1: /* $OpenBSD: kexc25519s.c,v 1.9 2015/04/27 00:37:53 dtucker 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:  * 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>
1.9       dtucker    28: #include <stdio.h>
1.1       markus     29: #include <string.h>
                     30: #include <signal.h>
                     31:
1.6       markus     32: #include "sshkey.h"
1.1       markus     33: #include "cipher.h"
1.6       markus     34: #include "digest.h"
1.1       markus     35: #include "kex.h"
                     36: #include "log.h"
                     37: #include "packet.h"
                     38: #include "ssh2.h"
1.6       markus     39: #include "sshbuf.h"
                     40: #include "ssherr.h"
1.1       markus     41:
1.6       markus     42: static int input_kex_c25519_init(int, u_int32_t, void *);
                     43:
                     44: int
                     45: kexc25519_server(struct ssh *ssh)
                     46: {
                     47:        debug("expecting SSH2_MSG_KEX_ECDH_INIT");
                     48:        ssh_dispatch_set(ssh, SSH2_MSG_KEX_ECDH_INIT, &input_kex_c25519_init);
                     49:        return 0;
                     50: }
                     51:
                     52: static int
                     53: input_kex_c25519_init(int type, u_int32_t seq, void *ctxt)
1.1       markus     54: {
1.6       markus     55:        struct ssh *ssh = ctxt;
                     56:        struct kex *kex = ssh->kex;
                     57:        struct sshkey *server_host_private, *server_host_public;
                     58:        struct sshbuf *shared_secret = NULL;
1.1       markus     59:        u_char *server_host_key_blob = NULL, *signature = NULL;
                     60:        u_char server_key[CURVE25519_SIZE];
                     61:        u_char *client_pubkey = NULL;
                     62:        u_char server_pubkey[CURVE25519_SIZE];
1.6       markus     63:        u_char hash[SSH_DIGEST_MAX_LENGTH];
                     64:        size_t slen, pklen, sbloblen, hashlen;
                     65:        int r;
1.1       markus     66:
                     67:        /* generate private key */
                     68:        kexc25519_keygen(server_key, server_pubkey);
                     69: #ifdef DEBUG_KEXECDH
                     70:        dump_digest("server private key:", server_key, sizeof(server_key));
                     71: #endif
                     72:        if (kex->load_host_public_key == NULL ||
1.6       markus     73:            kex->load_host_private_key == NULL) {
                     74:                r = SSH_ERR_INVALID_ARGUMENT;
                     75:                goto out;
                     76:        }
1.8       djm        77:        server_host_public = kex->load_host_public_key(kex->hostkey_type,
                     78:            kex->hostkey_nid, ssh);
                     79:        server_host_private = kex->load_host_private_key(kex->hostkey_type,
                     80:            kex->hostkey_nid, ssh);
1.7       djm        81:        if (server_host_public == NULL) {
1.6       markus     82:                r = SSH_ERR_NO_HOSTKEY_LOADED;
                     83:                goto out;
                     84:        }
1.1       markus     85:
1.6       markus     86:        if ((r = sshpkt_get_string(ssh, &client_pubkey, &pklen)) != 0 ||
                     87:            (r = sshpkt_get_end(ssh)) != 0)
                     88:                goto out;
                     89:        if (pklen != CURVE25519_SIZE) {
                     90:                r = SSH_ERR_SIGNATURE_INVALID;
                     91:                goto out;
                     92:        }
1.1       markus     93: #ifdef DEBUG_KEXECDH
                     94:        dump_digest("client public key:", client_pubkey, CURVE25519_SIZE);
                     95: #endif
                     96:
1.6       markus     97:        if ((shared_secret = sshbuf_new()) == NULL) {
                     98:                r = SSH_ERR_ALLOC_FAIL;
                     99:                goto out;
                    100:        }
                    101:        if ((r = kexc25519_shared_key(server_key, client_pubkey,
                    102:            shared_secret)) < 0)
                    103:                goto out;
1.1       markus    104:
                    105:        /* calc H */
1.6       markus    106:        if ((r = sshkey_to_blob(server_host_public, &server_host_key_blob,
                    107:            &sbloblen)) != 0)
                    108:                goto out;
                    109:        hashlen = sizeof(hash);
                    110:        if ((r = kex_c25519_hash(
1.3       djm       111:            kex->hash_alg,
1.1       markus    112:            kex->client_version_string,
                    113:            kex->server_version_string,
1.6       markus    114:            sshbuf_ptr(kex->peer), sshbuf_len(kex->peer),
                    115:            sshbuf_ptr(kex->my), sshbuf_len(kex->my),
1.1       markus    116:            server_host_key_blob, sbloblen,
                    117:            client_pubkey,
                    118:            server_pubkey,
1.6       markus    119:            sshbuf_ptr(shared_secret), sshbuf_len(shared_secret),
                    120:            hash, &hashlen)) < 0)
                    121:                goto out;
1.1       markus    122:
                    123:        /* save session id := H */
                    124:        if (kex->session_id == NULL) {
                    125:                kex->session_id_len = hashlen;
1.6       markus    126:                kex->session_id = malloc(kex->session_id_len);
                    127:                if (kex->session_id == NULL) {
                    128:                        r = SSH_ERR_ALLOC_FAIL;
                    129:                        goto out;
                    130:                }
1.1       markus    131:                memcpy(kex->session_id, hash, kex->session_id_len);
                    132:        }
                    133:
                    134:        /* sign H */
1.10    ! markus    135:        if ((r = kex->sign(server_host_private, server_host_public, &signature,
        !           136:             &slen, hash, hashlen, kex->hostkey_alg, ssh->compat)) < 0)
1.6       markus    137:                goto out;
1.1       markus    138:
                    139:        /* send server hostkey, ECDH pubkey 'Q_S' and signed H */
1.6       markus    140:        if ((r = sshpkt_start(ssh, SSH2_MSG_KEX_ECDH_REPLY)) != 0 ||
                    141:            (r = sshpkt_put_string(ssh, server_host_key_blob, sbloblen)) != 0 ||
                    142:            (r = sshpkt_put_string(ssh, server_pubkey, sizeof(server_pubkey))) != 0 ||
                    143:            (r = sshpkt_put_string(ssh, signature, slen)) != 0 ||
                    144:            (r = sshpkt_send(ssh)) != 0)
                    145:                goto out;
                    146:
                    147:        if ((r = kex_derive_keys(ssh, hash, hashlen, shared_secret)) == 0)
                    148:                r = kex_send_newkeys(ssh);
                    149: out:
                    150:        explicit_bzero(hash, sizeof(hash));
                    151:        explicit_bzero(server_key, sizeof(server_key));
                    152:        free(server_host_key_blob);
1.1       markus    153:        free(signature);
                    154:        free(client_pubkey);
1.6       markus    155:        sshbuf_free(shared_secret);
                    156:        return r;
1.1       markus    157: }