1 /* $OpenBSD: gss-genr.c,v 1.1 2003/08/22 10:56:09 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.
35 #include "monitor_wrap.h"
40 /* Check that the OID in a data stream matches that in the context */
42 ssh_gssapi_check_oid(Gssctxt *ctx, void *data, size_t len)
44 return (ctx != NULL && ctx->oid != GSS_C_NO_OID &&
45 ctx->oid->length == len &&
46 memcmp(ctx->oid->elements, data, len) == 0);
49 /* Set the contexts OID from a data stream */
51 ssh_gssapi_set_oid_data(Gssctxt *ctx, void *data, size_t len)
53 if (ctx->oid != GSS_C_NO_OID) {
54 xfree(ctx->oid->elements);
57 ctx->oid = xmalloc(sizeof(gss_OID_desc));
58 ctx->oid->length = len;
59 ctx->oid->elements = xmalloc(len);
60 memcpy(ctx->oid->elements, data, len);
63 /* Set the contexts OID */
65 ssh_gssapi_set_oid(Gssctxt *ctx, gss_OID oid)
67 ssh_gssapi_set_oid_data(ctx, oid->elements, oid->length);
70 /* All this effort to report an error ... */
72 ssh_gssapi_error(Gssctxt *ctxt)
74 debug("%s", ssh_gssapi_last_error(ctxt, NULL, NULL));
78 ssh_gssapi_last_error(Gssctxt *ctxt,
79 OM_uint32 *major_status, OM_uint32 *minor_status)
82 gss_buffer_desc msg = GSS_C_EMPTY_BUFFER;
89 if (major_status != NULL)
90 *major_status = ctxt->major;
91 if (minor_status != NULL)
92 *minor_status = ctxt->minor;
95 /* The GSSAPI error */
97 gss_display_status(&lmin, ctxt->major,
98 GSS_C_GSS_CODE, GSS_C_NULL_OID, &ctx, &msg);
100 buffer_append(&b, msg.value, msg.length);
101 buffer_put_char(&b, '\n');
103 gss_release_buffer(&lmin, &msg);
106 /* The mechanism specific error */
108 gss_display_status(&lmin, ctxt->minor,
109 GSS_C_MECH_CODE, GSS_C_NULL_OID, &ctx, &msg);
111 buffer_append(&b, msg.value, msg.length);
112 buffer_put_char(&b, '\n');
114 gss_release_buffer(&lmin, &msg);
117 buffer_put_char(&b, '\0');
118 ret = xmalloc(buffer_len(&b));
119 buffer_get(&b, ret, buffer_len(&b));
125 * Initialise our GSSAPI context. We use this opaque structure to contain all
126 * of the data which both the client and server need to persist across
127 * {accept,init}_sec_context calls, so that when we do it from the userauth
128 * stuff life is a little easier
131 ssh_gssapi_build_ctx(Gssctxt **ctx)
133 *ctx = xmalloc(sizeof (Gssctxt));
136 (*ctx)->context = GSS_C_NO_CONTEXT;
137 (*ctx)->name = GSS_C_NO_NAME;
138 (*ctx)->oid = GSS_C_NO_OID;
139 (*ctx)->creds = GSS_C_NO_CREDENTIAL;
140 (*ctx)->client = GSS_C_NO_NAME;
141 (*ctx)->client_creds = GSS_C_NO_CREDENTIAL;
144 /* Delete our context, providing it has been built correctly */
146 ssh_gssapi_delete_ctx(Gssctxt **ctx)
152 if ((*ctx)->context != GSS_C_NO_CONTEXT)
153 gss_delete_sec_context(&ms, &(*ctx)->context, GSS_C_NO_BUFFER);
154 if ((*ctx)->name != GSS_C_NO_NAME)
155 gss_release_name(&ms, &(*ctx)->name);
156 if ((*ctx)->oid != GSS_C_NO_OID) {
157 xfree((*ctx)->oid->elements);
159 (*ctx)->oid = GSS_C_NO_OID;
161 if ((*ctx)->creds != GSS_C_NO_CREDENTIAL)
162 gss_release_cred(&ms, &(*ctx)->creds);
163 if ((*ctx)->client != GSS_C_NO_NAME)
164 gss_release_name(&ms, &(*ctx)->client);
165 if ((*ctx)->client_creds != GSS_C_NO_CREDENTIAL)
166 gss_release_cred(&ms, &(*ctx)->client_creds);
173 * Wrapper to init_sec_context
174 * Requires that the context contains:
176 * server name (from ssh_gssapi_import_name)
179 ssh_gssapi_init_ctx(Gssctxt *ctx, int deleg_creds, gss_buffer_desc *recv_tok,
180 gss_buffer_desc* send_tok, OM_uint32 *flags)
185 deleg_flag = GSS_C_DELEG_FLAG;
186 debug("Delegating credentials");
189 ctx->major = gss_init_sec_context(&ctx->minor,
190 GSS_C_NO_CREDENTIAL, &ctx->context, ctx->name, ctx->oid,
191 GSS_C_MUTUAL_FLAG | GSS_C_INTEG_FLAG | deleg_flag,
192 0, NULL, recv_tok, NULL, send_tok, flags, NULL);
194 if (GSS_ERROR(ctx->major))
195 ssh_gssapi_error(ctx);
200 /* Create a service name for the given host */
202 ssh_gssapi_import_name(Gssctxt *ctx, const char *host)
204 gss_buffer_desc gssbuf;
206 gssbuf.length = sizeof("host@") + strlen(host);
207 gssbuf.value = xmalloc(gssbuf.length);
208 snprintf(gssbuf.value, gssbuf.length, "host@%s", host);
210 if ((ctx->major = gss_import_name(&ctx->minor,
211 &gssbuf, GSS_C_NT_HOSTBASED_SERVICE, &ctx->name)))
212 ssh_gssapi_error(ctx);
218 /* Acquire credentials for a server running on the current host.
219 * Requires that the context structure contains a valid OID
222 /* Returns a GSSAPI error code */
224 ssh_gssapi_acquire_cred(Gssctxt *ctx)
227 char lname[MAXHOSTNAMELEN];
230 gss_create_empty_oid_set(&status, &oidset);
231 gss_add_oid_set_member(&status, ctx->oid, &oidset);
233 if (gethostname(lname, MAXHOSTNAMELEN))
236 if (GSS_ERROR(ssh_gssapi_import_name(ctx, lname)))
239 if ((ctx->major = gss_acquire_cred(&ctx->minor,
240 ctx->name, 0, oidset, GSS_C_ACCEPT, &ctx->creds, NULL, NULL)))
241 ssh_gssapi_error(ctx);
243 gss_release_oid_set(&status, &oidset);
248 ssh_gssapi_server_ctx(Gssctxt **ctx, gss_OID oid) {
250 ssh_gssapi_delete_ctx(ctx);
251 ssh_gssapi_build_ctx(ctx);
252 ssh_gssapi_set_oid(*ctx, oid);
253 return (ssh_gssapi_acquire_cred(*ctx));