10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * Copyright (c) 2001-2003 Simon Wilkinson. All rights reserved. *
30Sstevel@tonic-gate  * Redistribution and use in source and binary forms, with or without
40Sstevel@tonic-gate  * modification, are permitted provided that the following conditions
50Sstevel@tonic-gate  * are met:
60Sstevel@tonic-gate  * 1. Redistributions of source code must retain the above copyright
70Sstevel@tonic-gate  *    notice, this list of conditions and the following disclaimer.
80Sstevel@tonic-gate  * 2. Redistributions in binary form must reproduce the above copyright
90Sstevel@tonic-gate  *    notice, this list of conditions and the following disclaimer in the
100Sstevel@tonic-gate  *    documentation and/or other materials provided with the distribution.
110Sstevel@tonic-gate  *
120Sstevel@tonic-gate  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR `AS IS'' AND ANY EXPRESS OR
130Sstevel@tonic-gate  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
140Sstevel@tonic-gate  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
150Sstevel@tonic-gate  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
160Sstevel@tonic-gate  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
170Sstevel@tonic-gate  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
180Sstevel@tonic-gate  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
190Sstevel@tonic-gate  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
200Sstevel@tonic-gate  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
210Sstevel@tonic-gate  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
220Sstevel@tonic-gate  */
230Sstevel@tonic-gate /*
24*1776Snw141292  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
250Sstevel@tonic-gate  * Use is subject to license terms.
260Sstevel@tonic-gate  */
270Sstevel@tonic-gate 
280Sstevel@tonic-gate #include "includes.h"
290Sstevel@tonic-gate 
300Sstevel@tonic-gate #ifdef GSSAPI
310Sstevel@tonic-gate 
320Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
330Sstevel@tonic-gate 
340Sstevel@tonic-gate #include "ssh.h"
350Sstevel@tonic-gate #include "ssh2.h"
360Sstevel@tonic-gate #include "xmalloc.h"
370Sstevel@tonic-gate #include "buffer.h"
380Sstevel@tonic-gate #include "bufaux.h"
390Sstevel@tonic-gate #include "packet.h"
400Sstevel@tonic-gate #include "compat.h"
410Sstevel@tonic-gate #include <openssl/evp.h>
420Sstevel@tonic-gate #include "cipher.h"
430Sstevel@tonic-gate #include "kex.h"
440Sstevel@tonic-gate #include "log.h"
450Sstevel@tonic-gate #include "compat.h"
460Sstevel@tonic-gate #include "xlist.h"
470Sstevel@tonic-gate #include "monitor_wrap.h"
480Sstevel@tonic-gate 
490Sstevel@tonic-gate #include <netdb.h>
500Sstevel@tonic-gate 
510Sstevel@tonic-gate #include "ssh-gss.h"
520Sstevel@tonic-gate 
530Sstevel@tonic-gate #ifdef HAVE_GSS_OID_TO_MECH
540Sstevel@tonic-gate #include <gssapi/gssapi_ext.h>
550Sstevel@tonic-gate #endif /* HAVE_GSS_OID_TO_MECH */
560Sstevel@tonic-gate 
570Sstevel@tonic-gate typedef struct {
580Sstevel@tonic-gate 	char *encoded;
590Sstevel@tonic-gate 	gss_OID oid;
600Sstevel@tonic-gate } ssh_gss_kex_mapping;
610Sstevel@tonic-gate 
620Sstevel@tonic-gate static ssh_gss_kex_mapping **gss_enc2oid = NULL;
630Sstevel@tonic-gate 
640Sstevel@tonic-gate static void ssh_gssapi_encode_oid_for_kex(const gss_OID oid, char **enc_name);
650Sstevel@tonic-gate static char *ssh_gssapi_make_kexalgs_list(gss_OID_set mechs,
660Sstevel@tonic-gate 					  const char *old_kexalgs);
670Sstevel@tonic-gate 
680Sstevel@tonic-gate /*
690Sstevel@tonic-gate  * Populate gss_enc2oid table and return list of kexnames.
700Sstevel@tonic-gate  *
710Sstevel@tonic-gate  * If called with both mechs == GSS_C_NULL_OID_SET and kexname_list == NULL
720Sstevel@tonic-gate  * then cached gss_enc2oid table is cleaned up.
730Sstevel@tonic-gate  */
740Sstevel@tonic-gate void
750Sstevel@tonic-gate ssh_gssapi_mech_oids_to_kexnames(const gss_OID_set mechs, char **kexname_list)
760Sstevel@tonic-gate {
770Sstevel@tonic-gate 	ssh_gss_kex_mapping **new_gss_enc2oid, **p;
780Sstevel@tonic-gate 	Buffer buf;
790Sstevel@tonic-gate 	char *enc_name;
800Sstevel@tonic-gate 	int i;
810Sstevel@tonic-gate 
820Sstevel@tonic-gate 	if (kexname_list != NULL)
830Sstevel@tonic-gate 		*kexname_list = NULL; /* default to failed */
840Sstevel@tonic-gate 
850Sstevel@tonic-gate 	if (mechs != GSS_C_NULL_OID_SET || kexname_list == NULL) {
860Sstevel@tonic-gate 		/* Cleanup gss_enc2oid table */
870Sstevel@tonic-gate 		for (p = gss_enc2oid ; p != NULL && *p != NULL ; p++) {
880Sstevel@tonic-gate 			if ((*p)->encoded)
890Sstevel@tonic-gate 				xfree((*p)->encoded);
900Sstevel@tonic-gate 			ssh_gssapi_release_oid(&(*p)->oid);
910Sstevel@tonic-gate 			xfree(*p);
920Sstevel@tonic-gate 		}
930Sstevel@tonic-gate 		if (gss_enc2oid)
940Sstevel@tonic-gate 			xfree(gss_enc2oid);
950Sstevel@tonic-gate 	}
960Sstevel@tonic-gate 
970Sstevel@tonic-gate 	if (mechs == GSS_C_NULL_OID_SET && kexname_list == NULL)
980Sstevel@tonic-gate 		return; /* nothing left to do */
990Sstevel@tonic-gate 
1000Sstevel@tonic-gate 	if (mechs) {
1010Sstevel@tonic-gate 		gss_OID mech;
1020Sstevel@tonic-gate 		/* Populate gss_enc2oid table */
1030Sstevel@tonic-gate 		new_gss_enc2oid = xmalloc(sizeof(ssh_gss_kex_mapping *) *
1040Sstevel@tonic-gate 				(mechs->count + 1));
1050Sstevel@tonic-gate 		memset(new_gss_enc2oid, 0,
1060Sstevel@tonic-gate 			sizeof(ssh_gss_kex_mapping *) * (mechs->count + 1));
1070Sstevel@tonic-gate 
1080Sstevel@tonic-gate 		for (i = 0 ; i < mechs->count ; i++) {
1090Sstevel@tonic-gate 			mech = &mechs->elements[i];
1100Sstevel@tonic-gate 			ssh_gssapi_encode_oid_for_kex((const gss_OID)mech,
1110Sstevel@tonic-gate 				&enc_name);
1120Sstevel@tonic-gate 
1130Sstevel@tonic-gate 			if (!enc_name)
1140Sstevel@tonic-gate 				continue;
1150Sstevel@tonic-gate 
1160Sstevel@tonic-gate 			new_gss_enc2oid[i] =
1170Sstevel@tonic-gate 				xmalloc(sizeof(ssh_gss_kex_mapping));
1180Sstevel@tonic-gate 			(new_gss_enc2oid[i])->encoded = enc_name;
1190Sstevel@tonic-gate 			(new_gss_enc2oid[i])->oid =
1200Sstevel@tonic-gate 				ssh_gssapi_dup_oid(&mechs->elements[i]);
1210Sstevel@tonic-gate 		}
1220Sstevel@tonic-gate 
1230Sstevel@tonic-gate 		/* Do this last to avoid run-ins with fatal_cleanups */
1240Sstevel@tonic-gate 		gss_enc2oid = new_gss_enc2oid;
1250Sstevel@tonic-gate 	}
1260Sstevel@tonic-gate 
1270Sstevel@tonic-gate 	if (!kexname_list)
1280Sstevel@tonic-gate 		return; /* nothing left to do */
1290Sstevel@tonic-gate 
1300Sstevel@tonic-gate 	/* Make kex name list */
1310Sstevel@tonic-gate 	buffer_init(&buf);
1320Sstevel@tonic-gate 	for (p = gss_enc2oid ; p && *p ; p++) {
1330Sstevel@tonic-gate 		buffer_put_char(&buf,',');
1340Sstevel@tonic-gate 		buffer_append(&buf, (*p)->encoded, strlen((*p)->encoded));
1350Sstevel@tonic-gate 	}
1360Sstevel@tonic-gate 
1370Sstevel@tonic-gate 	if (buffer_len(&buf) == 0) {
1380Sstevel@tonic-gate 		buffer_free(&buf);
1390Sstevel@tonic-gate 		return;
1400Sstevel@tonic-gate 	}
1410Sstevel@tonic-gate 
1420Sstevel@tonic-gate 	buffer_consume(&buf, 1); /* consume leading ',' */
1430Sstevel@tonic-gate 	buffer_put_char(&buf,'\0');
1440Sstevel@tonic-gate 
1450Sstevel@tonic-gate 	*kexname_list = xstrdup(buffer_ptr(&buf));
1460Sstevel@tonic-gate 	buffer_free(&buf);
1470Sstevel@tonic-gate }
1480Sstevel@tonic-gate 
1490Sstevel@tonic-gate void
1500Sstevel@tonic-gate ssh_gssapi_mech_oid_to_kexname(const gss_OID mech, char **kexname)
1510Sstevel@tonic-gate {
1520Sstevel@tonic-gate 	ssh_gss_kex_mapping **p;
1530Sstevel@tonic-gate 
1540Sstevel@tonic-gate 	if (mech == GSS_C_NULL_OID || !kexname)
1550Sstevel@tonic-gate 		return;
1560Sstevel@tonic-gate 
1570Sstevel@tonic-gate 	*kexname = NULL; /* default to not found */
1580Sstevel@tonic-gate 	if (gss_enc2oid) {
1590Sstevel@tonic-gate 		for (p = gss_enc2oid ; p && *p ; p++) {
1600Sstevel@tonic-gate 			if (mech->length == (*p)->oid->length &&
1610Sstevel@tonic-gate 			    memcmp(mech->elements, (*p)->oid->elements,
1620Sstevel@tonic-gate 					mech->length) == 0)
1630Sstevel@tonic-gate 				*kexname = xstrdup((*p)->encoded);
1640Sstevel@tonic-gate 		}
1650Sstevel@tonic-gate 	}
1660Sstevel@tonic-gate 
1670Sstevel@tonic-gate 	if (*kexname)
1680Sstevel@tonic-gate 		return; /* found */
1690Sstevel@tonic-gate 
1700Sstevel@tonic-gate 	ssh_gssapi_encode_oid_for_kex(mech, kexname);
1710Sstevel@tonic-gate }
1720Sstevel@tonic-gate 
1730Sstevel@tonic-gate void
1740Sstevel@tonic-gate ssh_gssapi_oid_of_kexname(const char *kexname, gss_OID *mech)
1750Sstevel@tonic-gate {
1760Sstevel@tonic-gate 	ssh_gss_kex_mapping **p;
1770Sstevel@tonic-gate 
1780Sstevel@tonic-gate 	if (!mech || !kexname || !*kexname)
1790Sstevel@tonic-gate 		return;
1800Sstevel@tonic-gate 
1810Sstevel@tonic-gate 	*mech = GSS_C_NULL_OID; /* default to not found */
1820Sstevel@tonic-gate 
1830Sstevel@tonic-gate 	if (!gss_enc2oid)
1840Sstevel@tonic-gate 		return;
1850Sstevel@tonic-gate 
1860Sstevel@tonic-gate 	for (p = gss_enc2oid ; p && *p ; p++) {
1870Sstevel@tonic-gate 		if (strcmp(kexname, (*p)->encoded) == 0) {
1880Sstevel@tonic-gate 			*mech = (*p)->oid;
1890Sstevel@tonic-gate 			return;
1900Sstevel@tonic-gate 		}
1910Sstevel@tonic-gate 	}
1920Sstevel@tonic-gate }
1930Sstevel@tonic-gate 
1940Sstevel@tonic-gate static
1950Sstevel@tonic-gate void
1960Sstevel@tonic-gate ssh_gssapi_encode_oid_for_kex(const gss_OID oid, char **enc_name)
1970Sstevel@tonic-gate {
1980Sstevel@tonic-gate 	Buffer		buf;
1990Sstevel@tonic-gate 	OM_uint32	oidlen;
2000Sstevel@tonic-gate 	u_int		enclen;
2010Sstevel@tonic-gate 	const EVP_MD    *evp_md = EVP_md5();
2020Sstevel@tonic-gate 	EVP_MD_CTX	md;
2030Sstevel@tonic-gate 	u_char          digest[EVP_MAX_MD_SIZE];
2040Sstevel@tonic-gate 	char		*encoded;
2050Sstevel@tonic-gate 
2060Sstevel@tonic-gate 	if (oid == GSS_C_NULL_OID || !enc_name)
2070Sstevel@tonic-gate 		return;
2080Sstevel@tonic-gate 
2090Sstevel@tonic-gate 	*enc_name = NULL;
2100Sstevel@tonic-gate 
2110Sstevel@tonic-gate 	oidlen = oid->length;
2120Sstevel@tonic-gate 
2130Sstevel@tonic-gate 	/* No GSS mechs have OIDs as long as 128 -- simplify DER encoding */
2140Sstevel@tonic-gate 	if (oidlen > 128)
2150Sstevel@tonic-gate 		return; /* fail gracefully */
2160Sstevel@tonic-gate 
2170Sstevel@tonic-gate 	/*
2180Sstevel@tonic-gate 	 * NOTE:  If we need to support SSH_BUG_GSSAPI_BER this is where
2190Sstevel@tonic-gate 	 * we'd do it.
2200Sstevel@tonic-gate 	 *
2210Sstevel@tonic-gate 	 * That means using "Se3H81ismmOC3OE+FwYCiQ==" for the Kerberos
2220Sstevel@tonic-gate 	 * V mech and "N3+k7/4wGxHyuP8Yxi4RhA==" for the GSI mech.  Ick.
2230Sstevel@tonic-gate 	 */
2240Sstevel@tonic-gate 
2250Sstevel@tonic-gate 	buffer_init(&buf);
2260Sstevel@tonic-gate 
2270Sstevel@tonic-gate 	/* UNIVERSAL class tag for OBJECT IDENTIFIER */
2280Sstevel@tonic-gate 	buffer_put_char(&buf, 0x06);
2290Sstevel@tonic-gate 	buffer_put_char(&buf, oidlen); /* one octet DER length -- see above */
2300Sstevel@tonic-gate 
2310Sstevel@tonic-gate 	/* OID elements */
2320Sstevel@tonic-gate 	buffer_append(&buf, oid->elements, oidlen);
2330Sstevel@tonic-gate 
2340Sstevel@tonic-gate 	/* Make digest */
2350Sstevel@tonic-gate 	EVP_DigestInit(&md, evp_md);
2360Sstevel@tonic-gate 	EVP_DigestUpdate(&md, buffer_ptr(&buf), buffer_len(&buf));
2370Sstevel@tonic-gate 	EVP_DigestFinal(&md, digest, NULL);
2380Sstevel@tonic-gate 	buffer_free(&buf);
2390Sstevel@tonic-gate 
2400Sstevel@tonic-gate 	/* Base 64 encoding */
2410Sstevel@tonic-gate 	encoded=xmalloc(EVP_MD_size(evp_md)*2);
2420Sstevel@tonic-gate 	enclen=__b64_ntop(digest, EVP_MD_size(evp_md),
2430Sstevel@tonic-gate 			    encoded,EVP_MD_size(evp_md)*2);
2440Sstevel@tonic-gate 	buffer_init(&buf);
2450Sstevel@tonic-gate 	buffer_append(&buf, KEX_GSS_SHA1, sizeof(KEX_GSS_SHA1)-1);
2460Sstevel@tonic-gate 	buffer_append(&buf, encoded, enclen);
2470Sstevel@tonic-gate 	buffer_put_char(&buf, '\0');
2480Sstevel@tonic-gate 
2490Sstevel@tonic-gate 	debug2("GSS-API Mechanism encoded as %s",encoded);
2500Sstevel@tonic-gate 
2510Sstevel@tonic-gate 	*enc_name = xstrdup(buffer_ptr(&buf));
2520Sstevel@tonic-gate 	buffer_free(&buf);
2530Sstevel@tonic-gate }
2540Sstevel@tonic-gate 
2550Sstevel@tonic-gate static
2560Sstevel@tonic-gate char *
2570Sstevel@tonic-gate ssh_gssapi_make_kexalgs_list(gss_OID_set mechs, const char *old_kexalgs)
2580Sstevel@tonic-gate {
2590Sstevel@tonic-gate 	char *gss_kexalgs, *new_kexalgs;
2600Sstevel@tonic-gate 	int len;
2610Sstevel@tonic-gate 
2620Sstevel@tonic-gate 	if (mechs == GSS_C_NULL_OID_SET)
2630Sstevel@tonic-gate 		return (xstrdup(old_kexalgs)); /* never null */
2640Sstevel@tonic-gate 
2650Sstevel@tonic-gate 	ssh_gssapi_mech_oids_to_kexnames(mechs, &gss_kexalgs);
2660Sstevel@tonic-gate 
2670Sstevel@tonic-gate 	if (gss_kexalgs == NULL || *gss_kexalgs == '\0')
2680Sstevel@tonic-gate 		return (xstrdup(old_kexalgs)); /* never null */
2690Sstevel@tonic-gate 
2700Sstevel@tonic-gate 	if (old_kexalgs == NULL || *old_kexalgs == '\0')
2710Sstevel@tonic-gate 		return (gss_kexalgs);
2720Sstevel@tonic-gate 
2730Sstevel@tonic-gate 	len = strlen(old_kexalgs) + strlen(gss_kexalgs) + 2;
2740Sstevel@tonic-gate 	new_kexalgs = xmalloc(len);
2750Sstevel@tonic-gate 	(void) snprintf(new_kexalgs, len, "%s,%s", gss_kexalgs, old_kexalgs);
2760Sstevel@tonic-gate 
2770Sstevel@tonic-gate 	return (new_kexalgs);
2780Sstevel@tonic-gate }
2790Sstevel@tonic-gate 
2800Sstevel@tonic-gate void
2810Sstevel@tonic-gate ssh_gssapi_modify_kex(Kex *kex, gss_OID_set mechs, char **proposal)
2820Sstevel@tonic-gate {
2830Sstevel@tonic-gate 	char *kexalgs, *orig_kexalgs, *p;
2840Sstevel@tonic-gate 	char **hostalg, *orig_hostalgs, *new_hostalgs;
2850Sstevel@tonic-gate 	char **hostalgs;
2860Sstevel@tonic-gate 	gss_OID_set dup_mechs;
2870Sstevel@tonic-gate 	OM_uint32 maj, min;
2880Sstevel@tonic-gate 	int i;
2890Sstevel@tonic-gate 
2900Sstevel@tonic-gate 	if (kex == NULL || proposal == NULL ||
2910Sstevel@tonic-gate 	    (orig_kexalgs = proposal[PROPOSAL_KEX_ALGS]) == NULL) {
2920Sstevel@tonic-gate 		fatal("INTERNAL ERROR (%s)", __func__);
2930Sstevel@tonic-gate 	}
2940Sstevel@tonic-gate 
2950Sstevel@tonic-gate 	orig_hostalgs = proposal[PROPOSAL_SERVER_HOST_KEY_ALGS];
2960Sstevel@tonic-gate 
2970Sstevel@tonic-gate 	if (kex->mechs == GSS_C_NULL_OID_SET && mechs == GSS_C_NULL_OID_SET)
2980Sstevel@tonic-gate 		return; /* didn't offer GSS last time, not offering now */
2990Sstevel@tonic-gate 
3000Sstevel@tonic-gate 	if (kex->mechs == GSS_C_NULL_OID_SET || mechs == GSS_C_NULL_OID_SET)
3010Sstevel@tonic-gate 		goto mod_offer; /* didn't offer last time or not offering now */
3020Sstevel@tonic-gate 
3030Sstevel@tonic-gate 	/* Check if mechs is congruent to kex->mechs (last offered) */
3040Sstevel@tonic-gate 	if (kex->mechs->count == mechs->count) {
3050Sstevel@tonic-gate 		int present, matches = 0;
3060Sstevel@tonic-gate 
3070Sstevel@tonic-gate 		for ( i = 0 ; i < mechs->count ; i++ ) {
3080Sstevel@tonic-gate 			maj = gss_test_oid_set_member(&min,
3090Sstevel@tonic-gate 				&kex->mechs->elements[i], mechs,
3100Sstevel@tonic-gate 				&present);
3110Sstevel@tonic-gate 
3120Sstevel@tonic-gate 			if (GSS_ERROR(maj)) {
3130Sstevel@tonic-gate 				mechs = GSS_C_NULL_OID_SET;
3140Sstevel@tonic-gate 				break;
3150Sstevel@tonic-gate 			}
3160Sstevel@tonic-gate 
3170Sstevel@tonic-gate 			matches += (present) ? 1 : 0;
3180Sstevel@tonic-gate 		}
3190Sstevel@tonic-gate 
3200Sstevel@tonic-gate 		if (matches == kex->mechs->count)
3210Sstevel@tonic-gate 			return; /* no change in offer from last time */
3220Sstevel@tonic-gate 	}
3230Sstevel@tonic-gate 
3240Sstevel@tonic-gate mod_offer:
3250Sstevel@tonic-gate 	/*
3260Sstevel@tonic-gate 	 * Remove previously offered mechs from PROPOSAL_KEX_ALGS proposal
3270Sstevel@tonic-gate 	 *
3280Sstevel@tonic-gate 	 * ASSUMPTION: GSS-API kex algs always go in front, so removing
3290Sstevel@tonic-gate 	 * them is a matter of skipping them.
3300Sstevel@tonic-gate 	 */
3310Sstevel@tonic-gate 	p = kexalgs = orig_kexalgs = proposal[PROPOSAL_KEX_ALGS];
3320Sstevel@tonic-gate 	while (p != NULL && *p != '\0' &&
3330Sstevel@tonic-gate 		strncmp(p, KEX_GSS_SHA1, strlen(KEX_GSS_SHA1)) == 0) {
3340Sstevel@tonic-gate 
3350Sstevel@tonic-gate 		if ((p = strchr(p, ',')) == NULL)
3360Sstevel@tonic-gate 			break;
3370Sstevel@tonic-gate 		p++;
3380Sstevel@tonic-gate 		kexalgs = p;
3390Sstevel@tonic-gate 
3400Sstevel@tonic-gate 	}
3410Sstevel@tonic-gate 	kexalgs = proposal[PROPOSAL_KEX_ALGS] = xstrdup(kexalgs);
3420Sstevel@tonic-gate 	xfree(orig_kexalgs);
3430Sstevel@tonic-gate 
3440Sstevel@tonic-gate 	(void) gss_release_oid_set(&min, &kex->mechs); /* ok if !kex->mechs */
3450Sstevel@tonic-gate 
3460Sstevel@tonic-gate 	/* Not offering GSS kexalgs now -> all done */
3470Sstevel@tonic-gate 	if (mechs == GSS_C_NULL_OID_SET)
3480Sstevel@tonic-gate 		return;
3490Sstevel@tonic-gate 
3500Sstevel@tonic-gate 	/* Remember mechs we're offering */
3510Sstevel@tonic-gate 	maj = gss_create_empty_oid_set(&min, &dup_mechs);
3520Sstevel@tonic-gate 	if (GSS_ERROR(maj))
3530Sstevel@tonic-gate 		return;
3540Sstevel@tonic-gate 	for ( i = 0 ; i < mechs->count ; i++ ) {
3550Sstevel@tonic-gate 		maj = gss_add_oid_set_member(&min, &mechs->elements[i],
3560Sstevel@tonic-gate 			&dup_mechs);
3570Sstevel@tonic-gate 
3580Sstevel@tonic-gate 		if (GSS_ERROR(maj)) {
3590Sstevel@tonic-gate 			(void) gss_release_oid_set(&min, &dup_mechs);
3600Sstevel@tonic-gate 			return;
3610Sstevel@tonic-gate 		}
3620Sstevel@tonic-gate 	}
3630Sstevel@tonic-gate 
3640Sstevel@tonic-gate 	/* Add mechs to kexalgs ... */
3650Sstevel@tonic-gate 	proposal[PROPOSAL_KEX_ALGS] = ssh_gssapi_make_kexalgs_list(mechs, kexalgs);
3660Sstevel@tonic-gate 	kex->mechs = dup_mechs; /* remember what we offer now */
3670Sstevel@tonic-gate 
3680Sstevel@tonic-gate 	/*
3690Sstevel@tonic-gate 	 * ... and add null host key alg, if it wasn't there before, but
3700Sstevel@tonic-gate 	 * not if we're the server and we have other host key algs to
3710Sstevel@tonic-gate 	 * offer.
3720Sstevel@tonic-gate 	 *
3730Sstevel@tonic-gate 	 * NOTE: Never remove "null" host key alg once added.
3740Sstevel@tonic-gate 	 */
3750Sstevel@tonic-gate 	if (orig_hostalgs == NULL || *orig_hostalgs == '\0') {
3760Sstevel@tonic-gate 		proposal[PROPOSAL_SERVER_HOST_KEY_ALGS] = xstrdup("null");
3770Sstevel@tonic-gate 	} else if (!kex->server) {
3780Sstevel@tonic-gate 		hostalgs = xsplit(orig_hostalgs, ',');
3790Sstevel@tonic-gate 		for ( hostalg = hostalgs ; *hostalg != NULL ; hostalg++ ) {
3800Sstevel@tonic-gate 			if (strcmp(*hostalg, "null") == 0) {
3810Sstevel@tonic-gate 				xfree_split_list(hostalgs);
3820Sstevel@tonic-gate 				return;
3830Sstevel@tonic-gate 			}
3840Sstevel@tonic-gate 		}
3850Sstevel@tonic-gate 		xfree_split_list(hostalgs);
3860Sstevel@tonic-gate 
3870Sstevel@tonic-gate 		if (kex->mechs != GSS_C_NULL_OID_SET) {
3880Sstevel@tonic-gate 			int len;
3890Sstevel@tonic-gate 
3900Sstevel@tonic-gate 			len = strlen(orig_hostalgs) + sizeof(",null");
3910Sstevel@tonic-gate 			new_hostalgs = xmalloc(len);
3920Sstevel@tonic-gate 			(void) snprintf(new_hostalgs, len, "%s,null",
3930Sstevel@tonic-gate 					orig_hostalgs);
3940Sstevel@tonic-gate 			proposal[PROPOSAL_SERVER_HOST_KEY_ALGS] = new_hostalgs;
3950Sstevel@tonic-gate 		}
3960Sstevel@tonic-gate 
3970Sstevel@tonic-gate 		xfree(orig_hostalgs);
3980Sstevel@tonic-gate 	}
3990Sstevel@tonic-gate }
4000Sstevel@tonic-gate 
4010Sstevel@tonic-gate /*
4020Sstevel@tonic-gate  * Yes, we harcode OIDs for some things, for now it's all we can do.
4030Sstevel@tonic-gate  *
4040Sstevel@tonic-gate  * We have to reference particular mechanisms due to lack of generality
4050Sstevel@tonic-gate  * in the GSS-API in several areas: authorization, mapping principal
4060Sstevel@tonic-gate  * names to usernames, "storing" delegated credentials, and discovering
4070Sstevel@tonic-gate  * whether a mechanism is a pseudo-mechanism that negotiates mechanisms.
4080Sstevel@tonic-gate  *
4090Sstevel@tonic-gate  * Even if they were in some header file or if __gss_mech_to_oid()
4100Sstevel@tonic-gate  * and/or __gss_oid_to_mech() were standard we'd still have to hardcode
4110Sstevel@tonic-gate  * the mechanism names, and since the mechanisms have no standard names
4120Sstevel@tonic-gate  * other than their OIDs it's actually worse [less portable] to hardcode
4130Sstevel@tonic-gate  * names than OIDs, so we hardcode OIDs.
4140Sstevel@tonic-gate  *
4150Sstevel@tonic-gate  * SPNEGO is a difficult problem though -- it MUST NOT be used in SSHv2,
4160Sstevel@tonic-gate  * but that's true of all possible pseudo-mechanisms that can perform
4170Sstevel@tonic-gate  * mechanism negotiation, and SPNEGO could have new OIDs in the future.
4180Sstevel@tonic-gate  * Ideally we could query each mechanism for its feature set and then
4190Sstevel@tonic-gate  * ignore any mechanisms that negotiate mechanisms, but, alas, there's
4200Sstevel@tonic-gate  * no interface to do that.
4210Sstevel@tonic-gate  *
4220Sstevel@tonic-gate  * In the future, if the necessary generic GSS interfaces for the issues
4230Sstevel@tonic-gate  * listed above are made available (even if they differ by platform, as
4240Sstevel@tonic-gate  * we can expect authorization interfaces will), then we can stop
4250Sstevel@tonic-gate  * referencing specific mechanism OIDs here.
4260Sstevel@tonic-gate  */
4270Sstevel@tonic-gate int
4280Sstevel@tonic-gate ssh_gssapi_is_spnego(gss_OID oid)
4290Sstevel@tonic-gate {
4300Sstevel@tonic-gate 	return (oid->length == 6 &&
4310Sstevel@tonic-gate 		memcmp("\053\006\001\005\005\002",
4320Sstevel@tonic-gate 			    oid->elements, 6) == 0);
4330Sstevel@tonic-gate }
4340Sstevel@tonic-gate 
4350Sstevel@tonic-gate int
4360Sstevel@tonic-gate ssh_gssapi_is_krb5(gss_OID oid)
4370Sstevel@tonic-gate {
4380Sstevel@tonic-gate 	return (oid->length == 9 &&
4390Sstevel@tonic-gate 		memcmp("\x2A\x86\x48\x86\xF7\x12\x01\x02\x02",
4400Sstevel@tonic-gate 			    oid->elements, 9) == 0);
4410Sstevel@tonic-gate }
4420Sstevel@tonic-gate 
4430Sstevel@tonic-gate int
4440Sstevel@tonic-gate ssh_gssapi_is_dh(gss_OID oid)
4450Sstevel@tonic-gate {
4460Sstevel@tonic-gate 	return (oid->length == 9 &&
4470Sstevel@tonic-gate 		memcmp("\053\006\004\001\052\002\032\002\005",
4480Sstevel@tonic-gate 			    oid->elements, 9) == 0);
4490Sstevel@tonic-gate }
4500Sstevel@tonic-gate 
4510Sstevel@tonic-gate int
4520Sstevel@tonic-gate ssh_gssapi_is_gsi(gss_OID oid)
4530Sstevel@tonic-gate {
4540Sstevel@tonic-gate 	return (oid->length == 9 &&
4550Sstevel@tonic-gate 		memcmp("\x2B\x06\x01\x04\x01\x9B\x50\x01\x01",
4560Sstevel@tonic-gate 			    oid->elements, 9) == 0);
4570Sstevel@tonic-gate }
4580Sstevel@tonic-gate 
4590Sstevel@tonic-gate const
4600Sstevel@tonic-gate char *
4610Sstevel@tonic-gate ssh_gssapi_oid_to_name(gss_OID oid)
4620Sstevel@tonic-gate {
4630Sstevel@tonic-gate #ifdef HAVE_GSS_OID_TO_MECH
4640Sstevel@tonic-gate 	return __gss_oid_to_mech(oid);
4650Sstevel@tonic-gate #else
4660Sstevel@tonic-gate 	if (ssh_gssapi_is_krb5(oid))
4670Sstevel@tonic-gate 		return "Kerberos";
4680Sstevel@tonic-gate 	if (ssh_gssapi_is_gsi(oid))
4690Sstevel@tonic-gate 		return "GSI";
4700Sstevel@tonic-gate 	return "(unknown)";
4710Sstevel@tonic-gate #endif /* HAVE_GSS_OID_TO_MECH */
4720Sstevel@tonic-gate }
4730Sstevel@tonic-gate 
4740Sstevel@tonic-gate char *
4750Sstevel@tonic-gate ssh_gssapi_oid_to_str(gss_OID oid)
4760Sstevel@tonic-gate {
4770Sstevel@tonic-gate #ifdef HAVE_GSS_OID_TO_STR
4780Sstevel@tonic-gate 	gss_buffer_desc	str_buf;
4790Sstevel@tonic-gate 	char		*str;
4800Sstevel@tonic-gate 	OM_uint32	maj, min;
4810Sstevel@tonic-gate 
4820Sstevel@tonic-gate 	maj = gss_oid_to_str(&min, oid, &str_buf);
4830Sstevel@tonic-gate 
4840Sstevel@tonic-gate 	if (GSS_ERROR(maj))
4850Sstevel@tonic-gate 		return xstrdup("<gss_oid_to_str() failed>");
4860Sstevel@tonic-gate 
4870Sstevel@tonic-gate 	str = xmalloc(str_buf.length + 1);
4880Sstevel@tonic-gate 	memset(str, 0, str_buf.length + 1);
4890Sstevel@tonic-gate 	strlcpy(str, str_buf.value, str_buf.length + 1);
4900Sstevel@tonic-gate 	(void) gss_release_buffer(&min, &str_buf);
4910Sstevel@tonic-gate 
4920Sstevel@tonic-gate 	return str;
4930Sstevel@tonic-gate #else
4940Sstevel@tonic-gate 	return xstrdup("<gss_oid_to_str() unsupported>");
4950Sstevel@tonic-gate #endif /* HAVE_GSS_OID_TO_STR */
4960Sstevel@tonic-gate }
4970Sstevel@tonic-gate 
4980Sstevel@tonic-gate /* Check that the OID in a data stream matches that in the context */
4990Sstevel@tonic-gate int ssh_gssapi_check_mech_oid(Gssctxt *ctx, void *data, size_t len) {
5000Sstevel@tonic-gate 
5010Sstevel@tonic-gate   return (ctx!=NULL && ctx->desired_mech != GSS_C_NULL_OID &&
5020Sstevel@tonic-gate   	  ctx->desired_mech->length == len &&
5030Sstevel@tonic-gate   	  memcmp(ctx->desired_mech->elements,data,len)==0);
5040Sstevel@tonic-gate }
5050Sstevel@tonic-gate 
5060Sstevel@tonic-gate /* Set the contexts OID from a data stream */
5070Sstevel@tonic-gate void ssh_gssapi_set_oid_data(Gssctxt *ctx, void *data, size_t len) {
5080Sstevel@tonic-gate   if (ctx->actual_mech != GSS_C_NULL_OID) {
5090Sstevel@tonic-gate 	xfree(ctx->actual_mech->elements);
5100Sstevel@tonic-gate    	xfree(ctx->actual_mech);
5110Sstevel@tonic-gate   }
5120Sstevel@tonic-gate   ctx->actual_mech=xmalloc(sizeof(gss_OID_desc));
5130Sstevel@tonic-gate   ctx->actual_mech->length=len;
5140Sstevel@tonic-gate   ctx->actual_mech->elements=xmalloc(len);
5150Sstevel@tonic-gate   memcpy(ctx->actual_mech->elements,data,len);
5160Sstevel@tonic-gate }
5170Sstevel@tonic-gate 
5180Sstevel@tonic-gate /* Set the contexts OID */
5190Sstevel@tonic-gate void ssh_gssapi_set_oid(Gssctxt *ctx, gss_OID oid) {
5200Sstevel@tonic-gate   ssh_gssapi_set_oid_data(ctx,oid->elements,oid->length);
5210Sstevel@tonic-gate }
5220Sstevel@tonic-gate 
5230Sstevel@tonic-gate /* All this effort to report an error ... */
5240Sstevel@tonic-gate 
5250Sstevel@tonic-gate void
5260Sstevel@tonic-gate ssh_gssapi_error(Gssctxt *ctxt, const char *where) {
5270Sstevel@tonic-gate 	if (where)
5280Sstevel@tonic-gate 		debug("GSS-API error while %s: %s", where,
5290Sstevel@tonic-gate 			ssh_gssapi_last_error(ctxt,NULL,NULL));
5300Sstevel@tonic-gate 	else
5310Sstevel@tonic-gate 		debug("GSS-API error: %s",
5320Sstevel@tonic-gate 			ssh_gssapi_last_error(ctxt,NULL,NULL));
5330Sstevel@tonic-gate }
5340Sstevel@tonic-gate 
5350Sstevel@tonic-gate char *
5360Sstevel@tonic-gate ssh_gssapi_last_error(Gssctxt *ctxt,
5370Sstevel@tonic-gate 		      OM_uint32 *major_status, OM_uint32 *minor_status) {
5380Sstevel@tonic-gate 	OM_uint32 lmin, more;
5390Sstevel@tonic-gate 	OM_uint32 maj, min;
5400Sstevel@tonic-gate 	gss_OID mech = GSS_C_NULL_OID;
5410Sstevel@tonic-gate         gss_buffer_desc msg;
5420Sstevel@tonic-gate         Buffer b;
5430Sstevel@tonic-gate         char *ret;
5440Sstevel@tonic-gate 
5450Sstevel@tonic-gate         buffer_init(&b);
5460Sstevel@tonic-gate 
5470Sstevel@tonic-gate 	if (ctxt) {
5480Sstevel@tonic-gate 		/* Get status codes from the Gssctxt */
5490Sstevel@tonic-gate 		maj = ctxt->major;
5500Sstevel@tonic-gate 		min = ctxt->minor;
5510Sstevel@tonic-gate 		/* Output them if desired */
5520Sstevel@tonic-gate 		if (major_status)
5530Sstevel@tonic-gate 			*major_status = maj;
5540Sstevel@tonic-gate 		if (minor_status)
5550Sstevel@tonic-gate 			*minor_status = min;
5560Sstevel@tonic-gate 		/* Get mechanism for minor status display */
5570Sstevel@tonic-gate 		mech = (ctxt->actual_mech != GSS_C_NULL_OID) ?
5580Sstevel@tonic-gate 			    ctxt->actual_mech : ctxt->desired_mech;
5590Sstevel@tonic-gate 	} else if (major_status && minor_status) {
5600Sstevel@tonic-gate 		maj = *major_status;
5610Sstevel@tonic-gate 		min = *major_status;
5620Sstevel@tonic-gate 	} else {
5630Sstevel@tonic-gate 		maj = GSS_S_COMPLETE;
5640Sstevel@tonic-gate 		min = 0;
5650Sstevel@tonic-gate 	}
5660Sstevel@tonic-gate 
5670Sstevel@tonic-gate         more = 0;
5680Sstevel@tonic-gate 	/* The GSSAPI error */
5690Sstevel@tonic-gate         do {
5700Sstevel@tonic-gate         	gss_display_status(&lmin, maj,
5710Sstevel@tonic-gate         			   GSS_C_GSS_CODE, GSS_C_NULL_OID,
5720Sstevel@tonic-gate         		           &more, &msg);
5730Sstevel@tonic-gate 
5740Sstevel@tonic-gate         	buffer_append(&b,msg.value,msg.length);
5750Sstevel@tonic-gate         	buffer_put_char(&b,'\n');
5760Sstevel@tonic-gate        	    	gss_release_buffer(&lmin, &msg);
5770Sstevel@tonic-gate         } while (more!=0);
5780Sstevel@tonic-gate 
5790Sstevel@tonic-gate         /* The mechanism specific error */
5800Sstevel@tonic-gate         do {
5810Sstevel@tonic-gate 		/*
5820Sstevel@tonic-gate 		 * If mech == GSS_C_NULL_OID we may get the default
5830Sstevel@tonic-gate 		 * mechanism, whatever that is, and that may not be
5840Sstevel@tonic-gate 		 * useful.
5850Sstevel@tonic-gate 		 */
5860Sstevel@tonic-gate         	gss_display_status(&lmin, min,
5870Sstevel@tonic-gate         			   GSS_C_MECH_CODE, mech,
5880Sstevel@tonic-gate         			   &more, &msg);
5890Sstevel@tonic-gate 
5900Sstevel@tonic-gate         	buffer_append(&b,msg.value,msg.length);
5910Sstevel@tonic-gate         	buffer_put_char(&b,'\n');
5920Sstevel@tonic-gate 
5930Sstevel@tonic-gate         	gss_release_buffer(&lmin, &msg);
5940Sstevel@tonic-gate         } while (more!=0);
5950Sstevel@tonic-gate 
5960Sstevel@tonic-gate         buffer_put_char(&b,'\0');
5970Sstevel@tonic-gate         ret=xstrdup(buffer_ptr(&b));
5980Sstevel@tonic-gate         buffer_free(&b);
5990Sstevel@tonic-gate 
6000Sstevel@tonic-gate         return (ret);
6010Sstevel@tonic-gate }
6020Sstevel@tonic-gate 
6030Sstevel@tonic-gate /* Initialise our GSSAPI context. We use this opaque structure to contain all
6040Sstevel@tonic-gate  * of the data which both the client and server need to persist across
6050Sstevel@tonic-gate  * {accept,init}_sec_context calls, so that when we do it from the userauth
6060Sstevel@tonic-gate  * stuff life is a little easier
6070Sstevel@tonic-gate  */
6080Sstevel@tonic-gate void
6090Sstevel@tonic-gate ssh_gssapi_build_ctx(Gssctxt **ctx, int client, gss_OID mech)
6100Sstevel@tonic-gate {
6110Sstevel@tonic-gate 	Gssctxt *newctx;
6120Sstevel@tonic-gate 
6130Sstevel@tonic-gate 
6140Sstevel@tonic-gate 	newctx = (Gssctxt*)xmalloc(sizeof (Gssctxt));
6150Sstevel@tonic-gate 	memset(newctx, 0, sizeof(Gssctxt));
6160Sstevel@tonic-gate 
617*1776Snw141292 
6180Sstevel@tonic-gate 	newctx->local = client;
6190Sstevel@tonic-gate 	newctx->desired_mech = ssh_gssapi_dup_oid(mech);
6200Sstevel@tonic-gate 
6210Sstevel@tonic-gate 	/* This happens to be redundant given the memset() above */
6220Sstevel@tonic-gate 	newctx->major = GSS_S_COMPLETE;
6230Sstevel@tonic-gate 	newctx->context = GSS_C_NO_CONTEXT;
6240Sstevel@tonic-gate 	newctx->actual_mech =  GSS_C_NULL_OID;
6250Sstevel@tonic-gate 	newctx->desired_name = GSS_C_NO_NAME;
6260Sstevel@tonic-gate 	newctx->src_name = GSS_C_NO_NAME;
6270Sstevel@tonic-gate 	newctx->dst_name = GSS_C_NO_NAME;
6280Sstevel@tonic-gate 	newctx->creds = GSS_C_NO_CREDENTIAL;
6290Sstevel@tonic-gate 	newctx->deleg_creds = GSS_C_NO_CREDENTIAL;
6300Sstevel@tonic-gate 
631*1776Snw141292 	newctx->default_creds = (*ctx != NULL) ? (*ctx)->default_creds : 0;
632*1776Snw141292 
633*1776Snw141292 	ssh_gssapi_delete_ctx(ctx);
634*1776Snw141292 
6350Sstevel@tonic-gate 	*ctx = newctx;
6360Sstevel@tonic-gate }
6370Sstevel@tonic-gate 
6380Sstevel@tonic-gate gss_OID
6390Sstevel@tonic-gate ssh_gssapi_dup_oid(gss_OID oid)
6400Sstevel@tonic-gate {
6410Sstevel@tonic-gate 	gss_OID new_oid;
6420Sstevel@tonic-gate 
6430Sstevel@tonic-gate 	new_oid = xmalloc(sizeof(gss_OID_desc));
6440Sstevel@tonic-gate 
6450Sstevel@tonic-gate 	new_oid->elements = xmalloc(oid->length);
6460Sstevel@tonic-gate 	new_oid->length = oid->length;
6470Sstevel@tonic-gate 	memcpy(new_oid->elements, oid->elements, oid->length);
6480Sstevel@tonic-gate 
6490Sstevel@tonic-gate 	return (new_oid);
6500Sstevel@tonic-gate }
6510Sstevel@tonic-gate 
6520Sstevel@tonic-gate gss_OID
6530Sstevel@tonic-gate ssh_gssapi_make_oid(size_t length, void *elements)
6540Sstevel@tonic-gate {
6550Sstevel@tonic-gate 	gss_OID_desc oid;
6560Sstevel@tonic-gate 
6570Sstevel@tonic-gate 	oid.length = length;
6580Sstevel@tonic-gate 	oid.elements = elements;
6590Sstevel@tonic-gate 
6600Sstevel@tonic-gate 	return (ssh_gssapi_dup_oid(&oid));
6610Sstevel@tonic-gate }
6620Sstevel@tonic-gate 
6630Sstevel@tonic-gate void
6640Sstevel@tonic-gate ssh_gssapi_release_oid(gss_OID *oid)
6650Sstevel@tonic-gate {
6660Sstevel@tonic-gate 	OM_uint32 min;
6670Sstevel@tonic-gate 
6680Sstevel@tonic-gate 	if (oid && *oid == GSS_C_NULL_OID)
6690Sstevel@tonic-gate 		return;
6700Sstevel@tonic-gate 	(void) gss_release_oid(&min, oid);
6710Sstevel@tonic-gate 
6720Sstevel@tonic-gate 	if (*oid == GSS_C_NULL_OID)
6730Sstevel@tonic-gate 		return; /* libgss did own this gss_OID and released it */
6740Sstevel@tonic-gate 
6750Sstevel@tonic-gate 	xfree((*oid)->elements);
6760Sstevel@tonic-gate 	xfree(*oid);
6770Sstevel@tonic-gate 	*oid = GSS_C_NULL_OID;
6780Sstevel@tonic-gate }
6790Sstevel@tonic-gate 
6800Sstevel@tonic-gate struct gss_name {
6810Sstevel@tonic-gate 	gss_OID                 name_type;
6820Sstevel@tonic-gate 	gss_buffer_t            external_name;
6830Sstevel@tonic-gate 	gss_OID                 mech_type;
6840Sstevel@tonic-gate 	void			*mech_name;
6850Sstevel@tonic-gate };
6860Sstevel@tonic-gate 
6870Sstevel@tonic-gate /* Delete our context, providing it has been built correctly */
6880Sstevel@tonic-gate void
6890Sstevel@tonic-gate ssh_gssapi_delete_ctx(Gssctxt **ctx)
6900Sstevel@tonic-gate {
6910Sstevel@tonic-gate 	OM_uint32 ms;
6920Sstevel@tonic-gate 
6930Sstevel@tonic-gate 	if ((*ctx) == NULL)
6940Sstevel@tonic-gate 		return;
6950Sstevel@tonic-gate 
6960Sstevel@tonic-gate 	if ((*ctx)->context != GSS_C_NO_CONTEXT)
6970Sstevel@tonic-gate 		gss_delete_sec_context(&ms,&(*ctx)->context,GSS_C_NO_BUFFER);
6980Sstevel@tonic-gate 	/* XXX if ((*ctx)->desired_mech != GSS_C_NULL_OID)
6990Sstevel@tonic-gate 		ssh_gssapi_release_oid(&(*ctx)->desired_mech);*/
7000Sstevel@tonic-gate 	if ((*ctx)->actual_mech != GSS_C_NULL_OID)
7010Sstevel@tonic-gate 		(void) ssh_gssapi_release_oid(&(*ctx)->actual_mech);
7020Sstevel@tonic-gate 	if ((*ctx)->desired_name != GSS_C_NO_NAME)
7030Sstevel@tonic-gate 		gss_release_name(&ms,&(*ctx)->desired_name);
7040Sstevel@tonic-gate 	/* if ((*ctx)->src_name != GSS_C_NO_NAME)
7050Sstevel@tonic-gate 		gss_release_name(&ms,&(*ctx)->src_name); */
7060Sstevel@tonic-gate 	if ((*ctx)->dst_name != GSS_C_NO_NAME)
7070Sstevel@tonic-gate 		gss_release_name(&ms,&(*ctx)->dst_name);
7080Sstevel@tonic-gate 	if ((*ctx)->creds != GSS_C_NO_CREDENTIAL)
7090Sstevel@tonic-gate 		gss_release_cred(&ms,&(*ctx)->creds);
7100Sstevel@tonic-gate 	if ((*ctx)->deleg_creds != GSS_C_NO_CREDENTIAL)
7110Sstevel@tonic-gate 		gss_release_cred(&ms,&(*ctx)->deleg_creds);
7120Sstevel@tonic-gate 
7130Sstevel@tonic-gate 	xfree(*ctx);
7140Sstevel@tonic-gate 	*ctx=NULL;
7150Sstevel@tonic-gate }
7160Sstevel@tonic-gate 
7170Sstevel@tonic-gate /* Create a GSS hostbased service principal name for a given server hostname */
7180Sstevel@tonic-gate int
7190Sstevel@tonic-gate ssh_gssapi_import_name(Gssctxt *ctx, const char *server_host)
7200Sstevel@tonic-gate {
7210Sstevel@tonic-gate 	gss_buffer_desc name_buf;
7220Sstevel@tonic-gate 	int		ret;
7230Sstevel@tonic-gate 
7240Sstevel@tonic-gate 	/* Build target principal */
7250Sstevel@tonic-gate 
7260Sstevel@tonic-gate 	/* Non-portable but very neat code relying on SUSv3:
7270Sstevel@tonic-gate 	name_buf.length = snprintf(NULL, 0, "%s@%S",
7280Sstevel@tonic-gate 		SSH_GSS_HOSTBASED_SERVICE, server_host);
7290Sstevel@tonic-gate 	 */
7300Sstevel@tonic-gate 
7310Sstevel@tonic-gate 	name_buf.length = strlen(SSH_GSS_HOSTBASED_SERVICE) +
7320Sstevel@tonic-gate 		    strlen(server_host) + 1; /* +1 for '@' */
7330Sstevel@tonic-gate 	name_buf.value = xmalloc(name_buf.length + 1); /* +1 for NUL */
7340Sstevel@tonic-gate 	ret = snprintf(name_buf.value, name_buf.length + 1, "%s@%s",
7350Sstevel@tonic-gate 			SSH_GSS_HOSTBASED_SERVICE, server_host);
7360Sstevel@tonic-gate 
7370Sstevel@tonic-gate 	debug3("%s: snprintf() returned %d, expected %d", __func__, ret, name_buf.length + 1);
7380Sstevel@tonic-gate 
7390Sstevel@tonic-gate 	ctx->major = gss_import_name(&ctx->minor, &name_buf,
7400Sstevel@tonic-gate 		GSS_C_NT_HOSTBASED_SERVICE, &ctx->desired_name);
7410Sstevel@tonic-gate 
7420Sstevel@tonic-gate 	if (GSS_ERROR(ctx->major)) {
7430Sstevel@tonic-gate 		ssh_gssapi_error(ctx, "calling GSS_Import_name()");
7440Sstevel@tonic-gate 		return 0;
7450Sstevel@tonic-gate 	}
7460Sstevel@tonic-gate 
7470Sstevel@tonic-gate 	xfree(name_buf.value);
7480Sstevel@tonic-gate 
7490Sstevel@tonic-gate 	return 1;
7500Sstevel@tonic-gate }
7510Sstevel@tonic-gate 
7520Sstevel@tonic-gate OM_uint32
7530Sstevel@tonic-gate ssh_gssapi_get_mic(Gssctxt *ctx, gss_buffer_desc *buffer, gss_buffer_desc *hash) {
7540Sstevel@tonic-gate 
7550Sstevel@tonic-gate 	ctx->major=gss_get_mic(&ctx->minor,ctx->context,
7560Sstevel@tonic-gate 				GSS_C_QOP_DEFAULT, buffer, hash);
7570Sstevel@tonic-gate 	if (GSS_ERROR(ctx->major))
7580Sstevel@tonic-gate 		ssh_gssapi_error(ctx, "while getting MIC");
7590Sstevel@tonic-gate 	return(ctx->major);
7600Sstevel@tonic-gate }
7610Sstevel@tonic-gate 
7620Sstevel@tonic-gate OM_uint32
7630Sstevel@tonic-gate ssh_gssapi_verify_mic(Gssctxt *ctx, gss_buffer_desc *buffer, gss_buffer_desc *hash) {
7640Sstevel@tonic-gate 	gss_qop_t qop;
7650Sstevel@tonic-gate 
7660Sstevel@tonic-gate 	ctx->major=gss_verify_mic(&ctx->minor,ctx->context, buffer, hash, &qop);
7670Sstevel@tonic-gate 	if (GSS_ERROR(ctx->major))
7680Sstevel@tonic-gate 		ssh_gssapi_error(ctx, "while verifying MIC");
7690Sstevel@tonic-gate 	return(ctx->major);
7700Sstevel@tonic-gate }
7710Sstevel@tonic-gate #endif /* GSSAPI */
772