]> andersk Git - gssapi-openssh.git/blame - openssh/kexgsss.c
merged OPENSSH_5_2P1_GSSAPI_20090831 to GPT-branch
[gssapi-openssh.git] / openssh / kexgsss.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
2e437378 29#include <string.h>
30
1c14df9e 31#include <openssl/crypto.h>
32#include <openssl/bn.h>
33
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"
1c14df9e 43#include "ssh-gss.h"
44#include "monitor_wrap.h"
46201ce0 45#include "servconf.h"
88928908 46
47static void kex_gss_send_error(Gssctxt *ctxt);
46201ce0 48extern ServerOptions options;
1c14df9e 49
50void
51kexgss_server(Kex *kex)
52{
1c14df9e 53 OM_uint32 maj_status, min_status;
54
34fee935 55 /*
56 * Some GSSAPI implementations use the input value of ret_flags (an
1c14df9e 57 * output variable) as a means of triggering mechanism specific
58 * features. Initializing it to zero avoids inadvertently
34fee935 59 * activating this non-standard behaviour.
60 */
1c14df9e 61
62 OM_uint32 ret_flags = 0;
34fee935 63 gss_buffer_desc gssbuf, recv_tok, msg_tok;
64 gss_buffer_desc send_tok = GSS_C_EMPTY_BUFFER;
1c14df9e 65 Gssctxt *ctxt = NULL;
e00da40d 66 u_int slen, klen, kout, hashlen;
67 u_char *kbuf, *hash;
34fee935 68 DH *dh;
69 int min = -1, max = -1, nbits = -1;
70 BIGNUM *shared_secret = NULL;
71 BIGNUM *dh_client_pub = NULL;
72 int type = 0;
88928908 73 gss_OID oid;
46201ce0 74 char *mechs;
2e437378 75
1c14df9e 76 /* Initialise GSSAPI */
77
34fee935 78 /* If we're rekeying, privsep means that some of the private structures
79 * in the GSSAPI code are no longer available. This kludges them back
2e437378 80 * into life
34fee935 81 */
82 if (!ssh_gssapi_oid_table_ok())
46201ce0 83 if ((mechs = ssh_gssapi_server_mechanisms()))
84 xfree(mechs);
34fee935 85
86 debug2("%s: Identifying %s", __func__, kex->name);
2e437378 87 oid = ssh_gssapi_id_kex(NULL, kex->name, kex->kex_type);
88 if (oid == GSS_C_NO_OID)
34fee935 89 fatal("Unknown gssapi mechanism");
90
91 debug2("%s: Acquiring credentials", __func__);
92
93 if (GSS_ERROR(PRIVSEP(ssh_gssapi_server_ctx(&ctxt, oid)))) {
88928908 94 kex_gss_send_error(ctxt);
2e437378 95 fatal("Unable to acquire credentials for the server");
96 }
97
98 switch (kex->kex_type) {
99 case KEX_GSS_GRP1_SHA1:
100 dh = dh_new_group1();
101 break;
102 case KEX_GSS_GRP14_SHA1:
103 dh = dh_new_group14();
104 break;
105 case KEX_GSS_GEX_SHA1:
34fee935 106 debug("Doing group exchange");
107 packet_read_expect(SSH2_MSG_KEXGSS_GROUPREQ);
108 min = packet_get_int();
109 nbits = packet_get_int();
110 max = packet_get_int();
111 min = MAX(DH_GRP_MIN, min);
112 max = MIN(DH_GRP_MAX, max);
113 packet_check_eom();
114 if (max < min || nbits < min || max < nbits)
115 fatal("GSS_GEX, bad parameters: %d !< %d !< %d",
116 min, nbits, max);
117 dh = PRIVSEP(choose_dh(min, nbits, max));
118 if (dh == NULL)
119 packet_disconnect("Protocol error: no matching group found");
120
121 packet_start(SSH2_MSG_KEXGSS_GROUP);
122 packet_put_bignum2(dh->p);
123 packet_put_bignum2(dh->g);
124 packet_send();
125
126 packet_write_wait();
2e437378 127 break;
128 default:
129 fatal("%s: Unexpected KEX type %d", __func__, kex->kex_type);
34fee935 130 }
2e437378 131
34fee935 132 dh_gen_key(dh, kex->we_need * 8);
133
1c14df9e 134 do {
135 debug("Wait SSH2_MSG_GSSAPI_INIT");
136 type = packet_read();
137 switch(type) {
138 case SSH2_MSG_KEXGSS_INIT:
34fee935 139 if (dh_client_pub != NULL)
140 fatal("Received KEXGSS_INIT after initialising");
141 recv_tok.value = packet_get_string(&slen);
142 recv_tok.length = slen;
143
144 if ((dh_client_pub = BN_new()) == NULL)
145 fatal("dh_client_pub == NULL");
146
147 packet_get_bignum2(dh_client_pub);
148
149 /* Send SSH_MSG_KEXGSS_HOSTKEY here, if we want */
1c14df9e 150 break;
151 case SSH2_MSG_KEXGSS_CONTINUE:
34fee935 152 recv_tok.value = packet_get_string(&slen);
153 recv_tok.length = slen;
1c14df9e 154 break;
155 default:
34fee935 156 packet_disconnect(
157 "Protocol error: didn't expect packet type %d",
158 type);
1c14df9e 159 }
1c14df9e 160
34fee935 161 maj_status = PRIVSEP(ssh_gssapi_accept_ctx(ctxt, &recv_tok,
162 &send_tok, &ret_flags));
163
164 xfree(recv_tok.value);
165
166 if (maj_status != GSS_S_COMPLETE && send_tok.length == 0)
88928908 167 fatal("Zero length token output when incomplete");
88928908 168
169 if (dh_client_pub == NULL)
170 fatal("No client public key");
1c14df9e 171
172 if (maj_status & GSS_S_CONTINUE_NEEDED) {
173 debug("Sending GSSAPI_CONTINUE");
174 packet_start(SSH2_MSG_KEXGSS_CONTINUE);
46201ce0 175 packet_put_string((char *)send_tok.value, send_tok.length);
1c14df9e 176 packet_send();
1c14df9e 177 gss_release_buffer(&min_status, &send_tok);
178 }
179 } while (maj_status & GSS_S_CONTINUE_NEEDED);
180
181 if (GSS_ERROR(maj_status)) {
88928908 182 kex_gss_send_error(ctxt);
34fee935 183 if (send_tok.length > 0) {
88928908 184 packet_start(SSH2_MSG_KEXGSS_CONTINUE);
46201ce0 185 packet_put_string((char *)send_tok.value, send_tok.length);
88928908 186 packet_send();
34fee935 187 }
188 packet_disconnect("GSSAPI Key Exchange handshake failed");
1c14df9e 189 }
34fee935 190
88928908 191 if (!(ret_flags & GSS_C_MUTUAL_FLAG))
34fee935 192 fatal("Mutual Authentication flag wasn't set");
193
88928908 194 if (!(ret_flags & GSS_C_INTEG_FLAG))
34fee935 195 fatal("Integrity flag wasn't set");
1c14df9e 196
34fee935 197 if (!dh_pub_is_valid(dh, dh_client_pub))
198 packet_disconnect("bad client public DH value");
1c14df9e 199
34fee935 200 klen = DH_size(dh);
201 kbuf = xmalloc(klen);
202 kout = DH_compute_key(kbuf, dh_client_pub, dh);
46201ce0 203 if (kout < 0)
204 fatal("DH_compute_key: failed");
1c14df9e 205
206 shared_secret = BN_new();
46201ce0 207 if (shared_secret == NULL)
208 fatal("kexgss_server: BN_new failed");
209
210 if (BN_bin2bn(kbuf, kout, shared_secret) == NULL)
211 fatal("kexgss_server: BN_bin2bn failed");
212
1c14df9e 213 memset(kbuf, 0, klen);
214 xfree(kbuf);
34fee935 215
2e437378 216 switch (kex->kex_type) {
217 case KEX_GSS_GRP1_SHA1:
218 case KEX_GSS_GRP14_SHA1:
219 kex_dh_hash(
220 kex->client_version_string, kex->server_version_string,
221 buffer_ptr(&kex->peer), buffer_len(&kex->peer),
222 buffer_ptr(&kex->my), buffer_len(&kex->my),
223 NULL, 0, /* Change this if we start sending host keys */
224 dh_client_pub, dh->pub_key, shared_secret,
225 &hash, &hashlen
226 );
227 break;
228 case KEX_GSS_GEX_SHA1:
e00da40d 229 kexgex_hash(
230 kex->evp_md,
34fee935 231 kex->client_version_string, kex->server_version_string,
232 buffer_ptr(&kex->peer), buffer_len(&kex->peer),
233 buffer_ptr(&kex->my), buffer_len(&kex->my),
234 NULL, 0,
235 min, nbits, max,
236 dh->p, dh->g,
237 dh_client_pub,
238 dh->pub_key,
e00da40d 239 shared_secret,
240 &hash, &hashlen
34fee935 241 );
2e437378 242 break;
243 default:
244 fatal("%s: Unexpected KEX type %d", __func__, kex->kex_type);
34fee935 245 }
2e437378 246
46201ce0 247 BN_clear_free(dh_client_pub);
34fee935 248
1c14df9e 249 if (kex->session_id == NULL) {
e00da40d 250 kex->session_id_len = hashlen;
1c14df9e 251 kex->session_id = xmalloc(kex->session_id_len);
252 memcpy(kex->session_id, hash, kex->session_id_len);
253 }
34fee935 254
1c14df9e 255 gssbuf.value = hash;
e00da40d 256 gssbuf.length = hashlen;
34fee935 257
258 if (GSS_ERROR(PRIVSEP(ssh_gssapi_sign(ctxt,&gssbuf,&msg_tok))))
88928908 259 fatal("Couldn't get MIC");
34fee935 260
1c14df9e 261 packet_start(SSH2_MSG_KEXGSS_COMPLETE);
262 packet_put_bignum2(dh->pub_key);
263 packet_put_string((char *)msg_tok.value,msg_tok.length);
264
34fee935 265 if (send_tok.length != 0) {
1c14df9e 266 packet_put_char(1); /* true */
34fee935 267 packet_put_string((char *)send_tok.value, send_tok.length);
1c14df9e 268 } else {
269 packet_put_char(0); /* false */
270 }
34fee935 271 packet_send();
272
273 gss_release_buffer(&min_status, &send_tok);
274 gss_release_buffer(&min_status, &msg_tok);
275
276 if (gss_kex_context == NULL)
277 gss_kex_context = ctxt;
278 else
279 ssh_gssapi_delete_ctx(&ctxt);
280
1c14df9e 281 DH_free(dh);
282
e00da40d 283 kex_derive_keys(kex, hash, hashlen, shared_secret);
1c14df9e 284 BN_clear_free(shared_secret);
285 kex_finish(kex);
46201ce0 286
287 /* If this was a rekey, then save out any delegated credentials we
288 * just exchanged. */
289 if (options.gss_store_rekey)
290 ssh_gssapi_rekey_creds();
1c14df9e 291}
292
88928908 293static void
294kex_gss_send_error(Gssctxt *ctxt) {
295 char *errstr;
296 OM_uint32 maj,min;
297
298 errstr=PRIVSEP(ssh_gssapi_last_error(ctxt,&maj,&min));
299 if (errstr) {
300 packet_start(SSH2_MSG_KEXGSS_ERROR);
301 packet_put_int(maj);
302 packet_put_int(min);
303 packet_put_cstring(errstr);
304 packet_put_cstring("");
305 packet_send();
306 packet_write_wait();
307 /* XXX - We should probably log the error locally here */
308 xfree(errstr);
309 }
310}
1c14df9e 311#endif /* GSSAPI */
This page took 0.09582 seconds and 5 git commands to generate.