2 * Copyright (c) 2001 Simon Wilkinson. All rights reserved. *
3 * Redistribution and use in source and binary forms, with or without
4 * modification, are permitted provided that the following conditions
6 * 1. Redistributions of source code must retain the above copyright
7 * notice, this list of conditions and the following disclaimer.
8 * 2. Redistributions in binary form must reproduce the above copyright
9 * notice, this list of conditions and the following disclaimer in the
10 * documentation and/or other materials provided with the distribution.
12 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR `AS IS'' AND ANY EXPRESS OR
13 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
14 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
15 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
16 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
17 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
18 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
19 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
20 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
21 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
35 #include <openssl/evp.h>
45 /* Assorted globals for tracking the clients identity once they've
48 gss_buffer_desc gssapi_client_name = {0,NULL}; /* Name of our client */
49 gss_cred_id_t gssapi_client_creds = GSS_C_NO_CREDENTIAL; /* Their credentials */
50 enum ssh_gss_id gssapi_client_type = GSS_LAST_ENTRY;
52 /* The mechanism name used in the list below is defined in the internet
53 * draft as the Base 64 encoding of the MD5 hash of the ASN.1 DER encoding
54 * of the underlying GSSAPI mechanism's OID.
56 * Also from the draft, before considering adding SPNEGO, bear in mind that
57 * "mechanisms ... MUST NOT use SPNEGO as the underlying GSSAPI mechanism"
60 /* These must be in the same order as ssh_gss_id, in ssh-gss.h */
62 ssh_gssapi_mech supported_mechs[]= {
64 /* Official OID - 1.2.850.113554.1.2.2 */
65 {"Se3H81ismmOC3OE+FwYCiQ==","Kerberos",
66 {9, "\x2A\x86\x48\x86\xF7\x12\x01\x02\x02"}},
69 /* gssapi_ssleay 1.3.6.1.4.1.3536.1.1 */
70 {"N3+k7/4wGxHyuP8Yxi4RhA==",
72 {9, "\x2B\x06\x01\x04\x01\x9B\x50\x01\x01"}
78 char gssprefix[]=KEX_GSS_SHA1;
80 /* Return a list of the gss-group1-sha1-x mechanisms supported by this
83 * We only support the mechanisms that we've indicated in the list above,
84 * but we check that they're supported by the GSSAPI mechanism on the
85 * machine. We also check, before including them in the list, that
86 * we have the necesary information in order to carry out the key exchange
87 * (that is, that the user has credentials, the server's creds are accessible,
90 * The way that this is done is fairly nasty, as we do a lot of work that
91 * is then thrown away. This should possibly be implemented with a cache
92 * that stores the results (in an expanded Gssctxt structure), which are
93 * then used by the first calls if that key exchange mechanism is chosen.
97 ssh_gssapi_mechanisms(int server,char *host) {
98 gss_OID_set supported;
99 OM_uint32 maj_status, min_status;
105 gss_buffer_desc token;
107 if (datafellows & SSH_OLD_GSSAPI) return NULL;
109 gss_indicate_mechs(&min_status, &supported);
114 if ((maj_status=gss_test_oid_set_member(&min_status,
115 &supported_mechs[i].oid,
121 ssh_gssapi_build_ctx(&ctx);
122 ssh_gssapi_set_oid(&ctx,&supported_mechs[i].oid);
124 if (ssh_gssapi_acquire_cred(&ctx)) {
125 ssh_gssapi_delete_ctx(&ctx);
130 if (ssh_gssapi_import_name(&ctx,host))
132 maj_status=ssh_gssapi_init_ctx(&ctx, 0,
136 ssh_gssapi_delete_ctx(&ctx);
137 if (GSS_ERROR(maj_status)) {
142 /* Append gss_group1_sha1_x to our list */
143 buffer_append(&buf, gssprefix,
145 buffer_append(&buf, supported_mechs[i].enc_name,
146 strlen(supported_mechs[i].enc_name));
148 } while (supported_mechs[++i].name != NULL);
150 buffer_put_char(&buf,'\0');
152 mechs=xmalloc(buffer_len(&buf));
153 buffer_get(&buf,mechs,buffer_len(&buf));
155 if (strlen(mechs)==0)
161 void ssh_gssapi_supported_oids(gss_OID_set *oidset) {
162 enum ssh_gss_id i =0;
163 OM_uint32 maj_status,min_status;
165 gss_OID_set supported;
167 gss_create_empty_oid_set(&min_status,oidset);
168 gss_indicate_mechs(&min_status, &supported);
170 while (supported_mechs[i].name!=NULL) {
171 if ((maj_status=gss_test_oid_set_member(&min_status,
172 &supported_mechs[i].oid,
178 gss_add_oid_set_member(&min_status,
179 &supported_mechs[i].oid,
186 /* Set the contexts OID from a data stream */
187 void ssh_gssapi_set_oid_data(Gssctxt *ctx, void *data, size_t len) {
188 if (ctx->oid != GSS_C_NO_OID) {
189 xfree(ctx->oid->elements);
192 ctx->oid=xmalloc(sizeof(gss_OID_desc));
193 ctx->oid->length=len;
194 ctx->oid->elements=xmalloc(len);
195 memcpy(ctx->oid->elements,data,len);
198 /* Set the contexts OID */
199 void ssh_gssapi_set_oid(Gssctxt *ctx, gss_OID oid) {
200 ssh_gssapi_set_oid_data(ctx,oid->elements,oid->length);
203 /* Find out which GSS type (out of the list we define in ssh-gss.h) a
204 * particular connection is using
206 enum ssh_gss_id ssh_gssapi_get_ctype(Gssctxt *ctxt) {
209 while(supported_mechs[i].name!=NULL &&
210 supported_mechs[i].oid.length != ctxt->oid->length &&
211 (memcmp(supported_mechs[i].oid.elements,
212 ctxt->oid->elements,ctxt->oid->length) !=0)) {
218 /* Set the GSS context's OID to the oid indicated by the given key exchange
220 int ssh_gssapi_id_kex(Gssctxt *ctx, char *name) {
223 if (strncmp(name, gssprefix, strlen(gssprefix)-1) !=0) {
227 name+=strlen(gssprefix); /* Move to the start of the MIME string */
229 while (supported_mechs[i].name!=NULL &&
230 strcmp(name,supported_mechs[i].enc_name)!=0) {
234 if (supported_mechs[i].name==NULL)
237 ssh_gssapi_set_oid(ctx,&supported_mechs[i].oid);
243 /* All this effort to report an error ... */
245 ssh_gssapi_error_ex(OM_uint32 major_status,OM_uint32 minor_status,
247 OM_uint32 lmaj, lmin;
252 /* The GSSAPI error */
254 lmaj = gss_display_status(&lmin, major_status,
258 if (lmaj == GSS_S_COMPLETE) {
259 debug((char *)msg.value);
260 if (send_packet) packet_send_debug((char *)msg.value);
261 (void) gss_release_buffer(&lmin, &msg);
265 /* The mechanism specific error */
267 lmaj = gss_display_status(&lmin, minor_status,
271 if (lmaj == GSS_S_COMPLETE) {
272 debug((char *)msg.value);
273 if (send_packet) packet_send_debug((char *)msg.value);
274 (void) gss_release_buffer(&lmin, &msg);
280 ssh_gssapi_error(OM_uint32 major_status,OM_uint32 minor_status) {
281 ssh_gssapi_error_ex(major_status, minor_status, 0);
285 ssh_gssapi_send_error(OM_uint32 major_status,OM_uint32 minor_status) {
286 ssh_gssapi_error_ex(major_status, minor_status, 1);
292 /* Initialise our GSSAPI context. We use this opaque structure to contain all
293 * of the data which both the client and server need to persist across
294 * {accept,init}_sec_context calls, so that when we do it from the userauth
295 * stuff life is a little easier
298 ssh_gssapi_build_ctx(Gssctxt *ctx)
300 ctx->context=GSS_C_NO_CONTEXT;
301 ctx->name=GSS_C_NO_NAME;
302 ctx->oid=GSS_C_NO_OID;
303 ctx->creds=GSS_C_NO_CREDENTIAL;
304 ctx->client=GSS_C_NO_NAME;
305 ctx->client_creds=GSS_C_NO_CREDENTIAL;
308 /* Delete our context, providing it has been built correctly */
310 ssh_gssapi_delete_ctx(Gssctxt *ctx)
314 if (ctx->context != GSS_C_NO_CONTEXT)
315 gss_delete_sec_context(&ms,&ctx->context,GSS_C_NO_BUFFER);
316 if (ctx->name != GSS_C_NO_NAME)
317 gss_release_name(&ms,&ctx->name);
318 if (ctx->oid != GSS_C_NO_OID) {
319 xfree(ctx->oid->elements);
321 ctx->oid = GSS_C_NO_OID;
323 if (ctx->creds != GSS_C_NO_CREDENTIAL)
324 gss_release_cred(&ms,&ctx->creds);
325 if (ctx->client != GSS_C_NO_NAME)
326 gss_release_name(&ms,&ctx->client);
327 if (ctx->client_creds != GSS_C_NO_CREDENTIAL)
328 gss_release_cred(&ms,&ctx->client_creds);
331 /* Wrapper to init_sec_context
332 * Requires that the context contains:
334 * server name (from ssh_gssapi_import_name)
337 ssh_gssapi_init_ctx(Gssctxt *ctx, int deleg_creds, gss_buffer_desc *recv_tok,
338 gss_buffer_desc* send_tok, OM_uint32 *flags)
340 OM_uint32 maj_status, min_status;
344 deleg_flag=GSS_C_DELEG_FLAG;
345 debug("Delegating credentials");
348 maj_status=gss_init_sec_context(&min_status,
349 GSS_C_NO_CREDENTIAL, /* def. cred */
356 0, /* default lifetime */
357 NULL, /* no channel bindings */
363 ctx->status=maj_status;
364 if (GSS_ERROR(maj_status)) {
365 ssh_gssapi_error(maj_status,min_status);
370 /* Wrapper arround accept_sec_context
371 * Requires that the context contains:
373 * credentials (from ssh_gssapi_acquire_cred)
375 OM_uint32 ssh_gssapi_accept_ctx(Gssctxt *ctx,gss_buffer_desc *recv_tok,
376 gss_buffer_desc *send_tok, OM_uint32 *flags)
378 OM_uint32 maj_status, min_status;
381 maj_status=gss_accept_sec_context(&min_status,
385 GSS_C_NO_CHANNEL_BINDINGS,
392 if (GSS_ERROR(maj_status)) {
393 ssh_gssapi_send_error(maj_status,min_status);
396 if (ctx->client_creds) {
397 debug("Received some client credentials");
399 debug("Got no client credentials");
402 /* FIXME: We should check that the mechanism thats being used is
403 * the one that we asked for (in ctx->oid) */
405 ctx->status=maj_status;
410 /* Create a service name for the given host */
412 ssh_gssapi_import_name(Gssctxt *ctx, const char *host) {
413 gss_buffer_desc gssbuf;
414 OM_uint32 maj_status, min_status;
415 struct hostent *hostinfo = NULL;
418 /* Make a copy of the host name, in case it was returned by a
419 * previous call to gethostbyname(). */
420 xhost = xstrdup(host);
422 /* Make sure we have the FQDN. Some GSSAPI implementations don't do
423 * this for us themselves */
425 hostinfo = gethostbyname(xhost);
427 if ((hostinfo == NULL) || (hostinfo->h_name == NULL)) {
428 debug("Unable to get FQDN for \"%s\"", xhost);
431 xhost = xstrdup(hostinfo->h_name);
434 gssbuf.length = sizeof("host@")+strlen(xhost);
436 gssbuf.value = xmalloc(gssbuf.length);
437 if (gssbuf.value == NULL) {
441 snprintf(gssbuf.value,gssbuf.length,"host@%s",xhost);
442 if ((maj_status=gss_import_name(&min_status,
444 GSS_C_NT_HOSTBASED_SERVICE,
446 ssh_gssapi_error(maj_status,min_status);
454 /* Acquire credentials for a server running on the current host.
455 * Requires that the context structure contains a valid OID
458 ssh_gssapi_acquire_cred(Gssctxt *ctx) {
459 OM_uint32 maj_status, min_status;
460 char lname[MAXHOSTNAMELEN];
463 gss_create_empty_oid_set(&min_status,&oidset);
464 gss_add_oid_set_member(&min_status,ctx->oid,&oidset);
466 if (gethostname(lname, MAXHOSTNAMELEN)) {
470 if ((maj_status=ssh_gssapi_import_name(ctx,lname))) {
473 if ((maj_status=gss_acquire_cred(&min_status,
481 ssh_gssapi_error(maj_status,min_status);
484 gss_release_oid_set(&min_status, &oidset);
488 /* Extract the client details from a given context. This can only reliably
489 * be called once for a context */
492 ssh_gssapi_getclient(Gssctxt *ctx, enum ssh_gss_id *type,
493 gss_buffer_desc *name, gss_cred_id_t *creds) {
495 OM_uint32 maj_status,min_status;
497 *type=ssh_gssapi_get_ctype(ctx);
498 if ((maj_status=gss_display_name(&min_status,ctx->client,name,NULL))) {
499 ssh_gssapi_error(maj_status,min_status);
502 /* This is icky. There appears to be no way to copy this structure,
503 * rather than the pointer to it, so we simply copy the pointer and
504 * mark the originator as empty so we don't destroy it.
506 *creds=ctx->client_creds;
507 ctx->client_creds=GSS_C_NO_CREDENTIAL;