2 * Copyright (c) 2001-2005 Simon Wilkinson. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
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.
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.
29 #include <openssl/crypto.h>
30 #include <openssl/bn.h>
44 kexgss_client(Kex *kex) {
45 gss_buffer_desc send_tok = GSS_C_EMPTY_BUFFER;
46 gss_buffer_desc recv_tok, gssbuf, msg_tok, *token_ptr;
48 OM_uint32 maj_status, min_status, ret_flags;
49 unsigned int klen, kout;
51 BIGNUM *dh_server_pub = NULL;
52 BIGNUM *shared_secret = NULL;
58 unsigned char *serverhostkey = NULL;
63 unsigned int slen = 0;
65 int nbits = -1, min = -1, max = -1;
68 /* Initialise our GSSAPI world */
69 ssh_gssapi_build_ctx(&ctxt);
70 if (ssh_gssapi_id_kex(ctxt, kex->name, &gex) == NULL)
71 fatal("Couldn't identify host exchange");
73 if (ssh_gssapi_import_name(ctxt, kex->gss_host))
74 fatal("Couldn't import hostname");
77 debug("Doing group exchange\n");
78 nbits = dh_estimate(kex->we_need * 8);
81 packet_start(SSH2_MSG_KEXGSS_GROUPREQ);
83 packet_put_int(nbits);
88 packet_read_expect(SSH2_MSG_KEXGSS_GROUP);
90 if ((p = BN_new()) == NULL)
91 fatal("BN_new() failed");
92 packet_get_bignum2(p);
93 if ((g = BN_new()) == NULL)
94 fatal("BN_new() failed");
95 packet_get_bignum2(g);
98 if (BN_num_bits(p) < min || BN_num_bits(p) > max)
99 fatal("GSSGRP_GEX group out of range: %d !< %d !< %d",
100 min, BN_num_bits(p), max);
102 dh = dh_new_group(g, p);
104 dh = dh_new_group1();
107 /* Step 1 - e is dh->pub_key */
108 dh_gen_key(dh, kex->we_need * 8);
110 /* This is f, we initialise it now to make life easier */
111 dh_server_pub = BN_new();
112 if (dh_server_pub == NULL)
113 fatal("dh_server_pub == NULL");
115 token_ptr = GSS_C_NO_BUFFER;
118 debug("Calling gss_init_sec_context");
120 maj_status = ssh_gssapi_init_ctx(ctxt,
121 kex->gss_deleg_creds, token_ptr, &send_tok,
124 if (GSS_ERROR(maj_status)) {
125 if (send_tok.length != 0) {
126 packet_start(SSH2_MSG_KEXGSS_CONTINUE);
127 packet_put_string(send_tok.value,
130 fatal("gss_init_context failed");
133 /* If we've got an old receive buffer get rid of it */
134 if (token_ptr != GSS_C_NO_BUFFER)
135 xfree(recv_tok.value);
137 if (maj_status == GSS_S_COMPLETE) {
138 /* If mutual state flag is not true, kex fails */
139 if (!(ret_flags & GSS_C_MUTUAL_FLAG))
140 fatal("Mutual authentication failed");
142 /* If integ avail flag is not true kex fails */
143 if (!(ret_flags & GSS_C_INTEG_FLAG))
144 fatal("Integrity check failed");
148 * If we have data to send, then the last message that we
149 * received cannot have been a 'complete'.
151 if (send_tok.length != 0) {
153 packet_start(SSH2_MSG_KEXGSS_INIT);
154 packet_put_string(send_tok.value,
156 packet_put_bignum2(dh->pub_key);
159 packet_start(SSH2_MSG_KEXGSS_CONTINUE);
160 packet_put_string(send_tok.value,
164 gss_release_buffer(&min_status, &send_tok);
166 /* If we've sent them data, they should reply */
168 type = packet_read();
169 if (type == SSH2_MSG_KEXGSS_HOSTKEY) {
170 debug("Received KEXGSS_HOSTKEY");
172 fatal("Server host key received more than once");
174 packet_get_string(&slen);
176 } while (type == SSH2_MSG_KEXGSS_HOSTKEY);
179 case SSH2_MSG_KEXGSS_CONTINUE:
180 debug("Received GSSAPI_CONTINUE");
181 if (maj_status == GSS_S_COMPLETE)
182 fatal("GSSAPI Continue received from server when complete");
183 recv_tok.value = packet_get_string(&strlen);
184 recv_tok.length = strlen;
186 case SSH2_MSG_KEXGSS_COMPLETE:
187 debug("Received GSSAPI_COMPLETE");
188 packet_get_bignum2(dh_server_pub);
189 msg_tok.value = packet_get_string(&strlen);
190 msg_tok.length = strlen;
192 /* Is there a token included? */
193 if (packet_get_char()) {
195 packet_get_string(&strlen);
196 recv_tok.length = strlen;
197 /* If we're already complete - protocol error */
198 if (maj_status == GSS_S_COMPLETE)
199 packet_disconnect("Protocol error: received token when complete");
201 /* No token included */
202 if (maj_status != GSS_S_COMPLETE)
203 packet_disconnect("Protocol error: did not receive final token");
206 case SSH2_MSG_KEXGSS_ERROR:
207 debug("Received Error");
208 maj_status = packet_get_int();
209 min_status = packet_get_int();
210 msg = packet_get_string(NULL);
211 lang = packet_get_string(NULL);
212 fatal("GSSAPI Key Exchange Error: \n%s",msg);
214 packet_disconnect("Protocol error: didn't expect packet type %d",
217 token_ptr = &recv_tok;
219 /* No data, and not complete */
220 if (maj_status != GSS_S_COMPLETE)
221 fatal("Not complete, and no token output");
223 } while (maj_status & GSS_S_CONTINUE_NEEDED);
226 * We _must_ have received a COMPLETE message in reply from the
227 * server, which will have set dh_server_pub and msg_tok
230 if (type != SSH2_MSG_KEXGSS_COMPLETE)
231 fatal("Didn't receive a SSH2_MSG_KEXGSS_COMPLETE when I expected it");
233 /* Check f in range [1, p-1] */
234 if (!dh_pub_is_valid(dh, dh_server_pub))
235 packet_disconnect("bad server public DH value");
237 /* compute K=f^x mod p */
239 kbuf = xmalloc(klen);
240 kout = DH_compute_key(kbuf, dh_server_pub, dh);
242 shared_secret = BN_new();
243 BN_bin2bn(kbuf,kout, shared_secret);
244 memset(kbuf, 0, klen);
250 kex->client_version_string,
251 kex->server_version_string,
252 buffer_ptr(&kex->my), buffer_len(&kex->my),
253 buffer_ptr(&kex->peer), buffer_len(&kex->peer),
263 /* The GSS hash is identical to the DH one */
264 kex_dh_hash( kex->client_version_string,
265 kex->server_version_string,
266 buffer_ptr(&kex->my), buffer_len(&kex->my),
267 buffer_ptr(&kex->peer), buffer_len(&kex->peer),
268 serverhostkey, slen, /* server host key */
270 dh_server_pub, /* f */
271 shared_secret, /* K */
279 /* Verify that the hash matches the MIC we just got. */
280 if (GSS_ERROR(ssh_gssapi_checkmic(ctxt, &gssbuf, &msg_tok)))
281 packet_disconnect("Hash's MIC didn't verify");
283 xfree(msg_tok.value);
287 xfree(serverhostkey);
288 BN_clear_free(dh_server_pub);
290 /* save session id */
291 if (kex->session_id == NULL) {
292 kex->session_id_len = 20;
293 kex->session_id = xmalloc(kex->session_id_len);
294 memcpy(kex->session_id, hash, kex->session_id_len);
297 if (gss_kex_context == NULL)
298 gss_kex_context = ctxt;
300 ssh_gssapi_delete_ctx(&ctxt);
302 kex_derive_keys(kex, hash, hashlen, shared_secret);
303 BN_clear_free(shared_secret);