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 u_int klen, kout, slen = 0, hashlen, strlen;
51 BIGNUM *dh_server_pub = NULL;
52 BIGNUM *shared_secret = NULL;
56 u_char *serverhostkey = NULL;
62 int nbits = 0, min = DH_GRP_MIN, max = DH_GRP_MAX;
64 /* Initialise our GSSAPI world */
65 ssh_gssapi_build_ctx(&ctxt);
66 if (ssh_gssapi_id_kex(ctxt, kex->name, &gex) == NULL)
67 fatal("Couldn't identify host exchange");
69 if (ssh_gssapi_import_name(ctxt, kex->gss_host))
70 fatal("Couldn't import hostname");
73 debug("Doing group exchange\n");
74 nbits = dh_estimate(kex->we_need * 8);
75 packet_start(SSH2_MSG_KEXGSS_GROUPREQ);
77 packet_put_int(nbits);
82 packet_read_expect(SSH2_MSG_KEXGSS_GROUP);
84 if ((p = BN_new()) == NULL)
85 fatal("BN_new() failed");
86 packet_get_bignum2(p);
87 if ((g = BN_new()) == NULL)
88 fatal("BN_new() failed");
89 packet_get_bignum2(g);
92 if (BN_num_bits(p) < min || BN_num_bits(p) > max)
93 fatal("GSSGRP_GEX group out of range: %d !< %d !< %d",
94 min, BN_num_bits(p), max);
96 dh = dh_new_group(g, p);
101 /* Step 1 - e is dh->pub_key */
102 dh_gen_key(dh, kex->we_need * 8);
104 /* This is f, we initialise it now to make life easier */
105 dh_server_pub = BN_new();
106 if (dh_server_pub == NULL)
107 fatal("dh_server_pub == NULL");
109 token_ptr = GSS_C_NO_BUFFER;
112 debug("Calling gss_init_sec_context");
114 maj_status = ssh_gssapi_init_ctx(ctxt,
115 kex->gss_deleg_creds, token_ptr, &send_tok,
118 if (GSS_ERROR(maj_status)) {
119 if (send_tok.length != 0) {
120 packet_start(SSH2_MSG_KEXGSS_CONTINUE);
121 packet_put_string(send_tok.value,
124 fatal("gss_init_context failed");
127 /* If we've got an old receive buffer get rid of it */
128 if (token_ptr != GSS_C_NO_BUFFER)
129 xfree(recv_tok.value);
131 if (maj_status == GSS_S_COMPLETE) {
132 /* If mutual state flag is not true, kex fails */
133 if (!(ret_flags & GSS_C_MUTUAL_FLAG))
134 fatal("Mutual authentication failed");
136 /* If integ avail flag is not true kex fails */
137 if (!(ret_flags & GSS_C_INTEG_FLAG))
138 fatal("Integrity check failed");
142 * If we have data to send, then the last message that we
143 * received cannot have been a 'complete'.
145 if (send_tok.length != 0) {
147 packet_start(SSH2_MSG_KEXGSS_INIT);
148 packet_put_string(send_tok.value,
150 packet_put_bignum2(dh->pub_key);
153 packet_start(SSH2_MSG_KEXGSS_CONTINUE);
154 packet_put_string(send_tok.value,
158 gss_release_buffer(&min_status, &send_tok);
160 /* If we've sent them data, they should reply */
162 type = packet_read();
163 if (type == SSH2_MSG_KEXGSS_HOSTKEY) {
164 debug("Received KEXGSS_HOSTKEY");
166 fatal("Server host key received more than once");
168 packet_get_string(&slen);
170 } while (type == SSH2_MSG_KEXGSS_HOSTKEY);
173 case SSH2_MSG_KEXGSS_CONTINUE:
174 debug("Received GSSAPI_CONTINUE");
175 if (maj_status == GSS_S_COMPLETE)
176 fatal("GSSAPI Continue received from server when complete");
177 recv_tok.value = packet_get_string(&strlen);
178 recv_tok.length = strlen;
180 case SSH2_MSG_KEXGSS_COMPLETE:
181 debug("Received GSSAPI_COMPLETE");
182 packet_get_bignum2(dh_server_pub);
183 msg_tok.value = packet_get_string(&strlen);
184 msg_tok.length = strlen;
186 /* Is there a token included? */
187 if (packet_get_char()) {
189 packet_get_string(&strlen);
190 recv_tok.length = strlen;
191 /* If we're already complete - protocol error */
192 if (maj_status == GSS_S_COMPLETE)
193 packet_disconnect("Protocol error: received token when complete");
195 /* No token included */
196 if (maj_status != GSS_S_COMPLETE)
197 packet_disconnect("Protocol error: did not receive final token");
200 case SSH2_MSG_KEXGSS_ERROR:
201 debug("Received Error");
202 maj_status = packet_get_int();
203 min_status = packet_get_int();
204 msg = packet_get_string(NULL);
205 lang = packet_get_string(NULL);
206 fatal("GSSAPI Key Exchange Error: \n%s",msg);
208 packet_disconnect("Protocol error: didn't expect packet type %d",
211 token_ptr = &recv_tok;
213 /* No data, and not complete */
214 if (maj_status != GSS_S_COMPLETE)
215 fatal("Not complete, and no token output");
217 } while (maj_status & GSS_S_CONTINUE_NEEDED);
220 * We _must_ have received a COMPLETE message in reply from the
221 * server, which will have set dh_server_pub and msg_tok
224 if (type != SSH2_MSG_KEXGSS_COMPLETE)
225 fatal("Didn't receive a SSH2_MSG_KEXGSS_COMPLETE when I expected it");
227 /* Check f in range [1, p-1] */
228 if (!dh_pub_is_valid(dh, dh_server_pub))
229 packet_disconnect("bad server public DH value");
231 /* compute K=f^x mod p */
233 kbuf = xmalloc(klen);
234 kout = DH_compute_key(kbuf, dh_server_pub, dh);
236 shared_secret = BN_new();
237 BN_bin2bn(kbuf,kout, shared_secret);
238 memset(kbuf, 0, klen);
244 kex->client_version_string,
245 kex->server_version_string,
246 buffer_ptr(&kex->my), buffer_len(&kex->my),
247 buffer_ptr(&kex->peer), buffer_len(&kex->peer),
257 /* The GSS hash is identical to the DH one */
258 kex_dh_hash( kex->client_version_string,
259 kex->server_version_string,
260 buffer_ptr(&kex->my), buffer_len(&kex->my),
261 buffer_ptr(&kex->peer), buffer_len(&kex->peer),
262 serverhostkey, slen, /* server host key */
264 dh_server_pub, /* f */
265 shared_secret, /* K */
271 gssbuf.length = hashlen;
273 /* Verify that the hash matches the MIC we just got. */
274 if (GSS_ERROR(ssh_gssapi_checkmic(ctxt, &gssbuf, &msg_tok)))
275 packet_disconnect("Hash's MIC didn't verify");
277 xfree(msg_tok.value);
281 xfree(serverhostkey);
282 BN_clear_free(dh_server_pub);
284 /* save session id */
285 if (kex->session_id == NULL) {
286 kex->session_id_len = hashlen;
287 kex->session_id = xmalloc(kex->session_id_len);
288 memcpy(kex->session_id, hash, kex->session_id_len);
291 if (gss_kex_context == NULL)
292 gss_kex_context = ctxt;
294 ssh_gssapi_delete_ctx(&ctxt);
296 kex_derive_keys(kex, hash, hashlen, shared_secret);
297 BN_clear_free(shared_secret);