1c0b9f4feSDoug Rabson /*-
2*4d846d26SWarner Losh * SPDX-License-Identifier: BSD-2-Clause
35e53a4f9SPedro F. Giffuni *
4c0b9f4feSDoug Rabson * Copyright (c) 2005 Doug Rabson
5c0b9f4feSDoug Rabson * All rights reserved.
6c0b9f4feSDoug Rabson *
7c0b9f4feSDoug Rabson * Redistribution and use in source and binary forms, with or without
8c0b9f4feSDoug Rabson * modification, are permitted provided that the following conditions
9c0b9f4feSDoug Rabson * are met:
10c0b9f4feSDoug Rabson * 1. Redistributions of source code must retain the above copyright
11c0b9f4feSDoug Rabson * notice, this list of conditions and the following disclaimer.
12c0b9f4feSDoug Rabson * 2. Redistributions in binary form must reproduce the above copyright
13c0b9f4feSDoug Rabson * notice, this list of conditions and the following disclaimer in the
14c0b9f4feSDoug Rabson * documentation and/or other materials provided with the distribution.
15c0b9f4feSDoug Rabson *
16c0b9f4feSDoug Rabson * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17c0b9f4feSDoug Rabson * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18c0b9f4feSDoug Rabson * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19c0b9f4feSDoug Rabson * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20c0b9f4feSDoug Rabson * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21c0b9f4feSDoug Rabson * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22c0b9f4feSDoug Rabson * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23c0b9f4feSDoug Rabson * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24c0b9f4feSDoug Rabson * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25c0b9f4feSDoug Rabson * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26c0b9f4feSDoug Rabson * SUCH DAMAGE.
27c0b9f4feSDoug Rabson */
28c0b9f4feSDoug Rabson
29c0b9f4feSDoug Rabson #include <gssapi/gssapi.h>
30c0b9f4feSDoug Rabson #include <stdlib.h>
31c0b9f4feSDoug Rabson #include <errno.h>
32c0b9f4feSDoug Rabson
33c0b9f4feSDoug Rabson #include "mech_switch.h"
34c0b9f4feSDoug Rabson #include "context.h"
35c0b9f4feSDoug Rabson
36c0b9f4feSDoug Rabson OM_uint32
gss_import_sec_context(OM_uint32 * minor_status,const gss_buffer_t interprocess_token,gss_ctx_id_t * context_handle)37c0b9f4feSDoug Rabson gss_import_sec_context(OM_uint32 *minor_status,
38c0b9f4feSDoug Rabson const gss_buffer_t interprocess_token,
39c0b9f4feSDoug Rabson gss_ctx_id_t *context_handle)
40c0b9f4feSDoug Rabson {
41c0b9f4feSDoug Rabson OM_uint32 major_status;
42c0b9f4feSDoug Rabson struct _gss_mech_switch *m;
43c0b9f4feSDoug Rabson struct _gss_context *ctx;
44c0b9f4feSDoug Rabson gss_OID_desc mech_oid;
45c0b9f4feSDoug Rabson gss_buffer_desc buf;
46c0b9f4feSDoug Rabson unsigned char *p;
47c0b9f4feSDoug Rabson size_t len;
48c0b9f4feSDoug Rabson
49c0b9f4feSDoug Rabson *minor_status = 0;
5033f12199SDoug Rabson *context_handle = GSS_C_NO_CONTEXT;
51c0b9f4feSDoug Rabson
52c0b9f4feSDoug Rabson /*
53c0b9f4feSDoug Rabson * We added an oid to the front of the token in
54c0b9f4feSDoug Rabson * gss_export_sec_context.
55c0b9f4feSDoug Rabson */
56c0b9f4feSDoug Rabson p = interprocess_token->value;
57c0b9f4feSDoug Rabson len = interprocess_token->length;
58c0b9f4feSDoug Rabson if (len < 2)
59c0b9f4feSDoug Rabson return (GSS_S_DEFECTIVE_TOKEN);
60c0b9f4feSDoug Rabson mech_oid.length = (p[0] << 8) | p[1];
61c0b9f4feSDoug Rabson if (len < mech_oid.length + 2)
62c0b9f4feSDoug Rabson return (GSS_S_DEFECTIVE_TOKEN);
63c0b9f4feSDoug Rabson mech_oid.elements = p + 2;
64c0b9f4feSDoug Rabson buf.length = len - 2 - mech_oid.length;
65c0b9f4feSDoug Rabson buf.value = p + 2 + mech_oid.length;
66c0b9f4feSDoug Rabson
67c0b9f4feSDoug Rabson m = _gss_find_mech_switch(&mech_oid);
68c0b9f4feSDoug Rabson if (!m)
69c0b9f4feSDoug Rabson return (GSS_S_DEFECTIVE_TOKEN);
70c0b9f4feSDoug Rabson
71c0b9f4feSDoug Rabson ctx = malloc(sizeof(struct _gss_context));
72c0b9f4feSDoug Rabson if (!ctx) {
73c0b9f4feSDoug Rabson *minor_status = ENOMEM;
74c0b9f4feSDoug Rabson return (GSS_S_FAILURE);
75c0b9f4feSDoug Rabson }
76c0b9f4feSDoug Rabson ctx->gc_mech = m;
77c0b9f4feSDoug Rabson major_status = m->gm_import_sec_context(minor_status,
78c0b9f4feSDoug Rabson &buf, &ctx->gc_ctx);
79c0b9f4feSDoug Rabson if (major_status != GSS_S_COMPLETE) {
8033f12199SDoug Rabson _gss_mg_error(m, major_status, *minor_status);
81c0b9f4feSDoug Rabson free(ctx);
82c0b9f4feSDoug Rabson } else {
83c0b9f4feSDoug Rabson *context_handle = (gss_ctx_id_t) ctx;
84c0b9f4feSDoug Rabson }
85c0b9f4feSDoug Rabson
86c0b9f4feSDoug Rabson return (major_status);
87c0b9f4feSDoug Rabson }
88