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