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