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

1.20    ! markus      1: /* $OpenBSD: kexdhs.c,v 1.19 2015/01/19 19:52:16 markus 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:        }
        !            99:        if ((server_host_public = kex->load_host_public_key(kex->hostkey_type,
        !           100:            ssh)) == NULL ||
        !           101:            (server_host_private = kex->load_host_private_key(kex->hostkey_type,
        !           102:            ssh)) == NULL) {
        !           103:                r = SSH_ERR_NO_HOSTKEY_LOADED;
        !           104:                goto out;
        !           105:        }
1.1       markus    106:
                    107:        /* key, cert */
1.20    ! markus    108:        if ((dh_client_pub = BN_new()) == NULL) {
        !           109:                r = SSH_ERR_ALLOC_FAIL;
        !           110:                goto out;
        !           111:        }
        !           112:        if ((r = sshpkt_get_bignum2(ssh, dh_client_pub)) != 0 ||
        !           113:            (r = sshpkt_get_end(ssh)) != 0)
        !           114:                goto out;
1.1       markus    115:
                    116: #ifdef DEBUG_KEXDH
                    117:        fprintf(stderr, "dh_client_pub= ");
                    118:        BN_print_fp(stderr, dh_client_pub);
                    119:        fprintf(stderr, "\n");
                    120:        debug("bits %d", BN_num_bits(dh_client_pub));
                    121: #endif
                    122:
                    123: #ifdef DEBUG_KEXDH
1.20    ! markus    124:        DHparams_print_fp(stderr, kex->dh);
1.1       markus    125:        fprintf(stderr, "pub= ");
1.20    ! markus    126:        BN_print_fp(stderr, kex->dh->pub_key);
1.1       markus    127:        fprintf(stderr, "\n");
                    128: #endif
1.20    ! markus    129:        if (!dh_pub_is_valid(kex->dh, dh_client_pub)) {
        !           130:                sshpkt_disconnect(ssh, "bad client public DH value");
        !           131:                r = SSH_ERR_MESSAGE_INCOMPLETE;
        !           132:                goto out;
        !           133:        }
1.1       markus    134:
1.20    ! markus    135:        klen = DH_size(kex->dh);
        !           136:        if ((kbuf = malloc(klen)) == NULL ||
        !           137:            (shared_secret = BN_new()) == NULL) {
        !           138:                r = SSH_ERR_ALLOC_FAIL;
        !           139:                goto out;
        !           140:        }
        !           141:        if ((kout = DH_compute_key(kbuf, dh_client_pub, kex->dh)) < 0 ||
        !           142:            BN_bin2bn(kbuf, kout, shared_secret) == NULL) {
        !           143:                r = SSH_ERR_LIBCRYPTO_ERROR;
        !           144:                goto out;
        !           145:        }
1.1       markus    146: #ifdef DEBUG_KEXDH
                    147:        dump_digest("shared secret", kbuf, kout);
                    148: #endif
1.20    ! markus    149:        if ((r = sshkey_to_blob(server_host_public, &server_host_key_blob,
        !           150:            &sbloblen)) != 0)
        !           151:                goto out;
1.1       markus    152:        /* calc H */
1.20    ! markus    153:        hashlen = sizeof(hash);
        !           154:        if ((r = kex_dh_hash(
1.1       markus    155:            kex->client_version_string,
                    156:            kex->server_version_string,
1.20    ! markus    157:            sshbuf_ptr(kex->peer), sshbuf_len(kex->peer),
        !           158:            sshbuf_ptr(kex->my), sshbuf_len(kex->my),
1.1       markus    159:            server_host_key_blob, sbloblen,
                    160:            dh_client_pub,
1.20    ! markus    161:            kex->dh->pub_key,
1.3       djm       162:            shared_secret,
1.20    ! markus    163:            hash, &hashlen)) != 0)
        !           164:                goto out;
1.1       markus    165:
                    166:        /* save session id := H */
                    167:        if (kex->session_id == NULL) {
1.3       djm       168:                kex->session_id_len = hashlen;
1.20    ! markus    169:                kex->session_id = malloc(kex->session_id_len);
        !           170:                if (kex->session_id == NULL) {
        !           171:                        r = SSH_ERR_ALLOC_FAIL;
        !           172:                        goto out;
        !           173:                }
1.1       markus    174:                memcpy(kex->session_id, hash, kex->session_id_len);
                    175:        }
                    176:
                    177:        /* sign H */
1.20    ! markus    178:        if ((r = kex->sign(server_host_private, server_host_public,
        !           179:            &signature, &slen, hash, hashlen, ssh->compat)) < 0)
        !           180:                goto out;
1.1       markus    181:
                    182:        /* destroy_sensitive_data(); */
                    183:
                    184:        /* send server hostkey, DH pubkey 'f' and singed H */
1.20    ! markus    185:        if ((r = sshpkt_start(ssh, SSH2_MSG_KEXDH_REPLY)) != 0 ||
        !           186:            (r = sshpkt_put_string(ssh, server_host_key_blob, sbloblen)) != 0 ||
        !           187:            (r = sshpkt_put_bignum2(ssh, kex->dh->pub_key)) != 0 ||     /* f */
        !           188:            (r = sshpkt_put_string(ssh, signature, slen)) != 0 ||
        !           189:            (r = sshpkt_send(ssh)) != 0)
        !           190:                goto out;
        !           191:
        !           192:        if ((r = kex_derive_keys_bn(ssh, hash, hashlen, shared_secret)) == 0)
        !           193:                r = kex_send_newkeys(ssh);
        !           194:  out:
        !           195:        explicit_bzero(hash, sizeof(hash));
        !           196:        DH_free(kex->dh);
        !           197:        kex->dh = NULL;
        !           198:        if (dh_client_pub)
        !           199:                BN_clear_free(dh_client_pub);
        !           200:        if (kbuf) {
        !           201:                explicit_bzero(kbuf, klen);
        !           202:                free(kbuf);
        !           203:        }
        !           204:        if (shared_secret)
        !           205:                BN_clear_free(shared_secret);
        !           206:        free(server_host_key_blob);
1.13      djm       207:        free(signature);
1.20    ! markus    208:        return r;
1.1       markus    209: }