1 /* $OpenBSD: gss-serv.c,v 1.5 2003/11/17 11:06:07 markus Exp $ */
4 * Copyright (c) 2001-2003 Simon Wilkinson. All rights reserved.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
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.
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.
34 #include <openssl/evp.h>
41 #include "monitor_wrap.h"
47 extern ServerOptions options;
48 extern u_char *session_id2;
49 extern int session_id2_len;
51 static ssh_gssapi_client gssapi_client =
52 { GSS_C_EMPTY_BUFFER, GSS_C_EMPTY_BUFFER,
53 GSS_C_NO_CREDENTIAL, NULL, {NULL, NULL, NULL}};
55 ssh_gssapi_mech gssapi_null_mech =
56 { NULL, NULL, {0, NULL}, NULL, NULL, NULL, NULL};
59 extern ssh_gssapi_mech gssapi_kerberos_mech;
60 extern ssh_gssapi_mech gssapi_kerberos_mech_old;
63 extern ssh_gssapi_mech gssapi_gsi_mech;
64 extern ssh_gssapi_mech gssapi_gsi_mech_old;
67 ssh_gssapi_mech* supported_mechs[]= {
69 &gssapi_kerberos_mech,
70 &gssapi_kerberos_mech_old, /* Support for legacy clients */
74 &gssapi_gsi_mech_old, /* Support for legacy clients */
79 #ifdef GSS_C_GLOBUS_LIMITED_PROXY_FLAG
80 static int limited = 0;
85 ssh_gssapi_supported_oids(gss_OID_set *oidset)
90 gss_OID_set supported;
92 gss_create_empty_oid_set(&min_status, oidset);
93 /* Ask priviledged process what mechanisms it supports. */
94 PRIVSEP(gss_indicate_mechs(&min_status, &supported));
96 while (supported_mechs[i]->name != NULL) {
97 if (GSS_ERROR(gss_test_oid_set_member(&min_status,
98 &supported_mechs[i]->oid, supported, &present)))
101 gss_add_oid_set_member(&min_status,
102 &supported_mechs[i]->oid, oidset);
108 /* Wrapper around accept_sec_context
109 * Requires that the context contains:
111 * credentials (from ssh_gssapi_acquire_cred)
115 ssh_gssapi_accept_ctx(Gssctxt *ctx, gss_buffer_desc *recv_tok,
116 gss_buffer_desc *send_tok, OM_uint32 *flags)
121 ctx->major = gss_accept_sec_context(&ctx->minor,
122 &ctx->context, ctx->creds, recv_tok,
123 GSS_C_NO_CHANNEL_BINDINGS, &ctx->client, &mech,
124 send_tok, flags, NULL, &ctx->client_creds);
126 if (GSS_ERROR(ctx->major))
127 ssh_gssapi_error(ctx);
129 if (ctx->client_creds)
130 debug("Received some client credentials");
132 debug("Got no client credentials");
136 /* Now, if we're complete and we have the right flags, then
137 * we flag the user as also having been authenticated
140 if (((flags == NULL) || ((*flags & GSS_C_MUTUAL_FLAG) &&
141 (*flags & GSS_C_INTEG_FLAG))) && (ctx->major == GSS_S_COMPLETE)) {
142 if (ssh_gssapi_getclient(ctx, &gssapi_client))
143 fatal("Couldn't convert client name");
144 #ifdef GSS_C_GLOBUS_LIMITED_PROXY_FLAG
145 if (flags && (*flags & GSS_C_GLOBUS_LIMITED_PROXY_FLAG))
154 * This parses an exported name, extracting the mechanism specific portion
155 * to use for ACL checking. It verifies that the name belongs the mechanism
156 * originally selected.
159 ssh_gssapi_parse_ename(Gssctxt *ctx, gss_buffer_t ename, gss_buffer_t name)
167 #ifdef GSI /* GSI gss_export_name() is broken. */
168 if ((ctx->oid->length == gssapi_gsi_mech.oid.length) &&
169 (memcmp(ctx->oid->elements, gssapi_gsi_mech.oid.elements,
170 gssapi_gsi_mech.oid.length) == 0)) {
171 name->length = ename->length;
172 name->value = xmalloc(ename->length+1);
173 memcpy(name->value, ename->value, ename->length);
174 return GSS_S_COMPLETE;
179 * Check that ename is long enough for all of the fixed length
180 * header, and that the initial ID bytes are correct
183 if (ename->length<6 || memcmp(tok,"\x04\x01", 2)!=0)
184 return GSS_S_FAILURE;
187 * Extract the OID, and check it. Here GSSAPI breaks with tradition
188 * and does use the OID type and length bytes. To confuse things
189 * there are two lengths - the first including these, and the
193 oidl = GET_16BIT(tok+2); /* length including next two bytes */
194 oidl = oidl-2; /* turn it into the _real_ length of the variable OID */
197 * Check the BER encoding for correct type and length, that the
198 * string is long enough and that the OID matches that in our context
200 if (tok[4] != 0x06 || tok[5] != oidl ||
201 ename->length < oidl+6 ||
202 !ssh_gssapi_check_oid(ctx,tok+6,oidl))
203 return GSS_S_FAILURE;
207 if (ename->length < offset+4)
208 return GSS_S_FAILURE;
210 name->length = GET_32BIT(tok+offset);
213 if (ename->length < offset+name->length)
214 return GSS_S_FAILURE;
216 name->value = xmalloc(name->length+1);
217 memcpy(name->value,tok+offset,name->length);
218 ((char *)name->value)[name->length] = 0;
220 return GSS_S_COMPLETE;
223 /* Extract the client details from a given context. This can only reliably
224 * be called once for a context */
226 /* Priviledged (called from accept_secure_ctx) */
228 ssh_gssapi_getclient(Gssctxt *ctx, ssh_gssapi_client *client)
232 gss_buffer_desc ename;
236 while (supported_mechs[i]->name != NULL) {
237 if (supported_mechs[i]->oid.length == ctx->oid->length &&
238 (memcmp(supported_mechs[i]->oid.elements,
239 ctx->oid->elements, ctx->oid->length) == 0))
240 client->mech = supported_mechs[i];
244 if (client->mech == NULL)
245 return GSS_S_FAILURE;
247 if ((ctx->major = gss_display_name(&ctx->minor, ctx->client,
248 &client->displayname, NULL))) {
249 ssh_gssapi_error(ctx);
253 if ((ctx->major = gss_export_name(&ctx->minor, ctx->client,
255 ssh_gssapi_error(ctx);
259 if ((ctx->major = ssh_gssapi_parse_ename(ctx,&ename,
260 &client->exportedname))) {
264 /* We can't copy this structure, so we just move the pointer to it */
265 client->creds = ctx->client_creds;
266 ctx->client_creds = GSS_C_NO_CREDENTIAL;
270 /* As user - called on fatal/exit */
272 ssh_gssapi_cleanup_creds(void)
274 if (gssapi_client.store.filename != NULL) {
275 /* Unlink probably isn't sufficient */
276 debug("removing gssapi cred file\"%s\"", gssapi_client.store.filename);
277 unlink(gssapi_client.store.filename);
283 ssh_gssapi_storecreds(void)
285 if (gssapi_client.mech && gssapi_client.mech->storecreds) {
286 (*gssapi_client.mech->storecreds)(&gssapi_client);
288 debug("ssh_gssapi_storecreds: Not a GSSAPI mechanism");
291 /* This allows GSSAPI methods to do things to the childs environment based
292 * on the passed authentication process and credentials.
296 ssh_gssapi_do_child(char ***envp, u_int *envsizep)
299 if (gssapi_client.store.envvar != NULL &&
300 gssapi_client.store.envval != NULL) {
302 debug("Setting %s to %s", gssapi_client.store.envvar,
303 gssapi_client.store.envval);
304 child_set_env(envp, envsizep, gssapi_client.store.envvar,
305 gssapi_client.store.envval);
311 ssh_gssapi_userok(char *user)
313 if (gssapi_client.exportedname.length == 0 ||
314 gssapi_client.exportedname.value == NULL) {
315 debug("No suitable client data");
318 #ifdef GSS_C_GLOBUS_LIMITED_PROXY_FLAG
320 debug("limited proxy not acceptable for remote login");
324 if (gssapi_client.mech && gssapi_client.mech->userok)
325 return ((*gssapi_client.mech->userok)(&gssapi_client, user));
327 debug("ssh_gssapi_userok: Unknown GSSAPI mechanism");
331 /* Return a list of the gss-group1-sha1-x mechanisms supported by this
334 * We only support the mechanisms that we've indicated in the list above,
335 * but we check that they're supported by the GSSAPI mechanism on the
336 * machine. We also check, before including them in the list, that
337 * we have the necesary information in order to carry out the key exchange
338 * (that is, that the user has credentials, the server's creds are accessible,
341 * The way that this is done is fairly nasty, as we do a lot of work that
342 * is then thrown away. This should possibly be implemented with a cache
343 * that stores the results (in an expanded Gssctxt structure), which are
344 * then used by the first calls if that key exchange mechanism is chosen.
349 ssh_gssapi_server_mechanisms() {
350 gss_OID_set supported;
352 OM_uint32 maj_status, min_status;
359 if (datafellows & SSH_OLD_GSSAPI) return NULL;
361 ssh_gssapi_supported_oids(&supported);
365 while(supported_mechs[i]->name != NULL) {
366 if ((maj_status=gss_test_oid_set_member(&min_status,
367 &supported_mechs[i]->oid,
374 if (!GSS_ERROR(PRIVSEP(ssh_gssapi_server_ctx(&ctx,
375 &supported_mechs[i]->oid)))) {
376 /* Append gss_group1_sha1_x to our list */
378 buffer_put_char(&buf,',');
379 buffer_append(&buf, KEX_GSS_SHA1,
380 sizeof(KEX_GSS_SHA1)-1);
382 supported_mechs[i]->enc_name,
383 strlen(supported_mechs[i]->enc_name));
384 debug("GSSAPI mechanism %s (%s%s) supported",
385 supported_mechs[i]->name, KEX_GSS_SHA1,
386 supported_mechs[i]->enc_name);
388 debug("no credentials for GSSAPI mechanism %s",
389 supported_mechs[i]->name);
392 debug("GSSAPI mechanism %s not supported",
393 supported_mechs[i]->name);
395 ssh_gssapi_delete_ctx(&ctx);
399 buffer_put_char(&buf,'\0');
401 mechs=xmalloc(buffer_len(&buf));
402 buffer_get(&buf,mechs,buffer_len(&buf));
404 if (strlen(mechs)==0)
410 /* Return the OID that corresponds to the given context name */
414 ssh_gssapi_server_id_kex(char *name) {
417 if (strncmp(name, KEX_GSS_SHA1, sizeof(KEX_GSS_SHA1)-1) !=0) {
421 name+=sizeof(KEX_GSS_SHA1)-1; /* Move to the start of the MIME string */
423 while (supported_mechs[i]->name!=NULL &&
424 strcmp(name,supported_mechs[i]->enc_name)!=0) {
428 if (supported_mechs[i]->name==NULL)
431 debug("using GSSAPI mechanism %s (%s%s)", supported_mechs[i]->name,
432 KEX_GSS_SHA1, supported_mechs[i]->enc_name);
434 return &supported_mechs[i]->oid;
439 ssh_gssapi_localname(char **user)
442 if (gssapi_client.displayname.length==0 ||
443 gssapi_client.displayname.value==NULL) {
444 debug("No suitable client data");
447 if (gssapi_client.mech && gssapi_client.mech->localname) {
448 return((*gssapi_client.mech->localname)(&gssapi_client,user));
450 debug("Unknown client authentication type");
457 ssh_gssapi_checkmic(Gssctxt *ctx, gss_buffer_t gssbuf, gss_buffer_t gssmic)
459 ctx->major = gss_verify_mic(&ctx->minor, ctx->context,
460 gssbuf, gssmic, NULL);