]> andersk Git - openssh.git/blob - gss-genr.c
- djm@cvs.openbsd.org 2006/03/20 04:07:49
[openssh.git] / gss-genr.c
1 /*      $OpenBSD: gss-genr.c,v 1.7 2006/03/20 04:07:49 djm Exp $        */
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
31 #include "xmalloc.h"
32 #include "bufaux.h"
33 #include "log.h"
34 #include "ssh2.h"
35
36 #include "ssh-gss.h"
37
38 extern u_char *session_id2;
39 extern u_int session_id2_len;
40
41 /* Check that the OID in a data stream matches that in the context */
42 int
43 ssh_gssapi_check_oid(Gssctxt *ctx, void *data, size_t len)
44 {
45         return (ctx != NULL && ctx->oid != GSS_C_NO_OID &&
46             ctx->oid->length == len &&
47             memcmp(ctx->oid->elements, data, len) == 0);
48 }
49
50 /* Set the contexts OID from a data stream */
51 void
52 ssh_gssapi_set_oid_data(Gssctxt *ctx, void *data, size_t len)
53 {
54         if (ctx->oid != GSS_C_NO_OID) {
55                 xfree(ctx->oid->elements);
56                 xfree(ctx->oid);
57         }
58         ctx->oid = xmalloc(sizeof(gss_OID_desc));
59         ctx->oid->length = len;
60         ctx->oid->elements = xmalloc(len);
61         memcpy(ctx->oid->elements, data, len);
62 }
63
64 /* Set the contexts OID */
65 void
66 ssh_gssapi_set_oid(Gssctxt *ctx, gss_OID oid)
67 {
68         ssh_gssapi_set_oid_data(ctx, oid->elements, oid->length);
69 }
70
71 /* All this effort to report an error ... */
72 void
73 ssh_gssapi_error(Gssctxt *ctxt)
74 {
75         char *s;
76
77         s = ssh_gssapi_last_error(ctxt, NULL, NULL);
78         debug("%s", s);
79         xfree(s);
80 }
81
82 char *
83 ssh_gssapi_last_error(Gssctxt *ctxt, OM_uint32 *major_status,
84     OM_uint32 *minor_status)
85 {
86         OM_uint32 lmin;
87         gss_buffer_desc msg = GSS_C_EMPTY_BUFFER;
88         OM_uint32 ctx;
89         Buffer b;
90         char *ret;
91
92         buffer_init(&b);
93
94         if (major_status != NULL)
95                 *major_status = ctxt->major;
96         if (minor_status != NULL)
97                 *minor_status = ctxt->minor;
98
99         ctx = 0;
100         /* The GSSAPI error */
101         do {
102                 gss_display_status(&lmin, ctxt->major,
103                     GSS_C_GSS_CODE, GSS_C_NULL_OID, &ctx, &msg);
104
105                 buffer_append(&b, msg.value, msg.length);
106                 buffer_put_char(&b, '\n');
107
108                 gss_release_buffer(&lmin, &msg);
109         } while (ctx != 0);
110
111         /* The mechanism specific error */
112         do {
113                 gss_display_status(&lmin, ctxt->minor,
114                     GSS_C_MECH_CODE, GSS_C_NULL_OID, &ctx, &msg);
115
116                 buffer_append(&b, msg.value, msg.length);
117                 buffer_put_char(&b, '\n');
118
119                 gss_release_buffer(&lmin, &msg);
120         } while (ctx != 0);
121
122         buffer_put_char(&b, '\0');
123         ret = xmalloc(buffer_len(&b));
124         buffer_get(&b, ret, buffer_len(&b));
125         buffer_free(&b);
126         return (ret);
127 }
128
129 /*
130  * Initialise our GSSAPI context. We use this opaque structure to contain all
131  * of the data which both the client and server need to persist across
132  * {accept,init}_sec_context calls, so that when we do it from the userauth
133  * stuff life is a little easier
134  */
135 void
136 ssh_gssapi_build_ctx(Gssctxt **ctx)
137 {
138         *ctx = xmalloc(sizeof (Gssctxt));
139         (*ctx)->major = 0;
140         (*ctx)->minor = 0;
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 */
150 void
151 ssh_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  */
183 OM_uint32
184 ssh_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 */
206 OM_uint32
207 ssh_gssapi_import_name(Gssctxt *ctx, const char *host)
208 {
209         gss_buffer_desc gssbuf;
210
211         gssbuf.length = sizeof("host@") + strlen(host);
212         gssbuf.value = xmalloc(gssbuf.length);
213         snprintf(gssbuf.value, gssbuf.length, "host@%s", host);
214
215         if ((ctx->major = gss_import_name(&ctx->minor,
216             &gssbuf, GSS_C_NT_HOSTBASED_SERVICE, &ctx->name)))
217                 ssh_gssapi_error(ctx);
218
219         xfree(gssbuf.value);
220         return (ctx->major);
221 }
222
223 /* Acquire credentials for a server running on the current host.
224  * Requires that the context structure contains a valid OID
225  */
226
227 /* Returns a GSSAPI error code */
228 OM_uint32
229 ssh_gssapi_acquire_cred(Gssctxt *ctx)
230 {
231         OM_uint32 status;
232         char lname[MAXHOSTNAMELEN];
233         gss_OID_set oidset;
234
235         gss_create_empty_oid_set(&status, &oidset);
236         gss_add_oid_set_member(&status, ctx->oid, &oidset);
237
238         if (gethostname(lname, MAXHOSTNAMELEN)) {
239                 gss_release_oid_set(&status, &oidset);
240                 return (-1);
241         }
242
243         if (GSS_ERROR(ssh_gssapi_import_name(ctx, lname))) {
244                 gss_release_oid_set(&status, &oidset);
245                 return (ctx->major);
246         }
247
248         if ((ctx->major = gss_acquire_cred(&ctx->minor,
249             ctx->name, 0, oidset, GSS_C_ACCEPT, &ctx->creds, NULL, NULL)))
250                 ssh_gssapi_error(ctx);
251
252         gss_release_oid_set(&status, &oidset);
253         return (ctx->major);
254 }
255
256 OM_uint32
257 ssh_gssapi_sign(Gssctxt *ctx, gss_buffer_t buffer, gss_buffer_t hash)
258 {
259         if ((ctx->major = gss_get_mic(&ctx->minor, ctx->context,
260             GSS_C_QOP_DEFAULT, buffer, hash)))
261                 ssh_gssapi_error(ctx);
262
263         return (ctx->major);
264 }
265
266 void
267 ssh_gssapi_buildmic(Buffer *b, const char *user, const char *service,
268     const char *context)
269 {
270         buffer_init(b);
271         buffer_put_string(b, session_id2, session_id2_len);
272         buffer_put_char(b, SSH2_MSG_USERAUTH_REQUEST);
273         buffer_put_cstring(b, user);
274         buffer_put_cstring(b, service);
275         buffer_put_cstring(b, context);
276 }
277
278 OM_uint32
279 ssh_gssapi_server_ctx(Gssctxt **ctx, gss_OID oid)
280 {
281         if (*ctx)
282                 ssh_gssapi_delete_ctx(ctx);
283         ssh_gssapi_build_ctx(ctx);
284         ssh_gssapi_set_oid(*ctx, oid);
285         return (ssh_gssapi_acquire_cred(*ctx));
286 }
287
288 #endif /* GSSAPI */
This page took 1.230041 seconds and 5 git commands to generate.