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