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

1.1     ! markus      1: /*
        !             2:  * Copyright (c) 2001 Markus Friedl.  All rights reserved.
        !             3:  * Copyright (c) 2010 Damien Miller.  All rights reserved.
        !             4:  * Copyright (c) 2013 Aris Adamantiadis.  All rights reserved.
        !             5:  *
        !             6:  * modification, are permitted provided that the following conditions
        !             7:  * are met:
        !             8:  * 1. Redistributions of source code must retain the above copyright
        !             9:  *    notice, this list of conditions and the following disclaimer.
        !            10:  * 2. Redistributions in binary form must reproduce the above copyright
        !            11:  *    notice, this list of conditions and the following disclaimer in the
        !            12:  *    documentation and/or other materials provided with the distribution.
        !            13:  *
        !            14:  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
        !            15:  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
        !            16:  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
        !            17:  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
        !            18:  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
        !            19:  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
        !            20:  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
        !            21:  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
        !            22:  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
        !            23:  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
        !            24:  */
        !            25:
        !            26: #include <sys/types.h>
        !            27: #include <string.h>
        !            28: #include <signal.h>
        !            29:
        !            30: #include "xmalloc.h"
        !            31: #include "buffer.h"
        !            32: #include "key.h"
        !            33: #include "cipher.h"
        !            34: #include "kex.h"
        !            35: #include "log.h"
        !            36: #include "packet.h"
        !            37: #include "ssh2.h"
        !            38:
        !            39: void
        !            40: kexc25519_server(Kex *kex)
        !            41: {
        !            42:        BIGNUM *shared_secret;
        !            43:        Key *server_host_private, *server_host_public;
        !            44:        u_char *server_host_key_blob = NULL, *signature = NULL;
        !            45:        u_char server_key[CURVE25519_SIZE];
        !            46:        u_char *client_pubkey = NULL;
        !            47:        u_char server_pubkey[CURVE25519_SIZE];
        !            48:        u_char *hash;
        !            49:        u_int slen, sbloblen, hashlen;
        !            50:
        !            51:        /* generate private key */
        !            52:        kexc25519_keygen(server_key, server_pubkey);
        !            53: #ifdef DEBUG_KEXECDH
        !            54:        dump_digest("server private key:", server_key, sizeof(server_key));
        !            55: #endif
        !            56:
        !            57:        if (kex->load_host_public_key == NULL ||
        !            58:            kex->load_host_private_key == NULL)
        !            59:                fatal("Cannot load hostkey");
        !            60:        server_host_public = kex->load_host_public_key(kex->hostkey_type);
        !            61:        if (server_host_public == NULL)
        !            62:                fatal("Unsupported hostkey type %d", kex->hostkey_type);
        !            63:        server_host_private = kex->load_host_private_key(kex->hostkey_type);
        !            64:
        !            65:        debug("expecting SSH2_MSG_KEX_ECDH_INIT");
        !            66:        packet_read_expect(SSH2_MSG_KEX_ECDH_INIT);
        !            67:        client_pubkey = packet_get_string(&slen);
        !            68:        if (slen != CURVE25519_SIZE)
        !            69:                fatal("Incorrect size for server Curve25519 pubkey: %d", slen);
        !            70:        packet_check_eom();
        !            71:
        !            72: #ifdef DEBUG_KEXECDH
        !            73:        dump_digest("client public key:", client_pubkey, CURVE25519_SIZE);
        !            74: #endif
        !            75:
        !            76:        shared_secret = kexc25519_shared_key(server_key, client_pubkey);
        !            77:
        !            78:        /* calc H */
        !            79:        key_to_blob(server_host_public, &server_host_key_blob, &sbloblen);
        !            80:        kex_c25519_hash(
        !            81:            kex->evp_md,
        !            82:            kex->client_version_string,
        !            83:            kex->server_version_string,
        !            84:            buffer_ptr(&kex->peer), buffer_len(&kex->peer),
        !            85:            buffer_ptr(&kex->my), buffer_len(&kex->my),
        !            86:            server_host_key_blob, sbloblen,
        !            87:            client_pubkey,
        !            88:            server_pubkey,
        !            89:            shared_secret,
        !            90:            &hash, &hashlen
        !            91:        );
        !            92:
        !            93:        /* save session id := H */
        !            94:        if (kex->session_id == NULL) {
        !            95:                kex->session_id_len = hashlen;
        !            96:                kex->session_id = xmalloc(kex->session_id_len);
        !            97:                memcpy(kex->session_id, hash, kex->session_id_len);
        !            98:        }
        !            99:
        !           100:        /* sign H */
        !           101:        kex->sign(server_host_private, server_host_public, &signature, &slen,
        !           102:            hash, hashlen);
        !           103:
        !           104:        /* destroy_sensitive_data(); */
        !           105:
        !           106:        /* send server hostkey, ECDH pubkey 'Q_S' and signed H */
        !           107:        packet_start(SSH2_MSG_KEX_ECDH_REPLY);
        !           108:        packet_put_string(server_host_key_blob, sbloblen);
        !           109:        packet_put_string(server_pubkey, sizeof(server_pubkey));
        !           110:        packet_put_string(signature, slen);
        !           111:        packet_send();
        !           112:
        !           113:        free(signature);
        !           114:        free(server_host_key_blob);
        !           115:        /* have keys, free server key */
        !           116:        free(client_pubkey);
        !           117:        kex_derive_keys(kex, hash, hashlen, shared_secret);
        !           118:        BN_clear_free(shared_secret);
        !           119:        kex_finish(kex);
        !           120: }