]> andersk Git - gssapi-openssh.git/blame - openssh/gss-genr.c
recommit previous changes to simon's re-organized code
[gssapi-openssh.git] / openssh / gss-genr.c
CommitLineData
5598e598 1/*
b59afbfe 2 * Copyright (c) 2001,2002 Simon Wilkinson. All rights reserved. *
5598e598 3 * Redistribution and use in source and binary forms, with or without
4 * modification, are permitted provided that the following conditions
5 * are met:
6 * 1. Redistributions of source code must retain the above copyright
7 * notice, this list of conditions and the following disclaimer.
8 * 2. Redistributions in binary form must reproduce the above copyright
9 * notice, this list of conditions and the following disclaimer in the
10 * documentation and/or other materials provided with the distribution.
11 *
12 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR `AS IS'' AND ANY EXPRESS OR
13 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
14 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
15 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
16 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
17 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
18 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
19 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
20 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
21 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
22 */
23
24#include "includes.h"
25
26#ifdef GSSAPI
27
28#include "ssh.h"
29#include "ssh2.h"
30#include "xmalloc.h"
31#include "buffer.h"
32#include "bufaux.h"
33#include "packet.h"
34#include "compat.h"
35#include <openssl/evp.h>
36#include "cipher.h"
37#include "kex.h"
38#include "log.h"
905081a4 39#include "compat.h"
b59afbfe 40#include "monitor_wrap.h"
4dd35ca9 41#include "canohost.h"
5598e598 42
43#include <netdb.h>
44
45#include "ssh-gss.h"
46
47/* Assorted globals for tracking the clients identity once they've
48 * authenticated */
49
50gss_buffer_desc gssapi_client_name = {0,NULL}; /* Name of our client */
51gss_cred_id_t gssapi_client_creds = GSS_C_NO_CREDENTIAL; /* Their credentials */
52enum ssh_gss_id gssapi_client_type = GSS_LAST_ENTRY;
53
b59afbfe 54unsigned char ssh1_key_digest[16]; /* used for ssh1 gssapi */
55
5598e598 56/* The mechanism name used in the list below is defined in the internet
57 * draft as the Base 64 encoding of the MD5 hash of the ASN.1 DER encoding
58 * of the underlying GSSAPI mechanism's OID.
59 *
60 * Also from the draft, before considering adding SPNEGO, bear in mind that
61 * "mechanisms ... MUST NOT use SPNEGO as the underlying GSSAPI mechanism"
62 */
63
64/* These must be in the same order as ssh_gss_id, in ssh-gss.h */
65
66ssh_gssapi_mech supported_mechs[]= {
67#ifdef KRB5
68 /* Official OID - 1.2.850.113554.1.2.2 */
69 {"Se3H81ismmOC3OE+FwYCiQ==","Kerberos",
70 {9, "\x2A\x86\x48\x86\xF7\x12\x01\x02\x02"}},
71#endif
72#ifdef GSI
73 /* gssapi_ssleay 1.3.6.1.4.1.3536.1.1 */
74 {"N3+k7/4wGxHyuP8Yxi4RhA==",
75 "GSI",
76 {9, "\x2B\x06\x01\x04\x01\x9B\x50\x01\x01"}
77 },
78#endif /* GSI */
79 {NULL,NULL,{0,0}}
80};
81
82char gssprefix[]=KEX_GSS_SHA1;
83
84/* Return a list of the gss-group1-sha1-x mechanisms supported by this
85 * program.
86 *
87 * We only support the mechanisms that we've indicated in the list above,
88 * but we check that they're supported by the GSSAPI mechanism on the
89 * machine. We also check, before including them in the list, that
90 * we have the necesary information in order to carry out the key exchange
91 * (that is, that the user has credentials, the server's creds are accessible,
92 * etc)
93 *
94 * The way that this is done is fairly nasty, as we do a lot of work that
95 * is then thrown away. This should possibly be implemented with a cache
96 * that stores the results (in an expanded Gssctxt structure), which are
97 * then used by the first calls if that key exchange mechanism is chosen.
98 */
99
100char *
f001f132 101ssh_gssapi_mechanisms(char *host) {
5598e598 102 gss_OID_set supported;
103 OM_uint32 maj_status, min_status;
104 Buffer buf;
105 int i = 0;
106 int present;
d4ab470d 107 int mech_count=0;
5598e598 108 char * mechs;
b59afbfe 109 Gssctxt * ctx = NULL;
5598e598 110
905081a4 111 if (datafellows & SSH_OLD_GSSAPI) return NULL;
112
f001f132 113 gss_indicate_mechs(&min_status, &supported);
5598e598 114
115 buffer_init(&buf);
116
117 do {
118 if ((maj_status=gss_test_oid_set_member(&min_status,
119 &supported_mechs[i].oid,
120 supported,
121 &present))) {
122 present=0;
123 }
124 if (present) {
f001f132 125 if (!GSS_ERROR(ssh_gssapi_client_ctx(&ctx,
126 &supported_mechs[i].oid,
127 host))) {
b59afbfe 128 /* Append gss_group1_sha1_x to our list */
d4ab470d 129 if (++mech_count > 1) {
130 buffer_append(&buf, ",", 1);
131 }
b59afbfe 132 buffer_append(&buf, gssprefix,
133 strlen(gssprefix));
134 buffer_append(&buf,
135 supported_mechs[i].enc_name,
136 strlen(supported_mechs[i].enc_name));
d4ab470d 137 debug("GSSAPI mechanism %s (%s%s) supported",
138 supported_mechs[i].name, gssprefix,
139 supported_mechs[i].enc_name);
140 } else {
141 debug("no credentials for GSSAPI mechanism %s",
142 supported_mechs[i].name);
143 }
144 } else {
145 debug("GSSAPI mechanism %s not supported",
146 supported_mechs[i].name);
147 }
5598e598 148 } while (supported_mechs[++i].name != NULL);
149
150 buffer_put_char(&buf,'\0');
151
152 mechs=xmalloc(buffer_len(&buf));
153 buffer_get(&buf,mechs,buffer_len(&buf));
154 buffer_free(&buf);
155 if (strlen(mechs)==0)
156 return(NULL);
157 else
158 return(mechs);
159}
160
5598e598 161/* Set the contexts OID from a data stream */
162void ssh_gssapi_set_oid_data(Gssctxt *ctx, void *data, size_t len) {
163 if (ctx->oid != GSS_C_NO_OID) {
164 xfree(ctx->oid->elements);
165 xfree(ctx->oid);
166 }
167 ctx->oid=xmalloc(sizeof(gss_OID_desc));
168 ctx->oid->length=len;
169 ctx->oid->elements=xmalloc(len);
170 memcpy(ctx->oid->elements,data,len);
171}
172
173/* Set the contexts OID */
174void ssh_gssapi_set_oid(Gssctxt *ctx, gss_OID oid) {
175 ssh_gssapi_set_oid_data(ctx,oid->elements,oid->length);
176}
177
178/* Find out which GSS type (out of the list we define in ssh-gss.h) a
179 * particular connection is using
180 */
181enum ssh_gss_id ssh_gssapi_get_ctype(Gssctxt *ctxt) {
182 enum ssh_gss_id i=0;
183
c57ffeb1 184 while(supported_mechs[i].name!=NULL) {
185 if (supported_mechs[i].oid.length == ctxt->oid->length &&
186 (memcmp(supported_mechs[i].oid.elements,
187 ctxt->oid->elements,ctxt->oid->length) == 0))
188 return i;
5598e598 189 i++;
190 }
c57ffeb1 191 return(GSS_LAST_ENTRY);
5598e598 192}
193
194/* Set the GSS context's OID to the oid indicated by the given key exchange
195 * name. */
b59afbfe 196gss_OID ssh_gssapi_id_kex(Gssctxt *ctx, char *name) {
5598e598 197 enum ssh_gss_id i=0;
198
199 if (strncmp(name, gssprefix, strlen(gssprefix)-1) !=0) {
b59afbfe 200 return(NULL);
5598e598 201 }
202
203 name+=strlen(gssprefix); /* Move to the start of the MIME string */
204
205 while (supported_mechs[i].name!=NULL &&
206 strcmp(name,supported_mechs[i].enc_name)!=0) {
207 i++;
208 }
209
210 if (supported_mechs[i].name==NULL)
b59afbfe 211 return (NULL);
5598e598 212
b59afbfe 213 if (ctx) ssh_gssapi_set_oid(ctx,&supported_mechs[i].oid);
5598e598 214
d4ab470d 215 debug("using GSSAPI mechanism %s (%s%s)", supported_mechs[i].name,
216 gssprefix, supported_mechs[i].enc_name);
217
b59afbfe 218 return &supported_mechs[i].oid;
5598e598 219}
220
221
222/* All this effort to report an error ... */
f001f132 223void
224ssh_gssapi_error(gss_OID mech, OM_uint32 major_status,
225 OM_uint32 minor_status) {
5598e598 226 OM_uint32 lmaj, lmin;
d4ab470d 227 gss_buffer_desc msg = {0,NULL};
5598e598 228 OM_uint32 ctx;
229
230 ctx = 0;
231 /* The GSSAPI error */
232 do {
f001f132 233 lmaj = gss_display_status(&lmin, major_status,
234 GSS_C_GSS_CODE,
235 mech, &ctx, &msg);
5598e598 236 if (lmaj == GSS_S_COMPLETE) {
237 debug((char *)msg.value);
238 (void) gss_release_buffer(&lmin, &msg);
239 }
240 } while (ctx!=0);
241
242 /* The mechanism specific error */
243 do {
f001f132 244 lmaj = gss_display_status(&lmin, minor_status,
245 GSS_C_MECH_CODE,
246 mech, &ctx, &msg);
5598e598 247 if (lmaj == GSS_S_COMPLETE) {
248 debug((char *)msg.value);
249 (void) gss_release_buffer(&lmin, &msg);
250 }
251 } while (ctx!=0);
252}
253
254/* Initialise our GSSAPI context. We use this opaque structure to contain all
255 * of the data which both the client and server need to persist across
256 * {accept,init}_sec_context calls, so that when we do it from the userauth
257 * stuff life is a little easier
258 */
259void
b59afbfe 260ssh_gssapi_build_ctx(Gssctxt **ctx)
5598e598 261{
b59afbfe 262 *ctx=xmalloc(sizeof (Gssctxt));
263 (*ctx)->context=GSS_C_NO_CONTEXT;
264 (*ctx)->name=GSS_C_NO_NAME;
265 (*ctx)->oid=GSS_C_NO_OID;
266 (*ctx)->creds=GSS_C_NO_CREDENTIAL;
267 (*ctx)->client=GSS_C_NO_NAME;
268 (*ctx)->client_creds=GSS_C_NO_CREDENTIAL;
5598e598 269}
270
271/* Delete our context, providing it has been built correctly */
272void
b59afbfe 273ssh_gssapi_delete_ctx(Gssctxt **ctx)
5598e598 274{
9eeaa28e 275#if !defined(MECHGLUE)
5598e598 276 OM_uint32 ms;
9eeaa28e 277#endif
5598e598 278
b59afbfe 279 /* Return if there's no context */
280 if ((*ctx)==NULL)
281 return;
282
d4ab470d 283#if !defined(MECHGLUE) /* mechglue has some memory management issues */
b59afbfe 284 if ((*ctx)->context != GSS_C_NO_CONTEXT)
285 gss_delete_sec_context(&ms,&(*ctx)->context,GSS_C_NO_BUFFER);
286 if ((*ctx)->name != GSS_C_NO_NAME)
287 gss_release_name(&ms,&(*ctx)->name);
288 if ((*ctx)->oid != GSS_C_NO_OID) {
289 xfree((*ctx)->oid->elements);
290 xfree((*ctx)->oid);
291 (*ctx)->oid = GSS_C_NO_OID;
5598e598 292 }
b59afbfe 293 if ((*ctx)->creds != GSS_C_NO_CREDENTIAL)
294 gss_release_cred(&ms,&(*ctx)->creds);
295 if ((*ctx)->client != GSS_C_NO_NAME)
296 gss_release_name(&ms,&(*ctx)->client);
297 if ((*ctx)->client_creds != GSS_C_NO_CREDENTIAL)
298 gss_release_cred(&ms,&(*ctx)->client_creds);
d4ab470d 299#endif
b59afbfe 300
301 xfree(*ctx);
302 *ctx=NULL;
5598e598 303}
304
305/* Wrapper to init_sec_context
306 * Requires that the context contains:
307 * oid
308 * server name (from ssh_gssapi_import_name)
309 */
310OM_uint32
311ssh_gssapi_init_ctx(Gssctxt *ctx, int deleg_creds, gss_buffer_desc *recv_tok,
312 gss_buffer_desc* send_tok, OM_uint32 *flags)
313{
314 OM_uint32 maj_status, min_status;
315 int deleg_flag = 0;
316
317 if (deleg_creds) {
318 deleg_flag=GSS_C_DELEG_FLAG;
319 debug("Delegating credentials");
320 }
321
322 maj_status=gss_init_sec_context(&min_status,
323 GSS_C_NO_CREDENTIAL, /* def. cred */
324 &ctx->context,
325 ctx->name,
326 ctx->oid,
327 GSS_C_MUTUAL_FLAG |
328 GSS_C_INTEG_FLAG |
329 deleg_flag,
330 0, /* default lifetime */
331 NULL, /* no channel bindings */
332 recv_tok,
333 NULL,
334 send_tok,
335 flags,
336 NULL);
337 ctx->status=maj_status;
338 if (GSS_ERROR(maj_status)) {
6f9f4dab 339 ssh_gssapi_error(ctx->oid,maj_status,min_status);
5598e598 340 }
341 return(maj_status);
342}
343
5598e598 344/* Create a service name for the given host */
345OM_uint32
1c828c5c 346ssh_gssapi_import_name(Gssctxt *ctx, const char *host) {
d4ab470d 347 gss_buffer_desc gssbuf = {0,NULL};
5598e598 348 OM_uint32 maj_status, min_status;
4dd35ca9 349 char *xhost;
5598e598 350
351 /* Make a copy of the host name, in case it was returned by a
352 * previous call to gethostbyname(). */
353 xhost = xstrdup(host);
354
4dd35ca9 355 /* Make sure we have the FQHN. Some GSSAPI implementations don't do
5598e598 356 * this for us themselves */
1df48666 357 resolve_localhost(&xhost);
9e495a24 358
33fd981a 359 gssbuf.length = sizeof("host@")+strlen(xhost);
5598e598 360
361 gssbuf.value = xmalloc(gssbuf.length);
362 if (gssbuf.value == NULL) {
1c828c5c 363 xfree(xhost);
5598e598 364 return(-1);
365 }
33fd981a 366 snprintf(gssbuf.value,gssbuf.length,"host@%s",xhost);
5598e598 367 if ((maj_status=gss_import_name(&min_status,
368 &gssbuf,
369 GSS_C_NT_HOSTBASED_SERVICE,
370 &ctx->name))) {
6f9f4dab 371 ssh_gssapi_error(ctx->oid, maj_status,min_status);
5598e598 372 }
373
1c828c5c 374 xfree(xhost);
5598e598 375 xfree(gssbuf.value);
376 return(maj_status);
377}
378
379/* Acquire credentials for a server running on the current host.
380 * Requires that the context structure contains a valid OID
b59afbfe 381 */
382
383/* Returns a GSSAPI error code */
5598e598 384OM_uint32
385ssh_gssapi_acquire_cred(Gssctxt *ctx) {
386 OM_uint32 maj_status, min_status;
387 char lname[MAXHOSTNAMELEN];
388 gss_OID_set oidset;
389
390 gss_create_empty_oid_set(&min_status,&oidset);
391 gss_add_oid_set_member(&min_status,ctx->oid,&oidset);
392
393 if (gethostname(lname, MAXHOSTNAMELEN)) {
394 return(-1);
395 }
396
397 if ((maj_status=ssh_gssapi_import_name(ctx,lname))) {
398 return(maj_status);
399 }
400 if ((maj_status=gss_acquire_cred(&min_status,
401 ctx->name,
402 0,
403 oidset,
404 GSS_C_ACCEPT,
405 &ctx->creds,
406 NULL,
407 NULL))) {
9eeaa28e 408 ssh_gssapi_error(ctx->oid,maj_status,min_status);
5598e598 409 }
410
411 gss_release_oid_set(&min_status, &oidset);
412 return(maj_status);
413}
414
415/* Extract the client details from a given context. This can only reliably
416 * be called once for a context */
417
418OM_uint32
419ssh_gssapi_getclient(Gssctxt *ctx, enum ssh_gss_id *type,
420 gss_buffer_desc *name, gss_cred_id_t *creds) {
421
422 OM_uint32 maj_status,min_status;
423
424 *type=ssh_gssapi_get_ctype(ctx);
425 if ((maj_status=gss_display_name(&min_status,ctx->client,name,NULL))) {
9eeaa28e 426 ssh_gssapi_error(ctx->oid,maj_status,min_status);
5598e598 427 }
428
429 /* This is icky. There appears to be no way to copy this structure,
430 * rather than the pointer to it, so we simply copy the pointer and
431 * mark the originator as empty so we don't destroy it.
432 */
433 *creds=ctx->client_creds;
434 ctx->client_creds=GSS_C_NO_CREDENTIAL;
435 return(maj_status);
436}
b59afbfe 437
438OM_uint32
439ssh_gssapi_sign(Gssctxt *ctx, gss_buffer_desc *buffer, gss_buffer_desc *hash) {
440 OM_uint32 maj_status,min_status;
5598e598 441
b59afbfe 442 /* ssh1 needs to exchange the hash of the keys */
443 /* will us this hash to return it */
444 if (!compat20) {
445 if ((maj_status=gss_wrap(&min_status,ctx->context,
446 0,
447 GSS_C_QOP_DEFAULT,
448 buffer,
449 NULL,
450 hash)))
6f9f4dab 451 ssh_gssapi_error(ctx->oid,maj_status,min_status);
b59afbfe 452 }
453 else
454
455 if ((maj_status=gss_get_mic(&min_status,ctx->context,
456 GSS_C_QOP_DEFAULT, buffer, hash))) {
6f9f4dab 457 ssh_gssapi_error(ctx->oid,maj_status,min_status);
b59afbfe 458 }
459
460 return(maj_status);
461}
462
b59afbfe 463OM_uint32
464ssh_gssapi_client_ctx(Gssctxt **ctx,gss_OID oid, char *host) {
d4ab470d 465 gss_buffer_desc token = {0,NULL};
b59afbfe 466 OM_uint32 major,minor;
467
468 if (*ctx) ssh_gssapi_delete_ctx(ctx);
469 ssh_gssapi_build_ctx(ctx);
470 ssh_gssapi_set_oid(*ctx,oid);
471 ssh_gssapi_import_name(*ctx,host);
472 major=ssh_gssapi_init_ctx(*ctx, 0, GSS_C_NO_BUFFER, &token, NULL);
473 gss_release_buffer(&minor,&token);
474 return(major);
475}
f001f132 476
5598e598 477#endif /* GSSAPI */
This page took 0.959771 seconds and 5 git commands to generate.