]> andersk Git - gssapi-openssh.git/blame - openssh/gss-genr.c
merged OpenSSH 4.2p1 to trunk
[gssapi-openssh.git] / openssh / gss-genr.c
CommitLineData
2ce0bfe4 1/* $OpenBSD: gss-genr.c,v 1.4 2005/07/17 07:17:55 djm Exp $ */
7cac2b65 2
5598e598 3/*
7cac2b65 4 * Copyright (c) 2001-2003 Simon Wilkinson. All rights reserved.
5 *
5598e598 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
5598e598 31#include "xmalloc.h"
32#include "buffer.h"
33#include "bufaux.h"
5598e598 34#include "compat.h"
35#include <openssl/evp.h>
5598e598 36#include "kex.h"
37#include "log.h"
b59afbfe 38#include "monitor_wrap.h"
4dd35ca9 39#include "canohost.h"
540d72c3 40#include "ssh2.h"
5598e598 41
42#include "ssh-gss.h"
43
540d72c3 44extern u_char *session_id2;
45extern u_int session_id2_len;
46
23987cb8 47typedef struct {
48 char *encoded;
49 gss_OID oid;
50} ssh_gss_kex_mapping;
51
52static ssh_gss_kex_mapping *gss_enc2oid;
5598e598 53
54/* Return a list of the gss-group1-sha1-x mechanisms supported by this
55 * program.
56 *
23987cb8 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.
5598e598 60 *
23987cb8 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.
5598e598 63 */
64
65char *
23987cb8 66ssh_gssapi_client_mechanisms(char *host) {
5598e598 67 gss_OID_set supported;
23987cb8 68 OM_uint32 min_status;
5598e598 69 Buffer buf;
70 int i = 0;
23987cb8 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
23987cb8 80 gss_indicate_mechs(&min_status,&supported);
dfddba3d 81 gss_enc2oid=xmalloc(sizeof(ssh_gss_kex_mapping)
82 *(supported->count+1));
5598e598 83
23987cb8 84 buffer_init(&buf);
85
23987cb8 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
23987cb8 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,
e23e524c 100 supported->elements[i].elements,
101 supported->elements[i].length);
23987cb8 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++;
d4ab470d 119 }
23987cb8 120 }
121 gss_enc2oid[oidpos].oid=NULL;
122 gss_enc2oid[oidpos].encoded=NULL;
5598e598 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)
23987cb8 130 return(NULL);
5598e598 131 else
23987cb8 132 return(mechs);
133}
134
135gss_OID
136ssh_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;
5598e598 155}
156
23987cb8 157/* Check that the OID in a data stream matches that in the context */
7cac2b65 158int
159ssh_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);
23987cb8 164}
7cac2b65 165
5598e598 166/* Set the contexts OID from a data stream */
7cac2b65 167void
168ssh_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);
5598e598 178}
179
180/* Set the contexts OID */
7cac2b65 181void
182ssh_gssapi_set_oid(Gssctxt *ctx, gss_OID oid)
183{
184 ssh_gssapi_set_oid_data(ctx, oid->elements, oid->length);
5598e598 185}
186
23987cb8 187/* All this effort to report an error ... */
23987cb8 188void
7cac2b65 189ssh_gssapi_error(Gssctxt *ctxt)
190{
191 debug("%s", ssh_gssapi_last_error(ctxt, NULL, NULL));
5598e598 192}
193
23987cb8 194char *
2ce0bfe4 195ssh_gssapi_last_error(Gssctxt *ctxt, OM_uint32 *major_status,
196 OM_uint32 *minor_status)
7cac2b65 197{
23987cb8 198 OM_uint32 lmin;
7cac2b65 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;
5598e598 212 /* The GSSAPI error */
7cac2b65 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);
5598e598 239}
240
7cac2b65 241/*
242 * Initialise our GSSAPI context. We use this opaque structure to contain all
5598e598 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 */
247void
b59afbfe 248ssh_gssapi_build_ctx(Gssctxt **ctx)
5598e598 249{
7cac2b65 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;
5598e598 259}
260
261/* Delete our context, providing it has been built correctly */
262void
b59afbfe 263ssh_gssapi_delete_ctx(Gssctxt **ctx)
5598e598 264{
9eeaa28e 265#if !defined(MECHGLUE)
5598e598 266 OM_uint32 ms;
9eeaa28e 267#endif
7cac2b65 268
269 if ((*ctx) == NULL)
b59afbfe 270 return;
d4ab470d 271#if !defined(MECHGLUE) /* mechglue has some memory management issues */
7cac2b65 272 if ((*ctx)->context != GSS_C_NO_CONTEXT)
273 gss_delete_sec_context(&ms, &(*ctx)->context, GSS_C_NO_BUFFER);
b59afbfe 274 if ((*ctx)->name != GSS_C_NO_NAME)
7cac2b65 275 gss_release_name(&ms, &(*ctx)->name);
b59afbfe 276 if ((*ctx)->oid != GSS_C_NO_OID) {
277 xfree((*ctx)->oid->elements);
278 xfree((*ctx)->oid);
279 (*ctx)->oid = GSS_C_NO_OID;
5598e598 280 }
b59afbfe 281 if ((*ctx)->creds != GSS_C_NO_CREDENTIAL)
7cac2b65 282 gss_release_cred(&ms, &(*ctx)->creds);
b59afbfe 283 if ((*ctx)->client != GSS_C_NO_NAME)
7cac2b65 284 gss_release_name(&ms, &(*ctx)->client);
b59afbfe 285 if ((*ctx)->client_creds != GSS_C_NO_CREDENTIAL)
7cac2b65 286 gss_release_cred(&ms, &(*ctx)->client_creds);
d4ab470d 287#endif
7cac2b65 288
b59afbfe 289 xfree(*ctx);
7cac2b65 290 *ctx = NULL;
5598e598 291}
292
7cac2b65 293/*
294 * Wrapper to init_sec_context
5598e598 295 * Requires that the context contains:
296 * oid
7cac2b65 297 * server name (from ssh_gssapi_import_name)
5598e598 298 */
7cac2b65 299OM_uint32
5598e598 300ssh_gssapi_init_ctx(Gssctxt *ctx, int deleg_creds, gss_buffer_desc *recv_tok,
7cac2b65 301 gss_buffer_desc* send_tok, OM_uint32 *flags)
5598e598 302{
5598e598 303 int deleg_flag = 0;
7cac2b65 304
5598e598 305 if (deleg_creds) {
7cac2b65 306 deleg_flag = GSS_C_DELEG_FLAG;
5598e598 307 debug("Delegating credentials");
308 }
7cac2b65 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);
5598e598 319}
320
5598e598 321/* Create a service name for the given host */
322OM_uint32
7cac2b65 323ssh_gssapi_import_name(Gssctxt *ctx, const char *host)
324{
23987cb8 325 gss_buffer_desc gssbuf;
4dd35ca9 326 char *xhost;
7cac2b65 327
5598e598 328 /* Make a copy of the host name, in case it was returned by a
e23e524c 329 * previous call to gethostbyname(). */
5598e598 330 xhost = xstrdup(host);
331
23987cb8 332 /* Make sure we have the FQDN. Some GSSAPI implementations don't do
e23e524c 333 * this for us themselves */
1df48666 334 resolve_localhost(&xhost);
23987cb8 335
7cac2b65 336 gssbuf.length = sizeof("host@") + strlen(xhost);
337 gssbuf.value = xmalloc(gssbuf.length);
5682ddfd 338 snprintf(gssbuf.value, gssbuf.length, "host@%s", xhost);
7cac2b65 339
340 if ((ctx->major = gss_import_name(&ctx->minor,
341 &gssbuf, GSS_C_NT_HOSTBASED_SERVICE, &ctx->name)))
23987cb8 342 ssh_gssapi_error(ctx);
7cac2b65 343
1c828c5c 344 xfree(xhost);
5598e598 345 xfree(gssbuf.value);
7cac2b65 346 return (ctx->major);
5598e598 347}
348
349/* Acquire credentials for a server running on the current host.
350 * Requires that the context structure contains a valid OID
b59afbfe 351 */
7cac2b65 352
b59afbfe 353/* Returns a GSSAPI error code */
5598e598 354OM_uint32
7cac2b65 355ssh_gssapi_acquire_cred(Gssctxt *ctx)
356{
23987cb8 357 OM_uint32 status;
5598e598 358 char lname[MAXHOSTNAMELEN];
359 gss_OID_set oidset;
5598e598 360
7cac2b65 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)))
23987cb8 372 ssh_gssapi_error(ctx);
7cac2b65 373
23987cb8 374 gss_release_oid_set(&status, &oidset);
7cac2b65 375 return (ctx->major);
376}
377
540d72c3 378OM_uint32
379ssh_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
388void
389ssh_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
7cac2b65 400OM_uint32
401ssh_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));
5598e598 407}
b59afbfe 408
23987cb8 409int
40883c8b 410ssh_gssapi_check_mechanism(gss_OID oid, const char *host) {
23987cb8 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);
b59afbfe 419 gss_release_buffer(&minor,&token);
23987cb8 420 ssh_gssapi_delete_ctx(&ctx);
421 return(!GSS_ERROR(major));
b59afbfe 422}
f001f132 423
5598e598 424#endif /* GSSAPI */
This page took 0.135444 seconds and 5 git commands to generate.