Better error reporting through com_err
[mech_eap.git] / util_token.c
1 /*
2  * Copyright (c) 2010, JANET(UK)
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  *
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  *
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.
15  *
16  * 3. Neither the name of JANET(UK) nor the names of its contributors
17  *    may be used to endorse or promote products derived from this software
18  *    without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
24  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30  * SUCH DAMAGE.
31  */
32 /*
33  * Copyright 1993 by OpenVision Technologies, Inc.
34  *
35  * Permission to use, copy, modify, distribute, and sell this software
36  * and its documentation for any purpose is hereby granted without fee,
37  * provided that the above copyright notice appears in all copies and
38  * that both that copyright notice and this permission notice appear in
39  * supporting documentation, and that the name of OpenVision not be used
40  * in advertising or publicity pertaining to distribution of the software
41  * without specific, written prior permission. OpenVision makes no
42  * representations about the suitability of this software for any
43  * purpose.  It is provided "as is" without express or implied warranty.
44  *
45  * OPENVISION DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
46  * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
47  * EVENT SHALL OPENVISION BE LIABLE FOR ANY SPECIAL, INDIRECT OR
48  * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF
49  * USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
50  * OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
51  * PERFORMANCE OF THIS SOFTWARE.
52  */
53
54 #include "gssapiP_eap.h"
55
56 /*
57  * $Id: util_token.c 23457 2009-12-08 00:04:48Z tlyu $
58  */
59
60 /* XXXX this code currently makes the assumption that a mech oid will
61    never be longer than 127 bytes.  This assumption is not inherent in
62    the interfaces, so the code can be fixed if the OSI namespace
63    balloons unexpectedly. */
64
65 /*
66  * Each token looks like this:
67  * 0x60                 tag for APPLICATION 0, SEQUENCE
68  *                              (constructed, definite-length)
69  * <length>             possible multiple bytes, need to parse/generate
70  * 0x06                 tag for OBJECT IDENTIFIER
71  * <moid_length>        compile-time constant string (assume 1 byte)
72  * <moid_bytes>         compile-time constant string
73  * <inner_bytes>        the ANY containing the application token
74  * bytes 0,1 are the token type
75  * bytes 2,n are the token data
76  *
77  * Note that the token type field is a feature of RFC 1964 mechanisms and
78  * is not used by other GSSAPI mechanisms.  As such, a token type of -1
79  * is interpreted to mean that no token type should be expected or
80  * generated.
81  *
82  * For the purposes of this abstraction, the token "header" consists of
83  * the sequence tag and length octets, the mech OID DER encoding, and the
84  * first two inner bytes, which indicate the token type.  The token
85  * "body" consists of everything else.
86  */
87
88 static size_t
89 der_length_size(size_t length)
90 {
91     if (length < (1<<7))
92         return 1;
93     else if (length < (1<<8))
94         return 2;
95 #if INT_MAX == 0x7fff
96     else
97         return 3;
98 #else
99     else if (length < (1<<16))
100         return 3;
101     else if (length < (1<<24))
102         return 4;
103     else
104         return 5;
105 #endif
106 }
107
108 static void
109 der_write_length(unsigned char **buf, size_t length)
110 {
111     if (length < (1<<7)) {
112         *(*buf)++ = (unsigned char)length;
113     } else {
114         *(*buf)++ = (unsigned char)(der_length_size(length)+127);
115 #if INT_MAX > 0x7fff
116         if (length >= (1<<24))
117             *(*buf)++ = (unsigned char)(length>>24);
118         if (length >= (1<<16))
119             *(*buf)++ = (unsigned char)((length>>16)&0xff);
120 #endif
121         if (length >= (1<<8))
122             *(*buf)++ = (unsigned char)((length>>8)&0xff);
123         *(*buf)++ = (unsigned char)(length&0xff);
124     }
125 }
126
127 /* returns decoded length, or < 0 on failure.  Advances buf and
128    decrements bufsize */
129
130 static int
131 der_read_length(unsigned char **buf, ssize_t *bufsize)
132 {
133     unsigned char sf;
134     int ret;
135
136     if (*bufsize < 1)
137         return -1;
138
139     sf = *(*buf)++;
140     (*bufsize)--;
141     if (sf & 0x80) {
142         if ((sf &= 0x7f) > ((*bufsize)-1))
143             return -1;
144         if (sf > sizeof(int))
145             return -1;
146         ret = 0;
147         for (; sf; sf--) {
148             ret = (ret<<8) + (*(*buf)++);
149             (*bufsize)--;
150         }
151     } else {
152         ret = sf;
153     }
154
155     return ret;
156 }
157
158 /* returns the length of a token, given the mech oid and the body size */
159
160 size_t
161 tokenSize(const gss_OID_desc *mech, size_t body_size)
162 {
163     /* set body_size to sequence contents size */
164     body_size += 4 + (size_t) mech->length;         /* NEED overflow check */
165     return 1 + der_length_size(body_size) + body_size;
166 }
167
168 /* fills in a buffer with the token header.  The buffer is assumed to
169    be the right size.  buf is advanced past the token header */
170
171 void
172 makeTokenHeader(
173     const gss_OID_desc *mech,
174     size_t body_size,
175     unsigned char **buf,
176     enum gss_eap_token_type tok_type)
177 {
178     *(*buf)++ = 0x60;
179     der_write_length(buf, (tok_type == -1) ?2:4 + mech->length + body_size);
180     *(*buf)++ = 0x06;
181     *(*buf)++ = (unsigned char)mech->length;
182     memcpy(*buf, mech->elements, mech->length);
183     *buf += mech->length;
184     assert(tok_type != TOK_TYPE_NONE);
185     *(*buf)++ = (unsigned char)((tok_type>>8) & 0xff);
186     *(*buf)++ = (unsigned char)(tok_type & 0xff);
187 }
188
189 /*
190  * Given a buffer containing a token, reads and verifies the token,
191  * leaving buf advanced past the token header, and setting body_size
192  * to the number of remaining bytes.  Returns 0 on success,
193  * G_BAD_TOK_HEADER for a variety of errors, and G_WRONG_MECH if the
194  * mechanism in the token does not match the mech argument.  buf and
195  * *body_size are left unmodified on error.
196  */
197
198 OM_uint32
199 verifyTokenHeader(OM_uint32 *minor,
200                   gss_OID mech,
201                   size_t *body_size,
202                   unsigned char **buf_in,
203                   size_t toksize_in,
204                   enum gss_eap_token_type *ret_tok_type)
205 {
206     unsigned char *buf = *buf_in;
207     ssize_t seqsize;
208     gss_OID_desc toid;
209     ssize_t toksize = (ssize_t)toksize_in;
210
211     *minor = GSSEAP_BAD_TOK_HEADER;
212
213     if (ret_tok_type != NULL)
214         *ret_tok_type = TOK_TYPE_NONE;
215
216     if ((toksize -= 1) < 0)
217         return GSS_S_DEFECTIVE_TOKEN;
218
219     if (*buf++ != 0x60)
220         return GSS_S_DEFECTIVE_TOKEN;
221
222     seqsize = der_read_length(&buf, &toksize);
223     if (seqsize < 0)
224         return GSS_S_DEFECTIVE_TOKEN;
225
226     if (seqsize != toksize)
227         return GSS_S_DEFECTIVE_TOKEN;
228
229     if ((toksize -= 1) < 0)
230         return GSS_S_DEFECTIVE_TOKEN;
231
232     if (*buf++ != 0x06)
233         return GSS_S_DEFECTIVE_TOKEN;
234
235     if ((toksize -= 1) < 0)
236         return GSS_S_DEFECTIVE_TOKEN;
237
238     toid.length = *buf++;
239
240     if ((toksize -= toid.length) < 0)
241         return GSS_S_DEFECTIVE_TOKEN;
242
243     toid.elements = buf;
244     buf += toid.length;
245
246     if (mech->elements == NULL) {
247         *mech = toid;
248         if (toid.length == 0)
249             return GSS_S_BAD_MECH;
250     } else if (!oidEqual(&toid, mech)) {
251         *minor = GSSEAP_WRONG_MECH;
252         return GSS_S_BAD_MECH;
253     }
254
255     if (ret_tok_type != NULL) {
256         if ((toksize -= 2) < 0)
257             return GSS_S_DEFECTIVE_TOKEN;
258
259         *ret_tok_type = load_uint16_be(buf);
260         buf += 2;
261     }
262
263     *buf_in = buf;
264     *body_size = toksize;
265
266     *minor = 0;
267     return GSS_S_COMPLETE;
268 }