Add support for mechanisms with no integrity
[openssh.git] / gss-genr.c
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 "ssh2.h"
42 #include "cipher.h"
43 #include "key.h"
44 #include "kex.h"
45 #include <openssl/evp.h>
46
47 #include "ssh-gss.h"
48
49 extern u_char *session_id2;
50 extern u_int session_id2_len;
51
52 typedef struct {
53         char *encoded;
54         gss_OID oid;
55 } ssh_gss_kex_mapping;
56
57 /*
58  * XXX - It would be nice to find a more elegant way of handling the
59  * XXX   passing of the key exchange context to the userauth routines
60  */
61
62 Gssctxt *gss_kex_context = NULL;
63
64 static ssh_gss_kex_mapping *gss_enc2oid = NULL;
65
66 int 
67 ssh_gssapi_oid_table_ok() {
68         return (gss_enc2oid != NULL);
69 }
70
71 /*
72  * Return a list of the gss-group1-sha1 mechanisms supported by this program
73  *
74  * We test mechanisms to ensure that we can use them, to avoid starting
75  * a key exchange with a bad mechanism
76  */
77
78 char *
79 ssh_gssapi_client_mechanisms(const char *host, const char *client) {
80         gss_OID_set gss_supported;
81         OM_uint32 min_status;
82
83         if (GSS_ERROR(gss_indicate_mechs(&min_status, &gss_supported)))
84                 return NULL;
85
86         return(ssh_gssapi_kex_mechs(gss_supported, ssh_gssapi_check_mechanism,
87             host, client));
88 }
89
90 char *
91 ssh_gssapi_kex_mechs(gss_OID_set gss_supported, ssh_gssapi_check_fn *check,
92     const char *host, const char *client) {
93         Buffer buf;
94         size_t i;
95         int oidpos, enclen;
96         char *mechs, *encoded;
97         u_char digest[EVP_MAX_MD_SIZE];
98         char deroid[2];
99         const EVP_MD *evp_md = EVP_md5();
100         EVP_MD_CTX md;
101
102         if (gss_enc2oid != NULL) {
103                 for (i = 0; gss_enc2oid[i].encoded != NULL; i++)
104                         xfree(gss_enc2oid[i].encoded);
105                 xfree(gss_enc2oid);
106         }
107
108         gss_enc2oid = xmalloc(sizeof(ssh_gss_kex_mapping) *
109             (gss_supported->count + 1));
110
111         buffer_init(&buf);
112
113         oidpos = 0;
114         for (i = 0; i < gss_supported->count; i++) {
115                 if (gss_supported->elements[i].length < 128 &&
116                     (*check)(NULL, &(gss_supported->elements[i]), host, client)) {
117
118                         deroid[0] = SSH_GSS_OIDTYPE;
119                         deroid[1] = gss_supported->elements[i].length;
120
121                         EVP_DigestInit(&md, evp_md);
122                         EVP_DigestUpdate(&md, deroid, 2);
123                         EVP_DigestUpdate(&md,
124                             gss_supported->elements[i].elements,
125                             gss_supported->elements[i].length);
126                         EVP_DigestFinal(&md, digest, NULL);
127
128                         encoded = xmalloc(EVP_MD_size(evp_md) * 2);
129                         enclen = __b64_ntop(digest, EVP_MD_size(evp_md),
130                             encoded, EVP_MD_size(evp_md) * 2);
131
132                         if (oidpos != 0)
133                                 buffer_put_char(&buf, ',');
134
135                         buffer_append(&buf, KEX_GSS_GEX_SHA1_ID,
136                             sizeof(KEX_GSS_GEX_SHA1_ID) - 1);
137                         buffer_append(&buf, encoded, enclen);
138                         buffer_put_char(&buf, ',');
139                         buffer_append(&buf, KEX_GSS_GRP1_SHA1_ID, 
140                             sizeof(KEX_GSS_GRP1_SHA1_ID) - 1);
141                         buffer_append(&buf, encoded, enclen);
142                         buffer_put_char(&buf, ',');
143                         buffer_append(&buf, KEX_GSS_GRP14_SHA1_ID,
144                             sizeof(KEX_GSS_GRP14_SHA1_ID) - 1);
145                         buffer_append(&buf, encoded, enclen);
146
147                         gss_enc2oid[oidpos].oid = &(gss_supported->elements[i]);
148                         gss_enc2oid[oidpos].encoded = encoded;
149                         oidpos++;
150                 }
151         }
152         gss_enc2oid[oidpos].oid = NULL;
153         gss_enc2oid[oidpos].encoded = NULL;
154
155         buffer_put_char(&buf, '\0');
156
157         mechs = xmalloc(buffer_len(&buf));
158         buffer_get(&buf, mechs, buffer_len(&buf));
159         buffer_free(&buf);
160
161         if (strlen(mechs) == 0) {
162                 xfree(mechs);
163                 mechs = NULL;
164         }
165         
166         return (mechs);
167 }
168
169 gss_OID
170 ssh_gssapi_id_kex(Gssctxt *ctx, char *name, int kex_type) {
171         int i = 0;
172         
173         switch (kex_type) {
174         case KEX_GSS_GRP1_SHA1:
175                 if (strlen(name) < sizeof(KEX_GSS_GRP1_SHA1_ID))
176                         return GSS_C_NO_OID;
177                 name += sizeof(KEX_GSS_GRP1_SHA1_ID) - 1;
178                 break;
179         case KEX_GSS_GRP14_SHA1:
180                 if (strlen(name) < sizeof(KEX_GSS_GRP14_SHA1_ID))
181                         return GSS_C_NO_OID;
182                 name += sizeof(KEX_GSS_GRP14_SHA1_ID) - 1;
183                 break;
184         case KEX_GSS_GEX_SHA1:
185                 if (strlen(name) < sizeof(KEX_GSS_GEX_SHA1_ID))
186                         return GSS_C_NO_OID;
187                 name += sizeof(KEX_GSS_GEX_SHA1_ID) - 1;
188                 break;
189         default:
190                 return GSS_C_NO_OID;
191         }
192
193         while (gss_enc2oid[i].encoded != NULL &&
194             strcmp(name, gss_enc2oid[i].encoded) != 0)
195                 i++;
196
197         if (gss_enc2oid[i].oid != NULL && ctx != NULL)
198                 ssh_gssapi_set_oid(ctx, gss_enc2oid[i].oid);
199
200         return gss_enc2oid[i].oid;
201 }
202
203 /* Check that the OID in a data stream matches that in the context */
204 int
205 ssh_gssapi_check_oid(Gssctxt *ctx, void *data, size_t len)
206 {
207         return (ctx != NULL && ctx->oid != GSS_C_NO_OID &&
208             ctx->oid->length == len &&
209             memcmp(ctx->oid->elements, data, len) == 0);
210 }
211
212 /* Set the contexts OID from a data stream */
213 void
214 ssh_gssapi_set_oid_data(Gssctxt *ctx, void *data, size_t len)
215 {
216         if (ctx->oid != GSS_C_NO_OID) {
217                 xfree(ctx->oid->elements);
218                 xfree(ctx->oid);
219         }
220         ctx->oid = xmalloc(sizeof(gss_OID_desc));
221         ctx->oid->length = len;
222         ctx->oid->elements = xmalloc(len);
223         memcpy(ctx->oid->elements, data, len);
224 }
225
226 /* Set the contexts OID */
227 void
228 ssh_gssapi_set_oid(Gssctxt *ctx, gss_OID oid)
229 {
230         ssh_gssapi_set_oid_data(ctx, oid->elements, oid->length);
231 }
232
233 /* All this effort to report an error ... */
234 void
235 ssh_gssapi_error(Gssctxt *ctxt)
236 {
237         char *s;
238
239         s = ssh_gssapi_last_error(ctxt, NULL, NULL);
240         debug("%s", s);
241         xfree(s);
242 }
243
244 char *
245 ssh_gssapi_last_error(Gssctxt *ctxt, OM_uint32 *major_status,
246     OM_uint32 *minor_status)
247 {
248         OM_uint32 lmin;
249         gss_buffer_desc msg = GSS_C_EMPTY_BUFFER;
250         OM_uint32 ctx;
251         Buffer b;
252         char *ret;
253
254         buffer_init(&b);
255
256         if (major_status != NULL)
257                 *major_status = ctxt->major;
258         if (minor_status != NULL)
259                 *minor_status = ctxt->minor;
260
261         ctx = 0;
262         /* The GSSAPI error */
263         do {
264                 gss_display_status(&lmin, ctxt->major,
265                     GSS_C_GSS_CODE, ctxt->oid, &ctx, &msg);
266
267                 buffer_append(&b, msg.value, msg.length);
268                 buffer_put_char(&b, '\n');
269
270                 gss_release_buffer(&lmin, &msg);
271         } while (ctx != 0);
272
273         /* The mechanism specific error */
274         do {
275                 gss_display_status(&lmin, ctxt->minor,
276                     GSS_C_MECH_CODE, ctxt->oid, &ctx, &msg);
277
278                 buffer_append(&b, msg.value, msg.length);
279                 buffer_put_char(&b, '\n');
280
281                 gss_release_buffer(&lmin, &msg);
282         } while (ctx != 0);
283
284         buffer_put_char(&b, '\0');
285         ret = xmalloc(buffer_len(&b));
286         buffer_get(&b, ret, buffer_len(&b));
287         buffer_free(&b);
288         return (ret);
289 }
290
291 /*
292  * Initialise our GSSAPI context. We use this opaque structure to contain all
293  * of the data which both the client and server need to persist across
294  * {accept,init}_sec_context calls, so that when we do it from the userauth
295  * stuff life is a little easier
296  */
297 void
298 ssh_gssapi_build_ctx(Gssctxt **ctx)
299 {
300         *ctx = xcalloc(1, sizeof (Gssctxt));
301         (*ctx)->context = GSS_C_NO_CONTEXT;
302         (*ctx)->name = GSS_C_NO_NAME;
303         (*ctx)->oid = GSS_C_NO_OID;
304         (*ctx)->creds = GSS_C_NO_CREDENTIAL;
305         (*ctx)->client = GSS_C_NO_NAME;
306         (*ctx)->client_creds = GSS_C_NO_CREDENTIAL;
307 }
308
309 /* Delete our context, providing it has been built correctly */
310 void
311 ssh_gssapi_delete_ctx(Gssctxt **ctx)
312 {
313         OM_uint32 ms;
314
315         if ((*ctx) == NULL)
316                 return;
317         if ((*ctx)->context != GSS_C_NO_CONTEXT)
318                 gss_delete_sec_context(&ms, &(*ctx)->context, GSS_C_NO_BUFFER);
319         if ((*ctx)->name != GSS_C_NO_NAME)
320                 gss_release_name(&ms, &(*ctx)->name);
321         if ((*ctx)->oid != GSS_C_NO_OID) {
322                 xfree((*ctx)->oid->elements);
323                 xfree((*ctx)->oid);
324                 (*ctx)->oid = GSS_C_NO_OID;
325         }
326         if ((*ctx)->creds != GSS_C_NO_CREDENTIAL)
327                 gss_release_cred(&ms, &(*ctx)->creds);
328         if ((*ctx)->client != GSS_C_NO_NAME)
329                 gss_release_name(&ms, &(*ctx)->client);
330         if ((*ctx)->client_creds != GSS_C_NO_CREDENTIAL)
331                 gss_release_cred(&ms, &(*ctx)->client_creds);
332
333         xfree(*ctx);
334         *ctx = NULL;
335 }
336
337 /*
338  * Wrapper to init_sec_context
339  * Requires that the context contains:
340  *      oid
341  *      server name (from ssh_gssapi_import_name)
342  */
343 OM_uint32
344 ssh_gssapi_init_ctx(Gssctxt *ctx, int deleg_creds, gss_buffer_desc *recv_tok,
345     gss_buffer_desc* send_tok, OM_uint32 *flags)
346 {
347         int deleg_flag = 0;
348
349         if (deleg_creds) {
350                 deleg_flag = GSS_C_DELEG_FLAG;
351                 debug("Delegating credentials");
352         }
353
354         ctx->major = gss_init_sec_context(&ctx->minor,
355             ctx->client_creds, &ctx->context, ctx->name, ctx->oid,
356             GSS_C_MUTUAL_FLAG | GSS_C_INTEG_FLAG | deleg_flag,
357             0, NULL, recv_tok, NULL, send_tok, flags, NULL);
358
359         if (GSS_ERROR(ctx->major))
360                 ssh_gssapi_error(ctx);
361
362         return (ctx->major);
363 }
364
365 /* Create a service name for the given host */
366 OM_uint32
367 ssh_gssapi_import_name(Gssctxt *ctx, const char *host)
368 {
369         gss_buffer_desc gssbuf;
370         char *val;
371
372         xasprintf(&val, "host@%s", host);
373         gssbuf.value = val;
374         gssbuf.length = strlen(gssbuf.value);
375
376         if ((ctx->major = gss_import_name(&ctx->minor,
377             &gssbuf, GSS_C_NT_HOSTBASED_SERVICE, &ctx->name)))
378                 ssh_gssapi_error(ctx);
379
380         xfree(gssbuf.value);
381         return (ctx->major);
382 }
383
384 OM_uint32
385 ssh_gssapi_client_identity(Gssctxt *ctx, const char *name)
386 {
387         gss_buffer_desc gssbuf;
388         gss_name_t gssname;
389         OM_uint32 status;
390         gss_OID_set oidset;
391
392         gssbuf.value = (void *) name;
393         gssbuf.length = strlen(gssbuf.value);
394
395         gss_create_empty_oid_set(&status, &oidset);
396         gss_add_oid_set_member(&status, ctx->oid, &oidset);
397
398         ctx->major = gss_import_name(&ctx->minor, &gssbuf,
399             GSS_C_NT_USER_NAME, &gssname);
400
401         if (!ctx->major)
402                 ctx->major = gss_acquire_cred(&ctx->minor, 
403                     gssname, 0, oidset, GSS_C_INITIATE, 
404                     &ctx->client_creds, NULL, NULL);
405
406         gss_release_name(&status, &gssname);
407         gss_release_oid_set(&status, &oidset);
408
409         if (ctx->major)
410                 ssh_gssapi_error(ctx);
411
412         return(ctx->major);
413 }
414
415 OM_uint32
416 ssh_gssapi_sign(Gssctxt *ctx, gss_buffer_t buffer, gss_buffer_t hash)
417 {
418         if (ctx == NULL) 
419                 return -1;
420
421         if ((ctx->major = gss_get_mic(&ctx->minor, ctx->context,
422             GSS_C_QOP_DEFAULT, buffer, hash)))
423                 ssh_gssapi_error(ctx);
424
425         return (ctx->major);
426 }
427
428 /* Priviledged when used by server */
429 OM_uint32
430 ssh_gssapi_checkmic(Gssctxt *ctx, gss_buffer_t gssbuf, gss_buffer_t gssmic)
431 {
432         if (ctx == NULL)
433                 return -1;
434
435         ctx->major = gss_verify_mic(&ctx->minor, ctx->context,
436             gssbuf, gssmic, NULL);
437
438         return (ctx->major);
439 }
440
441 void
442 ssh_gssapi_buildmic(Buffer *b, const char *user, const char *service,
443     const char *context)
444 {
445         buffer_init(b);
446         buffer_put_string(b, session_id2, session_id2_len);
447         buffer_put_char(b, SSH2_MSG_USERAUTH_REQUEST);
448         buffer_put_cstring(b, user);
449         buffer_put_cstring(b, service);
450         buffer_put_cstring(b, context);
451 }
452
453 int
454 ssh_gssapi_check_mechanism(Gssctxt **ctx, gss_OID oid, const char *host, 
455     const char *client)
456 {
457         gss_buffer_desc token = GSS_C_EMPTY_BUFFER;
458         OM_uint32 major, minor;
459         gss_OID_desc spnego_oid = {6, (void *)"\x2B\x06\x01\x05\x05\x02"};
460         Gssctxt *intctx = NULL;
461
462         if (ctx == NULL)
463                 ctx = &intctx;
464
465         /* RFC 4462 says we MUST NOT do SPNEGO */
466         if (oid->length == spnego_oid.length && 
467             (memcmp(oid->elements, spnego_oid.elements, oid->length) == 0))
468                 return 0; /* false */
469
470         ssh_gssapi_build_ctx(ctx);
471         ssh_gssapi_set_oid(*ctx, oid);
472         major = ssh_gssapi_import_name(*ctx, host);
473
474         if (!GSS_ERROR(major) && client)
475                 major = ssh_gssapi_client_identity(*ctx, client);
476
477         if (!GSS_ERROR(major)) {
478                 major = ssh_gssapi_init_ctx(*ctx, 0, GSS_C_NO_BUFFER, &token, 
479                     NULL);
480                 gss_release_buffer(&minor, &token);
481                 if ((*ctx)->context != GSS_C_NO_CONTEXT)
482                         gss_delete_sec_context(&minor, &(*ctx)->context,
483                             GSS_C_NO_BUFFER);
484         }
485
486         if (GSS_ERROR(major) || intctx != NULL) 
487                 ssh_gssapi_delete_ctx(ctx);
488
489         return (!GSS_ERROR(major));
490 }
491
492 int
493 ssh_gssapi_credentials_updated(Gssctxt *ctxt) {
494         static gss_name_t saved_name = GSS_C_NO_NAME;
495         static OM_uint32 saved_lifetime = 0;
496         static gss_OID saved_mech = GSS_C_NO_OID;
497         static gss_name_t name;
498         static OM_uint32 last_call = 0;
499         OM_uint32 lifetime, now, major, minor;
500         int equal;
501         gss_cred_usage_t usage = GSS_C_INITIATE;
502         
503         now = time(NULL);
504
505         if (ctxt) {
506                 debug("Rekey has happened - updating saved versions");
507
508                 if (saved_name != GSS_C_NO_NAME)
509                         gss_release_name(&minor, &saved_name);
510
511                 major = gss_inquire_cred(&minor, GSS_C_NO_CREDENTIAL,
512                     &saved_name, &saved_lifetime, NULL, NULL);
513
514                 if (!GSS_ERROR(major)) {
515                         saved_mech = ctxt->oid;
516                         saved_lifetime+= now;
517                 } else {
518                         /* Handle the error */
519                 }
520                 return 0;
521         }
522
523         if (now - last_call < 10)
524                 return 0;
525
526         last_call = now;
527
528         if (saved_mech == GSS_C_NO_OID)
529                 return 0;
530         
531         major = gss_inquire_cred(&minor, GSS_C_NO_CREDENTIAL, 
532             &name, &lifetime, NULL, NULL);
533         if (major == GSS_S_CREDENTIALS_EXPIRED)
534                 return 0;
535         else if (GSS_ERROR(major))
536                 return 0;
537
538         major = gss_compare_name(&minor, saved_name, name, &equal);
539         gss_release_name(&minor, &name);
540         if (GSS_ERROR(major))
541                 return 0;
542
543         if (equal && (saved_lifetime < lifetime + now - 10))
544                 return 1;
545
546         return 0;
547 }
548
549 #endif /* GSSAPI */