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