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

1.3     ! djm         1: /* $OpenBSD: kexc25519s.c,v 1.2 2013/11/02 22:02:14 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:  * 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: #include <string.h>
                     29: #include <signal.h>
                     30:
                     31: #include "xmalloc.h"
                     32: #include "buffer.h"
                     33: #include "key.h"
                     34: #include "cipher.h"
                     35: #include "kex.h"
                     36: #include "log.h"
                     37: #include "packet.h"
                     38: #include "ssh2.h"
                     39:
                     40: void
                     41: kexc25519_server(Kex *kex)
                     42: {
                     43:        BIGNUM *shared_secret;
                     44:        Key *server_host_private, *server_host_public;
                     45:        u_char *server_host_key_blob = NULL, *signature = NULL;
                     46:        u_char server_key[CURVE25519_SIZE];
                     47:        u_char *client_pubkey = NULL;
                     48:        u_char server_pubkey[CURVE25519_SIZE];
                     49:        u_char *hash;
                     50:        u_int slen, sbloblen, hashlen;
                     51:
                     52:        /* generate private key */
                     53:        kexc25519_keygen(server_key, server_pubkey);
                     54: #ifdef DEBUG_KEXECDH
                     55:        dump_digest("server private key:", server_key, sizeof(server_key));
                     56: #endif
                     57:
                     58:        if (kex->load_host_public_key == NULL ||
                     59:            kex->load_host_private_key == NULL)
                     60:                fatal("Cannot load hostkey");
                     61:        server_host_public = kex->load_host_public_key(kex->hostkey_type);
                     62:        if (server_host_public == NULL)
                     63:                fatal("Unsupported hostkey type %d", kex->hostkey_type);
                     64:        server_host_private = kex->load_host_private_key(kex->hostkey_type);
                     65:
                     66:        debug("expecting SSH2_MSG_KEX_ECDH_INIT");
                     67:        packet_read_expect(SSH2_MSG_KEX_ECDH_INIT);
                     68:        client_pubkey = packet_get_string(&slen);
                     69:        if (slen != CURVE25519_SIZE)
                     70:                fatal("Incorrect size for server Curve25519 pubkey: %d", slen);
                     71:        packet_check_eom();
                     72:
                     73: #ifdef DEBUG_KEXECDH
                     74:        dump_digest("client public key:", client_pubkey, CURVE25519_SIZE);
                     75: #endif
                     76:
                     77:        shared_secret = kexc25519_shared_key(server_key, client_pubkey);
                     78:
                     79:        /* calc H */
                     80:        key_to_blob(server_host_public, &server_host_key_blob, &sbloblen);
                     81:        kex_c25519_hash(
1.3     ! djm        82:            kex->hash_alg,
1.1       markus     83:            kex->client_version_string,
                     84:            kex->server_version_string,
                     85:            buffer_ptr(&kex->peer), buffer_len(&kex->peer),
                     86:            buffer_ptr(&kex->my), buffer_len(&kex->my),
                     87:            server_host_key_blob, sbloblen,
                     88:            client_pubkey,
                     89:            server_pubkey,
                     90:            shared_secret,
                     91:            &hash, &hashlen
                     92:        );
                     93:
                     94:        /* save session id := H */
                     95:        if (kex->session_id == NULL) {
                     96:                kex->session_id_len = hashlen;
                     97:                kex->session_id = xmalloc(kex->session_id_len);
                     98:                memcpy(kex->session_id, hash, kex->session_id_len);
                     99:        }
                    100:
                    101:        /* sign H */
                    102:        kex->sign(server_host_private, server_host_public, &signature, &slen,
                    103:            hash, hashlen);
                    104:
                    105:        /* destroy_sensitive_data(); */
                    106:
                    107:        /* send server hostkey, ECDH pubkey 'Q_S' and signed H */
                    108:        packet_start(SSH2_MSG_KEX_ECDH_REPLY);
                    109:        packet_put_string(server_host_key_blob, sbloblen);
                    110:        packet_put_string(server_pubkey, sizeof(server_pubkey));
                    111:        packet_put_string(signature, slen);
                    112:        packet_send();
                    113:
                    114:        free(signature);
                    115:        free(server_host_key_blob);
                    116:        /* have keys, free server key */
                    117:        free(client_pubkey);
                    118:        kex_derive_keys(kex, hash, hashlen, shared_secret);
                    119:        BN_clear_free(shared_secret);
                    120:        kex_finish(kex);
                    121: }