xref: /titanic_41/usr/src/cmd/ssh/libssh/common/ssh-gss.c (revision b9aa66a73c9016cf5c71fe80efe90ce9f2ca5c73)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * Copyright (c) 2001-2003 Simon Wilkinson. All rights reserved. *
37c478bd9Sstevel@tonic-gate  * Redistribution and use in source and binary forms, with or without
47c478bd9Sstevel@tonic-gate  * modification, are permitted provided that the following conditions
57c478bd9Sstevel@tonic-gate  * are met:
67c478bd9Sstevel@tonic-gate  * 1. Redistributions of source code must retain the above copyright
77c478bd9Sstevel@tonic-gate  *    notice, this list of conditions and the following disclaimer.
87c478bd9Sstevel@tonic-gate  * 2. Redistributions in binary form must reproduce the above copyright
97c478bd9Sstevel@tonic-gate  *    notice, this list of conditions and the following disclaimer in the
107c478bd9Sstevel@tonic-gate  *    documentation and/or other materials provided with the distribution.
117c478bd9Sstevel@tonic-gate  *
127c478bd9Sstevel@tonic-gate  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR `AS IS'' AND ANY EXPRESS OR
137c478bd9Sstevel@tonic-gate  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
147c478bd9Sstevel@tonic-gate  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
157c478bd9Sstevel@tonic-gate  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
167c478bd9Sstevel@tonic-gate  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
177c478bd9Sstevel@tonic-gate  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
187c478bd9Sstevel@tonic-gate  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
197c478bd9Sstevel@tonic-gate  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
207c478bd9Sstevel@tonic-gate  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
217c478bd9Sstevel@tonic-gate  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
227c478bd9Sstevel@tonic-gate  */
237c478bd9Sstevel@tonic-gate /*
24*b9aa66a7SJan Pechanec  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
257c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
267c478bd9Sstevel@tonic-gate  */
277c478bd9Sstevel@tonic-gate 
287c478bd9Sstevel@tonic-gate #include "includes.h"
297c478bd9Sstevel@tonic-gate 
307c478bd9Sstevel@tonic-gate #ifdef GSSAPI
317c478bd9Sstevel@tonic-gate 
327c478bd9Sstevel@tonic-gate #include "ssh.h"
337c478bd9Sstevel@tonic-gate #include "ssh2.h"
347c478bd9Sstevel@tonic-gate #include "xmalloc.h"
357c478bd9Sstevel@tonic-gate #include "buffer.h"
367c478bd9Sstevel@tonic-gate #include "bufaux.h"
377c478bd9Sstevel@tonic-gate #include "packet.h"
387c478bd9Sstevel@tonic-gate #include "compat.h"
397c478bd9Sstevel@tonic-gate #include <openssl/evp.h>
407c478bd9Sstevel@tonic-gate #include "cipher.h"
417c478bd9Sstevel@tonic-gate #include "kex.h"
427c478bd9Sstevel@tonic-gate #include "log.h"
437c478bd9Sstevel@tonic-gate #include "compat.h"
447c478bd9Sstevel@tonic-gate #include "xlist.h"
457c478bd9Sstevel@tonic-gate 
467c478bd9Sstevel@tonic-gate #include <netdb.h>
477c478bd9Sstevel@tonic-gate 
487c478bd9Sstevel@tonic-gate #include "ssh-gss.h"
497c478bd9Sstevel@tonic-gate 
507c478bd9Sstevel@tonic-gate #ifdef HAVE_GSS_OID_TO_MECH
517c478bd9Sstevel@tonic-gate #include <gssapi/gssapi_ext.h>
527c478bd9Sstevel@tonic-gate #endif /* HAVE_GSS_OID_TO_MECH */
537c478bd9Sstevel@tonic-gate 
547c478bd9Sstevel@tonic-gate typedef struct {
557c478bd9Sstevel@tonic-gate 	char *encoded;
567c478bd9Sstevel@tonic-gate 	gss_OID oid;
577c478bd9Sstevel@tonic-gate } ssh_gss_kex_mapping;
587c478bd9Sstevel@tonic-gate 
597c478bd9Sstevel@tonic-gate static ssh_gss_kex_mapping **gss_enc2oid = NULL;
607c478bd9Sstevel@tonic-gate 
617c478bd9Sstevel@tonic-gate static void ssh_gssapi_encode_oid_for_kex(const gss_OID oid, char **enc_name);
627c478bd9Sstevel@tonic-gate static char *ssh_gssapi_make_kexalgs_list(gss_OID_set mechs,
637c478bd9Sstevel@tonic-gate     const char *old_kexalgs);
647c478bd9Sstevel@tonic-gate 
657c478bd9Sstevel@tonic-gate /*
667c478bd9Sstevel@tonic-gate  * Populate gss_enc2oid table and return list of kexnames.
677c478bd9Sstevel@tonic-gate  *
687c478bd9Sstevel@tonic-gate  * If called with both mechs == GSS_C_NULL_OID_SET and kexname_list == NULL
697c478bd9Sstevel@tonic-gate  * then cached gss_enc2oid table is cleaned up.
707c478bd9Sstevel@tonic-gate  */
717c478bd9Sstevel@tonic-gate void
ssh_gssapi_mech_oids_to_kexnames(const gss_OID_set mechs,char ** kexname_list)727c478bd9Sstevel@tonic-gate ssh_gssapi_mech_oids_to_kexnames(const gss_OID_set mechs, char **kexname_list)
737c478bd9Sstevel@tonic-gate {
747c478bd9Sstevel@tonic-gate 	ssh_gss_kex_mapping **new_gss_enc2oid, **p;
757c478bd9Sstevel@tonic-gate 	Buffer buf;
767c478bd9Sstevel@tonic-gate 	char *enc_name;
777c478bd9Sstevel@tonic-gate 	int i;
787c478bd9Sstevel@tonic-gate 
797c478bd9Sstevel@tonic-gate 	if (kexname_list != NULL)
807c478bd9Sstevel@tonic-gate 		*kexname_list = NULL; /* default to failed */
817c478bd9Sstevel@tonic-gate 
827c478bd9Sstevel@tonic-gate 	if (mechs != GSS_C_NULL_OID_SET || kexname_list == NULL) {
837c478bd9Sstevel@tonic-gate 		/* Cleanup gss_enc2oid table */
847c478bd9Sstevel@tonic-gate 		for (p = gss_enc2oid; p != NULL && *p != NULL; p++) {
857c478bd9Sstevel@tonic-gate 			if ((*p)->encoded)
867c478bd9Sstevel@tonic-gate 				xfree((*p)->encoded);
877c478bd9Sstevel@tonic-gate 			ssh_gssapi_release_oid(&(*p)->oid);
887c478bd9Sstevel@tonic-gate 			xfree(*p);
897c478bd9Sstevel@tonic-gate 		}
907c478bd9Sstevel@tonic-gate 		if (gss_enc2oid)
917c478bd9Sstevel@tonic-gate 			xfree(gss_enc2oid);
927c478bd9Sstevel@tonic-gate 	}
937c478bd9Sstevel@tonic-gate 
947c478bd9Sstevel@tonic-gate 	if (mechs == GSS_C_NULL_OID_SET && kexname_list == NULL)
957c478bd9Sstevel@tonic-gate 		return; /* nothing left to do */
967c478bd9Sstevel@tonic-gate 
977c478bd9Sstevel@tonic-gate 	if (mechs) {
987c478bd9Sstevel@tonic-gate 		gss_OID mech;
997c478bd9Sstevel@tonic-gate 		/* Populate gss_enc2oid table */
1007c478bd9Sstevel@tonic-gate 		new_gss_enc2oid = xmalloc(sizeof (ssh_gss_kex_mapping *) *
1017c478bd9Sstevel@tonic-gate 		    (mechs->count + 1));
1027c478bd9Sstevel@tonic-gate 		memset(new_gss_enc2oid, 0,
1037c478bd9Sstevel@tonic-gate 		    sizeof (ssh_gss_kex_mapping *) * (mechs->count + 1));
1047c478bd9Sstevel@tonic-gate 
1057c478bd9Sstevel@tonic-gate 		for (i = 0; i < mechs->count; i++) {
1067c478bd9Sstevel@tonic-gate 			mech = &mechs->elements[i];
1077c478bd9Sstevel@tonic-gate 			ssh_gssapi_encode_oid_for_kex((const gss_OID)mech,
1087c478bd9Sstevel@tonic-gate 			    &enc_name);
1097c478bd9Sstevel@tonic-gate 
1107c478bd9Sstevel@tonic-gate 			if (!enc_name)
1117c478bd9Sstevel@tonic-gate 				continue;
1127c478bd9Sstevel@tonic-gate 
1137c478bd9Sstevel@tonic-gate 			new_gss_enc2oid[i] =
1147c478bd9Sstevel@tonic-gate 			    xmalloc(sizeof (ssh_gss_kex_mapping));
1157c478bd9Sstevel@tonic-gate 			(new_gss_enc2oid[i])->encoded = enc_name;
1167c478bd9Sstevel@tonic-gate 			(new_gss_enc2oid[i])->oid =
1177c478bd9Sstevel@tonic-gate 			    ssh_gssapi_dup_oid(&mechs->elements[i]);
1187c478bd9Sstevel@tonic-gate 		}
1197c478bd9Sstevel@tonic-gate 
1207c478bd9Sstevel@tonic-gate 		/* Do this last to avoid run-ins with fatal_cleanups */
1217c478bd9Sstevel@tonic-gate 		gss_enc2oid = new_gss_enc2oid;
1227c478bd9Sstevel@tonic-gate 	}
1237c478bd9Sstevel@tonic-gate 
1247c478bd9Sstevel@tonic-gate 	if (!kexname_list)
1257c478bd9Sstevel@tonic-gate 		return; /* nothing left to do */
1267c478bd9Sstevel@tonic-gate 
1277c478bd9Sstevel@tonic-gate 	/* Make kex name list */
1287c478bd9Sstevel@tonic-gate 	buffer_init(&buf);
1297c478bd9Sstevel@tonic-gate 	for (p = gss_enc2oid; p && *p; p++) {
1307c478bd9Sstevel@tonic-gate 		buffer_put_char(&buf, ',');
1317c478bd9Sstevel@tonic-gate 		buffer_append(&buf, (*p)->encoded, strlen((*p)->encoded));
1327c478bd9Sstevel@tonic-gate 	}
1337c478bd9Sstevel@tonic-gate 
1347c478bd9Sstevel@tonic-gate 	if (buffer_len(&buf) == 0) {
1357c478bd9Sstevel@tonic-gate 		buffer_free(&buf);
1367c478bd9Sstevel@tonic-gate 		return;
1377c478bd9Sstevel@tonic-gate 	}
1387c478bd9Sstevel@tonic-gate 
1397c478bd9Sstevel@tonic-gate 	buffer_consume(&buf, 1); /* consume leading ',' */
1407c478bd9Sstevel@tonic-gate 	buffer_put_char(&buf, '\0');
1417c478bd9Sstevel@tonic-gate 
1427c478bd9Sstevel@tonic-gate 	*kexname_list = xstrdup(buffer_ptr(&buf));
1437c478bd9Sstevel@tonic-gate 	buffer_free(&buf);
1447c478bd9Sstevel@tonic-gate }
1457c478bd9Sstevel@tonic-gate 
1467c478bd9Sstevel@tonic-gate void
ssh_gssapi_mech_oid_to_kexname(const gss_OID mech,char ** kexname)1477c478bd9Sstevel@tonic-gate ssh_gssapi_mech_oid_to_kexname(const gss_OID mech, char **kexname)
1487c478bd9Sstevel@tonic-gate {
1497c478bd9Sstevel@tonic-gate 	ssh_gss_kex_mapping **p;
1507c478bd9Sstevel@tonic-gate 
1517c478bd9Sstevel@tonic-gate 	if (mech == GSS_C_NULL_OID || !kexname)
1527c478bd9Sstevel@tonic-gate 		return;
1537c478bd9Sstevel@tonic-gate 
1547c478bd9Sstevel@tonic-gate 	*kexname = NULL; /* default to not found */
1557c478bd9Sstevel@tonic-gate 	if (gss_enc2oid) {
1567c478bd9Sstevel@tonic-gate 		for (p = gss_enc2oid; p && *p; p++) {
1577c478bd9Sstevel@tonic-gate 			if (mech->length == (*p)->oid->length &&
1587c478bd9Sstevel@tonic-gate 			    memcmp(mech->elements, (*p)->oid->elements,
1597c478bd9Sstevel@tonic-gate 			    mech->length) == 0)
1607c478bd9Sstevel@tonic-gate 				*kexname = xstrdup((*p)->encoded);
1617c478bd9Sstevel@tonic-gate 		}
1627c478bd9Sstevel@tonic-gate 	}
1637c478bd9Sstevel@tonic-gate 
1647c478bd9Sstevel@tonic-gate 	if (*kexname)
1657c478bd9Sstevel@tonic-gate 		return; /* found */
1667c478bd9Sstevel@tonic-gate 
1677c478bd9Sstevel@tonic-gate 	ssh_gssapi_encode_oid_for_kex(mech, kexname);
1687c478bd9Sstevel@tonic-gate }
1697c478bd9Sstevel@tonic-gate 
1707c478bd9Sstevel@tonic-gate void
ssh_gssapi_oid_of_kexname(const char * kexname,gss_OID * mech)1717c478bd9Sstevel@tonic-gate ssh_gssapi_oid_of_kexname(const char *kexname, gss_OID *mech)
1727c478bd9Sstevel@tonic-gate {
1737c478bd9Sstevel@tonic-gate 	ssh_gss_kex_mapping **p;
1747c478bd9Sstevel@tonic-gate 
1757c478bd9Sstevel@tonic-gate 	if (!mech || !kexname || !*kexname)
1767c478bd9Sstevel@tonic-gate 		return;
1777c478bd9Sstevel@tonic-gate 
1787c478bd9Sstevel@tonic-gate 	*mech = GSS_C_NULL_OID; /* default to not found */
1797c478bd9Sstevel@tonic-gate 
1807c478bd9Sstevel@tonic-gate 	if (!gss_enc2oid)
1817c478bd9Sstevel@tonic-gate 		return;
1827c478bd9Sstevel@tonic-gate 
1837c478bd9Sstevel@tonic-gate 	for (p = gss_enc2oid; p && *p; p++) {
1847c478bd9Sstevel@tonic-gate 		if (strcmp(kexname, (*p)->encoded) == 0) {
1857c478bd9Sstevel@tonic-gate 			*mech = (*p)->oid;
1867c478bd9Sstevel@tonic-gate 			return;
1877c478bd9Sstevel@tonic-gate 		}
1887c478bd9Sstevel@tonic-gate 	}
1897c478bd9Sstevel@tonic-gate }
1907c478bd9Sstevel@tonic-gate 
1917c478bd9Sstevel@tonic-gate static
1927c478bd9Sstevel@tonic-gate void
ssh_gssapi_encode_oid_for_kex(const gss_OID oid,char ** enc_name)1937c478bd9Sstevel@tonic-gate ssh_gssapi_encode_oid_for_kex(const gss_OID oid, char **enc_name)
1947c478bd9Sstevel@tonic-gate {
1957c478bd9Sstevel@tonic-gate 	Buffer buf;
1967c478bd9Sstevel@tonic-gate 	OM_uint32 oidlen;
197c15e4e4bSjp161948 	uint_t enclen;
1987c478bd9Sstevel@tonic-gate 	const EVP_MD *evp_md = EVP_md5();
1997c478bd9Sstevel@tonic-gate 	EVP_MD_CTX md;
200c15e4e4bSjp161948 	uchar_t digest[EVP_MAX_MD_SIZE];
2017c478bd9Sstevel@tonic-gate 	char *encoded;
2027c478bd9Sstevel@tonic-gate 
2037c478bd9Sstevel@tonic-gate 	if (oid == GSS_C_NULL_OID || !enc_name)
2047c478bd9Sstevel@tonic-gate 		return;
2057c478bd9Sstevel@tonic-gate 
2067c478bd9Sstevel@tonic-gate 	*enc_name = NULL;
2077c478bd9Sstevel@tonic-gate 
2087c478bd9Sstevel@tonic-gate 	oidlen = oid->length;
2097c478bd9Sstevel@tonic-gate 
2107c478bd9Sstevel@tonic-gate 	/* No GSS mechs have OIDs as long as 128 -- simplify DER encoding */
2117c478bd9Sstevel@tonic-gate 	if (oidlen > 128)
2127c478bd9Sstevel@tonic-gate 		return; /* fail gracefully */
2137c478bd9Sstevel@tonic-gate 
2147c478bd9Sstevel@tonic-gate 	/*
2157c478bd9Sstevel@tonic-gate 	 * NOTE:  If we need to support SSH_BUG_GSSAPI_BER this is where
2167c478bd9Sstevel@tonic-gate 	 * we'd do it.
2177c478bd9Sstevel@tonic-gate 	 *
2187c478bd9Sstevel@tonic-gate 	 * That means using "Se3H81ismmOC3OE+FwYCiQ==" for the Kerberos
2197c478bd9Sstevel@tonic-gate 	 * V mech and "N3+k7/4wGxHyuP8Yxi4RhA==" for the GSI mech.  Ick.
2207c478bd9Sstevel@tonic-gate 	 */
2217c478bd9Sstevel@tonic-gate 
2227c478bd9Sstevel@tonic-gate 	buffer_init(&buf);
2237c478bd9Sstevel@tonic-gate 
2247c478bd9Sstevel@tonic-gate 	/* UNIVERSAL class tag for OBJECT IDENTIFIER */
2257c478bd9Sstevel@tonic-gate 	buffer_put_char(&buf, 0x06);
2267c478bd9Sstevel@tonic-gate 	buffer_put_char(&buf, oidlen); /* one octet DER length -- see above */
2277c478bd9Sstevel@tonic-gate 
2287c478bd9Sstevel@tonic-gate 	/* OID elements */
2297c478bd9Sstevel@tonic-gate 	buffer_append(&buf, oid->elements, oidlen);
2307c478bd9Sstevel@tonic-gate 
2317c478bd9Sstevel@tonic-gate 	/* Make digest */
2327c478bd9Sstevel@tonic-gate 	EVP_DigestInit(&md, evp_md);
2337c478bd9Sstevel@tonic-gate 	EVP_DigestUpdate(&md, buffer_ptr(&buf), buffer_len(&buf));
2347c478bd9Sstevel@tonic-gate 	EVP_DigestFinal(&md, digest, NULL);
2357c478bd9Sstevel@tonic-gate 	buffer_free(&buf);
2367c478bd9Sstevel@tonic-gate 
2377c478bd9Sstevel@tonic-gate 	/* Base 64 encoding */
2387c478bd9Sstevel@tonic-gate 	encoded = xmalloc(EVP_MD_size(evp_md)*2);
2397c478bd9Sstevel@tonic-gate 	enclen = __b64_ntop(digest, EVP_MD_size(evp_md),
2407c478bd9Sstevel@tonic-gate 	    encoded, EVP_MD_size(evp_md) * 2);
2417c478bd9Sstevel@tonic-gate 	buffer_init(&buf);
2427c478bd9Sstevel@tonic-gate 	buffer_append(&buf, KEX_GSS_SHA1, sizeof (KEX_GSS_SHA1) - 1);
2437c478bd9Sstevel@tonic-gate 	buffer_append(&buf, encoded, enclen);
2447c478bd9Sstevel@tonic-gate 	buffer_put_char(&buf, '\0');
2457c478bd9Sstevel@tonic-gate 
2467c478bd9Sstevel@tonic-gate 	debug2("GSS-API Mechanism encoded as %s", encoded);
247cd7d5fafSJan Pechanec 	xfree(encoded);
2487c478bd9Sstevel@tonic-gate 
2497c478bd9Sstevel@tonic-gate 	*enc_name = xstrdup(buffer_ptr(&buf));
2507c478bd9Sstevel@tonic-gate 	buffer_free(&buf);
2517c478bd9Sstevel@tonic-gate }
2527c478bd9Sstevel@tonic-gate 
253c15e4e4bSjp161948 static char *
ssh_gssapi_make_kexalgs_list(gss_OID_set mechs,const char * old_kexalgs)2547c478bd9Sstevel@tonic-gate ssh_gssapi_make_kexalgs_list(gss_OID_set mechs, const char *old_kexalgs)
2557c478bd9Sstevel@tonic-gate {
2567c478bd9Sstevel@tonic-gate 	char *gss_kexalgs, *new_kexalgs;
2577c478bd9Sstevel@tonic-gate 	int len;
2587c478bd9Sstevel@tonic-gate 
2597c478bd9Sstevel@tonic-gate 	if (mechs == GSS_C_NULL_OID_SET)
2607c478bd9Sstevel@tonic-gate 		return (xstrdup(old_kexalgs)); /* never null */
2617c478bd9Sstevel@tonic-gate 
2627c478bd9Sstevel@tonic-gate 	ssh_gssapi_mech_oids_to_kexnames(mechs, &gss_kexalgs);
2637c478bd9Sstevel@tonic-gate 
2647c478bd9Sstevel@tonic-gate 	if (gss_kexalgs == NULL || *gss_kexalgs == '\0')
2657c478bd9Sstevel@tonic-gate 		return (xstrdup(old_kexalgs)); /* never null */
2667c478bd9Sstevel@tonic-gate 
2677c478bd9Sstevel@tonic-gate 	if (old_kexalgs == NULL || *old_kexalgs == '\0')
2687c478bd9Sstevel@tonic-gate 		return (gss_kexalgs);
2697c478bd9Sstevel@tonic-gate 
2707c478bd9Sstevel@tonic-gate 	len = strlen(old_kexalgs) + strlen(gss_kexalgs) + 2;
2717c478bd9Sstevel@tonic-gate 	new_kexalgs = xmalloc(len);
2727c478bd9Sstevel@tonic-gate 	(void) snprintf(new_kexalgs, len, "%s,%s", gss_kexalgs, old_kexalgs);
273cd7d5fafSJan Pechanec 	xfree(gss_kexalgs);
2747c478bd9Sstevel@tonic-gate 
2757c478bd9Sstevel@tonic-gate 	return (new_kexalgs);
2767c478bd9Sstevel@tonic-gate }
2777c478bd9Sstevel@tonic-gate 
2787c478bd9Sstevel@tonic-gate void
ssh_gssapi_modify_kex(Kex * kex,gss_OID_set mechs,char ** proposal)2797c478bd9Sstevel@tonic-gate ssh_gssapi_modify_kex(Kex *kex, gss_OID_set mechs, char **proposal)
2807c478bd9Sstevel@tonic-gate {
2817c478bd9Sstevel@tonic-gate 	char *kexalgs, *orig_kexalgs, *p;
2827c478bd9Sstevel@tonic-gate 	char **hostalg, *orig_hostalgs, *new_hostalgs;
2837c478bd9Sstevel@tonic-gate 	char **hostalgs;
2847c478bd9Sstevel@tonic-gate 	gss_OID_set dup_mechs;
2857c478bd9Sstevel@tonic-gate 	OM_uint32 maj, min;
2867c478bd9Sstevel@tonic-gate 	int i;
2877c478bd9Sstevel@tonic-gate 
2887c478bd9Sstevel@tonic-gate 	if (kex == NULL || proposal == NULL ||
289cd7d5fafSJan Pechanec 	    proposal[PROPOSAL_KEX_ALGS] == NULL) {
2907c478bd9Sstevel@tonic-gate 		fatal("INTERNAL ERROR (%s)", __func__);
2917c478bd9Sstevel@tonic-gate 	}
2927c478bd9Sstevel@tonic-gate 
2937c478bd9Sstevel@tonic-gate 	orig_hostalgs = proposal[PROPOSAL_SERVER_HOST_KEY_ALGS];
2947c478bd9Sstevel@tonic-gate 
2957c478bd9Sstevel@tonic-gate 	if (kex->mechs == GSS_C_NULL_OID_SET && mechs == GSS_C_NULL_OID_SET)
2967c478bd9Sstevel@tonic-gate 		return; /* didn't offer GSS last time, not offering now */
2977c478bd9Sstevel@tonic-gate 
2987c478bd9Sstevel@tonic-gate 	if (kex->mechs == GSS_C_NULL_OID_SET || mechs == GSS_C_NULL_OID_SET)
2997c478bd9Sstevel@tonic-gate 		goto mod_offer; /* didn't offer last time or not offering now */
3007c478bd9Sstevel@tonic-gate 
3017c478bd9Sstevel@tonic-gate 	/* Check if mechs is congruent to kex->mechs (last offered) */
3027c478bd9Sstevel@tonic-gate 	if (kex->mechs->count == mechs->count) {
3037c478bd9Sstevel@tonic-gate 		int present, matches = 0;
3047c478bd9Sstevel@tonic-gate 
3057c478bd9Sstevel@tonic-gate 		for (i = 0; i < mechs->count; i++) {
3067c478bd9Sstevel@tonic-gate 			maj = gss_test_oid_set_member(&min,
307c15e4e4bSjp161948 			    &kex->mechs->elements[i], mechs, &present);
3087c478bd9Sstevel@tonic-gate 
3097c478bd9Sstevel@tonic-gate 			if (GSS_ERROR(maj)) {
3107c478bd9Sstevel@tonic-gate 				mechs = GSS_C_NULL_OID_SET;
3117c478bd9Sstevel@tonic-gate 				break;
3127c478bd9Sstevel@tonic-gate 			}
3137c478bd9Sstevel@tonic-gate 
3147c478bd9Sstevel@tonic-gate 			matches += (present) ? 1 : 0;
3157c478bd9Sstevel@tonic-gate 		}
3167c478bd9Sstevel@tonic-gate 
3177c478bd9Sstevel@tonic-gate 		if (matches == kex->mechs->count)
3187c478bd9Sstevel@tonic-gate 			return; /* no change in offer from last time */
3197c478bd9Sstevel@tonic-gate 	}
3207c478bd9Sstevel@tonic-gate 
3217c478bd9Sstevel@tonic-gate mod_offer:
3227c478bd9Sstevel@tonic-gate 	/*
3237c478bd9Sstevel@tonic-gate 	 * Remove previously offered mechs from PROPOSAL_KEX_ALGS proposal
3247c478bd9Sstevel@tonic-gate 	 *
3257c478bd9Sstevel@tonic-gate 	 * ASSUMPTION: GSS-API kex algs always go in front, so removing
3267c478bd9Sstevel@tonic-gate 	 * them is a matter of skipping them.
3277c478bd9Sstevel@tonic-gate 	 */
3287c478bd9Sstevel@tonic-gate 	p = kexalgs = orig_kexalgs = proposal[PROPOSAL_KEX_ALGS];
3297c478bd9Sstevel@tonic-gate 	while (p != NULL && *p != '\0' &&
3307c478bd9Sstevel@tonic-gate 	    strncmp(p, KEX_GSS_SHA1, strlen(KEX_GSS_SHA1)) == 0) {
3317c478bd9Sstevel@tonic-gate 
3327c478bd9Sstevel@tonic-gate 		if ((p = strchr(p, ',')) == NULL)
3337c478bd9Sstevel@tonic-gate 			break;
3347c478bd9Sstevel@tonic-gate 		p++;
3357c478bd9Sstevel@tonic-gate 		kexalgs = p;
3367c478bd9Sstevel@tonic-gate 
3377c478bd9Sstevel@tonic-gate 	}
3387c478bd9Sstevel@tonic-gate 	kexalgs = proposal[PROPOSAL_KEX_ALGS] = xstrdup(kexalgs);
3397c478bd9Sstevel@tonic-gate 	xfree(orig_kexalgs);
3407c478bd9Sstevel@tonic-gate 
3417c478bd9Sstevel@tonic-gate 	(void) gss_release_oid_set(&min, &kex->mechs); /* ok if !kex->mechs */
3427c478bd9Sstevel@tonic-gate 
343cd7d5fafSJan Pechanec 	/* Not offering GSS kex algorithms now -> all done */
3447c478bd9Sstevel@tonic-gate 	if (mechs == GSS_C_NULL_OID_SET)
3457c478bd9Sstevel@tonic-gate 		return;
3467c478bd9Sstevel@tonic-gate 
3477c478bd9Sstevel@tonic-gate 	/* Remember mechs we're offering */
3487c478bd9Sstevel@tonic-gate 	maj = gss_create_empty_oid_set(&min, &dup_mechs);
3497c478bd9Sstevel@tonic-gate 	if (GSS_ERROR(maj))
3507c478bd9Sstevel@tonic-gate 		return;
3517c478bd9Sstevel@tonic-gate 	for (i = 0; i < mechs->count; i++) {
3527c478bd9Sstevel@tonic-gate 		maj = gss_add_oid_set_member(&min, &mechs->elements[i],
3537c478bd9Sstevel@tonic-gate 		    &dup_mechs);
3547c478bd9Sstevel@tonic-gate 
3557c478bd9Sstevel@tonic-gate 		if (GSS_ERROR(maj)) {
3567c478bd9Sstevel@tonic-gate 			(void) gss_release_oid_set(&min, &dup_mechs);
3577c478bd9Sstevel@tonic-gate 			return;
3587c478bd9Sstevel@tonic-gate 		}
3597c478bd9Sstevel@tonic-gate 	}
3607c478bd9Sstevel@tonic-gate 
361cd7d5fafSJan Pechanec 	/* Add mechs to kex algorithms ... */
362c15e4e4bSjp161948 	proposal[PROPOSAL_KEX_ALGS] = ssh_gssapi_make_kexalgs_list(mechs,
363c15e4e4bSjp161948 	    kexalgs);
364cd7d5fafSJan Pechanec 	xfree(kexalgs);
3657c478bd9Sstevel@tonic-gate 	kex->mechs = dup_mechs; /* remember what we offer now */
3667c478bd9Sstevel@tonic-gate 
3677c478bd9Sstevel@tonic-gate 	/*
3687c478bd9Sstevel@tonic-gate 	 * ... and add null host key alg, if it wasn't there before, but
3697c478bd9Sstevel@tonic-gate 	 * not if we're the server and we have other host key algs to
3707c478bd9Sstevel@tonic-gate 	 * offer.
3717c478bd9Sstevel@tonic-gate 	 *
3727c478bd9Sstevel@tonic-gate 	 * NOTE: Never remove "null" host key alg once added.
3737c478bd9Sstevel@tonic-gate 	 */
3747c478bd9Sstevel@tonic-gate 	if (orig_hostalgs == NULL || *orig_hostalgs == '\0') {
3757c478bd9Sstevel@tonic-gate 		proposal[PROPOSAL_SERVER_HOST_KEY_ALGS] = xstrdup("null");
3767c478bd9Sstevel@tonic-gate 	} else if (!kex->server) {
3777c478bd9Sstevel@tonic-gate 		hostalgs = xsplit(orig_hostalgs, ',');
3787c478bd9Sstevel@tonic-gate 		for (hostalg = hostalgs; *hostalg != NULL; hostalg++) {
3797c478bd9Sstevel@tonic-gate 			if (strcmp(*hostalg, "null") == 0) {
3807c478bd9Sstevel@tonic-gate 				xfree_split_list(hostalgs);
3817c478bd9Sstevel@tonic-gate 				return;
3827c478bd9Sstevel@tonic-gate 			}
3837c478bd9Sstevel@tonic-gate 		}
3847c478bd9Sstevel@tonic-gate 		xfree_split_list(hostalgs);
3857c478bd9Sstevel@tonic-gate 
3867c478bd9Sstevel@tonic-gate 		if (kex->mechs != GSS_C_NULL_OID_SET) {
3877c478bd9Sstevel@tonic-gate 			int len;
3887c478bd9Sstevel@tonic-gate 
3897c478bd9Sstevel@tonic-gate 			len = strlen(orig_hostalgs) + sizeof (",null");
3907c478bd9Sstevel@tonic-gate 			new_hostalgs = xmalloc(len);
3917c478bd9Sstevel@tonic-gate 			(void) snprintf(new_hostalgs, len, "%s,null",
3927c478bd9Sstevel@tonic-gate 			    orig_hostalgs);
3937c478bd9Sstevel@tonic-gate 			proposal[PROPOSAL_SERVER_HOST_KEY_ALGS] = new_hostalgs;
3947c478bd9Sstevel@tonic-gate 		}
3957c478bd9Sstevel@tonic-gate 
3967c478bd9Sstevel@tonic-gate 		xfree(orig_hostalgs);
3977c478bd9Sstevel@tonic-gate 	}
3987c478bd9Sstevel@tonic-gate }
3997c478bd9Sstevel@tonic-gate 
4007c478bd9Sstevel@tonic-gate /*
4017c478bd9Sstevel@tonic-gate  * Yes, we harcode OIDs for some things, for now it's all we can do.
4027c478bd9Sstevel@tonic-gate  *
4037c478bd9Sstevel@tonic-gate  * We have to reference particular mechanisms due to lack of generality
4047c478bd9Sstevel@tonic-gate  * in the GSS-API in several areas: authorization, mapping principal
4057c478bd9Sstevel@tonic-gate  * names to usernames, "storing" delegated credentials, and discovering
4067c478bd9Sstevel@tonic-gate  * whether a mechanism is a pseudo-mechanism that negotiates mechanisms.
4077c478bd9Sstevel@tonic-gate  *
4087c478bd9Sstevel@tonic-gate  * Even if they were in some header file or if __gss_mech_to_oid()
4097c478bd9Sstevel@tonic-gate  * and/or __gss_oid_to_mech() were standard we'd still have to hardcode
4107c478bd9Sstevel@tonic-gate  * the mechanism names, and since the mechanisms have no standard names
4117c478bd9Sstevel@tonic-gate  * other than their OIDs it's actually worse [less portable] to hardcode
4127c478bd9Sstevel@tonic-gate  * names than OIDs, so we hardcode OIDs.
4137c478bd9Sstevel@tonic-gate  *
4147c478bd9Sstevel@tonic-gate  * SPNEGO is a difficult problem though -- it MUST NOT be used in SSHv2,
4157c478bd9Sstevel@tonic-gate  * but that's true of all possible pseudo-mechanisms that can perform
4167c478bd9Sstevel@tonic-gate  * mechanism negotiation, and SPNEGO could have new OIDs in the future.
4177c478bd9Sstevel@tonic-gate  * Ideally we could query each mechanism for its feature set and then
4187c478bd9Sstevel@tonic-gate  * ignore any mechanisms that negotiate mechanisms, but, alas, there's
4197c478bd9Sstevel@tonic-gate  * no interface to do that.
4207c478bd9Sstevel@tonic-gate  *
4217c478bd9Sstevel@tonic-gate  * In the future, if the necessary generic GSS interfaces for the issues
4227c478bd9Sstevel@tonic-gate  * listed above are made available (even if they differ by platform, as
4237c478bd9Sstevel@tonic-gate  * we can expect authorization interfaces will), then we can stop
4247c478bd9Sstevel@tonic-gate  * referencing specific mechanism OIDs here.
4257c478bd9Sstevel@tonic-gate  */
4267c478bd9Sstevel@tonic-gate int
ssh_gssapi_is_spnego(gss_OID oid)4277c478bd9Sstevel@tonic-gate ssh_gssapi_is_spnego(gss_OID oid)
4287c478bd9Sstevel@tonic-gate {
4297c478bd9Sstevel@tonic-gate 	return (oid->length == 6 &&
430c15e4e4bSjp161948 	    memcmp("\053\006\001\005\005\002", oid->elements, 6) == 0);
4317c478bd9Sstevel@tonic-gate }
4327c478bd9Sstevel@tonic-gate 
4337c478bd9Sstevel@tonic-gate int
ssh_gssapi_is_krb5(gss_OID oid)4347c478bd9Sstevel@tonic-gate ssh_gssapi_is_krb5(gss_OID oid)
4357c478bd9Sstevel@tonic-gate {
4367c478bd9Sstevel@tonic-gate 	return (oid->length == 9 &&
4377c478bd9Sstevel@tonic-gate 	    memcmp("\x2A\x86\x48\x86\xF7\x12\x01\x02\x02",
4387c478bd9Sstevel@tonic-gate 	    oid->elements, 9) == 0);
4397c478bd9Sstevel@tonic-gate }
4407c478bd9Sstevel@tonic-gate 
4417c478bd9Sstevel@tonic-gate int
ssh_gssapi_is_dh(gss_OID oid)4427c478bd9Sstevel@tonic-gate ssh_gssapi_is_dh(gss_OID oid)
4437c478bd9Sstevel@tonic-gate {
4447c478bd9Sstevel@tonic-gate 	return (oid->length == 9 &&
4457c478bd9Sstevel@tonic-gate 	    memcmp("\053\006\004\001\052\002\032\002\005",
4467c478bd9Sstevel@tonic-gate 	    oid->elements, 9) == 0);
4477c478bd9Sstevel@tonic-gate }
4487c478bd9Sstevel@tonic-gate 
4497c478bd9Sstevel@tonic-gate int
ssh_gssapi_is_gsi(gss_OID oid)4507c478bd9Sstevel@tonic-gate ssh_gssapi_is_gsi(gss_OID oid)
4517c478bd9Sstevel@tonic-gate {
4527c478bd9Sstevel@tonic-gate 	return (oid->length == 9 &&
4537c478bd9Sstevel@tonic-gate 	    memcmp("\x2B\x06\x01\x04\x01\x9B\x50\x01\x01",
4547c478bd9Sstevel@tonic-gate 	    oid->elements, 9) == 0);
4557c478bd9Sstevel@tonic-gate }
4567c478bd9Sstevel@tonic-gate 
457c15e4e4bSjp161948 const char *
ssh_gssapi_oid_to_name(gss_OID oid)4587c478bd9Sstevel@tonic-gate ssh_gssapi_oid_to_name(gss_OID oid)
4597c478bd9Sstevel@tonic-gate {
4607c478bd9Sstevel@tonic-gate #ifdef HAVE_GSS_OID_TO_MECH
461c15e4e4bSjp161948 	return (__gss_oid_to_mech(oid));
4627c478bd9Sstevel@tonic-gate #else
4637c478bd9Sstevel@tonic-gate 	if (ssh_gssapi_is_krb5(oid))
464c15e4e4bSjp161948 		return ("Kerberos");
4657c478bd9Sstevel@tonic-gate 	if (ssh_gssapi_is_gsi(oid))
466c15e4e4bSjp161948 		return ("GSI");
467c15e4e4bSjp161948 	return ("(unknown)");
4687c478bd9Sstevel@tonic-gate #endif /* HAVE_GSS_OID_TO_MECH */
4697c478bd9Sstevel@tonic-gate }
4707c478bd9Sstevel@tonic-gate 
4717c478bd9Sstevel@tonic-gate char *
ssh_gssapi_oid_to_str(gss_OID oid)4727c478bd9Sstevel@tonic-gate ssh_gssapi_oid_to_str(gss_OID oid)
4737c478bd9Sstevel@tonic-gate {
4747c478bd9Sstevel@tonic-gate #ifdef HAVE_GSS_OID_TO_STR
4757c478bd9Sstevel@tonic-gate 	gss_buffer_desc	str_buf;
4767c478bd9Sstevel@tonic-gate 	char		*str;
4777c478bd9Sstevel@tonic-gate 	OM_uint32	maj, min;
4787c478bd9Sstevel@tonic-gate 
4797c478bd9Sstevel@tonic-gate 	maj = gss_oid_to_str(&min, oid, &str_buf);
4807c478bd9Sstevel@tonic-gate 
4817c478bd9Sstevel@tonic-gate 	if (GSS_ERROR(maj))
482c15e4e4bSjp161948 		return (xstrdup("<gss_oid_to_str() failed>"));
4837c478bd9Sstevel@tonic-gate 
4847c478bd9Sstevel@tonic-gate 	str = xmalloc(str_buf.length + 1);
4857c478bd9Sstevel@tonic-gate 	memset(str, 0, str_buf.length + 1);
4867c478bd9Sstevel@tonic-gate 	strlcpy(str, str_buf.value, str_buf.length + 1);
4877c478bd9Sstevel@tonic-gate 	(void) gss_release_buffer(&min, &str_buf);
4887c478bd9Sstevel@tonic-gate 
489c15e4e4bSjp161948 	return (str);
4907c478bd9Sstevel@tonic-gate #else
491c15e4e4bSjp161948 	return (xstrdup("<gss_oid_to_str() unsupported>"));
4927c478bd9Sstevel@tonic-gate #endif /* HAVE_GSS_OID_TO_STR */
4937c478bd9Sstevel@tonic-gate }
4947c478bd9Sstevel@tonic-gate 
4957c478bd9Sstevel@tonic-gate /* Check that the OID in a data stream matches that in the context */
496c15e4e4bSjp161948 int
ssh_gssapi_check_mech_oid(Gssctxt * ctx,void * data,size_t len)497c15e4e4bSjp161948 ssh_gssapi_check_mech_oid(Gssctxt *ctx, void *data, size_t len)
498c15e4e4bSjp161948 {
4997c478bd9Sstevel@tonic-gate 
5007c478bd9Sstevel@tonic-gate 	return (ctx != NULL && ctx->desired_mech != GSS_C_NULL_OID &&
5017c478bd9Sstevel@tonic-gate 	    ctx->desired_mech->length == len &&
5027c478bd9Sstevel@tonic-gate 	    memcmp(ctx->desired_mech->elements, data, len) == 0);
5037c478bd9Sstevel@tonic-gate }
5047c478bd9Sstevel@tonic-gate 
5057c478bd9Sstevel@tonic-gate /* Set the contexts OID from a data stream */
506c15e4e4bSjp161948 void
ssh_gssapi_set_oid_data(Gssctxt * ctx,void * data,size_t len)507c15e4e4bSjp161948 ssh_gssapi_set_oid_data(Gssctxt *ctx, void *data, size_t len)
508c15e4e4bSjp161948 {
5097c478bd9Sstevel@tonic-gate 	if (ctx->actual_mech != GSS_C_NULL_OID) {
5107c478bd9Sstevel@tonic-gate 		xfree(ctx->actual_mech->elements);
5117c478bd9Sstevel@tonic-gate 		xfree(ctx->actual_mech);
5127c478bd9Sstevel@tonic-gate 	}
5137c478bd9Sstevel@tonic-gate 	ctx->actual_mech = xmalloc(sizeof (gss_OID_desc));
5147c478bd9Sstevel@tonic-gate 	ctx->actual_mech->length = len;
5157c478bd9Sstevel@tonic-gate 	ctx->actual_mech->elements = xmalloc(len);
5167c478bd9Sstevel@tonic-gate 	memcpy(ctx->actual_mech->elements, data, len);
5177c478bd9Sstevel@tonic-gate }
5187c478bd9Sstevel@tonic-gate 
5197c478bd9Sstevel@tonic-gate /* Set the contexts OID */
520c15e4e4bSjp161948 void
ssh_gssapi_set_oid(Gssctxt * ctx,gss_OID oid)521c15e4e4bSjp161948 ssh_gssapi_set_oid(Gssctxt *ctx, gss_OID oid)
522c15e4e4bSjp161948 {
5237c478bd9Sstevel@tonic-gate 	ssh_gssapi_set_oid_data(ctx, oid->elements, oid->length);
5247c478bd9Sstevel@tonic-gate }
5257c478bd9Sstevel@tonic-gate 
5267c478bd9Sstevel@tonic-gate /* All this effort to report an error ... */
5277c478bd9Sstevel@tonic-gate 
5287c478bd9Sstevel@tonic-gate void
ssh_gssapi_error(Gssctxt * ctxt,const char * where)5299a8058b5Sjp161948 ssh_gssapi_error(Gssctxt *ctxt, const char *where)
5309a8058b5Sjp161948 {
5319a8058b5Sjp161948 	char *errmsg = ssh_gssapi_last_error(ctxt, NULL, NULL);
5329a8058b5Sjp161948 
5339a8058b5Sjp161948 	if (where != NULL)
5349a8058b5Sjp161948 		debug("GSS-API error while %s: %s", where, errmsg);
5357c478bd9Sstevel@tonic-gate 	else
5369a8058b5Sjp161948 		debug("GSS-API error: %s", errmsg);
5379a8058b5Sjp161948 
5389a8058b5Sjp161948 	/* ssh_gssapi_last_error() can't return NULL */
5399a8058b5Sjp161948 	xfree(errmsg);
5407c478bd9Sstevel@tonic-gate }
5417c478bd9Sstevel@tonic-gate 
5427c478bd9Sstevel@tonic-gate char *
ssh_gssapi_last_error(Gssctxt * ctxt,OM_uint32 * major_status,OM_uint32 * minor_status)543c15e4e4bSjp161948 ssh_gssapi_last_error(Gssctxt *ctxt, OM_uint32 *major_status,
544c15e4e4bSjp161948     OM_uint32 *minor_status)
545c15e4e4bSjp161948 {
5467c478bd9Sstevel@tonic-gate 	OM_uint32 lmin, more;
5477c478bd9Sstevel@tonic-gate 	OM_uint32 maj, min;
5487c478bd9Sstevel@tonic-gate 	gss_OID mech = GSS_C_NULL_OID;
5497c478bd9Sstevel@tonic-gate 	gss_buffer_desc msg;
5507c478bd9Sstevel@tonic-gate 	Buffer b;
5517c478bd9Sstevel@tonic-gate 	char *ret;
5527c478bd9Sstevel@tonic-gate 
5537c478bd9Sstevel@tonic-gate 	buffer_init(&b);
5547c478bd9Sstevel@tonic-gate 
5557c478bd9Sstevel@tonic-gate 	if (ctxt) {
5567c478bd9Sstevel@tonic-gate 		/* Get status codes from the Gssctxt */
5577c478bd9Sstevel@tonic-gate 		maj = ctxt->major;
5587c478bd9Sstevel@tonic-gate 		min = ctxt->minor;
5597c478bd9Sstevel@tonic-gate 		/* Output them if desired */
5607c478bd9Sstevel@tonic-gate 		if (major_status)
5617c478bd9Sstevel@tonic-gate 			*major_status = maj;
5627c478bd9Sstevel@tonic-gate 		if (minor_status)
5637c478bd9Sstevel@tonic-gate 			*minor_status = min;
5647c478bd9Sstevel@tonic-gate 		/* Get mechanism for minor status display */
5657c478bd9Sstevel@tonic-gate 		mech = (ctxt->actual_mech != GSS_C_NULL_OID) ?
5667c478bd9Sstevel@tonic-gate 		    ctxt->actual_mech : ctxt->desired_mech;
5677c478bd9Sstevel@tonic-gate 	} else if (major_status && minor_status) {
5687c478bd9Sstevel@tonic-gate 		maj = *major_status;
5697c478bd9Sstevel@tonic-gate 		min = *major_status;
5707c478bd9Sstevel@tonic-gate 	} else {
5717c478bd9Sstevel@tonic-gate 		maj = GSS_S_COMPLETE;
5727c478bd9Sstevel@tonic-gate 		min = 0;
5737c478bd9Sstevel@tonic-gate 	}
5747c478bd9Sstevel@tonic-gate 
5757c478bd9Sstevel@tonic-gate 	more = 0;
5767c478bd9Sstevel@tonic-gate 	/* The GSSAPI error */
5777c478bd9Sstevel@tonic-gate 	do {
578c15e4e4bSjp161948 		gss_display_status(&lmin, maj, GSS_C_GSS_CODE,
579c15e4e4bSjp161948 		    GSS_C_NULL_OID, &more, &msg);
5807c478bd9Sstevel@tonic-gate 
5817c478bd9Sstevel@tonic-gate 		buffer_append(&b, msg.value, msg.length);
5827c478bd9Sstevel@tonic-gate 		buffer_put_char(&b, '\n');
5837c478bd9Sstevel@tonic-gate 		gss_release_buffer(&lmin, &msg);
5847c478bd9Sstevel@tonic-gate 	} while (more != 0);
5857c478bd9Sstevel@tonic-gate 
5867c478bd9Sstevel@tonic-gate 	/* The mechanism specific error */
5877c478bd9Sstevel@tonic-gate 	do {
5887c478bd9Sstevel@tonic-gate 		/*
5897c478bd9Sstevel@tonic-gate 		 * If mech == GSS_C_NULL_OID we may get the default
5907c478bd9Sstevel@tonic-gate 		 * mechanism, whatever that is, and that may not be
5917c478bd9Sstevel@tonic-gate 		 * useful.
5927c478bd9Sstevel@tonic-gate 		 */
593c15e4e4bSjp161948 		gss_display_status(&lmin, min, GSS_C_MECH_CODE, mech, &more,
594c15e4e4bSjp161948 		    &msg);
5957c478bd9Sstevel@tonic-gate 
5967c478bd9Sstevel@tonic-gate 		buffer_append(&b, msg.value, msg.length);
5977c478bd9Sstevel@tonic-gate 		buffer_put_char(&b, '\n');
5987c478bd9Sstevel@tonic-gate 
5997c478bd9Sstevel@tonic-gate 		gss_release_buffer(&lmin, &msg);
6007c478bd9Sstevel@tonic-gate 	} while (more != 0);
6017c478bd9Sstevel@tonic-gate 
6027c478bd9Sstevel@tonic-gate 	buffer_put_char(&b, '\0');
6037c478bd9Sstevel@tonic-gate 	ret = xstrdup(buffer_ptr(&b));
6047c478bd9Sstevel@tonic-gate 	buffer_free(&b);
6057c478bd9Sstevel@tonic-gate 
6067c478bd9Sstevel@tonic-gate 	return (ret);
6077c478bd9Sstevel@tonic-gate }
6087c478bd9Sstevel@tonic-gate 
609c15e4e4bSjp161948 /*
610c15e4e4bSjp161948  * Initialise our GSSAPI context. We use this opaque structure to contain all
6117c478bd9Sstevel@tonic-gate  * of the data which both the client and server need to persist across
6127c478bd9Sstevel@tonic-gate  * {accept,init}_sec_context calls, so that when we do it from the userauth
6137c478bd9Sstevel@tonic-gate  * stuff life is a little easier
6147c478bd9Sstevel@tonic-gate  */
6157c478bd9Sstevel@tonic-gate void
ssh_gssapi_build_ctx(Gssctxt ** ctx,int client,gss_OID mech)6167c478bd9Sstevel@tonic-gate ssh_gssapi_build_ctx(Gssctxt **ctx, int client, gss_OID mech)
6177c478bd9Sstevel@tonic-gate {
6187c478bd9Sstevel@tonic-gate 	Gssctxt *newctx;
6197c478bd9Sstevel@tonic-gate 
6207c478bd9Sstevel@tonic-gate 
6217c478bd9Sstevel@tonic-gate 	newctx = (Gssctxt*)xmalloc(sizeof (Gssctxt));
6227c478bd9Sstevel@tonic-gate 	memset(newctx, 0, sizeof (Gssctxt));
6237c478bd9Sstevel@tonic-gate 
62455837012Snw141292 
6257c478bd9Sstevel@tonic-gate 	newctx->local = client;
6267c478bd9Sstevel@tonic-gate 	newctx->desired_mech = ssh_gssapi_dup_oid(mech);
6277c478bd9Sstevel@tonic-gate 
6287c478bd9Sstevel@tonic-gate 	/* This happens to be redundant given the memset() above */
6297c478bd9Sstevel@tonic-gate 	newctx->major = GSS_S_COMPLETE;
6307c478bd9Sstevel@tonic-gate 	newctx->context = GSS_C_NO_CONTEXT;
6317c478bd9Sstevel@tonic-gate 	newctx->actual_mech =  GSS_C_NULL_OID;
6327c478bd9Sstevel@tonic-gate 	newctx->desired_name = GSS_C_NO_NAME;
6337c478bd9Sstevel@tonic-gate 	newctx->src_name = GSS_C_NO_NAME;
6347c478bd9Sstevel@tonic-gate 	newctx->dst_name = GSS_C_NO_NAME;
6357c478bd9Sstevel@tonic-gate 	newctx->creds = GSS_C_NO_CREDENTIAL;
6367c478bd9Sstevel@tonic-gate 	newctx->deleg_creds = GSS_C_NO_CREDENTIAL;
6377c478bd9Sstevel@tonic-gate 
63855837012Snw141292 	newctx->default_creds = (*ctx != NULL) ? (*ctx)->default_creds : 0;
63955837012Snw141292 
64055837012Snw141292 	ssh_gssapi_delete_ctx(ctx);
64155837012Snw141292 
6427c478bd9Sstevel@tonic-gate 	*ctx = newctx;
6437c478bd9Sstevel@tonic-gate }
6447c478bd9Sstevel@tonic-gate 
6457c478bd9Sstevel@tonic-gate gss_OID
ssh_gssapi_dup_oid(gss_OID oid)6467c478bd9Sstevel@tonic-gate ssh_gssapi_dup_oid(gss_OID oid)
6477c478bd9Sstevel@tonic-gate {
6487c478bd9Sstevel@tonic-gate 	gss_OID new_oid;
6497c478bd9Sstevel@tonic-gate 
6507c478bd9Sstevel@tonic-gate 	new_oid = xmalloc(sizeof (gss_OID_desc));
6517c478bd9Sstevel@tonic-gate 
6527c478bd9Sstevel@tonic-gate 	new_oid->elements = xmalloc(oid->length);
6537c478bd9Sstevel@tonic-gate 	new_oid->length = oid->length;
6547c478bd9Sstevel@tonic-gate 	memcpy(new_oid->elements, oid->elements, oid->length);
6557c478bd9Sstevel@tonic-gate 
6567c478bd9Sstevel@tonic-gate 	return (new_oid);
6577c478bd9Sstevel@tonic-gate }
6587c478bd9Sstevel@tonic-gate 
6597c478bd9Sstevel@tonic-gate gss_OID
ssh_gssapi_make_oid(size_t length,void * elements)6607c478bd9Sstevel@tonic-gate ssh_gssapi_make_oid(size_t length, void *elements)
6617c478bd9Sstevel@tonic-gate {
6627c478bd9Sstevel@tonic-gate 	gss_OID_desc oid;
6637c478bd9Sstevel@tonic-gate 
6647c478bd9Sstevel@tonic-gate 	oid.length = length;
6657c478bd9Sstevel@tonic-gate 	oid.elements = elements;
6667c478bd9Sstevel@tonic-gate 
6677c478bd9Sstevel@tonic-gate 	return (ssh_gssapi_dup_oid(&oid));
6687c478bd9Sstevel@tonic-gate }
6697c478bd9Sstevel@tonic-gate 
6707c478bd9Sstevel@tonic-gate void
ssh_gssapi_release_oid(gss_OID * oid)6717c478bd9Sstevel@tonic-gate ssh_gssapi_release_oid(gss_OID *oid)
6727c478bd9Sstevel@tonic-gate {
6737c478bd9Sstevel@tonic-gate 	OM_uint32 min;
6747c478bd9Sstevel@tonic-gate 
6757c478bd9Sstevel@tonic-gate 	if (oid && *oid == GSS_C_NULL_OID)
6767c478bd9Sstevel@tonic-gate 		return;
6777c478bd9Sstevel@tonic-gate 	(void) gss_release_oid(&min, oid);
6787c478bd9Sstevel@tonic-gate 
6797c478bd9Sstevel@tonic-gate 	if (*oid == GSS_C_NULL_OID)
6807c478bd9Sstevel@tonic-gate 		return; /* libgss did own this gss_OID and released it */
6817c478bd9Sstevel@tonic-gate 
6827c478bd9Sstevel@tonic-gate 	xfree((*oid)->elements);
6837c478bd9Sstevel@tonic-gate 	xfree(*oid);
6847c478bd9Sstevel@tonic-gate 	*oid = GSS_C_NULL_OID;
6857c478bd9Sstevel@tonic-gate }
6867c478bd9Sstevel@tonic-gate 
6877c478bd9Sstevel@tonic-gate struct gss_name {
6887c478bd9Sstevel@tonic-gate 	gss_OID		name_type;
6897c478bd9Sstevel@tonic-gate 	gss_buffer_t	external_name;
6907c478bd9Sstevel@tonic-gate 	gss_OID		mech_type;
6917c478bd9Sstevel@tonic-gate 	void		*mech_name;
6927c478bd9Sstevel@tonic-gate };
6937c478bd9Sstevel@tonic-gate 
6947c478bd9Sstevel@tonic-gate /* Delete our context, providing it has been built correctly */
6957c478bd9Sstevel@tonic-gate void
ssh_gssapi_delete_ctx(Gssctxt ** ctx)6967c478bd9Sstevel@tonic-gate ssh_gssapi_delete_ctx(Gssctxt **ctx)
6977c478bd9Sstevel@tonic-gate {
6987c478bd9Sstevel@tonic-gate 	OM_uint32 ms;
6997c478bd9Sstevel@tonic-gate 
7007c478bd9Sstevel@tonic-gate 	if ((*ctx) == NULL)
7017c478bd9Sstevel@tonic-gate 		return;
7027c478bd9Sstevel@tonic-gate 
7037c478bd9Sstevel@tonic-gate 	if ((*ctx)->context != GSS_C_NO_CONTEXT)
7047c478bd9Sstevel@tonic-gate 		gss_delete_sec_context(&ms, &(*ctx)->context, GSS_C_NO_BUFFER);
705c15e4e4bSjp161948 #if 0
706c15e4e4bSjp161948 	/* XXX */
707c15e4e4bSjp161948 	if ((*ctx)->desired_mech != GSS_C_NULL_OID)
708c15e4e4bSjp161948 		ssh_gssapi_release_oid(&(*ctx)->desired_mech);
709c15e4e4bSjp161948 #endif
7107c478bd9Sstevel@tonic-gate 	if ((*ctx)->actual_mech != GSS_C_NULL_OID)
7117c478bd9Sstevel@tonic-gate 		(void) ssh_gssapi_release_oid(&(*ctx)->actual_mech);
7127c478bd9Sstevel@tonic-gate 	if ((*ctx)->desired_name != GSS_C_NO_NAME)
7137c478bd9Sstevel@tonic-gate 		gss_release_name(&ms, &(*ctx)->desired_name);
714c15e4e4bSjp161948 #if 0
715c15e4e4bSjp161948 	if ((*ctx)->src_name != GSS_C_NO_NAME)
716c15e4e4bSjp161948 		gss_release_name(&ms, &(*ctx)->src_name);
717c15e4e4bSjp161948 #endif
7187c478bd9Sstevel@tonic-gate 	if ((*ctx)->dst_name != GSS_C_NO_NAME)
7197c478bd9Sstevel@tonic-gate 		gss_release_name(&ms, &(*ctx)->dst_name);
7207c478bd9Sstevel@tonic-gate 	if ((*ctx)->creds != GSS_C_NO_CREDENTIAL)
7217c478bd9Sstevel@tonic-gate 		gss_release_cred(&ms, &(*ctx)->creds);
7227c478bd9Sstevel@tonic-gate 	if ((*ctx)->deleg_creds != GSS_C_NO_CREDENTIAL)
7237c478bd9Sstevel@tonic-gate 		gss_release_cred(&ms, &(*ctx)->deleg_creds);
7247c478bd9Sstevel@tonic-gate 
7257c478bd9Sstevel@tonic-gate 	xfree(*ctx);
7267c478bd9Sstevel@tonic-gate 	*ctx = NULL;
7277c478bd9Sstevel@tonic-gate }
7287c478bd9Sstevel@tonic-gate 
7297c478bd9Sstevel@tonic-gate /* Create a GSS hostbased service principal name for a given server hostname */
7307c478bd9Sstevel@tonic-gate int
ssh_gssapi_import_name(Gssctxt * ctx,const char * server_host)7317c478bd9Sstevel@tonic-gate ssh_gssapi_import_name(Gssctxt *ctx, const char *server_host)
7327c478bd9Sstevel@tonic-gate {
7337c478bd9Sstevel@tonic-gate 	gss_buffer_desc name_buf;
7347c478bd9Sstevel@tonic-gate 	int		ret;
7357c478bd9Sstevel@tonic-gate 
7367c478bd9Sstevel@tonic-gate 	/* Build target principal */
7377c478bd9Sstevel@tonic-gate 	name_buf.length = strlen(SSH_GSS_HOSTBASED_SERVICE) +
7387c478bd9Sstevel@tonic-gate 	    strlen(server_host) + 1; /* +1 for '@' */
7397c478bd9Sstevel@tonic-gate 	name_buf.value = xmalloc(name_buf.length + 1); /* +1 for NUL */
7407c478bd9Sstevel@tonic-gate 	ret = snprintf(name_buf.value, name_buf.length + 1, "%s@%s",
7417c478bd9Sstevel@tonic-gate 	    SSH_GSS_HOSTBASED_SERVICE, server_host);
7427c478bd9Sstevel@tonic-gate 
743c15e4e4bSjp161948 	debug3("%s: snprintf() returned %d, expected %d", __func__, ret,
744*b9aa66a7SJan Pechanec 	    name_buf.length);
7457c478bd9Sstevel@tonic-gate 
7467c478bd9Sstevel@tonic-gate 	ctx->major = gss_import_name(&ctx->minor, &name_buf,
7477c478bd9Sstevel@tonic-gate 	    GSS_C_NT_HOSTBASED_SERVICE, &ctx->desired_name);
7487c478bd9Sstevel@tonic-gate 
7497c478bd9Sstevel@tonic-gate 	if (GSS_ERROR(ctx->major)) {
7507c478bd9Sstevel@tonic-gate 		ssh_gssapi_error(ctx, "calling GSS_Import_name()");
751c15e4e4bSjp161948 		return (0);
7527c478bd9Sstevel@tonic-gate 	}
7537c478bd9Sstevel@tonic-gate 
7547c478bd9Sstevel@tonic-gate 	xfree(name_buf.value);
7557c478bd9Sstevel@tonic-gate 
756c15e4e4bSjp161948 	return (1);
7577c478bd9Sstevel@tonic-gate }
7587c478bd9Sstevel@tonic-gate 
7597c478bd9Sstevel@tonic-gate OM_uint32
ssh_gssapi_get_mic(Gssctxt * ctx,gss_buffer_desc * buffer,gss_buffer_desc * hash)760c15e4e4bSjp161948 ssh_gssapi_get_mic(Gssctxt *ctx, gss_buffer_desc *buffer, gss_buffer_desc *hash)
761c15e4e4bSjp161948 {
7627c478bd9Sstevel@tonic-gate 
7637c478bd9Sstevel@tonic-gate 	ctx->major = gss_get_mic(&ctx->minor, ctx->context,
7647c478bd9Sstevel@tonic-gate 	    GSS_C_QOP_DEFAULT, buffer, hash);
7657c478bd9Sstevel@tonic-gate 	if (GSS_ERROR(ctx->major))
7667c478bd9Sstevel@tonic-gate 		ssh_gssapi_error(ctx, "while getting MIC");
7677c478bd9Sstevel@tonic-gate 	return (ctx->major);
7687c478bd9Sstevel@tonic-gate }
7697c478bd9Sstevel@tonic-gate 
7707c478bd9Sstevel@tonic-gate OM_uint32
ssh_gssapi_verify_mic(Gssctxt * ctx,gss_buffer_desc * buffer,gss_buffer_desc * hash)771c15e4e4bSjp161948 ssh_gssapi_verify_mic(Gssctxt *ctx, gss_buffer_desc *buffer,
772c15e4e4bSjp161948     gss_buffer_desc *hash)
773c15e4e4bSjp161948 {
7747c478bd9Sstevel@tonic-gate 	gss_qop_t qop;
7757c478bd9Sstevel@tonic-gate 
776c15e4e4bSjp161948 	ctx->major = gss_verify_mic(&ctx->minor, ctx->context, buffer,
777c15e4e4bSjp161948 	    hash, &qop);
7787c478bd9Sstevel@tonic-gate 	if (GSS_ERROR(ctx->major))
7797c478bd9Sstevel@tonic-gate 		ssh_gssapi_error(ctx, "while verifying MIC");
7807c478bd9Sstevel@tonic-gate 	return (ctx->major);
7817c478bd9Sstevel@tonic-gate }
7827c478bd9Sstevel@tonic-gate #endif /* GSSAPI */
783