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

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