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