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