2 * Copyright (c) 2011, 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.
57 * Message protection services: unwrap with scatter-gather API.
60 #include "gssapiP_eap.h"
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)
68 unwrapToken(OM_uint32 *minor,
70 #ifdef HAVE_HEIMDAL_VERSION
71 krb5_crypto krbCrypto,
73 krb5_keyblock *unused GSSEAP_UNUSED,
77 gss_iov_buffer_desc *iov,
79 enum gss_eap_token_type toktype)
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;
86 unsigned char *ptr = NULL;
89 size_t dataLen, assocDataLen;
93 krb5_context krbContext;
94 #ifdef HAVE_HEIMDAL_VERSION
95 int freeCrypto = (krbCrypto == NULL);
98 GSSEAP_KRB_INIT(&krbContext);
102 if (qop_state != NULL)
103 *qop_state = GSS_C_QOP_DEFAULT;
105 header = gssEapLocateIov(iov, iov_count, GSS_IOV_BUFFER_TYPE_HEADER);
106 assert(header != NULL);
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;
115 trailer = gssEapLocateIov(iov, iov_count, GSS_IOV_BUFFER_TYPE_TRAILER);
117 flags = rfc4121Flags(ctx, TRUE);
119 if (toktype == TOK_TYPE_WRAP) {
120 keyUsage = !CTX_IS_INITIATOR(ctx)
121 ? KEY_USAGE_INITIATOR_SEAL
122 : KEY_USAGE_ACCEPTOR_SEAL;
124 keyUsage = !CTX_IS_INITIATOR(ctx)
125 ? KEY_USAGE_INITIATOR_SIGN
126 : KEY_USAGE_ACCEPTOR_SIGN;
129 gssEapIovMessageLength(iov, iov_count, &dataLen, &assocDataLen);
131 ptr = (unsigned char *)header->buffer.value;
133 if (header->buffer.length < 16) {
134 code = GSSEAP_TOK_TRUNC;
135 major = GSS_S_DEFECTIVE_TOKEN;
139 if ((ptr[2] & flags) != flags) {
140 code = GSSEAP_BAD_DIRECTION;
141 major = GSS_S_BAD_SIG;
145 #ifdef HAVE_HEIMDAL_VERSION
146 if (krbCrypto == NULL) {
147 code = krb5_crypto_init(krbContext, &ctx->rfc3961Key,
148 ETYPE_NULL, &krbCrypto);
154 if (toktype == TOK_TYPE_WRAP) {
155 size_t krbTrailerLen;
157 if (load_uint16_be(ptr) != TOK_TYPE_WRAP)
159 conf_flag = ((ptr[2] & TOK_FLAG_WRAP_CONFIDENTIAL) != 0);
162 ec = load_uint16_be(ptr + 4);
163 rrc = load_uint16_be(ptr + 6);
164 seqnum = load_uint64_be(ptr + 8);
166 code = krbCryptoLength(krbContext, KRB_CRYPTO_CONTEXT(ctx),
167 conf_flag ? KRB5_CRYPTO_TYPE_TRAILER :
168 KRB5_CRYPTO_TYPE_CHECKSUM,
174 if (trailer == NULL) {
175 size_t desired_rrc = krbTrailerLen;
178 desired_rrc += 16; /* E(Header) */
180 if ((ctx->gssFlags & GSS_C_DCE_STYLE) == 0)
184 /* According to MS, we only need to deal with a fixed RRC for DCE */
185 if (rrc != desired_rrc)
187 } else if (rrc != 0) {
192 unsigned char *althdr;
195 code = gssEapDecrypt(krbContext,
196 ((ctx->gssFlags & GSS_C_DCE_STYLE) != 0),
197 ec, rrc, KRB_CRYPTO_CONTEXT(ctx), keyUsage,
200 major = GSS_S_BAD_SIG;
204 /* Validate header integrity */
206 althdr = (unsigned char *)header->buffer.value + 16 + ec;
208 althdr = (unsigned char *)trailer->buffer.value + ec;
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;
219 /* Verify checksum: note EC is checksum size here, not padding */
220 if (ec != krbTrailerLen)
223 /* Zero EC, RRC before computing checksum */
224 store_uint16_be(0, ptr + 4);
225 store_uint16_be(0, ptr + 6);
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;
236 code = sequenceCheck(minor, &ctx->seqState, seqnum);
237 } else if (toktype == TOK_TYPE_MIC) {
238 if (load_uint16_be(ptr) != toktype)
244 seqnum = load_uint64_be(ptr + 8);
246 code = gssEapVerify(krbContext, ctx->checksumType, 0,
247 KRB_CRYPTO_CONTEXT(ctx), keyUsage,
248 iov, iov_count, &valid);
249 if (code != 0 || valid == FALSE) {
250 major = GSS_S_BAD_SIG;
253 code = sequenceCheck(minor, &ctx->seqState, seqnum);
254 } else if (toktype == TOK_TYPE_DELETE_CONTEXT) {
255 if (load_uint16_be(ptr) != TOK_TYPE_DELETE_CONTEXT)
262 if (conf_state != NULL)
263 *conf_state = conf_flag;
266 major = GSS_S_COMPLETE;
270 code = GSSEAP_BAD_WRAP_TOKEN;
271 major = GSS_S_DEFECTIVE_TOKEN;
275 #ifdef HAVE_HEIMDAL_VERSION
276 if (freeCrypto && krbCrypto != NULL)
277 krb5_crypto_destroy(krbContext, krbCrypto);
284 rotateLeft(void *ptr, size_t bufsiz, size_t rc)
294 tbuf = GSSEAP_MALLOC(rc);
298 memcpy(tbuf, ptr, rc);
299 memmove(ptr, (char *)ptr + rc, bufsiz - rc);
300 memcpy((char *)ptr + bufsiz - rc, tbuf, rc);
307 * Split a STREAM | SIGN_DATA | DATA into
308 * HEADER | SIGN_DATA | DATA | PADDING | TRAILER
311 unwrapStream(OM_uint32 *minor,
314 gss_qop_t *qop_state,
315 gss_iov_buffer_desc *iov,
317 enum gss_eap_token_type toktype)
320 OM_uint32 code = 0, major = GSS_S_FAILURE;
321 krb5_context krbContext;
322 int conf_req_flag, toktype2;
324 gss_iov_buffer_desc *tiov = NULL;
325 gss_iov_buffer_t stream, data = NULL;
326 gss_iov_buffer_t theader, tdata = NULL, tpadding, ttrailer;
327 #ifdef HAVE_HEIMDAL_VERSION
328 krb5_crypto krbCrypto = NULL;
331 GSSEAP_KRB_INIT(&krbContext);
333 assert(toktype == TOK_TYPE_WRAP);
335 if (toktype != TOK_TYPE_WRAP) {
336 code = GSSEAP_WRONG_TOK_ID;
340 stream = gssEapLocateIov(iov, iov_count, GSS_IOV_BUFFER_TYPE_STREAM);
341 assert(stream != NULL);
343 if (stream->buffer.length < 16) {
344 major = GSS_S_DEFECTIVE_TOKEN;
348 ptr = (unsigned char *)stream->buffer.value;
349 toktype2 = load_uint16_be(ptr);
352 tiov = (gss_iov_buffer_desc *)GSSEAP_CALLOC((size_t)iov_count + 2,
353 sizeof(gss_iov_buffer_desc));
360 theader = &tiov[i++];
361 theader->type = GSS_IOV_BUFFER_TYPE_HEADER;
362 theader->buffer.value = stream->buffer.value;
363 theader->buffer.length = 16;
365 /* n[SIGN_DATA] | DATA | m[SIGN_DATA] */
366 for (j = 0; j < iov_count; j++) {
367 OM_uint32 type = GSS_IOV_BUFFER_TYPE(iov[j].type);
369 if (type == GSS_IOV_BUFFER_TYPE_DATA) {
371 /* only a single DATA buffer can appear */
372 code = GSSEAP_BAD_STREAM_IOV;
379 if (type == GSS_IOV_BUFFER_TYPE_DATA ||
380 type == GSS_IOV_BUFFER_TYPE_SIGN_ONLY)
385 /* a single DATA buffer must be present */
386 code = GSSEAP_BAD_STREAM_IOV;
390 /* PADDING | TRAILER */
391 tpadding = &tiov[i++];
392 tpadding->type = GSS_IOV_BUFFER_TYPE_PADDING;
393 tpadding->buffer.length = 0;
394 tpadding->buffer.value = NULL;
396 ttrailer = &tiov[i++];
397 ttrailer->type = GSS_IOV_BUFFER_TYPE_TRAILER;
399 #ifdef HAVE_HEIMDAL_VERSION
400 code = krb5_crypto_init(krbContext, &ctx->rfc3961Key, ETYPE_NULL, &krbCrypto);
407 size_t krbHeaderLen = 0;
408 size_t krbTrailerLen = 0;
410 conf_req_flag = ((ptr[0] & TOK_FLAG_WRAP_CONFIDENTIAL) != 0);
411 ec = conf_req_flag ? load_uint16_be(ptr + 2) : 0;
412 rrc = load_uint16_be(ptr + 4);
415 code = rotateLeft((unsigned char *)stream->buffer.value + 16,
416 stream->buffer.length - 16, rrc);
419 store_uint16_be(0, ptr + 4); /* set RRC to zero */
423 code = krbCryptoLength(krbContext, KRB_CRYPTO_CONTEXT(ctx),
424 KRB5_CRYPTO_TYPE_HEADER, &krbHeaderLen);
427 theader->buffer.length += krbHeaderLen; /* length validated later */
430 /* no PADDING for CFX, EC is used instead */
431 code = krbCryptoLength(krbContext, KRB_CRYPTO_CONTEXT(ctx),
433 ? KRB5_CRYPTO_TYPE_TRAILER
434 : KRB5_CRYPTO_TYPE_CHECKSUM,
439 ttrailer->buffer.length = ec + (conf_req_flag ? 16 : 0 /* E(Header) */) +
441 ttrailer->buffer.value = (unsigned char *)stream->buffer.value +
442 stream->buffer.length - ttrailer->buffer.length;
445 /* IOV: -----------0-------------+---1---+--2--+----------------3--------------*/
446 /* CFX: GSS-Header | Kerb-Header | Data | | EC | E(Header) | Kerb-Trailer */
447 /* GSS: -------GSS-HEADER--------+-DATA--+-PAD-+----------GSS-TRAILER----------*/
449 /* validate lengths */
450 if (stream->buffer.length < theader->buffer.length +
451 tpadding->buffer.length +
452 ttrailer->buffer.length) {
453 major = GSS_S_DEFECTIVE_TOKEN;
454 code = GSSEAP_TOK_TRUNC;
459 tdata->buffer.length = stream->buffer.length - ttrailer->buffer.length -
460 tpadding->buffer.length - theader->buffer.length;
462 assert(data != NULL);
464 if (data->type & GSS_IOV_BUFFER_FLAG_ALLOCATE) {
465 code = gssEapAllocIov(tdata, tdata->buffer.length);
469 memcpy(tdata->buffer.value,
470 (unsigned char *)stream->buffer.value + theader->buffer.length,
471 tdata->buffer.length);
473 tdata->buffer.value = (unsigned char *)stream->buffer.value +
474 theader->buffer.length;
477 assert(i <= iov_count + 2);
479 major = unwrapToken(&code, ctx, KRB_CRYPTO_CONTEXT(ctx),
480 conf_state, qop_state, tiov, i, toktype);
481 if (major == GSS_S_COMPLETE) {
483 } else if (tdata->type & GSS_IOV_BUFFER_FLAG_ALLOCATED) {
486 gss_release_buffer(&tmp, &tdata->buffer);
487 tdata->type &= ~(GSS_IOV_BUFFER_FLAG_ALLOCATED);
493 #ifdef HAVE_HEIMDAL_VERSION
494 if (krbCrypto != NULL)
495 krb5_crypto_destroy(krbContext, krbCrypto);
504 gssEapUnwrapOrVerifyMIC(OM_uint32 *minor,
507 gss_qop_t *qop_state,
508 gss_iov_buffer_desc *iov,
510 enum gss_eap_token_type toktype)
514 if (ctx->encryptionType == ENCTYPE_NULL) {
515 *minor = GSSEAP_KEY_UNAVAILABLE;
516 return GSS_S_UNAVAILABLE;
519 if (gssEapLocateIov(iov, iov_count, GSS_IOV_BUFFER_TYPE_STREAM) != NULL) {
520 major = unwrapStream(minor, ctx, conf_state, qop_state,
521 iov, iov_count, toktype);
523 major = unwrapToken(minor, ctx,
524 NULL, /* krbCrypto */
525 conf_state, qop_state,
526 iov, iov_count, toktype);
533 gss_unwrap_iov(OM_uint32 *minor,
536 gss_qop_t *qop_state,
537 gss_iov_buffer_desc *iov,
542 if (ctx == GSS_C_NO_CONTEXT) {
544 return GSS_S_CALL_INACCESSIBLE_READ | GSS_S_NO_CONTEXT;
549 GSSEAP_MUTEX_LOCK(&ctx->mutex);
551 if (!CTX_IS_ESTABLISHED(ctx)) {
552 major = GSS_S_NO_CONTEXT;
553 *minor = GSSEAP_CONTEXT_INCOMPLETE;
557 major = gssEapUnwrapOrVerifyMIC(minor, ctx, conf_state, qop_state,
558 iov, iov_count, TOK_TYPE_WRAP);
559 if (GSS_ERROR(major))
563 GSSEAP_MUTEX_UNLOCK(&ctx->mutex);