]> andersk Git - openssh.git/blame - gss-genr.c
- stevesk@cvs.openbsd.org 2006/07/23 01:11:05
[openssh.git] / gss-genr.c
CommitLineData
00146caa 1/* $OpenBSD: gss-genr.c,v 1.11 2006/07/22 20:48:23 stevesk Exp $ */
7364bd04 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 "includes.h"
28
29#ifdef GSSAPI
30
00146caa 31#include <string.h>
32
7364bd04 33#include "xmalloc.h"
34#include "bufaux.h"
7364bd04 35#include "log.h"
0789992b 36#include "ssh2.h"
7364bd04 37
38#include "ssh-gss.h"
39
0789992b 40extern u_char *session_id2;
41extern u_int session_id2_len;
7364bd04 42
43/* Check that the OID in a data stream matches that in the context */
44int
45ssh_gssapi_check_oid(Gssctxt *ctx, void *data, size_t len)
46{
47 return (ctx != NULL && ctx->oid != GSS_C_NO_OID &&
48 ctx->oid->length == len &&
49 memcmp(ctx->oid->elements, data, len) == 0);
50}
51
52/* Set the contexts OID from a data stream */
53void
54ssh_gssapi_set_oid_data(Gssctxt *ctx, void *data, size_t len)
55{
56 if (ctx->oid != GSS_C_NO_OID) {
57 xfree(ctx->oid->elements);
58 xfree(ctx->oid);
59 }
60 ctx->oid = xmalloc(sizeof(gss_OID_desc));
61 ctx->oid->length = len;
62 ctx->oid->elements = xmalloc(len);
63 memcpy(ctx->oid->elements, data, len);
64}
65
66/* Set the contexts OID */
67void
68ssh_gssapi_set_oid(Gssctxt *ctx, gss_OID oid)
69{
70 ssh_gssapi_set_oid_data(ctx, oid->elements, oid->length);
71}
72
73/* All this effort to report an error ... */
74void
75ssh_gssapi_error(Gssctxt *ctxt)
76{
0926fd19 77 char *s;
78
79 s = ssh_gssapi_last_error(ctxt, NULL, NULL);
80 debug("%s", s);
81 xfree(s);
7364bd04 82}
83
84char *
4e2e5cfd 85ssh_gssapi_last_error(Gssctxt *ctxt, OM_uint32 *major_status,
86 OM_uint32 *minor_status)
7364bd04 87{
88 OM_uint32 lmin;
89 gss_buffer_desc msg = GSS_C_EMPTY_BUFFER;
90 OM_uint32 ctx;
91 Buffer b;
92 char *ret;
93
94 buffer_init(&b);
95
96 if (major_status != NULL)
97 *major_status = ctxt->major;
98 if (minor_status != NULL)
99 *minor_status = ctxt->minor;
100
101 ctx = 0;
102 /* The GSSAPI error */
103 do {
104 gss_display_status(&lmin, ctxt->major,
105 GSS_C_GSS_CODE, GSS_C_NULL_OID, &ctx, &msg);
106
107 buffer_append(&b, msg.value, msg.length);
108 buffer_put_char(&b, '\n');
109
110 gss_release_buffer(&lmin, &msg);
111 } while (ctx != 0);
112
113 /* The mechanism specific error */
114 do {
115 gss_display_status(&lmin, ctxt->minor,
116 GSS_C_MECH_CODE, GSS_C_NULL_OID, &ctx, &msg);
117
118 buffer_append(&b, msg.value, msg.length);
119 buffer_put_char(&b, '\n');
120
121 gss_release_buffer(&lmin, &msg);
122 } while (ctx != 0);
123
124 buffer_put_char(&b, '\0');
125 ret = xmalloc(buffer_len(&b));
126 buffer_get(&b, ret, buffer_len(&b));
127 buffer_free(&b);
128 return (ret);
129}
130
131/*
132 * Initialise our GSSAPI context. We use this opaque structure to contain all
133 * of the data which both the client and server need to persist across
134 * {accept,init}_sec_context calls, so that when we do it from the userauth
135 * stuff life is a little easier
136 */
137void
138ssh_gssapi_build_ctx(Gssctxt **ctx)
139{
52e3daed 140 *ctx = xcalloc(1, sizeof (Gssctxt));
7364bd04 141 (*ctx)->context = GSS_C_NO_CONTEXT;
142 (*ctx)->name = GSS_C_NO_NAME;
143 (*ctx)->oid = GSS_C_NO_OID;
144 (*ctx)->creds = GSS_C_NO_CREDENTIAL;
145 (*ctx)->client = GSS_C_NO_NAME;
146 (*ctx)->client_creds = GSS_C_NO_CREDENTIAL;
147}
148
149/* Delete our context, providing it has been built correctly */
150void
151ssh_gssapi_delete_ctx(Gssctxt **ctx)
152{
153 OM_uint32 ms;
154
155 if ((*ctx) == NULL)
156 return;
157 if ((*ctx)->context != GSS_C_NO_CONTEXT)
158 gss_delete_sec_context(&ms, &(*ctx)->context, GSS_C_NO_BUFFER);
159 if ((*ctx)->name != GSS_C_NO_NAME)
160 gss_release_name(&ms, &(*ctx)->name);
161 if ((*ctx)->oid != GSS_C_NO_OID) {
162 xfree((*ctx)->oid->elements);
163 xfree((*ctx)->oid);
164 (*ctx)->oid = GSS_C_NO_OID;
165 }
166 if ((*ctx)->creds != GSS_C_NO_CREDENTIAL)
167 gss_release_cred(&ms, &(*ctx)->creds);
168 if ((*ctx)->client != GSS_C_NO_NAME)
169 gss_release_name(&ms, &(*ctx)->client);
170 if ((*ctx)->client_creds != GSS_C_NO_CREDENTIAL)
171 gss_release_cred(&ms, &(*ctx)->client_creds);
172
173 xfree(*ctx);
174 *ctx = NULL;
175}
176
177/*
178 * Wrapper to init_sec_context
179 * Requires that the context contains:
180 * oid
181 * server name (from ssh_gssapi_import_name)
182 */
183OM_uint32
184ssh_gssapi_init_ctx(Gssctxt *ctx, int deleg_creds, gss_buffer_desc *recv_tok,
185 gss_buffer_desc* send_tok, OM_uint32 *flags)
186{
187 int deleg_flag = 0;
188
189 if (deleg_creds) {
190 deleg_flag = GSS_C_DELEG_FLAG;
191 debug("Delegating credentials");
192 }
193
194 ctx->major = gss_init_sec_context(&ctx->minor,
195 GSS_C_NO_CREDENTIAL, &ctx->context, ctx->name, ctx->oid,
196 GSS_C_MUTUAL_FLAG | GSS_C_INTEG_FLAG | deleg_flag,
197 0, NULL, recv_tok, NULL, send_tok, flags, NULL);
198
199 if (GSS_ERROR(ctx->major))
200 ssh_gssapi_error(ctx);
201
202 return (ctx->major);
203}
204
205/* Create a service name for the given host */
206OM_uint32
207ssh_gssapi_import_name(Gssctxt *ctx, const char *host)
208{
209 gss_buffer_desc gssbuf;
9c3c8eb1 210 char *val;
7364bd04 211
9c3c8eb1 212 xasprintf(&val, "host@%s", host);
213 gssbuf.value = val;
214 gssbuf.length = strlen(gssbuf.value);
7364bd04 215
216 if ((ctx->major = gss_import_name(&ctx->minor,
217 &gssbuf, GSS_C_NT_HOSTBASED_SERVICE, &ctx->name)))
218 ssh_gssapi_error(ctx);
219
220 xfree(gssbuf.value);
221 return (ctx->major);
222}
223
224/* Acquire credentials for a server running on the current host.
225 * Requires that the context structure contains a valid OID
226 */
227
228/* Returns a GSSAPI error code */
229OM_uint32
230ssh_gssapi_acquire_cred(Gssctxt *ctx)
231{
232 OM_uint32 status;
233 char lname[MAXHOSTNAMELEN];
234 gss_OID_set oidset;
235
236 gss_create_empty_oid_set(&status, &oidset);
237 gss_add_oid_set_member(&status, ctx->oid, &oidset);
238
0926fd19 239 if (gethostname(lname, MAXHOSTNAMELEN)) {
240 gss_release_oid_set(&status, &oidset);
7364bd04 241 return (-1);
0926fd19 242 }
7364bd04 243
0926fd19 244 if (GSS_ERROR(ssh_gssapi_import_name(ctx, lname))) {
245 gss_release_oid_set(&status, &oidset);
7364bd04 246 return (ctx->major);
0926fd19 247 }
7364bd04 248
249 if ((ctx->major = gss_acquire_cred(&ctx->minor,
250 ctx->name, 0, oidset, GSS_C_ACCEPT, &ctx->creds, NULL, NULL)))
251 ssh_gssapi_error(ctx);
252
253 gss_release_oid_set(&status, &oidset);
254 return (ctx->major);
255}
256
0789992b 257OM_uint32
258ssh_gssapi_sign(Gssctxt *ctx, gss_buffer_t buffer, gss_buffer_t hash)
259{
260 if ((ctx->major = gss_get_mic(&ctx->minor, ctx->context,
261 GSS_C_QOP_DEFAULT, buffer, hash)))
262 ssh_gssapi_error(ctx);
b6453d99 263
0789992b 264 return (ctx->major);
265}
266
267void
aff51935 268ssh_gssapi_buildmic(Buffer *b, const char *user, const char *service,
269 const char *context)
b6453d99 270{
0789992b 271 buffer_init(b);
272 buffer_put_string(b, session_id2, session_id2_len);
273 buffer_put_char(b, SSH2_MSG_USERAUTH_REQUEST);
274 buffer_put_cstring(b, user);
275 buffer_put_cstring(b, service);
276 buffer_put_cstring(b, context);
277}
278
7364bd04 279OM_uint32
8442cc66 280ssh_gssapi_server_ctx(Gssctxt **ctx, gss_OID oid)
281{
7364bd04 282 if (*ctx)
283 ssh_gssapi_delete_ctx(ctx);
284 ssh_gssapi_build_ctx(ctx);
285 ssh_gssapi_set_oid(*ctx, oid);
286 return (ssh_gssapi_acquire_cred(*ctx));
287}
288
289#endif /* GSSAPI */
This page took 0.15155 seconds and 5 git commands to generate.