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.
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,
72 gss_iov_buffer_desc *iov,
74 enum gss_eap_token_type toktype)
77 gss_iov_buffer_t header;
78 gss_iov_buffer_t padding;
79 gss_iov_buffer_t trailer;
81 unsigned char *ptr = NULL;
84 size_t dataLen, assocDataLen;
88 krb5_context krbContext;
90 GSSEAP_KRB_INIT(&krbContext);
94 if (qop_state != NULL)
95 *qop_state = GSS_C_QOP_DEFAULT;
97 header = gssEapLocateIov(iov, iov_count, GSS_IOV_BUFFER_TYPE_HEADER);
98 assert(header != NULL);
100 padding = gssEapLocateIov(iov, iov_count, GSS_IOV_BUFFER_TYPE_PADDING);
101 if (padding != NULL && padding->buffer.length != 0) {
102 *minor = GSSEAP_BAD_PADDING_IOV;
103 return GSS_S_DEFECTIVE_TOKEN;
106 trailer = gssEapLocateIov(iov, iov_count, GSS_IOV_BUFFER_TYPE_TRAILER);
108 flags = rfc4121Flags(ctx, TRUE);
110 if (toktype == TOK_TYPE_WRAP) {
111 keyUsage = !CTX_IS_INITIATOR(ctx)
112 ? KEY_USAGE_INITIATOR_SEAL
113 : KEY_USAGE_ACCEPTOR_SEAL;
115 keyUsage = !CTX_IS_INITIATOR(ctx)
116 ? KEY_USAGE_INITIATOR_SIGN
117 : KEY_USAGE_ACCEPTOR_SIGN;
120 gssEapIovMessageLength(iov, iov_count, &dataLen, &assocDataLen);
122 ptr = (unsigned char *)header->buffer.value;
124 if (header->buffer.length < 16) {
125 *minor = GSSEAP_TOK_TRUNC;
126 return GSS_S_DEFECTIVE_TOKEN;
129 if ((ptr[2] & flags) != flags) {
130 *minor = GSSEAP_BAD_DIRECTION;
131 return GSS_S_BAD_SIG;
134 if (toktype == TOK_TYPE_WRAP) {
135 unsigned int krbTrailerLen;
137 if (load_uint16_be(ptr) != TOK_TYPE_WRAP)
139 conf_flag = ((ptr[2] & TOK_FLAG_WRAP_CONFIDENTIAL) != 0);
142 ec = load_uint16_be(ptr + 4);
143 rrc = load_uint16_be(ptr + 6);
144 seqnum = load_uint64_be(ptr + 8);
146 code = krb5_c_crypto_length(krbContext,
148 conf_flag ? KRB5_CRYPTO_TYPE_TRAILER :
149 KRB5_CRYPTO_TYPE_CHECKSUM,
153 return GSS_S_FAILURE;
157 if (trailer == NULL) {
158 size_t desired_rrc = krbTrailerLen;
161 desired_rrc += 16; /* E(Header) */
163 if ((ctx->gssFlags & GSS_C_DCE_STYLE) == 0)
167 /* According to MS, we only need to deal with a fixed RRC for DCE */
168 if (rrc != desired_rrc)
170 } else if (rrc != 0) {
175 unsigned char *althdr;
178 code = gssEapDecrypt(krbContext,
179 ((ctx->gssFlags & GSS_C_DCE_STYLE) != 0),
180 ec, rrc, &ctx->rfc3961Key,
181 keyUsage, 0, iov, iov_count);
184 return GSS_S_BAD_SIG;
187 /* Validate header integrity */
189 althdr = (unsigned char *)header->buffer.value + 16 + ec;
191 althdr = (unsigned char *)trailer->buffer.value + ec;
193 if (load_uint16_be(althdr) != TOK_TYPE_WRAP
194 || althdr[2] != ptr[2]
195 || althdr[3] != ptr[3]
196 || memcmp(althdr + 8, ptr + 8, 8) != 0) {
197 *minor = GSSEAP_BAD_WRAP_TOKEN;
198 return GSS_S_BAD_SIG;
201 /* Verify checksum: note EC is checksum size here, not padding */
202 if (ec != krbTrailerLen)
205 /* Zero EC, RRC before computing checksum */
206 store_uint16_be(0, ptr + 4);
207 store_uint16_be(0, ptr + 6);
209 code = gssEapVerify(krbContext, ctx->checksumType, rrc,
210 &ctx->rfc3961Key, keyUsage,
211 iov, iov_count, &valid);
212 if (code != 0 || valid == FALSE) {
214 return GSS_S_BAD_SIG;
218 code = sequenceCheck(minor, &ctx->seqState, seqnum);
219 } else if (toktype == TOK_TYPE_MIC) {
220 if (load_uint16_be(ptr) != toktype)
226 seqnum = load_uint64_be(ptr + 8);
228 code = gssEapVerify(krbContext, ctx->checksumType, 0,
229 &ctx->rfc3961Key, keyUsage,
230 iov, iov_count, &valid);
231 if (code != 0 || valid == FALSE) {
233 return GSS_S_BAD_SIG;
235 code = sequenceCheck(minor, &ctx->seqState, seqnum);
236 } else if (toktype == TOK_TYPE_DELETE_CONTEXT) {
237 if (load_uint16_be(ptr) != TOK_TYPE_DELETE_CONTEXT)
246 if (conf_state != NULL)
247 *conf_state = conf_flag;
252 *minor = GSSEAP_BAD_WRAP_TOKEN;
254 return GSS_S_DEFECTIVE_TOKEN;
258 rotateLeft(void *ptr, size_t bufsiz, size_t rc)
268 tbuf = GSSEAP_MALLOC(rc);
272 memcpy(tbuf, ptr, rc);
273 memmove(ptr, (char *)ptr + rc, bufsiz - rc);
274 memcpy((char *)ptr + bufsiz - rc, tbuf, rc);
281 * Split a STREAM | SIGN_DATA | DATA into
282 * HEADER | SIGN_DATA | DATA | PADDING | TRAILER
285 unwrapStream(OM_uint32 *minor,
288 gss_qop_t *qop_state,
289 gss_iov_buffer_desc *iov,
291 enum gss_eap_token_type toktype)
294 OM_uint32 code = 0, major = GSS_S_FAILURE;
295 krb5_context krbContext;
296 int conf_req_flag, toktype2;
298 gss_iov_buffer_desc *tiov = NULL;
299 gss_iov_buffer_t stream, data = NULL;
300 gss_iov_buffer_t theader, tdata = NULL, tpadding, ttrailer;
302 GSSEAP_KRB_INIT(&krbContext);
304 assert(toktype == TOK_TYPE_WRAP);
306 if (toktype != TOK_TYPE_WRAP) {
307 code = GSSEAP_WRONG_TOK_ID;
311 stream = gssEapLocateIov(iov, iov_count, GSS_IOV_BUFFER_TYPE_STREAM);
312 assert(stream != NULL);
314 if (stream->buffer.length < 16) {
315 major = GSS_S_DEFECTIVE_TOKEN;
319 ptr = (unsigned char *)stream->buffer.value;
320 toktype2 = load_uint16_be(ptr);
323 tiov = (gss_iov_buffer_desc *)GSSEAP_CALLOC((size_t)iov_count + 2,
324 sizeof(gss_iov_buffer_desc));
331 theader = &tiov[i++];
332 theader->type = GSS_IOV_BUFFER_TYPE_HEADER;
333 theader->buffer.value = stream->buffer.value;
334 theader->buffer.length = 16;
336 /* n[SIGN_DATA] | DATA | m[SIGN_DATA] */
337 for (j = 0; j < iov_count; j++) {
338 OM_uint32 type = GSS_IOV_BUFFER_TYPE(iov[j].type);
340 if (type == GSS_IOV_BUFFER_TYPE_DATA) {
342 /* only a single DATA buffer can appear */
343 code = GSSEAP_BAD_STREAM_IOV;
350 if (type == GSS_IOV_BUFFER_TYPE_DATA ||
351 type == GSS_IOV_BUFFER_TYPE_SIGN_ONLY)
356 /* a single DATA buffer must be present */
357 code = GSSEAP_BAD_STREAM_IOV;
361 /* PADDING | TRAILER */
362 tpadding = &tiov[i++];
363 tpadding->type = GSS_IOV_BUFFER_TYPE_PADDING;
364 tpadding->buffer.length = 0;
365 tpadding->buffer.value = NULL;
367 ttrailer = &tiov[i++];
368 ttrailer->type = GSS_IOV_BUFFER_TYPE_TRAILER;
372 unsigned int krbHeaderLen = 0;
373 unsigned int krbTrailerLen = 0;
375 conf_req_flag = ((ptr[0] & TOK_FLAG_WRAP_CONFIDENTIAL) != 0);
376 ec = conf_req_flag ? load_uint16_be(ptr + 2) : 0;
377 rrc = load_uint16_be(ptr + 4);
380 code = rotateLeft((unsigned char *)stream->buffer.value + 16,
381 stream->buffer.length - 16, rrc);
384 store_uint16_be(0, ptr + 4); /* set RRC to zero */
388 code = krb5_c_crypto_length(krbContext, ctx->encryptionType,
389 KRB5_CRYPTO_TYPE_HEADER, &krbHeaderLen);
392 theader->buffer.length += krbHeaderLen; /* length validated later */
395 /* no PADDING for CFX, EC is used instead */
396 code = krb5_c_crypto_length(krbContext, ctx->encryptionType,
398 ? KRB5_CRYPTO_TYPE_TRAILER
399 : KRB5_CRYPTO_TYPE_CHECKSUM,
404 ttrailer->buffer.length = ec + (conf_req_flag ? 16 : 0 /* E(Header) */) +
406 ttrailer->buffer.value = (unsigned char *)stream->buffer.value +
407 stream->buffer.length - ttrailer->buffer.length;
410 /* IOV: -----------0-------------+---1---+--2--+----------------3--------------*/
411 /* CFX: GSS-Header | Kerb-Header | Data | | EC | E(Header) | Kerb-Trailer */
412 /* GSS: -------GSS-HEADER--------+-DATA--+-PAD-+----------GSS-TRAILER----------*/
414 /* validate lengths */
415 if (stream->buffer.length < theader->buffer.length +
416 tpadding->buffer.length +
417 ttrailer->buffer.length) {
418 major = GSS_S_DEFECTIVE_TOKEN;
419 code = GSSEAP_TOK_TRUNC;
424 tdata->buffer.length = stream->buffer.length - ttrailer->buffer.length -
425 tpadding->buffer.length - theader->buffer.length;
427 assert(data != NULL);
429 if (data->type & GSS_IOV_BUFFER_FLAG_ALLOCATE) {
430 code = gssEapAllocIov(tdata, tdata->buffer.length);
434 memcpy(tdata->buffer.value,
435 (unsigned char *)stream->buffer.value + theader->buffer.length,
436 tdata->buffer.length);
438 tdata->buffer.value = (unsigned char *)stream->buffer.value +
439 theader->buffer.length;
442 assert(i <= iov_count + 2);
444 major = unwrapToken(&code, ctx, conf_state, qop_state,
446 if (major == GSS_S_COMPLETE) {
448 } else if (tdata->type & GSS_IOV_BUFFER_FLAG_ALLOCATED) {
451 gss_release_buffer(&tmp, &tdata->buffer);
452 tdata->type &= ~(GSS_IOV_BUFFER_FLAG_ALLOCATED);
465 gssEapUnwrapOrVerifyMIC(OM_uint32 *minor,
468 gss_qop_t *qop_state,
469 gss_iov_buffer_desc *iov,
471 enum gss_eap_token_type toktype)
475 if (ctx->encryptionType == ENCTYPE_NULL) {
476 *minor = GSSEAP_KEY_UNAVAILABLE;
477 return GSS_S_UNAVAILABLE;
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);
484 major = unwrapToken(minor, ctx, conf_state, qop_state,
485 iov, iov_count, toktype);
492 gss_unwrap_iov(OM_uint32 *minor,
495 gss_qop_t *qop_state,
496 gss_iov_buffer_desc *iov,
501 if (ctx == GSS_C_NO_CONTEXT) {
503 return GSS_S_CALL_INACCESSIBLE_READ | GSS_S_NO_CONTEXT;
508 GSSEAP_MUTEX_LOCK(&ctx->mutex);
510 if (!CTX_IS_ESTABLISHED(ctx)) {
511 major = GSS_S_NO_CONTEXT;
512 *minor = GSSEAP_CONTEXT_INCOMPLETE;
516 major = gssEapUnwrapOrVerifyMIC(minor, ctx, conf_state, qop_state,
517 iov, iov_count, TOK_TYPE_WRAP);
518 if (GSS_ERROR(major))
522 GSSEAP_MUTEX_UNLOCK(&ctx->mutex);