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

1.4     ! djm         1: /* $OpenBSD: kexkems.c,v 1.3 2019/01/21 10:28:02 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) {
1.3       djm        72:        case KEX_DH_GRP1_SHA1:
                     73:        case KEX_DH_GRP14_SHA1:
                     74:        case KEX_DH_GRP14_SHA256:
                     75:        case KEX_DH_GRP16_SHA512:
                     76:        case KEX_DH_GRP18_SHA512:
                     77:                r = kex_dh_enc(kex, client_pubkey, pklen, &server_pubkey,
1.4     ! djm        78:                    &shared_secret);
        !            79:                break;
        !            80:        case KEX_ECDH_SHA2:
        !            81:                r = kex_ecdh_enc(kex, client_pubkey, pklen, &server_pubkey,
1.3       djm        82:                    &shared_secret);
                     83:                break;
1.2       djm        84:        case KEX_C25519_SHA256:
                     85:                r = kex_c25519_enc(kex, client_pubkey, pklen, &server_pubkey,
                     86:                    &shared_secret);
                     87:                break;
                     88:        case KEX_KEM_SNTRUP4591761X25519_SHA512:
                     89:                r = kex_kem_sntrup4591761x25519_enc(kex, client_pubkey, pklen,
                     90:                    &server_pubkey, &shared_secret);
                     91:                break;
                     92:        default:
                     93:                r = SSH_ERR_INVALID_ARGUMENT;
                     94:                break;
                     95:        }
                     96:        if (r !=0 )
1.1       djm        97:                goto out;
                     98:
                     99:        /* calc H */
                    100:        if ((r = sshkey_to_blob(server_host_public, &server_host_key_blob,
                    101:            &sbloblen)) != 0)
                    102:                goto out;
                    103:        hashlen = sizeof(hash);
                    104:        if ((r = kex_c25519_hash(
                    105:            kex->hash_alg,
                    106:            kex->client_version,
                    107:            kex->server_version,
                    108:            sshbuf_ptr(kex->peer), sshbuf_len(kex->peer),
                    109:            sshbuf_ptr(kex->my), sshbuf_len(kex->my),
                    110:            server_host_key_blob, sbloblen,
                    111:            client_pubkey, pklen,
                    112:            sshbuf_ptr(server_pubkey), sshbuf_len(server_pubkey),
                    113:            sshbuf_ptr(shared_secret), sshbuf_len(shared_secret),
                    114:            hash, &hashlen)) != 0)
                    115:                goto out;
                    116:
                    117:        /* sign H */
                    118:        if ((r = kex->sign(ssh, server_host_private, server_host_public,
                    119:            &signature, &slen, hash, hashlen, kex->hostkey_alg)) != 0)
                    120:                goto out;
                    121:
                    122:        /* send server hostkey, ECDH pubkey 'Q_S' and signed H */
                    123:        if ((r = sshpkt_start(ssh, SSH2_MSG_KEX_ECDH_REPLY)) != 0 ||
                    124:            (r = sshpkt_put_string(ssh, server_host_key_blob, sbloblen)) != 0 ||
                    125:            (r = sshpkt_put_stringb(ssh, server_pubkey)) != 0 ||
                    126:            (r = sshpkt_put_string(ssh, signature, slen)) != 0 ||
                    127:            (r = sshpkt_send(ssh)) != 0)
                    128:                goto out;
                    129:
                    130:        if ((r = kex_derive_keys(ssh, hash, hashlen, shared_secret)) == 0)
                    131:                r = kex_send_newkeys(ssh);
                    132: out:
                    133:        explicit_bzero(hash, sizeof(hash));
                    134:        free(server_host_key_blob);
                    135:        free(signature);
                    136:        free(client_pubkey);
                    137:        sshbuf_free(shared_secret);
                    138:        sshbuf_free(server_pubkey);
                    139:        return r;
                    140: }