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"
47 /* Assorted globals for tracking the clients identity once they've
50 gss_buffer_desc gssapi_client_name = {0,NULL}; /* Name of our client */
51 gss_cred_id_t gssapi_client_creds = GSS_C_NO_CREDENTIAL; /* Their credentials */
52 enum ssh_gss_id gssapi_client_type = GSS_LAST_ENTRY;
54 unsigned char ssh1_key_digest[16]; /* used for ssh1 gssapi */
56 /* The mechanism name used in the list below is defined in the internet
57 * draft as the Base 64 encoding of the MD5 hash of the ASN.1 DER encoding
58 * of the underlying GSSAPI mechanism's OID.
60 * Also from the draft, before considering adding SPNEGO, bear in mind that
61 * "mechanisms ... MUST NOT use SPNEGO as the underlying GSSAPI mechanism"
64 /* These must be in the same order as ssh_gss_id, in ssh-gss.h */
66 ssh_gssapi_mech supported_mechs[]= {
68 /* Official OID - 1.2.850.113554.1.2.2 */
69 {"Se3H81ismmOC3OE+FwYCiQ==","Kerberos",
70 {9, "\x2A\x86\x48\x86\xF7\x12\x01\x02\x02"}},
73 /* gssapi_ssleay 1.3.6.1.4.1.3536.1.1 */
74 {"N3+k7/4wGxHyuP8Yxi4RhA==",
76 {9, "\x2B\x06\x01\x04\x01\x9B\x50\x01\x01"}
82 char gssprefix[]=KEX_GSS_SHA1;
84 /* Return a list of the gss-group1-sha1-x mechanisms supported by this
87 * We only support the mechanisms that we've indicated in the list above,
88 * but we check that they're supported by the GSSAPI mechanism on the
89 * machine. We also check, before including them in the list, that
90 * we have the necesary information in order to carry out the key exchange
91 * (that is, that the user has credentials, the server's creds are accessible,
94 * The way that this is done is fairly nasty, as we do a lot of work that
95 * is then thrown away. This should possibly be implemented with a cache
96 * that stores the results (in an expanded Gssctxt structure), which are
97 * then used by the first calls if that key exchange mechanism is chosen.
101 ssh_gssapi_mechanisms(int server,char *host) {
102 gss_OID_set supported;
103 OM_uint32 maj_status, min_status;
109 Gssctxt * ctx = NULL;
111 if (datafellows & SSH_OLD_GSSAPI) return NULL;
113 gss_indicate_mechs(&min_status, &supported);
118 if ((maj_status=gss_test_oid_set_member(&min_status,
119 &supported_mechs[i].oid,
126 !GSS_ERROR(PRIVSEP(ssh_gssapi_server_ctx(&ctx,
127 &supported_mechs[i].oid))))
129 !GSS_ERROR(ssh_gssapi_client_ctx(&ctx,
130 &supported_mechs[i].oid,
132 /* Append gss_group1_sha1_x to our list */
133 if (++mech_count > 1) {
134 buffer_append(&buf, ",", 1);
136 buffer_append(&buf, gssprefix,
139 supported_mechs[i].enc_name,
140 strlen(supported_mechs[i].enc_name));
141 debug("GSSAPI mechanism %s (%s%s) supported",
142 supported_mechs[i].name, gssprefix,
143 supported_mechs[i].enc_name);
145 debug("no credentials for GSSAPI mechanism %s",
146 supported_mechs[i].name);
149 debug("GSSAPI mechanism %s not supported",
150 supported_mechs[i].name);
152 } while (supported_mechs[++i].name != NULL);
154 buffer_put_char(&buf,'\0');
156 mechs=xmalloc(buffer_len(&buf));
157 buffer_get(&buf,mechs,buffer_len(&buf));
159 if (strlen(mechs)==0)
165 void ssh_gssapi_supported_oids(gss_OID_set *oidset) {
166 enum ssh_gss_id i =0;
167 OM_uint32 maj_status,min_status;
169 gss_OID_set supported;
171 gss_create_empty_oid_set(&min_status,oidset);
172 gss_indicate_mechs(&min_status, &supported);
174 while (supported_mechs[i].name!=NULL) {
175 if ((maj_status=gss_test_oid_set_member(&min_status,
176 &supported_mechs[i].oid,
182 gss_add_oid_set_member(&min_status,
183 &supported_mechs[i].oid,
190 /* Set the contexts OID from a data stream */
191 void ssh_gssapi_set_oid_data(Gssctxt *ctx, void *data, size_t len) {
192 if (ctx->oid != GSS_C_NO_OID) {
193 xfree(ctx->oid->elements);
196 ctx->oid=xmalloc(sizeof(gss_OID_desc));
197 ctx->oid->length=len;
198 ctx->oid->elements=xmalloc(len);
199 memcpy(ctx->oid->elements,data,len);
202 /* Set the contexts OID */
203 void ssh_gssapi_set_oid(Gssctxt *ctx, gss_OID oid) {
204 ssh_gssapi_set_oid_data(ctx,oid->elements,oid->length);
207 /* Find out which GSS type (out of the list we define in ssh-gss.h) a
208 * particular connection is using
210 enum ssh_gss_id ssh_gssapi_get_ctype(Gssctxt *ctxt) {
213 while(supported_mechs[i].name!=NULL) {
214 if (supported_mechs[i].oid.length == ctxt->oid->length &&
215 (memcmp(supported_mechs[i].oid.elements,
216 ctxt->oid->elements,ctxt->oid->length) == 0))
220 return(GSS_LAST_ENTRY);
223 /* Set the GSS context's OID to the oid indicated by the given key exchange
225 gss_OID ssh_gssapi_id_kex(Gssctxt *ctx, char *name) {
228 if (strncmp(name, gssprefix, strlen(gssprefix)-1) !=0) {
232 name+=strlen(gssprefix); /* Move to the start of the MIME string */
234 while (supported_mechs[i].name!=NULL &&
235 strcmp(name,supported_mechs[i].enc_name)!=0) {
239 if (supported_mechs[i].name==NULL)
242 if (ctx) ssh_gssapi_set_oid(ctx,&supported_mechs[i].oid);
244 debug("using GSSAPI mechanism %s (%s%s)", supported_mechs[i].name,
245 gssprefix, supported_mechs[i].enc_name);
247 return &supported_mechs[i].oid;
251 /* All this effort to report an error ... */
253 ssh_gssapi_error_ex(OM_uint32 major_status,OM_uint32 minor_status,
255 OM_uint32 lmaj, lmin;
256 gss_buffer_desc msg = {0,NULL};
260 /* The GSSAPI error */
262 lmaj = gss_display_status(&lmin, major_status,
266 if (lmaj == GSS_S_COMPLETE) {
267 debug((char *)msg.value);
268 if (send_packet) packet_send_debug((char *)msg.value);
269 (void) gss_release_buffer(&lmin, &msg);
273 /* The mechanism specific error */
275 lmaj = gss_display_status(&lmin, minor_status,
279 if (lmaj == GSS_S_COMPLETE) {
280 debug((char *)msg.value);
281 if (send_packet) packet_send_debug((char *)msg.value);
282 (void) gss_release_buffer(&lmin, &msg);
288 ssh_gssapi_error(OM_uint32 major_status,OM_uint32 minor_status) {
289 ssh_gssapi_error_ex(major_status, minor_status, 0);
293 ssh_gssapi_send_error(OM_uint32 major_status,OM_uint32 minor_status) {
294 ssh_gssapi_error_ex(major_status, minor_status, 1);
300 /* Initialise our GSSAPI context. We use this opaque structure to contain all
301 * of the data which both the client and server need to persist across
302 * {accept,init}_sec_context calls, so that when we do it from the userauth
303 * stuff life is a little easier
306 ssh_gssapi_build_ctx(Gssctxt **ctx)
308 *ctx=xmalloc(sizeof (Gssctxt));
309 (*ctx)->context=GSS_C_NO_CONTEXT;
310 (*ctx)->name=GSS_C_NO_NAME;
311 (*ctx)->oid=GSS_C_NO_OID;
312 (*ctx)->creds=GSS_C_NO_CREDENTIAL;
313 (*ctx)->client=GSS_C_NO_NAME;
314 (*ctx)->client_creds=GSS_C_NO_CREDENTIAL;
317 /* Delete our context, providing it has been built correctly */
319 ssh_gssapi_delete_ctx(Gssctxt **ctx)
323 /* Return if there's no context */
327 #if !defined(MECHGLUE) /* mechglue has some memory management issues */
328 if ((*ctx)->context != GSS_C_NO_CONTEXT)
329 gss_delete_sec_context(&ms,&(*ctx)->context,GSS_C_NO_BUFFER);
330 if ((*ctx)->name != GSS_C_NO_NAME)
331 gss_release_name(&ms,&(*ctx)->name);
332 if ((*ctx)->oid != GSS_C_NO_OID) {
333 xfree((*ctx)->oid->elements);
335 (*ctx)->oid = GSS_C_NO_OID;
337 if ((*ctx)->creds != GSS_C_NO_CREDENTIAL)
338 gss_release_cred(&ms,&(*ctx)->creds);
339 if ((*ctx)->client != GSS_C_NO_NAME)
340 gss_release_name(&ms,&(*ctx)->client);
341 if ((*ctx)->client_creds != GSS_C_NO_CREDENTIAL)
342 gss_release_cred(&ms,&(*ctx)->client_creds);
349 /* Wrapper to init_sec_context
350 * Requires that the context contains:
352 * server name (from ssh_gssapi_import_name)
355 ssh_gssapi_init_ctx(Gssctxt *ctx, int deleg_creds, gss_buffer_desc *recv_tok,
356 gss_buffer_desc* send_tok, OM_uint32 *flags)
358 OM_uint32 maj_status, min_status;
362 deleg_flag=GSS_C_DELEG_FLAG;
363 debug("Delegating credentials");
366 maj_status=gss_init_sec_context(&min_status,
367 GSS_C_NO_CREDENTIAL, /* def. cred */
374 0, /* default lifetime */
375 NULL, /* no channel bindings */
381 ctx->status=maj_status;
382 if (GSS_ERROR(maj_status)) {
383 ssh_gssapi_error(maj_status,min_status);
388 /* Wrapper arround accept_sec_context
389 * Requires that the context contains:
391 * credentials (from ssh_gssapi_acquire_cred)
393 OM_uint32 ssh_gssapi_accept_ctx(Gssctxt *ctx,gss_buffer_desc *recv_tok,
394 gss_buffer_desc *send_tok, OM_uint32 *flags)
396 OM_uint32 maj_status, min_status;
399 maj_status=gss_accept_sec_context(&min_status,
403 GSS_C_NO_CHANNEL_BINDINGS,
410 if (GSS_ERROR(maj_status)) {
411 ssh_gssapi_send_error(maj_status,min_status);
414 if (ctx->client_creds) {
415 debug("Received some client credentials");
417 debug("Got no client credentials");
420 /* FIXME: We should check that the me
421 * the one that we asked for (in ctx->oid) */
423 ctx->status=maj_status;
425 /* Now, if we're complete and we have the right flags, then
426 * we flag the user as also having been authenticated
429 if (((flags==NULL) || ((*flags & GSS_C_MUTUAL_FLAG) &&
430 (*flags & GSS_C_INTEG_FLAG))) &&
431 (maj_status == GSS_S_COMPLETE)) {
432 if (ssh_gssapi_getclient(ctx,&gssapi_client_type,
434 &gssapi_client_creds))
435 fatal("Couldn't convert client name");
441 /* Create a service name for the given host */
443 ssh_gssapi_import_name(Gssctxt *ctx, const char *host) {
444 gss_buffer_desc gssbuf = {0,NULL};
445 OM_uint32 maj_status, min_status;
448 /* Make a copy of the host name, in case it was returned by a
449 * previous call to gethostbyname(). */
450 xhost = xstrdup(host);
452 /* If xhost is the loopback interface, switch it to our
453 true local hostname. */
454 resolve_localhost(&xhost);
456 /* Make sure we have the FQHN. Some GSSAPI implementations don't do
457 * this for us themselves */
460 gssbuf.length = sizeof("host@")+strlen(xhost);
462 gssbuf.value = xmalloc(gssbuf.length);
463 if (gssbuf.value == NULL) {
467 snprintf(gssbuf.value,gssbuf.length,"host@%s",xhost);
468 if ((maj_status=gss_import_name(&min_status,
470 GSS_C_NT_HOSTBASED_SERVICE,
472 ssh_gssapi_error(maj_status,min_status);
480 /* Acquire credentials for a server running on the current host.
481 * Requires that the context structure contains a valid OID
484 /* Returns a GSSAPI error code */
486 ssh_gssapi_acquire_cred(Gssctxt *ctx) {
487 OM_uint32 maj_status, min_status;
488 char lname[MAXHOSTNAMELEN];
491 gss_create_empty_oid_set(&min_status,&oidset);
492 gss_add_oid_set_member(&min_status,ctx->oid,&oidset);
494 if (gethostname(lname, MAXHOSTNAMELEN)) {
498 if ((maj_status=ssh_gssapi_import_name(ctx,lname))) {
501 if ((maj_status=gss_acquire_cred(&min_status,
509 ssh_gssapi_error(maj_status,min_status);
512 gss_release_oid_set(&min_status, &oidset);
516 /* Extract the client details from a given context. This can only reliably
517 * be called once for a context */
520 ssh_gssapi_getclient(Gssctxt *ctx, enum ssh_gss_id *type,
521 gss_buffer_desc *name, gss_cred_id_t *creds) {
523 OM_uint32 maj_status,min_status;
525 *type=ssh_gssapi_get_ctype(ctx);
526 if ((maj_status=gss_display_name(&min_status,ctx->client,name,NULL))) {
527 ssh_gssapi_error(maj_status,min_status);
530 /* This is icky. There appears to be no way to copy this structure,
531 * rather than the pointer to it, so we simply copy the pointer and
532 * mark the originator as empty so we don't destroy it.
534 *creds=ctx->client_creds;
535 ctx->client_creds=GSS_C_NO_CREDENTIAL;
540 ssh_gssapi_sign(Gssctxt *ctx, gss_buffer_desc *buffer, gss_buffer_desc *hash) {
541 OM_uint32 maj_status,min_status;
543 /* ssh1 needs to exchange the hash of the keys */
544 /* will us this hash to return it */
546 if ((maj_status=gss_wrap(&min_status,ctx->context,
552 ssh_gssapi_error(maj_status,min_status);
556 if ((maj_status=gss_get_mic(&min_status,ctx->context,
557 GSS_C_QOP_DEFAULT, buffer, hash))) {
558 ssh_gssapi_error(maj_status,min_status);
565 ssh_gssapi_server_ctx(Gssctxt **ctx,gss_OID oid) {
566 if (*ctx) ssh_gssapi_delete_ctx(ctx);
567 ssh_gssapi_build_ctx(ctx);
568 ssh_gssapi_set_oid(*ctx,oid);
569 return(ssh_gssapi_acquire_cred(*ctx));
573 ssh_gssapi_client_ctx(Gssctxt **ctx,gss_OID oid, char *host) {
574 gss_buffer_desc token = {0,NULL};
575 OM_uint32 major,minor;
577 if (*ctx) ssh_gssapi_delete_ctx(ctx);
578 ssh_gssapi_build_ctx(ctx);
579 ssh_gssapi_set_oid(*ctx,oid);
580 ssh_gssapi_import_name(*ctx,host);
581 major=ssh_gssapi_init_ctx(*ctx, 0, GSS_C_NO_BUFFER, &token, NULL);
582 gss_release_buffer(&minor,&token);