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

Annotation of src/usr.bin/ssh/kexdhs.c, Revision 1.35

1.35    ! djm         1: /* $OpenBSD: kexdhs.c,v 1.34 2019/01/21 10:03:37 djm Exp $ */
1.1       markus      2: /*
                      3:  * Copyright (c) 2001 Markus Friedl.  All rights reserved.
                      4:  *
                      5:  * Redistribution and use in source and binary forms, with or without
                      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:
1.6       stevesk    26:
1.7       deraadt    27: #include <sys/types.h>
1.6       stevesk    28: #include <string.h>
1.7       deraadt    29: #include <signal.h>
1.12      djm        30:
                     31: #include <openssl/dh.h>
1.1       markus     32:
1.20      markus     33: #include "sshkey.h"
1.7       deraadt    34: #include "cipher.h"
1.20      markus     35: #include "digest.h"
1.33      djm        36: #include "dh.h"
1.1       markus     37: #include "kex.h"
                     38: #include "log.h"
                     39: #include "packet.h"
                     40: #include "ssh2.h"
                     41:
1.20      markus     42: #include "dispatch.h"
                     43: #include "compat.h"
                     44: #include "ssherr.h"
                     45: #include "sshbuf.h"
                     46:
1.25      markus     47: static int input_kex_dh_init(int, u_int32_t, struct ssh *);
1.20      markus     48:
                     49: int
                     50: kexdh_server(struct ssh *ssh)
1.1       markus     51: {
1.20      markus     52:        struct kex *kex = ssh->kex;
                     53:        int r;
1.1       markus     54:
                     55:        /* generate server DH public key */
1.33      djm        56:        if ((r = kex_dh_keygen(kex)) != 0)
                     57:                return r;
1.1       markus     58:        debug("expecting SSH2_MSG_KEXDH_INIT");
1.20      markus     59:        ssh_dispatch_set(ssh, SSH2_MSG_KEXDH_INIT, &input_kex_dh_init);
1.33      djm        60:        return 0;
1.20      markus     61: }
                     62:
                     63: int
1.25      markus     64: input_kex_dh_init(int type, u_int32_t seq, struct ssh *ssh)
1.20      markus     65: {
                     66:        struct kex *kex = ssh->kex;
1.34      djm        67:        BIGNUM *dh_client_pub = NULL;
1.28      djm        68:        const BIGNUM *pub_key;
1.20      markus     69:        struct sshkey *server_host_public, *server_host_private;
1.34      djm        70:        struct sshbuf *shared_secret = NULL;
                     71:        u_char *signature = NULL, *server_host_key_blob = NULL;
1.20      markus     72:        u_char hash[SSH_DIGEST_MAX_LENGTH];
                     73:        size_t sbloblen, slen;
1.34      djm        74:        size_t hashlen;
                     75:        int r;
1.1       markus     76:
1.35    ! djm        77:        if ((r = kex_load_hostkey(ssh, &server_host_private,
        !            78:            &server_host_public)) != 0)
1.20      markus     79:                goto out;
1.1       markus     80:
                     81:        /* key, cert */
1.31      djm        82:        if ((r = sshpkt_get_bignum2(ssh, &dh_client_pub)) != 0 ||
                     83:            (r = sshpkt_get_end(ssh)) != 0)
1.20      markus     84:                goto out;
1.34      djm        85:        if ((shared_secret = sshbuf_new()) == NULL) {
1.20      markus     86:                r = SSH_ERR_ALLOC_FAIL;
                     87:                goto out;
                     88:        }
1.34      djm        89:        if ((r = kex_dh_compute_key(kex, dh_client_pub, shared_secret)) != 0)
1.20      markus     90:                goto out;
                     91:        if ((r = sshkey_to_blob(server_host_public, &server_host_key_blob,
                     92:            &sbloblen)) != 0)
                     93:                goto out;
1.1       markus     94:        /* calc H */
1.34      djm        95:        DH_get0_key(kex->dh, &pub_key, NULL);
1.20      markus     96:        hashlen = sizeof(hash);
                     97:        if ((r = kex_dh_hash(
1.24      djm        98:            kex->hash_alg,
1.29      djm        99:            kex->client_version,
                    100:            kex->server_version,
1.20      markus    101:            sshbuf_ptr(kex->peer), sshbuf_len(kex->peer),
                    102:            sshbuf_ptr(kex->my), sshbuf_len(kex->my),
1.1       markus    103:            server_host_key_blob, sbloblen,
                    104:            dh_client_pub,
1.28      djm       105:            pub_key,
1.34      djm       106:            sshbuf_ptr(shared_secret), sshbuf_len(shared_secret),
1.20      markus    107:            hash, &hashlen)) != 0)
                    108:                goto out;
1.1       markus    109:
                    110:        /* sign H */
1.30      djm       111:        if ((r = kex->sign(ssh, server_host_private, server_host_public,
                    112:            &signature, &slen, hash, hashlen, kex->hostkey_alg)) < 0)
1.20      markus    113:                goto out;
1.1       markus    114:
1.27      djm       115:        /* send server hostkey, DH pubkey 'f' and signed H */
1.20      markus    116:        if ((r = sshpkt_start(ssh, SSH2_MSG_KEXDH_REPLY)) != 0 ||
                    117:            (r = sshpkt_put_string(ssh, server_host_key_blob, sbloblen)) != 0 ||
1.28      djm       118:            (r = sshpkt_put_bignum2(ssh, pub_key)) != 0 ||      /* f */
1.20      markus    119:            (r = sshpkt_put_string(ssh, signature, slen)) != 0 ||
                    120:            (r = sshpkt_send(ssh)) != 0)
                    121:                goto out;
                    122:
1.34      djm       123:        if ((r = kex_derive_keys(ssh, hash, hashlen, shared_secret)) == 0)
1.20      markus    124:                r = kex_send_newkeys(ssh);
                    125:  out:
                    126:        explicit_bzero(hash, sizeof(hash));
                    127:        DH_free(kex->dh);
                    128:        kex->dh = NULL;
1.26      jsing     129:        BN_clear_free(dh_client_pub);
1.34      djm       130:        sshbuf_free(shared_secret);
1.20      markus    131:        free(server_host_key_blob);
1.13      djm       132:        free(signature);
1.20      markus    133:        return r;
1.1       markus    134: }