xref: /onnv-gate/usr/src/cmd/ssh/libssh/common/ssh-gss.c (revision 9845:0d705da26956)
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*9845SJan.Pechanec@Sun.COM  * Copyright 2009 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 #include "ssh.h"
330Sstevel@tonic-gate #include "ssh2.h"
340Sstevel@tonic-gate #include "xmalloc.h"
350Sstevel@tonic-gate #include "buffer.h"
360Sstevel@tonic-gate #include "bufaux.h"
370Sstevel@tonic-gate #include "packet.h"
380Sstevel@tonic-gate #include "compat.h"
390Sstevel@tonic-gate #include <openssl/evp.h>
400Sstevel@tonic-gate #include "cipher.h"
410Sstevel@tonic-gate #include "kex.h"
420Sstevel@tonic-gate #include "log.h"
430Sstevel@tonic-gate #include "compat.h"
440Sstevel@tonic-gate #include "xlist.h"
450Sstevel@tonic-gate 
460Sstevel@tonic-gate #include <netdb.h>
470Sstevel@tonic-gate 
480Sstevel@tonic-gate #include "ssh-gss.h"
490Sstevel@tonic-gate 
500Sstevel@tonic-gate #ifdef HAVE_GSS_OID_TO_MECH
510Sstevel@tonic-gate #include <gssapi/gssapi_ext.h>
520Sstevel@tonic-gate #endif /* HAVE_GSS_OID_TO_MECH */
530Sstevel@tonic-gate 
540Sstevel@tonic-gate typedef struct {
550Sstevel@tonic-gate 	char *encoded;
560Sstevel@tonic-gate 	gss_OID oid;
570Sstevel@tonic-gate } ssh_gss_kex_mapping;
580Sstevel@tonic-gate 
590Sstevel@tonic-gate static ssh_gss_kex_mapping **gss_enc2oid = NULL;
600Sstevel@tonic-gate 
610Sstevel@tonic-gate static void ssh_gssapi_encode_oid_for_kex(const gss_OID oid, char **enc_name);
620Sstevel@tonic-gate static char *ssh_gssapi_make_kexalgs_list(gss_OID_set mechs,
636080Sjp161948     const char *old_kexalgs);
640Sstevel@tonic-gate 
650Sstevel@tonic-gate /*
660Sstevel@tonic-gate  * Populate gss_enc2oid table and return list of kexnames.
670Sstevel@tonic-gate  *
680Sstevel@tonic-gate  * If called with both mechs == GSS_C_NULL_OID_SET and kexname_list == NULL
690Sstevel@tonic-gate  * then cached gss_enc2oid table is cleaned up.
700Sstevel@tonic-gate  */
710Sstevel@tonic-gate void
ssh_gssapi_mech_oids_to_kexnames(const gss_OID_set mechs,char ** kexname_list)720Sstevel@tonic-gate ssh_gssapi_mech_oids_to_kexnames(const gss_OID_set mechs, char **kexname_list)
730Sstevel@tonic-gate {
740Sstevel@tonic-gate 	ssh_gss_kex_mapping **new_gss_enc2oid, **p;
750Sstevel@tonic-gate 	Buffer buf;
760Sstevel@tonic-gate 	char *enc_name;
770Sstevel@tonic-gate 	int i;
780Sstevel@tonic-gate 
790Sstevel@tonic-gate 	if (kexname_list != NULL)
800Sstevel@tonic-gate 		*kexname_list = NULL; /* default to failed */
810Sstevel@tonic-gate 
820Sstevel@tonic-gate 	if (mechs != GSS_C_NULL_OID_SET || kexname_list == NULL) {
830Sstevel@tonic-gate 		/* Cleanup gss_enc2oid table */
846080Sjp161948 		for (p = gss_enc2oid; p != NULL && *p != NULL; p++) {
850Sstevel@tonic-gate 			if ((*p)->encoded)
860Sstevel@tonic-gate 				xfree((*p)->encoded);
870Sstevel@tonic-gate 			ssh_gssapi_release_oid(&(*p)->oid);
880Sstevel@tonic-gate 			xfree(*p);
890Sstevel@tonic-gate 		}
900Sstevel@tonic-gate 		if (gss_enc2oid)
910Sstevel@tonic-gate 			xfree(gss_enc2oid);
920Sstevel@tonic-gate 	}
930Sstevel@tonic-gate 
940Sstevel@tonic-gate 	if (mechs == GSS_C_NULL_OID_SET && kexname_list == NULL)
950Sstevel@tonic-gate 		return; /* nothing left to do */
960Sstevel@tonic-gate 
970Sstevel@tonic-gate 	if (mechs) {
980Sstevel@tonic-gate 		gss_OID mech;
990Sstevel@tonic-gate 		/* Populate gss_enc2oid table */
1006080Sjp161948 		new_gss_enc2oid = xmalloc(sizeof (ssh_gss_kex_mapping *) *
1016080Sjp161948 		    (mechs->count + 1));
1020Sstevel@tonic-gate 		memset(new_gss_enc2oid, 0,
1036080Sjp161948 		    sizeof (ssh_gss_kex_mapping *) * (mechs->count + 1));
1040Sstevel@tonic-gate 
1056080Sjp161948 		for (i = 0; i < mechs->count; i++) {
1060Sstevel@tonic-gate 			mech = &mechs->elements[i];
1070Sstevel@tonic-gate 			ssh_gssapi_encode_oid_for_kex((const gss_OID)mech,
1086080Sjp161948 			    &enc_name);
1090Sstevel@tonic-gate 
1100Sstevel@tonic-gate 			if (!enc_name)
1110Sstevel@tonic-gate 				continue;
1120Sstevel@tonic-gate 
1130Sstevel@tonic-gate 			new_gss_enc2oid[i] =
1146080Sjp161948 			    xmalloc(sizeof (ssh_gss_kex_mapping));
1150Sstevel@tonic-gate 			(new_gss_enc2oid[i])->encoded = enc_name;
1160Sstevel@tonic-gate 			(new_gss_enc2oid[i])->oid =
1176080Sjp161948 			    ssh_gssapi_dup_oid(&mechs->elements[i]);
1180Sstevel@tonic-gate 		}
1190Sstevel@tonic-gate 
1200Sstevel@tonic-gate 		/* Do this last to avoid run-ins with fatal_cleanups */
1210Sstevel@tonic-gate 		gss_enc2oid = new_gss_enc2oid;
1220Sstevel@tonic-gate 	}
1230Sstevel@tonic-gate 
1240Sstevel@tonic-gate 	if (!kexname_list)
1250Sstevel@tonic-gate 		return; /* nothing left to do */
1260Sstevel@tonic-gate 
1270Sstevel@tonic-gate 	/* Make kex name list */
1280Sstevel@tonic-gate 	buffer_init(&buf);
1296080Sjp161948 	for (p = gss_enc2oid; p && *p; p++) {
1306080Sjp161948 		buffer_put_char(&buf, ',');
1310Sstevel@tonic-gate 		buffer_append(&buf, (*p)->encoded, strlen((*p)->encoded));
1320Sstevel@tonic-gate 	}
1330Sstevel@tonic-gate 
1340Sstevel@tonic-gate 	if (buffer_len(&buf) == 0) {
1350Sstevel@tonic-gate 		buffer_free(&buf);
1360Sstevel@tonic-gate 		return;
1370Sstevel@tonic-gate 	}
1380Sstevel@tonic-gate 
1390Sstevel@tonic-gate 	buffer_consume(&buf, 1); /* consume leading ',' */
1406080Sjp161948 	buffer_put_char(&buf, '\0');
1410Sstevel@tonic-gate 
1420Sstevel@tonic-gate 	*kexname_list = xstrdup(buffer_ptr(&buf));
1430Sstevel@tonic-gate 	buffer_free(&buf);
1440Sstevel@tonic-gate }
1450Sstevel@tonic-gate 
1460Sstevel@tonic-gate void
ssh_gssapi_mech_oid_to_kexname(const gss_OID mech,char ** kexname)1470Sstevel@tonic-gate ssh_gssapi_mech_oid_to_kexname(const gss_OID mech, char **kexname)
1480Sstevel@tonic-gate {
1490Sstevel@tonic-gate 	ssh_gss_kex_mapping **p;
1500Sstevel@tonic-gate 
1510Sstevel@tonic-gate 	if (mech == GSS_C_NULL_OID || !kexname)
1520Sstevel@tonic-gate 		return;
1530Sstevel@tonic-gate 
1540Sstevel@tonic-gate 	*kexname = NULL; /* default to not found */
1550Sstevel@tonic-gate 	if (gss_enc2oid) {
1566080Sjp161948 		for (p = gss_enc2oid; p && *p; p++) {
1570Sstevel@tonic-gate 			if (mech->length == (*p)->oid->length &&
1580Sstevel@tonic-gate 			    memcmp(mech->elements, (*p)->oid->elements,
1596080Sjp161948 			    mech->length) == 0)
1600Sstevel@tonic-gate 				*kexname = xstrdup((*p)->encoded);
1610Sstevel@tonic-gate 		}
1620Sstevel@tonic-gate 	}
1630Sstevel@tonic-gate 
1640Sstevel@tonic-gate 	if (*kexname)
1650Sstevel@tonic-gate 		return; /* found */
1660Sstevel@tonic-gate 
1670Sstevel@tonic-gate 	ssh_gssapi_encode_oid_for_kex(mech, kexname);
1680Sstevel@tonic-gate }
1690Sstevel@tonic-gate 
1700Sstevel@tonic-gate void
ssh_gssapi_oid_of_kexname(const char * kexname,gss_OID * mech)1710Sstevel@tonic-gate ssh_gssapi_oid_of_kexname(const char *kexname, gss_OID *mech)
1720Sstevel@tonic-gate {
1730Sstevel@tonic-gate 	ssh_gss_kex_mapping **p;
1740Sstevel@tonic-gate 
1750Sstevel@tonic-gate 	if (!mech || !kexname || !*kexname)
1760Sstevel@tonic-gate 		return;
1770Sstevel@tonic-gate 
1780Sstevel@tonic-gate 	*mech = GSS_C_NULL_OID; /* default to not found */
1790Sstevel@tonic-gate 
1800Sstevel@tonic-gate 	if (!gss_enc2oid)
1810Sstevel@tonic-gate 		return;
1820Sstevel@tonic-gate 
1836080Sjp161948 	for (p = gss_enc2oid; p && *p; p++) {
1840Sstevel@tonic-gate 		if (strcmp(kexname, (*p)->encoded) == 0) {
1850Sstevel@tonic-gate 			*mech = (*p)->oid;
1860Sstevel@tonic-gate 			return;
1870Sstevel@tonic-gate 		}
1880Sstevel@tonic-gate 	}
1890Sstevel@tonic-gate }
1900Sstevel@tonic-gate 
1910Sstevel@tonic-gate static
1920Sstevel@tonic-gate void
ssh_gssapi_encode_oid_for_kex(const gss_OID oid,char ** enc_name)1930Sstevel@tonic-gate ssh_gssapi_encode_oid_for_kex(const gss_OID oid, char **enc_name)
1940Sstevel@tonic-gate {
1956080Sjp161948 	Buffer buf;
1966080Sjp161948 	OM_uint32 oidlen;
1976080Sjp161948 	uint_t enclen;
1986080Sjp161948 	const EVP_MD *evp_md = EVP_md5();
1996080Sjp161948 	EVP_MD_CTX md;
2006080Sjp161948 	uchar_t digest[EVP_MAX_MD_SIZE];
2016080Sjp161948 	char *encoded;
2020Sstevel@tonic-gate 
2030Sstevel@tonic-gate 	if (oid == GSS_C_NULL_OID || !enc_name)
2040Sstevel@tonic-gate 		return;
2050Sstevel@tonic-gate 
2060Sstevel@tonic-gate 	*enc_name = NULL;
2070Sstevel@tonic-gate 
2080Sstevel@tonic-gate 	oidlen = oid->length;
2090Sstevel@tonic-gate 
2100Sstevel@tonic-gate 	/* No GSS mechs have OIDs as long as 128 -- simplify DER encoding */
2110Sstevel@tonic-gate 	if (oidlen > 128)
2120Sstevel@tonic-gate 		return; /* fail gracefully */
2130Sstevel@tonic-gate 
2140Sstevel@tonic-gate 	/*
2150Sstevel@tonic-gate 	 * NOTE:  If we need to support SSH_BUG_GSSAPI_BER this is where
2160Sstevel@tonic-gate 	 * we'd do it.
2170Sstevel@tonic-gate 	 *
2180Sstevel@tonic-gate 	 * That means using "Se3H81ismmOC3OE+FwYCiQ==" for the Kerberos
2190Sstevel@tonic-gate 	 * V mech and "N3+k7/4wGxHyuP8Yxi4RhA==" for the GSI mech.  Ick.
2200Sstevel@tonic-gate 	 */
2210Sstevel@tonic-gate 
2220Sstevel@tonic-gate 	buffer_init(&buf);
2230Sstevel@tonic-gate 
2240Sstevel@tonic-gate 	/* UNIVERSAL class tag for OBJECT IDENTIFIER */
2250Sstevel@tonic-gate 	buffer_put_char(&buf, 0x06);
2260Sstevel@tonic-gate 	buffer_put_char(&buf, oidlen); /* one octet DER length -- see above */
2270Sstevel@tonic-gate 
2280Sstevel@tonic-gate 	/* OID elements */
2290Sstevel@tonic-gate 	buffer_append(&buf, oid->elements, oidlen);
2300Sstevel@tonic-gate 
2310Sstevel@tonic-gate 	/* Make digest */
2320Sstevel@tonic-gate 	EVP_DigestInit(&md, evp_md);
2330Sstevel@tonic-gate 	EVP_DigestUpdate(&md, buffer_ptr(&buf), buffer_len(&buf));
2340Sstevel@tonic-gate 	EVP_DigestFinal(&md, digest, NULL);
2350Sstevel@tonic-gate 	buffer_free(&buf);
2360Sstevel@tonic-gate 
2370Sstevel@tonic-gate 	/* Base 64 encoding */
2386080Sjp161948 	encoded = xmalloc(EVP_MD_size(evp_md)*2);
2396080Sjp161948 	enclen = __b64_ntop(digest, EVP_MD_size(evp_md),
2406080Sjp161948 	    encoded, EVP_MD_size(evp_md) * 2);
2410Sstevel@tonic-gate 	buffer_init(&buf);
2426080Sjp161948 	buffer_append(&buf, KEX_GSS_SHA1, sizeof (KEX_GSS_SHA1) - 1);
2430Sstevel@tonic-gate 	buffer_append(&buf, encoded, enclen);
2440Sstevel@tonic-gate 	buffer_put_char(&buf, '\0');
2456080Sjp161948 
2466080Sjp161948 	debug2("GSS-API Mechanism encoded as %s", encoded);
2477574SJan.Pechanec@Sun.COM 	xfree(encoded);
2480Sstevel@tonic-gate 
2490Sstevel@tonic-gate 	*enc_name = xstrdup(buffer_ptr(&buf));
2500Sstevel@tonic-gate 	buffer_free(&buf);
2510Sstevel@tonic-gate }
2520Sstevel@tonic-gate 
2536080Sjp161948 static char *
ssh_gssapi_make_kexalgs_list(gss_OID_set mechs,const char * old_kexalgs)2540Sstevel@tonic-gate ssh_gssapi_make_kexalgs_list(gss_OID_set mechs, const char *old_kexalgs)
2550Sstevel@tonic-gate {
2560Sstevel@tonic-gate 	char *gss_kexalgs, *new_kexalgs;
2570Sstevel@tonic-gate 	int len;
2580Sstevel@tonic-gate 
2590Sstevel@tonic-gate 	if (mechs == GSS_C_NULL_OID_SET)
2600Sstevel@tonic-gate 		return (xstrdup(old_kexalgs)); /* never null */
2610Sstevel@tonic-gate 
2620Sstevel@tonic-gate 	ssh_gssapi_mech_oids_to_kexnames(mechs, &gss_kexalgs);
2630Sstevel@tonic-gate 
2640Sstevel@tonic-gate 	if (gss_kexalgs == NULL || *gss_kexalgs == '\0')
2650Sstevel@tonic-gate 		return (xstrdup(old_kexalgs)); /* never null */
2660Sstevel@tonic-gate 
2670Sstevel@tonic-gate 	if (old_kexalgs == NULL || *old_kexalgs == '\0')
2680Sstevel@tonic-gate 		return (gss_kexalgs);
2690Sstevel@tonic-gate 
2700Sstevel@tonic-gate 	len = strlen(old_kexalgs) + strlen(gss_kexalgs) + 2;
2710Sstevel@tonic-gate 	new_kexalgs = xmalloc(len);
2720Sstevel@tonic-gate 	(void) snprintf(new_kexalgs, len, "%s,%s", gss_kexalgs, old_kexalgs);
2737574SJan.Pechanec@Sun.COM 	xfree(gss_kexalgs);
2740Sstevel@tonic-gate 
2750Sstevel@tonic-gate 	return (new_kexalgs);
2760Sstevel@tonic-gate }
2770Sstevel@tonic-gate 
2780Sstevel@tonic-gate void
ssh_gssapi_modify_kex(Kex * kex,gss_OID_set mechs,char ** proposal)2790Sstevel@tonic-gate ssh_gssapi_modify_kex(Kex *kex, gss_OID_set mechs, char **proposal)
2800Sstevel@tonic-gate {
2810Sstevel@tonic-gate 	char *kexalgs, *orig_kexalgs, *p;
2820Sstevel@tonic-gate 	char **hostalg, *orig_hostalgs, *new_hostalgs;
2830Sstevel@tonic-gate 	char **hostalgs;
2840Sstevel@tonic-gate 	gss_OID_set dup_mechs;
2850Sstevel@tonic-gate 	OM_uint32 maj, min;
2860Sstevel@tonic-gate 	int i;
2870Sstevel@tonic-gate 
2880Sstevel@tonic-gate 	if (kex == NULL || proposal == NULL ||
2897574SJan.Pechanec@Sun.COM 	    proposal[PROPOSAL_KEX_ALGS] == NULL) {
2900Sstevel@tonic-gate 		fatal("INTERNAL ERROR (%s)", __func__);
2910Sstevel@tonic-gate 	}
2920Sstevel@tonic-gate 
2930Sstevel@tonic-gate 	orig_hostalgs = proposal[PROPOSAL_SERVER_HOST_KEY_ALGS];
2940Sstevel@tonic-gate 
2950Sstevel@tonic-gate 	if (kex->mechs == GSS_C_NULL_OID_SET && mechs == GSS_C_NULL_OID_SET)
2960Sstevel@tonic-gate 		return; /* didn't offer GSS last time, not offering now */
2970Sstevel@tonic-gate 
2980Sstevel@tonic-gate 	if (kex->mechs == GSS_C_NULL_OID_SET || mechs == GSS_C_NULL_OID_SET)
2990Sstevel@tonic-gate 		goto mod_offer; /* didn't offer last time or not offering now */
3000Sstevel@tonic-gate 
3010Sstevel@tonic-gate 	/* Check if mechs is congruent to kex->mechs (last offered) */
3020Sstevel@tonic-gate 	if (kex->mechs->count == mechs->count) {
3030Sstevel@tonic-gate 		int present, matches = 0;
3040Sstevel@tonic-gate 
3056080Sjp161948 		for (i = 0; i < mechs->count; i++) {
3060Sstevel@tonic-gate 			maj = gss_test_oid_set_member(&min,
3076080Sjp161948 			    &kex->mechs->elements[i], mechs, &present);
3080Sstevel@tonic-gate 
3090Sstevel@tonic-gate 			if (GSS_ERROR(maj)) {
3100Sstevel@tonic-gate 				mechs = GSS_C_NULL_OID_SET;
3110Sstevel@tonic-gate 				break;
3120Sstevel@tonic-gate 			}
3130Sstevel@tonic-gate 
3140Sstevel@tonic-gate 			matches += (present) ? 1 : 0;
3150Sstevel@tonic-gate 		}
3160Sstevel@tonic-gate 
3170Sstevel@tonic-gate 		if (matches == kex->mechs->count)
3180Sstevel@tonic-gate 			return; /* no change in offer from last time */
3190Sstevel@tonic-gate 	}
3200Sstevel@tonic-gate 
3210Sstevel@tonic-gate mod_offer:
3220Sstevel@tonic-gate 	/*
3230Sstevel@tonic-gate 	 * Remove previously offered mechs from PROPOSAL_KEX_ALGS proposal
3240Sstevel@tonic-gate 	 *
3250Sstevel@tonic-gate 	 * ASSUMPTION: GSS-API kex algs always go in front, so removing
3260Sstevel@tonic-gate 	 * them is a matter of skipping them.
3270Sstevel@tonic-gate 	 */
3280Sstevel@tonic-gate 	p = kexalgs = orig_kexalgs = proposal[PROPOSAL_KEX_ALGS];
3290Sstevel@tonic-gate 	while (p != NULL && *p != '\0' &&
3306080Sjp161948 	    strncmp(p, KEX_GSS_SHA1, strlen(KEX_GSS_SHA1)) == 0) {
3310Sstevel@tonic-gate 
3320Sstevel@tonic-gate 		if ((p = strchr(p, ',')) == NULL)
3330Sstevel@tonic-gate 			break;
3340Sstevel@tonic-gate 		p++;
3350Sstevel@tonic-gate 		kexalgs = p;
3360Sstevel@tonic-gate 
3370Sstevel@tonic-gate 	}
3380Sstevel@tonic-gate 	kexalgs = proposal[PROPOSAL_KEX_ALGS] = xstrdup(kexalgs);
3390Sstevel@tonic-gate 	xfree(orig_kexalgs);
3400Sstevel@tonic-gate 
3410Sstevel@tonic-gate 	(void) gss_release_oid_set(&min, &kex->mechs); /* ok if !kex->mechs */
3420Sstevel@tonic-gate 
3437574SJan.Pechanec@Sun.COM 	/* Not offering GSS kex algorithms now -> all done */
3440Sstevel@tonic-gate 	if (mechs == GSS_C_NULL_OID_SET)
3450Sstevel@tonic-gate 		return;
3460Sstevel@tonic-gate 
3470Sstevel@tonic-gate 	/* Remember mechs we're offering */
3480Sstevel@tonic-gate 	maj = gss_create_empty_oid_set(&min, &dup_mechs);
3490Sstevel@tonic-gate 	if (GSS_ERROR(maj))
3500Sstevel@tonic-gate 		return;
3516080Sjp161948 	for (i = 0; i < mechs->count; i++) {
3520Sstevel@tonic-gate 		maj = gss_add_oid_set_member(&min, &mechs->elements[i],
3536080Sjp161948 		    &dup_mechs);
3540Sstevel@tonic-gate 
3550Sstevel@tonic-gate 		if (GSS_ERROR(maj)) {
3560Sstevel@tonic-gate 			(void) gss_release_oid_set(&min, &dup_mechs);
3570Sstevel@tonic-gate 			return;
3580Sstevel@tonic-gate 		}
3590Sstevel@tonic-gate 	}
3600Sstevel@tonic-gate 
3617574SJan.Pechanec@Sun.COM 	/* Add mechs to kex algorithms ... */
3626080Sjp161948 	proposal[PROPOSAL_KEX_ALGS] = ssh_gssapi_make_kexalgs_list(mechs,
3636080Sjp161948 	    kexalgs);
3647574SJan.Pechanec@Sun.COM 	xfree(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, ',');
3786080Sjp161948 		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 
3896080Sjp161948 			len = strlen(orig_hostalgs) + sizeof (",null");
3900Sstevel@tonic-gate 			new_hostalgs = xmalloc(len);
3910Sstevel@tonic-gate 			(void) snprintf(new_hostalgs, len, "%s,null",
3926080Sjp161948 			    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
ssh_gssapi_is_spnego(gss_OID oid)4270Sstevel@tonic-gate ssh_gssapi_is_spnego(gss_OID oid)
4280Sstevel@tonic-gate {
4290Sstevel@tonic-gate 	return (oid->length == 6 &&
4306080Sjp161948 	    memcmp("\053\006\001\005\005\002", oid->elements, 6) == 0);
4310Sstevel@tonic-gate }
4320Sstevel@tonic-gate 
4330Sstevel@tonic-gate int
ssh_gssapi_is_krb5(gss_OID oid)4340Sstevel@tonic-gate ssh_gssapi_is_krb5(gss_OID oid)
4350Sstevel@tonic-gate {
4360Sstevel@tonic-gate 	return (oid->length == 9 &&
4376080Sjp161948 	    memcmp("\x2A\x86\x48\x86\xF7\x12\x01\x02\x02",
4386080Sjp161948 	    oid->elements, 9) == 0);
4390Sstevel@tonic-gate }
4400Sstevel@tonic-gate 
4410Sstevel@tonic-gate int
ssh_gssapi_is_dh(gss_OID oid)4420Sstevel@tonic-gate ssh_gssapi_is_dh(gss_OID oid)
4430Sstevel@tonic-gate {
4440Sstevel@tonic-gate 	return (oid->length == 9 &&
4456080Sjp161948 	    memcmp("\053\006\004\001\052\002\032\002\005",
4466080Sjp161948 	    oid->elements, 9) == 0);
4470Sstevel@tonic-gate }
4480Sstevel@tonic-gate 
4490Sstevel@tonic-gate int
ssh_gssapi_is_gsi(gss_OID oid)4500Sstevel@tonic-gate ssh_gssapi_is_gsi(gss_OID oid)
4510Sstevel@tonic-gate {
4520Sstevel@tonic-gate 	return (oid->length == 9 &&
4536080Sjp161948 	    memcmp("\x2B\x06\x01\x04\x01\x9B\x50\x01\x01",
4546080Sjp161948 	    oid->elements, 9) == 0);
4550Sstevel@tonic-gate }
4560Sstevel@tonic-gate 
4576080Sjp161948 const char *
ssh_gssapi_oid_to_name(gss_OID oid)4580Sstevel@tonic-gate ssh_gssapi_oid_to_name(gss_OID oid)
4590Sstevel@tonic-gate {
4600Sstevel@tonic-gate #ifdef HAVE_GSS_OID_TO_MECH
4616080Sjp161948 	return (__gss_oid_to_mech(oid));
4620Sstevel@tonic-gate #else
4630Sstevel@tonic-gate 	if (ssh_gssapi_is_krb5(oid))
4646080Sjp161948 		return ("Kerberos");
4650Sstevel@tonic-gate 	if (ssh_gssapi_is_gsi(oid))
4666080Sjp161948 		return ("GSI");
4676080Sjp161948 	return ("(unknown)");
4680Sstevel@tonic-gate #endif /* HAVE_GSS_OID_TO_MECH */
4690Sstevel@tonic-gate }
4700Sstevel@tonic-gate 
4710Sstevel@tonic-gate char *
ssh_gssapi_oid_to_str(gss_OID oid)4720Sstevel@tonic-gate ssh_gssapi_oid_to_str(gss_OID oid)
4730Sstevel@tonic-gate {
4740Sstevel@tonic-gate #ifdef HAVE_GSS_OID_TO_STR
4750Sstevel@tonic-gate 	gss_buffer_desc	str_buf;
4760Sstevel@tonic-gate 	char		*str;
4770Sstevel@tonic-gate 	OM_uint32	maj, min;
4780Sstevel@tonic-gate 
4790Sstevel@tonic-gate 	maj = gss_oid_to_str(&min, oid, &str_buf);
4800Sstevel@tonic-gate 
4810Sstevel@tonic-gate 	if (GSS_ERROR(maj))
4826080Sjp161948 		return (xstrdup("<gss_oid_to_str() failed>"));
4830Sstevel@tonic-gate 
4840Sstevel@tonic-gate 	str = xmalloc(str_buf.length + 1);
4850Sstevel@tonic-gate 	memset(str, 0, str_buf.length + 1);
4860Sstevel@tonic-gate 	strlcpy(str, str_buf.value, str_buf.length + 1);
4870Sstevel@tonic-gate 	(void) gss_release_buffer(&min, &str_buf);
4880Sstevel@tonic-gate 
4896080Sjp161948 	return (str);
4900Sstevel@tonic-gate #else
4916080Sjp161948 	return (xstrdup("<gss_oid_to_str() unsupported>"));
4920Sstevel@tonic-gate #endif /* HAVE_GSS_OID_TO_STR */
4930Sstevel@tonic-gate }
4940Sstevel@tonic-gate 
4950Sstevel@tonic-gate /* Check that the OID in a data stream matches that in the context */
4966080Sjp161948 int
ssh_gssapi_check_mech_oid(Gssctxt * ctx,void * data,size_t len)4976080Sjp161948 ssh_gssapi_check_mech_oid(Gssctxt *ctx, void *data, size_t len)
4986080Sjp161948 {
4990Sstevel@tonic-gate 
5006080Sjp161948 	return (ctx != NULL && ctx->desired_mech != GSS_C_NULL_OID &&
5016080Sjp161948 	    ctx->desired_mech->length == len &&
5026080Sjp161948 	    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 */
5066080Sjp161948 void
ssh_gssapi_set_oid_data(Gssctxt * ctx,void * data,size_t len)5076080Sjp161948 ssh_gssapi_set_oid_data(Gssctxt *ctx, void *data, size_t len)
5086080Sjp161948 {
5096080Sjp161948 	if (ctx->actual_mech != GSS_C_NULL_OID) {
5106080Sjp161948 		xfree(ctx->actual_mech->elements);
5116080Sjp161948 		xfree(ctx->actual_mech);
5126080Sjp161948 	}
5136080Sjp161948 	ctx->actual_mech = xmalloc(sizeof (gss_OID_desc));
5146080Sjp161948 	ctx->actual_mech->length = len;
5156080Sjp161948 	ctx->actual_mech->elements = xmalloc(len);
5166080Sjp161948 	memcpy(ctx->actual_mech->elements, data, len);
5170Sstevel@tonic-gate }
5180Sstevel@tonic-gate 
5190Sstevel@tonic-gate /* Set the contexts OID */
5206080Sjp161948 void
ssh_gssapi_set_oid(Gssctxt * ctx,gss_OID oid)5216080Sjp161948 ssh_gssapi_set_oid(Gssctxt *ctx, gss_OID oid)
5226080Sjp161948 {
5236080Sjp161948 	ssh_gssapi_set_oid_data(ctx, oid->elements, oid->length);
5240Sstevel@tonic-gate }
5250Sstevel@tonic-gate 
5260Sstevel@tonic-gate /* All this effort to report an error ... */
5270Sstevel@tonic-gate 
5280Sstevel@tonic-gate void
ssh_gssapi_error(Gssctxt * ctxt,const char * where)5295562Sjp161948 ssh_gssapi_error(Gssctxt *ctxt, const char *where)
5305562Sjp161948 {
5315562Sjp161948 	char *errmsg = ssh_gssapi_last_error(ctxt, NULL, NULL);
5325562Sjp161948 
5335562Sjp161948 	if (where != NULL)
5345562Sjp161948 		debug("GSS-API error while %s: %s", where, errmsg);
5350Sstevel@tonic-gate 	else
5365562Sjp161948 		debug("GSS-API error: %s", errmsg);
5375562Sjp161948 
5385562Sjp161948 	/* ssh_gssapi_last_error() can't return NULL */
5395562Sjp161948 	xfree(errmsg);
5400Sstevel@tonic-gate }
5410Sstevel@tonic-gate 
5420Sstevel@tonic-gate char *
ssh_gssapi_last_error(Gssctxt * ctxt,OM_uint32 * major_status,OM_uint32 * minor_status)5436080Sjp161948 ssh_gssapi_last_error(Gssctxt *ctxt, OM_uint32 *major_status,
5446080Sjp161948     OM_uint32 *minor_status)
5456080Sjp161948 {
5460Sstevel@tonic-gate 	OM_uint32 lmin, more;
5470Sstevel@tonic-gate 	OM_uint32 maj, min;
5480Sstevel@tonic-gate 	gss_OID mech = GSS_C_NULL_OID;
5496080Sjp161948 	gss_buffer_desc msg;
5506080Sjp161948 	Buffer b;
5516080Sjp161948 	char *ret;
5520Sstevel@tonic-gate 
5536080Sjp161948 	buffer_init(&b);
5540Sstevel@tonic-gate 
5550Sstevel@tonic-gate 	if (ctxt) {
5560Sstevel@tonic-gate 		/* Get status codes from the Gssctxt */
5570Sstevel@tonic-gate 		maj = ctxt->major;
5580Sstevel@tonic-gate 		min = ctxt->minor;
5590Sstevel@tonic-gate 		/* Output them if desired */
5600Sstevel@tonic-gate 		if (major_status)
5610Sstevel@tonic-gate 			*major_status = maj;
5620Sstevel@tonic-gate 		if (minor_status)
5630Sstevel@tonic-gate 			*minor_status = min;
5640Sstevel@tonic-gate 		/* Get mechanism for minor status display */
5650Sstevel@tonic-gate 		mech = (ctxt->actual_mech != GSS_C_NULL_OID) ?
5666080Sjp161948 		    ctxt->actual_mech : ctxt->desired_mech;
5670Sstevel@tonic-gate 	} else if (major_status && minor_status) {
5680Sstevel@tonic-gate 		maj = *major_status;
5690Sstevel@tonic-gate 		min = *major_status;
5700Sstevel@tonic-gate 	} else {
5710Sstevel@tonic-gate 		maj = GSS_S_COMPLETE;
5720Sstevel@tonic-gate 		min = 0;
5730Sstevel@tonic-gate 	}
5740Sstevel@tonic-gate 
5756080Sjp161948 	more = 0;
5760Sstevel@tonic-gate 	/* The GSSAPI error */
5776080Sjp161948 	do {
5786080Sjp161948 		gss_display_status(&lmin, maj, GSS_C_GSS_CODE,
5796080Sjp161948 		    GSS_C_NULL_OID, &more, &msg);
5800Sstevel@tonic-gate 
5816080Sjp161948 		buffer_append(&b, msg.value, msg.length);
5826080Sjp161948 		buffer_put_char(&b, '\n');
5836080Sjp161948 		gss_release_buffer(&lmin, &msg);
5846080Sjp161948 	} while (more != 0);
5850Sstevel@tonic-gate 
5866080Sjp161948 	/* The mechanism specific error */
5876080Sjp161948 	do {
5880Sstevel@tonic-gate 		/*
5890Sstevel@tonic-gate 		 * If mech == GSS_C_NULL_OID we may get the default
5900Sstevel@tonic-gate 		 * mechanism, whatever that is, and that may not be
5910Sstevel@tonic-gate 		 * useful.
5920Sstevel@tonic-gate 		 */
5936080Sjp161948 		gss_display_status(&lmin, min, GSS_C_MECH_CODE, mech, &more,
5946080Sjp161948 		    &msg);
5950Sstevel@tonic-gate 
5966080Sjp161948 		buffer_append(&b, msg.value, msg.length);
5976080Sjp161948 		buffer_put_char(&b, '\n');
5980Sstevel@tonic-gate 
5996080Sjp161948 		gss_release_buffer(&lmin, &msg);
6006080Sjp161948 	} while (more != 0);
6010Sstevel@tonic-gate 
6026080Sjp161948 	buffer_put_char(&b, '\0');
6036080Sjp161948 	ret = xstrdup(buffer_ptr(&b));
6046080Sjp161948 	buffer_free(&b);
6050Sstevel@tonic-gate 
6066080Sjp161948 	return (ret);
6070Sstevel@tonic-gate }
6080Sstevel@tonic-gate 
6096080Sjp161948 /*
6106080Sjp161948  * Initialise our GSSAPI context. We use this opaque structure to contain all
6110Sstevel@tonic-gate  * of the data which both the client and server need to persist across
6120Sstevel@tonic-gate  * {accept,init}_sec_context calls, so that when we do it from the userauth
6130Sstevel@tonic-gate  * stuff life is a little easier
6140Sstevel@tonic-gate  */
6150Sstevel@tonic-gate void
ssh_gssapi_build_ctx(Gssctxt ** ctx,int client,gss_OID mech)6160Sstevel@tonic-gate ssh_gssapi_build_ctx(Gssctxt **ctx, int client, gss_OID mech)
6170Sstevel@tonic-gate {
6180Sstevel@tonic-gate 	Gssctxt *newctx;
6190Sstevel@tonic-gate 
6200Sstevel@tonic-gate 
6210Sstevel@tonic-gate 	newctx = (Gssctxt*)xmalloc(sizeof (Gssctxt));
6226080Sjp161948 	memset(newctx, 0, sizeof (Gssctxt));
6230Sstevel@tonic-gate 
6241776Snw141292 
6250Sstevel@tonic-gate 	newctx->local = client;
6260Sstevel@tonic-gate 	newctx->desired_mech = ssh_gssapi_dup_oid(mech);
6276080Sjp161948 
6280Sstevel@tonic-gate 	/* This happens to be redundant given the memset() above */
6290Sstevel@tonic-gate 	newctx->major = GSS_S_COMPLETE;
6300Sstevel@tonic-gate 	newctx->context = GSS_C_NO_CONTEXT;
6310Sstevel@tonic-gate 	newctx->actual_mech =  GSS_C_NULL_OID;
6320Sstevel@tonic-gate 	newctx->desired_name = GSS_C_NO_NAME;
6330Sstevel@tonic-gate 	newctx->src_name = GSS_C_NO_NAME;
6340Sstevel@tonic-gate 	newctx->dst_name = GSS_C_NO_NAME;
6350Sstevel@tonic-gate 	newctx->creds = GSS_C_NO_CREDENTIAL;
6360Sstevel@tonic-gate 	newctx->deleg_creds = GSS_C_NO_CREDENTIAL;
6370Sstevel@tonic-gate 
6381776Snw141292 	newctx->default_creds = (*ctx != NULL) ? (*ctx)->default_creds : 0;
6391776Snw141292 
6401776Snw141292 	ssh_gssapi_delete_ctx(ctx);
6411776Snw141292 
6420Sstevel@tonic-gate 	*ctx = newctx;
6430Sstevel@tonic-gate }
6440Sstevel@tonic-gate 
6450Sstevel@tonic-gate gss_OID
ssh_gssapi_dup_oid(gss_OID oid)6460Sstevel@tonic-gate ssh_gssapi_dup_oid(gss_OID oid)
6470Sstevel@tonic-gate {
6480Sstevel@tonic-gate 	gss_OID new_oid;
6490Sstevel@tonic-gate 
6506080Sjp161948 	new_oid = xmalloc(sizeof (gss_OID_desc));
6510Sstevel@tonic-gate 
6520Sstevel@tonic-gate 	new_oid->elements = xmalloc(oid->length);
6530Sstevel@tonic-gate 	new_oid->length = oid->length;
6540Sstevel@tonic-gate 	memcpy(new_oid->elements, oid->elements, oid->length);
6550Sstevel@tonic-gate 
6560Sstevel@tonic-gate 	return (new_oid);
6570Sstevel@tonic-gate }
6580Sstevel@tonic-gate 
6590Sstevel@tonic-gate gss_OID
ssh_gssapi_make_oid(size_t length,void * elements)6600Sstevel@tonic-gate ssh_gssapi_make_oid(size_t length, void *elements)
6610Sstevel@tonic-gate {
6620Sstevel@tonic-gate 	gss_OID_desc oid;
6630Sstevel@tonic-gate 
6640Sstevel@tonic-gate 	oid.length = length;
6650Sstevel@tonic-gate 	oid.elements = elements;
6660Sstevel@tonic-gate 
6670Sstevel@tonic-gate 	return (ssh_gssapi_dup_oid(&oid));
6680Sstevel@tonic-gate }
6690Sstevel@tonic-gate 
6700Sstevel@tonic-gate void
ssh_gssapi_release_oid(gss_OID * oid)6710Sstevel@tonic-gate ssh_gssapi_release_oid(gss_OID *oid)
6720Sstevel@tonic-gate {
6730Sstevel@tonic-gate 	OM_uint32 min;
6740Sstevel@tonic-gate 
6750Sstevel@tonic-gate 	if (oid && *oid == GSS_C_NULL_OID)
6760Sstevel@tonic-gate 		return;
6770Sstevel@tonic-gate 	(void) gss_release_oid(&min, oid);
6780Sstevel@tonic-gate 
6790Sstevel@tonic-gate 	if (*oid == GSS_C_NULL_OID)
6800Sstevel@tonic-gate 		return; /* libgss did own this gss_OID and released it */
6810Sstevel@tonic-gate 
6820Sstevel@tonic-gate 	xfree((*oid)->elements);
6830Sstevel@tonic-gate 	xfree(*oid);
6840Sstevel@tonic-gate 	*oid = GSS_C_NULL_OID;
6850Sstevel@tonic-gate }
6860Sstevel@tonic-gate 
6870Sstevel@tonic-gate struct gss_name {
6886080Sjp161948 	gss_OID		name_type;
6896080Sjp161948 	gss_buffer_t	external_name;
6906080Sjp161948 	gss_OID		mech_type;
6916080Sjp161948 	void		*mech_name;
6920Sstevel@tonic-gate };
6930Sstevel@tonic-gate 
6940Sstevel@tonic-gate /* Delete our context, providing it has been built correctly */
6950Sstevel@tonic-gate void
ssh_gssapi_delete_ctx(Gssctxt ** ctx)6960Sstevel@tonic-gate ssh_gssapi_delete_ctx(Gssctxt **ctx)
6970Sstevel@tonic-gate {
6980Sstevel@tonic-gate 	OM_uint32 ms;
6990Sstevel@tonic-gate 
7000Sstevel@tonic-gate 	if ((*ctx) == NULL)
7010Sstevel@tonic-gate 		return;
7020Sstevel@tonic-gate 
7030Sstevel@tonic-gate 	if ((*ctx)->context != GSS_C_NO_CONTEXT)
7046080Sjp161948 		gss_delete_sec_context(&ms, &(*ctx)->context, GSS_C_NO_BUFFER);
7056080Sjp161948 #if 0
7066080Sjp161948 	/* XXX */
7076080Sjp161948 	if ((*ctx)->desired_mech != GSS_C_NULL_OID)
7086080Sjp161948 		ssh_gssapi_release_oid(&(*ctx)->desired_mech);
7096080Sjp161948 #endif
7100Sstevel@tonic-gate 	if ((*ctx)->actual_mech != GSS_C_NULL_OID)
7110Sstevel@tonic-gate 		(void) ssh_gssapi_release_oid(&(*ctx)->actual_mech);
7120Sstevel@tonic-gate 	if ((*ctx)->desired_name != GSS_C_NO_NAME)
7136080Sjp161948 		gss_release_name(&ms, &(*ctx)->desired_name);
7146080Sjp161948 #if 0
7156080Sjp161948 	if ((*ctx)->src_name != GSS_C_NO_NAME)
7166080Sjp161948 		gss_release_name(&ms, &(*ctx)->src_name);
7176080Sjp161948 #endif
7180Sstevel@tonic-gate 	if ((*ctx)->dst_name != GSS_C_NO_NAME)
7196080Sjp161948 		gss_release_name(&ms, &(*ctx)->dst_name);
7200Sstevel@tonic-gate 	if ((*ctx)->creds != GSS_C_NO_CREDENTIAL)
7216080Sjp161948 		gss_release_cred(&ms, &(*ctx)->creds);
7220Sstevel@tonic-gate 	if ((*ctx)->deleg_creds != GSS_C_NO_CREDENTIAL)
7236080Sjp161948 		gss_release_cred(&ms, &(*ctx)->deleg_creds);
7240Sstevel@tonic-gate 
7250Sstevel@tonic-gate 	xfree(*ctx);
7266080Sjp161948 	*ctx = NULL;
7270Sstevel@tonic-gate }
7280Sstevel@tonic-gate 
7290Sstevel@tonic-gate /* Create a GSS hostbased service principal name for a given server hostname */
7300Sstevel@tonic-gate int
ssh_gssapi_import_name(Gssctxt * ctx,const char * server_host)7310Sstevel@tonic-gate ssh_gssapi_import_name(Gssctxt *ctx, const char *server_host)
7320Sstevel@tonic-gate {
7330Sstevel@tonic-gate 	gss_buffer_desc name_buf;
7340Sstevel@tonic-gate 	int		ret;
7350Sstevel@tonic-gate 
7360Sstevel@tonic-gate 	/* Build target principal */
7370Sstevel@tonic-gate 	name_buf.length = strlen(SSH_GSS_HOSTBASED_SERVICE) +
7386080Sjp161948 	    strlen(server_host) + 1; /* +1 for '@' */
7390Sstevel@tonic-gate 	name_buf.value = xmalloc(name_buf.length + 1); /* +1 for NUL */
7400Sstevel@tonic-gate 	ret = snprintf(name_buf.value, name_buf.length + 1, "%s@%s",
7416080Sjp161948 	    SSH_GSS_HOSTBASED_SERVICE, server_host);
7420Sstevel@tonic-gate 
7436080Sjp161948 	debug3("%s: snprintf() returned %d, expected %d", __func__, ret,
744*9845SJan.Pechanec@Sun.COM 	    name_buf.length);
7450Sstevel@tonic-gate 
7460Sstevel@tonic-gate 	ctx->major = gss_import_name(&ctx->minor, &name_buf,
7476080Sjp161948 	    GSS_C_NT_HOSTBASED_SERVICE, &ctx->desired_name);
7480Sstevel@tonic-gate 
7490Sstevel@tonic-gate 	if (GSS_ERROR(ctx->major)) {
7500Sstevel@tonic-gate 		ssh_gssapi_error(ctx, "calling GSS_Import_name()");
7516080Sjp161948 		return (0);
7520Sstevel@tonic-gate 	}
7530Sstevel@tonic-gate 
7540Sstevel@tonic-gate 	xfree(name_buf.value);
7550Sstevel@tonic-gate 
7566080Sjp161948 	return (1);
7570Sstevel@tonic-gate }
7580Sstevel@tonic-gate 
7590Sstevel@tonic-gate OM_uint32
ssh_gssapi_get_mic(Gssctxt * ctx,gss_buffer_desc * buffer,gss_buffer_desc * hash)7606080Sjp161948 ssh_gssapi_get_mic(Gssctxt *ctx, gss_buffer_desc *buffer, gss_buffer_desc *hash)
7616080Sjp161948 {
7620Sstevel@tonic-gate 
7636080Sjp161948 	ctx->major = gss_get_mic(&ctx->minor, ctx->context,
7646080Sjp161948 	    GSS_C_QOP_DEFAULT, buffer, hash);
7650Sstevel@tonic-gate 	if (GSS_ERROR(ctx->major))
7660Sstevel@tonic-gate 		ssh_gssapi_error(ctx, "while getting MIC");
7676080Sjp161948 	return (ctx->major);
7680Sstevel@tonic-gate }
7690Sstevel@tonic-gate 
7700Sstevel@tonic-gate OM_uint32
ssh_gssapi_verify_mic(Gssctxt * ctx,gss_buffer_desc * buffer,gss_buffer_desc * hash)7716080Sjp161948 ssh_gssapi_verify_mic(Gssctxt *ctx, gss_buffer_desc *buffer,
7726080Sjp161948     gss_buffer_desc *hash)
7736080Sjp161948 {
7740Sstevel@tonic-gate 	gss_qop_t qop;
7750Sstevel@tonic-gate 
7766080Sjp161948 	ctx->major = gss_verify_mic(&ctx->minor, ctx->context, buffer,
7776080Sjp161948 	    hash, &qop);
7780Sstevel@tonic-gate 	if (GSS_ERROR(ctx->major))
7790Sstevel@tonic-gate 		ssh_gssapi_error(ctx, "while verifying MIC");
7806080Sjp161948 	return (ctx->major);
7810Sstevel@tonic-gate }
7820Sstevel@tonic-gate #endif /* GSSAPI */
783