1  | 
     | 
     | 
    /* $OpenBSD: kexdhc.c,v 1.20 2017/05/30 14:23:52 markus Exp $ */  | 
    
    
    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  | 
     | 
     | 
     | 
    
    
    26  | 
     | 
     | 
    #include <sys/types.h>  | 
    
    
    27  | 
     | 
     | 
     | 
    
    
    28  | 
     | 
     | 
    #include <openssl/dh.h>  | 
    
    
    29  | 
     | 
     | 
     | 
    
    
    30  | 
     | 
     | 
    #include <stdio.h>  | 
    
    
    31  | 
     | 
     | 
    #include <string.h>  | 
    
    
    32  | 
     | 
     | 
    #include <signal.h>  | 
    
    
    33  | 
     | 
     | 
     | 
    
    
    34  | 
     | 
     | 
    #include "sshkey.h"  | 
    
    
    35  | 
     | 
     | 
    #include "cipher.h"  | 
    
    
    36  | 
     | 
     | 
    #include "digest.h"  | 
    
    
    37  | 
     | 
     | 
    #include "kex.h"  | 
    
    
    38  | 
     | 
     | 
    #include "log.h"  | 
    
    
    39  | 
     | 
     | 
    #include "packet.h"  | 
    
    
    40  | 
     | 
     | 
    #include "dh.h"  | 
    
    
    41  | 
     | 
     | 
    #include "ssh2.h"  | 
    
    
    42  | 
     | 
     | 
    #include "dispatch.h"  | 
    
    
    43  | 
     | 
     | 
    #include "compat.h"  | 
    
    
    44  | 
     | 
     | 
    #include "ssherr.h"  | 
    
    
    45  | 
     | 
     | 
    #include "sshbuf.h"  | 
    
    
    46  | 
     | 
     | 
     | 
    
    
    47  | 
     | 
     | 
    static int input_kex_dh(int, u_int32_t, struct ssh *);  | 
    
    
    48  | 
     | 
     | 
     | 
    
    
    49  | 
     | 
     | 
    int  | 
    
    
    50  | 
     | 
     | 
    kexdh_client(struct ssh *ssh)  | 
    
    
    51  | 
     | 
     | 
    { | 
    
    
    52  | 
     | 
     | 
    	struct kex *kex = ssh->kex;  | 
    
    
    53  | 
     | 
     | 
    	int r;  | 
    
    
    54  | 
     | 
     | 
     | 
    
    
    55  | 
     | 
     | 
    	/* generate and send 'e', client DH public key */  | 
    
    
    56  | 
     | 
     | 
    	switch (kex->kex_type) { | 
    
    
    57  | 
     | 
     | 
    	case KEX_DH_GRP1_SHA1:  | 
    
    
    58  | 
     | 
     | 
    		kex->dh = dh_new_group1();  | 
    
    
    59  | 
     | 
     | 
    		break;  | 
    
    
    60  | 
     | 
     | 
    	case KEX_DH_GRP14_SHA1:  | 
    
    
    61  | 
     | 
     | 
    	case KEX_DH_GRP14_SHA256:  | 
    
    
    62  | 
     | 
     | 
    		kex->dh = dh_new_group14();  | 
    
    
    63  | 
     | 
     | 
    		break;  | 
    
    
    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;  | 
    
    
    70  | 
     | 
     | 
    	default:  | 
    
    
    71  | 
     | 
     | 
    		r = SSH_ERR_INVALID_ARGUMENT;  | 
    
    
    72  | 
     | 
     | 
    		goto out;  | 
    
    
    73  | 
     | 
     | 
    	}  | 
    
    
    74  | 
     | 
     | 
    	if (kex->dh == NULL) { | 
    
    
    75  | 
     | 
     | 
    		r = SSH_ERR_ALLOC_FAIL;  | 
    
    
    76  | 
     | 
     | 
    		goto out;  | 
    
    
    77  | 
     | 
     | 
    	}  | 
    
    
    78  | 
     | 
     | 
    	debug("sending SSH2_MSG_KEXDH_INIT"); | 
    
    
    79  | 
     | 
     | 
    	if ((r = dh_gen_key(kex->dh, kex->we_need * 8)) != 0 ||  | 
    
    
    80  | 
     | 
     | 
    	    (r = sshpkt_start(ssh, SSH2_MSG_KEXDH_INIT)) != 0 ||  | 
    
    
    81  | 
     | 
     | 
    	    (r = sshpkt_put_bignum2(ssh, kex->dh->pub_key)) != 0 ||  | 
    
    
    82  | 
     | 
     | 
    	    (r = sshpkt_send(ssh)) != 0)  | 
    
    
    83  | 
     | 
     | 
    		goto out;  | 
    
    
    84  | 
     | 
     | 
    #ifdef DEBUG_KEXDH  | 
    
    
    85  | 
     | 
     | 
    	DHparams_print_fp(stderr, kex->dh);  | 
    
    
    86  | 
     | 
     | 
    	fprintf(stderr, "pub= ");  | 
    
    
    87  | 
     | 
     | 
    	BN_print_fp(stderr, kex->dh->pub_key);  | 
    
    
    88  | 
     | 
     | 
    	fprintf(stderr, "\n");  | 
    
    
    89  | 
     | 
     | 
    #endif  | 
    
    
    90  | 
     | 
     | 
    	debug("expecting SSH2_MSG_KEXDH_REPLY"); | 
    
    
    91  | 
     | 
     | 
    	ssh_dispatch_set(ssh, SSH2_MSG_KEXDH_REPLY, &input_kex_dh);  | 
    
    
    92  | 
     | 
     | 
    	r = 0;  | 
    
    
    93  | 
     | 
     | 
     out:  | 
    
    
    94  | 
     | 
     | 
    	return r;  | 
    
    
    95  | 
     | 
     | 
    }  | 
    
    
    96  | 
     | 
     | 
     | 
    
    
    97  | 
     | 
     | 
    static int  | 
    
    
    98  | 
     | 
     | 
    input_kex_dh(int type, u_int32_t seq, struct ssh *ssh)  | 
    
    
    99  | 
     | 
     | 
    { | 
    
    
    100  | 
     | 
     | 
    	struct kex *kex = ssh->kex;  | 
    
    
    101  | 
     | 
     | 
    	BIGNUM *dh_server_pub = NULL, *shared_secret = NULL;  | 
    
    
    102  | 
     | 
     | 
    	struct sshkey *server_host_key = NULL;  | 
    
    
    103  | 
     | 
     | 
    	u_char *kbuf = NULL, *server_host_key_blob = NULL, *signature = NULL;  | 
    
    
    104  | 
     | 
     | 
    	u_char hash[SSH_DIGEST_MAX_LENGTH];  | 
    
    
    105  | 
     | 
     | 
    	size_t klen = 0, slen, sbloblen, hashlen;  | 
    
    
    106  | 
     | 
     | 
    	int kout, r;  | 
    
    
    107  | 
     | 
     | 
     | 
    
    
    108  | 
     | 
     | 
    	if (kex->verify_host_key == NULL) { | 
    
    
    109  | 
     | 
     | 
    		r = SSH_ERR_INVALID_ARGUMENT;  | 
    
    
    110  | 
     | 
     | 
    		goto out;  | 
    
    
    111  | 
     | 
     | 
    	}  | 
    
    
    112  | 
     | 
     | 
    	/* key, cert */  | 
    
    
    113  | 
     | 
     | 
    	if ((r = sshpkt_get_string(ssh, &server_host_key_blob,  | 
    
    
    114  | 
     | 
     | 
    	    &sbloblen)) != 0 ||  | 
    
    
    115  | 
     | 
     | 
    	    (r = sshkey_from_blob(server_host_key_blob, sbloblen,  | 
    
    
    116  | 
     | 
     | 
    	    &server_host_key)) != 0)  | 
    
    
    117  | 
     | 
     | 
    		goto out;  | 
    
    
    118  | 
     | 
     | 
    	if (server_host_key->type != kex->hostkey_type ||  | 
    
    
    119  | 
     | 
     | 
    	    (kex->hostkey_type == KEY_ECDSA &&  | 
    
    
    120  | 
     | 
     | 
    	    server_host_key->ecdsa_nid != kex->hostkey_nid)) { | 
    
    
    121  | 
     | 
     | 
    		r = SSH_ERR_KEY_TYPE_MISMATCH;  | 
    
    
    122  | 
     | 
     | 
    		goto out;  | 
    
    
    123  | 
     | 
     | 
    	}  | 
    
    
    124  | 
     | 
     | 
    	if (kex->verify_host_key(server_host_key, ssh) == -1) { | 
    
    
    125  | 
     | 
     | 
    		r = SSH_ERR_SIGNATURE_INVALID;  | 
    
    
    126  | 
     | 
     | 
    		goto out;  | 
    
    
    127  | 
     | 
     | 
    	}  | 
    
    
    128  | 
     | 
     | 
    	/* DH parameter f, server public DH key */  | 
    
    
    129  | 
     | 
     | 
    	if ((dh_server_pub = BN_new()) == NULL) { | 
    
    
    130  | 
     | 
     | 
    		r = SSH_ERR_ALLOC_FAIL;  | 
    
    
    131  | 
     | 
     | 
    		goto out;  | 
    
    
    132  | 
     | 
     | 
    	}  | 
    
    
    133  | 
     | 
     | 
    	/* signed H */  | 
    
    
    134  | 
     | 
     | 
    	if ((r = sshpkt_get_bignum2(ssh, dh_server_pub)) != 0 ||  | 
    
    
    135  | 
     | 
     | 
    	    (r = sshpkt_get_string(ssh, &signature, &slen)) != 0 ||  | 
    
    
    136  | 
     | 
     | 
    	    (r = sshpkt_get_end(ssh)) != 0)  | 
    
    
    137  | 
     | 
     | 
    		goto out;  | 
    
    
    138  | 
     | 
     | 
    #ifdef DEBUG_KEXDH  | 
    
    
    139  | 
     | 
     | 
    	fprintf(stderr, "dh_server_pub= ");  | 
    
    
    140  | 
     | 
     | 
    	BN_print_fp(stderr, dh_server_pub);  | 
    
    
    141  | 
     | 
     | 
    	fprintf(stderr, "\n");  | 
    
    
    142  | 
     | 
     | 
    	debug("bits %d", BN_num_bits(dh_server_pub)); | 
    
    
    143  | 
     | 
     | 
    #endif  | 
    
    
    144  | 
     | 
     | 
    	if (!dh_pub_is_valid(kex->dh, dh_server_pub)) { | 
    
    
    145  | 
     | 
     | 
    		sshpkt_disconnect(ssh, "bad server public DH value");  | 
    
    
    146  | 
     | 
     | 
    		r = SSH_ERR_MESSAGE_INCOMPLETE;  | 
    
    
    147  | 
     | 
     | 
    		goto out;  | 
    
    
    148  | 
     | 
     | 
    	}  | 
    
    
    149  | 
     | 
     | 
     | 
    
    
    150  | 
     | 
     | 
    	klen = DH_size(kex->dh);  | 
    
    
    151  | 
     | 
     | 
    	if ((kbuf = malloc(klen)) == NULL ||  | 
    
    
    152  | 
     | 
     | 
    	    (shared_secret = BN_new()) == NULL) { | 
    
    
    153  | 
     | 
     | 
    		r = SSH_ERR_ALLOC_FAIL;  | 
    
    
    154  | 
     | 
     | 
    		goto out;  | 
    
    
    155  | 
     | 
     | 
    	}  | 
    
    
    156  | 
     | 
     | 
    	if ((kout = DH_compute_key(kbuf, dh_server_pub, kex->dh)) < 0 ||  | 
    
    
    157  | 
     | 
     | 
    	    BN_bin2bn(kbuf, kout, shared_secret) == NULL) { | 
    
    
    158  | 
     | 
     | 
    		r = SSH_ERR_LIBCRYPTO_ERROR;  | 
    
    
    159  | 
     | 
     | 
    		goto out;  | 
    
    
    160  | 
     | 
     | 
    	}  | 
    
    
    161  | 
     | 
     | 
    #ifdef DEBUG_KEXDH  | 
    
    
    162  | 
     | 
     | 
    	dump_digest("shared secret", kbuf, kout); | 
    
    
    163  | 
     | 
     | 
    #endif  | 
    
    
    164  | 
     | 
     | 
     | 
    
    
    165  | 
     | 
     | 
    	/* calc and verify H */  | 
    
    
    166  | 
     | 
     | 
    	hashlen = sizeof(hash);  | 
    
    
    167  | 
     | 
     | 
    	if ((r = kex_dh_hash(  | 
    
    
    168  | 
     | 
     | 
    	    kex->hash_alg,  | 
    
    
    169  | 
     | 
     | 
    	    kex->client_version_string,  | 
    
    
    170  | 
     | 
     | 
    	    kex->server_version_string,  | 
    
    
    171  | 
     | 
     | 
    	    sshbuf_ptr(kex->my), sshbuf_len(kex->my),  | 
    
    
    172  | 
     | 
     | 
    	    sshbuf_ptr(kex->peer), sshbuf_len(kex->peer),  | 
    
    
    173  | 
     | 
     | 
    	    server_host_key_blob, sbloblen,  | 
    
    
    174  | 
     | 
     | 
    	    kex->dh->pub_key,  | 
    
    
    175  | 
     | 
     | 
    	    dh_server_pub,  | 
    
    
    176  | 
     | 
     | 
    	    shared_secret,  | 
    
    
    177  | 
     | 
     | 
    	    hash, &hashlen)) != 0)  | 
    
    
    178  | 
     | 
     | 
    		goto out;  | 
    
    
    179  | 
     | 
     | 
     | 
    
    
    180  | 
     | 
     | 
    	if ((r = sshkey_verify(server_host_key, signature, slen, hash, hashlen,  | 
    
    
    181  | 
     | 
     | 
    	    ssh->compat)) != 0)  | 
    
    
    182  | 
     | 
     | 
    		goto out;  | 
    
    
    183  | 
     | 
     | 
     | 
    
    
    184  | 
     | 
     | 
    	/* save session id */  | 
    
    
    185  | 
     | 
     | 
    	if (kex->session_id == NULL) { | 
    
    
    186  | 
     | 
     | 
    		kex->session_id_len = hashlen;  | 
    
    
    187  | 
     | 
     | 
    		kex->session_id = malloc(kex->session_id_len);  | 
    
    
    188  | 
     | 
     | 
    		if (kex->session_id == NULL) { | 
    
    
    189  | 
     | 
     | 
    			r = SSH_ERR_ALLOC_FAIL;  | 
    
    
    190  | 
     | 
     | 
    			goto out;  | 
    
    
    191  | 
     | 
     | 
    		}  | 
    
    
    192  | 
     | 
     | 
    		memcpy(kex->session_id, hash, kex->session_id_len);  | 
    
    
    193  | 
     | 
     | 
    	}  | 
    
    
    194  | 
     | 
     | 
     | 
    
    
    195  | 
     | 
     | 
    	if ((r = kex_derive_keys_bn(ssh, hash, hashlen, shared_secret)) == 0)  | 
    
    
    196  | 
     | 
     | 
    		r = kex_send_newkeys(ssh);  | 
    
    
    197  | 
     | 
     | 
     out:  | 
    
    
    198  | 
     | 
     | 
    	explicit_bzero(hash, sizeof(hash));  | 
    
    
    199  | 
     | 
     | 
    	DH_free(kex->dh);  | 
    
    
    200  | 
     | 
     | 
    	kex->dh = NULL;  | 
    
    
    201  | 
     | 
     | 
    	if (dh_server_pub)  | 
    
    
    202  | 
     | 
     | 
    		BN_clear_free(dh_server_pub);  | 
    
    
    203  | 
     | 
     | 
    	if (kbuf) { | 
    
    
    204  | 
     | 
     | 
    		explicit_bzero(kbuf, klen);  | 
    
    
    205  | 
     | 
     | 
    		free(kbuf);  | 
    
    
    206  | 
     | 
     | 
    	}  | 
    
    
    207  | 
     | 
     | 
    	if (shared_secret)  | 
    
    
    208  | 
     | 
     | 
    		BN_clear_free(shared_secret);  | 
    
    
    209  | 
     | 
     | 
    	sshkey_free(server_host_key);  | 
    
    
    210  | 
     | 
     | 
    	free(server_host_key_blob);  | 
    
    
    211  | 
     | 
     | 
    	free(signature);  | 
    
    
    212  | 
     | 
     | 
    	return r;  | 
    
    
    213  | 
     | 
     | 
    }  |