2 * Copyright (c) 2001,2002 Simon Wilkinson. All rights reserved. *
3 * Redistribution and use in source and binary forms, with or without
4 * modification, are permitted provided that the following conditions
6 * 1. Redistributions of source code must retain the above copyright
7 * notice, this list of conditions and the following disclaimer.
8 * 2. Redistributions in binary form must reproduce the above copyright
9 * notice, this list of conditions and the following disclaimer in the
10 * documentation and/or other materials provided with the distribution.
12 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR `AS IS'' AND ANY EXPRESS OR
13 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
14 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
15 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
16 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
17 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
18 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
19 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
20 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
21 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
35 #include <openssl/evp.h>
40 #include "monitor_wrap.h"
46 /* Assorted globals for tracking the clients identity once they've
49 gss_buffer_desc gssapi_client_name = {0,NULL}; /* Name of our client */
50 gss_cred_id_t gssapi_client_creds = GSS_C_NO_CREDENTIAL; /* Their credentials */
51 enum ssh_gss_id gssapi_client_type = GSS_LAST_ENTRY;
53 unsigned char ssh1_key_digest[16]; /* used for ssh1 gssapi */
55 /* The mechanism name used in the list below is defined in the internet
56 * draft as the Base 64 encoding of the MD5 hash of the ASN.1 DER encoding
57 * of the underlying GSSAPI mechanism's OID.
59 * Also from the draft, before considering adding SPNEGO, bear in mind that
60 * "mechanisms ... MUST NOT use SPNEGO as the underlying GSSAPI mechanism"
63 /* These must be in the same order as ssh_gss_id, in ssh-gss.h */
65 ssh_gssapi_mech supported_mechs[]= {
67 /* Official OID - 1.2.850.113554.1.2.2 */
68 {"Se3H81ismmOC3OE+FwYCiQ==","Kerberos",
69 {9, "\x2A\x86\x48\x86\xF7\x12\x01\x02\x02"}},
72 /* gssapi_ssleay 1.3.6.1.4.1.3536.1.1 */
73 {"N3+k7/4wGxHyuP8Yxi4RhA==",
75 {9, "\x2B\x06\x01\x04\x01\x9B\x50\x01\x01"}
81 char gssprefix[]=KEX_GSS_SHA1;
83 /* Return a list of the gss-group1-sha1-x mechanisms supported by this
86 * We only support the mechanisms that we've indicated in the list above,
87 * but we check that they're supported by the GSSAPI mechanism on the
88 * machine. We also check, before including them in the list, that
89 * we have the necesary information in order to carry out the key exchange
90 * (that is, that the user has credentials, the server's creds are accessible,
93 * The way that this is done is fairly nasty, as we do a lot of work that
94 * is then thrown away. This should possibly be implemented with a cache
95 * that stores the results (in an expanded Gssctxt structure), which are
96 * then used by the first calls if that key exchange mechanism is chosen.
100 ssh_gssapi_mechanisms(int server,char *host) {
101 gss_OID_set supported;
102 OM_uint32 maj_status, min_status;
107 Gssctxt * ctx = NULL;
109 if (datafellows & SSH_OLD_GSSAPI) return NULL;
111 gss_indicate_mechs(&min_status, &supported);
116 if ((maj_status=gss_test_oid_set_member(&min_status,
117 &supported_mechs[i].oid,
124 !GSS_ERROR(PRIVSEP(ssh_gssapi_server_ctx(&ctx,
125 &supported_mechs[i].oid))))
127 !GSS_ERROR(ssh_gssapi_client_ctx(&ctx,
128 &supported_mechs[i].oid,
130 /* Append gss_group1_sha1_x to our list */
131 buffer_append(&buf, gssprefix,
134 supported_mechs[i].enc_name,
135 strlen(supported_mechs[i].enc_name));
138 } while (supported_mechs[++i].name != NULL);
140 buffer_put_char(&buf,'\0');
142 mechs=xmalloc(buffer_len(&buf));
143 buffer_get(&buf,mechs,buffer_len(&buf));
145 if (strlen(mechs)==0)
151 void ssh_gssapi_supported_oids(gss_OID_set *oidset) {
152 enum ssh_gss_id i =0;
153 OM_uint32 maj_status,min_status;
155 gss_OID_set supported;
157 gss_create_empty_oid_set(&min_status,oidset);
158 gss_indicate_mechs(&min_status, &supported);
160 while (supported_mechs[i].name!=NULL) {
161 if ((maj_status=gss_test_oid_set_member(&min_status,
162 &supported_mechs[i].oid,
168 gss_add_oid_set_member(&min_status,
169 &supported_mechs[i].oid,
176 /* Set the contexts OID from a data stream */
177 void ssh_gssapi_set_oid_data(Gssctxt *ctx, void *data, size_t len) {
178 if (ctx->oid != GSS_C_NO_OID) {
179 xfree(ctx->oid->elements);
182 ctx->oid=xmalloc(sizeof(gss_OID_desc));
183 ctx->oid->length=len;
184 ctx->oid->elements=xmalloc(len);
185 memcpy(ctx->oid->elements,data,len);
188 /* Set the contexts OID */
189 void ssh_gssapi_set_oid(Gssctxt *ctx, gss_OID oid) {
190 ssh_gssapi_set_oid_data(ctx,oid->elements,oid->length);
193 /* Find out which GSS type (out of the list we define in ssh-gss.h) a
194 * particular connection is using
196 enum ssh_gss_id ssh_gssapi_get_ctype(Gssctxt *ctxt) {
199 while(supported_mechs[i].name!=NULL) {
200 if (supported_mechs[i].oid.length == ctxt->oid->length &&
201 (memcmp(supported_mechs[i].oid.elements,
202 ctxt->oid->elements,ctxt->oid->length) == 0))
206 return(GSS_LAST_ENTRY);
209 /* Set the GSS context's OID to the oid indicated by the given key exchange
211 gss_OID ssh_gssapi_id_kex(Gssctxt *ctx, char *name) {
214 if (strncmp(name, gssprefix, strlen(gssprefix)-1) !=0) {
218 name+=strlen(gssprefix); /* Move to the start of the MIME string */
220 while (supported_mechs[i].name!=NULL &&
221 strcmp(name,supported_mechs[i].enc_name)!=0) {
225 if (supported_mechs[i].name==NULL)
228 if (ctx) ssh_gssapi_set_oid(ctx,&supported_mechs[i].oid);
230 return &supported_mechs[i].oid;
234 /* All this effort to report an error ... */
236 ssh_gssapi_error_ex(OM_uint32 major_status,OM_uint32 minor_status,
238 OM_uint32 lmaj, lmin;
243 /* The GSSAPI error */
245 lmaj = gss_display_status(&lmin, major_status,
249 if (lmaj == GSS_S_COMPLETE) {
250 debug((char *)msg.value);
251 if (send_packet) packet_send_debug((char *)msg.value);
252 (void) gss_release_buffer(&lmin, &msg);
256 /* The mechanism specific error */
258 lmaj = gss_display_status(&lmin, minor_status,
262 if (lmaj == GSS_S_COMPLETE) {
263 debug((char *)msg.value);
264 if (send_packet) packet_send_debug((char *)msg.value);
265 (void) gss_release_buffer(&lmin, &msg);
271 ssh_gssapi_error(OM_uint32 major_status,OM_uint32 minor_status) {
272 ssh_gssapi_error_ex(major_status, minor_status, 0);
276 ssh_gssapi_send_error(OM_uint32 major_status,OM_uint32 minor_status) {
277 ssh_gssapi_error_ex(major_status, minor_status, 1);
283 /* Initialise our GSSAPI context. We use this opaque structure to contain all
284 * of the data which both the client and server need to persist across
285 * {accept,init}_sec_context calls, so that when we do it from the userauth
286 * stuff life is a little easier
289 ssh_gssapi_build_ctx(Gssctxt **ctx)
291 *ctx=xmalloc(sizeof (Gssctxt));
292 (*ctx)->context=GSS_C_NO_CONTEXT;
293 (*ctx)->name=GSS_C_NO_NAME;
294 (*ctx)->oid=GSS_C_NO_OID;
295 (*ctx)->creds=GSS_C_NO_CREDENTIAL;
296 (*ctx)->client=GSS_C_NO_NAME;
297 (*ctx)->client_creds=GSS_C_NO_CREDENTIAL;
300 /* Delete our context, providing it has been built correctly */
302 ssh_gssapi_delete_ctx(Gssctxt **ctx)
306 /* Return if there's no context */
310 if ((*ctx)->context != GSS_C_NO_CONTEXT)
311 gss_delete_sec_context(&ms,&(*ctx)->context,GSS_C_NO_BUFFER);
312 if ((*ctx)->name != GSS_C_NO_NAME)
313 gss_release_name(&ms,&(*ctx)->name);
314 if ((*ctx)->oid != GSS_C_NO_OID) {
315 xfree((*ctx)->oid->elements);
317 (*ctx)->oid = GSS_C_NO_OID;
319 if ((*ctx)->creds != GSS_C_NO_CREDENTIAL)
320 gss_release_cred(&ms,&(*ctx)->creds);
321 if ((*ctx)->client != GSS_C_NO_NAME)
322 gss_release_name(&ms,&(*ctx)->client);
323 if ((*ctx)->client_creds != GSS_C_NO_CREDENTIAL)
324 gss_release_cred(&ms,&(*ctx)->client_creds);
330 /* Wrapper to init_sec_context
331 * Requires that the context contains:
333 * server name (from ssh_gssapi_import_name)
336 ssh_gssapi_init_ctx(Gssctxt *ctx, int deleg_creds, gss_buffer_desc *recv_tok,
337 gss_buffer_desc* send_tok, OM_uint32 *flags)
339 OM_uint32 maj_status, min_status;
343 deleg_flag=GSS_C_DELEG_FLAG;
344 debug("Delegating credentials");
347 maj_status=gss_init_sec_context(&min_status,
348 GSS_C_NO_CREDENTIAL, /* def. cred */
355 0, /* default lifetime */
356 NULL, /* no channel bindings */
362 ctx->status=maj_status;
363 if (GSS_ERROR(maj_status)) {
364 ssh_gssapi_error(maj_status,min_status);
369 /* Wrapper arround accept_sec_context
370 * Requires that the context contains:
372 * credentials (from ssh_gssapi_acquire_cred)
374 OM_uint32 ssh_gssapi_accept_ctx(Gssctxt *ctx,gss_buffer_desc *recv_tok,
375 gss_buffer_desc *send_tok, OM_uint32 *flags)
377 OM_uint32 maj_status, min_status;
380 maj_status=gss_accept_sec_context(&min_status,
384 GSS_C_NO_CHANNEL_BINDINGS,
391 if (GSS_ERROR(maj_status)) {
392 ssh_gssapi_send_error(maj_status,min_status);
395 if (ctx->client_creds) {
396 debug("Received some client credentials");
398 debug("Got no client credentials");
401 /* FIXME: We should check that the me
402 * the one that we asked for (in ctx->oid) */
404 ctx->status=maj_status;
406 /* Now, if we're complete and we have the right flags, then
407 * we flag the user as also having been authenticated
410 if (((flags==NULL) || ((*flags & GSS_C_MUTUAL_FLAG) &&
411 (*flags & GSS_C_INTEG_FLAG))) &&
412 (maj_status == GSS_S_COMPLETE)) {
413 if (ssh_gssapi_getclient(ctx,&gssapi_client_type,
415 &gssapi_client_creds))
416 fatal("Couldn't convert client name");
422 /* Create a service name for the given host */
424 ssh_gssapi_import_name(Gssctxt *ctx, const char *host) {
425 gss_buffer_desc gssbuf;
426 OM_uint32 maj_status, min_status;
427 struct hostent *hostinfo = NULL;
430 /* Make a copy of the host name, in case it was returned by a
431 * previous call to gethostbyname(). */
432 xhost = xstrdup(host);
434 /* Make sure we have the FQDN. Some GSSAPI implementations don't do
435 * this for us themselves */
436 hostinfo = gethostbyname(xhost);
438 /* Use local hostname when coming in on loopback interface because
439 we won't have 'localhost' credentials. */
441 hostinfo->h_addrtype == AF_INET) {
443 addr = *(struct in_addr *)(hostinfo->h_addr);
444 if (ntohl(addr.s_addr) == INADDR_LOOPBACK) {
446 if (gethostname(buf, 4096) == 0) {
447 hostinfo = gethostbyname(buf);
452 /* Go to the resolver to get the official hostname for our target.
453 WARNING: This makes us vulnerable to DNS spoofing. */
454 if ((hostinfo == NULL) || (hostinfo->h_name == NULL)) {
455 debug("Unable to get FQDN for \"%s\"", xhost);
457 addr = xmalloc(hostinfo->h_length);
458 memcpy(addr, hostinfo->h_addr, hostinfo->h_length);
459 hostinfo = gethostbyaddr(addr, hostinfo->h_length,
460 hostinfo->h_addrtype);
462 if ((hostinfo == NULL) || (hostinfo->h_name == NULL)) {
463 debug("Unable to get FQDN for \"%s\"", xhost);
466 xhost = xstrdup(hostinfo->h_name);
470 gssbuf.length = sizeof("host@")+strlen(xhost);
472 gssbuf.value = xmalloc(gssbuf.length);
473 if (gssbuf.value == NULL) {
477 snprintf(gssbuf.value,gssbuf.length,"host@%s",xhost);
478 if ((maj_status=gss_import_name(&min_status,
480 GSS_C_NT_HOSTBASED_SERVICE,
482 ssh_gssapi_error(maj_status,min_status);
490 /* Acquire credentials for a server running on the current host.
491 * Requires that the context structure contains a valid OID
494 /* Returns a GSSAPI error code */
496 ssh_gssapi_acquire_cred(Gssctxt *ctx) {
497 OM_uint32 maj_status, min_status;
498 char lname[MAXHOSTNAMELEN];
501 gss_create_empty_oid_set(&min_status,&oidset);
502 gss_add_oid_set_member(&min_status,ctx->oid,&oidset);
504 if (gethostname(lname, MAXHOSTNAMELEN)) {
508 if ((maj_status=ssh_gssapi_import_name(ctx,lname))) {
511 if ((maj_status=gss_acquire_cred(&min_status,
519 ssh_gssapi_error(maj_status,min_status);
522 gss_release_oid_set(&min_status, &oidset);
526 /* Extract the client details from a given context. This can only reliably
527 * be called once for a context */
530 ssh_gssapi_getclient(Gssctxt *ctx, enum ssh_gss_id *type,
531 gss_buffer_desc *name, gss_cred_id_t *creds) {
533 OM_uint32 maj_status,min_status;
535 *type=ssh_gssapi_get_ctype(ctx);
536 if ((maj_status=gss_display_name(&min_status,ctx->client,name,NULL))) {
537 ssh_gssapi_error(maj_status,min_status);
540 /* This is icky. There appears to be no way to copy this structure,
541 * rather than the pointer to it, so we simply copy the pointer and
542 * mark the originator as empty so we don't destroy it.
544 *creds=ctx->client_creds;
545 ctx->client_creds=GSS_C_NO_CREDENTIAL;
550 ssh_gssapi_sign(Gssctxt *ctx, gss_buffer_desc *buffer, gss_buffer_desc *hash) {
551 OM_uint32 maj_status,min_status;
553 /* ssh1 needs to exchange the hash of the keys */
554 /* will us this hash to return it */
556 if ((maj_status=gss_wrap(&min_status,ctx->context,
562 ssh_gssapi_error(maj_status,min_status);
566 if ((maj_status=gss_get_mic(&min_status,ctx->context,
567 GSS_C_QOP_DEFAULT, buffer, hash))) {
568 ssh_gssapi_error(maj_status,min_status);
575 ssh_gssapi_server_ctx(Gssctxt **ctx,gss_OID oid) {
576 if (*ctx) ssh_gssapi_delete_ctx(ctx);
577 ssh_gssapi_build_ctx(ctx);
578 ssh_gssapi_set_oid(*ctx,oid);
579 return(ssh_gssapi_acquire_cred(*ctx));
583 ssh_gssapi_client_ctx(Gssctxt **ctx,gss_OID oid, char *host) {
584 gss_buffer_desc token;
585 OM_uint32 major,minor;
587 if (*ctx) ssh_gssapi_delete_ctx(ctx);
588 ssh_gssapi_build_ctx(ctx);
589 ssh_gssapi_set_oid(*ctx,oid);
590 ssh_gssapi_import_name(*ctx,host);
591 major=ssh_gssapi_init_ctx(*ctx, 0, GSS_C_NO_BUFFER, &token, NULL);
592 gss_release_buffer(&minor,&token);