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