blob: 2bd7ee948424eaaf383c06851d7af1c6b45b0ca0 [file] [log] [blame]
Damien Miller8dbffe72006-08-05 11:02:17 +10001/* $OpenBSD: kexgexs.c,v 1.6 2006/07/26 02:35:17 stevesk Exp $ */
Damien Miller8e7fb332003-02-24 12:03:03 +11002/*
3 * Copyright (c) 2000 Niels Provos. All rights reserved.
4 * Copyright (c) 2001 Markus Friedl. All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 */
26
27#include "includes.h"
Damien Miller8e7fb332003-02-24 12:03:03 +110028
Damien Miller8dbffe72006-08-05 11:02:17 +100029#include <sys/param.h>
30
Damien Millere3476ed2006-07-24 14:13:33 +100031#include <string.h>
32
Damien Miller8e7fb332003-02-24 12:03:03 +110033#include "xmalloc.h"
34#include "key.h"
35#include "kex.h"
36#include "log.h"
37#include "packet.h"
38#include "dh.h"
39#include "ssh2.h"
40#include "compat.h"
41#include "monitor_wrap.h"
42
43void
44kexgex_server(Kex *kex)
45{
46 BIGNUM *shared_secret = NULL, *dh_client_pub = NULL;
47 Key *server_host_key;
48 DH *dh;
49 u_char *kbuf, *hash, *signature = NULL, *server_host_key_blob = NULL;
Damien Miller19bb3a52005-11-05 15:19:35 +110050 u_int sbloblen, klen, kout, slen, hashlen;
Damien Miller8e7fb332003-02-24 12:03:03 +110051 int min = -1, max = -1, nbits = -1, type;
52
53 if (kex->load_host_key == NULL)
54 fatal("Cannot load hostkey");
55 server_host_key = kex->load_host_key(kex->hostkey_type);
56 if (server_host_key == NULL)
57 fatal("Unsupported hostkey type %d", kex->hostkey_type);
58
59 type = packet_read();
60 switch (type) {
61 case SSH2_MSG_KEX_DH_GEX_REQUEST:
62 debug("SSH2_MSG_KEX_DH_GEX_REQUEST received");
63 min = packet_get_int();
64 nbits = packet_get_int();
65 max = packet_get_int();
66 min = MAX(DH_GRP_MIN, min);
67 max = MIN(DH_GRP_MAX, max);
68 break;
69 case SSH2_MSG_KEX_DH_GEX_REQUEST_OLD:
70 debug("SSH2_MSG_KEX_DH_GEX_REQUEST_OLD received");
71 nbits = packet_get_int();
72 min = DH_GRP_MIN;
73 max = DH_GRP_MAX;
74 /* unused for old GEX */
75 break;
76 default:
77 fatal("protocol error during kex, no DH_GEX_REQUEST: %d", type);
78 }
79 packet_check_eom();
80
81 if (max < min || nbits < min || max < nbits)
82 fatal("DH_GEX_REQUEST, bad parameters: %d !< %d !< %d",
83 min, nbits, max);
84
85 /* Contact privileged parent */
86 dh = PRIVSEP(choose_dh(min, nbits, max));
87 if (dh == NULL)
88 packet_disconnect("Protocol error: no matching DH grp found");
89
90 debug("SSH2_MSG_KEX_DH_GEX_GROUP sent");
91 packet_start(SSH2_MSG_KEX_DH_GEX_GROUP);
92 packet_put_bignum2(dh->p);
93 packet_put_bignum2(dh->g);
94 packet_send();
95
96 /* flush */
97 packet_write_wait();
98
99 /* Compute our exchange value in parallel with the client */
100 dh_gen_key(dh, kex->we_need * 8);
101
102 debug("expecting SSH2_MSG_KEX_DH_GEX_INIT");
103 packet_read_expect(SSH2_MSG_KEX_DH_GEX_INIT);
104
105 /* key, cert */
106 if ((dh_client_pub = BN_new()) == NULL)
107 fatal("dh_client_pub == NULL");
108 packet_get_bignum2(dh_client_pub);
109 packet_check_eom();
110
111#ifdef DEBUG_KEXDH
112 fprintf(stderr, "dh_client_pub= ");
113 BN_print_fp(stderr, dh_client_pub);
114 fprintf(stderr, "\n");
115 debug("bits %d", BN_num_bits(dh_client_pub));
116#endif
117
118#ifdef DEBUG_KEXDH
119 DHparams_print_fp(stderr, dh);
120 fprintf(stderr, "pub= ");
121 BN_print_fp(stderr, dh->pub_key);
122 fprintf(stderr, "\n");
123#endif
124 if (!dh_pub_is_valid(dh, dh_client_pub))
125 packet_disconnect("bad client public DH value");
126
127 klen = DH_size(dh);
128 kbuf = xmalloc(klen);
129 kout = DH_compute_key(kbuf, dh_client_pub, dh);
130#ifdef DEBUG_KEXDH
131 dump_digest("shared secret", kbuf, kout);
132#endif
133 if ((shared_secret = BN_new()) == NULL)
134 fatal("kexgex_server: BN_new failed");
135 BN_bin2bn(kbuf, kout, shared_secret);
136 memset(kbuf, 0, klen);
137 xfree(kbuf);
138
139 key_to_blob(server_host_key, &server_host_key_blob, &sbloblen);
140
141 if (type == SSH2_MSG_KEX_DH_GEX_REQUEST_OLD)
142 min = max = -1;
143
Damien Miller19bb3a52005-11-05 15:19:35 +1100144 /* calc H */
145 kexgex_hash(
146 kex->evp_md,
Damien Miller8e7fb332003-02-24 12:03:03 +1100147 kex->client_version_string,
148 kex->server_version_string,
149 buffer_ptr(&kex->peer), buffer_len(&kex->peer),
150 buffer_ptr(&kex->my), buffer_len(&kex->my),
151 server_host_key_blob, sbloblen,
152 min, nbits, max,
153 dh->p, dh->g,
154 dh_client_pub,
155 dh->pub_key,
Damien Miller19bb3a52005-11-05 15:19:35 +1100156 shared_secret,
157 &hash, &hashlen
Damien Miller8e7fb332003-02-24 12:03:03 +1100158 );
159 BN_clear_free(dh_client_pub);
160
161 /* save session id := H */
Damien Miller8e7fb332003-02-24 12:03:03 +1100162 if (kex->session_id == NULL) {
Damien Miller19bb3a52005-11-05 15:19:35 +1100163 kex->session_id_len = hashlen;
Damien Miller8e7fb332003-02-24 12:03:03 +1100164 kex->session_id = xmalloc(kex->session_id_len);
165 memcpy(kex->session_id, hash, kex->session_id_len);
166 }
167
168 /* sign H */
Damien Miller19bb3a52005-11-05 15:19:35 +1100169 PRIVSEP(key_sign(server_host_key, &signature, &slen, hash, hashlen));
Damien Miller8e7fb332003-02-24 12:03:03 +1100170
171 /* destroy_sensitive_data(); */
172
173 /* send server hostkey, DH pubkey 'f' and singed H */
174 debug("SSH2_MSG_KEX_DH_GEX_REPLY sent");
175 packet_start(SSH2_MSG_KEX_DH_GEX_REPLY);
176 packet_put_string(server_host_key_blob, sbloblen);
177 packet_put_bignum2(dh->pub_key); /* f */
178 packet_put_string(signature, slen);
179 packet_send();
180
181 xfree(signature);
182 xfree(server_host_key_blob);
183 /* have keys, free DH */
184 DH_free(dh);
185
Damien Miller19bb3a52005-11-05 15:19:35 +1100186 kex_derive_keys(kex, hash, hashlen, shared_secret);
Damien Miller8e7fb332003-02-24 12:03:03 +1100187 BN_clear_free(shared_secret);
188
189 kex_finish(kex);
190}