]> andersk Git - gssapi-openssh.git/blame - openssh/kexgssc.c
Note $GLOBUS_LOCATION/etc/ssh/ssh_config change now so I don't forget
[gssapi-openssh.git] / openssh / kexgssc.c
CommitLineData
1c14df9e 1/*
2e437378 2 * Copyright (c) 2001-2006 Simon Wilkinson. All rights reserved.
1c14df9e 3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
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.
12 *
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.
23 */
24
25#include "includes.h"
26
27#ifdef GSSAPI
28
29#include <openssl/crypto.h>
30#include <openssl/bn.h>
31
2e437378 32#include <string.h>
33
1c14df9e 34#include "xmalloc.h"
35#include "buffer.h"
2e437378 36#include "ssh2.h"
37#include "key.h"
38#include "cipher.h"
1c14df9e 39#include "kex.h"
40#include "log.h"
41#include "packet.h"
42#include "dh.h"
2e437378 43
1c14df9e 44#include "ssh-gss.h"
1c14df9e 45
1c14df9e 46void
34fee935 47kexgss_client(Kex *kex) {
48 gss_buffer_desc send_tok = GSS_C_EMPTY_BUFFER;
2e437378 49 gss_buffer_desc recv_tok, gssbuf, msg_tok, *token_ptr;
1c14df9e 50 Gssctxt *ctxt;
51 OM_uint32 maj_status, min_status, ret_flags;
e00da40d 52 u_int klen, kout, slen = 0, hashlen, strlen;
1c14df9e 53 DH *dh;
34fee935 54 BIGNUM *dh_server_pub = NULL;
55 BIGNUM *shared_secret = NULL;
56 BIGNUM *p = NULL;
57 BIGNUM *g = NULL;
e00da40d 58 u_char *kbuf, *hash;
59 u_char *serverhostkey = NULL;
88928908 60 char *msg;
61 char *lang;
1c14df9e 62 int type = 0;
63 int first = 1;
e00da40d 64 int nbits = 0, min = DH_GRP_MIN, max = DH_GRP_MAX;
34fee935 65
66 /* Initialise our GSSAPI world */
1c14df9e 67 ssh_gssapi_build_ctx(&ctxt);
2e437378 68 if (ssh_gssapi_id_kex(ctxt, kex->name, kex->kex_type)
69 == GSS_C_NO_OID)
1c14df9e 70 fatal("Couldn't identify host exchange");
8bb69cbe 71
34fee935 72 if (ssh_gssapi_import_name(ctxt, kex->gss_host))
73 fatal("Couldn't import hostname");
74
2e437378 75 switch (kex->kex_type) {
76 case KEX_GSS_GRP1_SHA1:
77 dh = dh_new_group1();
78 break;
79 case KEX_GSS_GRP14_SHA1:
80 dh = dh_new_group14();
81 break;
82 case KEX_GSS_GEX_SHA1:
34fee935 83 debug("Doing group exchange\n");
84 nbits = dh_estimate(kex->we_need * 8);
34fee935 85 packet_start(SSH2_MSG_KEXGSS_GROUPREQ);
86 packet_put_int(min);
87 packet_put_int(nbits);
88 packet_put_int(max);
89
90 packet_send();
91
92 packet_read_expect(SSH2_MSG_KEXGSS_GROUP);
93
94 if ((p = BN_new()) == NULL)
95 fatal("BN_new() failed");
96 packet_get_bignum2(p);
97 if ((g = BN_new()) == NULL)
98 fatal("BN_new() failed");
99 packet_get_bignum2(g);
100 packet_check_eom();
101
102 if (BN_num_bits(p) < min || BN_num_bits(p) > max)
103 fatal("GSSGRP_GEX group out of range: %d !< %d !< %d",
104 min, BN_num_bits(p), max);
105
106 dh = dh_new_group(g, p);
2e437378 107 break;
108 default:
109 fatal("%s: Unexpected KEX type %d", __func__, kex->kex_type);
1c14df9e 110 }
111
1c14df9e 112 /* Step 1 - e is dh->pub_key */
1c14df9e 113 dh_gen_key(dh, kex->we_need * 8);
114
115 /* This is f, we initialise it now to make life easier */
34fee935 116 dh_server_pub = BN_new();
117 if (dh_server_pub == NULL)
118 fatal("dh_server_pub == NULL");
119
1c14df9e 120 token_ptr = GSS_C_NO_BUFFER;
121
122 do {
123 debug("Calling gss_init_sec_context");
124
34fee935 125 maj_status = ssh_gssapi_init_ctx(ctxt,
126 kex->gss_deleg_creds, token_ptr, &send_tok,
127 &ret_flags);
1c14df9e 128
129 if (GSS_ERROR(maj_status)) {
34fee935 130 if (send_tok.length != 0) {
88928908 131 packet_start(SSH2_MSG_KEXGSS_CONTINUE);
132 packet_put_string(send_tok.value,
34fee935 133 send_tok.length);
134 }
1c14df9e 135 fatal("gss_init_context failed");
88928908 136 }
1c14df9e 137
138 /* If we've got an old receive buffer get rid of it */
139 if (token_ptr != GSS_C_NO_BUFFER)
34fee935 140 xfree(recv_tok.value);
141
1c14df9e 142 if (maj_status == GSS_S_COMPLETE) {
143 /* If mutual state flag is not true, kex fails */
34fee935 144 if (!(ret_flags & GSS_C_MUTUAL_FLAG))
1c14df9e 145 fatal("Mutual authentication failed");
34fee935 146
1c14df9e 147 /* If integ avail flag is not true kex fails */
34fee935 148 if (!(ret_flags & GSS_C_INTEG_FLAG))
1c14df9e 149 fatal("Integrity check failed");
1c14df9e 150 }
34fee935 151
152 /*
153 * If we have data to send, then the last message that we
154 * received cannot have been a 'complete'.
155 */
156 if (send_tok.length != 0) {
1c14df9e 157 if (first) {
158 packet_start(SSH2_MSG_KEXGSS_INIT);
159 packet_put_string(send_tok.value,
34fee935 160 send_tok.length);
1c14df9e 161 packet_put_bignum2(dh->pub_key);
34fee935 162 first = 0;
1c14df9e 163 } else {
164 packet_start(SSH2_MSG_KEXGSS_CONTINUE);
165 packet_put_string(send_tok.value,
34fee935 166 send_tok.length);
1c14df9e 167 }
168 packet_send();
34fee935 169 gss_release_buffer(&min_status, &send_tok);
170
171 /* If we've sent them data, they should reply */
172 do {
173 type = packet_read();
174 if (type == SSH2_MSG_KEXGSS_HOSTKEY) {
175 debug("Received KEXGSS_HOSTKEY");
176 if (serverhostkey)
177 fatal("Server host key received more than once");
178 serverhostkey =
179 packet_get_string(&slen);
180 }
181 } while (type == SSH2_MSG_KEXGSS_HOSTKEY);
1c14df9e 182
1c14df9e 183 switch (type) {
1c14df9e 184 case SSH2_MSG_KEXGSS_CONTINUE:
185 debug("Received GSSAPI_CONTINUE");
186 if (maj_status == GSS_S_COMPLETE)
187 fatal("GSSAPI Continue received from server when complete");
34fee935 188 recv_tok.value = packet_get_string(&strlen);
189 recv_tok.length = strlen;
1c14df9e 190 break;
191 case SSH2_MSG_KEXGSS_COMPLETE:
192 debug("Received GSSAPI_COMPLETE");
34fee935 193 packet_get_bignum2(dh_server_pub);
194 msg_tok.value = packet_get_string(&strlen);
195 msg_tok.length = strlen;
1c14df9e 196
197 /* Is there a token included? */
198 if (packet_get_char()) {
199 recv_tok.value=
88928908 200 packet_get_string(&strlen);
34fee935 201 recv_tok.length = strlen;
1c14df9e 202 /* If we're already complete - protocol error */
203 if (maj_status == GSS_S_COMPLETE)
204 packet_disconnect("Protocol error: received token when complete");
34fee935 205 } else {
206 /* No token included */
207 if (maj_status != GSS_S_COMPLETE)
208 packet_disconnect("Protocol error: did not receive final token");
1c14df9e 209 }
210 break;
88928908 211 case SSH2_MSG_KEXGSS_ERROR:
212 debug("Received Error");
34fee935 213 maj_status = packet_get_int();
214 min_status = packet_get_int();
215 msg = packet_get_string(NULL);
216 lang = packet_get_string(NULL);
2e437378 217 fatal("GSSAPI Error: \n%.400s",msg);
1c14df9e 218 default:
219 packet_disconnect("Protocol error: didn't expect packet type %d",
220 type);
221 }
34fee935 222 token_ptr = &recv_tok;
88928908 223 } else {
224 /* No data, and not complete */
34fee935 225 if (maj_status != GSS_S_COMPLETE)
88928908 226 fatal("Not complete, and no token output");
1c14df9e 227 }
34fee935 228 } while (maj_status & GSS_S_CONTINUE_NEEDED);
229
230 /*
231 * We _must_ have received a COMPLETE message in reply from the
232 * server, which will have set dh_server_pub and msg_tok
233 */
234
235 if (type != SSH2_MSG_KEXGSS_COMPLETE)
236 fatal("Didn't receive a SSH2_MSG_KEXGSS_COMPLETE when I expected it");
237
1c14df9e 238 /* Check f in range [1, p-1] */
34fee935 239 if (!dh_pub_is_valid(dh, dh_server_pub))
240 packet_disconnect("bad server public DH value");
1c14df9e 241
34fee935 242 /* compute K=f^x mod p */
243 klen = DH_size(dh);
244 kbuf = xmalloc(klen);
245 kout = DH_compute_key(kbuf, dh_server_pub, dh);
246
247 shared_secret = BN_new();
248 BN_bin2bn(kbuf,kout, shared_secret);
249 memset(kbuf, 0, klen);
250 xfree(kbuf);
251
2e437378 252 switch (kex->kex_type) {
253 case KEX_GSS_GRP1_SHA1:
254 case KEX_GSS_GRP14_SHA1:
255 kex_dh_hash( kex->client_version_string,
256 kex->server_version_string,
257 buffer_ptr(&kex->my), buffer_len(&kex->my),
258 buffer_ptr(&kex->peer), buffer_len(&kex->peer),
259 serverhostkey, slen, /* server host key */
260 dh->pub_key, /* e */
261 dh_server_pub, /* f */
262 shared_secret, /* K */
263 &hash, &hashlen
264 );
265 break;
266 case KEX_GSS_GEX_SHA1:
e00da40d 267 kexgex_hash(
268 kex->evp_md,
269 kex->client_version_string,
34fee935 270 kex->server_version_string,
271 buffer_ptr(&kex->my), buffer_len(&kex->my),
272 buffer_ptr(&kex->peer), buffer_len(&kex->peer),
273 serverhostkey, slen,
274 min, nbits, max,
275 dh->p, dh->g,
276 dh->pub_key,
277 dh_server_pub,
e00da40d 278 shared_secret,
279 &hash, &hashlen
34fee935 280 );
2e437378 281 break;
282 default:
283 fatal("%s: Unexpected KEX type %d", __func__, kex->kex_type);
284 }
34fee935 285
286 gssbuf.value = hash;
e00da40d 287 gssbuf.length = hashlen;
34fee935 288
2e437378 289 /* Verify that the hash matches the MIC we just got. */
34fee935 290 if (GSS_ERROR(ssh_gssapi_checkmic(ctxt, &gssbuf, &msg_tok)))
291 packet_disconnect("Hash's MIC didn't verify");
292
293 xfree(msg_tok.value);
294
295 DH_free(dh);
296 if (serverhostkey)
297 xfree(serverhostkey);
298 BN_clear_free(dh_server_pub);
299
300 /* save session id */
301 if (kex->session_id == NULL) {
e00da40d 302 kex->session_id_len = hashlen;
34fee935 303 kex->session_id = xmalloc(kex->session_id_len);
304 memcpy(kex->session_id, hash, kex->session_id_len);
305 }
306
307 if (gss_kex_context == NULL)
308 gss_kex_context = ctxt;
309 else
310 ssh_gssapi_delete_ctx(&ctxt);
311
e00da40d 312 kex_derive_keys(kex, hash, hashlen, shared_secret);
1c14df9e 313 BN_clear_free(shared_secret);
34fee935 314 kex_finish(kex);
1c14df9e 315}
316
317#endif /* GSSAPI */
This page took 0.102912 seconds and 5 git commands to generate.