blob: 57f12a2dc8e581d90a2ab45dbda0bd505cc145f4 [file] [log] [blame]
Damien Miller76758b62006-08-30 11:08:04 +10001/* $OpenBSD: gss-genr.c,v 1.17 2006/08/29 12:02:30 dtucker Exp $ */
Darren Tucker0efd1552003-08-26 11:49:55 +10002
3/*
Damien Millera1cb9f32006-08-19 00:33:34 +10004 * Copyright (c) 2001-2006 Simon Wilkinson. All rights reserved.
Darren Tucker0efd1552003-08-26 11:49:55 +10005 *
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
Damien Millerd7834352006-08-05 12:39:39 +100031#include <sys/types.h>
Damien Miller8dbffe72006-08-05 11:02:17 +100032#include <sys/param.h>
33
Damien Millerd7834352006-08-05 12:39:39 +100034#include <stdarg.h>
Damien Millere3476ed2006-07-24 14:13:33 +100035#include <string.h>
Damien Millerb8fe89c2006-07-24 14:51:00 +100036#include <unistd.h>
Damien Millere3476ed2006-07-24 14:13:33 +100037
Darren Tucker0efd1552003-08-26 11:49:55 +100038#include "xmalloc.h"
Damien Millerd7834352006-08-05 12:39:39 +100039#include "buffer.h"
Darren Tucker0efd1552003-08-26 11:49:55 +100040#include "log.h"
Damien Miller0425d402003-11-17 22:18:21 +110041#include "ssh2.h"
Darren Tucker0efd1552003-08-26 11:49:55 +100042
43#include "ssh-gss.h"
44
Damien Miller0425d402003-11-17 22:18:21 +110045extern u_char *session_id2;
46extern u_int session_id2_len;
Darren Tucker0efd1552003-08-26 11:49:55 +100047
48/* Check that the OID in a data stream matches that in the context */
49int
50ssh_gssapi_check_oid(Gssctxt *ctx, void *data, size_t len)
51{
52 return (ctx != NULL && ctx->oid != GSS_C_NO_OID &&
53 ctx->oid->length == len &&
54 memcmp(ctx->oid->elements, data, len) == 0);
55}
56
57/* Set the contexts OID from a data stream */
58void
59ssh_gssapi_set_oid_data(Gssctxt *ctx, void *data, size_t len)
60{
61 if (ctx->oid != GSS_C_NO_OID) {
62 xfree(ctx->oid->elements);
63 xfree(ctx->oid);
64 }
65 ctx->oid = xmalloc(sizeof(gss_OID_desc));
66 ctx->oid->length = len;
67 ctx->oid->elements = xmalloc(len);
68 memcpy(ctx->oid->elements, data, len);
69}
70
71/* Set the contexts OID */
72void
73ssh_gssapi_set_oid(Gssctxt *ctx, gss_OID oid)
74{
75 ssh_gssapi_set_oid_data(ctx, oid->elements, oid->length);
76}
77
78/* All this effort to report an error ... */
79void
80ssh_gssapi_error(Gssctxt *ctxt)
81{
Damien Millera66cf682006-03-26 00:05:23 +110082 char *s;
83
84 s = ssh_gssapi_last_error(ctxt, NULL, NULL);
85 debug("%s", s);
86 xfree(s);
Darren Tucker0efd1552003-08-26 11:49:55 +100087}
88
89char *
Damien Miller0dc1bef2005-07-17 17:22:45 +100090ssh_gssapi_last_error(Gssctxt *ctxt, OM_uint32 *major_status,
91 OM_uint32 *minor_status)
Darren Tucker0efd1552003-08-26 11:49:55 +100092{
93 OM_uint32 lmin;
94 gss_buffer_desc msg = GSS_C_EMPTY_BUFFER;
95 OM_uint32 ctx;
96 Buffer b;
97 char *ret;
98
99 buffer_init(&b);
100
101 if (major_status != NULL)
102 *major_status = ctxt->major;
103 if (minor_status != NULL)
104 *minor_status = ctxt->minor;
105
106 ctx = 0;
107 /* The GSSAPI error */
108 do {
109 gss_display_status(&lmin, ctxt->major,
110 GSS_C_GSS_CODE, GSS_C_NULL_OID, &ctx, &msg);
111
112 buffer_append(&b, msg.value, msg.length);
113 buffer_put_char(&b, '\n');
114
115 gss_release_buffer(&lmin, &msg);
116 } while (ctx != 0);
117
118 /* The mechanism specific error */
119 do {
120 gss_display_status(&lmin, ctxt->minor,
121 GSS_C_MECH_CODE, GSS_C_NULL_OID, &ctx, &msg);
122
123 buffer_append(&b, msg.value, msg.length);
124 buffer_put_char(&b, '\n');
125
126 gss_release_buffer(&lmin, &msg);
127 } while (ctx != 0);
128
129 buffer_put_char(&b, '\0');
130 ret = xmalloc(buffer_len(&b));
131 buffer_get(&b, ret, buffer_len(&b));
132 buffer_free(&b);
133 return (ret);
134}
135
136/*
137 * Initialise our GSSAPI context. We use this opaque structure to contain all
138 * of the data which both the client and server need to persist across
139 * {accept,init}_sec_context calls, so that when we do it from the userauth
140 * stuff life is a little easier
141 */
142void
143ssh_gssapi_build_ctx(Gssctxt **ctx)
144{
Damien Miller07d86be2006-03-26 14:19:21 +1100145 *ctx = xcalloc(1, sizeof (Gssctxt));
Darren Tucker0efd1552003-08-26 11:49:55 +1000146 (*ctx)->context = GSS_C_NO_CONTEXT;
147 (*ctx)->name = GSS_C_NO_NAME;
148 (*ctx)->oid = GSS_C_NO_OID;
149 (*ctx)->creds = GSS_C_NO_CREDENTIAL;
150 (*ctx)->client = GSS_C_NO_NAME;
151 (*ctx)->client_creds = GSS_C_NO_CREDENTIAL;
152}
153
154/* Delete our context, providing it has been built correctly */
155void
156ssh_gssapi_delete_ctx(Gssctxt **ctx)
157{
158 OM_uint32 ms;
159
160 if ((*ctx) == NULL)
161 return;
162 if ((*ctx)->context != GSS_C_NO_CONTEXT)
163 gss_delete_sec_context(&ms, &(*ctx)->context, GSS_C_NO_BUFFER);
164 if ((*ctx)->name != GSS_C_NO_NAME)
165 gss_release_name(&ms, &(*ctx)->name);
166 if ((*ctx)->oid != GSS_C_NO_OID) {
167 xfree((*ctx)->oid->elements);
168 xfree((*ctx)->oid);
169 (*ctx)->oid = GSS_C_NO_OID;
170 }
171 if ((*ctx)->creds != GSS_C_NO_CREDENTIAL)
172 gss_release_cred(&ms, &(*ctx)->creds);
173 if ((*ctx)->client != GSS_C_NO_NAME)
174 gss_release_name(&ms, &(*ctx)->client);
175 if ((*ctx)->client_creds != GSS_C_NO_CREDENTIAL)
176 gss_release_cred(&ms, &(*ctx)->client_creds);
177
178 xfree(*ctx);
179 *ctx = NULL;
180}
181
182/*
183 * Wrapper to init_sec_context
184 * Requires that the context contains:
185 * oid
186 * server name (from ssh_gssapi_import_name)
187 */
188OM_uint32
189ssh_gssapi_init_ctx(Gssctxt *ctx, int deleg_creds, gss_buffer_desc *recv_tok,
190 gss_buffer_desc* send_tok, OM_uint32 *flags)
191{
192 int deleg_flag = 0;
193
194 if (deleg_creds) {
195 deleg_flag = GSS_C_DELEG_FLAG;
196 debug("Delegating credentials");
197 }
198
199 ctx->major = gss_init_sec_context(&ctx->minor,
200 GSS_C_NO_CREDENTIAL, &ctx->context, ctx->name, ctx->oid,
201 GSS_C_MUTUAL_FLAG | GSS_C_INTEG_FLAG | deleg_flag,
202 0, NULL, recv_tok, NULL, send_tok, flags, NULL);
203
204 if (GSS_ERROR(ctx->major))
205 ssh_gssapi_error(ctx);
206
207 return (ctx->major);
208}
209
210/* Create a service name for the given host */
211OM_uint32
212ssh_gssapi_import_name(Gssctxt *ctx, const char *host)
213{
214 gss_buffer_desc gssbuf;
Damien Miller63e437f2006-04-23 12:05:46 +1000215 char *val;
Darren Tucker0efd1552003-08-26 11:49:55 +1000216
Damien Miller63e437f2006-04-23 12:05:46 +1000217 xasprintf(&val, "host@%s", host);
218 gssbuf.value = val;
219 gssbuf.length = strlen(gssbuf.value);
Darren Tucker0efd1552003-08-26 11:49:55 +1000220
221 if ((ctx->major = gss_import_name(&ctx->minor,
222 &gssbuf, GSS_C_NT_HOSTBASED_SERVICE, &ctx->name)))
223 ssh_gssapi_error(ctx);
224
225 xfree(gssbuf.value);
226 return (ctx->major);
227}
228
229/* Acquire credentials for a server running on the current host.
230 * Requires that the context structure contains a valid OID
231 */
232
233/* Returns a GSSAPI error code */
234OM_uint32
235ssh_gssapi_acquire_cred(Gssctxt *ctx)
236{
237 OM_uint32 status;
238 char lname[MAXHOSTNAMELEN];
239 gss_OID_set oidset;
240
241 gss_create_empty_oid_set(&status, &oidset);
242 gss_add_oid_set_member(&status, ctx->oid, &oidset);
243
Damien Millera66cf682006-03-26 00:05:23 +1100244 if (gethostname(lname, MAXHOSTNAMELEN)) {
245 gss_release_oid_set(&status, &oidset);
Darren Tucker0efd1552003-08-26 11:49:55 +1000246 return (-1);
Damien Millera66cf682006-03-26 00:05:23 +1100247 }
Darren Tucker0efd1552003-08-26 11:49:55 +1000248
Damien Millera66cf682006-03-26 00:05:23 +1100249 if (GSS_ERROR(ssh_gssapi_import_name(ctx, lname))) {
250 gss_release_oid_set(&status, &oidset);
Darren Tucker0efd1552003-08-26 11:49:55 +1000251 return (ctx->major);
Damien Millera66cf682006-03-26 00:05:23 +1100252 }
Darren Tucker0efd1552003-08-26 11:49:55 +1000253
254 if ((ctx->major = gss_acquire_cred(&ctx->minor,
255 ctx->name, 0, oidset, GSS_C_ACCEPT, &ctx->creds, NULL, NULL)))
256 ssh_gssapi_error(ctx);
257
258 gss_release_oid_set(&status, &oidset);
259 return (ctx->major);
260}
261
262OM_uint32
Damien Miller0425d402003-11-17 22:18:21 +1100263ssh_gssapi_sign(Gssctxt *ctx, gss_buffer_t buffer, gss_buffer_t hash)
264{
265 if ((ctx->major = gss_get_mic(&ctx->minor, ctx->context,
266 GSS_C_QOP_DEFAULT, buffer, hash)))
267 ssh_gssapi_error(ctx);
Damien Miller787b2ec2003-11-21 23:56:47 +1100268
Damien Miller0425d402003-11-17 22:18:21 +1100269 return (ctx->major);
270}
271
272void
Damien Millera8e06ce2003-11-21 23:48:55 +1100273ssh_gssapi_buildmic(Buffer *b, const char *user, const char *service,
274 const char *context)
Damien Miller787b2ec2003-11-21 23:56:47 +1100275{
Damien Miller0425d402003-11-17 22:18:21 +1100276 buffer_init(b);
277 buffer_put_string(b, session_id2, session_id2_len);
278 buffer_put_char(b, SSH2_MSG_USERAUTH_REQUEST);
279 buffer_put_cstring(b, user);
280 buffer_put_cstring(b, service);
281 buffer_put_cstring(b, context);
282}
283
284OM_uint32
Damien Miller6fd6def2005-11-05 15:07:05 +1100285ssh_gssapi_server_ctx(Gssctxt **ctx, gss_OID oid)
286{
Darren Tucker0efd1552003-08-26 11:49:55 +1000287 if (*ctx)
288 ssh_gssapi_delete_ctx(ctx);
289 ssh_gssapi_build_ctx(ctx);
290 ssh_gssapi_set_oid(*ctx, oid);
291 return (ssh_gssapi_acquire_cred(*ctx));
292}
293
Damien Millera1cb9f32006-08-19 00:33:34 +1000294int
Damien Miller3d2d6e92006-08-19 00:46:43 +1000295ssh_gssapi_check_mechanism(Gssctxt **ctx, gss_OID oid, const char *host)
Damien Millera1cb9f32006-08-19 00:33:34 +1000296{
297 gss_buffer_desc token = GSS_C_EMPTY_BUFFER;
298 OM_uint32 major, minor;
299 gss_OID_desc spnego_oid = {6, (void *)"\x2B\x06\x01\x05\x05\x02"};
300
301 /* RFC 4462 says we MUST NOT do SPNEGO */
302 if (oid->length == spnego_oid.length &&
303 (memcmp(oid->elements, spnego_oid.elements, oid->length) == 0))
Damien Millerdeccaa72006-08-19 08:50:57 +1000304 return 0; /* false */
Damien Millera1cb9f32006-08-19 00:33:34 +1000305
306 ssh_gssapi_build_ctx(ctx);
307 ssh_gssapi_set_oid(*ctx, oid);
308 major = ssh_gssapi_import_name(*ctx, host);
309 if (!GSS_ERROR(major)) {
310 major = ssh_gssapi_init_ctx(*ctx, 0, GSS_C_NO_BUFFER, &token,
311 NULL);
312 gss_release_buffer(&minor, &token);
Damien Miller76758b62006-08-30 11:08:04 +1000313 if ((*ctx)->context != GSS_C_NO_CONTEXT)
314 gss_delete_sec_context(&minor, &(*ctx)->context,
315 GSS_C_NO_BUFFER);
Damien Millera1cb9f32006-08-19 00:33:34 +1000316 }
317
318 if (GSS_ERROR(major))
319 ssh_gssapi_delete_ctx(ctx);
320
321 return (!GSS_ERROR(major));
322}
323
Darren Tucker0efd1552003-08-26 11:49:55 +1000324#endif /* GSSAPI */