2 * Copyright (c) 2008 Doug Rabson
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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
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);