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

1.30    ! djm         1: /* $OpenBSD: kexdhs.c,v 1.29 2018/12/27 03:25:25 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.1       markus     36: #include "kex.h"
                     37: #include "log.h"
                     38: #include "packet.h"
                     39: #include "dh.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.2       djm        56:        switch (kex->kex_type) {
                     57:        case KEX_DH_GRP1_SHA1:
1.20      markus     58:                kex->dh = dh_new_group1();
1.2       djm        59:                break;
                     60:        case KEX_DH_GRP14_SHA1:
1.24      djm        61:        case KEX_DH_GRP14_SHA256:
1.20      markus     62:                kex->dh = dh_new_group14();
1.2       djm        63:                break;
1.24      djm        64:        case KEX_DH_GRP16_SHA512:
                     65:                kex->dh = dh_new_group16();
                     66:                break;
                     67:        case KEX_DH_GRP18_SHA512:
                     68:                kex->dh = dh_new_group18();
                     69:                break;
1.2       djm        70:        default:
1.20      markus     71:                r = SSH_ERR_INVALID_ARGUMENT;
                     72:                goto out;
1.2       djm        73:        }
1.20      markus     74:        if (kex->dh == NULL) {
                     75:                r = SSH_ERR_ALLOC_FAIL;
                     76:                goto out;
                     77:        }
                     78:        if ((r = dh_gen_key(kex->dh, kex->we_need * 8)) != 0)
                     79:                goto out;
1.1       markus     80:
                     81:        debug("expecting SSH2_MSG_KEXDH_INIT");
1.20      markus     82:        ssh_dispatch_set(ssh, SSH2_MSG_KEXDH_INIT, &input_kex_dh_init);
                     83:        r = 0;
                     84:  out:
                     85:        return r;
                     86: }
                     87:
                     88: int
1.25      markus     89: input_kex_dh_init(int type, u_int32_t seq, struct ssh *ssh)
1.20      markus     90: {
                     91:        struct kex *kex = ssh->kex;
                     92:        BIGNUM *shared_secret = NULL, *dh_client_pub = NULL;
1.28      djm        93:        const BIGNUM *pub_key;
1.20      markus     94:        struct sshkey *server_host_public, *server_host_private;
                     95:        u_char *kbuf = NULL, *signature = NULL, *server_host_key_blob = NULL;
                     96:        u_char hash[SSH_DIGEST_MAX_LENGTH];
                     97:        size_t sbloblen, slen;
                     98:        size_t klen = 0, hashlen;
                     99:        int kout, r;
1.1       markus    100:
1.11      djm       101:        if (kex->load_host_public_key == NULL ||
1.20      markus    102:            kex->load_host_private_key == NULL) {
                    103:                r = SSH_ERR_INVALID_ARGUMENT;
                    104:                goto out;
                    105:        }
1.22      djm       106:        server_host_public = kex->load_host_public_key(kex->hostkey_type,
                    107:            kex->hostkey_nid, ssh);
                    108:        server_host_private = kex->load_host_private_key(kex->hostkey_type,
                    109:            kex->hostkey_nid, ssh);
1.21      djm       110:        if (server_host_public == NULL) {
1.20      markus    111:                r = SSH_ERR_NO_HOSTKEY_LOADED;
                    112:                goto out;
                    113:        }
1.1       markus    114:
                    115:        /* key, cert */
1.20      markus    116:        if ((dh_client_pub = BN_new()) == NULL) {
                    117:                r = SSH_ERR_ALLOC_FAIL;
                    118:                goto out;
                    119:        }
1.28      djm       120:        DH_get0_key(kex->dh, &pub_key, NULL);
1.20      markus    121:        if ((r = sshpkt_get_bignum2(ssh, dh_client_pub)) != 0 ||
                    122:            (r = sshpkt_get_end(ssh)) != 0)
                    123:                goto out;
1.1       markus    124:
                    125: #ifdef DEBUG_KEXDH
                    126:        fprintf(stderr, "dh_client_pub= ");
                    127:        BN_print_fp(stderr, dh_client_pub);
                    128:        fprintf(stderr, "\n");
                    129:        debug("bits %d", BN_num_bits(dh_client_pub));
1.20      markus    130:        DHparams_print_fp(stderr, kex->dh);
1.1       markus    131:        fprintf(stderr, "pub= ");
1.28      djm       132:        BN_print_fp(stderr, pub_key);
1.1       markus    133:        fprintf(stderr, "\n");
                    134: #endif
