]> andersk Git - gssapi-openssh.git/blob - openssh/gss-genr.c
Initial revision
[gssapi-openssh.git] / openssh / gss-genr.c
1 /*      $OpenBSD: gss-genr.c,v 1.3 2003/11/21 11:57:03 djm Exp $        */
2
3 /*
4  * Copyright (c) 2001-2003 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 "buffer.h"
33 #include "bufaux.h"
34 #include "compat.h"
35 #include <openssl/evp.h>
36 #include "kex.h"
37 #include "log.h"
38 #include "monitor_wrap.h"
39 #include "canohost.h"
40 #include "ssh2.h"
41
42 #include "ssh-gss.h"
43
44 extern u_char *session_id2;
45 extern u_int session_id2_len;
46
47 typedef struct {
48         char *encoded;
49         gss_OID oid;
50 } ssh_gss_kex_mapping;
51         
52 static ssh_gss_kex_mapping *gss_enc2oid;
53
54 /* Return a list of the gss-group1-sha1-x mechanisms supported by this
55  * program.
56  *
57  * On the client side, we don't need to worry about whether we 'know'
58  * about the mechanism or not - we assume that any mechanism that we've been
59  * linked against is suitable for inclusion.
60  *
61  * XXX - We might want to make this configurable in the future, so as to
62  * XXX - allow the user control over which mechanisms to use.
63  */
64  
65 char * 
66 ssh_gssapi_client_mechanisms(char *host) {
67         gss_OID_set     supported;
68         OM_uint32       min_status;
69         Buffer          buf;
70         int             i = 0;
71         char            *mechs;
72         char            *encoded;
73         int             enclen;
74         char            digest[EVP_MAX_MD_SIZE];
75         char            deroid[2];
76         const EVP_MD    *evp_md = EVP_md5();
77         EVP_MD_CTX      md;
78         int             oidpos=0;
79         
80         if (datafellows & SSH_OLD_GSSAPI) return NULL;
81         
82         gss_indicate_mechs(&min_status,&supported);
83         if (datafellows & SSH_BUG_GSSAPI_BER) {
84                 gss_enc2oid=xmalloc(sizeof(ssh_gss_kex_mapping)
85                                         *((supported->count*2)+1));
86         } else {
87                 gss_enc2oid=xmalloc(sizeof(ssh_gss_kex_mapping)
88                                         *(supported->count+1));
89         }
90         
91         buffer_init(&buf);
92
93
94         for (i=0;i<supported->count;i++) {
95
96                 gss_enc2oid[oidpos].encoded=NULL;
97                 
98                 if (supported->elements[i].length<128 &&
99                     ssh_gssapi_check_mechanism(&(supported->elements[i]),host)) {
100
101                         /* Earlier versions of this code interpreted the
102                          * spec incorrectly with regard to OID encoding. They
103                          * also mis-encoded the krb5 OID. The following
104                          * _temporary_ code interfaces with these broken
105                          * servers */
106
107                         if (datafellows & SSH_BUG_GSSAPI_BER) {
108                                 char *bodge=NULL;
109                                 gss_OID_desc krb5oid={9, "\x2A\x86\x48\x86\xF7\x12\x01\x02\x02"};
110                                 gss_OID_desc gsioid={9, "\x2B\x06\x01\x04\x01\x9B\x50\x01\x01"};
111                                 
112                                 if (supported->elements[i].length==krb5oid.length &&
113                                     memcmp(supported->elements[i].elements,
114                                            krb5oid.elements, krb5oid.length)==0) {
115                                         bodge="Se3H81ismmOC3OE+FwYCiQ==";
116                                 }
117                                 
118                                 if (supported->elements[i].length==gsioid.length &&
119                                     memcmp(supported->elements[i].elements,
120                                            gsioid.elements, gsioid.length)==0) {
121                                         bodge="N3+k7/4wGxHyuP8Yxi4RhA==";
122                                 }
123
124                                 if (bodge) {                            
125                                         if (oidpos!=0) {
126                                                 buffer_put_char(&buf,',');
127                                         }
128                                 
129                                         buffer_append(&buf, KEX_GSS_SHA1, sizeof(KEX_GSS_SHA1)-1);
130                                         buffer_append(&buf, bodge, strlen(bodge));
131
132                                         gss_enc2oid[oidpos].oid=&(supported->elements[i]);
133                                         gss_enc2oid[oidpos].encoded=bodge;
134                         
135                                         oidpos++;
136                                 }
137                         }
138                         
139                         /* Add the required DER encoding octets and MD5 hash */
140                         deroid[0]=0x06; /* Object Identifier */
141                         deroid[1]=supported->elements[i].length;
142
143                         EVP_DigestInit(&md, evp_md);
144                         EVP_DigestUpdate(&md,deroid,2);
145                         EVP_DigestUpdate(&md,
146                                          supported->elements[i].elements,
147                                          supported->elements[i].length);
148                         EVP_DigestFinal(&md, digest, NULL);
149                         
150                         /* Base64 encode it */
151                         encoded=xmalloc(EVP_MD_size(evp_md)*2);
152                         enclen=__b64_ntop(digest, EVP_MD_size(evp_md),
153                                           encoded,EVP_MD_size(evp_md)*2);
154                         if (oidpos!=0) {
155                                 buffer_put_char(&buf,',');
156                         }       
157                         buffer_append(&buf, KEX_GSS_SHA1, sizeof(KEX_GSS_SHA1)-1);
158                         buffer_append(&buf, encoded, enclen);
159
160                         debug("Mechanism encoded as %s",encoded);
161
162                         gss_enc2oid[oidpos].oid=&(supported->elements[i]);
163                         gss_enc2oid[oidpos].encoded=encoded;                    
164                         oidpos++;
165                 }
166         }
167         gss_enc2oid[oidpos].oid=NULL;
168         gss_enc2oid[oidpos].encoded=NULL;
169         
170         buffer_put_char(&buf,'\0');
171         
172         mechs=xmalloc(buffer_len(&buf));
173         buffer_get(&buf,mechs,buffer_len(&buf));
174         buffer_free(&buf);
175         if (strlen(mechs)==0)
176                 return(NULL);
177         else
178                 return(mechs);
179 }
180
181 gss_OID
182 ssh_gssapi_client_id_kex(Gssctxt *ctx, char *name) {
183         int i=0;
184         
185         if (strncmp(name, KEX_GSS_SHA1, sizeof(KEX_GSS_SHA1)-1) !=0) {
186                 return(NULL);
187         }
188         
189         name+=sizeof(KEX_GSS_SHA1)-1; /* Move to the start of the ID string */
190         
191         while (gss_enc2oid[i].encoded!=NULL &&
192                 strcmp(name,gss_enc2oid[i].encoded)!=0) {
193                 i++;
194         }
195         
196         if (gss_enc2oid[i].oid!=NULL) {
197                 ssh_gssapi_set_oid(ctx,gss_enc2oid[i].oid);
198         }
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         debug("%s", ssh_gssapi_last_error(ctxt, NULL, NULL));
238 }
239
240 char *
241 ssh_gssapi_last_error(Gssctxt *ctxt,
242                       OM_uint32 *major_status, OM_uint32 *minor_status)
243 {
244         OM_uint32 lmin;
245         gss_buffer_desc msg = GSS_C_EMPTY_BUFFER;
246         OM_uint32 ctx;
247         Buffer b;
248         char *ret;
249
250         buffer_init(&b);
251
252         if (major_status != NULL)
253                 *major_status = ctxt->major;
254         if (minor_status != NULL)
255                 *minor_status = ctxt->minor;
256
257         ctx = 0;
258         /* The GSSAPI error */
259         do {
260                 gss_display_status(&lmin, ctxt->major,
261                     GSS_C_GSS_CODE, ctxt->oid, &ctx, &msg);
262
263                 buffer_append(&b, msg.value, msg.length);
264                 buffer_put_char(&b, '\n');
265
266                 gss_release_buffer(&lmin, &msg);
267         } while (ctx != 0);
268
269         /* The mechanism specific error */
270         do {
271                 gss_display_status(&lmin, ctxt->minor,
272                     GSS_C_MECH_CODE, ctxt->oid, &ctx, &msg);
273
274                 buffer_append(&b, msg.value, msg.length);
275                 buffer_put_char(&b, '\n');
276
277                 gss_release_buffer(&lmin, &msg);
278         } while (ctx != 0);
279
280         buffer_put_char(&b, '\0');
281         ret = xmalloc(buffer_len(&b));
282         buffer_get(&b, ret, buffer_len(&b));
283         buffer_free(&b);
284         return (ret);
285 }
286
287 /*
288  * Initialise our GSSAPI context. We use this opaque structure to contain all
289  * of the data which both the client and server need to persist across
290  * {accept,init}_sec_context calls, so that when we do it from the userauth
291  * stuff life is a little easier
292  */
293 void
294 ssh_gssapi_build_ctx(Gssctxt **ctx)
295 {
296         *ctx = xmalloc(sizeof (Gssctxt));
297         (*ctx)->major = 0;
298         (*ctx)->minor = 0;
299         (*ctx)->context = GSS_C_NO_CONTEXT;
300         (*ctx)->name = GSS_C_NO_NAME;
301         (*ctx)->oid = GSS_C_NO_OID;
302         (*ctx)->creds = GSS_C_NO_CREDENTIAL;
303         (*ctx)->client = GSS_C_NO_NAME;
304         (*ctx)->client_creds = GSS_C_NO_CREDENTIAL;
305 }
306
307 /* Delete our context, providing it has been built correctly */
308 void
309 ssh_gssapi_delete_ctx(Gssctxt **ctx)
310 {
311 #if !defined(MECHGLUE)
312         OM_uint32 ms;
313 #endif
314
315         if ((*ctx) == NULL)
316                 return;
317 #if !defined(MECHGLUE) /* mechglue has some memory management issues */
318         if ((*ctx)->context != GSS_C_NO_CONTEXT)
319                 gss_delete_sec_context(&ms, &(*ctx)->context, GSS_C_NO_BUFFER);
320         if ((*ctx)->name != GSS_C_NO_NAME)
321                 gss_release_name(&ms, &(*ctx)->name);
322         if ((*ctx)->oid != GSS_C_NO_OID) {
323                 xfree((*ctx)->oid->elements);
324                 xfree((*ctx)->oid);
325                 (*ctx)->oid = GSS_C_NO_OID;
326         }
327         if ((*ctx)->creds != GSS_C_NO_CREDENTIAL)
328                 gss_release_cred(&ms, &(*ctx)->creds);
329         if ((*ctx)->client != GSS_C_NO_NAME)
330                 gss_release_name(&ms, &(*ctx)->client);
331         if ((*ctx)->client_creds != GSS_C_NO_CREDENTIAL)
332                 gss_release_cred(&ms, &(*ctx)->client_creds);
333 #endif
334
335         xfree(*ctx);
336         *ctx = NULL;
337 }
338
339 /*
340  * Wrapper to init_sec_context
341  * Requires that the context contains:
342  *      oid
343  *      server name (from ssh_gssapi_import_name)
344  */
345 OM_uint32
346 ssh_gssapi_init_ctx(Gssctxt *ctx, int deleg_creds, gss_buffer_desc *recv_tok,
347     gss_buffer_desc* send_tok, OM_uint32 *flags)
348 {
349         int deleg_flag = 0;
350
351         if (deleg_creds) {
352                 deleg_flag = GSS_C_DELEG_FLAG;
353                 debug("Delegating credentials");
354         }
355
356         ctx->major = gss_init_sec_context(&ctx->minor,
357             GSS_C_NO_CREDENTIAL, &ctx->context, ctx->name, ctx->oid,
358             GSS_C_MUTUAL_FLAG | GSS_C_INTEG_FLAG | deleg_flag,
359             0, NULL, recv_tok, NULL, send_tok, flags, NULL);
360
361         if (GSS_ERROR(ctx->major))
362                 ssh_gssapi_error(ctx);
363
364         return (ctx->major);
365 }
366
367 /* Create a service name for the given host */
368 OM_uint32
369 ssh_gssapi_import_name(Gssctxt *ctx, const char *host)
370 {
371         gss_buffer_desc gssbuf;
372         char *xhost;
373
374         /* Make a copy of the host name, in case it was returned by a
375          * previous call to gethostbyname(). */ 
376         xhost = xstrdup(host);
377
378         /* Make sure we have the FQDN. Some GSSAPI implementations don't do
379          * this for us themselves */
380         resolve_localhost(&xhost);
381         
382         gssbuf.length = sizeof("host@") + strlen(xhost);
383         gssbuf.value = xmalloc(gssbuf.length);
384         snprintf(gssbuf.value, gssbuf.length, "host@%s", xhost);
385
386         if ((ctx->major = gss_import_name(&ctx->minor,
387             &gssbuf, GSS_C_NT_HOSTBASED_SERVICE, &ctx->name)))
388                 ssh_gssapi_error(ctx);
389
390         xfree(xhost);
391         xfree(gssbuf.value);
392         return (ctx->major);
393 }
394
395 /* Acquire credentials for a server running on the current host.
396  * Requires that the context structure contains a valid OID
397  */
398
399 /* Returns a GSSAPI error code */
400 OM_uint32
401 ssh_gssapi_acquire_cred(Gssctxt *ctx)
402 {
403         OM_uint32 status;
404         char lname[MAXHOSTNAMELEN];
405         gss_OID_set oidset;
406
407         gss_create_empty_oid_set(&status, &oidset);
408         gss_add_oid_set_member(&status, ctx->oid, &oidset);
409
410         if (gethostname(lname, MAXHOSTNAMELEN))
411                 return (-1);
412
413         if (GSS_ERROR(ssh_gssapi_import_name(ctx, lname)))
414                 return (ctx->major);
415
416         if ((ctx->major = gss_acquire_cred(&ctx->minor,
417             ctx->name, 0, oidset, GSS_C_ACCEPT, &ctx->creds, NULL, NULL)))
418                 ssh_gssapi_error(ctx);
419
420         gss_release_oid_set(&status, &oidset);
421         return (ctx->major);
422 }
423
424 OM_uint32
425 ssh_gssapi_sign(Gssctxt *ctx, gss_buffer_t buffer, gss_buffer_t hash)
426 {
427         if ((ctx->major = gss_get_mic(&ctx->minor, ctx->context,
428             GSS_C_QOP_DEFAULT, buffer, hash)))
429                 ssh_gssapi_error(ctx);
430
431         return (ctx->major);
432 }
433
434 void
435 ssh_gssapi_buildmic(Buffer *b, const char *user, const char *service,
436     const char *context)
437 {
438         buffer_init(b);
439         buffer_put_string(b, session_id2, session_id2_len);
440         buffer_put_char(b, SSH2_MSG_USERAUTH_REQUEST);
441         buffer_put_cstring(b, user);
442         buffer_put_cstring(b, service);
443         buffer_put_cstring(b, context);
444 }
445
446 OM_uint32
447 ssh_gssapi_server_ctx(Gssctxt **ctx, gss_OID oid) {
448         if (*ctx)
449                 ssh_gssapi_delete_ctx(ctx);
450         ssh_gssapi_build_ctx(ctx);
451         ssh_gssapi_set_oid(*ctx, oid);
452         return (ssh_gssapi_acquire_cred(*ctx));
453 }
454
455 int
456 ssh_gssapi_check_mechanism(gss_OID oid, char *host) {
457         Gssctxt * ctx = NULL;
458         gss_buffer_desc token;
459         OM_uint32 major,minor;
460         
461         ssh_gssapi_build_ctx(&ctx);
462         ssh_gssapi_set_oid(ctx,oid);
463         ssh_gssapi_import_name(ctx,host);
464         major=ssh_gssapi_init_ctx(ctx,0, GSS_C_NO_BUFFER, &token, NULL);
465         gss_release_buffer(&minor,&token);
466         ssh_gssapi_delete_ctx(&ctx);
467         return(!GSS_ERROR(major));
468 }
469
470 #endif /* GSSAPI */
This page took 1.835465 seconds and 5 git commands to generate.