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>
46 kexgss_client(Kex *kex) {
47 gss_buffer_desc send_tok = GSS_C_EMPTY_BUFFER;
48 gss_buffer_desc recv_tok, gssbuf, msg_tok, *token_ptr;
50 OM_uint32 maj_status, min_status, ret_flags;
51 u_int klen, kout, slen = 0, hashlen, strlen;
53 BIGNUM *dh_server_pub = NULL;
54 BIGNUM *shared_secret = NULL;
58 u_char *serverhostkey = NULL;
64 int nbits = 0, min = DH_GRP_MIN, max = DH_GRP_MAX;
66 /* Initialise our GSSAPI world */
67 ssh_gssapi_build_ctx(&ctxt);
68 if (ssh_gssapi_id_kex(ctxt, kex->name, &gex) == NULL)
69 fatal("Couldn't identify host exchange");
71 if (ssh_gssapi_import_name(ctxt, kex->gss_host))
72 fatal("Couldn't import hostname");
75 debug("Doing group exchange\n");
76 nbits = dh_estimate(kex->we_need * 8);
77 packet_start(SSH2_MSG_KEXGSS_GROUPREQ);
79 packet_put_int(nbits);
84 packet_read_expect(SSH2_MSG_KEXGSS_GROUP);
86 if ((p = BN_new()) == NULL)
87 fatal("BN_new() failed");
88 packet_get_bignum2(p);
89 if ((g = BN_new()) == NULL)
90 fatal("BN_new() failed");
91 packet_get_bignum2(g);
94 if (BN_num_bits(p) < min || BN_num_bits(p) > max)
95 fatal("GSSGRP_GEX group out of range: %d !< %d !< %d",
96 min, BN_num_bits(p), max);
98 dh = dh_new_group(g, p);
100 dh = dh_new_group1();
103 /* Step 1 - e is dh->pub_key */
104 dh_gen_key(dh, kex->we_need * 8);
106 /* This is f, we initialise it now to make life easier */
107 dh_server_pub = BN_new();
108 if (dh_server_pub == NULL)
109 fatal("dh_server_pub == NULL");
111 token_ptr = GSS_C_NO_BUFFER;
114 debug("Calling gss_init_sec_context");
116 maj_status = ssh_gssapi_init_ctx(ctxt,
117 kex->gss_deleg_creds, token_ptr, &send_tok,
120 if (GSS_ERROR(maj_status)) {
121 if (send_tok.length != 0) {
122 packet_start(SSH2_MSG_KEXGSS_CONTINUE);
123 packet_put_string(send_tok.value,
126 fatal("gss_init_context failed");
129 /* If we've got an old receive buffer get rid of it */
130 if (token_ptr != GSS_C_NO_BUFFER)
131 xfree(recv_tok.value);
133 if (maj_status == GSS_S_COMPLETE) {
134 /* If mutual state flag is not true, kex fails */
135 if (!(ret_flags & GSS_C_MUTUAL_FLAG))
136 fatal("Mutual authentication failed");
138 /* If integ avail flag is not true kex fails */
139 if (!(ret_flags & GSS_C_INTEG_FLAG))
140 fatal("Integrity check failed");
144 * If we have data to send, then the last message that we
145 * received cannot have been a 'complete'.
147 if (send_tok.length != 0) {
149 packet_start(SSH2_MSG_KEXGSS_INIT);
150 packet_put_string(send_tok.value,
152 packet_put_bignum2(dh->pub_key);
155 packet_start(SSH2_MSG_KEXGSS_CONTINUE);
156 packet_put_string(send_tok.value,
160 gss_release_buffer(&min_status, &send_tok);
162 /* If we've sent them data, they should reply */
164 type = packet_read();
165 if (type == SSH2_MSG_KEXGSS_HOSTKEY) {
166 debug("Received KEXGSS_HOSTKEY");
168 fatal("Server host key received more than once");
170 packet_get_string(&slen);
172 } while (type == SSH2_MSG_KEXGSS_HOSTKEY);
175 case SSH2_MSG_KEXGSS_CONTINUE:
176 debug("Received GSSAPI_CONTINUE");
177 if (maj_status == GSS_S_COMPLETE)
178 fatal("GSSAPI Continue received from server when complete");
179 recv_tok.value = packet_get_string(&strlen);
180 recv_tok.length = strlen;
182 case SSH2_MSG_KEXGSS_COMPLETE:
183 debug("Received GSSAPI_COMPLETE");
184 packet_get_bignum2(dh_server_pub);
185 msg_tok.value = packet_get_string(&strlen);
186 msg_tok.length = strlen;
188 /* Is there a token included? */
189 if (packet_get_char()) {
191 packet_get_string(&strlen);
192 recv_tok.length = strlen;
193 /* If we're already complete - protocol error */
194 if (maj_status == GSS_S_COMPLETE)
195 packet_disconnect("Protocol error: received token when complete");
197 /* No token included */
198 if (maj_status != GSS_S_COMPLETE)
199 packet_disconnect("Protocol error: did not receive final token");
202 case SSH2_MSG_KEXGSS_ERROR:
203 debug("Received Error");
204 maj_status = packet_get_int();
205 min_status = packet_get_int();
206 msg = packet_get_string(NULL);
207 lang = packet_get_string(NULL);
208 fatal("GSSAPI Key Exchange Error: \n%s",msg);
210 packet_disconnect("Protocol error: didn't expect packet type %d",
213 token_ptr = &recv_tok;
215 /* No data, and not complete */
216 if (maj_status != GSS_S_COMPLETE)
217 fatal("Not complete, and no token output");
219 } while (maj_status & GSS_S_CONTINUE_NEEDED);
222 * We _must_ have received a COMPLETE message in reply from the
223 * server, which will have set dh_server_pub and msg_tok
226 if (type != SSH2_MSG_KEXGSS_COMPLETE)
227 fatal("Didn't receive a SSH2_MSG_KEXGSS_COMPLETE when I expected it");
229 /* Check f in range [1, p-1] */
230 if (!dh_pub_is_valid(dh, dh_server_pub))
231 packet_disconnect("bad server public DH value");
233 /* compute K=f^x mod p */
235 kbuf = xmalloc(klen);
236 kout = DH_compute_key(kbuf, dh_server_pub, dh);
238 shared_secret = BN_new();
239 BN_bin2bn(kbuf,kout, shared_secret);
240 memset(kbuf, 0, klen);
246 kex->client_version_string,
247 kex->server_version_string,
248 buffer_ptr(&kex->my), buffer_len(&kex->my),
249 buffer_ptr(&kex->peer), buffer_len(&kex->peer),
259 /* The GSS hash is identical to the DH one */
260 kex_dh_hash( kex->client_version_string,
261 kex->server_version_string,
262 buffer_ptr(&kex->my), buffer_len(&kex->my),
263 buffer_ptr(&kex->peer), buffer_len(&kex->peer),
264 serverhostkey, slen, /* server host key */
266 dh_server_pub, /* f */
267 shared_secret, /* K */
273 gssbuf.length = hashlen;
275 /* Verify that the hash matches the MIC we just got. */
276 if (GSS_ERROR(ssh_gssapi_checkmic(ctxt, &gssbuf, &msg_tok)))
277 packet_disconnect("Hash's MIC didn't verify");
279 xfree(msg_tok.value);
283 xfree(serverhostkey);
284 BN_clear_free(dh_server_pub);
286 /* save session id */
287 if (kex->session_id == NULL) {
288 kex->session_id_len = hashlen;
289 kex->session_id = xmalloc(kex->session_id_len);
290 memcpy(kex->session_id, hash, kex->session_id_len);
293 if (gss_kex_context == NULL)
294 gss_kex_context = ctxt;
296 ssh_gssapi_delete_ctx(&ctxt);
298 kex_derive_keys(kex, hash, hashlen, shared_secret);
299 BN_clear_free(shared_secret);