1*0Sstevel@tonic-gate /*
2*0Sstevel@tonic-gate  * Copyright (c) 2001 Markus Friedl.  All rights reserved.
3*0Sstevel@tonic-gate  *
4*0Sstevel@tonic-gate  * Redistribution and use in source and binary forms, with or without
5*0Sstevel@tonic-gate  * modification, are permitted provided that the following conditions
6*0Sstevel@tonic-gate  * are met:
7*0Sstevel@tonic-gate  * 1. Redistributions of source code must retain the above copyright
8*0Sstevel@tonic-gate  *    notice, this list of conditions and the following disclaimer.
9*0Sstevel@tonic-gate  * 2. Redistributions in binary form must reproduce the above copyright
10*0Sstevel@tonic-gate  *    notice, this list of conditions and the following disclaimer in the
11*0Sstevel@tonic-gate  *    documentation and/or other materials provided with the distribution.
12*0Sstevel@tonic-gate  *
13*0Sstevel@tonic-gate  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
14*0Sstevel@tonic-gate  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
15*0Sstevel@tonic-gate  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
16*0Sstevel@tonic-gate  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
17*0Sstevel@tonic-gate  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
18*0Sstevel@tonic-gate  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
19*0Sstevel@tonic-gate  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
20*0Sstevel@tonic-gate  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
21*0Sstevel@tonic-gate  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
22*0Sstevel@tonic-gate  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
23*0Sstevel@tonic-gate  */
24*0Sstevel@tonic-gate 
25*0Sstevel@tonic-gate #include "includes.h"
26*0Sstevel@tonic-gate RCSID("$OpenBSD: kexdh.c,v 1.18 2002/03/18 17:50:31 provos Exp $");
27*0Sstevel@tonic-gate 
28*0Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
29*0Sstevel@tonic-gate 
30*0Sstevel@tonic-gate #include <openssl/crypto.h>
31*0Sstevel@tonic-gate #include <openssl/bn.h>
32*0Sstevel@tonic-gate 
33*0Sstevel@tonic-gate #include "xmalloc.h"
34*0Sstevel@tonic-gate #include "buffer.h"
35*0Sstevel@tonic-gate #include "bufaux.h"
36*0Sstevel@tonic-gate #include "key.h"
37*0Sstevel@tonic-gate #include "kex.h"
38*0Sstevel@tonic-gate #include "log.h"
39*0Sstevel@tonic-gate #include "packet.h"
40*0Sstevel@tonic-gate #include "dh.h"
41*0Sstevel@tonic-gate #include "ssh2.h"
42*0Sstevel@tonic-gate #include "monitor_wrap.h"
43*0Sstevel@tonic-gate 
44*0Sstevel@tonic-gate void
45*0Sstevel@tonic-gate kexdh_client(Kex *kex)
46*0Sstevel@tonic-gate {
47*0Sstevel@tonic-gate 	BIGNUM *dh_server_pub = NULL, *shared_secret = NULL;
48*0Sstevel@tonic-gate 	DH *dh;
49*0Sstevel@tonic-gate 	Key *server_host_key;
50*0Sstevel@tonic-gate 	u_char *server_host_key_blob = NULL, *signature = NULL;
51*0Sstevel@tonic-gate 	u_char *kbuf, *hash;
52*0Sstevel@tonic-gate 	u_int klen, kout, slen, sbloblen;
53*0Sstevel@tonic-gate 
54*0Sstevel@tonic-gate 	/* generate and send 'e', client DH public key */
55*0Sstevel@tonic-gate 	dh = dh_new_group1();
56*0Sstevel@tonic-gate 	dh_gen_key(dh, kex->we_need * 8);
57*0Sstevel@tonic-gate 	packet_start(SSH2_MSG_KEXDH_INIT);
58*0Sstevel@tonic-gate 	packet_put_bignum2(dh->pub_key);
59*0Sstevel@tonic-gate 	packet_send();
60*0Sstevel@tonic-gate 
61*0Sstevel@tonic-gate 	debug("sending SSH2_MSG_KEXDH_INIT");
62*0Sstevel@tonic-gate #ifdef DEBUG_KEXDH
63*0Sstevel@tonic-gate 	DHparams_print_fp(stderr, dh);
64*0Sstevel@tonic-gate 	fprintf(stderr, "pub= ");
65*0Sstevel@tonic-gate 	BN_print_fp(stderr, dh->pub_key);
66*0Sstevel@tonic-gate 	fprintf(stderr, "\n");
67*0Sstevel@tonic-gate #endif
68*0Sstevel@tonic-gate 
69*0Sstevel@tonic-gate 	debug("expecting SSH2_MSG_KEXDH_REPLY");
70*0Sstevel@tonic-gate 	packet_read_expect(SSH2_MSG_KEXDH_REPLY);
71*0Sstevel@tonic-gate 
72*0Sstevel@tonic-gate 	/* key, cert */
73*0Sstevel@tonic-gate 	server_host_key_blob = packet_get_string(&sbloblen);
74*0Sstevel@tonic-gate 	server_host_key = key_from_blob(server_host_key_blob, sbloblen);
75*0Sstevel@tonic-gate 	if (server_host_key == NULL)
76*0Sstevel@tonic-gate 		fatal("cannot decode server_host_key_blob");
77*0Sstevel@tonic-gate 	if (server_host_key->type != kex->hostkey_type)
78*0Sstevel@tonic-gate 		fatal("type mismatch for decoded server_host_key_blob");
79*0Sstevel@tonic-gate 	if (kex->verify_host_key == NULL)
80*0Sstevel@tonic-gate 		fatal("cannot verify server_host_key");
81*0Sstevel@tonic-gate 	if (kex->verify_host_key(server_host_key) == -1)
82*0Sstevel@tonic-gate 		fatal("server_host_key verification failed");
83*0Sstevel@tonic-gate 
84*0Sstevel@tonic-gate 	/* DH paramter f, server public DH key */
85*0Sstevel@tonic-gate 	if ((dh_server_pub = BN_new()) == NULL)
86*0Sstevel@tonic-gate 		fatal("dh_server_pub == NULL");
87*0Sstevel@tonic-gate 	packet_get_bignum2(dh_server_pub);
88*0Sstevel@tonic-gate 
89*0Sstevel@tonic-gate #ifdef DEBUG_KEXDH
90*0Sstevel@tonic-gate 	fprintf(stderr, "dh_server_pub= ");
91*0Sstevel@tonic-gate 	BN_print_fp(stderr, dh_server_pub);
92*0Sstevel@tonic-gate 	fprintf(stderr, "\n");
93*0Sstevel@tonic-gate 	debug("bits %d", BN_num_bits(dh_server_pub));
94*0Sstevel@tonic-gate #endif
95*0Sstevel@tonic-gate 
96*0Sstevel@tonic-gate 	/* signed H */
97*0Sstevel@tonic-gate 	signature = packet_get_string(&slen);
98*0Sstevel@tonic-gate 	packet_check_eom();
99*0Sstevel@tonic-gate 
100*0Sstevel@tonic-gate 	if (!dh_pub_is_valid(dh, dh_server_pub))
101*0Sstevel@tonic-gate 		packet_disconnect("bad server public DH value");
102*0Sstevel@tonic-gate 
103*0Sstevel@tonic-gate 	klen = DH_size(dh);
104*0Sstevel@tonic-gate 	kbuf = xmalloc(klen);
105*0Sstevel@tonic-gate 	kout = DH_compute_key(kbuf, dh_server_pub, dh);
106*0Sstevel@tonic-gate #ifdef DEBUG_KEXDH
107*0Sstevel@tonic-gate 	dump_digest("shared secret", kbuf, kout);
108*0Sstevel@tonic-gate #endif
109*0Sstevel@tonic-gate 	if ((shared_secret = BN_new()) == NULL)
110*0Sstevel@tonic-gate 		fatal("kexdh_client: BN_new failed");
111*0Sstevel@tonic-gate 	BN_bin2bn(kbuf, kout, shared_secret);
112*0Sstevel@tonic-gate 	memset(kbuf, 0, klen);
113*0Sstevel@tonic-gate 	xfree(kbuf);
114*0Sstevel@tonic-gate 
115*0Sstevel@tonic-gate 	/* calc and verify H */
116*0Sstevel@tonic-gate 	hash = kex_dh_hash(
117*0Sstevel@tonic-gate 	    kex->client_version_string,
118*0Sstevel@tonic-gate 	    kex->server_version_string,
119*0Sstevel@tonic-gate 	    buffer_ptr(&kex->my), buffer_len(&kex->my),
120*0Sstevel@tonic-gate 	    buffer_ptr(&kex->peer), buffer_len(&kex->peer),
121*0Sstevel@tonic-gate 	    server_host_key_blob, sbloblen,
122*0Sstevel@tonic-gate 	    dh->pub_key,
123*0Sstevel@tonic-gate 	    dh_server_pub,
124*0Sstevel@tonic-gate 	    shared_secret
125*0Sstevel@tonic-gate 	);
126*0Sstevel@tonic-gate 	xfree(server_host_key_blob);
127*0Sstevel@tonic-gate 	BN_clear_free(dh_server_pub);
128*0Sstevel@tonic-gate 	DH_free(dh);
129*0Sstevel@tonic-gate 
130*0Sstevel@tonic-gate 	if (key_verify(server_host_key, signature, slen, hash, 20) != 1)
131*0Sstevel@tonic-gate 		fatal("key_verify failed for server_host_key");
132*0Sstevel@tonic-gate 	key_free(server_host_key);
133*0Sstevel@tonic-gate 	xfree(signature);
134*0Sstevel@tonic-gate 
135*0Sstevel@tonic-gate 	/* save session id */
136*0Sstevel@tonic-gate 	if (kex->session_id == NULL) {
137*0Sstevel@tonic-gate 		kex->session_id_len = 20;
138*0Sstevel@tonic-gate 		kex->session_id = xmalloc(kex->session_id_len);
139*0Sstevel@tonic-gate 		memcpy(kex->session_id, hash, kex->session_id_len);
140*0Sstevel@tonic-gate 	}
141*0Sstevel@tonic-gate 
142*0Sstevel@tonic-gate 	kex_derive_keys(kex, hash, shared_secret);
143*0Sstevel@tonic-gate 	BN_clear_free(shared_secret);
144*0Sstevel@tonic-gate 	kex_finish(kex);
145*0Sstevel@tonic-gate }
146