]> andersk Git - gssapi-openssh.git/blame_incremental - openssh/gss-genr.c
The man2html from jbasney on pkilab2 works whereas the standard one doesn't.
[gssapi-openssh.git] / openssh / gss-genr.c
... / ...
CommitLineData
1/* $OpenBSD: gss-genr.c,v 1.20 2009/06/22 05:39:28 dtucker Exp $ */
2
3/*
4 * Copyright (c) 2001-2009 Simon Wilkinson. All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR `AS IS'' AND ANY EXPRESS OR
16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 */
26
27#include "includes.h"
28
29#ifdef GSSAPI
30
31#include <sys/types.h>
32#include <sys/param.h>
33
34#include <stdarg.h>
35#include <string.h>
36#include <unistd.h>
37
38#include "xmalloc.h"
39#include "buffer.h"
40#include "log.h"
41#include "canohost.h"
42#include "ssh2.h"
43#include "cipher.h"
44#include "key.h"
45#include "kex.h"
46#include <openssl/evp.h>
47
48#include "ssh-gss.h"
49
50extern u_char *session_id2;
51extern u_int session_id2_len;
52
53typedef struct {
54 char *encoded;
55 gss_OID oid;
56} ssh_gss_kex_mapping;
57
58/*
59 * XXX - It would be nice to find a more elegant way of handling the
60 * XXX passing of the key exchange context to the userauth routines
61 */
62
63Gssctxt *gss_kex_context = NULL;
64
65static ssh_gss_kex_mapping *gss_enc2oid = NULL;
66
67int
68ssh_gssapi_oid_table_ok() {
69 return (gss_enc2oid != NULL);
70}
71
72/*
73 * Return a list of the gss-group1-sha1 mechanisms supported by this program
74 *
75 * We test mechanisms to ensure that we can use them, to avoid starting
76 * a key exchange with a bad mechanism
77 */
78
79char *
80ssh_gssapi_client_mechanisms(const char *host, const char *client) {
81 gss_OID_set gss_supported;
82 OM_uint32 min_status;
83
84 if (GSS_ERROR(gss_indicate_mechs(&min_status, &gss_supported)))
85 return NULL;
86
87 return(ssh_gssapi_kex_mechs(gss_supported, ssh_gssapi_check_mechanism,
88 host, client));
89}
90
91char *
92ssh_gssapi_kex_mechs(gss_OID_set gss_supported, ssh_gssapi_check_fn *check,
93 const char *host, const char *client) {
94 Buffer buf;
95 size_t i;
96 int oidpos, enclen;
97 char *mechs, *encoded;
98 u_char digest[EVP_MAX_MD_SIZE];
99 char deroid[2];
100 const EVP_MD *evp_md = EVP_md5();
101 EVP_MD_CTX md;
102
103 if (gss_enc2oid != NULL) {
104 for (i = 0; gss_enc2oid[i].encoded != NULL; i++)
105 xfree(gss_enc2oid[i].encoded);
106 xfree(gss_enc2oid);
107 }
108
109 gss_enc2oid = xmalloc(sizeof(ssh_gss_kex_mapping) *
110 (gss_supported->count + 1));
111
112 buffer_init(&buf);
113
114 oidpos = 0;
115 for (i = 0; i < gss_supported->count; i++) {
116 if (gss_supported->elements[i].length < 128 &&
117 (*check)(NULL, &(gss_supported->elements[i]), host, client)) {
118
119 deroid[0] = SSH_GSS_OIDTYPE;
120 deroid[1] = gss_supported->elements[i].length;
121
122 EVP_DigestInit(&md, evp_md);
123 EVP_DigestUpdate(&md, deroid, 2);
124 EVP_DigestUpdate(&md,
125 gss_supported->elements[i].elements,
126 gss_supported->elements[i].length);
127 EVP_DigestFinal(&md, digest, NULL);
128
129 encoded = xmalloc(EVP_MD_size(evp_md) * 2);
130 enclen = __b64_ntop(digest, EVP_MD_size(evp_md),
131 encoded, EVP_MD_size(evp_md) * 2);
132
133 if (oidpos != 0)
134 buffer_put_char(&buf, ',');
135
136 buffer_append(&buf, KEX_GSS_GEX_SHA1_ID,
137 sizeof(KEX_GSS_GEX_SHA1_ID) - 1);
138 buffer_append(&buf, encoded, enclen);
139 buffer_put_char(&buf, ',');
140 buffer_append(&buf, KEX_GSS_GRP1_SHA1_ID,
141 sizeof(KEX_GSS_GRP1_SHA1_ID) - 1);
142 buffer_append(&buf, encoded, enclen);
143 buffer_put_char(&buf, ',');
144 buffer_append(&buf, KEX_GSS_GRP14_SHA1_ID,
145 sizeof(KEX_GSS_GRP14_SHA1_ID) - 1);
146 buffer_append(&buf, encoded, enclen);
147
148 gss_enc2oid[oidpos].oid = &(gss_supported->elements[i]);
149 gss_enc2oid[oidpos].encoded = encoded;
150 oidpos++;
151 }
152 }
153 gss_enc2oid[oidpos].oid = NULL;
154 gss_enc2oid[oidpos].encoded = NULL;
155
156 buffer_put_char(&buf, '\0');
157
158 mechs = xmalloc(buffer_len(&buf));
159 buffer_get(&buf, mechs, buffer_len(&buf));
160 buffer_free(&buf);
161
162 if (strlen(mechs) == 0) {
163 xfree(mechs);
164 mechs = NULL;
165 }
166
167 return (mechs);
168}
169
170gss_OID
171ssh_gssapi_id_kex(Gssctxt *ctx, char *name, int kex_type) {
172 int i = 0;
173
174 switch (kex_type) {
175 case KEX_GSS_GRP1_SHA1:
176 if (strlen(name) < sizeof(KEX_GSS_GRP1_SHA1_ID))
177 return GSS_C_NO_OID;
178 name += sizeof(KEX_GSS_GRP1_SHA1_ID) - 1;
179 break;
180 case KEX_GSS_GRP14_SHA1:
181 if (strlen(name) < sizeof(KEX_GSS_GRP14_SHA1_ID))
182 return GSS_C_NO_OID;
183 name += sizeof(KEX_GSS_GRP14_SHA1_ID) - 1;
184 break;
185 case KEX_GSS_GEX_SHA1:
186 if (strlen(name) < sizeof(KEX_GSS_GEX_SHA1_ID))
187 return GSS_C_NO_OID;
188 name += sizeof(KEX_GSS_GEX_SHA1_ID) - 1;
189 break;
190 default:
191 return GSS_C_NO_OID;
192 }
193
194 while (gss_enc2oid[i].encoded != NULL &&
195 strcmp(name, gss_enc2oid[i].encoded) != 0)
196 i++;
197
198 if (gss_enc2oid[i].oid != NULL && ctx != NULL)
199 ssh_gssapi_set_oid(ctx, gss_enc2oid[i].oid);
200
201 return gss_enc2oid[i].oid;
202}
203
204/* Check that the OID in a data stream matches that in the context */
205int
206ssh_gssapi_check_oid(Gssctxt *ctx, void *data, size_t len)
207{
208 return (ctx != NULL && ctx->oid != GSS_C_NO_OID &&
209 ctx->oid->length == len &&
210 memcmp(ctx->oid->elements, data, len) == 0);
211}
212
213/* Set the contexts OID from a data stream */
214void
215ssh_gssapi_set_oid_data(Gssctxt *ctx, void *data, size_t len)
216{
217 if (ctx->oid != GSS_C_NO_OID) {
218 xfree(ctx->oid->elements);
219 xfree(ctx->oid);
220 }
221 ctx->oid = xmalloc(sizeof(gss_OID_desc));
222 ctx->oid->length = len;
223 ctx->oid->elements = xmalloc(len);
224 memcpy(ctx->oid->elements, data, len);
225}
226
227/* Set the contexts OID */
228void
229ssh_gssapi_set_oid(Gssctxt *ctx, gss_OID oid)
230{
231 ssh_gssapi_set_oid_data(ctx, oid->elements, oid->length);
232}
233
234/* All this effort to report an error ... */
235void
236ssh_gssapi_error(Gssctxt *ctxt)
237{
238 char *s;
239
240 s = ssh_gssapi_last_error(ctxt, NULL, NULL);
241 debug("%s", s);
242 xfree(s);
243}
244
245char *
246ssh_gssapi_last_error(Gssctxt *ctxt, OM_uint32 *major_status,
247 OM_uint32 *minor_status)
248{
249 OM_uint32 lmin;
250 gss_buffer_desc msg = GSS_C_EMPTY_BUFFER;
251 OM_uint32 ctx;
252 Buffer b;
253 char *ret;
254
255 buffer_init(&b);
256
257 if (major_status != NULL)
258 *major_status = ctxt->major;
259 if (minor_status != NULL)
260 *minor_status = ctxt->minor;
261
262 ctx = 0;
263 /* The GSSAPI error */
264 do {
265 gss_display_status(&lmin, ctxt->major,
266 GSS_C_GSS_CODE, ctxt->oid, &ctx, &msg);
267
268 buffer_append(&b, msg.value, msg.length);
269 buffer_put_char(&b, '\n');
270
271 gss_release_buffer(&lmin, &msg);
272 } while (ctx != 0);
273
274 /* The mechanism specific error */
275 do {
276 gss_display_status(&lmin, ctxt->minor,
277 GSS_C_MECH_CODE, ctxt->oid, &ctx, &msg);
278
279 buffer_append(&b, msg.value, msg.length);
280 buffer_put_char(&b, '\n');
281
282 gss_release_buffer(&lmin, &msg);
283 } while (ctx != 0);
284
285 buffer_put_char(&b, '\0');
286 ret = xmalloc(buffer_len(&b));
287 buffer_get(&b, ret, buffer_len(&b));
288 buffer_free(&b);
289 return (ret);
290}
291
292/*
293 * Initialise our GSSAPI context. We use this opaque structure to contain all
294 * of the data which both the client and server need to persist across
295 * {accept,init}_sec_context calls, so that when we do it from the userauth
296 * stuff life is a little easier
297 */
298void
299ssh_gssapi_build_ctx(Gssctxt **ctx)
300{
301 *ctx = xcalloc(1, sizeof (Gssctxt));
302 (*ctx)->context = GSS_C_NO_CONTEXT;
303 (*ctx)->name = GSS_C_NO_NAME;
304 (*ctx)->oid = GSS_C_NO_OID;
305 (*ctx)->creds = GSS_C_NO_CREDENTIAL;
306 (*ctx)->client = GSS_C_NO_NAME;
307 (*ctx)->client_creds = GSS_C_NO_CREDENTIAL;
308}
309
310/* Delete our context, providing it has been built correctly */
311void
312ssh_gssapi_delete_ctx(Gssctxt **ctx)
313{
314#if !defined(MECHGLUE)
315 OM_uint32 ms;
316#endif
317
318 if ((*ctx) == NULL)
319 return;
320#if !defined(MECHGLUE) /* mechglue has some memory management issues */
321 if ((*ctx)->context != GSS_C_NO_CONTEXT)
322 gss_delete_sec_context(&ms, &(*ctx)->context, GSS_C_NO_BUFFER);
323 if ((*ctx)->name != GSS_C_NO_NAME)
324 gss_release_name(&ms, &(*ctx)->name);
325 if ((*ctx)->oid != GSS_C_NO_OID) {
326 xfree((*ctx)->oid->elements);
327 xfree((*ctx)->oid);
328 (*ctx)->oid = GSS_C_NO_OID;
329 }
330 if ((*ctx)->creds != GSS_C_NO_CREDENTIAL)
331 gss_release_cred(&ms, &(*ctx)->creds);
332 if ((*ctx)->client != GSS_C_NO_NAME)
333 gss_release_name(&ms, &(*ctx)->client);
334 if ((*ctx)->client_creds != GSS_C_NO_CREDENTIAL)
335 gss_release_cred(&ms, &(*ctx)->client_creds);
336#endif
337
338 xfree(*ctx);
339 *ctx = NULL;
340}
341
342/*
343 * Wrapper to init_sec_context
344 * Requires that the context contains:
345 * oid
346 * server name (from ssh_gssapi_import_name)
347 */
348OM_uint32
349ssh_gssapi_init_ctx(Gssctxt *ctx, int deleg_creds, gss_buffer_desc *recv_tok,
350 gss_buffer_desc* send_tok, OM_uint32 *flags)
351{
352 int deleg_flag = 0;
353
354 if (deleg_creds) {
355 deleg_flag = GSS_C_DELEG_FLAG;
356 debug("Delegating credentials");
357 }
358
359 ctx->major = gss_init_sec_context(&ctx->minor,
360 ctx->client_creds, &ctx->context, ctx->name, ctx->oid,
361 GSS_C_MUTUAL_FLAG | GSS_C_INTEG_FLAG | deleg_flag,
362 0, NULL, recv_tok, NULL, send_tok, flags, NULL);
363
364 if (GSS_ERROR(ctx->major))
365 ssh_gssapi_error(ctx);
366
367 return (ctx->major);
368}
369
370/* Create a service name for the given host */
371OM_uint32
372ssh_gssapi_import_name(Gssctxt *ctx, const char *host)
373{
374 gss_buffer_desc gssbuf;
375 char *xhost;
376 char *val;
377
378 /* Make a copy of the host name, in case it was returned by a
379 * previous call to gethostbyname(). */
380 xhost = xstrdup(host);
381
382 /* Make sure we have the FQDN. Some GSSAPI implementations don't do
383 * this for us themselves */
384 resolve_localhost(&xhost);
385
386 xasprintf(&val, "host@%s", xhost);
387 gssbuf.value = val;
388 gssbuf.length = strlen(gssbuf.value);
389
390 if ((ctx->major = gss_import_name(&ctx->minor,
391 &gssbuf, GSS_C_NT_HOSTBASED_SERVICE, &ctx->name)))
392 ssh_gssapi_error(ctx);
393
394 xfree(xhost);
395 xfree(gssbuf.value);
396 return (ctx->major);
397}
398
399OM_uint32
400ssh_gssapi_client_identity(Gssctxt *ctx, const char *name)
401{
402 gss_buffer_desc gssbuf;
403 gss_name_t gssname;
404 OM_uint32 status;
405 gss_OID_set oidset;
406
407 gssbuf.value = (void *) name;
408 gssbuf.length = strlen(gssbuf.value);
409
410 gss_create_empty_oid_set(&status, &oidset);
411 gss_add_oid_set_member(&status, ctx->oid, &oidset);
412
413 ctx->major = gss_import_name(&ctx->minor, &gssbuf,
414 GSS_C_NT_USER_NAME, &gssname);
415
416 if (!ctx->major)
417 ctx->major = gss_acquire_cred(&ctx->minor,
418 gssname, 0, oidset, GSS_C_INITIATE,
419 &ctx->client_creds, NULL, NULL);
420
421 gss_release_name(&status, &gssname);
422 gss_release_oid_set(&status, &oidset);
423
424 if (ctx->major)
425 ssh_gssapi_error(ctx);
426
427 return(ctx->major);
428}
429
430OM_uint32
431ssh_gssapi_sign(Gssctxt *ctx, gss_buffer_t buffer, gss_buffer_t hash)
432{
433 if (ctx == NULL)
434 return -1;
435
436 if ((ctx->major = gss_get_mic(&ctx->minor, ctx->context,
437 GSS_C_QOP_DEFAULT, buffer, hash)))
438 ssh_gssapi_error(ctx);
439
440 return (ctx->major);
441}
442
443/* Priviledged when used by server */
444OM_uint32
445ssh_gssapi_checkmic(Gssctxt *ctx, gss_buffer_t gssbuf, gss_buffer_t gssmic)
446{
447 if (ctx == NULL)
448 return -1;
449
450 ctx->major = gss_verify_mic(&ctx->minor, ctx->context,
451 gssbuf, gssmic, NULL);
452
453 return (ctx->major);
454}
455
456void
457ssh_gssapi_buildmic(Buffer *b, const char *user, const char *service,
458 const char *context)
459{
460 buffer_init(b);
461 buffer_put_string(b, session_id2, session_id2_len);
462 buffer_put_char(b, SSH2_MSG_USERAUTH_REQUEST);
463 buffer_put_cstring(b, user);
464 buffer_put_cstring(b, service);
465 buffer_put_cstring(b, context);
466}
467
468int
469ssh_gssapi_check_mechanism(Gssctxt **ctx, gss_OID oid, const char *host,
470 const char *client)
471{
472 gss_buffer_desc token = GSS_C_EMPTY_BUFFER;
473 OM_uint32 major, minor;
474 gss_OID_desc spnego_oid = {6, (void *)"\x2B\x06\x01\x05\x05\x02"};
475 Gssctxt *intctx = NULL;
476
477 if (ctx == NULL)
478 ctx = &intctx;
479
480 /* RFC 4462 says we MUST NOT do SPNEGO */
481 if (oid->length == spnego_oid.length &&
482 (memcmp(oid->elements, spnego_oid.elements, oid->length) == 0))
483 return 0; /* false */
484
485 ssh_gssapi_build_ctx(ctx);
486 ssh_gssapi_set_oid(*ctx, oid);
487 major = ssh_gssapi_import_name(*ctx, host);
488
489 if (!GSS_ERROR(major) && client)
490 major = ssh_gssapi_client_identity(*ctx, client);
491
492 if (!GSS_ERROR(major)) {
493 major = ssh_gssapi_init_ctx(*ctx, 0, GSS_C_NO_BUFFER, &token,
494 NULL);
495 gss_release_buffer(&minor, &token);
496 if ((*ctx)->context != GSS_C_NO_CONTEXT)
497 gss_delete_sec_context(&minor, &(*ctx)->context,
498 GSS_C_NO_BUFFER);
499 }
500
501 if (GSS_ERROR(major) || intctx != NULL)
502 ssh_gssapi_delete_ctx(ctx);
503
504 return (!GSS_ERROR(major));
505}
506
507int
508ssh_gssapi_credentials_updated(Gssctxt *ctxt) {
509 static gss_name_t saved_name = GSS_C_NO_NAME;
510 static OM_uint32 saved_lifetime = 0;
511 static gss_OID saved_mech = GSS_C_NO_OID;
512 static gss_name_t name;
513 static OM_uint32 last_call = 0;
514 OM_uint32 lifetime, now, major, minor;
515 int equal;
516
517 now = time(NULL);
518
519 if (ctxt) {
520 debug("Rekey has happened - updating saved versions");
521
522 if (saved_name != GSS_C_NO_NAME)
523 gss_release_name(&minor, &saved_name);
524
525 major = gss_inquire_cred(&minor, GSS_C_NO_CREDENTIAL,
526 &saved_name, &saved_lifetime, NULL, NULL);
527
528 if (!GSS_ERROR(major)) {
529 saved_mech = ctxt->oid;
530 saved_lifetime+= now;
531 } else {
532 /* Handle the error */
533 }
534 return 0;
535 }
536
537 if (now - last_call < 10)
538 return 0;
539
540 last_call = now;
541
542 if (saved_mech == GSS_C_NO_OID)
543 return 0;
544
545 major = gss_inquire_cred(&minor, GSS_C_NO_CREDENTIAL,
546 &name, &lifetime, NULL, NULL);
547 if (major == GSS_S_CREDENTIALS_EXPIRED)
548 return 0;
549 else if (GSS_ERROR(major))
550 return 0;
551
552 major = gss_compare_name(&minor, saved_name, name, &equal);
553 gss_release_name(&minor, &name);
554 if (GSS_ERROR(major))
555 return 0;
556
557 if (equal && (saved_lifetime < lifetime + now - 10))
558 return 1;
559
560 return 0;
561}
562
563#endif /* GSSAPI */
This page took 0.050332 seconds and 5 git commands to generate.