]> andersk Git - gssapi-openssh.git/blob - openssh/gss-genr.c
minimize diffs with Simon's patch
[gssapi-openssh.git] / openssh / gss-genr.c
1 /*      $OpenBSD: gss-genr.c,v 1.6 2005/10/13 22:24:31 stevesk Exp $    */
2
3 /*
4  * Copyright (c) 2001-2005 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 "xmalloc.h"
32 #include "bufaux.h"
33 #include "log.h"
34 #include "canohost.h"
35 #include "ssh2.h"
36 #include <openssl/evp.h>
37
38 #include "ssh-gss.h"
39
40 extern u_char *session_id2;
41 extern u_int session_id2_len;
42
43 typedef struct {
44         char *encoded;
45         gss_OID oid;
46 } ssh_gss_kex_mapping;
47
48 /*
49  * XXX - It would be nice to find a more elegant way of handling the
50  * XXX   passing of the key exchange context to the userauth routines
51  */
52
53 Gssctxt *gss_kex_context = NULL;
54
55 static ssh_gss_kex_mapping *gss_enc2oid = NULL;
56
57 int 
58 ssh_gssapi_oid_table_ok() {
59         return (gss_enc2oid != NULL);
60 }
61
62 /*
63  * Return a list of the gss-group1-sha1 mechanisms supported by this program
64  *
65  * We test mechanisms to ensure that we can use them, to avoid starting
66  * a key exchange with a bad mechanism
67  */
68
69
70 char *
71 ssh_gssapi_client_mechanisms(const char *host) {
72         gss_OID_set gss_supported;
73         OM_uint32 min_status;
74
75         gss_indicate_mechs(&min_status, &gss_supported);
76
77         return(ssh_gssapi_kex_mechs(gss_supported, ssh_gssapi_check_mechanism,
78             (void *)host));
79 }
80
81 char *
82 ssh_gssapi_kex_mechs(gss_OID_set gss_supported, ssh_gssapi_check_fn *check,
83     void *data) {
84         Buffer buf;
85         size_t i;
86         int oidpos, enclen;
87         char *mechs, *encoded;
88         u_char digest[EVP_MAX_MD_SIZE];
89         char deroid[2];
90         const EVP_MD *evp_md = EVP_md5();
91         EVP_MD_CTX md;
92
93         if (gss_enc2oid != NULL) {
94                 for (i=0;gss_enc2oid[i].encoded!=NULL;i++)
95                         xfree(gss_enc2oid[i].encoded);
96                 xfree(gss_enc2oid);
97         }
98
99         gss_enc2oid = xmalloc(sizeof(ssh_gss_kex_mapping)*
100             (gss_supported->count+1));
101
102         buffer_init(&buf);
103
104         oidpos = 0;
105         for (i = 0;i < gss_supported->count;i++) {
106                 if (gss_supported->elements[i].length < 128 &&
107                     (*check)(&(gss_supported->elements[i]), data)) {
108
109                         deroid[0] = SSH_GSS_OIDTYPE;
110                         deroid[1] = gss_supported->elements[i].length;
111
112                         EVP_DigestInit(&md, evp_md);
113                         EVP_DigestUpdate(&md, deroid, 2);
114                         EVP_DigestUpdate(&md,
115                             gss_supported->elements[i].elements,
116                             gss_supported->elements[i].length);
117                         EVP_DigestFinal(&md, digest, NULL);
118
119                         encoded = xmalloc(EVP_MD_size(evp_md)*2);
120                         enclen = __b64_ntop(digest, EVP_MD_size(evp_md),
121                             encoded, EVP_MD_size(evp_md)*2);
122
123                         if (oidpos != 0)
124                             buffer_put_char(&buf, ',');
125
126                         buffer_append(&buf, KEX_GSS_GEX_SHA1_ID,
127                             sizeof(KEX_GSS_GEX_SHA1_ID)-1);
128                         buffer_append(&buf, encoded, enclen);
129                         buffer_put_char(&buf,',');
130                         buffer_append(&buf, KEX_GSS_GRP1_SHA1_ID, 
131                             sizeof(KEX_GSS_GRP1_SHA1_ID)-1);
132                         buffer_append(&buf, encoded, enclen);
133
134                         gss_enc2oid[oidpos].oid = &(gss_supported->elements[i]);
135                         gss_enc2oid[oidpos].encoded = encoded;
136                         oidpos++;
137                 }
138         }
139         gss_enc2oid[oidpos].oid = NULL;
140         gss_enc2oid[oidpos].encoded = NULL;
141
142         buffer_put_char(&buf, '\0');
143
144         mechs = xmalloc(buffer_len(&buf));
145         buffer_get(&buf, mechs, buffer_len(&buf));
146         buffer_free(&buf);
147
148         if (strlen(mechs) == 0) {
149                 xfree(mechs);
150                 mechs = NULL;
151         }
152         
153         return (mechs);
154 }
155
156 gss_OID
157 ssh_gssapi_id_kex(Gssctxt *ctx, char *name, int *gex) {
158         int i = 0;
159
160         if (strncmp(name, KEX_GSS_GRP1_SHA1_ID,
161             sizeof(KEX_GSS_GRP1_SHA1_ID)-1) == 0) {
162                 name+=sizeof(KEX_GSS_GRP1_SHA1_ID)-1;
163                 *gex = 0;
164         } else if (strncmp(name, KEX_GSS_GEX_SHA1_ID,
165             sizeof(KEX_GSS_GEX_SHA1_ID)-1) == 0) {
166                 name+=sizeof(KEX_GSS_GEX_SHA1_ID)-1;
167                 *gex = 1;
168         } else {
169                 return NULL;
170         }
171
172         while (gss_enc2oid[i].encoded != NULL &&
173             strcmp(name, gss_enc2oid[i].encoded) != 0) {
174                 i++;
175         }
176
177         if (gss_enc2oid[i].oid != NULL && ctx != NULL)
178                 ssh_gssapi_set_oid(ctx, gss_enc2oid[i].oid);
179
180         return gss_enc2oid[i].oid;
181 }
182
183 /* Check that the OID in a data stream matches that in the context */
184 int
185 ssh_gssapi_check_oid(Gssctxt *ctx, void *data, size_t len)
186 {
187         return (ctx != NULL && ctx->oid != GSS_C_NO_OID &&
188             ctx->oid->length == len &&
189             memcmp(ctx->oid->elements, data, len) == 0);
190 }
191
192 /* Set the contexts OID from a data stream */
193 void
194 ssh_gssapi_set_oid_data(Gssctxt *ctx, void *data, size_t len)
195 {
196         if (ctx->oid != GSS_C_NO_OID) {
197                 xfree(ctx->oid->elements);
198                 xfree(ctx->oid);
199         }
200         ctx->oid = xmalloc(sizeof(gss_OID_desc));
201         ctx->oid->length = len;
202         ctx->oid->elements = xmalloc(len);
203         memcpy(ctx->oid->elements, data, len);
204 }
205
206 /* Set the contexts OID */
207 void
208 ssh_gssapi_set_oid(Gssctxt *ctx, gss_OID oid)
209 {
210         ssh_gssapi_set_oid_data(ctx, oid->elements, oid->length);
211 }
212
213 /* All this effort to report an error ... */
214 void
215 ssh_gssapi_error(Gssctxt *ctxt)
216 {
217         debug("%s", ssh_gssapi_last_error(ctxt, NULL, NULL));
218 }
219
220 char *
221 ssh_gssapi_last_error(Gssctxt *ctxt, OM_uint32 *major_status,
222     OM_uint32 *minor_status)
223 {
224         OM_uint32 lmin;
225         gss_buffer_desc msg = GSS_C_EMPTY_BUFFER;
226         OM_uint32 ctx;
227         Buffer b;
228         char *ret;
229
230         buffer_init(&b);
231
232         if (major_status != NULL)
233                 *major_status = ctxt->major;
234         if (minor_status != NULL)
235                 *minor_status = ctxt->minor;
236
237         ctx = 0;
238         /* The GSSAPI error */
239         do {
240                 gss_display_status(&lmin, ctxt->major,
241                     GSS_C_GSS_CODE, ctxt->oid, &ctx, &msg);
242
243                 buffer_append(&b, msg.value, msg.length);
244                 buffer_put_char(&b, '\n');
245
246                 gss_release_buffer(&lmin, &msg);
247         } while (ctx != 0);
248
249         /* The mechanism specific error */
250         do {
251                 gss_display_status(&lmin, ctxt->minor,
252                     GSS_C_MECH_CODE, ctxt->oid, &ctx, &msg);
253
254                 buffer_append(&b, msg.value, msg.length);
255                 buffer_put_char(&b, '\n');
256
257                 gss_release_buffer(&lmin, &msg);
258         } while (ctx != 0);
259
260         buffer_put_char(&b, '\0');
261         ret = xmalloc(buffer_len(&b));
262         buffer_get(&b, ret, buffer_len(&b));
263         buffer_free(&b);
264         return (ret);
265 }
266
267 /*
268  * Initialise our GSSAPI context. We use this opaque structure to contain all
269  * of the data which both the client and server need to persist across
270  * {accept,init}_sec_context calls, so that when we do it from the userauth
271  * stuff life is a little easier
272  */
273 void
274 ssh_gssapi_build_ctx(Gssctxt **ctx)
275 {
276         *ctx = xmalloc(sizeof (Gssctxt));
277         (*ctx)->major = 0;
278         (*ctx)->minor = 0;
279         (*ctx)->context = GSS_C_NO_CONTEXT;
280         (*ctx)->name = GSS_C_NO_NAME;
281         (*ctx)->oid = GSS_C_NO_OID;
282         (*ctx)->creds = GSS_C_NO_CREDENTIAL;
283         (*ctx)->client = GSS_C_NO_NAME;
284         (*ctx)->client_creds = GSS_C_NO_CREDENTIAL;
285 }
286
287 /* Delete our context, providing it has been built correctly */
288 void
289 ssh_gssapi_delete_ctx(Gssctxt **ctx)
290 {
291 #if !defined(MECHGLUE)
292         OM_uint32 ms;
293 #endif
294
295         if ((*ctx) == NULL)
296                 return;
297 #if !defined(MECHGLUE) /* mechglue has some memory management issues */
298         if ((*ctx)->context != GSS_C_NO_CONTEXT)
299                 gss_delete_sec_context(&ms, &(*ctx)->context, GSS_C_NO_BUFFER);
300         if ((*ctx)->name != GSS_C_NO_NAME)
301                 gss_release_name(&ms, &(*ctx)->name);
302         if ((*ctx)->oid != GSS_C_NO_OID) {
303                 xfree((*ctx)->oid->elements);
304                 xfree((*ctx)->oid);
305                 (*ctx)->oid = GSS_C_NO_OID;
306         }
307         if ((*ctx)->creds != GSS_C_NO_CREDENTIAL)
308                 gss_release_cred(&ms, &(*ctx)->creds);
309         if ((*ctx)->client != GSS_C_NO_NAME)
310                 gss_release_name(&ms, &(*ctx)->client);
311         if ((*ctx)->client_creds != GSS_C_NO_CREDENTIAL)
312                 gss_release_cred(&ms, &(*ctx)->client_creds);
313 #endif
314
315         xfree(*ctx);
316         *ctx = NULL;
317 }
318
319 /*
320  * Wrapper to init_sec_context
321  * Requires that the context contains:
322  *      oid
323  *      server name (from ssh_gssapi_import_name)
324  */
325 OM_uint32
326 ssh_gssapi_init_ctx(Gssctxt *ctx, int deleg_creds, gss_buffer_desc *recv_tok,
327     gss_buffer_desc* send_tok, OM_uint32 *flags)
328 {
329         int deleg_flag = 0;
330
331         if (deleg_creds) {
332                 deleg_flag = GSS_C_DELEG_FLAG;
333                 debug("Delegating credentials");
334         }
335
336         ctx->major = gss_init_sec_context(&ctx->minor,
337             GSS_C_NO_CREDENTIAL, &ctx->context, ctx->name, ctx->oid,
338             GSS_C_MUTUAL_FLAG | GSS_C_INTEG_FLAG | deleg_flag,
339             0, NULL, recv_tok, NULL, send_tok, flags, NULL);
340
341         if (GSS_ERROR(ctx->major))
342                 ssh_gssapi_error(ctx);
343
344         return (ctx->major);
345 }
346
347 /* Create a service name for the given host */
348 OM_uint32
349 ssh_gssapi_import_name(Gssctxt *ctx, const char *host)
350 {
351         gss_buffer_desc gssbuf;
352         char *xhost;
353
354         /* Make a copy of the host name, in case it was returned by a
355          * previous call to gethostbyname(). */ 
356         xhost = xstrdup(host);
357
358         /* Make sure we have the FQDN. Some GSSAPI implementations don't do
359          * this for us themselves */
360         resolve_localhost(&xhost);
361         
362         gssbuf.length = sizeof("host@") + strlen(xhost);
363         gssbuf.value = xmalloc(gssbuf.length);
364         snprintf(gssbuf.value, gssbuf.length, "host@%s", xhost);
365
366         if ((ctx->major = gss_import_name(&ctx->minor,
367             &gssbuf, GSS_C_NT_HOSTBASED_SERVICE, &ctx->name)))
368                 ssh_gssapi_error(ctx);
369
370         xfree(xhost);
371         xfree(gssbuf.value);
372         return (ctx->major);
373 }
374
375 /* Acquire credentials for a server running on the current host.
376  * Requires that the context structure contains a valid OID
377  */
378
379 /* Returns a GSSAPI error code */
380 OM_uint32
381 ssh_gssapi_acquire_cred(Gssctxt *ctx)
382 {
383         OM_uint32 status;
384         char lname[MAXHOSTNAMELEN];
385         gss_OID_set oidset;
386
387         gss_create_empty_oid_set(&status, &oidset);
388         gss_add_oid_set_member(&status, ctx->oid, &oidset);
389
390         if (gethostname(lname, MAXHOSTNAMELEN))
391                 return (-1);
392
393         if (GSS_ERROR(ssh_gssapi_import_name(ctx, lname)))
394                 return (ctx->major);
395
396         if ((ctx->major = gss_acquire_cred(&ctx->minor,
397             ctx->name, 0, oidset, GSS_C_ACCEPT, &ctx->creds, NULL, NULL)))
398                 ssh_gssapi_error(ctx);
399
400         gss_release_oid_set(&status, &oidset);
401         return (ctx->major);
402 }
403
404 OM_uint32
405 ssh_gssapi_sign(Gssctxt *ctx, gss_buffer_t buffer, gss_buffer_t hash)
406 {
407         if (ctx == NULL) 
408                 return -1;
409
410         if ((ctx->major = gss_get_mic(&ctx->minor, ctx->context,
411             GSS_C_QOP_DEFAULT, buffer, hash)))
412                 ssh_gssapi_error(ctx);
413
414         return (ctx->major);
415 }
416
417 /* Priviledged when used by server */
418 OM_uint32
419 ssh_gssapi_checkmic(Gssctxt *ctx, gss_buffer_t gssbuf, gss_buffer_t gssmic)
420 {
421         if (ctx == NULL)
422                 return -1;
423
424         ctx->major = gss_verify_mic(&ctx->minor, ctx->context,
425             gssbuf, gssmic, NULL);
426
427         return (ctx->major);
428 }
429
430 void
431 ssh_gssapi_buildmic(Buffer *b, const char *user, const char *service,
432     const char *context)
433 {
434         buffer_init(b);
435         buffer_put_string(b, session_id2, session_id2_len);
436         buffer_put_char(b, SSH2_MSG_USERAUTH_REQUEST);
437         buffer_put_cstring(b, user);
438         buffer_put_cstring(b, service);
439         buffer_put_cstring(b, context);
440 }
441
442 OM_uint32
443 ssh_gssapi_server_ctx(Gssctxt **ctx, gss_OID oid)
444 {
445         if (*ctx)
446                 ssh_gssapi_delete_ctx(ctx);
447         ssh_gssapi_build_ctx(ctx);
448         ssh_gssapi_set_oid(*ctx, oid);
449         return (ssh_gssapi_acquire_cred(*ctx));
450 }
451
452 int
453 ssh_gssapi_check_mechanism(gss_OID oid, void *host) {
454         Gssctxt * ctx = NULL;
455         gss_buffer_desc token = GSS_C_EMPTY_BUFFER;
456         OM_uint32 major, minor;
457         
458         ssh_gssapi_build_ctx(&ctx);
459         ssh_gssapi_set_oid(ctx, oid);
460         ssh_gssapi_import_name(ctx, host);
461         major = ssh_gssapi_init_ctx(ctx, 0, GSS_C_NO_BUFFER, &token, NULL);
462         gss_release_buffer(&minor, &token);
463         ssh_gssapi_delete_ctx(&ctx);
464         return (!GSS_ERROR(major));
465 }
466
467 #endif /* GSSAPI */
This page took 0.099096 seconds and 5 git commands to generate.