b178da832c372bd9c63448c49ef912ade5937ed2
[mech_eap.git] / unwrap_iov.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 2008 by the Massachusetts Institute of Technology.
34  * All Rights Reserved.
35  *
36  * Export of this software from the United States of America may
37  *   require a specific license from the United States Government.
38  *   It is the responsibility of any person or organization contemplating
39  *   export to obtain such a license before exporting.
40  *
41  * WITHIN THAT CONSTRAINT, permission to use, copy, modify, and
42  * distribute this software and its documentation for any purpose and
43  * without fee is hereby granted, provided that the above copyright
44  * notice appear in all copies and that both that copyright notice and
45  * this permission notice appear in supporting documentation, and that
46  * the name of M.I.T. not be used in advertising or publicity pertaining
47  * to distribution of the software without specific, written prior
48  * permission.  Furthermore if you modify this software you must label
49  * your software as modified software and not distribute it in such a
50  * fashion that it might be confused with the original M.I.T. software.
51  * M.I.T. makes no representations about the suitability of
52  * this software for any purpose.  It is provided "as is" without express
53  * or implied warranty.
54  */
55
56 #include "gssapiP_eap.h"
57
58 /*
59  * Caller must provide TOKEN | DATA | PADDING | TRAILER, except
60  * for DCE in which case it can just provide TOKEN | DATA (must
61  * guarantee that DATA is padded)
62  */
63 OM_uint32
64 unwrapToken(OM_uint32 *minor,
65             gss_ctx_id_t ctx,
66             int *conf_state,
67             gss_qop_t *qop_state,
68             gss_iov_buffer_desc *iov,
69             int iov_count,
70             enum gss_eap_token_type toktype)
71 {
72     OM_uint32 code;
73     gss_iov_buffer_t header;
74     gss_iov_buffer_t padding;
75     gss_iov_buffer_t trailer;
76     unsigned char flags;
77     unsigned char *ptr = NULL;
78     int keyUsage;
79     size_t rrc, ec;
80     size_t dataLen, assocDataLen;
81     uint64_t seqnum;
82     int valid = 0;
83     int conf_flag = 0;
84     krb5_context krbContext;
85
86     GSSEAP_KRB_INIT(&krbContext);
87
88     *minor = 0;
89
90     if (qop_state != NULL)
91         *qop_state = GSS_C_QOP_DEFAULT;
92
93     header = gssEapLocateIov(iov, iov_count, GSS_IOV_BUFFER_TYPE_HEADER);
94     assert(header != NULL);
95
96     padding = gssEapLocateIov(iov, iov_count, GSS_IOV_BUFFER_TYPE_PADDING);
97     if (padding != NULL && padding->buffer.length != 0)
98         return GSS_S_DEFECTIVE_TOKEN;
99
100     trailer = gssEapLocateIov(iov, iov_count, GSS_IOV_BUFFER_TYPE_TRAILER);
101
102     flags = rfc4121Flags(ctx, TRUE);
103
104     switch (toktype) {
105     case TOK_TYPE_WRAP:
106         keyUsage = !CTX_IS_INITIATOR(ctx)
107                    ? KEY_USAGE_INITIATOR_SEAL
108                    : KEY_USAGE_ACCEPTOR_SEAL;
109         break;
110     case TOK_TYPE_GSS_CB:
111         keyUsage = KEY_USAGE_CHANNEL_BINDINGS;
112         break;
113     case TOK_TYPE_MIC:
114     default:
115         keyUsage = !CTX_IS_INITIATOR(ctx)
116                    ? KEY_USAGE_INITIATOR_SIGN
117                    : KEY_USAGE_ACCEPTOR_SIGN;
118         break;
119     }
120
121     gssEapIovMessageLength(iov, iov_count, &dataLen, &assocDataLen);
122
123     ptr = (unsigned char *)header->buffer.value;
124
125     if (header->buffer.length < 16)
126         return GSS_S_DEFECTIVE_TOKEN;
127
128     if ((ptr[2] & flags) != flags)
129         return GSS_S_BAD_SIG;
130
131     if (toktype == TOK_TYPE_WRAP) {
132         unsigned int krbTrailerLen;
133
134         if (load_uint16_be(ptr) != TOK_TYPE_WRAP)
135             goto defective;
136         conf_flag = ((ptr[2] & TOK_FLAG_WRAP_CONFIDENTIAL) != 0);
137         if (ptr[3] != 0xFF)
138             goto defective;
139         ec = load_uint16_be(ptr + 4);
140         rrc = load_uint16_be(ptr + 6);
141         seqnum = load_uint64_be(ptr + 8);
142
143         code = krb5_c_crypto_length(krbContext,
144                                     ctx->encryptionType,
145                                     conf_flag ? KRB5_CRYPTO_TYPE_TRAILER :
146                                     KRB5_CRYPTO_TYPE_CHECKSUM,
147                                     &krbTrailerLen);
148         if (code != 0) {
149             *minor = code;
150             return GSS_S_FAILURE;
151         }
152
153         /* Deal with RRC */
154         if (trailer == NULL) {
155             size_t desired_rrc = krbTrailerLen;
156
157             if (conf_flag) {
158                 desired_rrc += 16; /* E(Header) */
159
160                 if ((ctx->gssFlags & GSS_C_DCE_STYLE) == 0)
161                     desired_rrc += ec;
162             }
163
164             /* According to MS, we only need to deal with a fixed RRC for DCE */
165             if (rrc != desired_rrc)
166                 goto defective;
167         } else if (rrc != 0) {
168             goto defective;
169         }
170
171         if (conf_flag) {
172             unsigned char *althdr;
173
174             /* Decrypt */
175             code = gssEapDecrypt(krbContext,
176                                  ((ctx->gssFlags & GSS_C_DCE_STYLE) != 0),
177                                  ec, rrc, &ctx->rfc3961Key,
178                                  keyUsage, 0, iov, iov_count);
179             if (code != 0) {
180                 *minor = code;
181                 return GSS_S_BAD_SIG;
182             }
183
184             /* Validate header integrity */
185             if (trailer == NULL)
186                 althdr = (unsigned char *)header->buffer.value + 16 + ec;
187             else
188                 althdr = (unsigned char *)trailer->buffer.value + ec;
189
190             if (load_uint16_be(althdr) != TOK_TYPE_WRAP
191                 || althdr[2] != ptr[2]
192                 || althdr[3] != ptr[3]
193                 || memcmp(althdr + 8, ptr + 8, 8) != 0) {
194                 *minor = 0;
195                 return GSS_S_BAD_SIG;
196             }
197         } else {
198             /* Verify checksum: note EC is checksum size here, not padding */
199             if (ec != krbTrailerLen)
200                 goto defective;
201
202             /* Zero EC, RRC before computing checksum */
203             store_uint16_be(0, ptr + 4);
204             store_uint16_be(0, ptr + 6);
205
206             code = gssEapVerify(krbContext, ctx->checksumType, rrc,
207                                 &ctx->rfc3961Key, keyUsage,
208                                 iov, iov_count, &valid);
209             if (code != 0 || valid == FALSE) {
210                 *minor = code;
211                 return GSS_S_BAD_SIG;
212             }
213         }
214
215         code = sequenceCheck(minor, &ctx->seqState, seqnum);
216     } else if (toktype == TOK_TYPE_MIC || toktype == TOK_TYPE_GSS_CB) {
217         if (load_uint16_be(ptr) != toktype)
218             goto defective;
219
220     verify_mic_1:
221         if (ptr[3] != 0xFF)
222             goto defective;
223         seqnum = load_uint64_be(ptr + 8);
224
225         code = gssEapVerify(krbContext, ctx->checksumType, 0,
226                             &ctx->rfc3961Key, keyUsage,
227                             iov, iov_count, &valid);
228         if (code != 0 || valid == FALSE) {
229             *minor = code;
230             return GSS_S_BAD_SIG;
231         }
232         if (toktype != TOK_TYPE_GSS_CB)
233             code = sequenceCheck(minor, &ctx->seqState, seqnum);
234     } else if (toktype == TOK_TYPE_DELETE_CONTEXT) {
235         if (load_uint16_be(ptr) != TOK_TYPE_DELETE_CONTEXT)
236             goto defective;
237         goto verify_mic_1;
238     } else {
239         goto defective;
240     }
241
242     *minor = 0;
243
244     if (conf_state != NULL)
245         *conf_state = conf_flag;
246
247     return code;
248
249 defective:
250     *minor = 0;
251
252     return GSS_S_DEFECTIVE_TOKEN;
253 }
254
255 int
256 rotateLeft(void *ptr, size_t bufsiz, size_t rc)
257 {
258     void *tbuf;
259
260     if (bufsiz == 0)
261         return 0;
262     rc = rc % bufsiz;
263     if (rc == 0)
264         return 0;
265
266     tbuf = GSSEAP_MALLOC(rc);
267     if (tbuf == NULL)
268         return ENOMEM;
269
270     memcpy(tbuf, ptr, rc);
271     memmove(ptr, (char *)ptr + rc, bufsiz - rc);
272     memcpy((char *)ptr + bufsiz - rc, tbuf, rc);
273     GSSEAP_FREE(tbuf);
274
275     return 0;
276 }
277
278 /*
279  * Split a STREAM | SIGN_DATA | DATA into
280  *         HEADER | SIGN_DATA | DATA | PADDING | TRAILER
281  */
282 static OM_uint32
283 unwrapStream(OM_uint32 *minor,
284              gss_ctx_id_t ctx,
285              int *conf_state,
286              gss_qop_t *qop_state,
287              gss_iov_buffer_desc *iov,
288              int iov_count,
289              enum gss_eap_token_type toktype)
290 {
291     unsigned char *ptr;
292     OM_uint32 code = 0, major = GSS_S_FAILURE;
293     krb5_context krbContext;
294     int conf_req_flag, toktype2;
295     int i = 0, j;
296     gss_iov_buffer_desc *tiov = NULL;
297     gss_iov_buffer_t stream, data = NULL;
298     gss_iov_buffer_t theader, tdata = NULL, tpadding, ttrailer;
299
300     GSSEAP_KRB_INIT(&krbContext);
301
302     assert(toktype == TOK_TYPE_WRAP);
303
304     if (toktype != TOK_TYPE_WRAP || (ctx->gssFlags & GSS_C_DCE_STYLE)) {
305         code = EINVAL;
306         goto cleanup;
307     }
308
309     stream = gssEapLocateIov(iov, iov_count, GSS_IOV_BUFFER_TYPE_STREAM);
310     assert(stream != NULL);
311
312     if (stream->buffer.length < 16) {
313         major = GSS_S_DEFECTIVE_TOKEN;
314         goto cleanup;
315     }
316
317     ptr = (unsigned char *)stream->buffer.value;
318     toktype2 = load_uint16_be(ptr);
319     ptr += 2;
320
321     tiov = (gss_iov_buffer_desc *)GSSEAP_CALLOC((size_t)iov_count + 2,
322                                                 sizeof(gss_iov_buffer_desc));
323     if (tiov == NULL) {
324         code = ENOMEM;
325         goto cleanup;
326     }
327
328     /* HEADER */
329     theader = &tiov[i++];
330     theader->type = GSS_IOV_BUFFER_TYPE_HEADER;
331     theader->buffer.value = stream->buffer.value;
332     theader->buffer.length = 16;
333
334     /* n[SIGN_DATA] | DATA | m[SIGN_DATA] */
335     for (j = 0; j < iov_count; j++) {
336         OM_uint32 type = GSS_IOV_BUFFER_TYPE(iov[j].type);
337
338         if (type == GSS_IOV_BUFFER_TYPE_DATA) {
339             if (data != NULL) {
340                 /* only a single DATA buffer can appear */
341                 code = EINVAL;
342                 goto cleanup;
343             }
344
345             data = &iov[j];
346             tdata = &tiov[i];
347         }
348         if (type == GSS_IOV_BUFFER_TYPE_DATA ||
349             type == GSS_IOV_BUFFER_TYPE_SIGN_ONLY)
350             tiov[i++] = iov[j];
351     }
352
353     if (data == NULL) {
354         /* a single DATA buffer must be present */
355         code = EINVAL;
356         goto cleanup;
357     }
358
359     /* PADDING | TRAILER */
360     tpadding = &tiov[i++];
361     tpadding->type = GSS_IOV_BUFFER_TYPE_PADDING;
362     tpadding->buffer.length = 0;
363     tpadding->buffer.value = NULL;
364
365     ttrailer = &tiov[i++];
366     ttrailer->type = GSS_IOV_BUFFER_TYPE_TRAILER;
367
368     {
369         size_t ec, rrc;
370         unsigned int krbHeaderLen = 0;
371         unsigned int krbTrailerLen = 0;
372
373         conf_req_flag = ((ptr[0] & TOK_FLAG_WRAP_CONFIDENTIAL) != 0);
374         ec = conf_req_flag ? load_uint16_be(ptr + 2) : 0;
375         rrc = load_uint16_be(ptr + 4);
376
377         if (rrc != 0) {
378             code = rotateLeft((unsigned char *)stream->buffer.value + 16,
379                               stream->buffer.length - 16, rrc);
380             if (code != 0)
381                 goto cleanup;
382             store_uint16_be(0, ptr + 4); /* set RRC to zero */
383         }
384
385         if (conf_req_flag) {
386             code = krb5_c_crypto_length(krbContext, ctx->encryptionType,
387                                         KRB5_CRYPTO_TYPE_HEADER, &krbHeaderLen);
388             if (code != 0)
389                 goto cleanup;
390             theader->buffer.length += krbHeaderLen; /* length validated later */
391         }
392
393         /* no PADDING for CFX, EC is used instead */
394         code = krb5_c_crypto_length(krbContext, ctx->encryptionType,
395                                     conf_req_flag
396                                       ? KRB5_CRYPTO_TYPE_TRAILER
397                                       : KRB5_CRYPTO_TYPE_CHECKSUM,
398                                     &krbTrailerLen);
399         if (code != 0)
400             goto cleanup;
401
402         ttrailer->buffer.length = ec + (conf_req_flag ? 16 : 0 /* E(Header) */) +
403                                   krbTrailerLen;
404         ttrailer->buffer.value = (unsigned char *)stream->buffer.value +
405             stream->buffer.length - ttrailer->buffer.length;
406     }
407
408     /* IOV: -----------0-------------+---1---+--2--+----------------3--------------*/
409     /* Old: GSS-Header | Conf        | Data  | Pad |                               */
410     /* CFX: GSS-Header | Kerb-Header | Data  |     | EC | E(Header) | Kerb-Trailer */
411     /* GSS: -------GSS-HEADER--------+-DATA--+-PAD-+----------GSS-TRAILER----------*/
412
413     /* validate lengths */
414     if (stream->buffer.length < theader->buffer.length +
415         tpadding->buffer.length +
416         ttrailer->buffer.length) {
417         code = KRB5_BAD_MSIZE;
418         major = GSS_S_DEFECTIVE_TOKEN;
419         goto cleanup;
420     }
421
422     /* setup data */
423     tdata->buffer.length = stream->buffer.length - ttrailer->buffer.length -
424         tpadding->buffer.length - theader->buffer.length;
425
426     assert(data != NULL);
427
428     if (data->type & GSS_IOV_BUFFER_FLAG_ALLOCATE) {
429         code = gssEapAllocIov(tdata, tdata->buffer.length);
430         if (code != 0)
431             goto cleanup;
432
433         memcpy(tdata->buffer.value,
434                (unsigned char *)stream->buffer.value + theader->buffer.length,
435                tdata->buffer.length);
436     } else {
437         tdata->buffer.value = (unsigned char *)stream->buffer.value +
438                               theader->buffer.length;
439     }
440
441     assert(i <= iov_count + 2);
442
443     major = unwrapToken(&code, ctx, conf_state, qop_state,
444                         tiov, i, toktype);
445     if (major == GSS_S_COMPLETE) {
446         *data = *tdata;
447     } else if (tdata->type & GSS_IOV_BUFFER_FLAG_ALLOCATED) {
448         OM_uint32 tmp;
449
450         gss_release_buffer(&tmp, &tdata->buffer);
451         tdata->type &= ~(GSS_IOV_BUFFER_FLAG_ALLOCATED);
452     }
453
454 cleanup:
455     if (tiov != NULL)
456         GSSEAP_FREE(tiov);
457
458     *minor = code;
459
460     return major;
461 }
462
463 OM_uint32
464 gssEapUnwrapOrVerifyMIC(OM_uint32 *minor,
465                         gss_ctx_id_t ctx,
466                         int *conf_state,
467                         gss_qop_t *qop_state,
468                         gss_iov_buffer_desc *iov,
469                         int iov_count,
470                         enum gss_eap_token_type toktype)
471 {
472     OM_uint32 major;
473
474     if (!CTX_IS_ESTABLISHED(ctx))
475         return GSS_S_NO_CONTEXT;
476
477     if (ctx->encryptionType == ENCTYPE_NULL)
478         return GSS_S_UNAVAILABLE;
479
480     if (gssEapLocateIov(iov, iov_count, GSS_IOV_BUFFER_TYPE_STREAM) != NULL) {
481         major = unwrapStream(minor, ctx, conf_state, qop_state,
482                              iov, iov_count, toktype);
483     } else {
484         major = unwrapToken(minor, ctx, conf_state, qop_state,
485                             iov, iov_count, toktype);
486     }
487
488     return major;
489 }
490
491 OM_uint32
492 gss_unwrap_iov(OM_uint32 *minor,
493                gss_ctx_id_t ctx,
494                int *conf_state,
495                gss_qop_t *qop_state,
496                gss_iov_buffer_desc *iov,
497                int iov_count)
498 {
499     return gssEapUnwrapOrVerifyMIC(minor, ctx, conf_state, qop_state,
500                                    iov, iov_count, TOK_TYPE_WRAP);
501 }