gss_export_sec_context.c revision 153838
11541Srgrimes/*-
21541Srgrimes * Copyright (c) 2005 Doug Rabson
31541Srgrimes * All rights reserved.
41541Srgrimes *
51541Srgrimes * Redistribution and use in source and binary forms, with or without
61541Srgrimes * modification, are permitted provided that the following conditions
71541Srgrimes * are met:
81541Srgrimes * 1. Redistributions of source code must retain the above copyright
91541Srgrimes *    notice, this list of conditions and the following disclaimer.
101541Srgrimes * 2. Redistributions in binary form must reproduce the above copyright
111541Srgrimes *    notice, this list of conditions and the following disclaimer in the
121541Srgrimes *    documentation and/or other materials provided with the distribution.
131541Srgrimes *
141541Srgrimes * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
151541Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
161541Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
171541Srgrimes * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
181541Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
191541Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
201541Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
211541Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
221541Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
231541Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
241541Srgrimes * SUCH DAMAGE.
251541Srgrimes *
261541Srgrimes *	$FreeBSD: head/lib/libgssapi/gss_export_sec_context.c 153838 2005-12-29 14:40:22Z dfr $
271541Srgrimes */
281541Srgrimes
291541Srgrimes#include <gssapi/gssapi.h>
301541Srgrimes#include <stdlib.h>
311541Srgrimes#include <errno.h>
321541Srgrimes
331541Srgrimes#include "mech_switch.h"
341541Srgrimes#include "context.h"
351541Srgrimes
3622521SdysonOM_uint32
3733054Sbdegss_export_sec_context(OM_uint32 *minor_status,
381541Srgrimes    gss_ctx_id_t *context_handle,
391541Srgrimes    gss_buffer_t interprocess_token)
4022521Sdyson{
412175Spaul	OM_uint32 major_status;
422175Spaul	struct _gss_context *ctx = (struct _gss_context *) *context_handle;
432175Spaul	struct _gss_mech_switch *m = ctx->gc_mech;
4433054Sbde	gss_buffer_desc buf;
4533054Sbde
469336Sdfr	major_status = m->gm_export_sec_context(minor_status,
471541Srgrimes	    &ctx->gc_ctx, &buf);
481541Srgrimes
491541Srgrimes	if (major_status == GSS_S_COMPLETE) {
501541Srgrimes		unsigned char *p;
511541Srgrimes
521541Srgrimes		free(ctx);
531541Srgrimes		*context_handle = GSS_C_NO_CONTEXT;
541541Srgrimes		interprocess_token->length = buf.length
551541Srgrimes			+ 2 + m->gm_mech_oid.length;
5610222Sdfr		interprocess_token->value = malloc(interprocess_token->length);
5710222Sdfr		if (!interprocess_token->value) {
5810222Sdfr			/*
5910222Sdfr			 * We are in trouble here - the context is
6010222Sdfr			 * already gone. This is allowed as long as we
6110222Sdfr			 * set the caller's context_handle to
6210222Sdfr			 * GSS_C_NO_CONTEXT, which we did above.
631541Srgrimes			 * Return GSS_S_FAILURE.
641541Srgrimes			 */
651541Srgrimes			*minor_status = ENOMEM;
661541Srgrimes			return (GSS_S_FAILURE);
671541Srgrimes		}
681541Srgrimes		p = interprocess_token->value;
691541Srgrimes		p[0] = m->gm_mech_oid.length >> 8;
701541Srgrimes		p[1] = m->gm_mech_oid.length;
711541Srgrimes		memcpy(p + 2, m->gm_mech_oid.elements, m->gm_mech_oid.length);
721541Srgrimes		memcpy(p + 2 + m->gm_mech_oid.length, buf.value, buf.length);
731541Srgrimes		gss_release_buffer(minor_status, &buf);
741541Srgrimes	}
751541Srgrimes
761541Srgrimes	return (major_status);
771541Srgrimes}
781541Srgrimes