2 * Copyright (c) 2010, JANET(UK)
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
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.
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.
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
33 * Copyright 2008 by the Massachusetts Institute of Technology.
34 * All Rights Reserved.
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.
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.
56 #include "gssapiP_eap.h"
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)
64 unwrapToken(OM_uint32 *minor,
68 gss_iov_buffer_desc *iov,
70 enum gss_eap_token_type toktype)
73 gss_iov_buffer_t header;
74 gss_iov_buffer_t padding;
75 gss_iov_buffer_t trailer;
76 unsigned char acceptorFlag;
77 unsigned char *ptr = NULL;
80 size_t dataLen, assocDataLen;
83 krb5_cksumtype cksumtype;
85 krb5_context krbContext;
87 GSSEAP_KRB_INIT(&krbContext);
91 if (qop_state != NULL)
92 *qop_state = GSS_C_QOP_DEFAULT;
94 header = gssEapLocateIov(iov, iov_count, GSS_IOV_BUFFER_TYPE_HEADER);
95 assert(header != NULL);
97 padding = gssEapLocateIov(iov, iov_count, GSS_IOV_BUFFER_TYPE_PADDING);
98 if (padding != NULL && padding->buffer.length != 0)
99 return GSS_S_DEFECTIVE_TOKEN;
101 trailer = gssEapLocateIov(iov, iov_count, GSS_IOV_BUFFER_TYPE_TRAILER);
103 acceptorFlag = CTX_IS_INITIATOR(ctx) ? TOK_FLAG_SENDER_IS_ACCEPTOR : 0;
104 keyUsage = (toktype == TOK_TYPE_WRAP
105 ? (!CTX_IS_INITIATOR(ctx)
106 ? KEY_USAGE_INITIATOR_SEAL
107 : KEY_USAGE_ACCEPTOR_SEAL)
108 : (!CTX_IS_INITIATOR(ctx)
109 ? KEY_USAGE_INITIATOR_SIGN
110 : KEY_USAGE_ACCEPTOR_SIGN));
112 gssEapIovMessageLength(iov, iov_count, &dataLen, &assocDataLen);
114 ptr = (unsigned char *)header->buffer.value;
116 if (header->buffer.length < 16) {
118 return GSS_S_DEFECTIVE_TOKEN;
121 if ((ptr[2] & TOK_FLAG_SENDER_IS_ACCEPTOR) != acceptorFlag) {
122 return GSS_S_BAD_SIG;
125 if (ptr[2] & TOK_FLAG_ACCEPTOR_SUBKEY) {
126 return GSS_S_BAD_SIG;
129 if (toktype == TOK_TYPE_WRAP) {
130 unsigned int krbTrailerLen;
132 if (load_uint16_be(ptr) != TOK_TYPE_WRAP)
134 conf_flag = ((ptr[2] & TOK_FLAG_WRAP_CONFIDENTIAL) != 0);
137 ec = load_uint16_be(ptr + 4);
138 rrc = load_uint16_be(ptr + 6);
139 seqnum = load_uint64_be(ptr + 8);
141 code = krb5_c_crypto_length(krbContext,
143 conf_flag ? KRB5_CRYPTO_TYPE_TRAILER :
144 KRB5_CRYPTO_TYPE_CHECKSUM,
148 return GSS_S_FAILURE;
152 if (trailer == NULL) {
153 size_t desired_rrc = krbTrailerLen;
156 desired_rrc += 16; /* E(Header) */
158 if ((ctx->gssFlags & GSS_C_DCE_STYLE) == 0)
162 /* According to MS, we only need to deal with a fixed RRC for DCE */
163 if (rrc != desired_rrc)
165 } else if (rrc != 0) {
170 unsigned char *althdr;
173 code = gssEapDecrypt(krbContext,
174 ((ctx->gssFlags & GSS_C_DCE_STYLE) != 0),
175 ec, rrc, &ctx->rfc3961Key,
176 keyUsage, 0, iov, iov_count);
179 return GSS_S_BAD_SIG;
182 /* Validate header integrity */
184 althdr = (unsigned char *)header->buffer.value + 16 + ec;
186 althdr = (unsigned char *)trailer->buffer.value + ec;
188 if (load_uint16_be(althdr) != TOK_TYPE_WRAP
189 || althdr[2] != ptr[2]
190 || althdr[3] != ptr[3]
191 || memcmp(althdr + 8, ptr + 8, 8) != 0) {
193 return GSS_S_BAD_SIG;
196 /* Verify checksum: note EC is checksum size here, not padding */
197 if (ec != krbTrailerLen)
200 /* Zero EC, RRC before computing checksum */
201 store_uint16_be(0, ptr + 4);
202 store_uint16_be(0, ptr + 6);
204 code = gssEapVerify(krbContext, cksumtype, rrc,
205 &ctx->rfc3961Key, keyUsage,
206 iov, iov_count, &valid);
207 if (code != 0 || valid == FALSE) {
209 return GSS_S_BAD_SIG;
213 code = sequenceCheck(&ctx->seqState, seqnum);
214 } else if (toktype == TOK_TYPE_MIC) {
215 if (load_uint16_be(ptr) != TOK_TYPE_MIC)
221 seqnum = load_uint64_be(ptr + 8);
223 code = gssEapVerify(krbContext, cksumtype, 0,
224 &ctx->rfc3961Key, keyUsage,
225 iov, iov_count, &valid);
226 if (code != 0 || valid == FALSE) {
228 return GSS_S_BAD_SIG;
230 code = sequenceCheck(&ctx->seqState, seqnum);
231 } else if (toktype == TOK_TYPE_DELETE_CONTEXT) {
232 if (load_uint16_be(ptr) != TOK_TYPE_DELETE_CONTEXT)
241 if (conf_state != NULL)
242 *conf_state = conf_flag;
249 return GSS_S_DEFECTIVE_TOKEN;
253 rotateLeft(void *ptr, size_t bufsiz, size_t rc)
263 tbuf = GSSEAP_MALLOC(rc);
267 memcpy(tbuf, ptr, rc);
268 memmove(ptr, (char *)ptr + rc, bufsiz - rc);
269 memcpy((char *)ptr + bufsiz - rc, tbuf, rc);
276 * Split a STREAM | SIGN_DATA | DATA into
277 * HEADER | SIGN_DATA | DATA | PADDING | TRAILER
280 unwrapStream(OM_uint32 *minor,
283 gss_qop_t *qop_state,
284 gss_iov_buffer_desc *iov,
286 enum gss_eap_token_type toktype)
289 OM_uint32 code = 0, major = GSS_S_FAILURE;
290 krb5_context krbContext;
291 int conf_req_flag, toktype2;
293 gss_iov_buffer_desc *tiov = NULL;
294 gss_iov_buffer_t stream, data = NULL;
295 gss_iov_buffer_t theader, tdata = NULL, tpadding, ttrailer;
297 GSSEAP_KRB_INIT(&krbContext);
299 assert(toktype == TOK_TYPE_WRAP);
301 if (toktype != TOK_TYPE_WRAP || (ctx->gssFlags & GSS_C_DCE_STYLE)) {
306 stream = gssEapLocateIov(iov, iov_count, GSS_IOV_BUFFER_TYPE_STREAM);
307 assert(stream != NULL);
309 if (stream->buffer.length < 16) {
310 major = GSS_S_DEFECTIVE_TOKEN;
314 ptr = (unsigned char *)stream->buffer.value;
315 toktype2 = load_uint16_be(ptr);
318 tiov = (gss_iov_buffer_desc *)GSSEAP_CALLOC((size_t)iov_count + 2,
319 sizeof(gss_iov_buffer_desc));
326 theader = &tiov[i++];
327 theader->type = GSS_IOV_BUFFER_TYPE_HEADER;
328 theader->buffer.value = stream->buffer.value;
329 theader->buffer.length = 16;
331 /* n[SIGN_DATA] | DATA | m[SIGN_DATA] */
332 for (j = 0; j < iov_count; j++) {
333 OM_uint32 type = GSS_IOV_BUFFER_TYPE(iov[j].type);
335 if (type == GSS_IOV_BUFFER_TYPE_DATA) {
337 /* only a single DATA buffer can appear */
345 if (type == GSS_IOV_BUFFER_TYPE_DATA ||
346 type == GSS_IOV_BUFFER_TYPE_SIGN_ONLY)
351 /* a single DATA buffer must be present */
356 /* PADDING | TRAILER */
357 tpadding = &tiov[i++];
358 tpadding->type = GSS_IOV_BUFFER_TYPE_PADDING;
359 tpadding->buffer.length = 0;
360 tpadding->buffer.value = NULL;
362 ttrailer = &tiov[i++];
363 ttrailer->type = GSS_IOV_BUFFER_TYPE_TRAILER;
367 unsigned int krbHeaderLen = 0;
368 unsigned int krbTrailerLen = 0;
370 conf_req_flag = ((ptr[0] & TOK_FLAG_WRAP_CONFIDENTIAL) != 0);
371 ec = conf_req_flag ? load_uint16_be(ptr + 2) : 0;
372 rrc = load_uint16_be(ptr + 4);
375 code = rotateLeft((unsigned char *)stream->buffer.value + 16,
376 stream->buffer.length - 16, rrc);
379 store_uint16_be(0, ptr + 4); /* set RRC to zero */
383 code = krb5_c_crypto_length(krbContext, ctx->encryptionType,
384 KRB5_CRYPTO_TYPE_HEADER, &krbHeaderLen);
387 theader->buffer.length += krbHeaderLen; /* length validated later */
390 /* no PADDING for CFX, EC is used instead */
391 code = krb5_c_crypto_length(krbContext, ctx->encryptionType,
393 ? KRB5_CRYPTO_TYPE_TRAILER
394 : KRB5_CRYPTO_TYPE_CHECKSUM,
399 ttrailer->buffer.length = ec + (conf_req_flag ? 16 : 0 /* E(Header) */) +
401 ttrailer->buffer.value = (unsigned char *)stream->buffer.value +
402 stream->buffer.length - ttrailer->buffer.length;
405 /* IOV: -----------0-------------+---1---+--2--+----------------3--------------*/
406 /* Old: GSS-Header | Conf | Data | Pad | */
407 /* CFX: GSS-Header | Kerb-Header | Data | | EC | E(Header) | Kerb-Trailer */
408 /* GSS: -------GSS-HEADER--------+-DATA--+-PAD-+----------GSS-TRAILER----------*/
410 /* validate lengths */
411 if (stream->buffer.length < theader->buffer.length +
412 tpadding->buffer.length +
413 ttrailer->buffer.length) {
414 code = KRB5_BAD_MSIZE;
415 major = GSS_S_DEFECTIVE_TOKEN;
420 tdata->buffer.length = stream->buffer.length - ttrailer->buffer.length -
421 tpadding->buffer.length - theader->buffer.length;
423 assert(data != NULL);
425 if (data->type & GSS_IOV_BUFFER_FLAG_ALLOCATE) {
426 code = gssEapAllocIov(tdata, tdata->buffer.length);
430 memcpy(tdata->buffer.value,
431 (unsigned char *)stream->buffer.value + theader->buffer.length,
432 tdata->buffer.length);
434 tdata->buffer.value = (unsigned char *)stream->buffer.value +
435 theader->buffer.length;
438 assert(i <= iov_count + 2);
440 major = unwrapToken(&code, ctx, conf_state, qop_state,
442 if (major == GSS_S_COMPLETE) {
444 } else if (tdata->type & GSS_IOV_BUFFER_FLAG_ALLOCATED) {
447 gss_release_buffer(&tmp, &tdata->buffer);
448 tdata->type &= ~(GSS_IOV_BUFFER_FLAG_ALLOCATED);
461 gssEapUnwrapOrVerifyMIC(OM_uint32 *minor,
464 gss_qop_t *qop_state,
465 gss_iov_buffer_desc *iov,
467 enum gss_eap_token_type toktype)
471 if (!CTX_IS_ESTABLISHED(ctx))
472 return GSS_S_NO_CONTEXT;
474 if (gssEapLocateIov(iov, iov_count, GSS_IOV_BUFFER_TYPE_STREAM) != NULL) {
475 major = unwrapStream(minor, ctx, conf_state, qop_state,
476 iov, iov_count, toktype);
478 major = unwrapToken(minor, ctx, conf_state, qop_state,
479 iov, iov_count, toktype);
486 gss_unwrap_iov(OM_uint32 *minor,
489 gss_qop_t *qop_state,
490 gss_iov_buffer_desc *iov,
493 return gssEapUnwrapOrVerifyMIC(minor, ctx, conf_state, qop_state,
494 iov, iov_count, TOK_TYPE_WRAP);