xref: /titanic_41/usr/src/cmd/ssh/libssh/common/kexdhs.c (revision c40d696f8f0e05103b3795dd37198e00ae7ef955)
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 #include "monitor_wrap.h"
43 
44 void
45 kexdh_server(Kex *kex)
46 {
47 	BIGNUM *shared_secret = NULL, *dh_client_pub = NULL;
48 	DH *dh;
49 	Key *server_host_key;
50 	u_char *kbuf, *hash, *signature = NULL, *server_host_key_blob = NULL;
51 	u_int sbloblen, klen, kout;
52 	u_int slen;
53 
54 	/* generate server DH public key */
55 	dh = dh_new_group1();
56 	dh_gen_key(dh, kex->we_need * 8);
57 
58 	debug("expecting SSH2_MSG_KEXDH_INIT");
59 	packet_read_expect(SSH2_MSG_KEXDH_INIT);
60 
61 	if (kex->load_host_key == NULL)
62 		fatal("Cannot load hostkey");
63 	server_host_key = kex->load_host_key(kex->hostkey_type);
64 	if (server_host_key == NULL)
65 		fatal("Unsupported hostkey type %d", kex->hostkey_type);
66 
67 	/* key, cert */
68 	if ((dh_client_pub = BN_new()) == NULL)
69 		fatal("dh_client_pub == NULL");
70 	packet_get_bignum2(dh_client_pub);
71 	packet_check_eom();
72 
73 #ifdef DEBUG_KEXDH
74 	fprintf(stderr, "dh_client_pub= ");
75 	BN_print_fp(stderr, dh_client_pub);
76 	fprintf(stderr, "\n");
77 	debug("bits %d", BN_num_bits(dh_client_pub));
78 #endif
79 
80 #ifdef DEBUG_KEXDH
81 	DHparams_print_fp(stderr, dh);
82 	fprintf(stderr, "pub= ");
83 	BN_print_fp(stderr, dh->pub_key);
84 	fprintf(stderr, "\n");
85 #endif
86 	if (!dh_pub_is_valid(dh, dh_client_pub))
87 		packet_disconnect("bad client public DH value");
88 
89 	klen = DH_size(dh);
90 	kbuf = xmalloc(klen);
91 	kout = DH_compute_key(kbuf, dh_client_pub, dh);
92 #ifdef DEBUG_KEXDH
93 	dump_digest("shared secret", kbuf, kout);
94 #endif
95 	if ((shared_secret = BN_new()) == NULL)
96 		fatal("kexdh_server: BN_new failed");
97 	BN_bin2bn(kbuf, kout, shared_secret);
98 	memset(kbuf, 0, klen);
99 	xfree(kbuf);
100 
101 	key_to_blob(server_host_key, &server_host_key_blob, &sbloblen);
102 
103 	/* calc H */
104 	hash = kex_dh_hash(
105 	    kex->client_version_string,
106 	    kex->server_version_string,
107 	    buffer_ptr(&kex->peer), buffer_len(&kex->peer),
108 	    buffer_ptr(&kex->my), buffer_len(&kex->my),
109 	    server_host_key_blob, sbloblen,
110 	    dh_client_pub,
111 	    dh->pub_key,
112 	    shared_secret
113 	);
114 	BN_clear_free(dh_client_pub);
115 
116 	/* save session id := H */
117 	/* XXX hashlen depends on KEX */
118 	if (kex->session_id == NULL) {
119 		kex->session_id_len = 20;
120 		kex->session_id = xmalloc(kex->session_id_len);
121 		memcpy(kex->session_id, hash, kex->session_id_len);
122 	}
123 
124 	/* sign H */
125 	/* XXX hashlen depends on KEX */
126 	PRIVSEP(key_sign(server_host_key, &signature, &slen, hash, 20));
127 
128 	/* destroy_sensitive_data(); */
129 
130 	/* send server hostkey, DH pubkey 'f' and singed H */
131 	packet_start(SSH2_MSG_KEXDH_REPLY);
132 	packet_put_string(server_host_key_blob, sbloblen);
133 	packet_put_bignum2(dh->pub_key);	/* f */
134 	packet_put_string(signature, slen);
135 	packet_send();
136 
137 	xfree(signature);
138 	xfree(server_host_key_blob);
139 	/* have keys, free DH */
140 	DH_free(dh);
141 
142 	kex_derive_keys(kex, hash, shared_secret);
143 	BN_clear_free(shared_secret);
144 	kex_finish(kex);
145 }
146