1 /* $OpenBSD: gss-genr.c,v 1.11 2006/07/22 20:48:23 stevesk 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.
41 extern u_char *session_id2;
42 extern u_int session_id2_len;
44 /* Check that the OID in a data stream matches that in the context */
46 ssh_gssapi_check_oid(Gssctxt *ctx, void *data, size_t len)
48 return (ctx != NULL && ctx->oid != GSS_C_NO_OID &&
49 ctx->oid->length == len &&
50 memcmp(ctx->oid->elements, data, len) == 0);
53 /* Set the contexts OID from a data stream */
55 ssh_gssapi_set_oid_data(Gssctxt *ctx, void *data, size_t len)
57 if (ctx->oid != GSS_C_NO_OID) {
58 xfree(ctx->oid->elements);
61 ctx->oid = xmalloc(sizeof(gss_OID_desc));
62 ctx->oid->length = len;
63 ctx->oid->elements = xmalloc(len);
64 memcpy(ctx->oid->elements, data, len);
67 /* Set the contexts OID */
69 ssh_gssapi_set_oid(Gssctxt *ctx, gss_OID oid)
71 ssh_gssapi_set_oid_data(ctx, oid->elements, oid->length);
74 /* All this effort to report an error ... */
76 ssh_gssapi_error(Gssctxt *ctxt)
80 s = ssh_gssapi_last_error(ctxt, NULL, NULL);
86 ssh_gssapi_last_error(Gssctxt *ctxt, OM_uint32 *major_status,
87 OM_uint32 *minor_status)
90 gss_buffer_desc msg = GSS_C_EMPTY_BUFFER;
97 if (major_status != NULL)
98 *major_status = ctxt->major;
99 if (minor_status != NULL)
100 *minor_status = ctxt->minor;
103 /* The GSSAPI error */
105 gss_display_status(&lmin, ctxt->major,
106 GSS_C_GSS_CODE, GSS_C_NULL_OID, &ctx, &msg);
108 buffer_append(&b, msg.value, msg.length);
109 buffer_put_char(&b, '\n');
111 gss_release_buffer(&lmin, &msg);
114 /* The mechanism specific error */
116 gss_display_status(&lmin, ctxt->minor,
117 GSS_C_MECH_CODE, GSS_C_NULL_OID, &ctx, &msg);
119 buffer_append(&b, msg.value, msg.length);
120 buffer_put_char(&b, '\n');
122 gss_release_buffer(&lmin, &msg);
125 buffer_put_char(&b, '\0');
126 ret = xmalloc(buffer_len(&b));
127 buffer_get(&b, ret, buffer_len(&b));
133 * Initialise our GSSAPI context. We use this opaque structure to contain all
134 * of the data which both the client and server need to persist across
135 * {accept,init}_sec_context calls, so that when we do it from the userauth
136 * stuff life is a little easier
139 ssh_gssapi_build_ctx(Gssctxt **ctx)
141 *ctx = xcalloc(1, sizeof (Gssctxt));
142 (*ctx)->context = GSS_C_NO_CONTEXT;
143 (*ctx)->name = GSS_C_NO_NAME;
144 (*ctx)->oid = GSS_C_NO_OID;
145 (*ctx)->creds = GSS_C_NO_CREDENTIAL;
146 (*ctx)->client = GSS_C_NO_NAME;
147 (*ctx)->client_creds = GSS_C_NO_CREDENTIAL;
150 /* Delete our context, providing it has been built correctly */
152 ssh_gssapi_delete_ctx(Gssctxt **ctx)
158 if ((*ctx)->context != GSS_C_NO_CONTEXT)
159 gss_delete_sec_context(&ms, &(*ctx)->context, GSS_C_NO_BUFFER);
160 if ((*ctx)->name != GSS_C_NO_NAME)
161 gss_release_name(&ms, &(*ctx)->name);
162 if ((*ctx)->oid != GSS_C_NO_OID) {
163 xfree((*ctx)->oid->elements);
165 (*ctx)->oid = GSS_C_NO_OID;
167 if ((*ctx)->creds != GSS_C_NO_CREDENTIAL)
168 gss_release_cred(&ms, &(*ctx)->creds);
169 if ((*ctx)->client != GSS_C_NO_NAME)
170 gss_release_name(&ms, &(*ctx)->client);
171 if ((*ctx)->client_creds != GSS_C_NO_CREDENTIAL)
172 gss_release_cred(&ms, &(*ctx)->client_creds);
179 * Wrapper to init_sec_context
180 * Requires that the context contains:
182 * server name (from ssh_gssapi_import_name)
185 ssh_gssapi_init_ctx(Gssctxt *ctx, int deleg_creds, gss_buffer_desc *recv_tok,
186 gss_buffer_desc* send_tok, OM_uint32 *flags)
191 deleg_flag = GSS_C_DELEG_FLAG;
192 debug("Delegating credentials");
195 ctx->major = gss_init_sec_context(&ctx->minor,
196 GSS_C_NO_CREDENTIAL, &ctx->context, ctx->name, ctx->oid,
197 GSS_C_MUTUAL_FLAG | GSS_C_INTEG_FLAG | deleg_flag,
198 0, NULL, recv_tok, NULL, send_tok, flags, NULL);
200 if (GSS_ERROR(ctx->major))
201 ssh_gssapi_error(ctx);
206 /* Create a service name for the given host */
208 ssh_gssapi_import_name(Gssctxt *ctx, const char *host)
210 gss_buffer_desc gssbuf;
213 xasprintf(&val, "host@%s", host);
215 gssbuf.length = strlen(gssbuf.value);
217 if ((ctx->major = gss_import_name(&ctx->minor,
218 &gssbuf, GSS_C_NT_HOSTBASED_SERVICE, &ctx->name)))
219 ssh_gssapi_error(ctx);
225 /* Acquire credentials for a server running on the current host.
226 * Requires that the context structure contains a valid OID
229 /* Returns a GSSAPI error code */
231 ssh_gssapi_acquire_cred(Gssctxt *ctx)
234 char lname[MAXHOSTNAMELEN];
237 gss_create_empty_oid_set(&status, &oidset);
238 gss_add_oid_set_member(&status, ctx->oid, &oidset);
240 if (gethostname(lname, MAXHOSTNAMELEN)) {
241 gss_release_oid_set(&status, &oidset);
245 if (GSS_ERROR(ssh_gssapi_import_name(ctx, lname))) {
246 gss_release_oid_set(&status, &oidset);
250 if ((ctx->major = gss_acquire_cred(&ctx->minor,
251 ctx->name, 0, oidset, GSS_C_ACCEPT, &ctx->creds, NULL, NULL)))
252 ssh_gssapi_error(ctx);
254 gss_release_oid_set(&status, &oidset);
259 ssh_gssapi_sign(Gssctxt *ctx, gss_buffer_t buffer, gss_buffer_t hash)
261 if ((ctx->major = gss_get_mic(&ctx->minor, ctx->context,
262 GSS_C_QOP_DEFAULT, buffer, hash)))
263 ssh_gssapi_error(ctx);
269 ssh_gssapi_buildmic(Buffer *b, const char *user, const char *service,
273 buffer_put_string(b, session_id2, session_id2_len);
274 buffer_put_char(b, SSH2_MSG_USERAUTH_REQUEST);
275 buffer_put_cstring(b, user);
276 buffer_put_cstring(b, service);
277 buffer_put_cstring(b, context);
281 ssh_gssapi_server_ctx(Gssctxt **ctx, gss_OID oid)
284 ssh_gssapi_delete_ctx(ctx);
285 ssh_gssapi_build_ctx(ctx);
286 ssh_gssapi_set_oid(*ctx, oid);
287 return (ssh_gssapi_acquire_cred(*ctx));