blob: 7599f2af42d74b591f6775109f734d1be61b109a [file] [log] [blame]
Damien Millerd7834352006-08-05 12:39:39 +10001/* $OpenBSD: kexgexs.c,v 1.8 2006/08/03 03:34:42 deraadt 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 Millera7a73ee2006-08-05 11:37:59 +100031#include <stdio.h>
Damien Millere3476ed2006-07-24 14:13:33 +100032#include <string.h>
Damien Millerd7834352006-08-05 12:39:39 +100033#include <signal.h>
Damien Millere3476ed2006-07-24 14:13:33 +100034
Damien Miller8e7fb332003-02-24 12:03:03 +110035#include "xmalloc.h"
Damien Millerd7834352006-08-05 12:39:39 +100036#include "buffer.h"
Damien Miller8e7fb332003-02-24 12:03:03 +110037#include "key.h"
Damien Millerd7834352006-08-05 12:39:39 +100038#include "cipher.h"
Damien Miller8e7fb332003-02-24 12:03:03 +110039#include "kex.h"
40#include "log.h"
41#include "packet.h"
42#include "dh.h"
43#include "ssh2.h"
44#include "compat.h"
Damien Millerd7834352006-08-05 12:39:39 +100045#ifdef GSSAPI
46#include "ssh-gss.h"
47#endif
Damien Miller8e7fb332003-02-24 12:03:03 +110048#include "monitor_wrap.h"
49
50void
51kexgex_server(Kex *kex)
52{
53 BIGNUM *shared_secret = NULL, *dh_client_pub = NULL;
54 Key *server_host_key;
55 DH *dh;
56 u_char *kbuf, *hash, *signature = NULL, *server_host_key_blob = NULL;
Damien Miller19bb3a52005-11-05 15:19:35 +110057 u_int sbloblen, klen, kout, slen, hashlen;
Damien Miller8e7fb332003-02-24 12:03:03 +110058 int min = -1, max = -1, nbits = -1, type;
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 type = packet_read();
67 switch (type) {
68 case SSH2_MSG_KEX_DH_GEX_REQUEST:
69 debug("SSH2_MSG_KEX_DH_GEX_REQUEST received");
70 min = packet_get_int();
71 nbits = packet_get_int();
72 max = packet_get_int();
73 min = MAX(DH_GRP_MIN, min);
74 max = MIN(DH_GRP_MAX, max);
75 break;
76 case SSH2_MSG_KEX_DH_GEX_REQUEST_OLD:
77 debug("SSH2_MSG_KEX_DH_GEX_REQUEST_OLD received");
78 nbits = packet_get_int();
79 min = DH_GRP_MIN;
80 max = DH_GRP_MAX;
81 /* unused for old GEX */
82 break;
83 default:
84 fatal("protocol error during kex, no DH_GEX_REQUEST: %d", type);
85 }
86 packet_check_eom();
87
88 if (max < min || nbits < min || max < nbits)
89 fatal("DH_GEX_REQUEST, bad parameters: %d !< %d !< %d",
90 min, nbits, max);
91
92 /* Contact privileged parent */
93 dh = PRIVSEP(choose_dh(min, nbits, max));
94 if (dh == NULL)
95 packet_disconnect("Protocol error: no matching DH grp found");
96
97 debug("SSH2_MSG_KEX_DH_GEX_GROUP sent");
98 packet_start(SSH2_MSG_KEX_DH_GEX_GROUP);
99 packet_put_bignum2(dh->p);
100 packet_put_bignum2(dh->g);
101 packet_send();
102
103 /* flush */
104 packet_write_wait();
105
106 /* Compute our exchange value in parallel with the client */
107 dh_gen_key(dh, kex->we_need * 8);
108
109 debug("expecting SSH2_MSG_KEX_DH_GEX_INIT");
110 packet_read_expect(SSH2_MSG_KEX_DH_GEX_INIT);
111
112 /* key, cert */
113 if ((dh_client_pub = BN_new()) == NULL)
114 fatal("dh_client_pub == NULL");
115 packet_get_bignum2(dh_client_pub);
116 packet_check_eom();
117
118#ifdef DEBUG_KEXDH
119 fprintf(stderr, "dh_client_pub= ");
120 BN_print_fp(stderr, dh_client_pub);
121 fprintf(stderr, "\n");
122 debug("bits %d", BN_num_bits(dh_client_pub));
123#endif
124
125#ifdef DEBUG_KEXDH
126 DHparams_print_fp(stderr, dh);
127 fprintf(stderr, "pub= ");
128 BN_print_fp(stderr, dh->pub_key);
129 fprintf(stderr, "\n");
130#endif
131 if (!dh_pub_is_valid(dh, dh_client_pub))
132 packet_disconnect("bad client public DH value");
133
134 klen = DH_size(dh);
135 kbuf = xmalloc(klen);
136 kout = DH_compute_key(kbuf, dh_client_pub, dh);
137#ifdef DEBUG_KEXDH
138 dump_digest("shared secret", kbuf, kout);
139#endif
140 if ((shared_secret = BN_new()) == NULL)
141 fatal("kexgex_server: BN_new failed");
142 BN_bin2bn(kbuf, kout, shared_secret);
143 memset(kbuf, 0, klen);
144 xfree(kbuf);
145
146 key_to_blob(server_host_key, &server_host_key_blob, &sbloblen);
147
148 if (type == SSH2_MSG_KEX_DH_GEX_REQUEST_OLD)
149 min = max = -1;
150
Damien Miller19bb3a52005-11-05 15:19:35 +1100151 /* calc H */
152 kexgex_hash(
153 kex->evp_md,
Damien Miller8e7fb332003-02-24 12:03:03 +1100154 kex->client_version_string,
155 kex->server_version_string,
156 buffer_ptr(&kex->peer), buffer_len(&kex->peer),
157 buffer_ptr(&kex->my), buffer_len(&kex->my),
158 server_host_key_blob, sbloblen,
159 min, nbits, max,
160 dh->p, dh->g,
161 dh_client_pub,
162 dh->pub_key,
Damien Miller19bb3a52005-11-05 15:19:35 +1100163 shared_secret,
164 &hash, &hashlen
Damien Miller8e7fb332003-02-24 12:03:03 +1100165 );
166 BN_clear_free(dh_client_pub);
167
168 /* save session id := H */
Damien Miller8e7fb332003-02-24 12:03:03 +1100169 if (kex->session_id == NULL) {
Damien Miller19bb3a52005-11-05 15:19:35 +1100170 kex->session_id_len = hashlen;
Damien Miller8e7fb332003-02-24 12:03:03 +1100171 kex->session_id = xmalloc(kex->session_id_len);
172 memcpy(kex->session_id, hash, kex->session_id_len);
173 }
174
175 /* sign H */
Damien Miller19bb3a52005-11-05 15:19:35 +1100176 PRIVSEP(key_sign(server_host_key, &signature, &slen, hash, hashlen));
Damien Miller8e7fb332003-02-24 12:03:03 +1100177
178 /* destroy_sensitive_data(); */
179
180 /* send server hostkey, DH pubkey 'f' and singed H */
181 debug("SSH2_MSG_KEX_DH_GEX_REPLY sent");
182 packet_start(SSH2_MSG_KEX_DH_GEX_REPLY);
183 packet_put_string(server_host_key_blob, sbloblen);
184 packet_put_bignum2(dh->pub_key); /* f */
185 packet_put_string(signature, slen);
186 packet_send();
187
188 xfree(signature);
189 xfree(server_host_key_blob);
190 /* have keys, free DH */
191 DH_free(dh);
192
Damien Miller19bb3a52005-11-05 15:19:35 +1100193 kex_derive_keys(kex, hash, hashlen, shared_secret);
Damien Miller8e7fb332003-02-24 12:03:03 +1100194 BN_clear_free(shared_secret);
195
196 kex_finish(kex);
197}