1226031Sstas/*-
2226031Sstas * Copyright (c) 2005 Doug Rabson
3226031Sstas * All rights reserved.
4226031Sstas *
5226031Sstas * Redistribution and use in source and binary forms, with or without
6226031Sstas * modification, are permitted provided that the following conditions
7226031Sstas * are met:
8226031Sstas * 1. Redistributions of source code must retain the above copyright
9226031Sstas *    notice, this list of conditions and the following disclaimer.
10226031Sstas * 2. Redistributions in binary form must reproduce the above copyright
11226031Sstas *    notice, this list of conditions and the following disclaimer in the
12226031Sstas *    documentation and/or other materials provided with the distribution.
13226031Sstas *
14226031Sstas * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15226031Sstas * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16226031Sstas * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17226031Sstas * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18226031Sstas * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19226031Sstas * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20226031Sstas * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21226031Sstas * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22226031Sstas * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23226031Sstas * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24226031Sstas * SUCH DAMAGE.
25226031Sstas *
26226031Sstas *	$FreeBSD: src/lib/libgssapi/gss_import_sec_context.c,v 1.1 2005/12/29 14:40:20 dfr Exp $
27226031Sstas */
28226031Sstas
29226031Sstas#include "mech_locl.h"
30226031Sstas
31226031SstasGSSAPI_LIB_FUNCTION OM_uint32 GSSAPI_LIB_CALL
32226031Sstasgss_import_sec_context(OM_uint32 *minor_status,
33226031Sstas    const gss_buffer_t interprocess_token,
34226031Sstas    gss_ctx_id_t *context_handle)
35226031Sstas{
36226031Sstas	OM_uint32 major_status;
37226031Sstas	gssapi_mech_interface m;
38226031Sstas	struct _gss_context *ctx;
39226031Sstas	gss_OID_desc mech_oid;
40226031Sstas	gss_buffer_desc buf;
41226031Sstas	unsigned char *p;
42226031Sstas	size_t len;
43226031Sstas
44226031Sstas	*minor_status = 0;
45226031Sstas	*context_handle = GSS_C_NO_CONTEXT;
46226031Sstas
47226031Sstas	/*
48226031Sstas	 * We added an oid to the front of the token in
49226031Sstas	 * gss_export_sec_context.
50226031Sstas	 */
51226031Sstas	p = interprocess_token->value;
52226031Sstas	len = interprocess_token->length;
53226031Sstas	if (len < 2)
54226031Sstas		return (GSS_S_DEFECTIVE_TOKEN);
55226031Sstas	mech_oid.length = (p[0] << 8) | p[1];
56226031Sstas	if (len < mech_oid.length + 2)
57226031Sstas		return (GSS_S_DEFECTIVE_TOKEN);
58226031Sstas	mech_oid.elements = p + 2;
59226031Sstas	buf.length = len - 2 - mech_oid.length;
60226031Sstas	buf.value = p + 2 + mech_oid.length;
61226031Sstas
62226031Sstas	m = __gss_get_mechanism(&mech_oid);
63226031Sstas	if (!m)
64226031Sstas		return (GSS_S_DEFECTIVE_TOKEN);
65226031Sstas
66226031Sstas	ctx = malloc(sizeof(struct _gss_context));
67226031Sstas	if (!ctx) {
68226031Sstas		*minor_status = ENOMEM;
69226031Sstas		return (GSS_S_FAILURE);
70226031Sstas	}
71226031Sstas	ctx->gc_mech = m;
72226031Sstas	major_status = m->gm_import_sec_context(minor_status,
73226031Sstas	    &buf, &ctx->gc_ctx);
74226031Sstas	if (major_status != GSS_S_COMPLETE) {
75226031Sstas		_gss_mg_error(m, major_status, *minor_status);
76226031Sstas		free(ctx);
77226031Sstas	} else {
78226031Sstas		*context_handle = (gss_ctx_id_t) ctx;
79226031Sstas	}
80226031Sstas
81226031Sstas	return (major_status);
82226031Sstas}
83