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

Annotation of src/usr.bin/ssh/kexkems.c, Revision 1.2

1.2     ! djm         1: /* $OpenBSD: kexkems.c,v 1.1 2019/01/21 10:20:12 djm Exp $ */
1.1       djm         2: /*
                      3:  * Copyright (c) 2019 Markus Friedl.  All rights reserved.
                      4:  *
                      5:  * modification, are permitted provided that the following conditions
                      6:  * are met:
                      7:  * 1. Redistributions of source code must retain the above copyright
                      8:  *    notice, this list of conditions and the following disclaimer.
                      9:  * 2. Redistributions in binary form must reproduce the above copyright
                     10:  *    notice, this list of conditions and the following disclaimer in the
                     11:  *    documentation and/or other materials provided with the distribution.
                     12:  *
                     13:  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
                     14:  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
                     15:  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
                     16:  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
                     17:  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
                     18:  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
                     19:  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
                     20:  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
                     21:  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
                     22:  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
                     23:  */
                     24:
                     25: #include <sys/types.h>
                     26: #include <stdio.h>
                     27: #include <string.h>
                     28: #include <signal.h>
                     29:
                     30: #include "sshkey.h"
                     31: #include "digest.h"
                     32: #include "kex.h"
                     33: #include "log.h"
                     34: #include "packet.h"
                     35: #include "ssh2.h"
                     36: #include "sshbuf.h"
                     37: #include "ssherr.h"
                     38:
                     39: static int input_kex_kem_init(int, u_int32_t, struct ssh *);
                     40:
                     41: int
                     42: kex_kem_server(struct ssh *ssh)
                     43: {
                     44:        debug("expecting SSH2_MSG_KEX_ECDH_INIT");
                     45:        ssh_dispatch_set(ssh, SSH2_MSG_KEX_ECDH_INIT, &input_kex_kem_init);
                     46:        return 0;
                     47: }
                     48:
                     49: static int
                     50: input_kex_kem_init(int type, u_int32_t seq, struct ssh *ssh)
                     51: {
                     52:        struct kex *kex = ssh->kex;
                     53:        struct sshkey *server_host_private, *server_host_public;
                     54:        struct sshbuf *shared_secret = NULL;
                     55:        struct sshbuf *server_pubkey = NULL;
                     56:        u_char *server_host_key_blob = NULL, *signature = NULL;
                     57:        u_char *client_pubkey = NULL;
                     58:        u_char hash[SSH_DIGEST_MAX_LENGTH];
                     59:        size_t slen, pklen, sbloblen, hashlen;
                     60:        int r;
                     61:
                     62:        if ((r = kex_load_hostkey(ssh, &server_host_private,
                     63:            &server_host_public)) != 0)
                     64:                goto out;
                     65:
                     66:        if ((r = sshpkt_get_string(ssh, &client_pubkey, &pklen)) != 0 ||
                     67:            (r = sshpkt_get_end(ssh)) != 0)
                     68:                goto out;
                     69:
                     70:        /* compute shared secret */
1.2     ! djm        71:        switch (kex->kex_type) {
        !            72:        case KEX_C25519_SHA256:
        !            73:                r = kex_c25519_enc(kex, client_pubkey, pklen, &server_pubkey,
        !            74:                    &shared_secret);
        !            75:                break;
        !            76:        case KEX_KEM_SNTRUP4591761X25519_SHA512:
        !            77:                r = kex_kem_sntrup4591761x25519_enc(kex, client_pubkey, pklen,
        !            78:                    &server_pubkey, &shared_secret);
        !            79:                break;
        !            80:        default:
        !            81:                r = SSH_ERR_INVALID_ARGUMENT;
        !            82:                break;
        !            83:        }
        !            84:        if (r !=0 )
1.1       djm        85:                goto out;
                     86:
                     87:        /* calc H */
                     88:        if ((r = sshkey_to_blob(server_host_public, &server_host_key_blob,
                     89:            &sbloblen)) != 0)
                     90:                goto out;
                     91:        hashlen = sizeof(hash);
                     92:        if ((r = kex_c25519_hash(
                     93:            kex->hash_alg,
                     94:            kex->client_version,
                     95:            kex->server_version,
                     96:            sshbuf_ptr(kex->peer), sshbuf_len(kex->peer),
                     97:            sshbuf_ptr(kex->my), sshbuf_len(kex->my),
                     98:            server_host_key_blob, sbloblen,
                     99:            client_pubkey, pklen,
                    100:            sshbuf_ptr(server_pubkey), sshbuf_len(server_pubkey),
                    101:            sshbuf_ptr(shared_secret), sshbuf_len(shared_secret),
                    102:            hash, &hashlen)) != 0)
                    103:                goto out;
                    104:
                    105:        /* sign H */
                    106:        if ((r = kex->sign(ssh, server_host_private, server_host_public,
                    107:            &signature, &slen, hash, hashlen, kex->hostkey_alg)) != 0)
                    108:                goto out;
                    109:
                    110:        /* send server hostkey, ECDH pubkey 'Q_S' and signed H */
                    111:        if ((r = sshpkt_start(ssh, SSH2_MSG_KEX_ECDH_REPLY)) != 0 ||
                    112:            (r = sshpkt_put_string(ssh, server_host_key_blob, sbloblen)) != 0 ||
                    113:            (r = sshpkt_put_stringb(ssh, server_pubkey)) != 0 ||
                    114:            (r = sshpkt_put_string(ssh, signature, slen)) != 0 ||
                    115:            (r = sshpkt_send(ssh)) != 0)
                    116:                goto out;
                    117:
                    118:        if ((r = kex_derive_keys(ssh, hash, hashlen, shared_secret)) == 0)
                    119:                r = kex_send_newkeys(ssh);
                    120: out:
                    121:        explicit_bzero(hash, sizeof(hash));
                    122:        free(server_host_key_blob);
                    123:        free(signature);
                    124:        free(client_pubkey);
                    125:        sshbuf_free(shared_secret);
                    126:        sshbuf_free(server_pubkey);
                    127:        return r;
                    128: }