Simplify verify_mic path
[moonshot.git] / moonshot / mech_eap / unwrap_iov.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  * 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 /*
57  * Message protection services: unwrap with scatter-gather API.
58  */
59
60 #include "gssapiP_eap.h"
61
62 /*
63  * Caller must provide TOKEN | DATA | PADDING | TRAILER, except
64  * for DCE in which case it can just provide TOKEN | DATA (must
65  * guarantee that DATA is padded)
66  */
67 OM_uint32
68 unwrapToken(OM_uint32 *minor,
69             gss_ctx_id_t ctx,
70 #ifdef HAVE_HEIMDAL_VERSION
71             krb5_crypto krbCrypto,
72 #else
73             krb5_keyblock *unused GSSEAP_UNUSED,
74 #endif
75             int *conf_state,
76             gss_qop_t *qop_state,
77             gss_iov_buffer_desc *iov,
78             int iov_count,
79             enum gss_eap_token_type toktype)
80 {
81     OM_uint32 major = GSS_S_FAILURE, code;
82     gss_iov_buffer_t header;
83     gss_iov_buffer_t padding;
84     gss_iov_buffer_t trailer;
85     unsigned char flags;
86     unsigned char *ptr = NULL;
87     int keyUsage;
88     size_t rrc, ec;
89     size_t dataLen, assocDataLen;
90     uint64_t seqnum;
91     int valid = 0;
92     int conf_flag = 0;
93     krb5_context krbContext;
94 #ifdef HAVE_HEIMDAL_VERSION
95     int freeCrypto = (krbCrypto == NULL);
96 #endif
97
98     GSSEAP_KRB_INIT(&krbContext);
99
100     *minor = 0;
101
102     if (qop_state != NULL)
103         *qop_state = GSS_C_QOP_DEFAULT;
104
105     header = gssEapLocateIov(iov, iov_count, GSS_IOV_BUFFER_TYPE_HEADER);
106     GSSEAP_ASSERT(header != NULL);
107
108     padding = gssEapLocateIov(iov, iov_count, GSS_IOV_BUFFER_TYPE_PADDING);
109     if (padding != NULL && padding->buffer.length != 0) {
110         code = GSSEAP_BAD_PADDING_IOV;
111         major = GSS_S_DEFECTIVE_TOKEN;
112         goto cleanup;
113     }
114
115     trailer = gssEapLocateIov(iov, iov_count, GSS_IOV_BUFFER_TYPE_TRAILER);
116
117     flags = rfc4121Flags(ctx, TRUE);
118
119     if (toktype == TOK_TYPE_WRAP) {
120         keyUsage = !CTX_IS_INITIATOR(ctx)
121                    ? KEY_USAGE_INITIATOR_SEAL
122                    : KEY_USAGE_ACCEPTOR_SEAL;
123     } else {
124         keyUsage = !CTX_IS_INITIATOR(ctx)
125                    ? KEY_USAGE_INITIATOR_SIGN
126                    : KEY_USAGE_ACCEPTOR_SIGN;
127     }
128
129     gssEapIovMessageLength(iov, iov_count, &dataLen, &assocDataLen);
130
131     ptr = (unsigned char *)header->buffer.value;
132
133     if (header->buffer.length < 16) {
134         code = GSSEAP_TOK_TRUNC;
135         major = GSS_S_DEFECTIVE_TOKEN;
136         goto cleanup;
137     }
138
139     if ((ptr[2] & flags) != flags) {
140         code = GSSEAP_BAD_DIRECTION;
141         major = GSS_S_BAD_SIG;
142         goto cleanup;
143     }
144
145 #ifdef HAVE_HEIMDAL_VERSION
146     if (krbCrypto == NULL) {
147         code = krb5_crypto_init(krbContext, &ctx->rfc3961Key,
148                                 ETYPE_NULL, &krbCrypto);
149         if (code != 0)
150             goto cleanup;
151     }
152 #endif
153
154     if (toktype == TOK_TYPE_WRAP) {
155         size_t krbTrailerLen;
156
157         if (load_uint16_be(ptr) != TOK_TYPE_WRAP)
158             goto defective;
159         conf_flag = ((ptr[2] & TOK_FLAG_WRAP_CONFIDENTIAL) != 0);
160         if (ptr[3] != 0xFF)
161             goto defective;
162         ec = load_uint16_be(ptr + 4);
163         rrc = load_uint16_be(ptr + 6);
164         seqnum = load_uint64_be(ptr + 8);
165
166         code = krbCryptoLength(krbContext, KRB_CRYPTO_CONTEXT(ctx),
167                                conf_flag ? KRB5_CRYPTO_TYPE_TRAILER :
168                                            KRB5_CRYPTO_TYPE_CHECKSUM,
169                                &krbTrailerLen);
170         if (code != 0)
171             goto cleanup;
172
173         /* Deal with RRC */
174         if (trailer == NULL) {
175             size_t desired_rrc = krbTrailerLen;
176
177             if (conf_flag) {
178                 desired_rrc += 16; /* E(Header) */
179
180                 if ((ctx->gssFlags & GSS_C_DCE_STYLE) == 0)
181                     desired_rrc += ec;
182             }
183
184             /* According to MS, we only need to deal with a fixed RRC for DCE */
185             if (rrc != desired_rrc)
186                 goto defective;
187         } else if (rrc != 0) {
188             goto defective;
189         }
190
191         if (conf_flag) {
192             unsigned char *althdr;
193
194             /* Decrypt */
195             code = gssEapDecrypt(krbContext,
196                                  ((ctx->gssFlags & GSS_C_DCE_STYLE) != 0),
197                                  ec, rrc, KRB_CRYPTO_CONTEXT(ctx), keyUsage,
198                                  iov, iov_count);
199             if (code != 0) {
200                 major = GSS_S_BAD_SIG;
201                 goto cleanup;
202             }
203
204             /* Validate header integrity */
205             if (trailer == NULL)
206                 althdr = (unsigned char *)header->buffer.value + 16 + ec;
207             else
208                 althdr = (unsigned char *)trailer->buffer.value + ec;
209
210             if (load_uint16_be(althdr) != TOK_TYPE_WRAP
211                 || althdr[2] != ptr[2]
212                 || althdr[3] != ptr[3]
213                 || memcmp(althdr + 8, ptr + 8, 8) != 0) {
214                 code = GSSEAP_BAD_WRAP_TOKEN;
215                 major = GSS_S_BAD_SIG;
216                 goto cleanup;
217             }
218         } else {
219             /* Verify checksum: note EC is checksum size here, not padding */
220             if (ec != krbTrailerLen)
221                 goto defective;
222
223             /* Zero EC, RRC before computing checksum */
224             store_uint16_be(0, ptr + 4);
225             store_uint16_be(0, ptr + 6);
226
227             code = gssEapVerify(krbContext, ctx->checksumType, rrc,
228                                 KRB_CRYPTO_CONTEXT(ctx), keyUsage,
229                                 iov, iov_count, &valid);
230             if (code != 0 || valid == FALSE) {
231                 major = GSS_S_BAD_SIG;
232                 goto cleanup;
233             }
234         }
235
236         code = sequenceCheck(minor, &ctx->seqState, seqnum);
237     } else if (toktype == TOK_TYPE_MIC) {
238         if (load_uint16_be(ptr) != toktype)
239             goto defective;
240
241     verify_mic_1:
242         if (ptr[3] != 0xFF)
243             goto defective;
244         seqnum = load_uint64_be(ptr + 8);
245
246         /*
247          * Although MIC tokens don't have a RRC, they are similarly
248          * composed of a header and a checksum. So the verify_mic()
249          * can be implemented with a single header buffer, fake the
250          * RRC to the putative trailer length if no trailer buffer.
251          */
252         code = gssEapVerify(krbContext, ctx->checksumType,
253                             trailer != NULL ? 0 : header->buffer.length - 16,
254                             KRB_CRYPTO_CONTEXT(ctx), keyUsage,
255                             iov, iov_count, &valid);
256         if (code != 0 || valid == FALSE) {
257             major = GSS_S_BAD_SIG;
258             goto cleanup;
259         }
260         code = sequenceCheck(minor, &ctx->seqState, seqnum);
261     } else if (toktype == TOK_TYPE_DELETE_CONTEXT) {
262         if (load_uint16_be(ptr) != TOK_TYPE_DELETE_CONTEXT)
263             goto defective;
264         goto verify_mic_1;
265     } else {
266         goto defective;
267     }
268
269     if (conf_state != NULL)
270         *conf_state = conf_flag;
271
272     code = 0;
273     major = GSS_S_COMPLETE;
274     goto cleanup;
275
276 defective:
277     code = GSSEAP_BAD_WRAP_TOKEN;
278     major = GSS_S_DEFECTIVE_TOKEN;
279
280 cleanup:
281     *minor = code;
282 #ifdef HAVE_HEIMDAL_VERSION
283     if (freeCrypto && krbCrypto != NULL)
284         krb5_crypto_destroy(krbContext, krbCrypto);
285 #endif
286
287     return major;
288 }
289
290 int
291 rotateLeft(void *ptr, size_t bufsiz, size_t rc)
292 {
293     void *tbuf;
294
295     if (bufsiz == 0)
296         return 0;
297     rc = rc % bufsiz;
298     if (rc == 0)
299         return 0;
300
301     tbuf = GSSEAP_MALLOC(rc);
302     if (tbuf == NULL)
303         return ENOMEM;
304
305     memcpy(tbuf, ptr, rc);
306     memmove(ptr, (char *)ptr + rc, bufsiz - rc);
307     memcpy((char *)ptr + bufsiz - rc, tbuf, rc);
308     GSSEAP_FREE(tbuf);
309
310     return 0;
311 }
312
313 /*
314  * Split a STREAM | SIGN_DATA | DATA into
315  *         HEADER | SIGN_DATA | DATA | PADDING | TRAILER
316  */
317 static OM_uint32
318 unwrapStream(OM_uint32 *minor,
319              gss_ctx_id_t ctx,
320              int *conf_state,
321              gss_qop_t *qop_state,
322              gss_iov_buffer_desc *iov,
323              int iov_count,
324              enum gss_eap_token_type toktype)
325 {
326     unsigned char *ptr;
327     OM_uint32 code = 0, major = GSS_S_FAILURE;
328     krb5_context krbContext;
329     int conf_req_flag;
330     int i = 0, j;
331     gss_iov_buffer_desc *tiov = NULL;
332     gss_iov_buffer_t stream, data = NULL;
333     gss_iov_buffer_t theader, tdata = NULL, tpadding, ttrailer;
334 #ifdef HAVE_HEIMDAL_VERSION
335     krb5_crypto krbCrypto = NULL;
336 #endif
337
338     GSSEAP_KRB_INIT(&krbContext);
339
340     GSSEAP_ASSERT(toktype == TOK_TYPE_WRAP);
341
342     if (toktype != TOK_TYPE_WRAP) {
343         code = GSSEAP_WRONG_TOK_ID;
344         goto cleanup;
345     }
346
347     stream = gssEapLocateIov(iov, iov_count, GSS_IOV_BUFFER_TYPE_STREAM);
348     GSSEAP_ASSERT(stream != NULL);
349
350     if (stream->buffer.length < 16) {
351         major = GSS_S_DEFECTIVE_TOKEN;
352         goto cleanup;
353     }
354
355     ptr = (unsigned char *)stream->buffer.value;
356     ptr += 2; /* skip token type */
357
358     tiov = (gss_iov_buffer_desc *)GSSEAP_CALLOC((size_t)iov_count + 2,
359                                                 sizeof(gss_iov_buffer_desc));
360     if (tiov == NULL) {
361         code = ENOMEM;
362         goto cleanup;
363     }
364
365     /* HEADER */
366     theader = &tiov[i++];
367     theader->type = GSS_IOV_BUFFER_TYPE_HEADER;
368     theader->buffer.value = stream->buffer.value;
369     theader->buffer.length = 16;
370
371     /* n[SIGN_DATA] | DATA | m[SIGN_DATA] */
372     for (j = 0; j < iov_count; j++) {
373         OM_uint32 type = GSS_IOV_BUFFER_TYPE(iov[j].type);
374
375         if (type == GSS_IOV_BUFFER_TYPE_DATA) {
376             if (data != NULL) {
377                 /* only a single DATA buffer can appear */
378                 code = GSSEAP_BAD_STREAM_IOV;
379                 goto cleanup;
380             }
381
382             data = &iov[j];
383             tdata = &tiov[i];
384         }
385         if (type == GSS_IOV_BUFFER_TYPE_DATA ||
386             type == GSS_IOV_BUFFER_TYPE_SIGN_ONLY)
387             tiov[i++] = iov[j];
388     }
389
390     if (data == NULL) {
391         /* a single DATA buffer must be present */
392         code = GSSEAP_BAD_STREAM_IOV;
393         goto cleanup;
394     }
395
396     /* PADDING | TRAILER */
397     tpadding = &tiov[i++];
398     tpadding->type = GSS_IOV_BUFFER_TYPE_PADDING;
399     tpadding->buffer.length = 0;
400     tpadding->buffer.value = NULL;
401
402     ttrailer = &tiov[i++];
403     ttrailer->type = GSS_IOV_BUFFER_TYPE_TRAILER;
404
405 #ifdef HAVE_HEIMDAL_VERSION
406     code = krb5_crypto_init(krbContext, &ctx->rfc3961Key, ETYPE_NULL, &krbCrypto);
407     if (code != 0)
408         goto cleanup;
409 #endif
410
411     {
412         size_t ec, rrc;
413         size_t krbHeaderLen = 0;
414         size_t krbTrailerLen = 0;
415
416         conf_req_flag = ((ptr[0] & TOK_FLAG_WRAP_CONFIDENTIAL) != 0);
417         ec = conf_req_flag ? load_uint16_be(ptr + 2) : 0;
418         rrc = load_uint16_be(ptr + 4);
419
420         if (rrc != 0) {
421             code = rotateLeft((unsigned char *)stream->buffer.value + 16,
422                               stream->buffer.length - 16, rrc);
423             if (code != 0)
424                 goto cleanup;
425             store_uint16_be(0, ptr + 4); /* set RRC to zero */
426         }
427
428         if (conf_req_flag) {
429             code = krbCryptoLength(krbContext, KRB_CRYPTO_CONTEXT(ctx),
430                                     KRB5_CRYPTO_TYPE_HEADER, &krbHeaderLen);
431             if (code != 0)
432                 goto cleanup;
433             theader->buffer.length += krbHeaderLen; /* length validated later */
434         }
435
436         /* no PADDING for CFX, EC is used instead */
437         code = krbCryptoLength(krbContext, KRB_CRYPTO_CONTEXT(ctx),
438                                conf_req_flag
439                                   ? KRB5_CRYPTO_TYPE_TRAILER
440                                   : KRB5_CRYPTO_TYPE_CHECKSUM,
441                                &krbTrailerLen);
442         if (code != 0)
443             goto cleanup;
444
445         ttrailer->buffer.length = ec + (conf_req_flag ? 16 : 0 /* E(Header) */) +
446                                   krbTrailerLen;
447         ttrailer->buffer.value = (unsigned char *)stream->buffer.value +
448             stream->buffer.length - ttrailer->buffer.length;
449     }
450
451     /* IOV: -----------0-------------+---1---+--2--+----------------3--------------*/
452     /* CFX: GSS-Header | Kerb-Header | Data  |     | EC | E(Header) | Kerb-Trailer */
453     /* GSS: -------GSS-HEADER--------+-DATA--+-PAD-+----------GSS-TRAILER----------*/
454
455     /* validate lengths */
456     if (stream->buffer.length < theader->buffer.length +
457         tpadding->buffer.length +
458         ttrailer->buffer.length) {
459         major = GSS_S_DEFECTIVE_TOKEN;
460         code = GSSEAP_TOK_TRUNC;
461         goto cleanup;
462     }
463
464     /* setup data */
465     tdata->buffer.length = stream->buffer.length - ttrailer->buffer.length -
466         tpadding->buffer.length - theader->buffer.length;
467
468     GSSEAP_ASSERT(data != NULL);
469
470     if (data->type & GSS_IOV_BUFFER_FLAG_ALLOCATE) {
471         code = gssEapAllocIov(tdata, tdata->buffer.length);
472         if (code != 0)
473             goto cleanup;
474
475         memcpy(tdata->buffer.value,
476                (unsigned char *)stream->buffer.value + theader->buffer.length,
477                tdata->buffer.length);
478     } else {
479         tdata->buffer.value = (unsigned char *)stream->buffer.value +
480                               theader->buffer.length;
481     }
482
483     GSSEAP_ASSERT(i <= iov_count + 2);
484
485     major = unwrapToken(&code, ctx, KRB_CRYPTO_CONTEXT(ctx),
486                         conf_state, qop_state, tiov, i, toktype);
487     if (major == GSS_S_COMPLETE) {
488         *data = *tdata;
489     } else if (tdata->type & GSS_IOV_BUFFER_FLAG_ALLOCATED) {
490         OM_uint32 tmp;
491
492         gss_release_buffer(&tmp, &tdata->buffer);
493         tdata->type &= ~(GSS_IOV_BUFFER_FLAG_ALLOCATED);
494     }
495
496 cleanup:
497     if (tiov != NULL)
498         GSSEAP_FREE(tiov);
499 #ifdef HAVE_HEIMDAL_VERSION
500     if (krbCrypto != NULL)
501         krb5_crypto_destroy(krbContext, krbCrypto);
502 #endif
503
504     *minor = code;
505
506     return major;
507 }
508
509 OM_uint32
510 gssEapUnwrapOrVerifyMIC(OM_uint32 *minor,
511                         gss_ctx_id_t ctx,
512                         int *conf_state,
513                         gss_qop_t *qop_state,
514                         gss_iov_buffer_desc *iov,
515                         int iov_count,
516                         enum gss_eap_token_type toktype)
517 {
518     OM_uint32 major;
519
520     if (ctx->encryptionType == ENCTYPE_NULL) {
521         *minor = GSSEAP_KEY_UNAVAILABLE;
522         return GSS_S_UNAVAILABLE;
523     }
524
525     if (gssEapLocateIov(iov, iov_count, GSS_IOV_BUFFER_TYPE_STREAM) != NULL) {
526         major = unwrapStream(minor, ctx, conf_state, qop_state,
527                              iov, iov_count, toktype);
528     } else {
529         major = unwrapToken(minor, ctx,
530                             NULL, /* krbCrypto */
531                             conf_state, qop_state,
532                             iov, iov_count, toktype);
533     }
534
535     return major;
536 }
537
538 OM_uint32 GSSAPI_CALLCONV
539 gss_unwrap_iov(OM_uint32 *minor,
540                gss_ctx_id_t ctx,
541                int *conf_state,
542                gss_qop_t *qop_state,
543                gss_iov_buffer_desc *iov,
544                int iov_count)
545 {
546     OM_uint32 major;
547
548     if (ctx == GSS_C_NO_CONTEXT) {
549         *minor = EINVAL;
550         return GSS_S_CALL_INACCESSIBLE_READ | GSS_S_NO_CONTEXT;
551     }
552
553     *minor = 0;
554
555     GSSEAP_MUTEX_LOCK(&ctx->mutex);
556
557     if (!CTX_IS_ESTABLISHED(ctx)) {
558         major = GSS_S_NO_CONTEXT;
559         *minor = GSSEAP_CONTEXT_INCOMPLETE;
560         goto cleanup;
561     }
562
563     major = gssEapUnwrapOrVerifyMIC(minor, ctx, conf_state, qop_state,
564                                     iov, iov_count, TOK_TYPE_WRAP);
565     if (GSS_ERROR(major))
566         goto cleanup;
567
568 cleanup:
569     GSSEAP_MUTEX_UNLOCK(&ctx->mutex);
570
571     return major;
572 }