705cbc1290514b6aa88e8e15e300bcd6a1f85320
[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     if (toktype == TOK_TYPE_WRAP) {
105         keyUsage = !CTX_IS_INITIATOR(ctx)
106                    ? KEY_USAGE_INITIATOR_SEAL
107                    : KEY_USAGE_ACCEPTOR_SEAL;
108     } else {
109         keyUsage = !CTX_IS_INITIATOR(ctx)
110                    ? KEY_USAGE_INITIATOR_SIGN
111                    : KEY_USAGE_ACCEPTOR_SIGN;
112     }
113
114     gssEapIovMessageLength(iov, iov_count, &dataLen, &assocDataLen);
115
116     ptr = (unsigned char *)header->buffer.value;
117
118     if (header->buffer.length < 16)
119         return GSS_S_DEFECTIVE_TOKEN;
120
121     if ((ptr[2] & flags) != flags)
122         return GSS_S_BAD_SIG;
123
124     if (toktype == TOK_TYPE_WRAP) {
125         unsigned int krbTrailerLen;
126
127         if (load_uint16_be(ptr) != TOK_TYPE_WRAP)
128             goto defective;
129         conf_flag = ((ptr[2] & TOK_FLAG_WRAP_CONFIDENTIAL) != 0);
130         if (ptr[3] != 0xFF)
131             goto defective;
132         ec = load_uint16_be(ptr + 4);
133         rrc = load_uint16_be(ptr + 6);
134         seqnum = load_uint64_be(ptr + 8);
135
136         code = krb5_c_crypto_length(krbContext,
137                                     ctx->encryptionType,
138                                     conf_flag ? KRB5_CRYPTO_TYPE_TRAILER :
139                                     KRB5_CRYPTO_TYPE_CHECKSUM,
140                                     &krbTrailerLen);
141         if (code != 0) {
142             *minor = code;
143             return GSS_S_FAILURE;
144         }
145
146         /* Deal with RRC */
147         if (trailer == NULL) {
148             size_t desired_rrc = krbTrailerLen;
149
150             if (conf_flag) {
151                 desired_rrc += 16; /* E(Header) */
152
153                 if ((ctx->gssFlags & GSS_C_DCE_STYLE) == 0)
154                     desired_rrc += ec;
155             }
156
157             /* According to MS, we only need to deal with a fixed RRC for DCE */
158             if (rrc != desired_rrc)
159                 goto defective;
160         } else if (rrc != 0) {
161             goto defective;
162         }
163
164         if (conf_flag) {
165             unsigned char *althdr;
166
167             /* Decrypt */
168             code = gssEapDecrypt(krbContext,
169                                  ((ctx->gssFlags & GSS_C_DCE_STYLE) != 0),
170                                  ec, rrc, &ctx->rfc3961Key,
171                                  keyUsage, 0, iov, iov_count);
172             if (code != 0) {
173                 *minor = code;
174                 return GSS_S_BAD_SIG;
175             }
176
177             /* Validate header integrity */
178             if (trailer == NULL)
179                 althdr = (unsigned char *)header->buffer.value + 16 + ec;
180             else
181                 althdr = (unsigned char *)trailer->buffer.value + ec;
182
183             if (load_uint16_be(althdr) != TOK_TYPE_WRAP
184                 || althdr[2] != ptr[2]
185                 || althdr[3] != ptr[3]
186                 || memcmp(althdr + 8, ptr + 8, 8) != 0) {
187                 *minor = 0;
188                 return GSS_S_BAD_SIG;
189             }
190         } else {
191             /* Verify checksum: note EC is checksum size here, not padding */
192             if (ec != krbTrailerLen)
193                 goto defective;
194
195             /* Zero EC, RRC before computing checksum */
196             store_uint16_be(0, ptr + 4);
197             store_uint16_be(0, ptr + 6);
198
199             code = gssEapVerify(krbContext, ctx->checksumType, rrc,
200                                 &ctx->rfc3961Key, keyUsage,
201                                 iov, iov_count, &valid);
202             if (code != 0 || valid == FALSE) {
203                 *minor = code;
204                 return GSS_S_BAD_SIG;
205             }
206         }
207
208         code = sequenceCheck(minor, &ctx->seqState, seqnum);
209     } else if (toktype == TOK_TYPE_MIC) {
210         if (load_uint16_be(ptr) != toktype)
211             goto defective;
212
213     verify_mic_1:
214         if (ptr[3] != 0xFF)
215             goto defective;
216         seqnum = load_uint64_be(ptr + 8);
217
218         code = gssEapVerify(krbContext, ctx->checksumType, 0,
219                             &ctx->rfc3961Key, keyUsage,
220                             iov, iov_count, &valid);
221         if (code != 0 || valid == FALSE) {
222             *minor = code;
223             return GSS_S_BAD_SIG;
224         }
225         code = sequenceCheck(minor, &ctx->seqState, seqnum);
226     } else if (toktype == TOK_TYPE_DELETE_CONTEXT) {
227         if (load_uint16_be(ptr) != TOK_TYPE_DELETE_CONTEXT)
228             goto defective;
229         goto verify_mic_1;
230     } else {
231         goto defective;
232     }
233
234     *minor = 0;
235
236     if (conf_state != NULL)
237         *conf_state = conf_flag;
238
239     return code;
240
241 defective:
242     *minor = 0;
243
244     return GSS_S_DEFECTIVE_TOKEN;
245 }
246
247 int
248 rotateLeft(void *ptr, size_t bufsiz, size_t rc)
249 {
250     void *tbuf;
251
252     if (bufsiz == 0)
253         return 0;
254     rc = rc % bufsiz;
255     if (rc == 0)
256         return 0;
257
258     tbuf = GSSEAP_MALLOC(rc);
259     if (tbuf == NULL)
260         return ENOMEM;
261
262     memcpy(tbuf, ptr, rc);
263     memmove(ptr, (char *)ptr + rc, bufsiz - rc);
264     memcpy((char *)ptr + bufsiz - rc, tbuf, rc);
265     GSSEAP_FREE(tbuf);
266
267     return 0;
268 }
269
270 /*
271  * Split a STREAM | SIGN_DATA | DATA into
272  *         HEADER | SIGN_DATA | DATA | PADDING | TRAILER
273  */
274 static OM_uint32
275 unwrapStream(OM_uint32 *minor,
276              gss_ctx_id_t ctx,
277              int *conf_state,
278              gss_qop_t *qop_state,
279              gss_iov_buffer_desc *iov,
280              int iov_count,
281              enum gss_eap_token_type toktype)
282 {
283     unsigned char *ptr;
284     OM_uint32 code = 0, major = GSS_S_FAILURE;
285     krb5_context krbContext;
286     int conf_req_flag, toktype2;
287     int i = 0, j;
288     gss_iov_buffer_desc *tiov = NULL;
289     gss_iov_buffer_t stream, data = NULL;
290     gss_iov_buffer_t theader, tdata = NULL, tpadding, ttrailer;
291
292     GSSEAP_KRB_INIT(&krbContext);
293
294     assert(toktype == TOK_TYPE_WRAP);
295
296     if (toktype != TOK_TYPE_WRAP) {
297         code = EINVAL;
298         goto cleanup;
299     }
300
301     stream = gssEapLocateIov(iov, iov_count, GSS_IOV_BUFFER_TYPE_STREAM);
302     assert(stream != NULL);
303
304     if (stream->buffer.length < 16) {
305         major = GSS_S_DEFECTIVE_TOKEN;
306         goto cleanup;
307     }
308
309     ptr = (unsigned char *)stream->buffer.value;
310     toktype2 = load_uint16_be(ptr);
311     ptr += 2;
312
313     tiov = (gss_iov_buffer_desc *)GSSEAP_CALLOC((size_t)iov_count + 2,
314                                                 sizeof(gss_iov_buffer_desc));
315     if (tiov == NULL) {
316         code = ENOMEM;
317         goto cleanup;
318     }
319
320     /* HEADER */
321     theader = &tiov[i++];
322     theader->type = GSS_IOV_BUFFER_TYPE_HEADER;
323     theader->buffer.value = stream->buffer.value;
324     theader->buffer.length = 16;
325
326     /* n[SIGN_DATA] | DATA | m[SIGN_DATA] */
327     for (j = 0; j < iov_count; j++) {
328         OM_uint32 type = GSS_IOV_BUFFER_TYPE(iov[j].type);
329
330         if (type == GSS_IOV_BUFFER_TYPE_DATA) {
331             if (data != NULL) {
332                 /* only a single DATA buffer can appear */
333                 code = EINVAL;
334                 goto cleanup;
335             }
336
337             data = &iov[j];
338             tdata = &tiov[i];
339         }
340         if (type == GSS_IOV_BUFFER_TYPE_DATA ||
341             type == GSS_IOV_BUFFER_TYPE_SIGN_ONLY)
342             tiov[i++] = iov[j];
343     }
344
345     if (data == NULL) {
346         /* a single DATA buffer must be present */
347         code = EINVAL;
348         goto cleanup;
349     }
350
351     /* PADDING | TRAILER */
352     tpadding = &tiov[i++];
353     tpadding->type = GSS_IOV_BUFFER_TYPE_PADDING;
354     tpadding->buffer.length = 0;
355     tpadding->buffer.value = NULL;
356
357     ttrailer = &tiov[i++];
358     ttrailer->type = GSS_IOV_BUFFER_TYPE_TRAILER;
359
360     {
361         size_t ec, rrc;
362         unsigned int krbHeaderLen = 0;
363         unsigned int krbTrailerLen = 0;
364
365         conf_req_flag = ((ptr[0] & TOK_FLAG_WRAP_CONFIDENTIAL) != 0);
366         ec = conf_req_flag ? load_uint16_be(ptr + 2) : 0;
367         rrc = load_uint16_be(ptr + 4);
368
369         if (rrc != 0) {
370             code = rotateLeft((unsigned char *)stream->buffer.value + 16,
371                               stream->buffer.length - 16, rrc);
372             if (code != 0)
373                 goto cleanup;
374             store_uint16_be(0, ptr + 4); /* set RRC to zero */
375         }
376
377         if (conf_req_flag) {
378             code = krb5_c_crypto_length(krbContext, ctx->encryptionType,
379                                         KRB5_CRYPTO_TYPE_HEADER, &krbHeaderLen);
380             if (code != 0)
381                 goto cleanup;
382             theader->buffer.length += krbHeaderLen; /* length validated later */
383         }
384
385         /* no PADDING for CFX, EC is used instead */
386         code = krb5_c_crypto_length(krbContext, ctx->encryptionType,
387                                     conf_req_flag
388                                       ? KRB5_CRYPTO_TYPE_TRAILER
389                                       : KRB5_CRYPTO_TYPE_CHECKSUM,
390                                     &krbTrailerLen);
391         if (code != 0)
392             goto cleanup;
393
394         ttrailer->buffer.length = ec + (conf_req_flag ? 16 : 0 /* E(Header) */) +
395                                   krbTrailerLen;
396         ttrailer->buffer.value = (unsigned char *)stream->buffer.value +
397             stream->buffer.length - ttrailer->buffer.length;
398     }
399
400     /* IOV: -----------0-------------+---1---+--2--+----------------3--------------*/
401     /* CFX: GSS-Header | Kerb-Header | Data  |     | EC | E(Header) | Kerb-Trailer */
402     /* GSS: -------GSS-HEADER--------+-DATA--+-PAD-+----------GSS-TRAILER----------*/
403
404     /* validate lengths */
405     if (stream->buffer.length < theader->buffer.length +
406         tpadding->buffer.length +
407         ttrailer->buffer.length) {
408         code = KRB5_BAD_MSIZE;
409         major = GSS_S_DEFECTIVE_TOKEN;
410         goto cleanup;
411     }
412
413     /* setup data */
414     tdata->buffer.length = stream->buffer.length - ttrailer->buffer.length -
415         tpadding->buffer.length - theader->buffer.length;
416
417     assert(data != NULL);
418
419     if (data->type & GSS_IOV_BUFFER_FLAG_ALLOCATE) {
420         code = gssEapAllocIov(tdata, tdata->buffer.length);
421         if (code != 0)
422             goto cleanup;
423
424         memcpy(tdata->buffer.value,
425                (unsigned char *)stream->buffer.value + theader->buffer.length,
426                tdata->buffer.length);
427     } else {
428         tdata->buffer.value = (unsigned char *)stream->buffer.value +
429                               theader->buffer.length;
430     }
431
432     assert(i <= iov_count + 2);
433
434     major = unwrapToken(&code, ctx, conf_state, qop_state,
435                         tiov, i, toktype);
436     if (major == GSS_S_COMPLETE) {
437         *data = *tdata;
438     } else if (tdata->type & GSS_IOV_BUFFER_FLAG_ALLOCATED) {
439         OM_uint32 tmp;
440
441         gss_release_buffer(&tmp, &tdata->buffer);
442         tdata->type &= ~(GSS_IOV_BUFFER_FLAG_ALLOCATED);
443     }
444
445 cleanup:
446     if (tiov != NULL)
447         GSSEAP_FREE(tiov);
448
449     *minor = code;
450
451     return major;
452 }
453
454 OM_uint32
455 gssEapUnwrapOrVerifyMIC(OM_uint32 *minor,
456                         gss_ctx_id_t ctx,
457                         int *conf_state,
458                         gss_qop_t *qop_state,
459                         gss_iov_buffer_desc *iov,
460                         int iov_count,
461                         enum gss_eap_token_type toktype)
462 {
463     OM_uint32 major;
464
465     if (ctx->encryptionType == ENCTYPE_NULL)
466         return GSS_S_UNAVAILABLE;
467
468     if (gssEapLocateIov(iov, iov_count, GSS_IOV_BUFFER_TYPE_STREAM) != NULL) {
469         major = unwrapStream(minor, ctx, conf_state, qop_state,
470                              iov, iov_count, toktype);
471     } else {
472         major = unwrapToken(minor, ctx, conf_state, qop_state,
473                             iov, iov_count, toktype);
474     }
475
476     return major;
477 }
478
479 OM_uint32
480 gss_unwrap_iov(OM_uint32 *minor,
481                gss_ctx_id_t ctx,
482                int *conf_state,
483                gss_qop_t *qop_state,
484                gss_iov_buffer_desc *iov,
485                int iov_count)
486 {
487     OM_uint32 major = GSS_S_NO_CONTEXT;
488
489     if (ctx == GSS_C_NO_CONTEXT) {
490         *minor = EINVAL;
491         return GSS_S_NO_CONTEXT;
492     }
493
494     *minor = 0;
495
496     GSSEAP_MUTEX_LOCK(&ctx->mutex);
497
498     if (CTX_IS_ESTABLISHED(ctx)) {
499         major = gssEapUnwrapOrVerifyMIC(minor, ctx, conf_state, qop_state,
500                                         iov, iov_count, TOK_TYPE_WRAP);
501     }
502
503     GSSEAP_MUTEX_UNLOCK(&ctx->mutex);
504
505     return major;
506 }