1 /* $OpenBSD: gss-genr.c,v 1.4 2005/07/17 07:17:55 djm Exp $ */
4 * Copyright (c) 2001-2003 Simon Wilkinson. All rights reserved.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
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.
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.
35 #include <openssl/evp.h>
38 #include "monitor_wrap.h"
44 extern u_char *session_id2;
45 extern u_int session_id2_len;
50 } ssh_gss_kex_mapping;
52 static ssh_gss_kex_mapping *gss_enc2oid;
54 /* Return a list of the gss-group1-sha1-x mechanisms supported by this
57 * On the client side, we don't need to worry about whether we 'know'
58 * about the mechanism or not - we assume that any mechanism that we've been
59 * linked against is suitable for inclusion.
61 * XXX - We might want to make this configurable in the future, so as to
62 * XXX - allow the user control over which mechanisms to use.
66 ssh_gssapi_client_mechanisms(char *host) {
67 gss_OID_set supported;
74 unsigned char digest[EVP_MAX_MD_SIZE];
76 const EVP_MD *evp_md = EVP_md5();
80 gss_indicate_mechs(&min_status,&supported);
81 gss_enc2oid=xmalloc(sizeof(ssh_gss_kex_mapping)
82 *(supported->count+1));
86 for (i=0;i<supported->count;i++) {
88 gss_enc2oid[oidpos].encoded=NULL;
90 if (supported->elements[i].length<128 &&
91 ssh_gssapi_check_mechanism(&(supported->elements[i]),host)) {
93 /* Add the required DER encoding octets and MD5 hash */
94 deroid[0]=0x06; /* Object Identifier */
95 deroid[1]=supported->elements[i].length;
97 EVP_DigestInit(&md, evp_md);
98 EVP_DigestUpdate(&md,deroid,2);
100 supported->elements[i].elements,
101 supported->elements[i].length);
102 EVP_DigestFinal(&md, digest, NULL);
104 /* Base64 encode it */
105 encoded=xmalloc(EVP_MD_size(evp_md)*2);
106 enclen=__b64_ntop(digest, EVP_MD_size(evp_md),
107 encoded,EVP_MD_size(evp_md)*2);
109 buffer_put_char(&buf,',');
111 buffer_append(&buf, KEX_GSS_SHA1, sizeof(KEX_GSS_SHA1)-1);
112 buffer_append(&buf, encoded, enclen);
114 debug("Mechanism encoded as %s",encoded);
116 gss_enc2oid[oidpos].oid=&(supported->elements[i]);
117 gss_enc2oid[oidpos].encoded=encoded;
121 gss_enc2oid[oidpos].oid=NULL;
122 gss_enc2oid[oidpos].encoded=NULL;
124 buffer_put_char(&buf,'\0');
126 mechs=xmalloc(buffer_len(&buf));
127 buffer_get(&buf,mechs,buffer_len(&buf));
129 if (strlen(mechs)==0)
136 ssh_gssapi_client_id_kex(Gssctxt *ctx, char *name) {
139 if (strncmp(name, KEX_GSS_SHA1, sizeof(KEX_GSS_SHA1)-1) !=0) {
143 name+=sizeof(KEX_GSS_SHA1)-1; /* Move to the start of the ID string */
145 while (gss_enc2oid[i].encoded!=NULL &&
146 strcmp(name,gss_enc2oid[i].encoded)!=0) {
150 if (gss_enc2oid[i].oid!=NULL) {
151 ssh_gssapi_set_oid(ctx,gss_enc2oid[i].oid);
154 return gss_enc2oid[i].oid;
157 /* Check that the OID in a data stream matches that in the context */
159 ssh_gssapi_check_oid(Gssctxt *ctx, void *data, size_t len)
161 return (ctx != NULL && ctx->oid != GSS_C_NO_OID &&
162 ctx->oid->length == len &&
163 memcmp(ctx->oid->elements, data, len) == 0);
166 /* Set the contexts OID from a data stream */
168 ssh_gssapi_set_oid_data(Gssctxt *ctx, void *data, size_t len)
170 if (ctx->oid != GSS_C_NO_OID) {
171 xfree(ctx->oid->elements);
174 ctx->oid = xmalloc(sizeof(gss_OID_desc));
175 ctx->oid->length = len;
176 ctx->oid->elements = xmalloc(len);
177 memcpy(ctx->oid->elements, data, len);
180 /* Set the contexts OID */
182 ssh_gssapi_set_oid(Gssctxt *ctx, gss_OID oid)
184 ssh_gssapi_set_oid_data(ctx, oid->elements, oid->length);
187 /* All this effort to report an error ... */
189 ssh_gssapi_error(Gssctxt *ctxt)
191 debug("%s", ssh_gssapi_last_error(ctxt, NULL, NULL));
195 ssh_gssapi_last_error(Gssctxt *ctxt, OM_uint32 *major_status,
196 OM_uint32 *minor_status)
199 gss_buffer_desc msg = GSS_C_EMPTY_BUFFER;
206 if (major_status != NULL)
207 *major_status = ctxt->major;
208 if (minor_status != NULL)
209 *minor_status = ctxt->minor;
212 /* The GSSAPI error */
214 gss_display_status(&lmin, ctxt->major,
215 GSS_C_GSS_CODE, ctxt->oid, &ctx, &msg);
217 buffer_append(&b, msg.value, msg.length);
218 buffer_put_char(&b, '\n');
220 gss_release_buffer(&lmin, &msg);
223 /* The mechanism specific error */
225 gss_display_status(&lmin, ctxt->minor,
226 GSS_C_MECH_CODE, ctxt->oid, &ctx, &msg);
228 buffer_append(&b, msg.value, msg.length);
229 buffer_put_char(&b, '\n');
231 gss_release_buffer(&lmin, &msg);
234 buffer_put_char(&b, '\0');
235 ret = xmalloc(buffer_len(&b));
236 buffer_get(&b, ret, buffer_len(&b));
242 * Initialise our GSSAPI context. We use this opaque structure to contain all
243 * of the data which both the client and server need to persist across
244 * {accept,init}_sec_context calls, so that when we do it from the userauth
245 * stuff life is a little easier
248 ssh_gssapi_build_ctx(Gssctxt **ctx)
250 *ctx = xmalloc(sizeof (Gssctxt));
253 (*ctx)->context = GSS_C_NO_CONTEXT;
254 (*ctx)->name = GSS_C_NO_NAME;
255 (*ctx)->oid = GSS_C_NO_OID;
256 (*ctx)->creds = GSS_C_NO_CREDENTIAL;
257 (*ctx)->client = GSS_C_NO_NAME;
258 (*ctx)->client_creds = GSS_C_NO_CREDENTIAL;
261 /* Delete our context, providing it has been built correctly */
263 ssh_gssapi_delete_ctx(Gssctxt **ctx)
265 #if !defined(MECHGLUE)
271 #if !defined(MECHGLUE) /* mechglue has some memory management issues */
272 if ((*ctx)->context != GSS_C_NO_CONTEXT)
273 gss_delete_sec_context(&ms, &(*ctx)->context, GSS_C_NO_BUFFER);
274 if ((*ctx)->name != GSS_C_NO_NAME)
275 gss_release_name(&ms, &(*ctx)->name);
276 if ((*ctx)->oid != GSS_C_NO_OID) {
277 xfree((*ctx)->oid->elements);
279 (*ctx)->oid = GSS_C_NO_OID;
281 if ((*ctx)->creds != GSS_C_NO_CREDENTIAL)
282 gss_release_cred(&ms, &(*ctx)->creds);
283 if ((*ctx)->client != GSS_C_NO_NAME)
284 gss_release_name(&ms, &(*ctx)->client);
285 if ((*ctx)->client_creds != GSS_C_NO_CREDENTIAL)
286 gss_release_cred(&ms, &(*ctx)->client_creds);
294 * Wrapper to init_sec_context
295 * Requires that the context contains:
297 * server name (from ssh_gssapi_import_name)
300 ssh_gssapi_init_ctx(Gssctxt *ctx, int deleg_creds, gss_buffer_desc *recv_tok,
301 gss_buffer_desc* send_tok, OM_uint32 *flags)
306 deleg_flag = GSS_C_DELEG_FLAG;
307 debug("Delegating credentials");
310 ctx->major = gss_init_sec_context(&ctx->minor,
311 GSS_C_NO_CREDENTIAL, &ctx->context, ctx->name, ctx->oid,
312 GSS_C_MUTUAL_FLAG | GSS_C_INTEG_FLAG | deleg_flag,
313 0, NULL, recv_tok, NULL, send_tok, flags, NULL);
315 if (GSS_ERROR(ctx->major))
316 ssh_gssapi_error(ctx);
321 /* Create a service name for the given host */
323 ssh_gssapi_import_name(Gssctxt *ctx, const char *host)
325 gss_buffer_desc gssbuf;
328 /* Make a copy of the host name, in case it was returned by a
329 * previous call to gethostbyname(). */
330 xhost = xstrdup(host);
332 /* Make sure we have the FQDN. Some GSSAPI implementations don't do
333 * this for us themselves */
334 resolve_localhost(&xhost);
336 gssbuf.length = sizeof("host@") + strlen(xhost);
337 gssbuf.value = xmalloc(gssbuf.length);
338 snprintf(gssbuf.value, gssbuf.length, "host@%s", xhost);
340 if ((ctx->major = gss_import_name(&ctx->minor,
341 &gssbuf, GSS_C_NT_HOSTBASED_SERVICE, &ctx->name)))
342 ssh_gssapi_error(ctx);
349 /* Acquire credentials for a server running on the current host.
350 * Requires that the context structure contains a valid OID
353 /* Returns a GSSAPI error code */
355 ssh_gssapi_acquire_cred(Gssctxt *ctx)
358 char lname[MAXHOSTNAMELEN];
361 gss_create_empty_oid_set(&status, &oidset);
362 gss_add_oid_set_member(&status, ctx->oid, &oidset);
364 if (gethostname(lname, MAXHOSTNAMELEN))
367 if (GSS_ERROR(ssh_gssapi_import_name(ctx, lname)))
370 if ((ctx->major = gss_acquire_cred(&ctx->minor,
371 ctx->name, 0, oidset, GSS_C_ACCEPT, &ctx->creds, NULL, NULL)))
372 ssh_gssapi_error(ctx);
374 gss_release_oid_set(&status, &oidset);
379 ssh_gssapi_sign(Gssctxt *ctx, gss_buffer_t buffer, gss_buffer_t hash)
381 if ((ctx->major = gss_get_mic(&ctx->minor, ctx->context,
382 GSS_C_QOP_DEFAULT, buffer, hash)))
383 ssh_gssapi_error(ctx);
389 ssh_gssapi_buildmic(Buffer *b, const char *user, const char *service,
393 buffer_put_string(b, session_id2, session_id2_len);
394 buffer_put_char(b, SSH2_MSG_USERAUTH_REQUEST);
395 buffer_put_cstring(b, user);
396 buffer_put_cstring(b, service);
397 buffer_put_cstring(b, context);
401 ssh_gssapi_server_ctx(Gssctxt **ctx, gss_OID oid) {
403 ssh_gssapi_delete_ctx(ctx);
404 ssh_gssapi_build_ctx(ctx);
405 ssh_gssapi_set_oid(*ctx, oid);
406 return (ssh_gssapi_acquire_cred(*ctx));
410 ssh_gssapi_check_mechanism(gss_OID oid, const char *host) {
411 Gssctxt * ctx = NULL;
412 gss_buffer_desc token;
413 OM_uint32 major,minor;
415 ssh_gssapi_build_ctx(&ctx);
416 ssh_gssapi_set_oid(ctx,oid);
417 ssh_gssapi_import_name(ctx,host);
418 major=ssh_gssapi_init_ctx(ctx,0, GSS_C_NO_BUFFER, &token, NULL);
419 gss_release_buffer(&minor,&token);
420 ssh_gssapi_delete_ctx(&ctx);
421 return(!GSS_ERROR(major));