1 /*	$NetBSD: gss-serv.c,v 1.15 2020/05/28 17:05:49 christos Exp $	*/
2 /* $OpenBSD: gss-serv.c,v 1.32 2020/03/13 03:17:07 djm Exp $ */
3 
4 /*
5  * Copyright (c) 2001-2003 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: gss-serv.c,v 1.15 2020/05/28 17:05:49 christos Exp $");
30 
31 #include <sys/param.h>
32 #include <sys/types.h>
33 #include <sys/queue.h>
34 
35 #ifdef GSSAPI
36 
37 #include <string.h>
38 #include <unistd.h>
39 #include <netdb.h>
40 #include <limits.h>
41 #include <stdarg.h>
42 
43 #include "xmalloc.h"
44 #include "sshkey.h"
45 #include "hostfile.h"
46 #include "auth.h"
47 #include "log.h"
48 #include "channels.h"
49 #include "session.h"
50 #include "misc.h"
51 #include "servconf.h"
52 
53 #include "ssh-gss.h"
54 
55 extern ServerOptions options;
56 
57 static ssh_gssapi_client gssapi_client =
58     { GSS_C_EMPTY_BUFFER, GSS_C_EMPTY_BUFFER,
59     GSS_C_NO_CREDENTIAL, NULL, {NULL, NULL, NULL, NULL}};
60 
61 ssh_gssapi_mech gssapi_null_mech =
62     { NULL, NULL, {0, NULL}, NULL, NULL, NULL, NULL};
63 
64 #ifdef KRB5
65 extern ssh_gssapi_mech gssapi_kerberos_mech;
66 #endif
67 
68 ssh_gssapi_mech* supported_mechs[]= {
69 #ifdef KRB5
70 	&gssapi_kerberos_mech,
71 #endif
72 	&gssapi_null_mech,
73 };
74 
75 /*
76  * ssh_gssapi_supported_oids() can cause sandbox violations, so prepare the
77  * list of supported mechanisms before privsep is set up.
78  */
79 static gss_OID_set supported_oids;
80 
81 void
ssh_gssapi_prepare_supported_oids(void)82 ssh_gssapi_prepare_supported_oids(void)
83 {
84 	ssh_gssapi_supported_oids(&supported_oids);
85 }
86 
87 OM_uint32
ssh_gssapi_test_oid_supported(OM_uint32 * ms,gss_OID member,int * present)88 ssh_gssapi_test_oid_supported(OM_uint32 *ms, gss_OID member, int *present)
89 {
90 	if (supported_oids == NULL)
91 		ssh_gssapi_prepare_supported_oids();
92 	return gss_test_oid_set_member(ms, member, supported_oids, present);
93 }
94 
95 /*
96  * Acquire credentials for a server running on the current host.
97  * Requires that the context structure contains a valid OID
98  */
99 
100 /* Returns a GSSAPI error code */
101 /* Privileged (called from ssh_gssapi_server_ctx) */
102 static OM_uint32
ssh_gssapi_acquire_cred(Gssctxt * ctx)103 ssh_gssapi_acquire_cred(Gssctxt *ctx)
104 {
105 	OM_uint32 status;
106 	char lname[NI_MAXHOST];
107 	gss_OID_set oidset;
108 
109 	if (options.gss_strict_acceptor) {
110 		gss_create_empty_oid_set(&status, &oidset);
111 		gss_add_oid_set_member(&status, ctx->oid, &oidset);
112 
113 		if (gethostname(lname, MAXHOSTNAMELEN)) {
114 			gss_release_oid_set(&status, &oidset);
115 			return (-1);
116 		}
117 
118 		if (GSS_ERROR(ssh_gssapi_import_name(ctx, lname))) {
119 			gss_release_oid_set(&status, &oidset);
120 			return (ctx->major);
121 		}
122 
123 		if ((ctx->major = gss_acquire_cred(&ctx->minor,
124 		    ctx->name, 0, oidset, GSS_C_ACCEPT, &ctx->creds,
125 		    NULL, NULL)))
126 			ssh_gssapi_error(ctx);
127 
128 		gss_release_oid_set(&status, &oidset);
129 		return (ctx->major);
130 	} else {
131 		ctx->name = GSS_C_NO_NAME;
132 		ctx->creds = GSS_C_NO_CREDENTIAL;
133 	}
134 	return GSS_S_COMPLETE;
135 }
136 
137 /* Privileged */
138 OM_uint32
ssh_gssapi_server_ctx(Gssctxt ** ctx,gss_OID oid)139 ssh_gssapi_server_ctx(Gssctxt **ctx, gss_OID oid)
140 {
141 	if (*ctx)
142 		ssh_gssapi_delete_ctx(ctx);
143 	ssh_gssapi_build_ctx(ctx);
144 	ssh_gssapi_set_oid(*ctx, oid);
145 	return (ssh_gssapi_acquire_cred(*ctx));
146 }
147 
148 /* Unprivileged */
149 void
ssh_gssapi_supported_oids(gss_OID_set * oidset)150 ssh_gssapi_supported_oids(gss_OID_set *oidset)
151 {
152 	int i = 0;
153 	OM_uint32 min_status;
154 	int present;
155 	gss_OID_set supported;
156 
157 	gss_create_empty_oid_set(&min_status, oidset);
158 	gss_indicate_mechs(&min_status, &supported);
159 
160 	while (supported_mechs[i]->name != NULL) {
161 		if (GSS_ERROR(gss_test_oid_set_member(&min_status,
162 		    &supported_mechs[i]->oid, supported, &present)))
163 			present = 0;
164 		if (present)
165 			gss_add_oid_set_member(&min_status,
166 			    &supported_mechs[i]->oid, oidset);
167 		i++;
168 	}
169 
170 	gss_release_oid_set(&min_status, &supported);
171 }
172 
173 
174 /* Wrapper around accept_sec_context
175  * Requires that the context contains:
176  *    oid
177  *    credentials	(from ssh_gssapi_acquire_cred)
178  */
179 /* Privileged */
180 OM_uint32
ssh_gssapi_accept_ctx(Gssctxt * ctx,gss_buffer_desc * recv_tok,gss_buffer_desc * send_tok,OM_uint32 * flags)181 ssh_gssapi_accept_ctx(Gssctxt *ctx, gss_buffer_desc *recv_tok,
182     gss_buffer_desc *send_tok, OM_uint32 *flags)
183 {
184 	OM_uint32 status;
185 	gss_OID mech;
186 
187 	ctx->major = gss_accept_sec_context(&ctx->minor,
188 	    &ctx->context, ctx->creds, recv_tok,
189 	    GSS_C_NO_CHANNEL_BINDINGS, &ctx->client, &mech,
190 	    send_tok, flags, NULL, &ctx->client_creds);
191 
192 	if (GSS_ERROR(ctx->major))
193 		ssh_gssapi_error(ctx);
194 
195 	if (ctx->client_creds)
196 		debug("Received some client credentials");
197 	else
198 		debug("Got no client credentials");
199 
200 	status = ctx->major;
201 
202 	/* Now, if we're complete and we have the right flags, then
203 	 * we flag the user as also having been authenticated
204 	 */
205 
206 	if (((flags == NULL) || ((*flags & GSS_C_MUTUAL_FLAG) &&
207 	    (*flags & GSS_C_INTEG_FLAG))) && (ctx->major == GSS_S_COMPLETE)) {
208 		if (ssh_gssapi_getclient(ctx, &gssapi_client))
209 			fatal("Couldn't convert client name");
210 	}
211 
212 	return (status);
213 }
214 
215 /*
216  * This parses an exported name, extracting the mechanism specific portion
217  * to use for ACL checking. It verifies that the name belongs the mechanism
218  * originally selected.
219  */
220 static OM_uint32
ssh_gssapi_parse_ename(Gssctxt * ctx,gss_buffer_t ename,gss_buffer_t name)221 ssh_gssapi_parse_ename(Gssctxt *ctx, gss_buffer_t ename, gss_buffer_t name)
222 {
223 	u_char *tok;
224 	OM_uint32 offset;
225 	OM_uint32 oidl;
226 
227 	tok = ename->value;
228 
229 	/*
230 	 * Check that ename is long enough for all of the fixed length
231 	 * header, and that the initial ID bytes are correct
232 	 */
233 
234 	if (ename->length < 6 || memcmp(tok, "\x04\x01", 2) != 0)
235 		return GSS_S_FAILURE;
236 
237 	/*
238 	 * Extract the OID, and check it. Here GSSAPI breaks with tradition
239 	 * and does use the OID type and length bytes. To confuse things
240 	 * there are two lengths - the first including these, and the
241 	 * second without.
242 	 */
243 
244 	oidl = get_u16(tok+2); /* length including next two bytes */
245 	oidl = oidl-2; /* turn it into the _real_ length of the variable OID */
246 
247 	/*
248 	 * Check the BER encoding for correct type and length, that the
249 	 * string is long enough and that the OID matches that in our context
250 	 */
251 	if (tok[4] != 0x06 || tok[5] != oidl ||
252 	    ename->length < oidl+6 ||
253 	    !ssh_gssapi_check_oid(ctx, tok+6, oidl))
254 		return GSS_S_FAILURE;
255 
256 	offset = oidl+6;
257 
258 	if (ename->length < offset+4)
259 		return GSS_S_FAILURE;
260 
261 	name->length = get_u32(tok+offset);
262 	offset += 4;
263 
264 	if (UINT_MAX - offset < name->length)
265 		return GSS_S_FAILURE;
266 	if (ename->length < offset+name->length)
267 		return GSS_S_FAILURE;
268 
269 	name->value = xmalloc(name->length+1);
270 	memcpy(name->value, tok+offset, name->length);
271 	((char *)name->value)[name->length] = 0;
272 
273 	return GSS_S_COMPLETE;
274 }
275 
276 /* Extract the client details from a given context. This can only reliably
277  * be called once for a context */
278 
279 /* Privileged (called from accept_secure_ctx) */
280 OM_uint32
ssh_gssapi_getclient(Gssctxt * ctx,ssh_gssapi_client * client)281 ssh_gssapi_getclient(Gssctxt *ctx, ssh_gssapi_client *client)
282 {
283 	int i = 0;
284 
285 	gss_buffer_desc ename;
286 
287 	client->mech = NULL;
288 
289 	while (supported_mechs[i]->name != NULL) {
290 		if (supported_mechs[i]->oid.length == ctx->oid->length &&
291 		    (memcmp(supported_mechs[i]->oid.elements,
292 		    ctx->oid->elements, ctx->oid->length) == 0))
293 			client->mech = supported_mechs[i];
294 		i++;
295 	}
296 
297 	if (client->mech == NULL)
298 		return GSS_S_FAILURE;
299 
300 	if ((ctx->major = gss_display_name(&ctx->minor, ctx->client,
301 	    &client->displayname, NULL))) {
302 		ssh_gssapi_error(ctx);
303 		return (ctx->major);
304 	}
305 
306 	if ((ctx->major = gss_export_name(&ctx->minor, ctx->client,
307 	    &ename))) {
308 		ssh_gssapi_error(ctx);
309 		return (ctx->major);
310 	}
311 
312 	if ((ctx->major = ssh_gssapi_parse_ename(ctx,&ename,
313 	    &client->exportedname))) {
314 		return (ctx->major);
315 	}
316 
317 	/* We can't copy this structure, so we just move the pointer to it */
318 	client->creds = ctx->client_creds;
319 	ctx->client_creds = GSS_C_NO_CREDENTIAL;
320 	return (ctx->major);
321 }
322 
323 /* As user - called on fatal/exit */
324 void
ssh_gssapi_cleanup_creds(void)325 ssh_gssapi_cleanup_creds(void)
326 {
327 	if (gssapi_client.store.filename != NULL) {
328 		/* Unlink probably isn't sufficient */
329 		debug("removing gssapi cred file\"%s\"",
330 		    gssapi_client.store.filename);
331 		unlink(gssapi_client.store.filename);
332 	}
333 }
334 
335 /* As user */
336 void
ssh_gssapi_storecreds(void)337 ssh_gssapi_storecreds(void)
338 {
339 	if (gssapi_client.mech && gssapi_client.mech->storecreds) {
340 		(*gssapi_client.mech->storecreds)(&gssapi_client);
341 	} else
342 		debug("ssh_gssapi_storecreds: Not a GSSAPI mechanism");
343 }
344 
345 /* This allows GSSAPI methods to do things to the child's environment based
346  * on the passed authentication process and credentials.
347  */
348 /* As user */
349 void
ssh_gssapi_do_child(char *** envp,u_int * envsizep)350 ssh_gssapi_do_child(char ***envp, u_int *envsizep)
351 {
352 
353 	if (gssapi_client.store.envvar != NULL &&
354 	    gssapi_client.store.envval != NULL) {
355 		debug("Setting %s to %s", gssapi_client.store.envvar,
356 		    gssapi_client.store.envval);
357 		child_set_env(envp, envsizep, gssapi_client.store.envvar,
358 		    gssapi_client.store.envval);
359 	}
360 }
361 
362 /* Privileged */
363 int
ssh_gssapi_userok(char * user)364 ssh_gssapi_userok(char *user)
365 {
366 	OM_uint32 lmin;
367 
368 	if (gssapi_client.exportedname.length == 0 ||
369 	    gssapi_client.exportedname.value == NULL) {
370 		debug("No suitable client data");
371 		return 0;
372 	}
373 	if (gssapi_client.mech && gssapi_client.mech->userok)
374 		if ((*gssapi_client.mech->userok)(&gssapi_client, user))
375 			return 1;
376 		else {
377 			/* Destroy delegated credentials if userok fails */
378 			gss_release_buffer(&lmin, &gssapi_client.displayname);
379 			gss_release_buffer(&lmin, &gssapi_client.exportedname);
380 			gss_release_cred(&lmin, &gssapi_client.creds);
381 			explicit_bzero(&gssapi_client,
382 			    sizeof(ssh_gssapi_client));
383 			return 0;
384 		}
385 	else
386 		debug("ssh_gssapi_userok: Unknown GSSAPI mechanism");
387 	return (0);
388 }
389 
390 /* Privileged */
391 OM_uint32
ssh_gssapi_checkmic(Gssctxt * ctx,gss_buffer_t gssbuf,gss_buffer_t gssmic)392 ssh_gssapi_checkmic(Gssctxt *ctx, gss_buffer_t gssbuf, gss_buffer_t gssmic)
393 {
394 	ctx->major = gss_verify_mic(&ctx->minor, ctx->context,
395 	    gssbuf, gssmic, NULL);
396 
397 	return (ctx->major);
398 }
399 
400 /* Privileged */
ssh_gssapi_displayname(void)401 const char *ssh_gssapi_displayname(void)
402 {
403 	if (gssapi_client.displayname.length == 0 ||
404 	    gssapi_client.displayname.value == NULL)
405 		return NULL;
406 	return (char *)gssapi_client.displayname.value;
407 }
408 
409 #endif
410