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