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