xref: /openbsd/usr.bin/ssh/gss-genr.c (revision 264ca280)
1 /* $OpenBSD: gss-genr.c,v 1.23 2015/01/20 23:14:00 deraadt Exp $ */
2 
3 /*
4  * Copyright (c) 2001-2007 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 #ifdef GSSAPI
28 
29 #include <sys/param.h>
30 
31 #include <stdarg.h>
32 #include <string.h>
33 #include <limits.h>
34 
35 #include "xmalloc.h"
36 #include "buffer.h"
37 #include "log.h"
38 #include "ssh2.h"
39 
40 #include "ssh-gss.h"
41 
42 extern u_char *session_id2;
43 extern u_int session_id2_len;
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 		free(ctx->oid->elements);
60 		free(ctx->oid);
61 	}
62 	ctx->oid = xcalloc(1, 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 {
79 	char *s;
80 
81 	s = ssh_gssapi_last_error(ctxt, NULL, NULL);
82 	debug("%s", s);
83 	free(s);
84 }
85 
86 char *
87 ssh_gssapi_last_error(Gssctxt *ctxt, OM_uint32 *major_status,
88     OM_uint32 *minor_status)
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, ctxt->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, ctxt->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 {
142 	*ctx = xcalloc(1, sizeof (Gssctxt));
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 		free((*ctx)->oid->elements);
165 		free((*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 	free(*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;
212 	char *val;
213 
214 	xasprintf(&val, "host@%s", host);
215 	gssbuf.value = val;
216 	gssbuf.length = strlen(gssbuf.value);
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 	free(gssbuf.value);
223 	return (ctx->major);
224 }
225 
226 OM_uint32
227 ssh_gssapi_sign(Gssctxt *ctx, gss_buffer_t buffer, gss_buffer_t hash)
228 {
229 	if ((ctx->major = gss_get_mic(&ctx->minor, ctx->context,
230 	    GSS_C_QOP_DEFAULT, buffer, hash)))
231 		ssh_gssapi_error(ctx);
232 
233 	return (ctx->major);
234 }
235 
236 void
237 ssh_gssapi_buildmic(Buffer *b, const char *user, const char *service,
238     const char *context)
239 {
240 	buffer_init(b);
241 	buffer_put_string(b, session_id2, session_id2_len);
242 	buffer_put_char(b, SSH2_MSG_USERAUTH_REQUEST);
243 	buffer_put_cstring(b, user);
244 	buffer_put_cstring(b, service);
245 	buffer_put_cstring(b, context);
246 }
247 
248 int
249 ssh_gssapi_check_mechanism(Gssctxt **ctx, gss_OID oid, const char *host)
250 {
251 	gss_buffer_desc token = GSS_C_EMPTY_BUFFER;
252 	OM_uint32 major, minor;
253 	gss_OID_desc spnego_oid = {6, (void *)"\x2B\x06\x01\x05\x05\x02"};
254 
255 	/* RFC 4462 says we MUST NOT do SPNEGO */
256 	if (oid->length == spnego_oid.length &&
257 	    (memcmp(oid->elements, spnego_oid.elements, oid->length) == 0))
258 		return 0; /* false */
259 
260 	ssh_gssapi_build_ctx(ctx);
261 	ssh_gssapi_set_oid(*ctx, oid);
262 	major = ssh_gssapi_import_name(*ctx, host);
263 	if (!GSS_ERROR(major)) {
264 		major = ssh_gssapi_init_ctx(*ctx, 0, GSS_C_NO_BUFFER, &token,
265 		    NULL);
266 		gss_release_buffer(&minor, &token);
267 		if ((*ctx)->context != GSS_C_NO_CONTEXT)
268 			gss_delete_sec_context(&minor, &(*ctx)->context,
269 			    GSS_C_NO_BUFFER);
270 	}
271 
272 	if (GSS_ERROR(major))
273 		ssh_gssapi_delete_ctx(ctx);
274 
275 	return (!GSS_ERROR(major));
276 }
277 
278 #endif /* GSSAPI */
279