xref: /netbsd-src/crypto/external/bsd/openssh/dist/gss-genr.c (revision c5555919c1ef14dca119ee5d4a4e93656523a56e)
1*c5555919Schristos /*	$NetBSD: gss-genr.c,v 1.12 2024/06/25 16:36:54 christos Exp $	*/
2*c5555919Schristos /* $OpenBSD: gss-genr.c,v 1.29 2024/02/01 02:37:33 djm Exp $ */
3ca32bd8dSchristos 
4ca32bd8dSchristos /*
5ca32bd8dSchristos  * Copyright (c) 2001-2007 Simon Wilkinson. All rights reserved.
6ca32bd8dSchristos  *
7ca32bd8dSchristos  * Redistribution and use in source and binary forms, with or without
8ca32bd8dSchristos  * modification, are permitted provided that the following conditions
9ca32bd8dSchristos  * are met:
10ca32bd8dSchristos  * 1. Redistributions of source code must retain the above copyright
11ca32bd8dSchristos  *    notice, this list of conditions and the following disclaimer.
12ca32bd8dSchristos  * 2. Redistributions in binary form must reproduce the above copyright
13ca32bd8dSchristos  *    notice, this list of conditions and the following disclaimer in the
14ca32bd8dSchristos  *    documentation and/or other materials provided with the distribution.
15ca32bd8dSchristos  *
16ca32bd8dSchristos  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR `AS IS'' AND ANY EXPRESS OR
17ca32bd8dSchristos  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18ca32bd8dSchristos  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19ca32bd8dSchristos  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20ca32bd8dSchristos  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21ca32bd8dSchristos  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22ca32bd8dSchristos  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23ca32bd8dSchristos  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24ca32bd8dSchristos  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25ca32bd8dSchristos  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26ca32bd8dSchristos  */
27ca32bd8dSchristos 
28313c6c94Schristos #include "includes.h"
29313c6c94Schristos __RCSID("$NetBSD");
30ca32bd8dSchristos #ifdef GSSAPI
31ca32bd8dSchristos 
32ca32bd8dSchristos 
3347dc7704Schristos #include <stdarg.h>
34ca32bd8dSchristos #include <string.h>
35313c6c94Schristos #include <unistd.h>
36ca32bd8dSchristos #include <stdarg.h>
37e4d43b82Schristos #include <limits.h>
38ca32bd8dSchristos 
39ca32bd8dSchristos #include "xmalloc.h"
4055a4608bSchristos #include "ssherr.h"
4155a4608bSchristos #include "sshbuf.h"
42ca32bd8dSchristos #include "log.h"
43ca32bd8dSchristos #include "ssh2.h"
44ca32bd8dSchristos 
45ca32bd8dSchristos #include "ssh-gss.h"
46ca32bd8dSchristos 
4755a4608bSchristos /* sshbuf_get for gss_buffer_desc */
4855a4608bSchristos int
ssh_gssapi_get_buffer_desc(struct sshbuf * b,gss_buffer_desc * g)4955a4608bSchristos ssh_gssapi_get_buffer_desc(struct sshbuf *b, gss_buffer_desc *g)
5055a4608bSchristos {
5155a4608bSchristos 	int r;
5255a4608bSchristos 	u_char *p;
5355a4608bSchristos 	size_t len;
5455a4608bSchristos 
5555a4608bSchristos 	if ((r = sshbuf_get_string(b, &p, &len)) != 0)
5655a4608bSchristos 		return r;
5755a4608bSchristos 	g->value = p;
5855a4608bSchristos 	g->length = len;
5955a4608bSchristos 	return 0;
6055a4608bSchristos }
6155a4608bSchristos 
62ca32bd8dSchristos /* Check that the OID in a data stream matches that in the context */
63ca32bd8dSchristos int
ssh_gssapi_check_oid(Gssctxt * ctx,void * data,size_t len)64ca32bd8dSchristos ssh_gssapi_check_oid(Gssctxt *ctx, void *data, size_t len)
65ca32bd8dSchristos {
66ca32bd8dSchristos 	return (ctx != NULL && ctx->oid != GSS_C_NO_OID &&
67ca32bd8dSchristos 	    ctx->oid->length == len &&
68ca32bd8dSchristos 	    memcmp(ctx->oid->elements, data, len) == 0);
69ca32bd8dSchristos }
70ca32bd8dSchristos 
71ca32bd8dSchristos /* Set the contexts OID from a data stream */
72ca32bd8dSchristos void
ssh_gssapi_set_oid_data(Gssctxt * ctx,void * data,size_t len)73ca32bd8dSchristos ssh_gssapi_set_oid_data(Gssctxt *ctx, void *data, size_t len)
74ca32bd8dSchristos {
75ca32bd8dSchristos 	if (ctx->oid != GSS_C_NO_OID) {
7600a838c4Schristos 		free(ctx->oid->elements);
7700a838c4Schristos 		free(ctx->oid);
78ca32bd8dSchristos 	}
7900a838c4Schristos 	ctx->oid = xcalloc(1, sizeof(gss_OID_desc));
80ca32bd8dSchristos 	ctx->oid->length = len;
81ca32bd8dSchristos 	ctx->oid->elements = xmalloc(len);
82ca32bd8dSchristos 	memcpy(ctx->oid->elements, data, len);
83ca32bd8dSchristos }
84ca32bd8dSchristos 
85ca32bd8dSchristos /* Set the contexts OID */
86ca32bd8dSchristos void
ssh_gssapi_set_oid(Gssctxt * ctx,gss_OID oid)87ca32bd8dSchristos ssh_gssapi_set_oid(Gssctxt *ctx, gss_OID oid)
88ca32bd8dSchristos {
89ca32bd8dSchristos 	ssh_gssapi_set_oid_data(ctx, oid->elements, oid->length);
90ca32bd8dSchristos }
91ca32bd8dSchristos 
92ca32bd8dSchristos /* All this effort to report an error ... */
93ca32bd8dSchristos void
ssh_gssapi_error(Gssctxt * ctxt)94ca32bd8dSchristos ssh_gssapi_error(Gssctxt *ctxt)
95ca32bd8dSchristos {
96ca32bd8dSchristos 	char *s;
97ca32bd8dSchristos 
98ca32bd8dSchristos 	s = ssh_gssapi_last_error(ctxt, NULL, NULL);
99ca32bd8dSchristos 	debug("%s", s);
10000a838c4Schristos 	free(s);
101ca32bd8dSchristos }
102ca32bd8dSchristos 
103ca32bd8dSchristos char *
ssh_gssapi_last_error(Gssctxt * ctxt,OM_uint32 * major_status,OM_uint32 * minor_status)104ca32bd8dSchristos ssh_gssapi_last_error(Gssctxt *ctxt, OM_uint32 *major_status,
105ca32bd8dSchristos     OM_uint32 *minor_status)
106ca32bd8dSchristos {
107ca32bd8dSchristos 	OM_uint32 lmin;
108ca32bd8dSchristos 	gss_buffer_desc msg = GSS_C_EMPTY_BUFFER;
109ca32bd8dSchristos 	OM_uint32 ctx;
11055a4608bSchristos 	struct sshbuf *b;
111ca32bd8dSchristos 	char *ret;
11255a4608bSchristos 	int r;
113ca32bd8dSchristos 
11455a4608bSchristos 	if ((b = sshbuf_new()) == NULL)
11517418e98Schristos 		fatal_f("sshbuf_new failed");
116ca32bd8dSchristos 
117ca32bd8dSchristos 	if (major_status != NULL)
118ca32bd8dSchristos 		*major_status = ctxt->major;
119ca32bd8dSchristos 	if (minor_status != NULL)
120ca32bd8dSchristos 		*minor_status = ctxt->minor;
121ca32bd8dSchristos 
122ca32bd8dSchristos 	ctx = 0;
123ca32bd8dSchristos 	/* The GSSAPI error */
124ca32bd8dSchristos 	do {
125ca32bd8dSchristos 		gss_display_status(&lmin, ctxt->major,
126ca32bd8dSchristos 		    GSS_C_GSS_CODE, ctxt->oid, &ctx, &msg);
127ca32bd8dSchristos 
12855a4608bSchristos 		if ((r = sshbuf_put(b, msg.value, msg.length)) != 0 ||
12955a4608bSchristos 		    (r = sshbuf_put_u8(b, '\n')) != 0)
13017418e98Schristos 			fatal_fr(r, "assemble GSS_CODE");
131ca32bd8dSchristos 
132ca32bd8dSchristos 		gss_release_buffer(&lmin, &msg);
133ca32bd8dSchristos 	} while (ctx != 0);
134ca32bd8dSchristos 
135ca32bd8dSchristos 	/* The mechanism specific error */
136ca32bd8dSchristos 	do {
137ca32bd8dSchristos 		gss_display_status(&lmin, ctxt->minor,
138ca32bd8dSchristos 		    GSS_C_MECH_CODE, ctxt->oid, &ctx, &msg);
139ca32bd8dSchristos 
14055a4608bSchristos 		if ((r = sshbuf_put(b, msg.value, msg.length)) != 0 ||
14155a4608bSchristos 		    (r = sshbuf_put_u8(b, '\n')) != 0)
14217418e98Schristos 			fatal_fr(r, "assemble MECH_CODE");
143ca32bd8dSchristos 
144ca32bd8dSchristos 		gss_release_buffer(&lmin, &msg);
145ca32bd8dSchristos 	} while (ctx != 0);
146ca32bd8dSchristos 
14755a4608bSchristos 	if ((r = sshbuf_put_u8(b, '\n')) != 0)
14817418e98Schristos 		fatal_fr(r, "assemble newline");
14955a4608bSchristos 	ret = xstrdup((const char *)sshbuf_ptr(b));
15055a4608bSchristos 	sshbuf_free(b);
151ca32bd8dSchristos 	return (ret);
152ca32bd8dSchristos }
153ca32bd8dSchristos 
154ca32bd8dSchristos /*
155ca32bd8dSchristos  * Initialise our GSSAPI context. We use this opaque structure to contain all
156ca32bd8dSchristos  * of the data which both the client and server need to persist across
157ca32bd8dSchristos  * {accept,init}_sec_context calls, so that when we do it from the userauth
158ca32bd8dSchristos  * stuff life is a little easier
159ca32bd8dSchristos  */
160ca32bd8dSchristos void
ssh_gssapi_build_ctx(Gssctxt ** ctx)161ca32bd8dSchristos ssh_gssapi_build_ctx(Gssctxt **ctx)
162ca32bd8dSchristos {
163ca32bd8dSchristos 	*ctx = xcalloc(1, sizeof (Gssctxt));
164ca32bd8dSchristos 	(*ctx)->context = GSS_C_NO_CONTEXT;
165ca32bd8dSchristos 	(*ctx)->name = GSS_C_NO_NAME;
166ca32bd8dSchristos 	(*ctx)->oid = GSS_C_NO_OID;
167ca32bd8dSchristos 	(*ctx)->creds = GSS_C_NO_CREDENTIAL;
168ca32bd8dSchristos 	(*ctx)->client = GSS_C_NO_NAME;
169ca32bd8dSchristos 	(*ctx)->client_creds = GSS_C_NO_CREDENTIAL;
170ca32bd8dSchristos }
171ca32bd8dSchristos 
172ca32bd8dSchristos /* Delete our context, providing it has been built correctly */
173ca32bd8dSchristos void
ssh_gssapi_delete_ctx(Gssctxt ** ctx)174ca32bd8dSchristos ssh_gssapi_delete_ctx(Gssctxt **ctx)
175ca32bd8dSchristos {
176ca32bd8dSchristos 	OM_uint32 ms;
177ca32bd8dSchristos 
178ca32bd8dSchristos 	if ((*ctx) == NULL)
179ca32bd8dSchristos 		return;
180ca32bd8dSchristos 	if ((*ctx)->context != GSS_C_NO_CONTEXT)
181ca32bd8dSchristos 		gss_delete_sec_context(&ms, &(*ctx)->context, GSS_C_NO_BUFFER);
182ca32bd8dSchristos 	if ((*ctx)->name != GSS_C_NO_NAME)
183ca32bd8dSchristos 		gss_release_name(&ms, &(*ctx)->name);
184ca32bd8dSchristos 	if ((*ctx)->oid != GSS_C_NO_OID) {
18500a838c4Schristos 		free((*ctx)->oid->elements);
18600a838c4Schristos 		free((*ctx)->oid);
187ca32bd8dSchristos 		(*ctx)->oid = GSS_C_NO_OID;
188ca32bd8dSchristos 	}
189ca32bd8dSchristos 	if ((*ctx)->creds != GSS_C_NO_CREDENTIAL)
190ca32bd8dSchristos 		gss_release_cred(&ms, &(*ctx)->creds);
191ca32bd8dSchristos 	if ((*ctx)->client != GSS_C_NO_NAME)
192ca32bd8dSchristos 		gss_release_name(&ms, &(*ctx)->client);
193ca32bd8dSchristos 	if ((*ctx)->client_creds != GSS_C_NO_CREDENTIAL)
194ca32bd8dSchristos 		gss_release_cred(&ms, &(*ctx)->client_creds);
195ca32bd8dSchristos 
19600a838c4Schristos 	free(*ctx);
197ca32bd8dSchristos 	*ctx = NULL;
198ca32bd8dSchristos }
199ca32bd8dSchristos 
200ca32bd8dSchristos /*
201ca32bd8dSchristos  * Wrapper to init_sec_context
202ca32bd8dSchristos  * Requires that the context contains:
203ca32bd8dSchristos  *	oid
204ca32bd8dSchristos  *	server name (from ssh_gssapi_import_name)
205ca32bd8dSchristos  */
206ca32bd8dSchristos OM_uint32
ssh_gssapi_init_ctx(Gssctxt * ctx,int deleg_creds,gss_buffer_desc * recv_tok,gss_buffer_desc * send_tok,OM_uint32 * flags)207ca32bd8dSchristos ssh_gssapi_init_ctx(Gssctxt *ctx, int deleg_creds, gss_buffer_desc *recv_tok,
208ca32bd8dSchristos     gss_buffer_desc* send_tok, OM_uint32 *flags)
209ca32bd8dSchristos {
210ca32bd8dSchristos 	int deleg_flag = 0;
211ca32bd8dSchristos 
212ca32bd8dSchristos 	if (deleg_creds) {
213ca32bd8dSchristos 		deleg_flag = GSS_C_DELEG_FLAG;
214ca32bd8dSchristos 		debug("Delegating credentials");
215ca32bd8dSchristos 	}
216ca32bd8dSchristos 
217ca32bd8dSchristos 	ctx->major = gss_init_sec_context(&ctx->minor,
218ca32bd8dSchristos 	    GSS_C_NO_CREDENTIAL, &ctx->context, ctx->name, ctx->oid,
219ca32bd8dSchristos 	    GSS_C_MUTUAL_FLAG | GSS_C_INTEG_FLAG | deleg_flag,
220ca32bd8dSchristos 	    0, NULL, recv_tok, NULL, send_tok, flags, NULL);
221ca32bd8dSchristos 
222ca32bd8dSchristos 	if (GSS_ERROR(ctx->major))
223ca32bd8dSchristos 		ssh_gssapi_error(ctx);
224ca32bd8dSchristos 
225ca32bd8dSchristos 	return (ctx->major);
226ca32bd8dSchristos }
227ca32bd8dSchristos 
228ca32bd8dSchristos /* Create a service name for the given host */
229ca32bd8dSchristos OM_uint32
ssh_gssapi_import_name(Gssctxt * ctx,const char * host)230ca32bd8dSchristos ssh_gssapi_import_name(Gssctxt *ctx, const char *host)
231ca32bd8dSchristos {
232ca32bd8dSchristos 	gss_buffer_desc gssbuf;
233ca32bd8dSchristos 	char *val;
234ca32bd8dSchristos 
235ca32bd8dSchristos 	xasprintf(&val, "host@%s", host);
236ca32bd8dSchristos 	gssbuf.value = val;
237ca32bd8dSchristos 	gssbuf.length = strlen(gssbuf.value);
238ca32bd8dSchristos 
239ca32bd8dSchristos 	if ((ctx->major = gss_import_name(&ctx->minor,
240ca32bd8dSchristos 	    &gssbuf, GSS_C_NT_HOSTBASED_SERVICE, &ctx->name)))
241ca32bd8dSchristos 		ssh_gssapi_error(ctx);
242ca32bd8dSchristos 
24300a838c4Schristos 	free(gssbuf.value);
244ca32bd8dSchristos 	return (ctx->major);
245ca32bd8dSchristos }
246ca32bd8dSchristos 
247ca32bd8dSchristos OM_uint32
ssh_gssapi_sign(Gssctxt * ctx,gss_buffer_t buffer,gss_buffer_t hash)248ca32bd8dSchristos ssh_gssapi_sign(Gssctxt *ctx, gss_buffer_t buffer, gss_buffer_t hash)
249ca32bd8dSchristos {
250ca32bd8dSchristos 	if ((ctx->major = gss_get_mic(&ctx->minor, ctx->context,
251ca32bd8dSchristos 	    GSS_C_QOP_DEFAULT, buffer, hash)))
252ca32bd8dSchristos 		ssh_gssapi_error(ctx);
253ca32bd8dSchristos 
254ca32bd8dSchristos 	return (ctx->major);
255ca32bd8dSchristos }
256ca32bd8dSchristos 
257ca32bd8dSchristos void
ssh_gssapi_buildmic(struct sshbuf * b,const char * user,const char * service,const char * context,const struct sshbuf * session_id)25855a4608bSchristos ssh_gssapi_buildmic(struct sshbuf *b, const char *user, const char *service,
25917418e98Schristos     const char *context, const struct sshbuf *session_id)
260ca32bd8dSchristos {
26155a4608bSchristos 	int r;
26255a4608bSchristos 
26355a4608bSchristos 	sshbuf_reset(b);
26417418e98Schristos 	if ((r = sshbuf_put_stringb(b, session_id)) != 0 ||
26555a4608bSchristos 	    (r = sshbuf_put_u8(b, SSH2_MSG_USERAUTH_REQUEST)) != 0 ||
26655a4608bSchristos 	    (r = sshbuf_put_cstring(b, user)) != 0 ||
26755a4608bSchristos 	    (r = sshbuf_put_cstring(b, service)) != 0 ||
26855a4608bSchristos 	    (r = sshbuf_put_cstring(b, context)) != 0)
26917418e98Schristos 		fatal_fr(r, "assemble buildmic");
270ca32bd8dSchristos }
271ca32bd8dSchristos 
272ca32bd8dSchristos int
ssh_gssapi_check_mechanism(Gssctxt ** ctx,gss_OID oid,const char * host)273ca32bd8dSchristos ssh_gssapi_check_mechanism(Gssctxt **ctx, gss_OID oid, const char *host)
274ca32bd8dSchristos {
275ca32bd8dSchristos 	gss_buffer_desc token = GSS_C_EMPTY_BUFFER;
276ca32bd8dSchristos 	OM_uint32 major, minor;
277185c8f97Schristos 	gss_OID_desc spnego_oid = {6, __UNCONST("\x2B\x06\x01\x05\x05\x02")};
278ca32bd8dSchristos 
279ca32bd8dSchristos 	/* RFC 4462 says we MUST NOT do SPNEGO */
280ca32bd8dSchristos 	if (oid->length == spnego_oid.length &&
281ca32bd8dSchristos 	    (memcmp(oid->elements, spnego_oid.elements, oid->length) == 0))
282ca32bd8dSchristos 		return 0; /* false */
283ca32bd8dSchristos 
284ca32bd8dSchristos 	ssh_gssapi_build_ctx(ctx);
285ca32bd8dSchristos 	ssh_gssapi_set_oid(*ctx, oid);
286ca32bd8dSchristos 	major = ssh_gssapi_import_name(*ctx, host);
287ca32bd8dSchristos 	if (!GSS_ERROR(major)) {
288ca32bd8dSchristos 		major = ssh_gssapi_init_ctx(*ctx, 0, GSS_C_NO_BUFFER, &token,
289ca32bd8dSchristos 		    NULL);
290ca32bd8dSchristos 		gss_release_buffer(&minor, &token);
291ca32bd8dSchristos 		if ((*ctx)->context != GSS_C_NO_CONTEXT)
292ca32bd8dSchristos 			gss_delete_sec_context(&minor, &(*ctx)->context,
293ca32bd8dSchristos 			    GSS_C_NO_BUFFER);
294ca32bd8dSchristos 	}
295ca32bd8dSchristos 
296ca32bd8dSchristos 	if (GSS_ERROR(major))
297ca32bd8dSchristos 		ssh_gssapi_delete_ctx(ctx);
298ca32bd8dSchristos 
299ca32bd8dSchristos 	return (!GSS_ERROR(major));
300ca32bd8dSchristos }
301ca32bd8dSchristos 
302ca32bd8dSchristos #endif /* GSSAPI */
303