1153838Sdfr/*-
2153838Sdfr * Copyright (c) 2005 Doug Rabson
3153838Sdfr * All rights reserved.
4153838Sdfr *
5153838Sdfr * Redistribution and use in source and binary forms, with or without
6153838Sdfr * modification, are permitted provided that the following conditions
7153838Sdfr * are met:
8153838Sdfr * 1. Redistributions of source code must retain the above copyright
9153838Sdfr *    notice, this list of conditions and the following disclaimer.
10153838Sdfr * 2. Redistributions in binary form must reproduce the above copyright
11153838Sdfr *    notice, this list of conditions and the following disclaimer in the
12153838Sdfr *    documentation and/or other materials provided with the distribution.
13153838Sdfr *
14153838Sdfr * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15153838Sdfr * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16153838Sdfr * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17153838Sdfr * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18153838Sdfr * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19153838Sdfr * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20153838Sdfr * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21153838Sdfr * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22153838Sdfr * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23153838Sdfr * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24153838Sdfr * SUCH DAMAGE.
25153838Sdfr *
26153838Sdfr *	$FreeBSD$
27153838Sdfr */
28153838Sdfr
29153838Sdfr#include <gssapi/gssapi.h>
30153838Sdfr#include <stdlib.h>
31153838Sdfr#include <errno.h>
32153838Sdfr
33153838Sdfr#include "mech_switch.h"
34153838Sdfr#include "context.h"
35153838Sdfr
36153838SdfrOM_uint32
37153838Sdfrgss_import_sec_context(OM_uint32 *minor_status,
38153838Sdfr    const gss_buffer_t interprocess_token,
39153838Sdfr    gss_ctx_id_t *context_handle)
40153838Sdfr{
41153838Sdfr	OM_uint32 major_status;
42153838Sdfr	struct _gss_mech_switch *m;
43153838Sdfr	struct _gss_context *ctx;
44153838Sdfr	gss_OID_desc mech_oid;
45153838Sdfr	gss_buffer_desc buf;
46153838Sdfr	unsigned char *p;
47153838Sdfr	size_t len;
48153838Sdfr
49153838Sdfr	*minor_status = 0;
50178828Sdfr	*context_handle = GSS_C_NO_CONTEXT;
51153838Sdfr
52153838Sdfr	/*
53153838Sdfr	 * We added an oid to the front of the token in
54153838Sdfr	 * gss_export_sec_context.
55153838Sdfr	 */
56153838Sdfr	p = interprocess_token->value;
57153838Sdfr	len = interprocess_token->length;
58153838Sdfr	if (len < 2)
59153838Sdfr		return (GSS_S_DEFECTIVE_TOKEN);
60153838Sdfr	mech_oid.length = (p[0] << 8) | p[1];
61153838Sdfr	if (len < mech_oid.length + 2)
62153838Sdfr		return (GSS_S_DEFECTIVE_TOKEN);
63153838Sdfr	mech_oid.elements = p + 2;
64153838Sdfr	buf.length = len - 2 - mech_oid.length;
65153838Sdfr	buf.value = p + 2 + mech_oid.length;
66153838Sdfr
67153838Sdfr	m = _gss_find_mech_switch(&mech_oid);
68153838Sdfr	if (!m)
69153838Sdfr		return (GSS_S_DEFECTIVE_TOKEN);
70153838Sdfr
71153838Sdfr	ctx = malloc(sizeof(struct _gss_context));
72153838Sdfr	if (!ctx) {
73153838Sdfr		*minor_status = ENOMEM;
74153838Sdfr		return (GSS_S_FAILURE);
75153838Sdfr	}
76153838Sdfr	ctx->gc_mech = m;
77153838Sdfr	major_status = m->gm_import_sec_context(minor_status,
78153838Sdfr	    &buf, &ctx->gc_ctx);
79153838Sdfr	if (major_status != GSS_S_COMPLETE) {
80178828Sdfr		_gss_mg_error(m, major_status, *minor_status);
81153838Sdfr		free(ctx);
82153838Sdfr	} else {
83153838Sdfr		*context_handle = (gss_ctx_id_t) ctx;
84153838Sdfr	}
85153838Sdfr
86153838Sdfr	return (major_status);
87153838Sdfr}
88