1/*-
2 * Copyright (c) 2008 Doug Rabson
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 *    notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 *    notice, this list of conditions and the following disclaimer in the
12 *    documentation and/or other materials provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24 * SUCH DAMAGE.
25 *
26 * $FreeBSD$
27 */
28
29#include <gssapi/gssapi.h>
30#include <stdlib.h>
31#include <string.h>
32
33#include "utils.h"
34
35OM_uint32
36gss_decapsulate_token(const gss_buffer_t input_token, gss_OID oid,
37    gss_buffer_t output_token)
38{
39	unsigned char *p = input_token->value;
40	size_t len = input_token->length;
41	size_t a, b;
42	gss_OID_desc mech_oid;
43
44	_gss_buffer_zero(output_token);
45
46	/*
47	 * Token must start with [APPLICATION 0] SEQUENCE.
48	 */
49	if (len == 0 || *p != 0x60)
50		return (GSS_S_DEFECTIVE_TOKEN);
51	p++;
52	len--;
53
54	/*
55	 * Decode the length and make sure it agrees with the
56	 * token length.
57	 */
58	if (len == 0)
59		return (GSS_S_DEFECTIVE_TOKEN);
60	if ((*p & 0x80) == 0) {
61		a = *p;
62		p++;
63		len--;
64	} else {
65		b = *p & 0x7f;
66		p++;
67		len--;
68		if (len < b)
69			return (GSS_S_DEFECTIVE_TOKEN);
70		a = 0;
71		while (b) {
72			a = (a << 8) | *p;
73			p++;
74			len--;
75			b--;
76		}
77	}
78	if (a != len)
79		return (GSS_S_DEFECTIVE_TOKEN);
80
81	/*
82	 * Decode the OID for the mechanism. Simplify life by
83	 * assuming that the OID length is less than 128 bytes.
84	 */
85	if (len < 2 || *p != 0x06)
86		return (GSS_S_DEFECTIVE_TOKEN);
87	if ((p[1] & 0x80) || p[1] > (len - 2))
88		return (GSS_S_DEFECTIVE_TOKEN);
89	mech_oid.length = p[1];
90	p += 2;
91	len -= 2;
92	mech_oid.elements = p;
93
94	if (!gss_oid_equal(&mech_oid, oid))
95		return (GSS_S_FAILURE);
96
97	p += mech_oid.length;
98	len -= mech_oid.length;
99
100	output_token->length = len;
101	output_token->value = malloc(len);
102	if (!output_token->value)
103		return (GSS_S_DEFECTIVE_TOKEN);
104	memcpy(output_token->value, p, len);
105
106	return (GSS_S_COMPLETE);
107}
108