]> andersk Git - gssapi-openssh.git/blob - openssh/gss-genr.c
don't use the resolver to determine the full hostname of the target
[gssapi-openssh.git] / openssh / gss-genr.c
1 /*
2  * Copyright (c) 2001,2002 Simon Wilkinson. All rights reserved. *
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"
39 #include "compat.h"
40 #include "monitor_wrap.h"
41 #include "canohost.h"
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  
50 gss_buffer_desc gssapi_client_name = {0,NULL}; /* Name of our client */
51 gss_cred_id_t   gssapi_client_creds = GSS_C_NO_CREDENTIAL; /* Their credentials */
52 enum ssh_gss_id gssapi_client_type = GSS_LAST_ENTRY;
53
54 unsigned char ssh1_key_digest[16]; /* used for ssh1 gssapi */
55
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
66 ssh_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
82 char 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  
100 char * 
101 ssh_gssapi_mechanisms(int server,char *host) {
102         gss_OID_set     supported;
103         OM_uint32       maj_status, min_status;
104         Buffer          buf;
105         int             i = 0;
106         int             present;
107         int             mech_count=0;
108         char *          mechs;
109         Gssctxt *       ctx = NULL;     
110
111         if (datafellows & SSH_OLD_GSSAPI) return NULL;
112         
113         gss_indicate_mechs(&min_status, &supported);
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) {
125                         if ((server && 
126                              !GSS_ERROR(PRIVSEP(ssh_gssapi_server_ctx(&ctx,
127                                                             &supported_mechs[i].oid)))) 
128                             || (!server &&
129                                 !GSS_ERROR(ssh_gssapi_client_ctx(&ctx,
130                                                        &supported_mechs[i].oid,
131                                                        host)))) {
132                                 /* Append gss_group1_sha1_x to our list */
133                                 if (++mech_count > 1) {
134                                     buffer_append(&buf, ",", 1);
135                                 }
136                                 buffer_append(&buf, gssprefix,
137                                               strlen(gssprefix));
138                                 buffer_append(&buf, 
139                                               supported_mechs[i].enc_name,
140                                               strlen(supported_mechs[i].enc_name));
141                                 debug("GSSAPI mechanism %s (%s%s) supported",
142                                       supported_mechs[i].name, gssprefix,
143                                       supported_mechs[i].enc_name);
144                         } else {
145                             debug("no credentials for GSSAPI mechanism %s",
146                                   supported_mechs[i].name);
147                         }
148                 } else {
149                     debug("GSSAPI mechanism %s not supported",
150                           supported_mechs[i].name);
151                 }
152         } while (supported_mechs[++i].name != NULL);
153         
154         buffer_put_char(&buf,'\0');
155         
156         mechs=xmalloc(buffer_len(&buf));
157         buffer_get(&buf,mechs,buffer_len(&buf));
158         buffer_free(&buf);
159         if (strlen(mechs)==0)
160            return(NULL);
161         else
162            return(mechs);
163 }
164
165 void ssh_gssapi_supported_oids(gss_OID_set *oidset) {
166         enum ssh_gss_id i =0;
167         OM_uint32 maj_status,min_status;
168         int present;
169         gss_OID_set supported;
170         
171         gss_create_empty_oid_set(&min_status,oidset);
172         gss_indicate_mechs(&min_status, &supported);
173
174         while (supported_mechs[i].name!=NULL) {
175                 if ((maj_status=gss_test_oid_set_member(&min_status,
176                                                        &supported_mechs[i].oid,
177                                                        supported,
178                                                        &present))) {
179                         present=0;
180                 }
181                 if (present) {
182                         gss_add_oid_set_member(&min_status,
183                                                &supported_mechs[i].oid,
184                                                oidset); 
185                 }
186                 i++;
187         }
188 }       
189
190 /* Set the contexts OID from a data stream */
191 void ssh_gssapi_set_oid_data(Gssctxt *ctx, void *data, size_t len) { 
192   if (ctx->oid != GSS_C_NO_OID) {
193         xfree(ctx->oid->elements);
194         xfree(ctx->oid);
195   }
196   ctx->oid=xmalloc(sizeof(gss_OID_desc));
197   ctx->oid->length=len;
198   ctx->oid->elements=xmalloc(len);
199   memcpy(ctx->oid->elements,data,len);
200 }
201
202 /* Set the contexts OID */
203 void ssh_gssapi_set_oid(Gssctxt *ctx, gss_OID oid) {  
204   ssh_gssapi_set_oid_data(ctx,oid->elements,oid->length);
205 }
206
207 /* Find out which GSS type (out of the list we define in ssh-gss.h) a
208  * particular connection is using 
209  */
210 enum ssh_gss_id ssh_gssapi_get_ctype(Gssctxt *ctxt) {
211         enum ssh_gss_id i=0;
212         
213         while(supported_mechs[i].name!=NULL) {
214            if (supported_mechs[i].oid.length == ctxt->oid->length &&
215                (memcmp(supported_mechs[i].oid.elements,
216                        ctxt->oid->elements,ctxt->oid->length) == 0))
217                return i;
218            i++;
219         }
220         return(GSS_LAST_ENTRY);
221 }
222
223 /* Set the GSS context's OID to the oid indicated by the given key exchange
224  * name. */
225 gss_OID ssh_gssapi_id_kex(Gssctxt *ctx, char *name) {
226   enum ssh_gss_id i=0;
227   
228   if (strncmp(name, gssprefix, strlen(gssprefix)-1) !=0) {
229      return(NULL);
230   }
231   
232   name+=strlen(gssprefix); /* Move to the start of the MIME string */
233   
234   while (supported_mechs[i].name!=NULL &&
235          strcmp(name,supported_mechs[i].enc_name)!=0) {
236         i++;
237   }
238
239   if (supported_mechs[i].name==NULL)
240      return (NULL);
241
242   if (ctx) ssh_gssapi_set_oid(ctx,&supported_mechs[i].oid);
243
244   debug("using GSSAPI mechanism %s (%s%s)", supported_mechs[i].name,
245         gssprefix, supported_mechs[i].enc_name);
246
247   return &supported_mechs[i].oid;
248 }
249
250
251 /* All this effort to report an error ... */
252 static void
253 ssh_gssapi_error_ex(OM_uint32 major_status,OM_uint32 minor_status,
254                     int send_packet) {
255         OM_uint32 lmaj, lmin;
256         gss_buffer_desc msg = {0,NULL};
257         OM_uint32 ctx;
258         
259         ctx = 0;
260         /* The GSSAPI error */
261         do {
262                 lmaj = gss_display_status(&lmin, major_status,
263                                           GSS_C_GSS_CODE,
264                                           GSS_C_NULL_OID,
265                                           &ctx, &msg);
266                 if (lmaj == GSS_S_COMPLETE) {
267                         debug((char *)msg.value);
268                         if (send_packet) packet_send_debug((char *)msg.value);
269                         (void) gss_release_buffer(&lmin, &msg);
270                 }
271         } while (ctx!=0);          
272
273         /* The mechanism specific error */
274         do {
275                 lmaj = gss_display_status(&lmin, minor_status,
276                                           GSS_C_MECH_CODE,
277                                           GSS_C_NULL_OID,
278                                           &ctx, &msg);
279                 if (lmaj == GSS_S_COMPLETE) {
280                         debug((char *)msg.value);
281                         if (send_packet) packet_send_debug((char *)msg.value);
282                         (void) gss_release_buffer(&lmin, &msg);
283                 }
284         } while (ctx!=0);
285 }
286
287 void
288 ssh_gssapi_error(OM_uint32 major_status,OM_uint32 minor_status) {
289     ssh_gssapi_error_ex(major_status, minor_status, 0);
290 }
291
292 void
293 ssh_gssapi_send_error(OM_uint32 major_status,OM_uint32 minor_status) {
294     ssh_gssapi_error_ex(major_status, minor_status, 1);
295 }
296
297
298
299
300 /* Initialise our GSSAPI context. We use this opaque structure to contain all
301  * of the data which both the client and server need to persist across
302  * {accept,init}_sec_context calls, so that when we do it from the userauth
303  * stuff life is a little easier
304  */
305 void
306 ssh_gssapi_build_ctx(Gssctxt **ctx)
307 {
308         *ctx=xmalloc(sizeof (Gssctxt));
309         (*ctx)->context=GSS_C_NO_CONTEXT;
310         (*ctx)->name=GSS_C_NO_NAME;
311         (*ctx)->oid=GSS_C_NO_OID;
312         (*ctx)->creds=GSS_C_NO_CREDENTIAL;
313         (*ctx)->client=GSS_C_NO_NAME;
314         (*ctx)->client_creds=GSS_C_NO_CREDENTIAL;
315 }
316
317 /* Delete our context, providing it has been built correctly */
318 void
319 ssh_gssapi_delete_ctx(Gssctxt **ctx)
320 {
321         OM_uint32 ms;
322         
323         /* Return if there's no context */
324         if ((*ctx)==NULL)
325                 return;
326                 
327 #if !defined(MECHGLUE) /* mechglue has some memory management issues */
328         if ((*ctx)->context != GSS_C_NO_CONTEXT) 
329                 gss_delete_sec_context(&ms,&(*ctx)->context,GSS_C_NO_BUFFER);
330         if ((*ctx)->name != GSS_C_NO_NAME)
331                 gss_release_name(&ms,&(*ctx)->name);
332         if ((*ctx)->oid != GSS_C_NO_OID) {
333                 xfree((*ctx)->oid->elements);
334                 xfree((*ctx)->oid);
335                 (*ctx)->oid = GSS_C_NO_OID;
336         }
337         if ((*ctx)->creds != GSS_C_NO_CREDENTIAL)
338                 gss_release_cred(&ms,&(*ctx)->creds);
339         if ((*ctx)->client != GSS_C_NO_NAME)
340                 gss_release_name(&ms,&(*ctx)->client);  
341         if ((*ctx)->client_creds != GSS_C_NO_CREDENTIAL)
342                 gss_release_cred(&ms,&(*ctx)->client_creds);
343 #endif
344         
345         xfree(*ctx);
346         *ctx=NULL; 
347 }
348
349 /* Wrapper to init_sec_context 
350  * Requires that the context contains:
351  *      oid
352  *      server name (from ssh_gssapi_import_name)
353  */
354 OM_uint32 
355 ssh_gssapi_init_ctx(Gssctxt *ctx, int deleg_creds, gss_buffer_desc *recv_tok,
356                     gss_buffer_desc* send_tok, OM_uint32 *flags) 
357 {
358         OM_uint32 maj_status, min_status;
359         int deleg_flag = 0;
360         
361         if (deleg_creds) {
362                 deleg_flag=GSS_C_DELEG_FLAG;
363                 debug("Delegating credentials");
364         }
365                 
366         maj_status=gss_init_sec_context(&min_status,
367                                         GSS_C_NO_CREDENTIAL, /* def. cred */
368                                         &ctx->context,
369                                         ctx->name,
370                                         ctx->oid,
371                                         GSS_C_MUTUAL_FLAG |
372                                         GSS_C_INTEG_FLAG |
373                                         deleg_flag,
374                                         0, /* default lifetime */
375                                         NULL, /* no channel bindings */
376                                         recv_tok,
377                                         NULL,
378                                         send_tok,
379                                         flags,
380                                         NULL);
381         ctx->status=maj_status;
382         if (GSS_ERROR(maj_status)) {
383                 ssh_gssapi_error(maj_status,min_status);
384         }
385         return(maj_status);
386 }
387
388 /* Wrapper arround accept_sec_context
389  * Requires that the context contains:
390  *    oid               
391  *    credentials       (from ssh_gssapi_acquire_cred)
392  */
393 OM_uint32 ssh_gssapi_accept_ctx(Gssctxt *ctx,gss_buffer_desc *recv_tok,
394                                 gss_buffer_desc *send_tok, OM_uint32 *flags) 
395 {
396         OM_uint32 maj_status, min_status;
397         gss_OID mech;
398         
399         maj_status=gss_accept_sec_context(&min_status,
400                                           &ctx->context,
401                                           ctx->creds,
402                                           recv_tok,
403                                           GSS_C_NO_CHANNEL_BINDINGS,
404                                           &ctx->client,
405                                           &mech,
406                                           send_tok,
407                                           flags,
408                                           NULL,
409                                           &ctx->client_creds);
410         if (GSS_ERROR(maj_status)) {
411                 ssh_gssapi_send_error(maj_status,min_status);
412         }
413         
414         if (ctx->client_creds) {
415                 debug("Received some client credentials");
416         } else {
417                 debug("Got no client credentials");
418         }
419
420         /* FIXME: We should check that the me
421          * the one that we asked for (in ctx->oid) */
422
423         ctx->status=maj_status;
424         
425         /* Now, if we're complete and we have the right flags, then
426          * we flag the user as also having been authenticated
427          */
428         
429         if (((flags==NULL) || ((*flags & GSS_C_MUTUAL_FLAG) && 
430                                (*flags & GSS_C_INTEG_FLAG))) &&
431             (maj_status == GSS_S_COMPLETE)) {
432                 if (ssh_gssapi_getclient(ctx,&gssapi_client_type,
433                                          &gssapi_client_name,
434                                          &gssapi_client_creds))
435                         fatal("Couldn't convert client name");
436         }
437
438         return(maj_status);
439 }
440
441 /* Create a service name for the given host */
442 OM_uint32
443 ssh_gssapi_import_name(Gssctxt *ctx, const char *host) {
444         gss_buffer_desc gssbuf = {0,NULL};
445         OM_uint32 maj_status, min_status;
446         char *xhost;
447         
448         /* Make a copy of the host name, in case it was returned by a
449          * previous call to gethostbyname(). */ 
450         xhost = xstrdup(host);
451
452         /* If xhost is the loopback interface, switch it to our
453            true local hostname. */
454         resolve_localhost(&xhost);
455
456         /* Make sure we have the FQHN. Some GSSAPI implementations don't do
457          * this for us themselves */
458         make_fqhn(&xhost);
459
460         gssbuf.length = sizeof("host@")+strlen(xhost);
461
462         gssbuf.value = xmalloc(gssbuf.length);
463         if (gssbuf.value == NULL) {
464                 xfree(xhost);
465                 return(-1);
466         }
467         snprintf(gssbuf.value,gssbuf.length,"host@%s",xhost);
468         if ((maj_status=gss_import_name(&min_status,
469                                         &gssbuf,
470                                         GSS_C_NT_HOSTBASED_SERVICE,
471                                         &ctx->name))) {
472                 ssh_gssapi_error(maj_status,min_status);
473         }
474         
475         xfree(xhost);
476         xfree(gssbuf.value);
477         return(maj_status);
478 }
479
480 /* Acquire credentials for a server running on the current host.
481  * Requires that the context structure contains a valid OID
482  */
483  
484 /* Returns a GSSAPI error code */
485 OM_uint32
486 ssh_gssapi_acquire_cred(Gssctxt *ctx) {
487         OM_uint32 maj_status, min_status;
488         char lname[MAXHOSTNAMELEN];
489         gss_OID_set oidset;
490         
491         gss_create_empty_oid_set(&min_status,&oidset);
492         gss_add_oid_set_member(&min_status,ctx->oid,&oidset);
493         
494         if (gethostname(lname, MAXHOSTNAMELEN)) {
495                 return(-1);
496         }
497
498         if ((maj_status=ssh_gssapi_import_name(ctx,lname))) {
499                 return(maj_status);
500         }
501         if ((maj_status=gss_acquire_cred(&min_status,
502                                     ctx->name,
503                                     0,
504                                     oidset,
505                                     GSS_C_ACCEPT,
506                                     &ctx->creds,
507                                     NULL,
508                                     NULL))) {
509                 ssh_gssapi_error(maj_status,min_status);
510         }
511                                 
512         gss_release_oid_set(&min_status, &oidset);
513         return(maj_status);
514 }
515
516 /* Extract the client details from a given context. This can only reliably
517  * be called once for a context */
518
519 OM_uint32 
520 ssh_gssapi_getclient(Gssctxt *ctx, enum ssh_gss_id *type,
521                      gss_buffer_desc *name, gss_cred_id_t *creds) {
522
523         OM_uint32 maj_status,min_status;
524         
525         *type=ssh_gssapi_get_ctype(ctx);
526         if ((maj_status=gss_display_name(&min_status,ctx->client,name,NULL))) {
527                 ssh_gssapi_error(maj_status,min_status);
528         }
529         
530         /* This is icky. There appears to be no way to copy this structure,
531          * rather than the pointer to it, so we simply copy the pointer and
532          * mark the originator as empty so we don't destroy it. 
533          */
534         *creds=ctx->client_creds;
535         ctx->client_creds=GSS_C_NO_CREDENTIAL;
536         return(maj_status);
537 }
538
539 OM_uint32
540 ssh_gssapi_sign(Gssctxt *ctx, gss_buffer_desc *buffer, gss_buffer_desc *hash) {
541         OM_uint32 maj_status,min_status;
542         
543         /* ssh1 needs to exchange the hash of the keys */
544         /* will us this hash to return it */
545         if (!compat20) {
546                 if ((maj_status=gss_wrap(&min_status,ctx->context,
547                                         0,
548                                         GSS_C_QOP_DEFAULT,
549                                         buffer,
550                                         NULL,
551                                         hash)))
552                         ssh_gssapi_error(maj_status,min_status);
553         }
554         else
555
556         if ((maj_status=gss_get_mic(&min_status,ctx->context,
557                                     GSS_C_QOP_DEFAULT, buffer, hash))) {
558                 ssh_gssapi_error(maj_status,min_status);
559         }
560         
561         return(maj_status);
562 }
563
564 OM_uint32
565 ssh_gssapi_server_ctx(Gssctxt **ctx,gss_OID oid) {
566         if (*ctx) ssh_gssapi_delete_ctx(ctx);
567         ssh_gssapi_build_ctx(ctx);
568         ssh_gssapi_set_oid(*ctx,oid);
569         return(ssh_gssapi_acquire_cred(*ctx));
570 }
571
572 OM_uint32 
573 ssh_gssapi_client_ctx(Gssctxt **ctx,gss_OID oid, char *host) {
574         gss_buffer_desc token = {0,NULL};
575         OM_uint32 major,minor;
576         
577         if (*ctx) ssh_gssapi_delete_ctx(ctx);
578         ssh_gssapi_build_ctx(ctx);
579         ssh_gssapi_set_oid(*ctx,oid);
580         ssh_gssapi_import_name(*ctx,host);
581         major=ssh_gssapi_init_ctx(*ctx, 0, GSS_C_NO_BUFFER, &token, NULL);
582         gss_release_buffer(&minor,&token);
583         return(major);
584 }
585                                                                                         
586 #endif /* GSSAPI */
This page took 0.442087 seconds and 5 git commands to generate.