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