1.20      markus    135:        if (!dh_pub_is_valid(kex->dh, dh_client_pub)) {
                    136:                sshpkt_disconnect(ssh, "bad client public DH value");
                    137:                r = SSH_ERR_MESSAGE_INCOMPLETE;
                    138:                goto out;
                    139:        }
1.1       markus    140:
1.20      markus    141:        klen = DH_size(kex->dh);
                    142:        if ((kbuf = malloc(klen)) == NULL ||
                    143:            (shared_secret = BN_new()) == NULL) {
                    144:                r = SSH_ERR_ALLOC_FAIL;
                    145:                goto out;
                    146:        }
                    147:        if ((kout = DH_compute_key(kbuf, dh_client_pub, kex->dh)) < 0 ||
                    148:            BN_bin2bn(kbuf, kout, shared_secret) == NULL) {
                    149:                r = SSH_ERR_LIBCRYPTO_ERROR;
                    150:                goto out;
                    151:        }
1.1       markus    152: #ifdef DEBUG_KEXDH
                    153:        dump_digest("shared secret", kbuf, kout);
                    154: #endif
1.20      markus    155:        if ((r = sshkey_to_blob(server_host_public, &server_host_key_blob,
                    156:            &sbloblen)) != 0)
                    157:                goto out;
1.1       markus    158:        /* calc H */
1.20      markus    159:        hashlen = sizeof(hash);
                    160:        if ((r = kex_dh_hash(
1.24      djm       161:            kex->hash_alg,
1.29      djm       162:            kex->client_version,
                    163:            kex->server_version,
1.20      markus    164:            sshbuf_ptr(kex->peer), sshbuf_len(kex->peer),
                    165:            sshbuf_ptr(kex->my), sshbuf_len(kex->my),
1.1       markus    166:            server_host_key_blob, sbloblen,
                    167:            dh_client_pub,
1.28      djm       168:            pub_key,
1.3       djm       169:            shared_secret,
1.20      markus    170:            hash, &hashlen)) != 0)
                    171:                goto out;
1.1       markus    172:
                    173:        /* save session id := H */
                    174:        if (kex->session_id == NULL) {
1.3       djm       175:                kex->session_id_len = hashlen;
1.20      markus    176:                kex->session_id = malloc(kex->session_id_len);
                    177:                if (kex->session_id == NULL) {
                    178:                        r = SSH_ERR_ALLOC_FAIL;
                    179:                        goto out;
                    180:                }
1.1       markus    181:                memcpy(kex->session_id, hash, kex->session_id_len);
                    182:        }
                    183:
                    184:        /* sign H */
1.30    ! djm       185:        if ((r = kex->sign(ssh, server_host_private, server_host_public,
        !           186:            &signature, &slen, hash, hashlen, kex->hostkey_alg)) < 0)
1.20      markus    187:                goto out;
1.1       markus    188:
                    189:        /* destroy_sensitive_data(); */
                    190:
1.27      djm       191:        /* send server hostkey, DH pubkey 'f' and signed H */
1.20      markus    192:        if ((r = sshpkt_start(ssh, SSH2_MSG_KEXDH_REPLY)) != 0 ||
                    193:            (r = sshpkt_put_string(ssh, server_host_key_blob, sbloblen)) != 0 ||
1.28      djm       194:            (r = sshpkt_put_bignum2(ssh, pub_key)) != 0 ||      /* f */
1.20      markus    195:            (r = sshpkt_put_string(ssh, signature, slen)) != 0 ||
                    196:            (r = sshpkt_send(ssh)) != 0)
                    197:                goto out;
                    198:
                    199:        if ((r = kex_derive_keys_bn(ssh, hash, hashlen, shared_secret)) == 0)
                    200:                r = kex_send_newkeys(ssh);
                    201:  out:
                    202:        explicit_bzero(hash, sizeof(hash));
                    203:        DH_free(kex->dh);
                    204:        kex->dh = NULL;
1.26      jsing     205:        BN_clear_free(dh_client_pub);
1.20      markus    206:        if (kbuf) {
                    207:                explicit_bzero(kbuf, klen);
                    208:                free(kbuf);
                    209:        }
1.26      jsing     210:        BN_clear_free(shared_secret);
1.20      markus    211:        free(server_host_key_blob);
1.13      djm       212:        free(signature);
1.20      markus    213:        return r;
1.1       markus    214: }