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