2 * SPDX-License-Identifier: BSD-2-Clause
4 * Copyright (c) 2008 Doug Rabson
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 #include <gssapi/gssapi.h>
36 gss_decapsulate_token(const gss_buffer_t input_token, gss_OID oid,
37 gss_buffer_t output_token)
39 unsigned char *p = input_token->value;
40 size_t len = input_token->length;
42 gss_OID_desc mech_oid;
44 _gss_buffer_zero(output_token);
47 * Token must start with [APPLICATION 0] SEQUENCE.
49 if (len == 0 || *p != 0x60)
50 return (GSS_S_DEFECTIVE_TOKEN);
55 * Decode the length and make sure it agrees with the
59 return (GSS_S_DEFECTIVE_TOKEN);
60 if ((*p & 0x80) == 0) {
69 return (GSS_S_DEFECTIVE_TOKEN);
79 return (GSS_S_DEFECTIVE_TOKEN);
82 * Decode the OID for the mechanism. Simplify life by
83 * assuming that the OID length is less than 128 bytes.
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];
92 mech_oid.elements = p;
94 if (!gss_oid_equal(&mech_oid, oid))
95 return (GSS_S_FAILURE);
98 len -= mech_oid.length;
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);
106 return (GSS_S_COMPLETE);