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