]> andersk Git - gssapi-openssh.git/blob - openssh/gss-genr.c
initialize pointers to avoid compiler warnings
[gssapi-openssh.git] / openssh / gss-genr.c
1 /*      $OpenBSD: gss-genr.c,v 1.4 2005/07/17 07:17:55 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         size_t          i = 0;
71         char            *mechs;
72         char            *encoded;
73         int             enclen;
74         unsigned 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         gss_indicate_mechs(&min_status,&supported);
81         gss_enc2oid=xmalloc(sizeof(ssh_gss_kex_mapping)
82                             *(supported->count+1));
83         
84         buffer_init(&buf);
85
86         for (i=0;i<supported->count;i++) {
87
88                 gss_enc2oid[oidpos].encoded=NULL;
89                 
90                 if (supported->elements[i].length<128 &&
91                     ssh_gssapi_check_mechanism(&(supported->elements[i]),host)) {
92
93                         /* Add the required DER encoding octets and MD5 hash */
94                         deroid[0]=0x06; /* Object Identifier */
95                         deroid[1]=supported->elements[i].length;
96
97                         EVP_DigestInit(&md, evp_md);
98                         EVP_DigestUpdate(&md,deroid,2);
99                         EVP_DigestUpdate(&md,
100                                          supported->elements[i].elements,
101                                          supported->elements[i].length);
102                         EVP_DigestFinal(&md, digest, NULL);
103                         
104                         /* Base64 encode it */
105                         encoded=xmalloc(EVP_MD_size(evp_md)*2);
106                         enclen=__b64_ntop(digest, EVP_MD_size(evp_md),
107                                           encoded,EVP_MD_size(evp_md)*2);
108                         if (oidpos!=0) {
109                                 buffer_put_char(&buf,',');
110                         }       
111                         buffer_append(&buf, KEX_GSS_SHA1, sizeof(KEX_GSS_SHA1)-1);
112                         buffer_append(&buf, encoded, enclen);
113
114                         debug("Mechanism encoded as %s",encoded);
115
116                         gss_enc2oid[oidpos].oid=&(supported->elements[i]);
117                         gss_enc2oid[oidpos].encoded=encoded;                    
118                         oidpos++;
119                 }
120         }
121         gss_enc2oid[oidpos].oid=NULL;
122         gss_enc2oid[oidpos].encoded=NULL;
123         
124         buffer_put_char(&buf,'\0');
125         
126         mechs=xmalloc(buffer_len(&buf));
127         buffer_get(&buf,mechs,buffer_len(&buf));
128         buffer_free(&buf);
129         if (strlen(mechs)==0)
130                 return(NULL);
131         else
132                 return(mechs);
133 }
134
135 gss_OID
136 ssh_gssapi_client_id_kex(Gssctxt *ctx, char *name) {
137         int i=0;
138         
139         if (strncmp(name, KEX_GSS_SHA1, sizeof(KEX_GSS_SHA1)-1) !=0) {
140                 return(NULL);
141         }
142         
143         name+=sizeof(KEX_GSS_SHA1)-1; /* Move to the start of the ID string */
144         
145         while (gss_enc2oid[i].encoded!=NULL &&
146                 strcmp(name,gss_enc2oid[i].encoded)!=0) {
147                 i++;
148         }
149         
150         if (gss_enc2oid[i].oid!=NULL) {
151                 ssh_gssapi_set_oid(ctx,gss_enc2oid[i].oid);
152         }
153
154         return gss_enc2oid[i].oid;
155 }
156
157 /* Check that the OID in a data stream matches that in the context */
158 int
159 ssh_gssapi_check_oid(Gssctxt *ctx, void *data, size_t len)
160 {
161         return (ctx != NULL && ctx->oid != GSS_C_NO_OID &&
162             ctx->oid->length == len &&
163             memcmp(ctx->oid->elements, data, len) == 0);
164 }
165
166 /* Set the contexts OID from a data stream */
167 void
168 ssh_gssapi_set_oid_data(Gssctxt *ctx, void *data, size_t len)
169 {
170         if (ctx->oid != GSS_C_NO_OID) {
171                 xfree(ctx->oid->elements);
172                 xfree(ctx->oid);
173         }
174         ctx->oid = xmalloc(sizeof(gss_OID_desc));
175         ctx->oid->length = len;
176         ctx->oid->elements = xmalloc(len);
177         memcpy(ctx->oid->elements, data, len);
178 }
179
180 /* Set the contexts OID */
181 void
182 ssh_gssapi_set_oid(Gssctxt *ctx, gss_OID oid)
183 {
184         ssh_gssapi_set_oid_data(ctx, oid->elements, oid->length);
185 }
186
187 /* All this effort to report an error ... */
188 void
189 ssh_gssapi_error(Gssctxt *ctxt)
190 {
191         debug("%s", ssh_gssapi_last_error(ctxt, NULL, NULL));
192 }
193
194 char *
195 ssh_gssapi_last_error(Gssctxt *ctxt, OM_uint32 *major_status,
196     OM_uint32 *minor_status)
197 {
198         OM_uint32 lmin;
199         gss_buffer_desc msg = GSS_C_EMPTY_BUFFER;
200         OM_uint32 ctx;
201         Buffer b;
202         char *ret;
203
204         buffer_init(&b);
205
206         if (major_status != NULL)
207                 *major_status = ctxt->major;
208         if (minor_status != NULL)
209                 *minor_status = ctxt->minor;
210
211         ctx = 0;
212         /* The GSSAPI error */
213         do {
214                 gss_display_status(&lmin, ctxt->major,
215                     GSS_C_GSS_CODE, ctxt->oid, &ctx, &msg);
216
217                 buffer_append(&b, msg.value, msg.length);
218                 buffer_put_char(&b, '\n');
219
220                 gss_release_buffer(&lmin, &msg);
221         } while (ctx != 0);
222
223         /* The mechanism specific error */
224         do {
225                 gss_display_status(&lmin, ctxt->minor,
226                     GSS_C_MECH_CODE, ctxt->oid, &ctx, &msg);
227
228                 buffer_append(&b, msg.value, msg.length);
229                 buffer_put_char(&b, '\n');
230
231                 gss_release_buffer(&lmin, &msg);
232         } while (ctx != 0);
233
234         buffer_put_char(&b, '\0');
235         ret = xmalloc(buffer_len(&b));
236         buffer_get(&b, ret, buffer_len(&b));
237         buffer_free(&b);
238         return (ret);
239 }
240
241 /*
242  * Initialise our GSSAPI context. We use this opaque structure to contain all
243  * of the data which both the client and server need to persist across
244  * {accept,init}_sec_context calls, so that when we do it from the userauth
245  * stuff life is a little easier
246  */
247 void
248 ssh_gssapi_build_ctx(Gssctxt **ctx)
249 {
250         *ctx = xmalloc(sizeof (Gssctxt));
251         (*ctx)->major = 0;
252         (*ctx)->minor = 0;
253         (*ctx)->context = GSS_C_NO_CONTEXT;
254         (*ctx)->name = GSS_C_NO_NAME;
255         (*ctx)->oid = GSS_C_NO_OID;
256         (*ctx)->creds = GSS_C_NO_CREDENTIAL;
257         (*ctx)->client = GSS_C_NO_NAME;
258         (*ctx)->client_creds = GSS_C_NO_CREDENTIAL;
259 }
260
261 /* Delete our context, providing it has been built correctly */
262 void
263 ssh_gssapi_delete_ctx(Gssctxt **ctx)
264 {
265 #if !defined(MECHGLUE)
266         OM_uint32 ms;
267 #endif
268
269         if ((*ctx) == NULL)
270                 return;
271 #if !defined(MECHGLUE) /* mechglue has some memory management issues */
272         if ((*ctx)->context != GSS_C_NO_CONTEXT)
273                 gss_delete_sec_context(&ms, &(*ctx)->context, GSS_C_NO_BUFFER);
274         if ((*ctx)->name != GSS_C_NO_NAME)
275                 gss_release_name(&ms, &(*ctx)->name);
276         if ((*ctx)->oid != GSS_C_NO_OID) {
277                 xfree((*ctx)->oid->elements);
278                 xfree((*ctx)->oid);
279                 (*ctx)->oid = GSS_C_NO_OID;
280         }
281         if ((*ctx)->creds != GSS_C_NO_CREDENTIAL)
282                 gss_release_cred(&ms, &(*ctx)->creds);
283         if ((*ctx)->client != GSS_C_NO_NAME)
284                 gss_release_name(&ms, &(*ctx)->client);
285         if ((*ctx)->client_creds != GSS_C_NO_CREDENTIAL)
286                 gss_release_cred(&ms, &(*ctx)->client_creds);
287 #endif
288
289         xfree(*ctx);
290         *ctx = NULL;
291 }
292
293 /*
294  * Wrapper to init_sec_context
295  * Requires that the context contains:
296  *      oid
297  *      server name (from ssh_gssapi_import_name)
298  */
299 OM_uint32
300 ssh_gssapi_init_ctx(Gssctxt *ctx, int deleg_creds, gss_buffer_desc *recv_tok,
301     gss_buffer_desc* send_tok, OM_uint32 *flags)
302 {
303         int deleg_flag = 0;
304
305         if (deleg_creds) {
306                 deleg_flag = GSS_C_DELEG_FLAG;
307                 debug("Delegating credentials");
308         }
309
310         ctx->major = gss_init_sec_context(&ctx->minor,
311             GSS_C_NO_CREDENTIAL, &ctx->context, ctx->name, ctx->oid,
312             GSS_C_MUTUAL_FLAG | GSS_C_INTEG_FLAG | deleg_flag,
313             0, NULL, recv_tok, NULL, send_tok, flags, NULL);
314
315         if (GSS_ERROR(ctx->major))
316                 ssh_gssapi_error(ctx);
317
318         return (ctx->major);
319 }
320
321 /* Create a service name for the given host */
322 OM_uint32
323 ssh_gssapi_import_name(Gssctxt *ctx, const char *host)
324 {
325         gss_buffer_desc gssbuf;
326         char *xhost;
327
328         /* Make a copy of the host name, in case it was returned by a
329          * previous call to gethostbyname(). */ 
330         xhost = xstrdup(host);
331
332         /* Make sure we have the FQDN. Some GSSAPI implementations don't do
333          * this for us themselves */
334         resolve_localhost(&xhost);
335         
336         gssbuf.length = sizeof("host@") + strlen(xhost);
337         gssbuf.value = xmalloc(gssbuf.length);
338         snprintf(gssbuf.value, gssbuf.length, "host@%s", xhost);
339
340         if ((ctx->major = gss_import_name(&ctx->minor,
341             &gssbuf, GSS_C_NT_HOSTBASED_SERVICE, &ctx->name)))
342                 ssh_gssapi_error(ctx);
343
344         xfree(xhost);
345         xfree(gssbuf.value);
346         return (ctx->major);
347 }
348
349 /* Acquire credentials for a server running on the current host.
350  * Requires that the context structure contains a valid OID
351  */
352
353 /* Returns a GSSAPI error code */
354 OM_uint32
355 ssh_gssapi_acquire_cred(Gssctxt *ctx)
356 {
357         OM_uint32 status;
358         char lname[MAXHOSTNAMELEN];
359         gss_OID_set oidset;
360
361         gss_create_empty_oid_set(&status, &oidset);
362         gss_add_oid_set_member(&status, ctx->oid, &oidset);
363
364         if (gethostname(lname, MAXHOSTNAMELEN))
365                 return (-1);
366
367         if (GSS_ERROR(ssh_gssapi_import_name(ctx, lname)))
368                 return (ctx->major);
369
370         if ((ctx->major = gss_acquire_cred(&ctx->minor,
371             ctx->name, 0, oidset, GSS_C_ACCEPT, &ctx->creds, NULL, NULL)))
372                 ssh_gssapi_error(ctx);
373
374         gss_release_oid_set(&status, &oidset);
375         return (ctx->major);
376 }
377
378 OM_uint32
379 ssh_gssapi_sign(Gssctxt *ctx, gss_buffer_t buffer, gss_buffer_t hash)
380 {
381         if ((ctx->major = gss_get_mic(&ctx->minor, ctx->context,
382             GSS_C_QOP_DEFAULT, buffer, hash)))
383                 ssh_gssapi_error(ctx);
384
385         return (ctx->major);
386 }
387
388 void
389 ssh_gssapi_buildmic(Buffer *b, const char *user, const char *service,
390     const char *context)
391 {
392         buffer_init(b);
393         buffer_put_string(b, session_id2, session_id2_len);
394         buffer_put_char(b, SSH2_MSG_USERAUTH_REQUEST);
395         buffer_put_cstring(b, user);
396         buffer_put_cstring(b, service);
397         buffer_put_cstring(b, context);
398 }
399
400 OM_uint32
401 ssh_gssapi_server_ctx(Gssctxt **ctx, gss_OID oid) {
402         if (*ctx)
403                 ssh_gssapi_delete_ctx(ctx);
404         ssh_gssapi_build_ctx(ctx);
405         ssh_gssapi_set_oid(*ctx, oid);
406         return (ssh_gssapi_acquire_cred(*ctx));
407 }
408
409 int
410 ssh_gssapi_check_mechanism(gss_OID oid, const char *host) {
411         Gssctxt * ctx = NULL;
412         gss_buffer_desc token;
413         OM_uint32 major,minor;
414         
415         ssh_gssapi_build_ctx(&ctx);
416         ssh_gssapi_set_oid(ctx,oid);
417         ssh_gssapi_import_name(ctx,host);
418         major=ssh_gssapi_init_ctx(ctx,0, GSS_C_NO_BUFFER, &token, NULL);
419         gss_release_buffer(&minor,&token);
420         ssh_gssapi_delete_ctx(&ctx);
421         return(!GSS_ERROR(major));
422 }
423
424 #endif /* GSSAPI */
This page took 0.130916 seconds and 5 git commands to generate.