1*ebfedea0SLionel Sambuc /* $NetBSD: decapsulate.c,v 1.1.1.1 2011/04/13 18:14:45 elric Exp $ */
2*ebfedea0SLionel Sambuc
3*ebfedea0SLionel Sambuc /*
4*ebfedea0SLionel Sambuc * Copyright (c) 1997 - 2001 Kungliga Tekniska Högskolan
5*ebfedea0SLionel Sambuc * (Royal Institute of Technology, Stockholm, Sweden).
6*ebfedea0SLionel Sambuc * All rights reserved.
7*ebfedea0SLionel Sambuc *
8*ebfedea0SLionel Sambuc * Redistribution and use in source and binary forms, with or without
9*ebfedea0SLionel Sambuc * modification, are permitted provided that the following conditions
10*ebfedea0SLionel Sambuc * are met:
11*ebfedea0SLionel Sambuc *
12*ebfedea0SLionel Sambuc * 1. Redistributions of source code must retain the above copyright
13*ebfedea0SLionel Sambuc * notice, this list of conditions and the following disclaimer.
14*ebfedea0SLionel Sambuc *
15*ebfedea0SLionel Sambuc * 2. Redistributions in binary form must reproduce the above copyright
16*ebfedea0SLionel Sambuc * notice, this list of conditions and the following disclaimer in the
17*ebfedea0SLionel Sambuc * documentation and/or other materials provided with the distribution.
18*ebfedea0SLionel Sambuc *
19*ebfedea0SLionel Sambuc * 3. Neither the name of the Institute nor the names of its contributors
20*ebfedea0SLionel Sambuc * may be used to endorse or promote products derived from this software
21*ebfedea0SLionel Sambuc * without specific prior written permission.
22*ebfedea0SLionel Sambuc *
23*ebfedea0SLionel Sambuc * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
24*ebfedea0SLionel Sambuc * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25*ebfedea0SLionel Sambuc * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26*ebfedea0SLionel Sambuc * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
27*ebfedea0SLionel Sambuc * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28*ebfedea0SLionel Sambuc * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29*ebfedea0SLionel Sambuc * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30*ebfedea0SLionel Sambuc * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31*ebfedea0SLionel Sambuc * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32*ebfedea0SLionel Sambuc * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33*ebfedea0SLionel Sambuc * SUCH DAMAGE.
34*ebfedea0SLionel Sambuc */
35*ebfedea0SLionel Sambuc
36*ebfedea0SLionel Sambuc #include "gsskrb5_locl.h"
37*ebfedea0SLionel Sambuc
38*ebfedea0SLionel Sambuc /*
39*ebfedea0SLionel Sambuc * return the length of the mechanism in token or -1
40*ebfedea0SLionel Sambuc * (which implies that the token was bad - GSS_S_DEFECTIVE_TOKEN
41*ebfedea0SLionel Sambuc */
42*ebfedea0SLionel Sambuc
43*ebfedea0SLionel Sambuc ssize_t
_gsskrb5_get_mech(const u_char * ptr,size_t total_len,const u_char ** mech_ret)44*ebfedea0SLionel Sambuc _gsskrb5_get_mech (const u_char *ptr,
45*ebfedea0SLionel Sambuc size_t total_len,
46*ebfedea0SLionel Sambuc const u_char **mech_ret)
47*ebfedea0SLionel Sambuc {
48*ebfedea0SLionel Sambuc size_t len, len_len, mech_len, foo;
49*ebfedea0SLionel Sambuc const u_char *p = ptr;
50*ebfedea0SLionel Sambuc int e;
51*ebfedea0SLionel Sambuc
52*ebfedea0SLionel Sambuc if (total_len < 1)
53*ebfedea0SLionel Sambuc return -1;
54*ebfedea0SLionel Sambuc if (*p++ != 0x60)
55*ebfedea0SLionel Sambuc return -1;
56*ebfedea0SLionel Sambuc e = der_get_length (p, total_len - 1, &len, &len_len);
57*ebfedea0SLionel Sambuc if (e || 1 + len_len + len != total_len)
58*ebfedea0SLionel Sambuc return -1;
59*ebfedea0SLionel Sambuc p += len_len;
60*ebfedea0SLionel Sambuc if (*p++ != 0x06)
61*ebfedea0SLionel Sambuc return -1;
62*ebfedea0SLionel Sambuc e = der_get_length (p, total_len - 1 - len_len - 1,
63*ebfedea0SLionel Sambuc &mech_len, &foo);
64*ebfedea0SLionel Sambuc if (e)
65*ebfedea0SLionel Sambuc return -1;
66*ebfedea0SLionel Sambuc p += foo;
67*ebfedea0SLionel Sambuc *mech_ret = p;
68*ebfedea0SLionel Sambuc return mech_len;
69*ebfedea0SLionel Sambuc }
70*ebfedea0SLionel Sambuc
71*ebfedea0SLionel Sambuc OM_uint32
_gssapi_verify_mech_header(u_char ** str,size_t total_len,gss_OID mech)72*ebfedea0SLionel Sambuc _gssapi_verify_mech_header(u_char **str,
73*ebfedea0SLionel Sambuc size_t total_len,
74*ebfedea0SLionel Sambuc gss_OID mech)
75*ebfedea0SLionel Sambuc {
76*ebfedea0SLionel Sambuc const u_char *p;
77*ebfedea0SLionel Sambuc ssize_t mech_len;
78*ebfedea0SLionel Sambuc
79*ebfedea0SLionel Sambuc mech_len = _gsskrb5_get_mech (*str, total_len, &p);
80*ebfedea0SLionel Sambuc if (mech_len < 0)
81*ebfedea0SLionel Sambuc return GSS_S_DEFECTIVE_TOKEN;
82*ebfedea0SLionel Sambuc
83*ebfedea0SLionel Sambuc if (mech_len != mech->length)
84*ebfedea0SLionel Sambuc return GSS_S_BAD_MECH;
85*ebfedea0SLionel Sambuc if (ct_memcmp(p,
86*ebfedea0SLionel Sambuc mech->elements,
87*ebfedea0SLionel Sambuc mech->length) != 0)
88*ebfedea0SLionel Sambuc return GSS_S_BAD_MECH;
89*ebfedea0SLionel Sambuc p += mech_len;
90*ebfedea0SLionel Sambuc *str = rk_UNCONST(p);
91*ebfedea0SLionel Sambuc return GSS_S_COMPLETE;
92*ebfedea0SLionel Sambuc }
93*ebfedea0SLionel Sambuc
94*ebfedea0SLionel Sambuc OM_uint32
_gsskrb5_verify_header(u_char ** str,size_t total_len,const void * type,gss_OID oid)95*ebfedea0SLionel Sambuc _gsskrb5_verify_header(u_char **str,
96*ebfedea0SLionel Sambuc size_t total_len,
97*ebfedea0SLionel Sambuc const void *type,
98*ebfedea0SLionel Sambuc gss_OID oid)
99*ebfedea0SLionel Sambuc {
100*ebfedea0SLionel Sambuc OM_uint32 ret;
101*ebfedea0SLionel Sambuc size_t len;
102*ebfedea0SLionel Sambuc u_char *p = *str;
103*ebfedea0SLionel Sambuc
104*ebfedea0SLionel Sambuc ret = _gssapi_verify_mech_header(str, total_len, oid);
105*ebfedea0SLionel Sambuc if (ret)
106*ebfedea0SLionel Sambuc return ret;
107*ebfedea0SLionel Sambuc
108*ebfedea0SLionel Sambuc len = total_len - (*str - p);
109*ebfedea0SLionel Sambuc
110*ebfedea0SLionel Sambuc if (len < 2)
111*ebfedea0SLionel Sambuc return GSS_S_DEFECTIVE_TOKEN;
112*ebfedea0SLionel Sambuc
113*ebfedea0SLionel Sambuc if (ct_memcmp (*str, type, 2) != 0)
114*ebfedea0SLionel Sambuc return GSS_S_DEFECTIVE_TOKEN;
115*ebfedea0SLionel Sambuc *str += 2;
116*ebfedea0SLionel Sambuc
117*ebfedea0SLionel Sambuc return 0;
118*ebfedea0SLionel Sambuc }
119*ebfedea0SLionel Sambuc
120*ebfedea0SLionel Sambuc /*
121*ebfedea0SLionel Sambuc * Remove the GSS-API wrapping from `in_token' giving `out_data.
122*ebfedea0SLionel Sambuc * Does not copy data, so just free `in_token'.
123*ebfedea0SLionel Sambuc */
124*ebfedea0SLionel Sambuc
125*ebfedea0SLionel Sambuc OM_uint32
_gssapi_decapsulate(OM_uint32 * minor_status,gss_buffer_t input_token_buffer,krb5_data * out_data,const gss_OID mech)126*ebfedea0SLionel Sambuc _gssapi_decapsulate(
127*ebfedea0SLionel Sambuc OM_uint32 *minor_status,
128*ebfedea0SLionel Sambuc gss_buffer_t input_token_buffer,
129*ebfedea0SLionel Sambuc krb5_data *out_data,
130*ebfedea0SLionel Sambuc const gss_OID mech
131*ebfedea0SLionel Sambuc )
132*ebfedea0SLionel Sambuc {
133*ebfedea0SLionel Sambuc u_char *p;
134*ebfedea0SLionel Sambuc OM_uint32 ret;
135*ebfedea0SLionel Sambuc
136*ebfedea0SLionel Sambuc p = input_token_buffer->value;
137*ebfedea0SLionel Sambuc ret = _gssapi_verify_mech_header(&p,
138*ebfedea0SLionel Sambuc input_token_buffer->length,
139*ebfedea0SLionel Sambuc mech);
140*ebfedea0SLionel Sambuc if (ret) {
141*ebfedea0SLionel Sambuc *minor_status = 0;
142*ebfedea0SLionel Sambuc return ret;
143*ebfedea0SLionel Sambuc }
144*ebfedea0SLionel Sambuc
145*ebfedea0SLionel Sambuc out_data->length = input_token_buffer->length -
146*ebfedea0SLionel Sambuc (p - (u_char *)input_token_buffer->value);
147*ebfedea0SLionel Sambuc out_data->data = p;
148*ebfedea0SLionel Sambuc return GSS_S_COMPLETE;
149*ebfedea0SLionel Sambuc }
150*ebfedea0SLionel Sambuc
151*ebfedea0SLionel Sambuc /*
152*ebfedea0SLionel Sambuc * Remove the GSS-API wrapping from `in_token' giving `out_data.
153*ebfedea0SLionel Sambuc * Does not copy data, so just free `in_token'.
154*ebfedea0SLionel Sambuc */
155*ebfedea0SLionel Sambuc
156*ebfedea0SLionel Sambuc OM_uint32
_gsskrb5_decapsulate(OM_uint32 * minor_status,gss_buffer_t input_token_buffer,krb5_data * out_data,const void * type,gss_OID oid)157*ebfedea0SLionel Sambuc _gsskrb5_decapsulate(OM_uint32 *minor_status,
158*ebfedea0SLionel Sambuc gss_buffer_t input_token_buffer,
159*ebfedea0SLionel Sambuc krb5_data *out_data,
160*ebfedea0SLionel Sambuc const void *type,
161*ebfedea0SLionel Sambuc gss_OID oid)
162*ebfedea0SLionel Sambuc {
163*ebfedea0SLionel Sambuc u_char *p;
164*ebfedea0SLionel Sambuc OM_uint32 ret;
165*ebfedea0SLionel Sambuc
166*ebfedea0SLionel Sambuc p = input_token_buffer->value;
167*ebfedea0SLionel Sambuc ret = _gsskrb5_verify_header(&p,
168*ebfedea0SLionel Sambuc input_token_buffer->length,
169*ebfedea0SLionel Sambuc type,
170*ebfedea0SLionel Sambuc oid);
171*ebfedea0SLionel Sambuc if (ret) {
172*ebfedea0SLionel Sambuc *minor_status = 0;
173*ebfedea0SLionel Sambuc return ret;
174*ebfedea0SLionel Sambuc }
175*ebfedea0SLionel Sambuc
176*ebfedea0SLionel Sambuc out_data->length = input_token_buffer->length -
177*ebfedea0SLionel Sambuc (p - (u_char *)input_token_buffer->value);
178*ebfedea0SLionel Sambuc out_data->data = p;
179*ebfedea0SLionel Sambuc return GSS_S_COMPLETE;
180*ebfedea0SLionel Sambuc }
181*ebfedea0SLionel Sambuc
182*ebfedea0SLionel Sambuc /*
183*ebfedea0SLionel Sambuc * Verify padding of a gss wrapped message and return its length.
184*ebfedea0SLionel Sambuc */
185*ebfedea0SLionel Sambuc
186*ebfedea0SLionel Sambuc OM_uint32
_gssapi_verify_pad(gss_buffer_t wrapped_token,size_t datalen,size_t * padlen)187*ebfedea0SLionel Sambuc _gssapi_verify_pad(gss_buffer_t wrapped_token,
188*ebfedea0SLionel Sambuc size_t datalen,
189*ebfedea0SLionel Sambuc size_t *padlen)
190*ebfedea0SLionel Sambuc {
191*ebfedea0SLionel Sambuc u_char *pad;
192*ebfedea0SLionel Sambuc size_t padlength;
193*ebfedea0SLionel Sambuc int i;
194*ebfedea0SLionel Sambuc
195*ebfedea0SLionel Sambuc pad = (u_char *)wrapped_token->value + wrapped_token->length - 1;
196*ebfedea0SLionel Sambuc padlength = *pad;
197*ebfedea0SLionel Sambuc
198*ebfedea0SLionel Sambuc if (padlength > datalen)
199*ebfedea0SLionel Sambuc return GSS_S_BAD_MECH;
200*ebfedea0SLionel Sambuc
201*ebfedea0SLionel Sambuc for (i = padlength; i > 0 && *pad == padlength; i--, pad--)
202*ebfedea0SLionel Sambuc ;
203*ebfedea0SLionel Sambuc if (i != 0)
204*ebfedea0SLionel Sambuc return GSS_S_BAD_MIC;
205*ebfedea0SLionel Sambuc
206*ebfedea0SLionel Sambuc *padlen = padlength;
207*ebfedea0SLionel Sambuc
208*ebfedea0SLionel Sambuc return 0;
209*ebfedea0SLionel Sambuc }
210