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

1.2     ! markus      1: /* $OpenBSD: myproposal.h,v 1.33 2013/11/02 21:59:15 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:
                     34: #include "xmalloc.h"
                     35: #include "buffer.h"
                     36: #include "key.h"
                     37: #include "cipher.h"
                     38: #include "kex.h"
                     39: #include "log.h"
                     40: #include "packet.h"
                     41: #include "ssh2.h"
                     42:
                     43: void
                     44: kexc25519_client(Kex *kex)
                     45: {
                     46:        BIGNUM *shared_secret;
                     47:        Key *server_host_key;
                     48:        u_char client_key[CURVE25519_SIZE];
                     49:        u_char client_pubkey[CURVE25519_SIZE];
                     50:        u_char *server_pubkey = NULL;
                     51:        u_char *server_host_key_blob = NULL, *signature = NULL;
                     52:        u_char *hash;
                     53:        u_int slen, sbloblen, hashlen;
                     54:
                     55:        kexc25519_keygen(client_key, client_pubkey);
                     56:
                     57:        packet_start(SSH2_MSG_KEX_ECDH_INIT);
                     58:        packet_put_string(client_pubkey, sizeof(client_pubkey));
                     59:        packet_send();
                     60:        debug("sending SSH2_MSG_KEX_ECDH_INIT");
                     61:
                     62: #ifdef DEBUG_KEXECDH
                     63:        dump_digest("client private key:", client_key, sizeof(client_key));
                     64: #endif
                     65:
                     66:        debug("expecting SSH2_MSG_KEX_ECDH_REPLY");
                     67:        packet_read_expect(SSH2_MSG_KEX_ECDH_REPLY);
                     68:
                     69:        /* hostkey */
                     70:        server_host_key_blob = packet_get_string(&sbloblen);
                     71:        server_host_key = key_from_blob(server_host_key_blob, sbloblen);
                     72:        if (server_host_key == NULL)
                     73:                fatal("cannot decode server_host_key_blob");
                     74:        if (server_host_key->type != kex->hostkey_type)
                     75:                fatal("type mismatch for decoded server_host_key_blob");
                     76:        if (kex->verify_host_key == NULL)
                     77:                fatal("cannot verify server_host_key");
                     78:        if (kex->verify_host_key(server_host_key) == -1)
                     79:                fatal("server_host_key verification failed");
                     80:
                     81:        /* Q_S, server public key */
                     82:        server_pubkey = packet_get_string(&slen);
                     83:        if (slen != CURVE25519_SIZE)
                     84:                fatal("Incorrect size for server Curve25519 pubkey: %d", slen);
                     85:
                     86: #ifdef DEBUG_KEXECDH
                     87:        dump_digest("server public key:", server_pubkey, CURVE25519_SIZE);
                     88: #endif
                     89:
                     90:        /* signed H */
                     91:        signature = packet_get_string(&slen);
                     92:        packet_check_eom();
                     93:
                     94:        shared_secret = kexc25519_shared_key(client_key, server_pubkey);
                     95:
                     96:        /* calc and verify H */
                     97:        kex_c25519_hash(
                     98:            kex->evp_md,
                     99:            kex->client_version_string,
                    100:            kex->server_version_string,
                    101:            buffer_ptr(&kex->my), buffer_len(&kex->my),
                    102:            buffer_ptr(&kex->peer), buffer_len(&kex->peer),
                    103:            server_host_key_blob, sbloblen,
                    104:            client_pubkey,
                    105:            server_pubkey,
                    106:            shared_secret,
                    107:            &hash, &hashlen
                    108:        );
                    109:        free(server_host_key_blob);
                    110:        free(server_pubkey);
                    111:        if (key_verify(server_host_key, signature, slen, hash, hashlen) != 1)
                    112:                fatal("key_verify failed for server_host_key");
                    113:        key_free(server_host_key);
                    114:        free(signature);
                    115:
                    116:        /* save session id */
                    117:        if (kex->session_id == NULL) {
                    118:                kex->session_id_len = hashlen;
                    119:                kex->session_id = xmalloc(kex->session_id_len);
                    120:                memcpy(kex->session_id, hash, kex->session_id_len);
                    121:        }
                    122:
                    123:        kex_derive_keys(kex, hash, hashlen, shared_secret);
                    124:        BN_clear_free(shared_secret);
                    125:        kex_finish(kex);
                    126: }