integrity protect extension token exchange
[mech_eap.orig] / mech_eap / util_token.c
1 /*
2  * Copyright (c) 2011, 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  * Portions 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 /*
55  * Utility routines for GSS tokens.
56  */
57
58 #include "gssapiP_eap.h"
59
60 OM_uint32
61 gssEapEncodeInnerTokens(OM_uint32 *minor,
62                         struct gss_eap_token_buffer_set *tokens,
63                         gss_buffer_t buffer)
64 {
65     OM_uint32 major, tmpMinor;
66     size_t required = 0, i;
67     unsigned char *p;
68
69     buffer->value = NULL;
70     buffer->length = 0;
71
72     for (i = 0; i < tokens->buffers.count; i++) {
73         required += 8 + tokens->buffers.elements[i].length;
74     }
75
76     /*
77      * We must always return a non-NULL token otherwise the calling state
78      * machine assumes we are finished. Hence care in case malloc(0) does
79      * return NULL.
80      */
81     buffer->value = GSSEAP_MALLOC(required ? required : 1);
82     if (buffer->value == NULL) {
83         major = GSS_S_FAILURE;
84         *minor = ENOMEM;
85         goto cleanup;
86     }
87
88     buffer->length = required;
89     p = (unsigned char *)buffer->value;
90
91     for (i = 0; i < tokens->buffers.count; i++) {
92         gss_buffer_t tokenBuffer = &tokens->buffers.elements[i];
93
94         assert((tokens->types[i] & ITOK_FLAG_VERIFIED) == 0); /* private flag */
95
96          /*
97           * Extensions are encoded as type-length-value, where the upper
98           * bit of the type indicates criticality.
99           */
100         store_uint32_be(tokens->types[i], &p[0]);
101         store_uint32_be(tokenBuffer->length, &p[4]);
102         memcpy(&p[8], tokenBuffer->value, tokenBuffer->length);
103
104         p += 8 + tokenBuffer->length;
105     }
106
107     assert(p == (unsigned char *)buffer->value + required);
108     assert(buffer->value != NULL);
109
110     major = GSS_S_COMPLETE;
111     *minor = 0;
112
113 cleanup:
114     if (GSS_ERROR(major)) {
115         gss_release_buffer(&tmpMinor, buffer);
116     }
117
118     return major;
119 }
120
121 OM_uint32
122 gssEapDecodeInnerTokens(OM_uint32 *minor,
123                         const gss_buffer_t buffer,
124                         struct gss_eap_token_buffer_set *tokens)
125 {
126     OM_uint32 major, tmpMinor;
127     unsigned char *p;
128     size_t remain;
129
130     tokens->buffers.count = 0;
131     tokens->buffers.elements = NULL;
132     tokens->types = NULL;
133
134     if (buffer->length == 0) {
135         major = GSS_S_COMPLETE;
136         goto cleanup;
137     }
138
139     p = (unsigned char *)buffer->value;
140     remain = buffer->length;
141
142     do {
143         OM_uint32 *ntypes;
144         gss_buffer_desc tokenBuffer, *newTokenBuffers;
145
146         if (remain < 8) {
147             major = GSS_S_DEFECTIVE_TOKEN;
148             *minor = GSSEAP_TOK_TRUNC;
149             goto cleanup;
150         }
151
152         ntypes = GSSEAP_REALLOC(tokens->types,
153                                 (tokens->buffers.count + 1) * sizeof(OM_uint32));
154         if (ntypes == NULL) {
155             major = GSS_S_FAILURE;
156             *minor = ENOMEM;
157             goto cleanup;
158         }
159         tokens->types = ntypes;
160
161         tokens->types[tokens->buffers.count] = load_uint32_be(&p[0]);
162         tokenBuffer.length = load_uint32_be(&p[4]);
163
164         if (remain < 8 + tokenBuffer.length) {
165             major = GSS_S_DEFECTIVE_TOKEN;
166             *minor = GSSEAP_TOK_TRUNC;
167             goto cleanup;
168         }
169         tokenBuffer.value = &p[8];
170
171         newTokenBuffers = GSSEAP_REALLOC(tokens->buffers.elements,
172                                          (tokens->buffers.count + 1) * sizeof(gss_buffer_desc));
173         if (newTokenBuffers == NULL) {
174             major = GSS_S_FAILURE;
175             *minor = ENOMEM;
176             goto cleanup;
177         }
178
179         tokens->buffers.elements = newTokenBuffers;
180         tokens->buffers.elements[tokens->buffers.count] = tokenBuffer;
181         tokens->buffers.count++;
182
183         p      += 8 + tokenBuffer.length;
184         remain -= 8 + tokenBuffer.length;
185
186     } while (remain != 0);
187
188     major = GSS_S_COMPLETE;
189     *minor = 0;
190
191 cleanup:
192     if (GSS_ERROR(major))
193         gssEapReleaseInnerTokens(&tmpMinor, tokens, 0);
194
195     return major;
196 }
197
198 /*
199  * $Id: util_token.c 23457 2009-12-08 00:04:48Z tlyu $
200  */
201
202 /* XXXX this code currently makes the assumption that a mech oid will
203    never be longer than 127 bytes.  This assumption is not inherent in
204    the interfaces, so the code can be fixed if the OSI namespace
205    balloons unexpectedly. */
206
207 /*
208  * Each token looks like this:
209  * 0x60                 tag for APPLICATION 0, SEQUENCE
210  *                              (constructed, definite-length)
211  * <length>             possible multiple bytes, need to parse/generate
212  * 0x06                 tag for OBJECT IDENTIFIER
213  * <moid_length>        compile-time constant string (assume 1 byte)
214  * <moid_bytes>         compile-time constant string
215  * <inner_bytes>        the ANY containing the application token
216  * bytes 0,1 are the token type
217  * bytes 2,n are the token data
218  *
219  * Note that the token type field is a feature of RFC 1964 mechanisms and
220  * is not used by other GSSAPI mechanisms.  As such, a token type of -1
221  * is interpreted to mean that no token type should be expected or
222  * generated.
223  *
224  * For the purposes of this abstraction, the token "header" consists of
225  * the sequence tag and length octets, the mech OID DER encoding, and the
226  * first two inner bytes, which indicate the token type.  The token
227  * "body" consists of everything else.
228  */
229
230 static size_t
231 der_length_size(size_t length)
232 {
233     if (length < (1<<7))
234         return 1;
235     else if (length < (1<<8))
236         return 2;
237 #if INT_MAX == 0x7fff
238     else
239         return 3;
240 #else
241     else if (length < (1<<16))
242         return 3;
243     else if (length < (1<<24))
244         return 4;
245     else
246         return 5;
247 #endif
248 }
249
250 static void
251 der_write_length(unsigned char **buf, size_t length)
252 {
253     if (length < (1<<7)) {
254         *(*buf)++ = (unsigned char)length;
255     } else {
256         *(*buf)++ = (unsigned char)(der_length_size(length)+127);
257 #if INT_MAX > 0x7fff
258         if (length >= (1<<24))
259             *(*buf)++ = (unsigned char)(length>>24);
260         if (length >= (1<<16))
261             *(*buf)++ = (unsigned char)((length>>16)&0xff);
262 #endif
263         if (length >= (1<<8))
264             *(*buf)++ = (unsigned char)((length>>8)&0xff);
265         *(*buf)++ = (unsigned char)(length&0xff);
266     }
267 }
268
269 /* returns decoded length, or < 0 on failure.  Advances buf and
270    decrements bufsize */
271
272 static int
273 der_read_length(unsigned char **buf, ssize_t *bufsize)
274 {
275     unsigned char sf;
276     int ret;
277
278     if (*bufsize < 1)
279         return -1;
280
281     sf = *(*buf)++;
282     (*bufsize)--;
283     if (sf & 0x80) {
284         if ((sf &= 0x7f) > ((*bufsize)-1))
285             return -1;
286         if (sf > sizeof(int))
287             return -1;
288         ret = 0;
289         for (; sf; sf--) {
290             ret = (ret<<8) + (*(*buf)++);
291             (*bufsize)--;
292         }
293     } else {
294         ret = sf;
295     }
296
297     return ret;
298 }
299
300 /* returns the length of a token, given the mech oid and the body size */
301
302 size_t
303 tokenSize(const gss_OID_desc *mech, size_t body_size)
304 {
305     assert(mech != GSS_C_NO_OID);
306
307     /* set body_size to sequence contents size */
308     body_size += 4 + (size_t) mech->length;         /* NEED overflow check */
309     return 1 + der_length_size(body_size) + body_size;
310 }
311
312 /* fills in a buffer with the token header.  The buffer is assumed to
313    be the right size.  buf is advanced past the token header */
314
315 void
316 makeTokenHeader(
317     const gss_OID_desc *mech,
318     size_t body_size,
319     unsigned char **buf,
320     enum gss_eap_token_type tok_type)
321 {
322     *(*buf)++ = 0x60;
323     der_write_length(buf, (tok_type == -1) ?2:4 + mech->length + body_size);
324     *(*buf)++ = 0x06;
325     *(*buf)++ = (unsigned char)mech->length;
326     memcpy(*buf, mech->elements, mech->length);
327     *buf += mech->length;
328     assert(tok_type != TOK_TYPE_NONE);
329     *(*buf)++ = (unsigned char)((tok_type>>8) & 0xff);
330     *(*buf)++ = (unsigned char)(tok_type & 0xff);
331 }
332
333 /*
334  * Given a buffer containing a token, reads and verifies the token,
335  * leaving buf advanced past the token header, and setting body_size
336  * to the number of remaining bytes.  Returns 0 on success,
337  * G_BAD_TOK_HEADER for a variety of errors, and G_WRONG_MECH if the
338  * mechanism in the token does not match the mech argument.  buf and
339  * *body_size are left unmodified on error.
340  */
341
342 OM_uint32
343 verifyTokenHeader(OM_uint32 *minor,
344                   gss_OID mech,
345                   size_t *body_size,
346                   unsigned char **buf_in,
347                   size_t toksize_in,
348                   enum gss_eap_token_type *ret_tok_type)
349 {
350     unsigned char *buf = *buf_in;
351     ssize_t seqsize;
352     gss_OID_desc toid;
353     ssize_t toksize = (ssize_t)toksize_in;
354
355     *minor = GSSEAP_BAD_TOK_HEADER;
356
357     if (ret_tok_type != NULL)
358         *ret_tok_type = TOK_TYPE_NONE;
359
360     if ((toksize -= 1) < 0)
361         return GSS_S_DEFECTIVE_TOKEN;
362
363     if (*buf++ != 0x60)
364         return GSS_S_DEFECTIVE_TOKEN;
365
366     seqsize = der_read_length(&buf, &toksize);
367     if (seqsize < 0)
368         return GSS_S_DEFECTIVE_TOKEN;
369
370     if (seqsize != toksize)
371         return GSS_S_DEFECTIVE_TOKEN;
372
373     if ((toksize -= 1) < 0)
374         return GSS_S_DEFECTIVE_TOKEN;
375
376     if (*buf++ != 0x06)
377         return GSS_S_DEFECTIVE_TOKEN;
378
379     if ((toksize -= 1) < 0)
380         return GSS_S_DEFECTIVE_TOKEN;
381
382     toid.length = *buf++;
383
384     if ((toksize -= toid.length) < 0)
385         return GSS_S_DEFECTIVE_TOKEN;
386
387     toid.elements = buf;
388     buf += toid.length;
389
390     if (mech->elements == NULL) {
391         *mech = toid;
392         if (toid.length == 0)
393             return GSS_S_BAD_MECH;
394     } else if (!oidEqual(&toid, mech)) {
395         *minor = GSSEAP_WRONG_MECH;
396         return GSS_S_BAD_MECH;
397     }
398
399     if (ret_tok_type != NULL) {
400         if ((toksize -= 2) < 0)
401             return GSS_S_DEFECTIVE_TOKEN;
402
403         *ret_tok_type = load_uint16_be(buf);
404         buf += 2;
405     }
406
407     *buf_in = buf;
408     *body_size = toksize;
409
410     *minor = 0;
411     return GSS_S_COMPLETE;
412 }
413
414 OM_uint32
415 gssEapAllocInnerTokens(OM_uint32 *minor,
416                        size_t count,
417                        struct gss_eap_token_buffer_set *tokens)
418 {
419     OM_uint32 major;
420
421     tokens->buffers.count = 0;
422     tokens->buffers.elements = (gss_buffer_desc *)GSSEAP_CALLOC(count, sizeof(gss_buffer_desc));
423     if (tokens->buffers.elements == NULL) {
424         major = GSS_S_FAILURE;
425         *minor = ENOMEM;
426         goto cleanup;
427     }
428
429     tokens->types = (OM_uint32 *)GSSEAP_CALLOC(count, sizeof(OM_uint32));
430     if (tokens->types == NULL) {
431         major = GSS_S_FAILURE;
432         *minor = ENOMEM;
433         goto cleanup;
434     }
435
436     major = GSS_S_COMPLETE;
437     *minor = 0;
438
439 cleanup:
440     if (GSS_ERROR(major)) {
441         if (tokens->buffers.elements != NULL) {
442             GSSEAP_FREE(tokens->buffers.elements);
443             tokens->buffers.elements = NULL;
444         }
445         if (tokens->types != NULL) {
446             GSSEAP_FREE(tokens->types);
447             tokens->types = NULL;
448         }
449     }
450
451     return major;
452 }
453
454 OM_uint32
455 gssEapReleaseInnerTokens(OM_uint32 *minor,
456                          struct gss_eap_token_buffer_set *tokens,
457                          int freeBuffers)
458 {
459     OM_uint32 tmpMinor;
460     size_t i;
461
462     if (tokens->buffers.elements != NULL) {
463         if (freeBuffers) {
464             for (i = 0; i < tokens->buffers.count; i++)
465                 gss_release_buffer(&tmpMinor, &tokens->buffers.elements[i]);
466         }
467         GSSEAP_FREE(tokens->buffers.elements);
468         tokens->buffers.elements = NULL;
469     }
470     tokens->buffers.count = 0;
471
472     if (tokens->types != NULL) {
473         GSSEAP_FREE(tokens->types);
474         tokens->types = NULL;
475     }
476
477     *minor = 0;
478     return GSS_S_COMPLETE;
479 }