1264919Sjkim/*-
2264919Sjkim * SPDX-License-Identifier: BSD-2-Clause
3264919Sjkim *
4264919Sjkim * Copyright (c) 2005 Doug Rabson
5264919Sjkim * All rights reserved.
6264919Sjkim *
7316303Sjkim * Redistribution and use in source and binary forms, with or without
8316303Sjkim * modification, are permitted provided that the following conditions
9316303Sjkim * are met:
10316303Sjkim * 1. Redistributions of source code must retain the above copyright
11316303Sjkim *    notice, this list of conditions and the following disclaimer.
12264919Sjkim * 2. Redistributions in binary form must reproduce the above copyright
13264919Sjkim *    notice, this list of conditions and the following disclaimer in the
14316303Sjkim *    documentation and/or other materials provided with the distribution.
15316303Sjkim *
16316303Sjkim * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17316303Sjkim * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18316303Sjkim * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19316303Sjkim * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20316303Sjkim * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21316303Sjkim * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22316303Sjkim * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23316303Sjkim * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24316303Sjkim * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25316303Sjkim * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26316303Sjkim * SUCH DAMAGE.
27316303Sjkim */
28316303Sjkim
29316303Sjkim#include <gssapi/gssapi.h>
30316303Sjkim#include <stdlib.h>
31316303Sjkim#include <string.h>
32316303Sjkim#include <errno.h>
33316303Sjkim
34316303Sjkim#include "mech_switch.h"
35316303Sjkim#include "context.h"
36316303Sjkim#include "utils.h"
37316303Sjkim
38316303SjkimOM_uint32
39316303Sjkimgss_export_sec_context(OM_uint32 *minor_status,
40316303Sjkim    gss_ctx_id_t *context_handle,
41316303Sjkim    gss_buffer_t interprocess_token)
42316303Sjkim{
43316303Sjkim	OM_uint32 major_status;
44316303Sjkim	struct _gss_context *ctx = (struct _gss_context *) *context_handle;
45316303Sjkim	struct _gss_mech_switch *m = ctx->gc_mech;
46316303Sjkim	gss_buffer_desc buf;
47316303Sjkim
48316303Sjkim	_gss_buffer_zero(interprocess_token);
49316303Sjkim
50316303Sjkim	major_status = m->gm_export_sec_context(minor_status,
51316303Sjkim	    &ctx->gc_ctx, &buf);
52316303Sjkim
53316303Sjkim	if (major_status == GSS_S_COMPLETE) {
54316303Sjkim		unsigned char *p;
55316303Sjkim
56316303Sjkim		free(ctx);
57316303Sjkim		*context_handle = GSS_C_NO_CONTEXT;
58316303Sjkim		interprocess_token->length = buf.length
59316303Sjkim			+ 2 + m->gm_mech_oid.length;
60316303Sjkim		interprocess_token->value = malloc(interprocess_token->length);
61316303Sjkim		if (!interprocess_token->value) {
62316303Sjkim			/*
63316303Sjkim			 * We are in trouble here - the context is
64316303Sjkim			 * already gone. This is allowed as long as we
65316303Sjkim			 * set the caller's context_handle to
66316303Sjkim			 * GSS_C_NO_CONTEXT, which we did above.
67316303Sjkim			 * Return GSS_S_FAILURE.
68316303Sjkim			 */
69316303Sjkim			_gss_buffer_zero(interprocess_token);
70316303Sjkim			*minor_status = ENOMEM;
71316303Sjkim			return (GSS_S_FAILURE);
72316303Sjkim		}
73316303Sjkim		p = interprocess_token->value;
74316303Sjkim		p[0] = m->gm_mech_oid.length >> 8;
75316303Sjkim		p[1] = m->gm_mech_oid.length;
76316303Sjkim		memcpy(p + 2, m->gm_mech_oid.elements, m->gm_mech_oid.length);
77316303Sjkim		memcpy(p + 2 + m->gm_mech_oid.length, buf.value, buf.length);
78316303Sjkim		gss_release_buffer(minor_status, &buf);
79316303Sjkim	} else {
80316303Sjkim		_gss_mg_error(m, major_status, *minor_status);
81316303Sjkim	}
82316303Sjkim
83316303Sjkim	return (major_status);
84316303Sjkim}
85316303Sjkim