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