]> andersk Git - gssapi-openssh.git/blame - openssh/gss-genr.c
remove unneeded #include "compat.h"
[gssapi-openssh.git] / openssh / gss-genr.c
CommitLineData
7cac2b65 1/* $OpenBSD: gss-genr.c,v 1.1 2003/08/22 10:56:09 markus Exp $ */
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
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"
905081a4 42#include "compat.h"
b59afbfe 43#include "monitor_wrap.h"
4dd35ca9 44#include "canohost.h"
5598e598 45
46#include <netdb.h>
47
48#include "ssh-gss.h"
49
23987cb8 50typedef struct {
51 char *encoded;
52 gss_OID oid;
53} ssh_gss_kex_mapping;
54
55static ssh_gss_kex_mapping *gss_enc2oid;
5598e598 56
57/* Return a list of the gss-group1-sha1-x mechanisms supported by this
58 * program.
59 *
23987cb8 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.
5598e598 63 *
23987cb8 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.
5598e598 66 */
67
68char *
23987cb8 69ssh_gssapi_client_mechanisms(char *host) {
5598e598 70 gss_OID_set supported;
23987cb8 71 OM_uint32 min_status;
5598e598 72 Buffer buf;
73 int i = 0;
23987cb8 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
905081a4 83 if (datafellows & SSH_OLD_GSSAPI) return NULL;
84
23987cb8 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 }
5598e598 93
23987cb8 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
e23e524c 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 */
23987cb8 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++;
d4ab470d 139 }
d4ab470d 140 }
23987cb8 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,
e23e524c 149 supported->elements[i].elements,
150 supported->elements[i].length);
23987cb8 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++;
d4ab470d 168 }
23987cb8 169 }
170 gss_enc2oid[oidpos].oid=NULL;
171 gss_enc2oid[oidpos].encoded=NULL;
5598e598 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)
23987cb8 179 return(NULL);
5598e598 180 else
23987cb8 181 return(mechs);
182}
183
184gss_OID
185ssh_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;
5598e598 204}
205
23987cb8 206/* Check that the OID in a data stream matches that in the context */
7cac2b65 207int
208ssh_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);
23987cb8 213}
7cac2b65 214
5598e598 215/* Set the contexts OID from a data stream */
7cac2b65 216void
217ssh_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);
5598e598 227}
228
229/* Set the contexts OID */
7cac2b65 230void
231ssh_gssapi_set_oid(Gssctxt *ctx, gss_OID oid)
232{
233 ssh_gssapi_set_oid_data(ctx, oid->elements, oid->length);
5598e598 234}
235
23987cb8 236/* All this effort to report an error ... */
23987cb8 237void
7cac2b65 238ssh_gssapi_error(Gssctxt *ctxt)
239{
240 debug("%s", ssh_gssapi_last_error(ctxt, NULL, NULL));
5598e598 241}
242
23987cb8 243char *
7cac2b65 244ssh_gssapi_last_error(Gssctxt *ctxt,
245 OM_uint32 *major_status, OM_uint32 *minor_status)
246{
23987cb8 247 OM_uint32 lmin;
7cac2b65 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;
5598e598 261 /* The GSSAPI error */
7cac2b65 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);
5598e598 288}
289
7cac2b65 290/*
291 * Initialise our GSSAPI context. We use this opaque structure to contain all
5598e598 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 */
296void
b59afbfe 297ssh_gssapi_build_ctx(Gssctxt **ctx)
5598e598 298{
7cac2b65 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;
5598e598 308}
309
310/* Delete our context, providing it has been built correctly */
311void
b59afbfe 312ssh_gssapi_delete_ctx(Gssctxt **ctx)
5598e598 313{
9eeaa28e 314#if !defined(MECHGLUE)
5598e598 315 OM_uint32 ms;
9eeaa28e 316#endif
7cac2b65 317
318 if ((*ctx) == NULL)
b59afbfe 319 return;
d4ab470d 320#if !defined(MECHGLUE) /* mechglue has some memory management issues */
7cac2b65 321 if ((*ctx)->context != GSS_C_NO_CONTEXT)
322 gss_delete_sec_context(&ms, &(*ctx)->context, GSS_C_NO_BUFFER);
b59afbfe 323 if ((*ctx)->name != GSS_C_NO_NAME)
7cac2b65 324 gss_release_name(&ms, &(*ctx)->name);
b59afbfe 325 if ((*ctx)->oid != GSS_C_NO_OID) {
326 xfree((*ctx)->oid->elements);
327 xfree((*ctx)->oid);
328 (*ctx)->oid = GSS_C_NO_OID;
5598e598 329 }
b59afbfe 330 if ((*ctx)->creds != GSS_C_NO_CREDENTIAL)
7cac2b65 331 gss_release_cred(&ms, &(*ctx)->creds);
b59afbfe 332 if ((*ctx)->client != GSS_C_NO_NAME)
7cac2b65 333 gss_release_name(&ms, &(*ctx)->client);
b59afbfe 334 if ((*ctx)->client_creds != GSS_C_NO_CREDENTIAL)
7cac2b65 335 gss_release_cred(&ms, &(*ctx)->client_creds);
d4ab470d 336#endif
7cac2b65 337
b59afbfe 338 xfree(*ctx);
7cac2b65 339 *ctx = NULL;
5598e598 340}
341
7cac2b65 342/*
343 * Wrapper to init_sec_context
5598e598 344 * Requires that the context contains:
345 * oid
7cac2b65 346 * server name (from ssh_gssapi_import_name)
5598e598 347 */
7cac2b65 348OM_uint32
5598e598 349ssh_gssapi_init_ctx(Gssctxt *ctx, int deleg_creds, gss_buffer_desc *recv_tok,
7cac2b65 350 gss_buffer_desc* send_tok, OM_uint32 *flags)
5598e598 351{
5598e598 352 int deleg_flag = 0;
7cac2b65 353
5598e598 354 if (deleg_creds) {
7cac2b65 355 deleg_flag = GSS_C_DELEG_FLAG;
5598e598 356 debug("Delegating credentials");
357 }
7cac2b65 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);
5598e598 368}
369
5598e598 370/* Create a service name for the given host */
371OM_uint32
7cac2b65 372ssh_gssapi_import_name(Gssctxt *ctx, const char *host)
373{
23987cb8 374 gss_buffer_desc gssbuf;
4dd35ca9 375 char *xhost;
7cac2b65 376
5598e598 377 /* Make a copy of the host name, in case it was returned by a
e23e524c 378 * previous call to gethostbyname(). */
5598e598 379 xhost = xstrdup(host);
380
23987cb8 381 /* Make sure we have the FQDN. Some GSSAPI implementations don't do
e23e524c 382 * this for us themselves */
1df48666 383 resolve_localhost(&xhost);
23987cb8 384
7cac2b65 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)))
23987cb8 391 ssh_gssapi_error(ctx);
7cac2b65 392
1c828c5c 393 xfree(xhost);
5598e598 394 xfree(gssbuf.value);
7cac2b65 395 return (ctx->major);
5598e598 396}
397
398/* Acquire credentials for a server running on the current host.
399 * Requires that the context structure contains a valid OID
b59afbfe 400 */
7cac2b65 401
b59afbfe 402/* Returns a GSSAPI error code */
5598e598 403OM_uint32
7cac2b65 404ssh_gssapi_acquire_cred(Gssctxt *ctx)
405{
23987cb8 406 OM_uint32 status;
5598e598 407 char lname[MAXHOSTNAMELEN];
408 gss_OID_set oidset;
5598e598 409
7cac2b65 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)))
23987cb8 421 ssh_gssapi_error(ctx);
7cac2b65 422
23987cb8 423 gss_release_oid_set(&status, &oidset);
7cac2b65 424 return (ctx->major);
425}
426
427OM_uint32
428ssh_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));
5598e598 434}
b59afbfe 435
436OM_uint32
437ssh_gssapi_sign(Gssctxt *ctx, gss_buffer_desc *buffer, gss_buffer_desc *hash) {
5598e598 438
23987cb8 439 if ((ctx->major=gss_get_mic(&ctx->minor,ctx->context,
b59afbfe 440 GSS_C_QOP_DEFAULT, buffer, hash))) {
23987cb8 441 ssh_gssapi_error(ctx);
b59afbfe 442 }
443
23987cb8 444 return(ctx->major);
b59afbfe 445}
446
23987cb8 447int
448ssh_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);
b59afbfe 457 gss_release_buffer(&minor,&token);
23987cb8 458 ssh_gssapi_delete_ctx(&ctx);
459 return(!GSS_ERROR(major));
b59afbfe 460}
f001f132 461
5598e598 462#endif /* GSSAPI */
This page took 0.132022 seconds and 5 git commands to generate.