2 * Copyright (c) 2001-2006 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.
31 #include <openssl/crypto.h>
32 #include <openssl/bn.h>
49 kexgss_client(Kex *kex) {
50 gss_buffer_desc send_tok = GSS_C_EMPTY_BUFFER;
51 gss_buffer_desc recv_tok, gssbuf, msg_tok, *token_ptr;
53 OM_uint32 maj_status, min_status, ret_flags;
54 u_int klen, kout, slen = 0, hashlen, strlen;
56 BIGNUM *dh_server_pub = NULL;
57 BIGNUM *shared_secret = NULL;
61 u_char *serverhostkey = NULL;
66 int nbits = 0, min = DH_GRP_MIN, max = DH_GRP_MAX;
68 /* Initialise our GSSAPI world */
69 ssh_gssapi_build_ctx(&ctxt);
70 if (ssh_gssapi_id_kex(ctxt, kex->name, kex->kex_type)
72 fatal("Couldn't identify host exchange");
74 if (ssh_gssapi_import_name(ctxt, kex->gss_host))
75 fatal("Couldn't import hostname");
77 switch (kex->kex_type) {
78 case KEX_GSS_GRP1_SHA1:
81 case KEX_GSS_GRP14_SHA1:
82 dh = dh_new_group14();
84 case KEX_GSS_GEX_SHA1:
85 debug("Doing group exchange\n");
86 nbits = dh_estimate(kex->we_need * 8);
87 packet_start(SSH2_MSG_KEXGSS_GROUPREQ);
89 packet_put_int(nbits);
94 packet_read_expect(SSH2_MSG_KEXGSS_GROUP);
96 if ((p = BN_new()) == NULL)
97 fatal("BN_new() failed");
98 packet_get_bignum2(p);
99 if ((g = BN_new()) == NULL)
100 fatal("BN_new() failed");
101 packet_get_bignum2(g);
104 if (BN_num_bits(p) < min || BN_num_bits(p) > max)
105 fatal("GSSGRP_GEX group out of range: %d !< %d !< %d",
106 min, BN_num_bits(p), max);
108 dh = dh_new_group(g, p);
111 fatal("%s: Unexpected KEX type %d", __func__, kex->kex_type);
114 /* Step 1 - e is dh->pub_key */
115 dh_gen_key(dh, kex->we_need * 8);
117 /* This is f, we initialise it now to make life easier */
118 dh_server_pub = BN_new();
119 if (dh_server_pub == NULL)
120 fatal("dh_server_pub == NULL");
122 token_ptr = GSS_C_NO_BUFFER;
125 debug("Calling gss_init_sec_context");
127 maj_status = ssh_gssapi_init_ctx(ctxt,
128 kex->gss_deleg_creds, token_ptr, &send_tok,
131 if (GSS_ERROR(maj_status)) {
132 if (send_tok.length != 0) {
133 packet_start(SSH2_MSG_KEXGSS_CONTINUE);
134 packet_put_string(send_tok.value,
137 fatal("gss_init_context failed");
140 /* If we've got an old receive buffer get rid of it */
141 if (token_ptr != GSS_C_NO_BUFFER)
142 xfree(recv_tok.value);
144 if (maj_status == GSS_S_COMPLETE) {
145 /* If mutual state flag is not true, kex fails */
146 if (!(ret_flags & GSS_C_MUTUAL_FLAG))
147 fatal("Mutual authentication failed");
149 /* If integ avail flag is not true kex fails */
150 if (!(ret_flags & GSS_C_INTEG_FLAG))
151 fatal("Integrity check failed");
155 * If we have data to send, then the last message that we
156 * received cannot have been a 'complete'.
158 if (send_tok.length != 0) {
160 packet_start(SSH2_MSG_KEXGSS_INIT);
161 packet_put_string(send_tok.value,
163 packet_put_bignum2(dh->pub_key);
166 packet_start(SSH2_MSG_KEXGSS_CONTINUE);
167 packet_put_string(send_tok.value,
171 gss_release_buffer(&min_status, &send_tok);
173 /* If we've sent them data, they should reply */
175 type = packet_read();
176 if (type == SSH2_MSG_KEXGSS_HOSTKEY) {
177 debug("Received KEXGSS_HOSTKEY");
179 fatal("Server host key received more than once");
181 packet_get_string(&slen);
183 } while (type == SSH2_MSG_KEXGSS_HOSTKEY);
186 case SSH2_MSG_KEXGSS_CONTINUE:
187 debug("Received GSSAPI_CONTINUE");
188 if (maj_status == GSS_S_COMPLETE)
189 fatal("GSSAPI Continue received from server when complete");
190 recv_tok.value = packet_get_string(&strlen);
191 recv_tok.length = strlen;
193 case SSH2_MSG_KEXGSS_COMPLETE:
194 debug("Received GSSAPI_COMPLETE");
195 packet_get_bignum2(dh_server_pub);
196 msg_tok.value = packet_get_string(&strlen);
197 msg_tok.length = strlen;
199 /* Is there a token included? */
200 if (packet_get_char()) {
202 packet_get_string(&strlen);
203 recv_tok.length = strlen;
204 /* If we're already complete - protocol error */
205 if (maj_status == GSS_S_COMPLETE)
206 packet_disconnect("Protocol error: received token when complete");
208 /* No token included */
209 if (maj_status != GSS_S_COMPLETE)
210 packet_disconnect("Protocol error: did not receive final token");
213 case SSH2_MSG_KEXGSS_ERROR:
214 debug("Received Error");
215 maj_status = packet_get_int();
216 min_status = packet_get_int();
217 msg = packet_get_string(NULL);
218 lang = packet_get_string(NULL);
219 fatal("GSSAPI Error: \n%.400s",msg);
221 packet_disconnect("Protocol error: didn't expect packet type %d",
224 token_ptr = &recv_tok;
226 /* No data, and not complete */
227 if (maj_status != GSS_S_COMPLETE)
228 fatal("Not complete, and no token output");
230 } while (maj_status & GSS_S_CONTINUE_NEEDED);
233 * We _must_ have received a COMPLETE message in reply from the
234 * server, which will have set dh_server_pub and msg_tok
237 if (type != SSH2_MSG_KEXGSS_COMPLETE)
238 fatal("Didn't receive a SSH2_MSG_KEXGSS_COMPLETE when I expected it");
240 /* Check f in range [1, p-1] */
241 if (!dh_pub_is_valid(dh, dh_server_pub))
242 packet_disconnect("bad server public DH value");
244 /* compute K=f^x mod p */
246 kbuf = xmalloc(klen);
247 kout = DH_compute_key(kbuf, dh_server_pub, dh);
249 shared_secret = BN_new();
250 BN_bin2bn(kbuf,kout, shared_secret);
251 memset(kbuf, 0, klen);
254 switch (kex->kex_type) {
255 case KEX_GSS_GRP1_SHA1:
256 case KEX_GSS_GRP14_SHA1:
257 kex_dh_hash( kex->client_version_string,
258 kex->server_version_string,
259 buffer_ptr(&kex->my), buffer_len(&kex->my),
260 buffer_ptr(&kex->peer), buffer_len(&kex->peer),
261 serverhostkey, slen, /* server host key */
263 dh_server_pub, /* f */
264 shared_secret, /* K */
268 case KEX_GSS_GEX_SHA1:
271 kex->client_version_string,
272 kex->server_version_string,
273 buffer_ptr(&kex->my), buffer_len(&kex->my),
274 buffer_ptr(&kex->peer), buffer_len(&kex->peer),
285 fatal("%s: Unexpected KEX type %d", __func__, kex->kex_type);
289 gssbuf.length = hashlen;
291 /* Verify that the hash matches the MIC we just got. */
292 if (GSS_ERROR(ssh_gssapi_checkmic(ctxt, &gssbuf, &msg_tok)))
293 packet_disconnect("Hash's MIC didn't verify");
295 xfree(msg_tok.value);
299 xfree(serverhostkey);
300 BN_clear_free(dh_server_pub);
302 /* save session id */
303 if (kex->session_id == NULL) {
304 kex->session_id_len = hashlen;
305 kex->session_id = xmalloc(kex->session_id_len);
306 memcpy(kex->session_id, hash, kex->session_id_len);
309 if (gss_kex_context == NULL)
310 gss_kex_context = ctxt;
312 ssh_gssapi_delete_ctx(&ctxt);
314 kex_derive_keys(kex, hash, hashlen, shared_secret);
315 BN_clear_free(shared_secret);