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